blob: 34d48950f9bcb2c4a03bba490733d9fa1ddea66b [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
Steven Cooreman75b74362020-07-28 14:30:13 +0200608 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
609 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200610 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
Steven Cooreman75b74362020-07-28 14:30:13 +0200673 /* Re-export the data to PSA export format, such that we can store export
674 * representation in the key slot. Export representation in case of RSA is
675 * the smallest representation that's allowed as input, so a straight-up
676 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200677 output = mbedtls_calloc( 1, data_length );
678
679 if( output == NULL )
680 {
681 status = PSA_ERROR_INSUFFICIENT_MEMORY;
682 goto exit;
683 }
684
Steven Cooremana01795d2020-07-24 22:48:15 +0200685 status = psa_export_rsa_key( slot->attr.type,
686 &rsa,
687 output,
688 data_length,
689 &data_length);
690
691exit:
692 /* Always free the RSA object */
693 mbedtls_rsa_free( &rsa );
694
695 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000696 if( status != PSA_SUCCESS )
697 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200698 mbedtls_free( output );
699 slot->data.key.data = NULL;
700 slot->data.key.bytes = 0;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000701 return( status );
702 }
703
Steven Cooremana01795d2020-07-24 22:48:15 +0200704 /* On success, store the allocated export-formatted key. */
705 slot->data.key.data = output;
706 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000707
708 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200709}
Steven Cooremana01795d2020-07-24 22:48:15 +0200710#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200711
Jaeden Ameroccdce902019-01-10 11:42:27 +0000712#if defined(MBEDTLS_ECP_C)
Steven Cooremanacda8342020-07-24 23:09:52 +0200713/* Load the key slot contents into an mbedTLS internal representation object.
714 * Note: caller is responsible for freeing the object properly */
715static psa_status_t psa_load_ecp_representation( const psa_key_slot_t *slot,
716 mbedtls_ecp_keypair *ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100717{
718 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200719 size_t data_length = slot->data.key.bytes;
720 psa_status_t status;
721 mbedtls_ecp_keypair_init( ecp );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100722
Steven Cooremanacda8342020-07-24 23:09:52 +0200723 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100724 {
725 /* A public key is represented as:
726 * - The byte 0x04;
727 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
728 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
729 * So its data length is 2m+1 where n is the key size in bits.
730 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200731 if( ( slot->data.key.bytes & 1 ) == 0 )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100732 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +0200733 data_length = slot->data.key.bytes / 2;
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100734 }
735
Gilles Peskine4cd32772019-12-02 20:49:42 +0100736 /* Load the group. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200737 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type),
738 data_length );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100739 if( grp_id == MBEDTLS_ECP_DP_NONE )
740 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +0200741 status = mbedtls_to_psa_error(
742 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
743 if( status != PSA_SUCCESS )
744 return( status );
745
746 /* Load the key material */
747 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
748 {
749 /* Load the public value. */
750 status = mbedtls_to_psa_error(
751 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
752 slot->data.key.data,
753 slot->data.key.bytes ) );
754 if( status != PSA_SUCCESS )
755 goto exit;
756
757 /* Check that the point is on the curve. */
758 status = mbedtls_to_psa_error(
759 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
760 if( status != PSA_SUCCESS )
761 goto exit;
762 }
763 else
764 {
765 /* Load the secret value. */
766 status = mbedtls_to_psa_error(
767 mbedtls_ecp_read_key( ecp->grp.id,
768 ecp,
769 slot->data.key.data,
770 slot->data.key.bytes ) );
771
772 if( status != PSA_SUCCESS )
773 goto exit;
774 /* Validate the private key. */
775 status = mbedtls_to_psa_error(
776 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
777 if( status != PSA_SUCCESS )
778 goto exit;
779 }
780exit:
781 if( status != PSA_SUCCESS )
782 mbedtls_ecp_keypair_free( ecp );
783 return status;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100784}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000785
Steven Cooremanacda8342020-07-24 23:09:52 +0200786static psa_status_t psa_export_ecp_key( psa_key_type_t type,
787 mbedtls_ecp_keypair *ecp,
788 uint8_t *data,
789 size_t data_size,
790 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200791{
Steven Cooremanacda8342020-07-24 23:09:52 +0200792 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000793
Steven Cooremanacda8342020-07-24 23:09:52 +0200794 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200795 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200796 /* Check whether the public part is loaded */
797 if( mbedtls_ecp_is_zero( &ecp->Q ) )
798 {
799 /* Calculate the public key */
800 status = mbedtls_to_psa_error(
801 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
802 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
803 if( status != PSA_SUCCESS )
804 return status;
805 }
806
807 return( mbedtls_to_psa_error(
808 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
809 MBEDTLS_ECP_PF_UNCOMPRESSED,
810 data_length,
811 data,
812 data_size ) ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200813 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200814 else
815 {
816 if( data_size < PSA_BITS_TO_BYTES(ecp->grp.nbits) )
817 return( PSA_ERROR_BUFFER_TOO_SMALL );
818
819 status = mbedtls_to_psa_error(
820 mbedtls_ecp_write_key( ecp,
821 data,
822 PSA_BITS_TO_BYTES(ecp->grp.nbits) ) );
823 if( status == PSA_SUCCESS )
824 {
825 *data_length = PSA_BITS_TO_BYTES(ecp->grp.nbits);
826 }
827
828 return( status );
829 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200830}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200831
Steven Cooremanacda8342020-07-24 23:09:52 +0200832static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
833 const uint8_t *data,
834 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100835{
Steven Cooremanacda8342020-07-24 23:09:52 +0200836 psa_status_t status;
837 uint8_t* output = NULL;
838 mbedtls_ecp_keypair ecp;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100839
Steven Cooremanacda8342020-07-24 23:09:52 +0200840 /* Temporarily load input into slot. The cast here is safe since it'll
841 * only be used for load_ecp_representation, which doesn't modify the
842 * buffer. */
843 slot->data.key.data = (uint8_t *)data;
844 slot->data.key.bytes = data_length;
845
846 /* Parse input */
847 status = psa_load_ecp_representation( slot, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100848 if( status != PSA_SUCCESS )
849 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100850
Steven Cooremanacda8342020-07-24 23:09:52 +0200851 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
852 slot->attr.bits = (psa_key_bits_t) ecp.grp.nbits + 1;
853 else
854 slot->attr.bits = (psa_key_bits_t) ecp.grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200855
Steven Cooremanacda8342020-07-24 23:09:52 +0200856 /* Re-export the data to PSA export format. There is currently no support
857 * for other input formats then the export format, so this is a 1-1
858 * copy operation. */
859 output = mbedtls_calloc( 1, data_length );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100860
Steven Cooremanacda8342020-07-24 23:09:52 +0200861 if( output == NULL )
862 {
863 status = PSA_ERROR_INSUFFICIENT_MEMORY;
864 goto exit;
865 }
866
867 status = psa_export_ecp_key( slot->attr.type,
868 &ecp,
869 output,
870 data_length,
871 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100872
873exit:
Steven Cooremanacda8342020-07-24 23:09:52 +0200874 /* Always free the PK object (will also free contained RSA context) */
875 mbedtls_ecp_keypair_free( &ecp );
876
877 /* Free the allocated buffer only on error. */
878 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100879 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200880 mbedtls_free( output );
881 slot->data.key.data = NULL;
882 slot->data.key.bytes = 0;
883 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100884 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200885
886 /* On success, store the allocated export-formatted key. */
887 slot->data.key.data = output;
888 slot->data.key.bytes = data_length;
889
890 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100891}
892#endif /* defined(MBEDTLS_ECP_C) */
893
Gilles Peskineb46bef22019-07-30 21:32:04 +0200894/** Return the size of the key in the given slot, in bits.
895 *
896 * \param[in] slot A key slot.
897 *
898 * \return The key size in bits, read from the metadata in the slot.
899 */
900static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
901{
902 return( slot->attr.bits );
903}
904
Steven Cooreman75b74362020-07-28 14:30:13 +0200905/** Try to allocate a buffer to an empty key slot.
906 *
907 * \param[in,out] slot Key slot to attach buffer to.
908 * \param[in] buffer_length Requested size of the buffer.
909 *
910 * \retval #PSA_SUCCESS
911 * The buffer has been successfully allocated.
912 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
913 * Not enough memory was available for allocation.
914 * \retval #PSA_ERROR_ALREADY_EXISTS
915 * Trying to allocate a buffer to a non-empty key slot.
916 */
917static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
918 size_t buffer_length )
919{
920 if( slot->data.key.data != NULL )
921 return PSA_ERROR_ALREADY_EXISTS;
922
923 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
924 if( slot->data.key.data == NULL )
925 return PSA_ERROR_INSUFFICIENT_MEMORY;
926
927 slot->data.key.bytes = buffer_length;
928 return PSA_SUCCESS;
929}
930
Gilles Peskine8e338702019-07-30 20:06:31 +0200931/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100932 * previously. This function assumes that the slot does not contain
933 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100934psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
935 const uint8_t *data,
936 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100937{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200938 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100939
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200940 /* zero-length keys are never supported. */
941 if( data_length == 0 )
942 return( PSA_ERROR_NOT_SUPPORTED );
943
Gilles Peskine8e338702019-07-30 20:06:31 +0200944 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200946 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200947
Steven Cooreman75b74362020-07-28 14:30:13 +0200948 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
949 if( data_length > SIZE_MAX / 8 )
950 return( PSA_ERROR_NOT_SUPPORTED );
951
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200952 /* Enforce a size limit, and in particular ensure that the bit
953 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200954 if( bit_size > PSA_MAX_KEY_BITS )
955 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200956
957 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200958 if( status != PSA_SUCCESS )
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200959 return status;
960
961 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +0200962 status = psa_allocate_buffer_to_slot( slot, data_length );
963 if( status != PSA_SUCCESS )
964 return status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200965
966 /* copy key into allocated buffer */
967 memcpy(slot->data.key.data, data, data_length);
968
969 /* Write the actual key size to the slot.
970 * psa_start_key_creation() wrote the size declared by the
971 * caller, which may be 0 (meaning unspecified) or wrong. */
972 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100973 }
Steven Cooreman19fd5742020-07-24 23:31:01 +0200974 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
975 {
Gilles Peskinef76aa772018-10-29 19:24:33 +0100976#if defined(MBEDTLS_ECP_C)
Steven Cooreman19fd5742020-07-24 23:31:01 +0200977 status = psa_import_ecp_key( slot,
978 data, data_length );
979#else
980 /* No drivers have been implemented yet, so without mbed TLS backing
981 * there's no way to do ECP with the current library. */
982 return( PSA_ERROR_NOT_SUPPORTED );
983#endif /* defined(MBEDTLS_ECP_C) */
984 }
985 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100986 {
Steven Cooreman19fd5742020-07-24 23:31:01 +0200987#if defined(MBEDTLS_RSA_C)
988 status = psa_import_rsa_key( slot,
989 data, data_length );
990#else
991 /* No drivers have been implemented yet, so without mbed TLS backing
992 * there's no way to do RSA with the current library. */
993 status = PSA_ERROR_NOT_SUPPORTED;
994#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 }
996 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100997 {
Steven Cooreman19fd5742020-07-24 23:31:01 +0200998 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100999 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001000 }
Darryl Green940d72c2018-07-13 13:18:51 +01001001
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{
Steven Cooreman75b74362020-07-28 14:30:13 +02001158 /* Check whether key is already clean */
1159 if( slot->data.key.data == NULL )
1160 return PSA_SUCCESS;
1161
Gilles Peskine73167e12019-07-12 23:44:37 +02001162#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1163 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001164 {
1165 /* No key material to clean. */
1166 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001167 else
1168#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001169 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001170 {
1171 /* No key material to clean. */
1172 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001173 else if( key_type_is_raw_bytes( slot->attr.type ) ||
1174 PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1175 PSA_KEY_TYPE_IS_ECC( 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
Darryl Green40225ba2018-11-15 14:48:15 +00001182 {
1183 /* Shouldn't happen: the key type is not any type that we
1184 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001185 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001186 }
1187
1188 return( PSA_SUCCESS );
1189}
1190
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001191/** Completely wipe a slot in memory, including its policy.
1192 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001193psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001194{
1195 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001196 /* Multipart operations may still be using the key. This is safe
1197 * because all multipart operation objects are independent from
1198 * the key slot: if they need to access the key after the setup
1199 * phase, they have a copy of the key. Note that this means that
1200 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001201 /* At this point, key material and other type-specific content has
1202 * been wiped. Clear remaining metadata. We can call memset and not
1203 * zeroize because the metadata is not particularly sensitive. */
1204 memset( slot, 0, sizeof( *slot ) );
1205 return( status );
1206}
1207
Gilles Peskinec5487a82018-12-03 18:08:14 +01001208psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001209{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001210 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001211 psa_status_t status; /* status of the last operation */
1212 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001213#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1214 psa_se_drv_table_entry_t *driver;
1215#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001216
Gilles Peskine1841cf42019-10-08 15:48:25 +02001217 if( handle == 0 )
1218 return( PSA_SUCCESS );
1219
Gilles Peskinec5487a82018-12-03 18:08:14 +01001220 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001221 if( status != PSA_SUCCESS )
1222 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001223
1224#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001225 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001226 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001227 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001228 /* For a key in a secure element, we need to do three things:
1229 * remove the key file in internal storage, destroy the
1230 * key inside the secure element, and update the driver's
1231 * persistent data. Start a transaction that will encompass these
1232 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001233 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001234 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001235 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001236 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001237 status = psa_crypto_save_transaction( );
1238 if( status != PSA_SUCCESS )
1239 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001240 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001241 /* We should still try to destroy the key in the secure
1242 * element and the key metadata in storage. This is especially
1243 * important if the error is that the storage is full.
1244 * But how to do it exactly without risking an inconsistent
1245 * state after a reset?
1246 * https://github.com/ARMmbed/mbed-crypto/issues/215
1247 */
1248 overall_status = status;
1249 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001250 }
1251
Gilles Peskine354f7672019-07-12 23:46:38 +02001252 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001253 if( overall_status == PSA_SUCCESS )
1254 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001255 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001256#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1257
Darryl Greend49a4992018-06-18 17:27:26 +01001258#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001259 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001260 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001261 status = psa_destroy_persistent_key( slot->attr.id );
1262 if( overall_status == PSA_SUCCESS )
1263 overall_status = status;
1264
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001265 /* TODO: other slots may have a copy of the same key. We should
1266 * invalidate them.
1267 * https://github.com/ARMmbed/mbed-crypto/issues/214
1268 */
Darryl Greend49a4992018-06-18 17:27:26 +01001269 }
1270#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001271
Gilles Peskinefc762652019-07-22 19:30:34 +02001272#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1273 if( driver != NULL )
1274 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001275 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001276 if( overall_status == PSA_SUCCESS )
1277 overall_status = status;
1278 status = psa_crypto_stop_transaction( );
1279 if( overall_status == PSA_SUCCESS )
1280 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001281 }
1282#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1283
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001284#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1285exit:
1286#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001287 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001288 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1289 if( overall_status == PSA_SUCCESS )
1290 overall_status = status;
1291 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001292}
1293
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001294void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001295{
Gilles Peskineb699f072019-04-26 16:06:02 +02001296 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001297 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001298}
1299
Gilles Peskineb699f072019-04-26 16:06:02 +02001300psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1301 psa_key_type_t type,
1302 const uint8_t *data,
1303 size_t data_length )
1304{
1305 uint8_t *copy = NULL;
1306
1307 if( data_length != 0 )
1308 {
1309 copy = mbedtls_calloc( 1, data_length );
1310 if( copy == NULL )
1311 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1312 memcpy( copy, data, data_length );
1313 }
1314 /* After this point, this function is guaranteed to succeed, so it
1315 * can start modifying `*attributes`. */
1316
1317 if( attributes->domain_parameters != NULL )
1318 {
1319 mbedtls_free( attributes->domain_parameters );
1320 attributes->domain_parameters = NULL;
1321 attributes->domain_parameters_size = 0;
1322 }
1323
1324 attributes->domain_parameters = copy;
1325 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001326 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001327 return( PSA_SUCCESS );
1328}
1329
1330psa_status_t psa_get_key_domain_parameters(
1331 const psa_key_attributes_t *attributes,
1332 uint8_t *data, size_t data_size, size_t *data_length )
1333{
1334 if( attributes->domain_parameters_size > data_size )
1335 return( PSA_ERROR_BUFFER_TOO_SMALL );
1336 *data_length = attributes->domain_parameters_size;
1337 if( attributes->domain_parameters_size != 0 )
1338 memcpy( data, attributes->domain_parameters,
1339 attributes->domain_parameters_size );
1340 return( PSA_SUCCESS );
1341}
1342
1343#if defined(MBEDTLS_RSA_C)
1344static psa_status_t psa_get_rsa_public_exponent(
1345 const mbedtls_rsa_context *rsa,
1346 psa_key_attributes_t *attributes )
1347{
1348 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001349 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001350 uint8_t *buffer = NULL;
1351 size_t buflen;
1352 mbedtls_mpi_init( &mpi );
1353
1354 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1355 if( ret != 0 )
1356 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001357 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1358 {
1359 /* It's the default value, which is reported as an empty string,
1360 * so there's nothing to do. */
1361 goto exit;
1362 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001363
1364 buflen = mbedtls_mpi_size( &mpi );
1365 buffer = mbedtls_calloc( 1, buflen );
1366 if( buffer == NULL )
1367 {
1368 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1369 goto exit;
1370 }
1371 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1372 if( ret != 0 )
1373 goto exit;
1374 attributes->domain_parameters = buffer;
1375 attributes->domain_parameters_size = buflen;
1376
1377exit:
1378 mbedtls_mpi_free( &mpi );
1379 if( ret != 0 )
1380 mbedtls_free( buffer );
1381 return( mbedtls_to_psa_error( ret ) );
1382}
1383#endif /* MBEDTLS_RSA_C */
1384
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001385/** Retrieve all the publicly-accessible attributes of a key.
1386 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001387psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1388 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001389{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001390 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001391 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001392
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001393 psa_reset_key_attributes( attributes );
1394
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001395 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001396 if( status != PSA_SUCCESS )
1397 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001398
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001399 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001400 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1401 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1402
1403#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1404 if( psa_key_slot_is_external( slot ) )
1405 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1406#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001407
Gilles Peskine8e338702019-07-30 20:06:31 +02001408 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001409 {
1410#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001411 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001412 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001413#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001414 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001415 * is not yet implemented.
1416 * https://github.com/ARMmbed/mbed-crypto/issues/216
1417 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001418 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001419 break;
1420#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001421 {
1422 mbedtls_rsa_context rsa;
1423 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
1424
1425 status = psa_load_rsa_representation( slot, &rsa );
1426 if( status != PSA_SUCCESS )
1427 break;
1428
1429 status = psa_get_rsa_public_exponent( &rsa,
1430 attributes );
1431 mbedtls_rsa_free( &rsa );
1432 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001433 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001434#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001435 default:
1436 /* Nothing else to do. */
1437 break;
1438 }
1439
1440 if( status != PSA_SUCCESS )
1441 psa_reset_key_attributes( attributes );
1442 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001443}
1444
Gilles Peskinec8000c02019-08-02 20:15:51 +02001445#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1446psa_status_t psa_get_key_slot_number(
1447 const psa_key_attributes_t *attributes,
1448 psa_key_slot_number_t *slot_number )
1449{
1450 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1451 {
1452 *slot_number = attributes->slot_number;
1453 return( PSA_SUCCESS );
1454 }
1455 else
1456 return( PSA_ERROR_INVALID_ARGUMENT );
1457}
1458#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1459
Steven Cooremana01795d2020-07-24 22:48:15 +02001460static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1461 uint8_t *data,
1462 size_t data_size,
1463 size_t *data_length )
1464{
1465 if( slot->data.key.bytes > data_size )
1466 return( PSA_ERROR_BUFFER_TOO_SMALL );
1467 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1468 memset( data + slot->data.key.bytes, 0,
1469 data_size - slot->data.key.bytes );
1470 *data_length = slot->data.key.bytes;
1471 return( PSA_SUCCESS );
1472}
1473
Gilles Peskinef603c712019-01-19 13:40:11 +01001474static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1475 uint8_t *data,
1476 size_t data_size,
1477 size_t *data_length,
1478 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001479{
Gilles Peskine5d309672019-07-12 23:47:28 +02001480#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1481 const psa_drv_se_t *drv;
1482 psa_drv_se_context_t *drv_context;
1483#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1484
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001485 *data_length = 0;
1486
Gilles Peskine8e338702019-07-30 20:06:31 +02001487 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001488 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001489
Gilles Peskinef9168942019-09-12 19:20:29 +02001490 /* Reject a zero-length output buffer now, since this can never be a
1491 * valid key representation. This way we know that data must be a valid
1492 * pointer and we can do things like memset(data, ..., data_size). */
1493 if( data_size == 0 )
1494 return( PSA_ERROR_BUFFER_TOO_SMALL );
1495
Gilles Peskine5d309672019-07-12 23:47:28 +02001496#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001497 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001498 {
1499 psa_drv_se_export_key_t method;
1500 if( drv->key_management == NULL )
1501 return( PSA_ERROR_NOT_SUPPORTED );
1502 method = ( export_public_key ?
1503 drv->key_management->p_export_public :
1504 drv->key_management->p_export );
1505 if( method == NULL )
1506 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001507 return( method( drv_context,
1508 slot->data.se.slot_number,
1509 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001510 }
1511#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1512
Gilles Peskine8e338702019-07-30 20:06:31 +02001513 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001514 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001515 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001516 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001517 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1518 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001519 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001520 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001521 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001522 /* Exporting public -> public */
1523 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1524 }
1525 else if( !export_public_key )
1526 {
1527 /* Exporting private -> private */
1528 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1529 }
1530 /* Need to export the public part of a private key,
1531 * so conversion is needed */
1532 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1533 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001534#if defined(MBEDTLS_RSA_C)
Steven Cooreman560c28a2020-07-24 23:20:24 +02001535 mbedtls_rsa_context rsa;
1536 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
Steven Cooremana01795d2020-07-24 22:48:15 +02001537
Steven Cooreman560c28a2020-07-24 23:20:24 +02001538 psa_status_t status = psa_load_rsa_representation( slot, &rsa );
1539 if( status != PSA_SUCCESS )
1540 return status;
Steven Cooremana01795d2020-07-24 22:48:15 +02001541
Steven Cooreman560c28a2020-07-24 23:20:24 +02001542 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
1543 &rsa,
1544 data,
1545 data_size,
1546 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001547
Steven Cooreman560c28a2020-07-24 23:20:24 +02001548 mbedtls_rsa_free( &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001549
Steven Cooreman560c28a2020-07-24 23:20:24 +02001550 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001551#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001552 /* We don't know how to convert a private RSA key to public. */
1553 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001554#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001555 }
1556 else
Moran Pekera998bc62018-04-16 18:16:20 +03001557 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001558#if defined(MBEDTLS_ECP_C)
1559 mbedtls_ecp_keypair ecp;
1560 psa_status_t status = psa_load_ecp_representation( slot, &ecp );
1561 if( status != PSA_SUCCESS )
1562 return status;
1563
1564 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1565 PSA_KEY_TYPE_ECC_GET_FAMILY(
1566 slot->attr.type ) ),
1567 &ecp,
1568 data,
1569 data_size,
1570 data_length );
1571
1572 mbedtls_ecp_keypair_free( &ecp );
1573 return( status );
1574#else
1575 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001576 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001577#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001578 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001579 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001580 else
1581 {
1582 /* This shouldn't happen in the reference implementation, but
1583 it is valid for a special-purpose implementation to omit
1584 support for exporting certain key types. */
1585 return( PSA_ERROR_NOT_SUPPORTED );
1586 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001587}
1588
Gilles Peskinec5487a82018-12-03 18:08:14 +01001589psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001590 uint8_t *data,
1591 size_t data_size,
1592 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001593{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001594 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001595 psa_status_t status;
1596
1597 /* Set the key to empty now, so that even when there are errors, we always
1598 * set data_length to a value between 0 and data_size. On error, setting
1599 * the key to empty is a good choice because an empty key representation is
1600 * unlikely to be accepted anywhere. */
1601 *data_length = 0;
1602
1603 /* Export requires the EXPORT flag. There is an exception for public keys,
1604 * which don't require any flag, but psa_get_key_from_slot takes
1605 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001606 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001607 if( status != PSA_SUCCESS )
1608 return( status );
1609 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001610 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001611}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001612
Gilles Peskinec5487a82018-12-03 18:08:14 +01001613psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001614 uint8_t *data,
1615 size_t data_size,
1616 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001617{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001618 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001619 psa_status_t status;
1620
1621 /* Set the key to empty now, so that even when there are errors, we always
1622 * set data_length to a value between 0 and data_size. On error, setting
1623 * the key to empty is a good choice because an empty key representation is
1624 * unlikely to be accepted anywhere. */
1625 *data_length = 0;
1626
1627 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001628 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001629 if( status != PSA_SUCCESS )
1630 return( status );
1631 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001632 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001633}
1634
Gilles Peskine91e8c332019-08-02 19:19:39 +02001635#if defined(static_assert)
1636static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1637 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001638static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001639 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001640static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001641 "One or more key attribute flag is listed as both internal-only and external-only" );
1642#endif
1643
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001644/** Validate that a key policy is internally well-formed.
1645 *
1646 * This function only rejects invalid policies. It does not validate the
1647 * consistency of the policy with respect to other attributes of the key
1648 * such as the key type.
1649 */
1650static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001651{
1652 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001653 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001654 PSA_KEY_USAGE_ENCRYPT |
1655 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001656 PSA_KEY_USAGE_SIGN_HASH |
1657 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001658 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1659 return( PSA_ERROR_INVALID_ARGUMENT );
1660
Gilles Peskine4747d192019-04-17 15:05:45 +02001661 return( PSA_SUCCESS );
1662}
1663
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001664/** Validate the internal consistency of key attributes.
1665 *
1666 * This function only rejects invalid attribute values. If does not
1667 * validate the consistency of the attributes with any key data that may
1668 * be involved in the creation of the key.
1669 *
1670 * Call this function early in the key creation process.
1671 *
1672 * \param[in] attributes Key attributes for the new key.
1673 * \param[out] p_drv On any return, the driver for the key, if any.
1674 * NULL for a transparent key.
1675 *
1676 */
1677static psa_status_t psa_validate_key_attributes(
1678 const psa_key_attributes_t *attributes,
1679 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001680{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001681 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001682
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001683 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1684 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001685 if( status != PSA_SUCCESS )
1686 return( status );
1687
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001688 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1689 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001690 if( status != PSA_SUCCESS )
1691 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001692
1693 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001694 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001695 return( status );
1696
1697 /* Refuse to create overly large keys.
1698 * Note that this doesn't trigger on import if the attributes don't
1699 * explicitly specify a size (so psa_get_key_bits returns 0), so
1700 * psa_import_key() needs its own checks. */
1701 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1702 return( PSA_ERROR_NOT_SUPPORTED );
1703
Gilles Peskine91e8c332019-08-02 19:19:39 +02001704 /* Reject invalid flags. These should not be reachable through the API. */
1705 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1706 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1707 return( PSA_ERROR_INVALID_ARGUMENT );
1708
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001709 return( PSA_SUCCESS );
1710}
1711
Gilles Peskine4747d192019-04-17 15:05:45 +02001712/** Prepare a key slot to receive key material.
1713 *
1714 * This function allocates a key slot and sets its metadata.
1715 *
1716 * If this function fails, call psa_fail_key_creation().
1717 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001718 * This function is intended to be used as follows:
1719 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1720 * it with the specified attributes, and assign it a handle.
1721 * -# Populate the slot with the key material.
1722 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1723 * In case of failure at any step, stop the sequence and call
1724 * psa_fail_key_creation().
1725 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001726 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001727 * \param[in] attributes Key attributes for the new key.
1728 * \param[out] handle On success, a handle for the allocated slot.
1729 * \param[out] p_slot On success, a pointer to the prepared slot.
1730 * \param[out] p_drv On any return, the driver for the key, if any.
1731 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001732 *
1733 * \retval #PSA_SUCCESS
1734 * The key slot is ready to receive key material.
1735 * \return If this function fails, the key slot is an invalid state.
1736 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001737 */
1738static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001739 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001740 const psa_key_attributes_t *attributes,
1741 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001742 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001743 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001744{
1745 psa_status_t status;
1746 psa_key_slot_t *slot;
1747
Gilles Peskinedf179142019-07-15 22:02:14 +02001748 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001749 *p_drv = NULL;
1750
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001751 status = psa_validate_key_attributes( attributes, p_drv );
1752 if( status != PSA_SUCCESS )
1753 return( status );
1754
Gilles Peskineedbed562019-08-07 18:19:59 +02001755 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001756 if( status != PSA_SUCCESS )
1757 return( status );
1758 slot = *p_slot;
1759
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001760 /* We're storing the declared bit-size of the key. It's up to each
1761 * creation mechanism to verify that this information is correct.
1762 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001763 * an input (generate, device) but not for those where the bit-size
1764 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001765
1766 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001767
Gilles Peskine91e8c332019-08-02 19:19:39 +02001768 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001769 * external-only flags, query `attributes`. Thanks to the check
1770 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001771 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001772 * may have set. */
1773 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001774
Gilles Peskinecbaff462019-07-12 23:46:04 +02001775#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001776 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001777 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001778 * create the key file in internal storage, create the
1779 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001780 * persistent data. This is done by starting a transaction that will
1781 * encompass these three actions.
1782 * For registering a volatile key, we just need to find an appropriate
1783 * slot number inside the SE. Since the key is designated volatile, creating
1784 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001785 /* The first thing to do is to find a slot number for the new key.
1786 * We save the slot number in persistent storage as part of the
1787 * transaction data. It will be needed to recover if the power
1788 * fails during the key creation process, to clean up on the secure
1789 * element side after restarting. Obtaining a slot number from the
1790 * secure element driver updates its persistent state, but we do not yet
1791 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001792 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001793 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001794 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001795 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001796 &slot->data.se.slot_number );
1797 if( status != PSA_SUCCESS )
1798 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001799
1800 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001801 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001802 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1803 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1804 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1805 psa_crypto_transaction.key.id = slot->attr.id;
1806 status = psa_crypto_save_transaction( );
1807 if( status != PSA_SUCCESS )
1808 {
1809 (void) psa_crypto_stop_transaction( );
1810 return( status );
1811 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001812 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001813 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001814
1815 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1816 {
1817 /* Key registration only makes sense with a secure element. */
1818 return( PSA_ERROR_INVALID_ARGUMENT );
1819 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001820#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1821
Darryl Green0c6575a2018-11-07 16:05:30 +00001822 return( status );
1823}
Gilles Peskine4747d192019-04-17 15:05:45 +02001824
1825/** Finalize the creation of a key once its key material has been set.
1826 *
1827 * This entails writing the key to persistent storage.
1828 *
1829 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001830 * See the documentation of psa_start_key_creation() for the intended use
1831 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001832 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001833 * \param[in,out] slot Pointer to the slot with key material.
1834 * \param[in] driver The secure element driver for the key,
1835 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001836 *
1837 * \retval #PSA_SUCCESS
1838 * The key was successfully created. The handle is now valid.
1839 * \return If this function fails, the key slot is an invalid state.
1840 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001841 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001842static psa_status_t psa_finish_key_creation(
1843 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001844 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001845{
1846 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001847 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001848 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001849
1850#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001851 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001852 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001853#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1854 if( driver != NULL )
1855 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001856 psa_se_key_data_storage_t data;
1857#if defined(static_assert)
1858 static_assert( sizeof( slot->data.se.slot_number ) ==
1859 sizeof( data.slot_number ),
1860 "Slot number size does not match psa_se_key_data_storage_t" );
1861 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1862 "Bit-size size does not match psa_se_key_data_storage_t" );
1863#endif
1864 memcpy( &data.slot_number, &slot->data.se.slot_number,
1865 sizeof( slot->data.se.slot_number ) );
1866 memcpy( &data.bits, &slot->attr.bits,
1867 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001868 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001869 (uint8_t*) &data,
1870 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001871 }
1872 else
1873#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1874 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001875 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001876 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001877 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001878 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1879 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001880 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001881 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1882 status = psa_internal_export_key( slot,
1883 buffer, buffer_size, &length,
1884 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001885 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001886 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001887 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001888
Gilles Peskinef9168942019-09-12 19:20:29 +02001889 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001890 mbedtls_free( buffer );
1891 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001892 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001893#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1894
Gilles Peskinecbaff462019-07-12 23:46:04 +02001895#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001896 /* Finish the transaction for a key creation. This does not
1897 * happen when registering an existing key. Detect this case
1898 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001899 * creation of a persistent key in a secure element requires a transaction,
1900 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001901 if( driver != NULL &&
1902 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001903 {
1904 status = psa_save_se_persistent_data( driver );
1905 if( status != PSA_SUCCESS )
1906 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001907 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001908 return( status );
1909 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001910 status = psa_crypto_stop_transaction( );
1911 if( status != PSA_SUCCESS )
1912 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001913 }
1914#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1915
Gilles Peskine4747d192019-04-17 15:05:45 +02001916 return( status );
1917}
1918
1919/** Abort the creation of a key.
1920 *
1921 * You may call this function after calling psa_start_key_creation(),
1922 * or after psa_finish_key_creation() fails. In other circumstances, this
1923 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001924 * See the documentation of psa_start_key_creation() for the intended use
1925 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001926 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001927 * \param[in,out] slot Pointer to the slot with key material.
1928 * \param[in] driver The secure element driver for the key,
1929 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001930 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001931static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001932 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001933{
Gilles Peskine011e4282019-06-26 18:34:38 +02001934 (void) driver;
1935
Gilles Peskine4747d192019-04-17 15:05:45 +02001936 if( slot == NULL )
1937 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001938
1939#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001940 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001941 * element, and the failure happened later (when saving metadata
1942 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001943 * element.
1944 * https://github.com/ARMmbed/mbed-crypto/issues/217
1945 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001946
Gilles Peskined7729582019-08-05 15:55:54 +02001947 /* Abort the ongoing transaction if any (there may not be one if
1948 * the creation process failed before starting one, or if the
1949 * key creation is a registration of a key in a secure element).
1950 * Earlier functions must already have done what it takes to undo any
1951 * partial creation. All that's left is to update the transaction data
1952 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001953 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001954#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1955
Gilles Peskine4747d192019-04-17 15:05:45 +02001956 psa_wipe_key_slot( slot );
1957}
1958
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001959/** Validate optional attributes during key creation.
1960 *
1961 * Some key attributes are optional during key creation. If they are
1962 * specified in the attributes structure, check that they are consistent
1963 * with the data in the slot.
1964 *
1965 * This function should be called near the end of key creation, after
1966 * the slot in memory is fully populated but before saving persistent data.
1967 */
1968static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001969 const psa_key_slot_t *slot,
1970 const psa_key_attributes_t *attributes )
1971{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001972 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001973 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001974 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001975 return( PSA_ERROR_INVALID_ARGUMENT );
1976 }
1977
1978 if( attributes->domain_parameters_size != 0 )
1979 {
1980#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001981 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001982 {
Steven Cooremana01795d2020-07-24 22:48:15 +02001983 mbedtls_rsa_context rsa;
1984 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
Steven Cooreman75b74362020-07-28 14:30:13 +02001985 mbedtls_mpi actual, required;
Steven Cooremana01795d2020-07-24 22:48:15 +02001986
1987 psa_status_t status = psa_load_rsa_representation( slot, &rsa );
1988 if( status != PSA_SUCCESS )
1989 return status;
Steven Cooreman75b74362020-07-28 14:30:13 +02001990
Janos Follath24eed8d2019-11-22 13:21:35 +00001991 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001992 mbedtls_mpi_init( &actual );
1993 mbedtls_mpi_init( &required );
Steven Cooremana01795d2020-07-24 22:48:15 +02001994 ret = mbedtls_rsa_export( &rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001995 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana01795d2020-07-24 22:48:15 +02001996 mbedtls_rsa_free( &rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001997 if( ret != 0 )
1998 goto rsa_exit;
1999 ret = mbedtls_mpi_read_binary( &required,
2000 attributes->domain_parameters,
2001 attributes->domain_parameters_size );
2002 if( ret != 0 )
2003 goto rsa_exit;
2004 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2005 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2006 rsa_exit:
2007 mbedtls_mpi_free( &actual );
2008 mbedtls_mpi_free( &required );
2009 if( ret != 0)
2010 return( mbedtls_to_psa_error( ret ) );
2011 }
2012 else
2013#endif
2014 {
2015 return( PSA_ERROR_INVALID_ARGUMENT );
2016 }
2017 }
2018
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002019 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002020 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002021 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002022 return( PSA_ERROR_INVALID_ARGUMENT );
2023 }
2024
2025 return( PSA_SUCCESS );
2026}
2027
Gilles Peskine4747d192019-04-17 15:05:45 +02002028psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002029 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002030 size_t data_length,
2031 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002032{
2033 psa_status_t status;
2034 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002035 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002036
Gilles Peskine0f84d622019-09-12 19:03:13 +02002037 /* Reject zero-length symmetric keys (including raw data key objects).
2038 * This also rejects any key which might be encoded as an empty string,
2039 * which is never valid. */
2040 if( data_length == 0 )
2041 return( PSA_ERROR_INVALID_ARGUMENT );
2042
Gilles Peskinedf179142019-07-15 22:02:14 +02002043 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2044 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002045 if( status != PSA_SUCCESS )
2046 goto exit;
2047
Gilles Peskine5d309672019-07-12 23:47:28 +02002048#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2049 if( driver != NULL )
2050 {
2051 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002052 /* The driver should set the number of key bits, however in
2053 * case it doesn't, we initialize bits to an invalid value. */
2054 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002055 if( drv->key_management == NULL ||
2056 drv->key_management->p_import == NULL )
2057 {
2058 status = PSA_ERROR_NOT_SUPPORTED;
2059 goto exit;
2060 }
2061 status = drv->key_management->p_import(
2062 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002063 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002064 &bits );
2065 if( status != PSA_SUCCESS )
2066 goto exit;
2067 if( bits > PSA_MAX_KEY_BITS )
2068 {
2069 status = PSA_ERROR_NOT_SUPPORTED;
2070 goto exit;
2071 }
2072 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002073 }
2074 else
2075#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2076 {
2077 status = psa_import_key_into_slot( slot, data, data_length );
2078 if( status != PSA_SUCCESS )
2079 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002080 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002081 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002082 if( status != PSA_SUCCESS )
2083 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002084
Gilles Peskine011e4282019-06-26 18:34:38 +02002085 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002086exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002087 if( status != PSA_SUCCESS )
2088 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002089 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002090 *handle = 0;
2091 }
2092 return( status );
2093}
2094
Gilles Peskined7729582019-08-05 15:55:54 +02002095#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2096psa_status_t mbedtls_psa_register_se_key(
2097 const psa_key_attributes_t *attributes )
2098{
2099 psa_status_t status;
2100 psa_key_slot_t *slot = NULL;
2101 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002102 psa_key_handle_t handle = 0;
2103
2104 /* Leaving attributes unspecified is not currently supported.
2105 * It could make sense to query the key type and size from the
2106 * secure element, but not all secure elements support this
2107 * and the driver HAL doesn't currently support it. */
2108 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2109 return( PSA_ERROR_NOT_SUPPORTED );
2110 if( psa_get_key_bits( attributes ) == 0 )
2111 return( PSA_ERROR_NOT_SUPPORTED );
2112
2113 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2114 &handle, &slot, &driver );
2115 if( status != PSA_SUCCESS )
2116 goto exit;
2117
Gilles Peskined7729582019-08-05 15:55:54 +02002118 status = psa_finish_key_creation( slot, driver );
2119
2120exit:
2121 if( status != PSA_SUCCESS )
2122 {
2123 psa_fail_key_creation( slot, driver );
2124 }
2125 /* Registration doesn't keep the key in RAM. */
2126 psa_close_key( handle );
2127 return( status );
2128}
2129#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2130
Gilles Peskinef603c712019-01-19 13:40:11 +01002131static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002132 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002133{
2134 psa_status_t status;
2135 uint8_t *buffer = NULL;
2136 size_t buffer_size = 0;
2137 size_t length;
2138
Gilles Peskine8e338702019-07-30 20:06:31 +02002139 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002140 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002141 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002142 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002143 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002144 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2145 if( status != PSA_SUCCESS )
2146 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002147 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002148 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002149
2150exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002151 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002152 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002153 return( status );
2154}
2155
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002156psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2157 const psa_key_attributes_t *specified_attributes,
2158 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002159{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002160 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002161 psa_key_slot_t *source_slot = NULL;
2162 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002163 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002164 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002165
Gilles Peskine28f8f302019-07-24 13:30:31 +02002166 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002167 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002168 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002169 goto exit;
2170
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002171 status = psa_validate_optional_attributes( source_slot,
2172 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002173 if( status != PSA_SUCCESS )
2174 goto exit;
2175
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002176 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002177 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002178 if( status != PSA_SUCCESS )
2179 goto exit;
2180
Gilles Peskinedf179142019-07-15 22:02:14 +02002181 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2182 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002183 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002184 if( status != PSA_SUCCESS )
2185 goto exit;
2186
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002187#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2188 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002189 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002190 /* Copying to a secure element is not implemented yet. */
2191 status = PSA_ERROR_NOT_SUPPORTED;
2192 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002193 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002194#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002195
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002196 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002197 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002198 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002199
Gilles Peskine011e4282019-06-26 18:34:38 +02002200 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002201exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002202 if( status != PSA_SUCCESS )
2203 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002204 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002205 *target_handle = 0;
2206 }
2207 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002208}
2209
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002210
2211
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002212/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002213/* Message digests */
2214/****************************************************************/
2215
Gilles Peskineb16841e2019-10-10 20:36:12 +02002216#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002217static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002218{
2219 switch( alg )
2220 {
2221#if defined(MBEDTLS_MD2_C)
2222 case PSA_ALG_MD2:
2223 return( &mbedtls_md2_info );
2224#endif
2225#if defined(MBEDTLS_MD4_C)
2226 case PSA_ALG_MD4:
2227 return( &mbedtls_md4_info );
2228#endif
2229#if defined(MBEDTLS_MD5_C)
2230 case PSA_ALG_MD5:
2231 return( &mbedtls_md5_info );
2232#endif
2233#if defined(MBEDTLS_RIPEMD160_C)
2234 case PSA_ALG_RIPEMD160:
2235 return( &mbedtls_ripemd160_info );
2236#endif
2237#if defined(MBEDTLS_SHA1_C)
2238 case PSA_ALG_SHA_1:
2239 return( &mbedtls_sha1_info );
2240#endif
2241#if defined(MBEDTLS_SHA256_C)
2242 case PSA_ALG_SHA_224:
2243 return( &mbedtls_sha224_info );
2244 case PSA_ALG_SHA_256:
2245 return( &mbedtls_sha256_info );
2246#endif
2247#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002248#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002249 case PSA_ALG_SHA_384:
2250 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002251#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002252 case PSA_ALG_SHA_512:
2253 return( &mbedtls_sha512_info );
2254#endif
2255 default:
2256 return( NULL );
2257 }
2258}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002259#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002260
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002261psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2262{
2263 switch( operation->alg )
2264 {
Gilles Peskine81736312018-06-26 15:04:31 +02002265 case 0:
2266 /* The object has (apparently) been initialized but it is not
2267 * in use. It's ok to call abort on such an object, and there's
2268 * nothing to do. */
2269 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002270#if defined(MBEDTLS_MD2_C)
2271 case PSA_ALG_MD2:
2272 mbedtls_md2_free( &operation->ctx.md2 );
2273 break;
2274#endif
2275#if defined(MBEDTLS_MD4_C)
2276 case PSA_ALG_MD4:
2277 mbedtls_md4_free( &operation->ctx.md4 );
2278 break;
2279#endif
2280#if defined(MBEDTLS_MD5_C)
2281 case PSA_ALG_MD5:
2282 mbedtls_md5_free( &operation->ctx.md5 );
2283 break;
2284#endif
2285#if defined(MBEDTLS_RIPEMD160_C)
2286 case PSA_ALG_RIPEMD160:
2287 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2288 break;
2289#endif
2290#if defined(MBEDTLS_SHA1_C)
2291 case PSA_ALG_SHA_1:
2292 mbedtls_sha1_free( &operation->ctx.sha1 );
2293 break;
2294#endif
2295#if defined(MBEDTLS_SHA256_C)
2296 case PSA_ALG_SHA_224:
2297 case PSA_ALG_SHA_256:
2298 mbedtls_sha256_free( &operation->ctx.sha256 );
2299 break;
2300#endif
2301#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002302#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002303 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002304#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002305 case PSA_ALG_SHA_512:
2306 mbedtls_sha512_free( &operation->ctx.sha512 );
2307 break;
2308#endif
2309 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002310 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002311 }
2312 operation->alg = 0;
2313 return( PSA_SUCCESS );
2314}
2315
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002316psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002317 psa_algorithm_t alg )
2318{
Janos Follath24eed8d2019-11-22 13:21:35 +00002319 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002320
2321 /* A context must be freshly initialized before it can be set up. */
2322 if( operation->alg != 0 )
2323 {
2324 return( PSA_ERROR_BAD_STATE );
2325 }
2326
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327 switch( alg )
2328 {
2329#if defined(MBEDTLS_MD2_C)
2330 case PSA_ALG_MD2:
2331 mbedtls_md2_init( &operation->ctx.md2 );
2332 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2333 break;
2334#endif
2335#if defined(MBEDTLS_MD4_C)
2336 case PSA_ALG_MD4:
2337 mbedtls_md4_init( &operation->ctx.md4 );
2338 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2339 break;
2340#endif
2341#if defined(MBEDTLS_MD5_C)
2342 case PSA_ALG_MD5:
2343 mbedtls_md5_init( &operation->ctx.md5 );
2344 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2345 break;
2346#endif
2347#if defined(MBEDTLS_RIPEMD160_C)
2348 case PSA_ALG_RIPEMD160:
2349 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2350 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2351 break;
2352#endif
2353#if defined(MBEDTLS_SHA1_C)
2354 case PSA_ALG_SHA_1:
2355 mbedtls_sha1_init( &operation->ctx.sha1 );
2356 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2357 break;
2358#endif
2359#if defined(MBEDTLS_SHA256_C)
2360 case PSA_ALG_SHA_224:
2361 mbedtls_sha256_init( &operation->ctx.sha256 );
2362 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2363 break;
2364 case PSA_ALG_SHA_256:
2365 mbedtls_sha256_init( &operation->ctx.sha256 );
2366 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2367 break;
2368#endif
2369#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002370#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002371 case PSA_ALG_SHA_384:
2372 mbedtls_sha512_init( &operation->ctx.sha512 );
2373 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2374 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002375#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002376 case PSA_ALG_SHA_512:
2377 mbedtls_sha512_init( &operation->ctx.sha512 );
2378 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2379 break;
2380#endif
2381 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002382 return( PSA_ALG_IS_HASH( alg ) ?
2383 PSA_ERROR_NOT_SUPPORTED :
2384 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002385 }
2386 if( ret == 0 )
2387 operation->alg = alg;
2388 else
2389 psa_hash_abort( operation );
2390 return( mbedtls_to_psa_error( ret ) );
2391}
2392
2393psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2394 const uint8_t *input,
2395 size_t input_length )
2396{
Janos Follath24eed8d2019-11-22 13:21:35 +00002397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002398
2399 /* Don't require hash implementations to behave correctly on a
2400 * zero-length input, which may have an invalid pointer. */
2401 if( input_length == 0 )
2402 return( PSA_SUCCESS );
2403
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002404 switch( operation->alg )
2405 {
2406#if defined(MBEDTLS_MD2_C)
2407 case PSA_ALG_MD2:
2408 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2409 input, input_length );
2410 break;
2411#endif
2412#if defined(MBEDTLS_MD4_C)
2413 case PSA_ALG_MD4:
2414 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2415 input, input_length );
2416 break;
2417#endif
2418#if defined(MBEDTLS_MD5_C)
2419 case PSA_ALG_MD5:
2420 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2421 input, input_length );
2422 break;
2423#endif
2424#if defined(MBEDTLS_RIPEMD160_C)
2425 case PSA_ALG_RIPEMD160:
2426 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2427 input, input_length );
2428 break;
2429#endif
2430#if defined(MBEDTLS_SHA1_C)
2431 case PSA_ALG_SHA_1:
2432 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2433 input, input_length );
2434 break;
2435#endif
2436#if defined(MBEDTLS_SHA256_C)
2437 case PSA_ALG_SHA_224:
2438 case PSA_ALG_SHA_256:
2439 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2440 input, input_length );
2441 break;
2442#endif
2443#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002444#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002445 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002446#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002447 case PSA_ALG_SHA_512:
2448 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2449 input, input_length );
2450 break;
2451#endif
2452 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002453 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002454 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002455
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002456 if( ret != 0 )
2457 psa_hash_abort( operation );
2458 return( mbedtls_to_psa_error( ret ) );
2459}
2460
2461psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2462 uint8_t *hash,
2463 size_t hash_size,
2464 size_t *hash_length )
2465{
itayzafrir40835d42018-08-02 13:14:17 +03002466 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002468 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002469
2470 /* Fill the output buffer with something that isn't a valid hash
2471 * (barring an attack on the hash and deliberately-crafted input),
2472 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002473 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002474 /* If hash_size is 0 then hash may be NULL and then the
2475 * call to memset would have undefined behavior. */
2476 if( hash_size != 0 )
2477 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002478
2479 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002480 {
2481 status = PSA_ERROR_BUFFER_TOO_SMALL;
2482 goto exit;
2483 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002484
2485 switch( operation->alg )
2486 {
2487#if defined(MBEDTLS_MD2_C)
2488 case PSA_ALG_MD2:
2489 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2490 break;
2491#endif
2492#if defined(MBEDTLS_MD4_C)
2493 case PSA_ALG_MD4:
2494 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2495 break;
2496#endif
2497#if defined(MBEDTLS_MD5_C)
2498 case PSA_ALG_MD5:
2499 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2500 break;
2501#endif
2502#if defined(MBEDTLS_RIPEMD160_C)
2503 case PSA_ALG_RIPEMD160:
2504 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2505 break;
2506#endif
2507#if defined(MBEDTLS_SHA1_C)
2508 case PSA_ALG_SHA_1:
2509 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2510 break;
2511#endif
2512#if defined(MBEDTLS_SHA256_C)
2513 case PSA_ALG_SHA_224:
2514 case PSA_ALG_SHA_256:
2515 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2516 break;
2517#endif
2518#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002519#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002520 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002521#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002522 case PSA_ALG_SHA_512:
2523 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2524 break;
2525#endif
2526 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002527 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002528 }
itayzafrir40835d42018-08-02 13:14:17 +03002529 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002530
itayzafrir40835d42018-08-02 13:14:17 +03002531exit:
2532 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002533 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002534 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002535 return( psa_hash_abort( operation ) );
2536 }
2537 else
2538 {
2539 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002540 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002541 }
2542}
2543
Gilles Peskine2d277862018-06-18 15:41:12 +02002544psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2545 const uint8_t *hash,
2546 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002547{
2548 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2549 size_t actual_hash_length;
2550 psa_status_t status = psa_hash_finish( operation,
2551 actual_hash, sizeof( actual_hash ),
2552 &actual_hash_length );
2553 if( status != PSA_SUCCESS )
2554 return( status );
2555 if( actual_hash_length != hash_length )
2556 return( PSA_ERROR_INVALID_SIGNATURE );
2557 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2558 return( PSA_ERROR_INVALID_SIGNATURE );
2559 return( PSA_SUCCESS );
2560}
2561
Gilles Peskine0a749c82019-11-28 19:33:58 +01002562psa_status_t psa_hash_compute( psa_algorithm_t alg,
2563 const uint8_t *input, size_t input_length,
2564 uint8_t *hash, size_t hash_size,
2565 size_t *hash_length )
2566{
2567 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2568 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2569
2570 *hash_length = hash_size;
2571 status = psa_hash_setup( &operation, alg );
2572 if( status != PSA_SUCCESS )
2573 goto exit;
2574 status = psa_hash_update( &operation, input, input_length );
2575 if( status != PSA_SUCCESS )
2576 goto exit;
2577 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2578 if( status != PSA_SUCCESS )
2579 goto exit;
2580
2581exit:
2582 if( status == PSA_SUCCESS )
2583 status = psa_hash_abort( &operation );
2584 else
2585 psa_hash_abort( &operation );
2586 return( status );
2587}
2588
2589psa_status_t psa_hash_compare( psa_algorithm_t alg,
2590 const uint8_t *input, size_t input_length,
2591 const uint8_t *hash, size_t hash_length )
2592{
2593 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2594 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2595
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_verify( &operation, hash, 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
Gilles Peskineeb35d782019-01-22 17:56:16 +01002614psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2615 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002616{
2617 if( target_operation->alg != 0 )
2618 return( PSA_ERROR_BAD_STATE );
2619
2620 switch( source_operation->alg )
2621 {
2622 case 0:
2623 return( PSA_ERROR_BAD_STATE );
2624#if defined(MBEDTLS_MD2_C)
2625 case PSA_ALG_MD2:
2626 mbedtls_md2_clone( &target_operation->ctx.md2,
2627 &source_operation->ctx.md2 );
2628 break;
2629#endif
2630#if defined(MBEDTLS_MD4_C)
2631 case PSA_ALG_MD4:
2632 mbedtls_md4_clone( &target_operation->ctx.md4,
2633 &source_operation->ctx.md4 );
2634 break;
2635#endif
2636#if defined(MBEDTLS_MD5_C)
2637 case PSA_ALG_MD5:
2638 mbedtls_md5_clone( &target_operation->ctx.md5,
2639 &source_operation->ctx.md5 );
2640 break;
2641#endif
2642#if defined(MBEDTLS_RIPEMD160_C)
2643 case PSA_ALG_RIPEMD160:
2644 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2645 &source_operation->ctx.ripemd160 );
2646 break;
2647#endif
2648#if defined(MBEDTLS_SHA1_C)
2649 case PSA_ALG_SHA_1:
2650 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2651 &source_operation->ctx.sha1 );
2652 break;
2653#endif
2654#if defined(MBEDTLS_SHA256_C)
2655 case PSA_ALG_SHA_224:
2656 case PSA_ALG_SHA_256:
2657 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2658 &source_operation->ctx.sha256 );
2659 break;
2660#endif
2661#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002662#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002663 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002664#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002665 case PSA_ALG_SHA_512:
2666 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2667 &source_operation->ctx.sha512 );
2668 break;
2669#endif
2670 default:
2671 return( PSA_ERROR_NOT_SUPPORTED );
2672 }
2673
2674 target_operation->alg = source_operation->alg;
2675 return( PSA_SUCCESS );
2676}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002677
2678
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002679/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002680/* MAC */
2681/****************************************************************/
2682
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002683static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002684 psa_algorithm_t alg,
2685 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002686 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002687 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002688{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002689 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002690 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002691
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002692 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002693 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002694
Gilles Peskine8c9def32018-02-08 10:02:12 +01002695 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2696 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002697 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002698 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002699 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002700 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002701 mode = MBEDTLS_MODE_STREAM;
2702 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002703 case PSA_ALG_CTR:
2704 mode = MBEDTLS_MODE_CTR;
2705 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002706 case PSA_ALG_CFB:
2707 mode = MBEDTLS_MODE_CFB;
2708 break;
2709 case PSA_ALG_OFB:
2710 mode = MBEDTLS_MODE_OFB;
2711 break;
2712 case PSA_ALG_CBC_NO_PADDING:
2713 mode = MBEDTLS_MODE_CBC;
2714 break;
2715 case PSA_ALG_CBC_PKCS7:
2716 mode = MBEDTLS_MODE_CBC;
2717 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002718 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002719 mode = MBEDTLS_MODE_CCM;
2720 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002721 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722 mode = MBEDTLS_MODE_GCM;
2723 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002724 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2725 mode = MBEDTLS_MODE_CHACHAPOLY;
2726 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727 default:
2728 return( NULL );
2729 }
2730 }
2731 else if( alg == PSA_ALG_CMAC )
2732 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002733 else
2734 return( NULL );
2735
2736 switch( key_type )
2737 {
2738 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002739 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002740 break;
2741 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002742 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2743 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002744 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002745 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002747 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002748 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2749 * but two-key Triple-DES is functionally three-key Triple-DES
2750 * with K1=K3, so that's how we present it to mbedtls. */
2751 if( key_bits == 128 )
2752 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002753 break;
2754 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002755 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002756 break;
2757 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002758 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002759 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002760 case PSA_KEY_TYPE_CHACHA20:
2761 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2762 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002763 default:
2764 return( NULL );
2765 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002766 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002767 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002768
Jaeden Amero23bbb752018-06-26 14:16:54 +01002769 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2770 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002771}
2772
Gilles Peskinea05219c2018-11-16 16:02:56 +01002773#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002774static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002775{
Gilles Peskine2d277862018-06-18 15:41:12 +02002776 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002777 {
2778 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002779 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002780 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002781 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002782 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002783 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002784 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002785 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002786 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002787 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002788 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002789 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002790 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002791 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002792 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002793 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002794 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002795 return( 128 );
2796 default:
2797 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002798 }
2799}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002800#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002801
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002802/* Initialize the MAC operation structure. Once this function has been
2803 * called, psa_mac_abort can run and will do the right thing. */
2804static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2805 psa_algorithm_t alg )
2806{
2807 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2808
2809 operation->alg = alg;
2810 operation->key_set = 0;
2811 operation->iv_set = 0;
2812 operation->iv_required = 0;
2813 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002814 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002815
2816#if defined(MBEDTLS_CMAC_C)
2817 if( alg == PSA_ALG_CMAC )
2818 {
2819 operation->iv_required = 0;
2820 mbedtls_cipher_init( &operation->ctx.cmac );
2821 status = PSA_SUCCESS;
2822 }
2823 else
2824#endif /* MBEDTLS_CMAC_C */
2825#if defined(MBEDTLS_MD_C)
2826 if( PSA_ALG_IS_HMAC( operation->alg ) )
2827 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002828 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2829 operation->ctx.hmac.hash_ctx.alg = 0;
2830 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002831 }
2832 else
2833#endif /* MBEDTLS_MD_C */
2834 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002835 if( ! PSA_ALG_IS_MAC( alg ) )
2836 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002837 }
2838
2839 if( status != PSA_SUCCESS )
2840 memset( operation, 0, sizeof( *operation ) );
2841 return( status );
2842}
2843
Gilles Peskine01126fa2018-07-12 17:04:55 +02002844#if defined(MBEDTLS_MD_C)
2845static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2846{
Gilles Peskine3f108122018-12-07 18:14:53 +01002847 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002848 return( psa_hash_abort( &hmac->hash_ctx ) );
2849}
2850#endif /* MBEDTLS_MD_C */
2851
Gilles Peskine8c9def32018-02-08 10:02:12 +01002852psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2853{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002854 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002855 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002856 /* The object has (apparently) been initialized but it is not
2857 * in use. It's ok to call abort on such an object, and there's
2858 * nothing to do. */
2859 return( PSA_SUCCESS );
2860 }
2861 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002862#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002863 if( operation->alg == PSA_ALG_CMAC )
2864 {
2865 mbedtls_cipher_free( &operation->ctx.cmac );
2866 }
2867 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002868#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002869#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002870 if( PSA_ALG_IS_HMAC( operation->alg ) )
2871 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002872 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002873 }
2874 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002875#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002876 {
2877 /* Sanity check (shouldn't happen: operation->alg should
2878 * always have been initialized to a valid value). */
2879 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002880 }
Moran Peker41deec42018-04-04 15:43:05 +03002881
Gilles Peskine8c9def32018-02-08 10:02:12 +01002882 operation->alg = 0;
2883 operation->key_set = 0;
2884 operation->iv_set = 0;
2885 operation->iv_required = 0;
2886 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002887 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002888
Gilles Peskine8c9def32018-02-08 10:02:12 +01002889 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002890
2891bad_state:
2892 /* If abort is called on an uninitialized object, we can't trust
2893 * anything. Wipe the object in case it contains confidential data.
2894 * This may result in a memory leak if a pointer gets overwritten,
2895 * but it's too late to do anything about this. */
2896 memset( operation, 0, sizeof( *operation ) );
2897 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002898}
2899
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002900#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002901static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002902 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002903 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002904 const mbedtls_cipher_info_t *cipher_info )
2905{
Janos Follath24eed8d2019-11-22 13:21:35 +00002906 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002907
2908 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002909
2910 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2911 if( ret != 0 )
2912 return( ret );
2913
2914 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002915 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002916 key_bits );
2917 return( ret );
2918}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002919#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002920
Gilles Peskine248051a2018-06-20 16:09:38 +02002921#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002922static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2923 const uint8_t *key,
2924 size_t key_length,
2925 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002926{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002927 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002928 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002929 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002930 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002931 psa_status_t status;
2932
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002933 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2934 * overflow below. This should never trigger if the hash algorithm
2935 * is implemented correctly. */
2936 /* The size checks against the ipad and opad buffers cannot be written
2937 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2938 * because that triggers -Wlogical-op on GCC 7.3. */
2939 if( block_size > sizeof( ipad ) )
2940 return( PSA_ERROR_NOT_SUPPORTED );
2941 if( block_size > sizeof( hmac->opad ) )
2942 return( PSA_ERROR_NOT_SUPPORTED );
2943 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002944 return( PSA_ERROR_NOT_SUPPORTED );
2945
Gilles Peskined223b522018-06-11 18:12:58 +02002946 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002947 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002948 status = psa_hash_compute( hash_alg, key, key_length,
2949 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002950 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002951 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002952 }
Gilles Peskine96889972018-07-12 17:07:03 +02002953 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2954 * but it is permitted. It is common when HMAC is used in HKDF, for
2955 * example. Don't call `memcpy` in the 0-length because `key` could be
2956 * an invalid pointer which would make the behavior undefined. */
2957 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002958 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002959
Gilles Peskined223b522018-06-11 18:12:58 +02002960 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2961 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002962 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002963 ipad[i] ^= 0x36;
2964 memset( ipad + key_length, 0x36, block_size - key_length );
2965
2966 /* Copy the key material from ipad to opad, flipping the requisite bits,
2967 * and filling the rest of opad with the requisite constant. */
2968 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002969 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2970 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002971
Gilles Peskine01126fa2018-07-12 17:04:55 +02002972 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002973 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002974 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002975
Gilles Peskine01126fa2018-07-12 17:04:55 +02002976 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002977
2978cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002979 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002980
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002981 return( status );
2982}
Gilles Peskine248051a2018-06-20 16:09:38 +02002983#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002984
Gilles Peskine89167cb2018-07-08 20:12:23 +02002985static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002986 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002987 psa_algorithm_t alg,
2988 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002989{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002990 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002991 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002992 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002993 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002994 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002995 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002996 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002997
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002998 /* A context must be freshly initialized before it can be set up. */
2999 if( operation->alg != 0 )
3000 {
3001 return( PSA_ERROR_BAD_STATE );
3002 }
3003
Gilles Peskined911eb72018-08-14 15:18:45 +02003004 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003005 if( status != PSA_SUCCESS )
3006 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003007 if( is_sign )
3008 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003009
Gilles Peskine28f8f302019-07-24 13:30:31 +02003010 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003011 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003012 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003013 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003014
Gilles Peskine8c9def32018-02-08 10:02:12 +01003015#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003016 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003017 {
3018 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003019 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003020 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003021 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003022 if( cipher_info == NULL )
3023 {
3024 status = PSA_ERROR_NOT_SUPPORTED;
3025 goto exit;
3026 }
3027 operation->mac_size = cipher_info->block_size;
3028 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3029 status = mbedtls_to_psa_error( ret );
3030 }
3031 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003032#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003033#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003034 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003035 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003036 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003037 if( hash_alg == 0 )
3038 {
3039 status = PSA_ERROR_NOT_SUPPORTED;
3040 goto exit;
3041 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003042
3043 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3044 /* Sanity check. This shouldn't fail on a valid configuration. */
3045 if( operation->mac_size == 0 ||
3046 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3047 {
3048 status = PSA_ERROR_NOT_SUPPORTED;
3049 goto exit;
3050 }
3051
Gilles Peskine8e338702019-07-30 20:06:31 +02003052 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003053 {
3054 status = PSA_ERROR_INVALID_ARGUMENT;
3055 goto exit;
3056 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003057
Gilles Peskine01126fa2018-07-12 17:04:55 +02003058 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003059 slot->data.key.data,
3060 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003061 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003062 }
3063 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003064#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003065 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003066 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003067 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003068 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003069
Gilles Peskined911eb72018-08-14 15:18:45 +02003070 if( truncated == 0 )
3071 {
3072 /* The "normal" case: untruncated algorithm. Nothing to do. */
3073 }
3074 else if( truncated < 4 )
3075 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003076 /* A very short MAC is too short for security since it can be
3077 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3078 * so we make this our minimum, even though 32 bits is still
3079 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003080 status = PSA_ERROR_NOT_SUPPORTED;
3081 }
3082 else if( truncated > operation->mac_size )
3083 {
3084 /* It's impossible to "truncate" to a larger length. */
3085 status = PSA_ERROR_INVALID_ARGUMENT;
3086 }
3087 else
3088 operation->mac_size = truncated;
3089
Gilles Peskinefbfac682018-07-08 20:51:54 +02003090exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003091 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003092 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003093 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003094 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003095 else
3096 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003097 operation->key_set = 1;
3098 }
3099 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003100}
3101
Gilles Peskine89167cb2018-07-08 20:12:23 +02003102psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003103 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003104 psa_algorithm_t alg )
3105{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003106 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003107}
3108
3109psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003110 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003111 psa_algorithm_t alg )
3112{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003113 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003114}
3115
Gilles Peskine8c9def32018-02-08 10:02:12 +01003116psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3117 const uint8_t *input,
3118 size_t input_length )
3119{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003120 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003121 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003122 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003123 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003124 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003125 operation->has_input = 1;
3126
Gilles Peskine8c9def32018-02-08 10:02:12 +01003127#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003128 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003129 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003130 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3131 input, input_length );
3132 status = mbedtls_to_psa_error( ret );
3133 }
3134 else
3135#endif /* MBEDTLS_CMAC_C */
3136#if defined(MBEDTLS_MD_C)
3137 if( PSA_ALG_IS_HMAC( operation->alg ) )
3138 {
3139 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3140 input_length );
3141 }
3142 else
3143#endif /* MBEDTLS_MD_C */
3144 {
3145 /* This shouldn't happen if `operation` was initialized by
3146 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003147 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003148 }
3149
Gilles Peskinefbfac682018-07-08 20:51:54 +02003150 if( status != PSA_SUCCESS )
3151 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003152 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003153}
3154
Gilles Peskine01126fa2018-07-12 17:04:55 +02003155#if defined(MBEDTLS_MD_C)
3156static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3157 uint8_t *mac,
3158 size_t mac_size )
3159{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003160 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003161 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3162 size_t hash_size = 0;
3163 size_t block_size = psa_get_hash_block_size( hash_alg );
3164 psa_status_t status;
3165
Gilles Peskine01126fa2018-07-12 17:04:55 +02003166 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3167 if( status != PSA_SUCCESS )
3168 return( status );
3169 /* From here on, tmp needs to be wiped. */
3170
3171 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3172 if( status != PSA_SUCCESS )
3173 goto exit;
3174
3175 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3176 if( status != PSA_SUCCESS )
3177 goto exit;
3178
3179 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3180 if( status != PSA_SUCCESS )
3181 goto exit;
3182
Gilles Peskined911eb72018-08-14 15:18:45 +02003183 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3184 if( status != PSA_SUCCESS )
3185 goto exit;
3186
3187 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003188
3189exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003190 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003191 return( status );
3192}
3193#endif /* MBEDTLS_MD_C */
3194
mohammad16036df908f2018-04-02 08:34:15 -07003195static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003196 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003197 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003198{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003199 if( ! operation->key_set )
3200 return( PSA_ERROR_BAD_STATE );
3201 if( operation->iv_required && ! operation->iv_set )
3202 return( PSA_ERROR_BAD_STATE );
3203
Gilles Peskine8c9def32018-02-08 10:02:12 +01003204 if( mac_size < operation->mac_size )
3205 return( PSA_ERROR_BUFFER_TOO_SMALL );
3206
Gilles Peskine8c9def32018-02-08 10:02:12 +01003207#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003208 if( operation->alg == PSA_ALG_CMAC )
3209 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003210 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3211 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3212 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003213 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003214 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003215 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003216 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003217 else
3218#endif /* MBEDTLS_CMAC_C */
3219#if defined(MBEDTLS_MD_C)
3220 if( PSA_ALG_IS_HMAC( operation->alg ) )
3221 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003222 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003223 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003224 }
3225 else
3226#endif /* MBEDTLS_MD_C */
3227 {
3228 /* This shouldn't happen if `operation` was initialized by
3229 * a setup function. */
3230 return( PSA_ERROR_BAD_STATE );
3231 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003232}
3233
Gilles Peskineacd4be32018-07-08 19:56:25 +02003234psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3235 uint8_t *mac,
3236 size_t mac_size,
3237 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003238{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003239 psa_status_t status;
3240
Jaeden Amero252ef282019-02-15 14:05:35 +00003241 if( operation->alg == 0 )
3242 {
3243 return( PSA_ERROR_BAD_STATE );
3244 }
3245
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003246 /* Fill the output buffer with something that isn't a valid mac
3247 * (barring an attack on the mac and deliberately-crafted input),
3248 * in case the caller doesn't check the return status properly. */
3249 *mac_length = mac_size;
3250 /* If mac_size is 0 then mac may be NULL and then the
3251 * call to memset would have undefined behavior. */
3252 if( mac_size != 0 )
3253 memset( mac, '!', mac_size );
3254
Gilles Peskine89167cb2018-07-08 20:12:23 +02003255 if( ! operation->is_sign )
3256 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003257 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003258 }
mohammad16036df908f2018-04-02 08:34:15 -07003259
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003260 status = psa_mac_finish_internal( operation, mac, mac_size );
3261
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003262 if( status == PSA_SUCCESS )
3263 {
3264 status = psa_mac_abort( operation );
3265 if( status == PSA_SUCCESS )
3266 *mac_length = operation->mac_size;
3267 else
3268 memset( mac, '!', mac_size );
3269 }
3270 else
3271 psa_mac_abort( operation );
3272 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003273}
3274
Gilles Peskineacd4be32018-07-08 19:56:25 +02003275psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3276 const uint8_t *mac,
3277 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003278{
Gilles Peskine828ed142018-06-18 23:25:51 +02003279 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003280 psa_status_t status;
3281
Jaeden Amero252ef282019-02-15 14:05:35 +00003282 if( operation->alg == 0 )
3283 {
3284 return( PSA_ERROR_BAD_STATE );
3285 }
3286
Gilles Peskine89167cb2018-07-08 20:12:23 +02003287 if( operation->is_sign )
3288 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003289 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003290 }
3291 if( operation->mac_size != mac_length )
3292 {
3293 status = PSA_ERROR_INVALID_SIGNATURE;
3294 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003295 }
mohammad16036df908f2018-04-02 08:34:15 -07003296
3297 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003298 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003299 if( status != PSA_SUCCESS )
3300 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003301
3302 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3303 status = PSA_ERROR_INVALID_SIGNATURE;
3304
3305cleanup:
3306 if( status == PSA_SUCCESS )
3307 status = psa_mac_abort( operation );
3308 else
3309 psa_mac_abort( operation );
3310
Gilles Peskine3f108122018-12-07 18:14:53 +01003311 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003312
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003313 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003314}
3315
3316
Gilles Peskine20035e32018-02-03 22:44:14 +01003317
Gilles Peskine20035e32018-02-03 22:44:14 +01003318/****************************************************************/
3319/* Asymmetric cryptography */
3320/****************************************************************/
3321
Gilles Peskine2b450e32018-06-27 15:42:46 +02003322#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003323/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003324 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003325static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3326 size_t hash_length,
3327 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003328{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003329 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003330 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003331 *md_alg = mbedtls_md_get_type( md_info );
3332
3333 /* The Mbed TLS RSA module uses an unsigned int for hash length
3334 * parameters. Validate that it fits so that we don't risk an
3335 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003336#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003337 if( hash_length > UINT_MAX )
3338 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003339#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003340
3341#if defined(MBEDTLS_PKCS1_V15)
3342 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3343 * must be correct. */
3344 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3345 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003346 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003347 if( md_info == NULL )
3348 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003349 if( mbedtls_md_get_size( md_info ) != hash_length )
3350 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003351 }
3352#endif /* MBEDTLS_PKCS1_V15 */
3353
3354#if defined(MBEDTLS_PKCS1_V21)
3355 /* PSS requires a hash internally. */
3356 if( PSA_ALG_IS_RSA_PSS( alg ) )
3357 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003358 if( md_info == NULL )
3359 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003360 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003361#endif /* MBEDTLS_PKCS1_V21 */
3362
Gilles Peskine61b91d42018-06-08 16:09:36 +02003363 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003364}
3365
Gilles Peskine2b450e32018-06-27 15:42:46 +02003366static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3367 psa_algorithm_t alg,
3368 const uint8_t *hash,
3369 size_t hash_length,
3370 uint8_t *signature,
3371 size_t signature_size,
3372 size_t *signature_length )
3373{
3374 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003375 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003376 mbedtls_md_type_t md_alg;
3377
3378 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3379 if( status != PSA_SUCCESS )
3380 return( status );
3381
Gilles Peskine630a18a2018-06-29 17:49:35 +02003382 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003383 return( PSA_ERROR_BUFFER_TOO_SMALL );
3384
3385#if defined(MBEDTLS_PKCS1_V15)
3386 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3387 {
3388 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3389 MBEDTLS_MD_NONE );
3390 ret = mbedtls_rsa_pkcs1_sign( rsa,
3391 mbedtls_ctr_drbg_random,
3392 &global_data.ctr_drbg,
3393 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003394 md_alg,
3395 (unsigned int) hash_length,
3396 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003397 signature );
3398 }
3399 else
3400#endif /* MBEDTLS_PKCS1_V15 */
3401#if defined(MBEDTLS_PKCS1_V21)
3402 if( PSA_ALG_IS_RSA_PSS( alg ) )
3403 {
3404 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3405 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3406 mbedtls_ctr_drbg_random,
3407 &global_data.ctr_drbg,
3408 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003409 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003410 (unsigned int) hash_length,
3411 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003412 signature );
3413 }
3414 else
3415#endif /* MBEDTLS_PKCS1_V21 */
3416 {
3417 return( PSA_ERROR_INVALID_ARGUMENT );
3418 }
3419
3420 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003421 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003422 return( mbedtls_to_psa_error( ret ) );
3423}
3424
3425static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3426 psa_algorithm_t alg,
3427 const uint8_t *hash,
3428 size_t hash_length,
3429 const uint8_t *signature,
3430 size_t signature_length )
3431{
3432 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003434 mbedtls_md_type_t md_alg;
3435
3436 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3437 if( status != PSA_SUCCESS )
3438 return( status );
3439
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003440 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3441 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003442
3443#if defined(MBEDTLS_PKCS1_V15)
3444 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3445 {
3446 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3447 MBEDTLS_MD_NONE );
3448 ret = mbedtls_rsa_pkcs1_verify( rsa,
3449 mbedtls_ctr_drbg_random,
3450 &global_data.ctr_drbg,
3451 MBEDTLS_RSA_PUBLIC,
3452 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003453 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003454 hash,
3455 signature );
3456 }
3457 else
3458#endif /* MBEDTLS_PKCS1_V15 */
3459#if defined(MBEDTLS_PKCS1_V21)
3460 if( PSA_ALG_IS_RSA_PSS( alg ) )
3461 {
3462 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3463 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3464 mbedtls_ctr_drbg_random,
3465 &global_data.ctr_drbg,
3466 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003467 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003468 (unsigned int) hash_length,
3469 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003470 signature );
3471 }
3472 else
3473#endif /* MBEDTLS_PKCS1_V21 */
3474 {
3475 return( PSA_ERROR_INVALID_ARGUMENT );
3476 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003477
3478 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3479 * the rest of the signature is invalid". This has little use in
3480 * practice and PSA doesn't report this distinction. */
3481 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3482 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003483 return( mbedtls_to_psa_error( ret ) );
3484}
3485#endif /* MBEDTLS_RSA_C */
3486
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003487#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003488/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3489 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3490 * (even though these functions don't modify it). */
3491static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3492 psa_algorithm_t alg,
3493 const uint8_t *hash,
3494 size_t hash_length,
3495 uint8_t *signature,
3496 size_t signature_size,
3497 size_t *signature_length )
3498{
Janos Follath24eed8d2019-11-22 13:21:35 +00003499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003500 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003501 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003502 mbedtls_mpi_init( &r );
3503 mbedtls_mpi_init( &s );
3504
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003505 if( signature_size < 2 * curve_bytes )
3506 {
3507 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3508 goto cleanup;
3509 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003510
Gilles Peskinea05219c2018-11-16 16:02:56 +01003511#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003512 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3513 {
3514 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3515 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3516 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003517 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3518 &ecp->d, hash,
3519 hash_length, md_alg,
3520 mbedtls_ctr_drbg_random,
3521 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003522 }
3523 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003524#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003525 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003526 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003527 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3528 hash, hash_length,
3529 mbedtls_ctr_drbg_random,
3530 &global_data.ctr_drbg ) );
3531 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003532
3533 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3534 signature,
3535 curve_bytes ) );
3536 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3537 signature + curve_bytes,
3538 curve_bytes ) );
3539
3540cleanup:
3541 mbedtls_mpi_free( &r );
3542 mbedtls_mpi_free( &s );
3543 if( ret == 0 )
3544 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003545 return( mbedtls_to_psa_error( ret ) );
3546}
3547
3548static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3549 const uint8_t *hash,
3550 size_t hash_length,
3551 const uint8_t *signature,
3552 size_t signature_length )
3553{
Janos Follath24eed8d2019-11-22 13:21:35 +00003554 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003555 mbedtls_mpi r, s;
3556 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3557 mbedtls_mpi_init( &r );
3558 mbedtls_mpi_init( &s );
3559
3560 if( signature_length != 2 * curve_bytes )
3561 return( PSA_ERROR_INVALID_SIGNATURE );
3562
3563 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3564 signature,
3565 curve_bytes ) );
3566 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3567 signature + curve_bytes,
3568 curve_bytes ) );
3569
Steven Cooremanacda8342020-07-24 23:09:52 +02003570 /* Check whether the public part is loaded. If not, load it. */
3571 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3572 {
3573 MBEDTLS_MPI_CHK(
3574 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3575 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3576 }
3577
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003578 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3579 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003580
3581cleanup:
3582 mbedtls_mpi_free( &r );
3583 mbedtls_mpi_free( &s );
3584 return( mbedtls_to_psa_error( ret ) );
3585}
3586#endif /* MBEDTLS_ECDSA_C */
3587
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003588psa_status_t psa_sign_hash( psa_key_handle_t handle,
3589 psa_algorithm_t alg,
3590 const uint8_t *hash,
3591 size_t hash_length,
3592 uint8_t *signature,
3593 size_t signature_size,
3594 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003595{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003596 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003597 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003598#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3599 const psa_drv_se_t *drv;
3600 psa_drv_se_context_t *drv_context;
3601#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003602
3603 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003604 /* Immediately reject a zero-length signature buffer. This guarantees
3605 * that signature must be a valid pointer. (On the other hand, the hash
3606 * buffer can in principle be empty since it doesn't actually have
3607 * to be a hash.) */
3608 if( signature_size == 0 )
3609 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003610
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003611 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003612 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003613 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003614 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003615 {
3616 status = PSA_ERROR_INVALID_ARGUMENT;
3617 goto exit;
3618 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003619
Gilles Peskineedc64242019-08-07 21:05:07 +02003620#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3621 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3622 {
3623 if( drv->asymmetric == NULL ||
3624 drv->asymmetric->p_sign == NULL )
3625 {
3626 status = PSA_ERROR_NOT_SUPPORTED;
3627 goto exit;
3628 }
3629 status = drv->asymmetric->p_sign( drv_context,
3630 slot->data.se.slot_number,
3631 alg,
3632 hash, hash_length,
3633 signature, signature_size,
3634 signature_length );
3635 }
3636 else
3637#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003638#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003639 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003640 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003641 mbedtls_rsa_context rsa;
3642 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3643
3644 status = psa_load_rsa_representation( slot,
3645 &rsa );
3646 if( status != PSA_SUCCESS )
3647 goto exit;
3648
3649 status = psa_rsa_sign( &rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003650 alg,
3651 hash, hash_length,
3652 signature, signature_size,
3653 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003654
3655 mbedtls_rsa_free( &rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003656 }
3657 else
3658#endif /* defined(MBEDTLS_RSA_C) */
3659#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003660 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003661 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003662#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003663 if(
3664#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3665 PSA_ALG_IS_ECDSA( alg )
3666#else
3667 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3668#endif
3669 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003670 {
3671 mbedtls_ecp_keypair ecp;
3672 status = psa_load_ecp_representation( slot, &ecp );
3673 if( status != PSA_SUCCESS )
3674 goto exit;
3675 status = psa_ecdsa_sign( &ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003676 alg,
3677 hash, hash_length,
3678 signature, signature_size,
3679 signature_length );
Steven Cooremanacda8342020-07-24 23:09:52 +02003680 mbedtls_ecp_keypair_free( &ecp );
3681 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003682 else
3683#endif /* defined(MBEDTLS_ECDSA_C) */
3684 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003685 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003686 }
itayzafrir5c753392018-05-08 11:18:38 +03003687 }
3688 else
3689#endif /* defined(MBEDTLS_ECP_C) */
3690 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003691 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003692 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003693
3694exit:
3695 /* Fill the unused part of the output buffer (the whole buffer on error,
3696 * the trailing part on success) with something that isn't a valid mac
3697 * (barring an attack on the mac and deliberately-crafted input),
3698 * in case the caller doesn't check the return status properly. */
3699 if( status == PSA_SUCCESS )
3700 memset( signature + *signature_length, '!',
3701 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003702 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003703 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003704 /* If signature_size is 0 then we have nothing to do. We must not call
3705 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003706 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003707}
3708
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003709psa_status_t psa_verify_hash( psa_key_handle_t handle,
3710 psa_algorithm_t alg,
3711 const uint8_t *hash,
3712 size_t hash_length,
3713 const uint8_t *signature,
3714 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003715{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003716 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003717 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003718#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3719 const psa_drv_se_t *drv;
3720 psa_drv_se_context_t *drv_context;
3721#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003722
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003723 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003724 if( status != PSA_SUCCESS )
3725 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003726
Gilles Peskineedc64242019-08-07 21:05:07 +02003727#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3728 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3729 {
3730 if( drv->asymmetric == NULL ||
3731 drv->asymmetric->p_verify == NULL )
3732 return( PSA_ERROR_NOT_SUPPORTED );
3733 return( drv->asymmetric->p_verify( drv_context,
3734 slot->data.se.slot_number,
3735 alg,
3736 hash, hash_length,
3737 signature, signature_length ) );
3738 }
3739 else
3740#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003741#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003742 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003743 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003744 mbedtls_rsa_context rsa;
3745 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3746
3747 status = psa_load_rsa_representation( slot, &rsa );
3748 if( status != PSA_SUCCESS )
3749 return status;
3750
3751 status = psa_rsa_verify( &rsa,
3752 alg,
3753 hash, hash_length,
3754 signature, signature_length );
3755 mbedtls_rsa_free( &rsa );
3756 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003757 }
3758 else
3759#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003760#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003761 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003762 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003763#if defined(MBEDTLS_ECDSA_C)
3764 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003765 {
3766 mbedtls_ecp_keypair ecp;
3767 status = psa_load_ecp_representation( slot, &ecp );
3768 if( status != PSA_SUCCESS )
3769 return status;
3770 status = psa_ecdsa_verify( &ecp,
3771 hash, hash_length,
3772 signature, signature_length );
3773 mbedtls_ecp_keypair_free( &ecp );
3774 return status;
3775 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003776 else
3777#endif /* defined(MBEDTLS_ECDSA_C) */
3778 {
3779 return( PSA_ERROR_INVALID_ARGUMENT );
3780 }
itayzafrir5c753392018-05-08 11:18:38 +03003781 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003782 else
3783#endif /* defined(MBEDTLS_ECP_C) */
3784 {
3785 return( PSA_ERROR_NOT_SUPPORTED );
3786 }
3787}
3788
Gilles Peskine072ac562018-06-30 00:21:29 +02003789#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3790static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3791 mbedtls_rsa_context *rsa )
3792{
3793 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3794 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3795 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3796 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3797}
3798#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3799
Gilles Peskinec5487a82018-12-03 18:08:14 +01003800psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003801 psa_algorithm_t alg,
3802 const uint8_t *input,
3803 size_t input_length,
3804 const uint8_t *salt,
3805 size_t salt_length,
3806 uint8_t *output,
3807 size_t output_size,
3808 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003809{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003810 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003811 psa_status_t status;
3812
Darryl Green5cc689a2018-07-24 15:34:10 +01003813 (void) input;
3814 (void) input_length;
3815 (void) salt;
3816 (void) output;
3817 (void) output_size;
3818
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003819 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003820
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003821 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3822 return( PSA_ERROR_INVALID_ARGUMENT );
3823
Gilles Peskine28f8f302019-07-24 13:30:31 +02003824 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003825 if( status != PSA_SUCCESS )
3826 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003827 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3828 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003829 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003830
3831#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003832 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003833 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003834 mbedtls_rsa_context rsa;
3835 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
Steven Cooreman75b74362020-07-28 14:30:13 +02003836 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02003837
3838 status = psa_load_rsa_representation( slot, &rsa );
3839 if( status != PSA_SUCCESS )
3840 return status;
Steven Cooremana01795d2020-07-24 22:48:15 +02003841 if( output_size < mbedtls_rsa_get_len( &rsa ) )
3842 {
3843 mbedtls_rsa_free( &rsa );
Vikas Katariya21599b62019-08-02 12:26:29 +01003844 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooremana01795d2020-07-24 22:48:15 +02003845 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003846#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003847 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003848 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003849 ret = mbedtls_rsa_pkcs1_encrypt( &rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003850 mbedtls_ctr_drbg_random,
3851 &global_data.ctr_drbg,
3852 MBEDTLS_RSA_PUBLIC,
3853 input_length,
3854 input,
3855 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003856 }
3857 else
3858#endif /* MBEDTLS_PKCS1_V15 */
3859#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003860 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003861 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003862 psa_rsa_oaep_set_padding_mode( alg, &rsa );
3863 ret = mbedtls_rsa_rsaes_oaep_encrypt( &rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003864 mbedtls_ctr_drbg_random,
3865 &global_data.ctr_drbg,
3866 MBEDTLS_RSA_PUBLIC,
3867 salt, salt_length,
3868 input_length,
3869 input,
3870 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003871 }
3872 else
3873#endif /* MBEDTLS_PKCS1_V21 */
3874 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003875 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003876 return( PSA_ERROR_INVALID_ARGUMENT );
3877 }
3878 if( ret == 0 )
Steven Cooremana01795d2020-07-24 22:48:15 +02003879 *output_length = mbedtls_rsa_get_len( &rsa );
3880
3881 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003882 return( mbedtls_to_psa_error( ret ) );
3883 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003884 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003885#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003886 {
3887 return( PSA_ERROR_NOT_SUPPORTED );
3888 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003889}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003890
Gilles Peskinec5487a82018-12-03 18:08:14 +01003891psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003892 psa_algorithm_t alg,
3893 const uint8_t *input,
3894 size_t input_length,
3895 const uint8_t *salt,
3896 size_t salt_length,
3897 uint8_t *output,
3898 size_t output_size,
3899 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003900{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003901 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003902 psa_status_t status;
3903
Darryl Green5cc689a2018-07-24 15:34:10 +01003904 (void) input;
3905 (void) input_length;
3906 (void) salt;
3907 (void) output;
3908 (void) output_size;
3909
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003910 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003911
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003912 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3913 return( PSA_ERROR_INVALID_ARGUMENT );
3914
Gilles Peskine28f8f302019-07-24 13:30:31 +02003915 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003916 if( status != PSA_SUCCESS )
3917 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003918 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003919 return( PSA_ERROR_INVALID_ARGUMENT );
3920
3921#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003922 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003923 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003924 mbedtls_rsa_context rsa;
3925 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
Steven Cooreman75b74362020-07-28 14:30:13 +02003926 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02003927
3928 status = psa_load_rsa_representation( slot, &rsa );
3929 if( status != PSA_SUCCESS )
3930 return status;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003931
Steven Cooremana01795d2020-07-24 22:48:15 +02003932 if( input_length != mbedtls_rsa_get_len( &rsa ) )
3933 {
3934 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana01795d2020-07-24 22:48:15 +02003936 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003937
3938#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003939 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003940 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003941 ret = mbedtls_rsa_pkcs1_decrypt( &rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003942 mbedtls_ctr_drbg_random,
3943 &global_data.ctr_drbg,
3944 MBEDTLS_RSA_PRIVATE,
3945 output_length,
3946 input,
3947 output,
3948 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003949 }
3950 else
3951#endif /* MBEDTLS_PKCS1_V15 */
3952#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003953 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003954 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003955 psa_rsa_oaep_set_padding_mode( alg, &rsa );
3956 ret = mbedtls_rsa_rsaes_oaep_decrypt( &rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003957 mbedtls_ctr_drbg_random,
3958 &global_data.ctr_drbg,
3959 MBEDTLS_RSA_PRIVATE,
3960 salt, salt_length,
3961 output_length,
3962 input,
3963 output,
3964 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003965 }
3966 else
3967#endif /* MBEDTLS_PKCS1_V21 */
3968 {
Steven Cooremana01795d2020-07-24 22:48:15 +02003969 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003970 return( PSA_ERROR_INVALID_ARGUMENT );
3971 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003972
Steven Cooremana01795d2020-07-24 22:48:15 +02003973 mbedtls_rsa_free( &rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003974 return( mbedtls_to_psa_error( ret ) );
3975 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003976 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003977#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003978 {
3979 return( PSA_ERROR_NOT_SUPPORTED );
3980 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003981}
Gilles Peskine20035e32018-02-03 22:44:14 +01003982
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003983
3984
mohammad1603503973b2018-03-12 15:59:30 +02003985/****************************************************************/
3986/* Symmetric cryptography */
3987/****************************************************************/
3988
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003989/* Initialize the cipher operation structure. Once this function has been
3990 * called, psa_cipher_abort can run and will do the right thing. */
3991static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3992 psa_algorithm_t alg )
3993{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003994 if( ! PSA_ALG_IS_CIPHER( alg ) )
3995 {
3996 memset( operation, 0, sizeof( *operation ) );
3997 return( PSA_ERROR_INVALID_ARGUMENT );
3998 }
3999
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004000 operation->alg = alg;
4001 operation->key_set = 0;
4002 operation->iv_set = 0;
4003 operation->iv_required = 1;
4004 operation->iv_size = 0;
4005 operation->block_size = 0;
4006 mbedtls_cipher_init( &operation->ctx.cipher );
4007 return( PSA_SUCCESS );
4008}
4009
Gilles Peskinee553c652018-06-04 16:22:46 +02004010static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004011 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004012 psa_algorithm_t alg,
4013 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004014{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004015 int ret = 0;
4016 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004017 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004018 size_t key_bits;
4019 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004020 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4021 PSA_KEY_USAGE_ENCRYPT :
4022 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004023
Jaeden Amero36ee5d02019-02-19 09:25:10 +00004024 /* A context must be freshly initialized before it can be set up. */
4025 if( operation->alg != 0 )
4026 {
4027 return( PSA_ERROR_BAD_STATE );
4028 }
4029
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004030 status = psa_cipher_init( operation, alg );
4031 if( status != PSA_SUCCESS )
4032 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004033
Gilles Peskine28f8f302019-07-24 13:30:31 +02004034 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004035 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004036 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004037 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02004038
Gilles Peskine8e338702019-07-30 20:06:31 +02004039 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004040 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004041 {
4042 status = PSA_ERROR_NOT_SUPPORTED;
4043 goto exit;
4044 }
mohammad1603503973b2018-03-12 15:59:30 +02004045
mohammad1603503973b2018-03-12 15:59:30 +02004046 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004047 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004048 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004049
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004050#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004051 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004052 {
4053 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004054 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004055 memcpy( keys, slot->data.key.data, 16 );
4056 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004057 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4058 keys,
4059 192, cipher_operation );
4060 }
4061 else
4062#endif
4063 {
4064 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004065 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004066 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004067 }
Moran Peker41deec42018-04-04 15:43:05 +03004068 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004069 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004070
mohammad16038481e742018-03-18 13:57:31 +02004071#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004072 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004073 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004074 case PSA_ALG_CBC_NO_PADDING:
4075 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4076 MBEDTLS_PADDING_NONE );
4077 break;
4078 case PSA_ALG_CBC_PKCS7:
4079 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4080 MBEDTLS_PADDING_PKCS7 );
4081 break;
4082 default:
4083 /* The algorithm doesn't involve padding. */
4084 ret = 0;
4085 break;
4086 }
4087 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004088 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004089#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4090
mohammad1603503973b2018-03-12 15:59:30 +02004091 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004092 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004093 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004094 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02004095 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004096 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004097 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004098#if defined(MBEDTLS_CHACHA20_C)
4099 else
4100 if( alg == PSA_ALG_CHACHA20 )
4101 operation->iv_size = 12;
4102#endif
mohammad1603503973b2018-03-12 15:59:30 +02004103
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004104exit:
4105 if( status == 0 )
4106 status = mbedtls_to_psa_error( ret );
4107 if( status != 0 )
4108 psa_cipher_abort( operation );
4109 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004110}
4111
Gilles Peskinefe119512018-07-08 21:39:34 +02004112psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004113 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004114 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004115{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004116 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004117}
4118
Gilles Peskinefe119512018-07-08 21:39:34 +02004119psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004120 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004121 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004122{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004123 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004124}
4125
Gilles Peskinefe119512018-07-08 21:39:34 +02004126psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004127 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004128 size_t iv_size,
4129 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004130{
itayzafrir534bd7c2018-08-02 13:56:32 +03004131 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004133 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004134 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004135 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004136 }
Moran Peker41deec42018-04-04 15:43:05 +03004137 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004138 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004139 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004140 goto exit;
4141 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004142 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4143 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004144 if( ret != 0 )
4145 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004146 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004147 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004148 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004149
mohammad16038481e742018-03-18 13:57:31 +02004150 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004151 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004152
Moran Peker395db872018-05-31 14:07:14 +03004153exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03004154 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03004155 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004156 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004157}
4158
Gilles Peskinefe119512018-07-08 21:39:34 +02004159psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004160 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004161 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004162{
itayzafrir534bd7c2018-08-02 13:56:32 +03004163 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004164 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004165 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004166 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004167 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004168 }
Moran Pekera28258c2018-05-29 16:25:04 +03004169 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004170 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004171 status = PSA_ERROR_INVALID_ARGUMENT;
4172 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004173 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004174 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4175 status = mbedtls_to_psa_error( ret );
4176exit:
4177 if( status == PSA_SUCCESS )
4178 operation->iv_set = 1;
4179 else
mohammad1603503973b2018-03-12 15:59:30 +02004180 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004181 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004182}
4183
Gilles Peskinee553c652018-06-04 16:22:46 +02004184psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4185 const uint8_t *input,
4186 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004187 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004188 size_t output_size,
4189 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004190{
itayzafrir534bd7c2018-08-02 13:56:32 +03004191 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004192 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004193 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00004194
4195 if( operation->alg == 0 )
4196 {
4197 return( PSA_ERROR_BAD_STATE );
4198 }
4199
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004200 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004201 {
4202 /* Take the unprocessed partial block left over from previous
4203 * update calls, if any, plus the input to this call. Remove
4204 * the last partial block, if any. You get the data that will be
4205 * output in this call. */
4206 expected_output_size =
4207 ( operation->ctx.cipher.unprocessed_len + input_length )
4208 / operation->block_size * operation->block_size;
4209 }
4210 else
4211 {
4212 expected_output_size = input_length;
4213 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004214
Gilles Peskine89d789c2018-06-04 17:17:16 +02004215 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004216 {
4217 status = PSA_ERROR_BUFFER_TOO_SMALL;
4218 goto exit;
4219 }
mohammad160382759612018-03-12 18:16:40 +02004220
mohammad1603503973b2018-03-12 15:59:30 +02004221 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004222 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004223 status = mbedtls_to_psa_error( ret );
4224exit:
4225 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004226 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004227 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004228}
4229
Gilles Peskinee553c652018-06-04 16:22:46 +02004230psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4231 uint8_t *output,
4232 size_t output_size,
4233 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004234{
David Saadab4ecc272019-02-14 13:48:10 +02004235 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004236 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004237 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004238
mohammad1603503973b2018-03-12 15:59:30 +02004239 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004240 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004241 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004242 }
4243 if( operation->iv_required && ! operation->iv_set )
4244 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004245 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004246 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004247
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004248 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004249 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4250 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004251 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004252 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004253 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004254 }
4255
Janos Follath315b51c2018-07-09 16:04:51 +01004256 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4257 temp_output_buffer,
4258 output_length );
4259 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004260 {
Janos Follath315b51c2018-07-09 16:04:51 +01004261 status = mbedtls_to_psa_error( cipher_ret );
4262 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004263 }
Janos Follath315b51c2018-07-09 16:04:51 +01004264
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004265 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004266 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004267 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004268 memcpy( output, temp_output_buffer, *output_length );
4269 else
4270 {
Janos Follath315b51c2018-07-09 16:04:51 +01004271 status = PSA_ERROR_BUFFER_TOO_SMALL;
4272 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004273 }
mohammad1603503973b2018-03-12 15:59:30 +02004274
Gilles Peskine3f108122018-12-07 18:14:53 +01004275 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004276 status = psa_cipher_abort( operation );
4277
4278 return( status );
4279
4280error:
4281
4282 *output_length = 0;
4283
Gilles Peskine3f108122018-12-07 18:14:53 +01004284 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004285 (void) psa_cipher_abort( operation );
4286
4287 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004288}
4289
Gilles Peskinee553c652018-06-04 16:22:46 +02004290psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4291{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004292 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004293 {
4294 /* The object has (apparently) been initialized but it is not
4295 * in use. It's ok to call abort on such an object, and there's
4296 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004297 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004298 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004299
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004300 /* Sanity check (shouldn't happen: operation->alg should
4301 * always have been initialized to a valid value). */
4302 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4303 return( PSA_ERROR_BAD_STATE );
4304
mohammad1603503973b2018-03-12 15:59:30 +02004305 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004306
Moran Peker41deec42018-04-04 15:43:05 +03004307 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004308 operation->key_set = 0;
4309 operation->iv_set = 0;
4310 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004311 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004312 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004313
Moran Peker395db872018-05-31 14:07:14 +03004314 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004315}
4316
Gilles Peskinea0655c32018-04-30 17:06:50 +02004317
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004318
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004319
mohammad16035955c982018-04-26 00:53:03 +03004320/****************************************************************/
4321/* AEAD */
4322/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004323
Gilles Peskineedf9a652018-08-17 18:11:56 +02004324typedef struct
4325{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004326 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004327 const mbedtls_cipher_info_t *cipher_info;
4328 union
4329 {
4330#if defined(MBEDTLS_CCM_C)
4331 mbedtls_ccm_context ccm;
4332#endif /* MBEDTLS_CCM_C */
4333#if defined(MBEDTLS_GCM_C)
4334 mbedtls_gcm_context gcm;
4335#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004336#if defined(MBEDTLS_CHACHAPOLY_C)
4337 mbedtls_chachapoly_context chachapoly;
4338#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004339 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004340 psa_algorithm_t core_alg;
4341 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004342 uint8_t tag_length;
4343} aead_operation_t;
4344
Gilles Peskine30a9e412019-01-14 18:36:12 +01004345static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004346{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004347 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004348 {
4349#if defined(MBEDTLS_CCM_C)
4350 case PSA_ALG_CCM:
4351 mbedtls_ccm_free( &operation->ctx.ccm );
4352 break;
4353#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004354#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004355 case PSA_ALG_GCM:
4356 mbedtls_gcm_free( &operation->ctx.gcm );
4357 break;
4358#endif /* MBEDTLS_GCM_C */
4359 }
4360}
4361
4362static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004363 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004364 psa_key_usage_t usage,
4365 psa_algorithm_t alg )
4366{
4367 psa_status_t status;
4368 size_t key_bits;
4369 mbedtls_cipher_id_t cipher_id;
4370
Gilles Peskine28f8f302019-07-24 13:30:31 +02004371 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004372 if( status != PSA_SUCCESS )
4373 return( status );
4374
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004375 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004376
4377 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004378 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004379 &cipher_id );
4380 if( operation->cipher_info == NULL )
4381 return( PSA_ERROR_NOT_SUPPORTED );
4382
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004383 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004384 {
4385#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004386 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4387 operation->core_alg = PSA_ALG_CCM;
4388 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004389 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4390 * The call to mbedtls_ccm_encrypt_and_tag or
4391 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004392 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004393 return( PSA_ERROR_INVALID_ARGUMENT );
4394 mbedtls_ccm_init( &operation->ctx.ccm );
4395 status = mbedtls_to_psa_error(
4396 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004397 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004398 (unsigned int) key_bits ) );
4399 if( status != 0 )
4400 goto cleanup;
4401 break;
4402#endif /* MBEDTLS_CCM_C */
4403
4404#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004405 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4406 operation->core_alg = PSA_ALG_GCM;
4407 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004408 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4409 * The call to mbedtls_gcm_crypt_and_tag or
4410 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004411 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004412 return( PSA_ERROR_INVALID_ARGUMENT );
4413 mbedtls_gcm_init( &operation->ctx.gcm );
4414 status = mbedtls_to_psa_error(
4415 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004416 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004417 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004418 if( status != 0 )
4419 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004420 break;
4421#endif /* MBEDTLS_GCM_C */
4422
Gilles Peskine26869f22019-05-06 15:25:00 +02004423#if defined(MBEDTLS_CHACHAPOLY_C)
4424 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4425 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4426 operation->full_tag_length = 16;
4427 /* We only support the default tag length. */
4428 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4429 return( PSA_ERROR_NOT_SUPPORTED );
4430 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4431 status = mbedtls_to_psa_error(
4432 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004433 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004434 if( status != 0 )
4435 goto cleanup;
4436 break;
4437#endif /* MBEDTLS_CHACHAPOLY_C */
4438
Gilles Peskineedf9a652018-08-17 18:11:56 +02004439 default:
4440 return( PSA_ERROR_NOT_SUPPORTED );
4441 }
4442
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004443 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4444 {
4445 status = PSA_ERROR_INVALID_ARGUMENT;
4446 goto cleanup;
4447 }
4448 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004449
Gilles Peskineedf9a652018-08-17 18:11:56 +02004450 return( PSA_SUCCESS );
4451
4452cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004453 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004454 return( status );
4455}
4456
Gilles Peskinec5487a82018-12-03 18:08:14 +01004457psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004458 psa_algorithm_t alg,
4459 const uint8_t *nonce,
4460 size_t nonce_length,
4461 const uint8_t *additional_data,
4462 size_t additional_data_length,
4463 const uint8_t *plaintext,
4464 size_t plaintext_length,
4465 uint8_t *ciphertext,
4466 size_t ciphertext_size,
4467 size_t *ciphertext_length )
4468{
mohammad16035955c982018-04-26 00:53:03 +03004469 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004470 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004471 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004472
mohammad1603f08a5502018-06-03 15:05:47 +03004473 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004474
Gilles Peskinec5487a82018-12-03 18:08:14 +01004475 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004476 if( status != PSA_SUCCESS )
4477 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004478
Gilles Peskineedf9a652018-08-17 18:11:56 +02004479 /* For all currently supported modes, the tag is at the end of the
4480 * ciphertext. */
4481 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4482 {
4483 status = PSA_ERROR_BUFFER_TOO_SMALL;
4484 goto exit;
4485 }
4486 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004487
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004488#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004489 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004490 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004491 status = mbedtls_to_psa_error(
4492 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4493 MBEDTLS_GCM_ENCRYPT,
4494 plaintext_length,
4495 nonce, nonce_length,
4496 additional_data, additional_data_length,
4497 plaintext, ciphertext,
4498 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004499 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004500 else
4501#endif /* MBEDTLS_GCM_C */
4502#if defined(MBEDTLS_CCM_C)
4503 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004504 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004505 status = mbedtls_to_psa_error(
4506 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4507 plaintext_length,
4508 nonce, nonce_length,
4509 additional_data,
4510 additional_data_length,
4511 plaintext, ciphertext,
4512 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004513 }
mohammad16035c8845f2018-05-09 05:40:09 -07004514 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004515#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004516#if defined(MBEDTLS_CHACHAPOLY_C)
4517 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4518 {
4519 if( nonce_length != 12 || operation.tag_length != 16 )
4520 {
4521 status = PSA_ERROR_NOT_SUPPORTED;
4522 goto exit;
4523 }
4524 status = mbedtls_to_psa_error(
4525 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4526 plaintext_length,
4527 nonce,
4528 additional_data,
4529 additional_data_length,
4530 plaintext,
4531 ciphertext,
4532 tag ) );
4533 }
4534 else
4535#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004536 {
mohammad1603554faad2018-06-03 15:07:38 +03004537 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004538 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004539
Gilles Peskineedf9a652018-08-17 18:11:56 +02004540 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4541 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004542
Gilles Peskineedf9a652018-08-17 18:11:56 +02004543exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004544 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004545 if( status == PSA_SUCCESS )
4546 *ciphertext_length = plaintext_length + operation.tag_length;
4547 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004548}
4549
Gilles Peskineee652a32018-06-01 19:23:52 +02004550/* Locate the tag in a ciphertext buffer containing the encrypted data
4551 * followed by the tag. Return the length of the part preceding the tag in
4552 * *plaintext_length. This is the size of the plaintext in modes where
4553 * the encrypted data has the same size as the plaintext, such as
4554 * CCM and GCM. */
4555static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4556 const uint8_t *ciphertext,
4557 size_t ciphertext_length,
4558 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004559 const uint8_t **p_tag )
4560{
4561 size_t payload_length;
4562 if( tag_length > ciphertext_length )
4563 return( PSA_ERROR_INVALID_ARGUMENT );
4564 payload_length = ciphertext_length - tag_length;
4565 if( payload_length > plaintext_size )
4566 return( PSA_ERROR_BUFFER_TOO_SMALL );
4567 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004568 return( PSA_SUCCESS );
4569}
4570
Gilles Peskinec5487a82018-12-03 18:08:14 +01004571psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004572 psa_algorithm_t alg,
4573 const uint8_t *nonce,
4574 size_t nonce_length,
4575 const uint8_t *additional_data,
4576 size_t additional_data_length,
4577 const uint8_t *ciphertext,
4578 size_t ciphertext_length,
4579 uint8_t *plaintext,
4580 size_t plaintext_size,
4581 size_t *plaintext_length )
4582{
mohammad16035955c982018-04-26 00:53:03 +03004583 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004584 aead_operation_t operation;
4585 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004586
Gilles Peskineee652a32018-06-01 19:23:52 +02004587 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004588
Gilles Peskinec5487a82018-12-03 18:08:14 +01004589 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004590 if( status != PSA_SUCCESS )
4591 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004592
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004593 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4594 ciphertext, ciphertext_length,
4595 plaintext_size, &tag );
4596 if( status != PSA_SUCCESS )
4597 goto exit;
4598
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004599#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004600 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004601 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004602 status = mbedtls_to_psa_error(
4603 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4604 ciphertext_length - operation.tag_length,
4605 nonce, nonce_length,
4606 additional_data,
4607 additional_data_length,
4608 tag, operation.tag_length,
4609 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004610 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004611 else
4612#endif /* MBEDTLS_GCM_C */
4613#if defined(MBEDTLS_CCM_C)
4614 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004615 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004616 status = mbedtls_to_psa_error(
4617 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4618 ciphertext_length - operation.tag_length,
4619 nonce, nonce_length,
4620 additional_data,
4621 additional_data_length,
4622 ciphertext, plaintext,
4623 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004624 }
mohammad160339574652018-06-01 04:39:53 -07004625 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004626#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004627#if defined(MBEDTLS_CHACHAPOLY_C)
4628 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4629 {
4630 if( nonce_length != 12 || operation.tag_length != 16 )
4631 {
4632 status = PSA_ERROR_NOT_SUPPORTED;
4633 goto exit;
4634 }
4635 status = mbedtls_to_psa_error(
4636 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4637 ciphertext_length - operation.tag_length,
4638 nonce,
4639 additional_data,
4640 additional_data_length,
4641 tag,
4642 ciphertext,
4643 plaintext ) );
4644 }
4645 else
4646#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004647 {
mohammad1603554faad2018-06-03 15:07:38 +03004648 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004649 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004650
Gilles Peskineedf9a652018-08-17 18:11:56 +02004651 if( status != PSA_SUCCESS && plaintext_size != 0 )
4652 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004653
Gilles Peskineedf9a652018-08-17 18:11:56 +02004654exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004655 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004656 if( status == PSA_SUCCESS )
4657 *plaintext_length = ciphertext_length - operation.tag_length;
4658 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004659}
4660
Gilles Peskinea0655c32018-04-30 17:06:50 +02004661
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004662
Gilles Peskine20035e32018-02-03 22:44:14 +01004663/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004664/* Generators */
4665/****************************************************************/
4666
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004667#define HKDF_STATE_INIT 0 /* no input yet */
4668#define HKDF_STATE_STARTED 1 /* got salt */
4669#define HKDF_STATE_KEYED 2 /* got key */
4670#define HKDF_STATE_OUTPUT 3 /* output started */
4671
Gilles Peskinecbe66502019-05-16 16:59:18 +02004672static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004673 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004674{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4676 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004677 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004678 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004679}
4680
4681
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004682psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004683{
4684 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004685 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004686 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004687 {
4688 /* The object has (apparently) been initialized but it is not
4689 * in use. It's ok to call abort on such an object, and there's
4690 * nothing to do. */
4691 }
4692 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004693#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004694 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004695 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004696 mbedtls_free( operation->ctx.hkdf.info );
4697 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004698 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004699 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004700 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004701 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004702 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004703 if( operation->ctx.tls12_prf.seed != NULL )
4704 {
4705 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4706 operation->ctx.tls12_prf.seed_length );
4707 mbedtls_free( operation->ctx.tls12_prf.seed );
4708 }
4709
4710 if( operation->ctx.tls12_prf.label != NULL )
4711 {
4712 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4713 operation->ctx.tls12_prf.label_length );
4714 mbedtls_free( operation->ctx.tls12_prf.label );
4715 }
4716
4717 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4718
4719 /* We leave the fields Ai and output_block to be erased safely by the
4720 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004721 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004722 else
4723#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004724 {
4725 status = PSA_ERROR_BAD_STATE;
4726 }
Janos Follath083036a2019-06-11 10:22:26 +01004727 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004728 return( status );
4729}
4730
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004731psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004732 size_t *capacity)
4733{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004735 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004737 return PSA_ERROR_BAD_STATE;
4738 }
4739
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004740 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004741 return( PSA_SUCCESS );
4742}
4743
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004745 size_t capacity )
4746{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004747 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004748 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004750 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004751 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004752 return( PSA_SUCCESS );
4753}
4754
Gilles Peskinebef7f142018-07-12 17:22:21 +02004755#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004756/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004757 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004758static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004759 psa_algorithm_t hash_alg,
4760 uint8_t *output,
4761 size_t output_length )
4762{
4763 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4764 psa_status_t status;
4765
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004766 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4767 return( PSA_ERROR_BAD_STATE );
4768 hkdf->state = HKDF_STATE_OUTPUT;
4769
Gilles Peskinebef7f142018-07-12 17:22:21 +02004770 while( output_length != 0 )
4771 {
4772 /* Copy what remains of the current block */
4773 uint8_t n = hash_length - hkdf->offset_in_block;
4774 if( n > output_length )
4775 n = (uint8_t) output_length;
4776 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4777 output += n;
4778 output_length -= n;
4779 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004780 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004781 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004782 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004783 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004784 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004785 * object was corrupted or if this function is called directly
4786 * inside the library. */
4787 if( hkdf->block_number == 0xff )
4788 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004789
4790 /* We need a new block */
4791 ++hkdf->block_number;
4792 hkdf->offset_in_block = 0;
4793 status = psa_hmac_setup_internal( &hkdf->hmac,
4794 hkdf->prk, hash_length,
4795 hash_alg );
4796 if( status != PSA_SUCCESS )
4797 return( status );
4798 if( hkdf->block_number != 1 )
4799 {
4800 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4801 hkdf->output_block,
4802 hash_length );
4803 if( status != PSA_SUCCESS )
4804 return( status );
4805 }
4806 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4807 hkdf->info,
4808 hkdf->info_length );
4809 if( status != PSA_SUCCESS )
4810 return( status );
4811 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4812 &hkdf->block_number, 1 );
4813 if( status != PSA_SUCCESS )
4814 return( status );
4815 status = psa_hmac_finish_internal( &hkdf->hmac,
4816 hkdf->output_block,
4817 sizeof( hkdf->output_block ) );
4818 if( status != PSA_SUCCESS )
4819 return( status );
4820 }
4821
4822 return( PSA_SUCCESS );
4823}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004824
Janos Follath7742fee2019-06-17 12:58:10 +01004825static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4826 psa_tls12_prf_key_derivation_t *tls12_prf,
4827 psa_algorithm_t alg )
4828{
4829 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4830 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004831 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4832 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004833
Janos Follath7742fee2019-06-17 12:58:10 +01004834 /* We can't be wanting more output after block 0xff, otherwise
4835 * the capacity check in psa_key_derivation_output_bytes() would have
4836 * prevented this call. It could happen only if the operation
4837 * object was corrupted or if this function is called directly
4838 * inside the library. */
4839 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004840 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004841
4842 /* We need a new block */
4843 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004844 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004845
4846 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4847 *
4848 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4849 *
4850 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4851 * HMAC_hash(secret, A(2) + seed) +
4852 * HMAC_hash(secret, A(3) + seed) + ...
4853 *
4854 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004855 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004856 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004857 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004858 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004859 * is currently extracted as `output_block` and where i is
4860 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004861 */
4862
Janos Follathea29bfb2019-06-19 12:21:20 +01004863 /* Save the hash context before using it, to preserve the hash state with
4864 * only the inner padding in it. We need this, because inner padding depends
4865 * on the key (secret in the RFC's terminology). */
4866 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4867 if( status != PSA_SUCCESS )
4868 goto cleanup;
4869
4870 /* Calculate A(i) where i = tls12_prf->block_number. */
4871 if( tls12_prf->block_number == 1 )
4872 {
4873 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4874 * the variable seed and in this instance means it in the context of the
4875 * P_hash function, where seed = label + seed.) */
4876 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4877 tls12_prf->label, tls12_prf->label_length );
4878 if( status != PSA_SUCCESS )
4879 goto cleanup;
4880 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4881 tls12_prf->seed, tls12_prf->seed_length );
4882 if( status != PSA_SUCCESS )
4883 goto cleanup;
4884 }
4885 else
4886 {
4887 /* A(i) = HMAC_hash(secret, A(i-1)) */
4888 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4889 tls12_prf->Ai, hash_length );
4890 if( status != PSA_SUCCESS )
4891 goto cleanup;
4892 }
4893
4894 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4895 tls12_prf->Ai, hash_length );
4896 if( status != PSA_SUCCESS )
4897 goto cleanup;
4898 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4899 if( status != PSA_SUCCESS )
4900 goto cleanup;
4901
4902 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4903 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4904 tls12_prf->Ai, hash_length );
4905 if( status != PSA_SUCCESS )
4906 goto cleanup;
4907 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4908 tls12_prf->label, tls12_prf->label_length );
4909 if( status != PSA_SUCCESS )
4910 goto cleanup;
4911 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4912 tls12_prf->seed, tls12_prf->seed_length );
4913 if( status != PSA_SUCCESS )
4914 goto cleanup;
4915 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4916 tls12_prf->output_block, hash_length );
4917 if( status != PSA_SUCCESS )
4918 goto cleanup;
4919 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4920 if( status != PSA_SUCCESS )
4921 goto cleanup;
4922
Janos Follath7742fee2019-06-17 12:58:10 +01004923
4924cleanup:
4925
Janos Follathea29bfb2019-06-19 12:21:20 +01004926 cleanup_status = psa_hash_abort( &backup );
4927 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4928 status = cleanup_status;
4929
4930 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004931}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004932
Janos Follath844eb0e2019-06-19 12:10:49 +01004933static psa_status_t psa_key_derivation_tls12_prf_read(
4934 psa_tls12_prf_key_derivation_t *tls12_prf,
4935 psa_algorithm_t alg,
4936 uint8_t *output,
4937 size_t output_length )
4938{
4939 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4940 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4941 psa_status_t status;
4942 uint8_t offset, length;
4943
4944 while( output_length != 0 )
4945 {
4946 /* Check if we have fully processed the current block. */
4947 if( tls12_prf->left_in_block == 0 )
4948 {
4949 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4950 alg );
4951 if( status != PSA_SUCCESS )
4952 return( status );
4953
4954 continue;
4955 }
4956
4957 if( tls12_prf->left_in_block > output_length )
4958 length = (uint8_t) output_length;
4959 else
4960 length = tls12_prf->left_in_block;
4961
4962 offset = hash_length - tls12_prf->left_in_block;
4963 memcpy( output, tls12_prf->output_block + offset, length );
4964 output += length;
4965 output_length -= length;
4966 tls12_prf->left_in_block -= length;
4967 }
4968
4969 return( PSA_SUCCESS );
4970}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004971#endif /* MBEDTLS_MD_C */
4972
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004973psa_status_t psa_key_derivation_output_bytes(
4974 psa_key_derivation_operation_t *operation,
4975 uint8_t *output,
4976 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004977{
4978 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004979 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004980
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004981 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004982 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004983 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004984 return PSA_ERROR_BAD_STATE;
4985 }
4986
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004987 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004988 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004989 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004990 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004991 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004992 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004993 goto exit;
4994 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004995 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004996 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004997 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004998 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004999 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5000 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005001 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005002 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005003 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005004 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005005 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005006
Gilles Peskinebef7f142018-07-12 17:22:21 +02005007#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005008 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005009 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005010 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005011 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005012 output, output_length );
5013 }
Janos Follathadbec812019-06-14 11:05:39 +01005014 else
Janos Follathadbec812019-06-14 11:05:39 +01005015 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005016 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005017 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005018 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005019 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005020 output_length );
5021 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005022 else
5023#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005024 {
5025 return( PSA_ERROR_BAD_STATE );
5026 }
5027
5028exit:
5029 if( status != PSA_SUCCESS )
5030 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005031 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 * This allows us to differentiate between exhausted operations and
5033 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5034 * operations. */
5035 psa_algorithm_t alg = operation->alg;
5036 psa_key_derivation_abort( operation );
5037 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005038 memset( output, '!', output_length );
5039 }
5040 return( status );
5041}
5042
Gilles Peskine08542d82018-07-19 17:05:42 +02005043#if defined(MBEDTLS_DES_C)
5044static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5045{
5046 if( data_size >= 8 )
5047 mbedtls_des_key_set_parity( data );
5048 if( data_size >= 16 )
5049 mbedtls_des_key_set_parity( data + 8 );
5050 if( data_size >= 24 )
5051 mbedtls_des_key_set_parity( data + 16 );
5052}
5053#endif /* MBEDTLS_DES_C */
5054
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005055static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005056 psa_key_slot_t *slot,
5057 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005058 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005059{
5060 uint8_t *data = NULL;
5061 size_t bytes = PSA_BITS_TO_BYTES( bits );
5062 psa_status_t status;
5063
Gilles Peskine8e338702019-07-30 20:06:31 +02005064 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005065 return( PSA_ERROR_INVALID_ARGUMENT );
5066 if( bits % 8 != 0 )
5067 return( PSA_ERROR_INVALID_ARGUMENT );
5068 data = mbedtls_calloc( 1, bytes );
5069 if( data == NULL )
5070 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5071
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005072 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005073 if( status != PSA_SUCCESS )
5074 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005075#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005076 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005077 psa_des_set_key_parity( data, bytes );
5078#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005079 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005080
5081exit:
5082 mbedtls_free( data );
5083 return( status );
5084}
5085
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005086psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005087 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005088 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005089{
5090 psa_status_t status;
5091 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005092 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005093
5094 /* Reject any attempt to create a zero-length key so that we don't
5095 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5096 if( psa_get_key_bits( attributes ) == 0 )
5097 return( PSA_ERROR_INVALID_ARGUMENT );
5098
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005099 if( ! operation->can_output_key )
5100 return( PSA_ERROR_NOT_PERMITTED );
5101
Gilles Peskinedf179142019-07-15 22:02:14 +02005102 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5103 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005104#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5105 if( driver != NULL )
5106 {
5107 /* Deriving a key in a secure element is not implemented yet. */
5108 status = PSA_ERROR_NOT_SUPPORTED;
5109 }
5110#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005111 if( status == PSA_SUCCESS )
5112 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005113 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005114 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005115 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005116 }
5117 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005118 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005119 if( status != PSA_SUCCESS )
5120 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005121 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005122 *handle = 0;
5123 }
5124 return( status );
5125}
5126
Gilles Peskineeab56e42018-07-12 17:12:33 +02005127
5128
5129/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005130/* Key derivation */
5131/****************************************************************/
5132
Gilles Peskine969c5d62019-01-16 15:53:06 +01005133static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005134 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005135 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005136{
Janos Follath5fe19732019-06-20 15:09:30 +01005137 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5138 * initialisers for this union leave some bytes unspecified.) */
5139 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5140
Gilles Peskine969c5d62019-01-16 15:53:06 +01005141 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005142#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005143 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5144 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5145 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005146 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005147 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005148 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5149 if( hash_size == 0 )
5150 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005151 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5152 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005153 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005154 {
5155 return( PSA_ERROR_NOT_SUPPORTED );
5156 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005157 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005158 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005159 }
5160#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005161 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005162 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005163}
5164
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005165psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005166 psa_algorithm_t alg )
5167{
5168 psa_status_t status;
5169
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005170 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005171 return( PSA_ERROR_BAD_STATE );
5172
Gilles Peskine6843c292019-01-18 16:44:49 +01005173 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5174 return( PSA_ERROR_INVALID_ARGUMENT );
5175 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005176 {
5177 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005178 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005179 }
5180 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5181 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005182 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005183 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005184 else
5185 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005186
5187 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005188 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005189 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005190}
5191
5192#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005193static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005194 psa_algorithm_t hash_alg,
5195 psa_key_derivation_step_t step,
5196 const uint8_t *data,
5197 size_t data_length )
5198{
5199 psa_status_t status;
5200 switch( step )
5201 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005202 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005203 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005204 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005205 status = psa_hmac_setup_internal( &hkdf->hmac,
5206 data, data_length,
5207 hash_alg );
5208 if( status != PSA_SUCCESS )
5209 return( status );
5210 hkdf->state = HKDF_STATE_STARTED;
5211 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005212 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005213 /* If no salt was provided, use an empty salt. */
5214 if( hkdf->state == HKDF_STATE_INIT )
5215 {
5216 status = psa_hmac_setup_internal( &hkdf->hmac,
5217 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005218 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005219 if( status != PSA_SUCCESS )
5220 return( status );
5221 hkdf->state = HKDF_STATE_STARTED;
5222 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005223 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005224 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005225 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5226 data, data_length );
5227 if( status != PSA_SUCCESS )
5228 return( status );
5229 status = psa_hmac_finish_internal( &hkdf->hmac,
5230 hkdf->prk,
5231 sizeof( hkdf->prk ) );
5232 if( status != PSA_SUCCESS )
5233 return( status );
5234 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5235 hkdf->block_number = 0;
5236 hkdf->state = HKDF_STATE_KEYED;
5237 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005238 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005239 if( hkdf->state == HKDF_STATE_OUTPUT )
5240 return( PSA_ERROR_BAD_STATE );
5241 if( hkdf->info_set )
5242 return( PSA_ERROR_BAD_STATE );
5243 hkdf->info_length = data_length;
5244 if( data_length != 0 )
5245 {
5246 hkdf->info = mbedtls_calloc( 1, data_length );
5247 if( hkdf->info == NULL )
5248 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5249 memcpy( hkdf->info, data, data_length );
5250 }
5251 hkdf->info_set = 1;
5252 return( PSA_SUCCESS );
5253 default:
5254 return( PSA_ERROR_INVALID_ARGUMENT );
5255 }
5256}
Janos Follathb03233e2019-06-11 15:30:30 +01005257
Janos Follathf08e2652019-06-13 09:05:41 +01005258static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5259 const uint8_t *data,
5260 size_t data_length )
5261{
5262 if( prf->state != TLS12_PRF_STATE_INIT )
5263 return( PSA_ERROR_BAD_STATE );
5264
Janos Follathd6dce9f2019-07-04 09:11:38 +01005265 if( data_length != 0 )
5266 {
5267 prf->seed = mbedtls_calloc( 1, data_length );
5268 if( prf->seed == NULL )
5269 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005270
Janos Follathd6dce9f2019-07-04 09:11:38 +01005271 memcpy( prf->seed, data, data_length );
5272 prf->seed_length = data_length;
5273 }
Janos Follathf08e2652019-06-13 09:05:41 +01005274
5275 prf->state = TLS12_PRF_STATE_SEED_SET;
5276
5277 return( PSA_SUCCESS );
5278}
5279
Janos Follath81550542019-06-13 14:26:34 +01005280static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5281 psa_algorithm_t hash_alg,
5282 const uint8_t *data,
5283 size_t data_length )
5284{
5285 psa_status_t status;
5286 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5287 return( PSA_ERROR_BAD_STATE );
5288
5289 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5290 if( status != PSA_SUCCESS )
5291 return( status );
5292
5293 prf->state = TLS12_PRF_STATE_KEY_SET;
5294
5295 return( PSA_SUCCESS );
5296}
5297
Janos Follath6660f0e2019-06-17 08:44:03 +01005298static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5299 psa_tls12_prf_key_derivation_t *prf,
5300 psa_algorithm_t hash_alg,
5301 const uint8_t *data,
5302 size_t data_length )
5303{
5304 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005305 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5306 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005307
5308 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5309 return( PSA_ERROR_INVALID_ARGUMENT );
5310
5311 /* Quoting RFC 4279, Section 2:
5312 *
5313 * The premaster secret is formed as follows: if the PSK is N octets
5314 * long, concatenate a uint16 with the value N, N zero octets, a second
5315 * uint16 with the value N, and the PSK itself.
5316 */
5317
Janos Follath40e13932019-06-26 13:22:29 +01005318 *cur++ = ( data_length >> 8 ) & 0xff;
5319 *cur++ = ( data_length >> 0 ) & 0xff;
5320 memset( cur, 0, data_length );
5321 cur += data_length;
5322 *cur++ = pms[0];
5323 *cur++ = pms[1];
5324 memcpy( cur, data, data_length );
5325 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005326
Janos Follath40e13932019-06-26 13:22:29 +01005327 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005328
5329 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5330 return( status );
5331}
5332
Janos Follath63028dd2019-06-13 09:15:47 +01005333static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5334 const uint8_t *data,
5335 size_t data_length )
5336{
5337 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5338 return( PSA_ERROR_BAD_STATE );
5339
Janos Follathd6dce9f2019-07-04 09:11:38 +01005340 if( data_length != 0 )
5341 {
5342 prf->label = mbedtls_calloc( 1, data_length );
5343 if( prf->label == NULL )
5344 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005345
Janos Follathd6dce9f2019-07-04 09:11:38 +01005346 memcpy( prf->label, data, data_length );
5347 prf->label_length = data_length;
5348 }
Janos Follath63028dd2019-06-13 09:15:47 +01005349
5350 prf->state = TLS12_PRF_STATE_LABEL_SET;
5351
5352 return( PSA_SUCCESS );
5353}
5354
Janos Follathb03233e2019-06-11 15:30:30 +01005355static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5356 psa_algorithm_t hash_alg,
5357 psa_key_derivation_step_t step,
5358 const uint8_t *data,
5359 size_t data_length )
5360{
Janos Follathb03233e2019-06-11 15:30:30 +01005361 switch( step )
5362 {
Janos Follathf08e2652019-06-13 09:05:41 +01005363 case PSA_KEY_DERIVATION_INPUT_SEED:
5364 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005365 case PSA_KEY_DERIVATION_INPUT_SECRET:
5366 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005367 case PSA_KEY_DERIVATION_INPUT_LABEL:
5368 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005369 default:
5370 return( PSA_ERROR_INVALID_ARGUMENT );
5371 }
5372}
Janos Follath6660f0e2019-06-17 08:44:03 +01005373
5374static psa_status_t psa_tls12_prf_psk_to_ms_input(
5375 psa_tls12_prf_key_derivation_t *prf,
5376 psa_algorithm_t hash_alg,
5377 psa_key_derivation_step_t step,
5378 const uint8_t *data,
5379 size_t data_length )
5380{
5381 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005382 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005383 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5384 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005385 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005386
5387 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5388}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005389#endif /* MBEDTLS_MD_C */
5390
Gilles Peskineb8965192019-09-24 16:21:10 +02005391/** Check whether the given key type is acceptable for the given
5392 * input step of a key derivation.
5393 *
5394 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5395 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5396 * Both secret and non-secret inputs can alternatively have the type
5397 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5398 * that the input was passed as a buffer rather than via a key object.
5399 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005400static int psa_key_derivation_check_input_type(
5401 psa_key_derivation_step_t step,
5402 psa_key_type_t key_type )
5403{
5404 switch( step )
5405 {
5406 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005407 if( key_type == PSA_KEY_TYPE_DERIVE )
5408 return( PSA_SUCCESS );
5409 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005410 return( PSA_SUCCESS );
5411 break;
5412 case PSA_KEY_DERIVATION_INPUT_LABEL:
5413 case PSA_KEY_DERIVATION_INPUT_SALT:
5414 case PSA_KEY_DERIVATION_INPUT_INFO:
5415 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005416 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5417 return( PSA_SUCCESS );
5418 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005419 return( PSA_SUCCESS );
5420 break;
5421 }
5422 return( PSA_ERROR_INVALID_ARGUMENT );
5423}
5424
Janos Follathb80a94e2019-06-12 15:54:46 +01005425static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005426 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005427 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005428 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005429 const uint8_t *data,
5430 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005431{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005432 psa_status_t status;
5433 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5434
5435 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005436 if( status != PSA_SUCCESS )
5437 goto exit;
5438
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005439#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005440 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005441 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005442 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005443 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005444 step, data, data_length );
5445 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005446 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005447 {
Janos Follathb03233e2019-06-11 15:30:30 +01005448 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5449 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5450 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005451 }
5452 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5453 {
5454 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5455 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5456 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005457 }
5458 else
5459#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005460 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005461 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005462 return( PSA_ERROR_BAD_STATE );
5463 }
5464
Gilles Peskine224b0d62019-09-23 18:13:17 +02005465exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005466 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005467 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005468 return( status );
5469}
5470
Janos Follath51f4a0f2019-06-14 11:35:55 +01005471psa_status_t psa_key_derivation_input_bytes(
5472 psa_key_derivation_operation_t *operation,
5473 psa_key_derivation_step_t step,
5474 const uint8_t *data,
5475 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005476{
Gilles Peskineb8965192019-09-24 16:21:10 +02005477 return( psa_key_derivation_input_internal( operation, step,
5478 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005479 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005480}
5481
Janos Follath51f4a0f2019-06-14 11:35:55 +01005482psa_status_t psa_key_derivation_input_key(
5483 psa_key_derivation_operation_t *operation,
5484 psa_key_derivation_step_t step,
5485 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005486{
5487 psa_key_slot_t *slot;
5488 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005489
Gilles Peskine28f8f302019-07-24 13:30:31 +02005490 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005491 PSA_KEY_USAGE_DERIVE,
5492 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005493 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005494 {
5495 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005496 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005497 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005498
5499 /* Passing a key object as a SECRET input unlocks the permission
5500 * to output to a key object. */
5501 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5502 operation->can_output_key = 1;
5503
Janos Follathb80a94e2019-06-12 15:54:46 +01005504 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005505 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005506 slot->data.key.data,
5507 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005508}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005509
5510
5511
5512/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005513/* Key agreement */
5514/****************************************************************/
5515
Gilles Peskinea05219c2018-11-16 16:02:56 +01005516#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005517static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5518 size_t peer_key_length,
5519 const mbedtls_ecp_keypair *our_key,
5520 uint8_t *shared_secret,
5521 size_t shared_secret_size,
5522 size_t *shared_secret_length )
5523{
Steven Cooremanacda8342020-07-24 23:09:52 +02005524 mbedtls_ecp_keypair their_key;
5525 psa_key_slot_t their_key_slot;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005526 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005527 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005528 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005529 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005530 mbedtls_ecdh_init( &ecdh );
Steven Cooremanacda8342020-07-24 23:09:52 +02005531 memset(&their_key_slot, 0, sizeof(their_key_slot));
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005532
Steven Cooremanacda8342020-07-24 23:09:52 +02005533 /* Creating ephemeral key slot for import purposes */
5534 their_key_slot.attr.type = PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve);
5535 their_key_slot.data.key.data = (uint8_t*) peer_key;
5536 their_key_slot.data.key.bytes = peer_key_length;
5537
5538 status = psa_load_ecp_representation( &their_key_slot, &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005539 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005540 goto exit;
5541
Jaeden Amero97271b32019-01-10 19:38:51 +00005542 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02005543 mbedtls_ecdh_get_params( &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005544 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005545 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005546 status = mbedtls_to_psa_error(
5547 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5548 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005549 goto exit;
5550
Jaeden Amero97271b32019-01-10 19:38:51 +00005551 status = mbedtls_to_psa_error(
5552 mbedtls_ecdh_calc_secret( &ecdh,
5553 shared_secret_length,
5554 shared_secret, shared_secret_size,
5555 mbedtls_ctr_drbg_random,
5556 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005557 if( status != PSA_SUCCESS )
5558 goto exit;
5559 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5560 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005561
5562exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005563 if( status != PSA_SUCCESS )
5564 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005565 mbedtls_ecdh_free( &ecdh );
Steven Cooremanacda8342020-07-24 23:09:52 +02005566 mbedtls_ecp_keypair_free( &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005567 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005568}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005569#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005570
Gilles Peskine01d718c2018-09-18 12:01:02 +02005571#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5572
Gilles Peskine0216fe12019-04-11 21:23:21 +02005573static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5574 psa_key_slot_t *private_key,
5575 const uint8_t *peer_key,
5576 size_t peer_key_length,
5577 uint8_t *shared_secret,
5578 size_t shared_secret_size,
5579 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005580{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005581 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005582 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005583#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005584 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005585 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005586 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005587 mbedtls_ecp_keypair ecp;
5588 psa_status_t status = psa_load_ecp_representation( private_key, &ecp );
5589 if( status != PSA_SUCCESS )
5590 return status;
5591 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
5592 &ecp,
5593 shared_secret, shared_secret_size,
5594 shared_secret_length );
5595 mbedtls_ecp_keypair_free( &ecp );
5596 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005597#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005598 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005599 (void) private_key;
5600 (void) peer_key;
5601 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005602 (void) shared_secret;
5603 (void) shared_secret_size;
5604 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005605 return( PSA_ERROR_NOT_SUPPORTED );
5606 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005607}
5608
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005609/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005610 * to potentially free embedded data structures and wipe confidential data.
5611 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005612static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005613 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005614 psa_key_slot_t *private_key,
5615 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005616 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005617{
5618 psa_status_t status;
5619 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5620 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005621 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005622
5623 /* Step 1: run the secret agreement algorithm to generate the shared
5624 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005625 status = psa_key_agreement_raw_internal( ka_alg,
5626 private_key,
5627 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005628 shared_secret,
5629 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005630 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005631 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005632 goto exit;
5633
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005634 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005635 * the shared secret. A shared secret is permitted wherever a key
5636 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005637 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005638 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005639 shared_secret,
5640 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005641
Gilles Peskine12313cd2018-06-20 00:20:32 +02005642exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005643 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005644 return( status );
5645}
5646
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005647psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005648 psa_key_derivation_step_t step,
5649 psa_key_handle_t private_key,
5650 const uint8_t *peer_key,
5651 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005652{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005653 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005654 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005655 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005656 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005657 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005658 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005659 if( status != PSA_SUCCESS )
5660 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005661 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005662 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005663 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005664 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005665 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005666 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005667}
5668
Gilles Peskinebe697d82019-05-16 18:00:41 +02005669psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5670 psa_key_handle_t private_key,
5671 const uint8_t *peer_key,
5672 size_t peer_key_length,
5673 uint8_t *output,
5674 size_t output_size,
5675 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005676{
5677 psa_key_slot_t *slot;
5678 psa_status_t status;
5679
5680 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5681 {
5682 status = PSA_ERROR_INVALID_ARGUMENT;
5683 goto exit;
5684 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005685 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005686 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005687 if( status != PSA_SUCCESS )
5688 goto exit;
5689
5690 status = psa_key_agreement_raw_internal( alg, slot,
5691 peer_key, peer_key_length,
5692 output, output_size,
5693 output_length );
5694
5695exit:
5696 if( status != PSA_SUCCESS )
5697 {
5698 /* If an error happens and is not handled properly, the output
5699 * may be used as a key to protect sensitive data. Arrange for such
5700 * a key to be random, which is likely to result in decryption or
5701 * verification errors. This is better than filling the buffer with
5702 * some constant data such as zeros, which would result in the data
5703 * being protected with a reproducible, easily knowable key.
5704 */
5705 psa_generate_random( output, output_size );
5706 *output_length = output_size;
5707 }
5708 return( status );
5709}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005710
5711
5712/****************************************************************/
5713/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005714/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005715
Gilles Peskine4c317f42018-07-12 01:24:09 +02005716psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005717 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005718{
Janos Follath24eed8d2019-11-22 13:21:35 +00005719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005720 GUARD_MODULE_INITIALIZED;
5721
Gilles Peskinef181eca2019-08-07 13:49:00 +02005722 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5723 {
5724 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5725 output,
5726 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5727 if( ret != 0 )
5728 return( mbedtls_to_psa_error( ret ) );
5729 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5730 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5731 }
5732
Gilles Peskinee59236f2018-01-27 23:32:46 +01005733 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5734 return( mbedtls_to_psa_error( ret ) );
5735}
5736
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005737#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5738#include "mbedtls/entropy_poll.h"
5739
Gilles Peskine7228da22019-07-15 11:06:15 +02005740psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005741 size_t seed_size )
5742{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005743 if( global_data.initialized )
5744 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005745
5746 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5747 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5748 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5749 return( PSA_ERROR_INVALID_ARGUMENT );
5750
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005751 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005752}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005753#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005754
Gilles Peskinee56e8782019-04-26 17:34:02 +02005755#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5756static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5757 size_t domain_parameters_size,
5758 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005759{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005760 size_t i;
5761 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005762
Gilles Peskinee56e8782019-04-26 17:34:02 +02005763 if( domain_parameters_size == 0 )
5764 {
5765 *exponent = 65537;
5766 return( PSA_SUCCESS );
5767 }
5768
5769 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5770 * support values that fit in a 32-bit integer, which is larger than
5771 * int on just about every platform anyway. */
5772 if( domain_parameters_size > sizeof( acc ) )
5773 return( PSA_ERROR_NOT_SUPPORTED );
5774 for( i = 0; i < domain_parameters_size; i++ )
5775 acc = ( acc << 8 ) | domain_parameters[i];
5776 if( acc > INT_MAX )
5777 return( PSA_ERROR_NOT_SUPPORTED );
5778 *exponent = acc;
5779 return( PSA_SUCCESS );
5780}
5781#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5782
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005783static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005784 psa_key_slot_t *slot, size_t bits,
5785 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005786{
Gilles Peskine8e338702019-07-30 20:06:31 +02005787 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005788
Gilles Peskinee56e8782019-04-26 17:34:02 +02005789 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005790 return( PSA_ERROR_INVALID_ARGUMENT );
5791
Gilles Peskinee59236f2018-01-27 23:32:46 +01005792 if( key_type_is_raw_bytes( type ) )
5793 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005794 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005795
5796 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005797 if( status != PSA_SUCCESS )
5798 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005799
5800 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02005801 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
5802 if( status != PSA_SUCCESS )
5803 return status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005804
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005805 status = psa_generate_random( slot->data.key.data,
5806 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005807 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005808 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005809
5810 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005811#if defined(MBEDTLS_DES_C)
5812 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005813 psa_des_set_key_parity( slot->data.key.data,
5814 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005815#endif /* MBEDTLS_DES_C */
5816 }
5817 else
5818
5819#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005820 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005821 {
Steven Cooremana01795d2020-07-24 22:48:15 +02005822 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005823 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005824 int exponent;
5825 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005826 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5827 return( PSA_ERROR_NOT_SUPPORTED );
5828 /* Accept only byte-aligned keys, for the same reasons as
5829 * in psa_import_rsa_key(). */
5830 if( bits % 8 != 0 )
5831 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005832 status = psa_read_rsa_exponent( domain_parameters,
5833 domain_parameters_size,
5834 &exponent );
5835 if( status != PSA_SUCCESS )
5836 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02005837 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5838 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005839 mbedtls_ctr_drbg_random,
5840 &global_data.ctr_drbg,
5841 (unsigned int) bits,
5842 exponent );
5843 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005844 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02005845
5846 /* Make sure to always have an export representation available */
5847 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
5848
Steven Cooreman75b74362020-07-28 14:30:13 +02005849 status = psa_allocate_buffer_to_slot( slot, bytes );
5850 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005851 {
5852 mbedtls_rsa_free( &rsa );
Steven Cooreman75b74362020-07-28 14:30:13 +02005853 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005854 }
Steven Cooremana01795d2020-07-24 22:48:15 +02005855
5856 status = psa_export_rsa_key( type,
5857 &rsa,
5858 slot->data.key.data,
5859 bytes,
5860 &slot->data.key.bytes );
5861 mbedtls_rsa_free( &rsa );
5862 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005863 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005864 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005865 }
5866 else
5867#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5868
5869#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005870 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005871 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005872 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005873 mbedtls_ecp_group_id grp_id =
5874 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005875 const mbedtls_ecp_curve_info *curve_info =
5876 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02005877 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005879 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005880 return( PSA_ERROR_NOT_SUPPORTED );
5881 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5882 return( PSA_ERROR_NOT_SUPPORTED );
5883 if( curve_info->bit_size != bits )
5884 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005885 mbedtls_ecp_keypair_init( &ecp );
5886 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005887 mbedtls_ctr_drbg_random,
5888 &global_data.ctr_drbg );
5889 if( ret != 0 )
5890 {
Steven Cooremanacda8342020-07-24 23:09:52 +02005891 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005892 return( mbedtls_to_psa_error( ret ) );
5893 }
Steven Cooremanacda8342020-07-24 23:09:52 +02005894
5895
5896 /* Make sure to always have an export representation available */
5897 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02005898 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
5899 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005900 {
5901 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman75b74362020-07-28 14:30:13 +02005902 return status;
Steven Cooremanacda8342020-07-24 23:09:52 +02005903 }
Steven Cooreman75b74362020-07-28 14:30:13 +02005904
5905 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02005906 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
5907
5908 mbedtls_ecp_keypair_free( &ecp );
5909 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005910 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005911 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005912 }
5913 else
5914#endif /* MBEDTLS_ECP_C */
5915
5916 return( PSA_ERROR_NOT_SUPPORTED );
5917
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005918 return( PSA_SUCCESS );
5919}
Darryl Green0c6575a2018-11-07 16:05:30 +00005920
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005921psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005922 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005923{
5924 psa_status_t status;
5925 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005926 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005927
Gilles Peskine0f84d622019-09-12 19:03:13 +02005928 /* Reject any attempt to create a zero-length key so that we don't
5929 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5930 if( psa_get_key_bits( attributes ) == 0 )
5931 return( PSA_ERROR_INVALID_ARGUMENT );
5932
Gilles Peskinedf179142019-07-15 22:02:14 +02005933 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5934 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005935 if( status != PSA_SUCCESS )
5936 goto exit;
5937
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005938#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5939 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005940 {
Gilles Peskine11792082019-08-06 18:36:36 +02005941 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5942 size_t pubkey_length = 0; /* We don't support this feature yet */
5943 if( drv->key_management == NULL ||
5944 drv->key_management->p_generate == NULL )
5945 {
5946 status = PSA_ERROR_NOT_SUPPORTED;
5947 goto exit;
5948 }
5949 status = drv->key_management->p_generate(
5950 psa_get_se_driver_context( driver ),
5951 slot->data.se.slot_number, attributes,
5952 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005953 }
Gilles Peskine11792082019-08-06 18:36:36 +02005954 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005955#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005956 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005957 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005958 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005959 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005960 }
Gilles Peskine11792082019-08-06 18:36:36 +02005961
5962exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005963 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005964 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005965 if( status != PSA_SUCCESS )
5966 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005967 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005968 *handle = 0;
5969 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005970 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005971}
5972
5973
Gilles Peskinee59236f2018-01-27 23:32:46 +01005974
5975/****************************************************************/
5976/* Module setup */
5977/****************************************************************/
5978
Gilles Peskine5e769522018-11-20 21:59:56 +01005979psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5980 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5981 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5982{
5983 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5984 return( PSA_ERROR_BAD_STATE );
5985 global_data.entropy_init = entropy_init;
5986 global_data.entropy_free = entropy_free;
5987 return( PSA_SUCCESS );
5988}
5989
Gilles Peskinee59236f2018-01-27 23:32:46 +01005990void mbedtls_psa_crypto_free( void )
5991{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005992 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005993 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5994 {
5995 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005996 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005997 }
5998 /* Wipe all remaining data, including configuration.
5999 * In particular, this sets all state indicator to the value
6000 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006001 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006002#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006003 /* Unregister all secure element drivers, so that we restart from
6004 * a pristine state. */
6005 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006006#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006007}
6008
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006009#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6010/** Recover a transaction that was interrupted by a power failure.
6011 *
6012 * This function is called during initialization, before psa_crypto_init()
6013 * returns. If this function returns a failure status, the initialization
6014 * fails.
6015 */
6016static psa_status_t psa_crypto_recover_transaction(
6017 const psa_crypto_transaction_t *transaction )
6018{
6019 switch( transaction->unknown.type )
6020 {
6021 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6022 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006023 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006024 * is implemented.
6025 * https://github.com/ARMmbed/mbed-crypto/issues/218
6026 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006027 default:
6028 /* We found an unsupported transaction in the storage.
6029 * We don't know what state the storage is in. Give up. */
6030 return( PSA_ERROR_STORAGE_FAILURE );
6031 }
6032}
6033#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6034
Gilles Peskinee59236f2018-01-27 23:32:46 +01006035psa_status_t psa_crypto_init( void )
6036{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006037 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006038 const unsigned char drbg_seed[] = "PSA";
6039
Gilles Peskinec6b69072018-11-20 21:42:52 +01006040 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006041 if( global_data.initialized != 0 )
6042 return( PSA_SUCCESS );
6043
Gilles Peskine5e769522018-11-20 21:59:56 +01006044 /* Set default configuration if
6045 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6046 if( global_data.entropy_init == NULL )
6047 global_data.entropy_init = mbedtls_entropy_init;
6048 if( global_data.entropy_free == NULL )
6049 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006050
Gilles Peskinec6b69072018-11-20 21:42:52 +01006051 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006052 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006053#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6054 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6055 /* The PSA entropy injection feature depends on using NV seed as an entropy
6056 * source. Add NV seed as an entropy source for PSA entropy injection. */
6057 mbedtls_entropy_add_source( &global_data.entropy,
6058 mbedtls_nv_seed_poll, NULL,
6059 MBEDTLS_ENTROPY_BLOCK_SIZE,
6060 MBEDTLS_ENTROPY_SOURCE_STRONG );
6061#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006062 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006063 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006064 status = mbedtls_to_psa_error(
6065 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6066 mbedtls_entropy_func,
6067 &global_data.entropy,
6068 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6069 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006070 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006071 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006072
Gilles Peskine66fb1262018-12-10 16:29:04 +01006073 status = psa_initialize_key_slots( );
6074 if( status != PSA_SUCCESS )
6075 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006076
Gilles Peskined9348f22019-10-01 15:22:29 +02006077#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6078 status = psa_init_all_se_drivers( );
6079 if( status != PSA_SUCCESS )
6080 goto exit;
6081#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6082
Gilles Peskine4b734222019-07-24 15:56:31 +02006083#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006084 status = psa_crypto_load_transaction( );
6085 if( status == PSA_SUCCESS )
6086 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006087 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6088 if( status != PSA_SUCCESS )
6089 goto exit;
6090 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006091 }
6092 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6093 {
6094 /* There's no transaction to complete. It's all good. */
6095 status = PSA_SUCCESS;
6096 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006097#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006098
Gilles Peskinec6b69072018-11-20 21:42:52 +01006099 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006100 global_data.initialized = 1;
6101
6102exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006103 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006104 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006105 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006106}
6107
6108#endif /* MBEDTLS_PSA_CRYPTO_C */