blob: 1901281c57b40552d91715293560b930eef24162 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
itayzafrir7723ab12019-02-14 10:28:02 +020025#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010026#include "psa/crypto.h"
27
Gilles Peskine039b90c2018-12-07 18:24:41 +010028#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010029#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020030#include "psa_crypto_driver_wrappers.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020031#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020032#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020033#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010034#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010035/* Include internal declarations that are useful for implementing persistently
36 * stored keys. */
37#include "psa_crypto_storage.h"
38
Gilles Peskineb46bef22019-07-30 21:32:04 +020039#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include <stdlib.h>
41#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020043#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#define mbedtls_calloc calloc
45#define mbedtls_free free
46#endif
47
Gilles Peskinea5905292018-02-07 20:59:33 +010048#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020049#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000050#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020051#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/blowfish.h"
53#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020054#include "mbedtls/chacha20.h"
55#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/cipher.h"
57#include "mbedtls/ccm.h"
58#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010059#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020061#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010062#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/error.h"
65#include "mbedtls/gcm.h"
66#include "mbedtls/md2.h"
67#include "mbedtls/md4.h"
68#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010069#include "mbedtls/md.h"
70#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010071#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010073#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000074#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/sha1.h"
78#include "mbedtls/sha256.h"
79#include "mbedtls/sha512.h"
80#include "mbedtls/xtea.h"
81
Gilles Peskine996deb12018-08-01 15:45:45 +020082#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
83
Gilles Peskine9ef733f2018-02-07 21:05:37 +010084/* constant-time buffer comparison */
85static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
86{
87 size_t i;
88 unsigned char diff = 0;
89
90 for( i = 0; i < n; i++ )
91 diff |= a[i] ^ b[i];
92
93 return( diff );
94}
95
96
97
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010098/****************************************************************/
99/* Global data, support functions and library management */
100/****************************************************************/
101
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102static int key_type_is_raw_bytes( psa_key_type_t type )
103{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200104 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105}
106
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100107/* Values for psa_global_data_t::rng_state */
108#define RNG_NOT_INITIALIZED 0
109#define RNG_INITIALIZED 1
110#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100111
Gilles Peskine2d277862018-06-18 15:41:12 +0200112typedef struct
113{
Gilles Peskine5e769522018-11-20 21:59:56 +0100114 void (* entropy_init )( mbedtls_entropy_context *ctx );
115 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100116 mbedtls_entropy_context entropy;
117 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100118 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100119 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120} psa_global_data_t;
121
122static psa_global_data_t global_data;
123
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124#define GUARD_MODULE_INITIALIZED \
125 if( global_data.initialized == 0 ) \
126 return( PSA_ERROR_BAD_STATE );
127
Steven Cooreman01164162020-07-20 15:31:37 +0200128psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100129{
Gilles Peskinea5905292018-02-07 20:59:33 +0100130 /* If there's both a high-level code and low-level code, dispatch on
131 * the high-level code. */
132 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133 {
134 case 0:
135 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100136
137 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
138 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
139 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
140 return( PSA_ERROR_NOT_SUPPORTED );
141 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
142 return( PSA_ERROR_HARDWARE_FAILURE );
143
144 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
Gilles Peskine9a944802018-06-21 09:35:35 +0200147 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
148 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
149 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
150 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
151 case MBEDTLS_ERR_ASN1_INVALID_DATA:
152 return( PSA_ERROR_INVALID_ARGUMENT );
153 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
154 return( PSA_ERROR_INSUFFICIENT_MEMORY );
155 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
156 return( PSA_ERROR_BUFFER_TOO_SMALL );
157
Jaeden Amero93e21112019-02-20 13:57:28 +0000158#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000159 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
161 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
162#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
164 return( PSA_ERROR_NOT_SUPPORTED );
165 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
166 return( PSA_ERROR_HARDWARE_FAILURE );
167
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000169 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
171 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
174 return( PSA_ERROR_NOT_SUPPORTED );
175 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
176 return( PSA_ERROR_HARDWARE_FAILURE );
177
178 case MBEDTLS_ERR_CCM_BAD_INPUT:
179 return( PSA_ERROR_INVALID_ARGUMENT );
180 case MBEDTLS_ERR_CCM_AUTH_FAILED:
181 return( PSA_ERROR_INVALID_SIGNATURE );
182 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
183 return( PSA_ERROR_HARDWARE_FAILURE );
184
Gilles Peskine26869f22019-05-06 15:25:00 +0200185 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
186 return( PSA_ERROR_INVALID_ARGUMENT );
187
188 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
189 return( PSA_ERROR_BAD_STATE );
190 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
191 return( PSA_ERROR_INVALID_SIGNATURE );
192
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
194 return( PSA_ERROR_NOT_SUPPORTED );
195 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
198 return( PSA_ERROR_INSUFFICIENT_MEMORY );
199 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
200 return( PSA_ERROR_INVALID_PADDING );
201 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200202 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
205 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200206 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
208 return( PSA_ERROR_HARDWARE_FAILURE );
209
210 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
214 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
215 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
216 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
217 return( PSA_ERROR_NOT_SUPPORTED );
218 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
220
221 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
Gilles Peskinee59236f2018-01-27 23:32:46 +0100226 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
227 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
228 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
229 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100230
231 case MBEDTLS_ERR_GCM_AUTH_FAILED:
232 return( PSA_ERROR_INVALID_SIGNATURE );
233 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200234 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
243 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
244 return( PSA_ERROR_NOT_SUPPORTED );
245 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
246 return( PSA_ERROR_INVALID_ARGUMENT );
247 case MBEDTLS_ERR_MD_ALLOC_FAILED:
248 return( PSA_ERROR_INSUFFICIENT_MEMORY );
249 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
250 return( PSA_ERROR_STORAGE_FAILURE );
251 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
255 return( PSA_ERROR_STORAGE_FAILURE );
256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
261 return( PSA_ERROR_BUFFER_TOO_SMALL );
262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
269 return( PSA_ERROR_INSUFFICIENT_MEMORY );
270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
282 return( PSA_ERROR_NOT_SUPPORTED );
283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
285 return( PSA_ERROR_NOT_PERMITTED );
286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
291 return( PSA_ERROR_NOT_SUPPORTED );
292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
293 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
300 return( PSA_ERROR_NOT_SUPPORTED );
301
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
303 return( PSA_ERROR_HARDWARE_FAILURE );
304
305 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_RSA_INVALID_PADDING:
308 return( PSA_ERROR_INVALID_PADDING );
309 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
312 return( PSA_ERROR_INVALID_ARGUMENT );
313 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
314 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200315 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100316 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
317 return( PSA_ERROR_INVALID_SIGNATURE );
318 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
319 return( PSA_ERROR_BUFFER_TOO_SMALL );
320 case MBEDTLS_ERR_RSA_RNG_FAILED:
321 return( PSA_ERROR_INSUFFICIENT_MEMORY );
322 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
323 return( PSA_ERROR_NOT_SUPPORTED );
324 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
327 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
328 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
329 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
330 return( PSA_ERROR_HARDWARE_FAILURE );
331
332 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
333 return( PSA_ERROR_INVALID_ARGUMENT );
334 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
336
itayzafrir5c753392018-05-08 11:18:38 +0300337 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300338 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300339 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
341 return( PSA_ERROR_BUFFER_TOO_SMALL );
342 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
343 return( PSA_ERROR_NOT_SUPPORTED );
344 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
345 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
346 return( PSA_ERROR_INVALID_SIGNATURE );
347 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
348 return( PSA_ERROR_INSUFFICIENT_MEMORY );
349 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
350 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000351 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
352 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
Gilles Peskine8e338702019-07-30 20:06:31 +0200369 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100374mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100375 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200376{
377 switch( curve )
378 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100379 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100380 switch( byte_length )
381 {
382 case PSA_BITS_TO_BYTES( 192 ):
383 return( MBEDTLS_ECP_DP_SECP192R1 );
384 case PSA_BITS_TO_BYTES( 224 ):
385 return( MBEDTLS_ECP_DP_SECP224R1 );
386 case PSA_BITS_TO_BYTES( 256 ):
387 return( MBEDTLS_ECP_DP_SECP256R1 );
388 case PSA_BITS_TO_BYTES( 384 ):
389 return( MBEDTLS_ECP_DP_SECP384R1 );
390 case PSA_BITS_TO_BYTES( 521 ):
391 return( MBEDTLS_ECP_DP_SECP521R1 );
392 default:
393 return( MBEDTLS_ECP_DP_NONE );
394 }
395 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100396
Paul Elliott8ff510a2020-06-02 17:19:28 +0100397 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100398 switch( byte_length )
399 {
400 case PSA_BITS_TO_BYTES( 256 ):
401 return( MBEDTLS_ECP_DP_BP256R1 );
402 case PSA_BITS_TO_BYTES( 384 ):
403 return( MBEDTLS_ECP_DP_BP384R1 );
404 case PSA_BITS_TO_BYTES( 512 ):
405 return( MBEDTLS_ECP_DP_BP512R1 );
406 default:
407 return( MBEDTLS_ECP_DP_NONE );
408 }
409 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100410
Paul Elliott8ff510a2020-06-02 17:19:28 +0100411 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 switch( byte_length )
413 {
414 case PSA_BITS_TO_BYTES( 255 ):
415 return( MBEDTLS_ECP_DP_CURVE25519 );
416 case PSA_BITS_TO_BYTES( 448 ):
417 return( MBEDTLS_ECP_DP_CURVE448 );
418 default:
419 return( MBEDTLS_ECP_DP_NONE );
420 }
421 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100422
Paul Elliott8ff510a2020-06-02 17:19:28 +0100423 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 switch( byte_length )
425 {
426 case PSA_BITS_TO_BYTES( 192 ):
427 return( MBEDTLS_ECP_DP_SECP192K1 );
428 case PSA_BITS_TO_BYTES( 224 ):
429 return( MBEDTLS_ECP_DP_SECP224K1 );
430 case PSA_BITS_TO_BYTES( 256 ):
431 return( MBEDTLS_ECP_DP_SECP256K1 );
432 default:
433 return( MBEDTLS_ECP_DP_NONE );
434 }
435 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100436
Gilles Peskine12313cd2018-06-20 00:20:32 +0200437 default:
438 return( MBEDTLS_ECP_DP_NONE );
439 }
440}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100441#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200442
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200443static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
444 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445{
446 /* Check that the bit size is acceptable for the key type */
447 switch( type )
448 {
449 case PSA_KEY_TYPE_RAW_DATA:
450#if defined(MBEDTLS_MD_C)
451 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200452#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200453 case PSA_KEY_TYPE_DERIVE:
454 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200455#if defined(MBEDTLS_AES_C)
456 case PSA_KEY_TYPE_AES:
457 if( bits != 128 && bits != 192 && bits != 256 )
458 return( PSA_ERROR_INVALID_ARGUMENT );
459 break;
460#endif
461#if defined(MBEDTLS_CAMELLIA_C)
462 case PSA_KEY_TYPE_CAMELLIA:
463 if( bits != 128 && bits != 192 && bits != 256 )
464 return( PSA_ERROR_INVALID_ARGUMENT );
465 break;
466#endif
467#if defined(MBEDTLS_DES_C)
468 case PSA_KEY_TYPE_DES:
469 if( bits != 64 && bits != 128 && bits != 192 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
471 break;
472#endif
473#if defined(MBEDTLS_ARC4_C)
474 case PSA_KEY_TYPE_ARC4:
475 if( bits < 8 || bits > 2048 )
476 return( PSA_ERROR_INVALID_ARGUMENT );
477 break;
478#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200479#if defined(MBEDTLS_CHACHA20_C)
480 case PSA_KEY_TYPE_CHACHA20:
481 if( bits != 256 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200485 default:
486 return( PSA_ERROR_NOT_SUPPORTED );
487 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200488 if( bits % 8 != 0 )
489 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491 return( PSA_SUCCESS );
492}
493
Steven Cooremana01795d2020-07-24 22:48:15 +0200494#if defined(MBEDTLS_RSA_C)
495
496#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100497/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
498 * that are not a multiple of 8) well. For example, there is only
499 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
500 * way to return the exact bit size of a key.
501 * To keep things simple, reject non-byte-aligned key sizes. */
502static psa_status_t psa_check_rsa_key_byte_aligned(
503 const mbedtls_rsa_context *rsa )
504{
505 mbedtls_mpi n;
506 psa_status_t status;
507 mbedtls_mpi_init( &n );
508 status = mbedtls_to_psa_error(
509 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
510 if( status == PSA_SUCCESS )
511 {
512 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
513 status = PSA_ERROR_NOT_SUPPORTED;
514 }
515 mbedtls_mpi_free( &n );
516 return( status );
517}
Steven Cooremana01795d2020-07-24 22:48:15 +0200518#endif /* MBEDTLS_PK_PARSE_C */
Gilles Peskine86a440b2018-11-12 18:39:40 +0100519
Steven Cooreman7f391872020-07-30 14:57:44 +0200520/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200521 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200522 * \param[in] type The type of key contained in \p data.
523 * \param[in] data The buffer from which to load the representation.
524 * \param[in] data_length The size in bytes of \p data.
525 * \param[out] p_rsa Returns a pointer to an RSA context on success.
526 * The caller is responsible for freeing both the
527 * contents of the context and the context itself
528 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200529 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200530static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
531 const uint8_t *data,
532 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200533 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200534{
Steven Cooremana01795d2020-07-24 22:48:15 +0200535#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000536 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200537 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000538 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200539 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000540
541 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200542 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200544 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200547 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000548 if( status != PSA_SUCCESS )
549 goto exit;
550
551 /* We have something that the pkparse module recognizes. If it is a
552 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200553 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555 status = PSA_ERROR_INVALID_ARGUMENT;
556 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
560 * supports non-byte-aligned key sizes, but not well. For example,
561 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200562 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000563 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
564 {
565 status = PSA_ERROR_NOT_SUPPORTED;
566 goto exit;
567 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200568 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200569 if( status != PSA_SUCCESS )
570 goto exit;
571
Steven Cooremana2371e52020-07-28 14:30:39 +0200572 /* Copy out the pointer to the RSA context, and reset the PK context
573 * such that pk_free doesn't free the RSA context we just grabbed. */
574 *p_rsa = mbedtls_pk_rsa( ctx );
575 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000576
577exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200578 mbedtls_pk_free( &ctx );
579 return( status );
580#else
Steven Cooreman4fed4552020-08-03 14:46:03 +0200581 (void) data;
582 (void) data_length;
Steven Cooreman7f391872020-07-30 14:57:44 +0200583 (void) type;
Steven Cooremana01795d2020-07-24 22:48:15 +0200584 (void) rsa;
585 return( PSA_ERROR_NOT_SUPPORTED );
586#endif /* MBEDTLS_PK_PARSE_C */
587}
588
589/** Export an RSA key to export representation
590 *
591 * \param[in] type The type of key (public/private) to export
592 * \param[in] rsa The internal RSA representation from which to export
593 * \param[out] data The buffer to export to
594 * \param[in] data_size The length of the buffer to export to
595 * \param[out] data_length The amount of bytes written to \p data
596 */
597static psa_status_t psa_export_rsa_key( psa_key_type_t type,
598 mbedtls_rsa_context *rsa,
599 uint8_t *data,
600 size_t data_size,
601 size_t *data_length )
602{
603#if defined(MBEDTLS_PK_WRITE_C)
604 int ret;
605 mbedtls_pk_context pk;
606 uint8_t *pos = data + data_size;
607
608 mbedtls_pk_init( &pk );
609 pk.pk_info = &mbedtls_rsa_info;
610 pk.pk_ctx = rsa;
611
612 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200613 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
614 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200615 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
616 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
617 else
618 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
619
620 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200621 {
622 /* Clean up in case pk_write failed halfway through. */
623 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200624 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200625 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200626
627 /* The mbedtls_pk_xxx functions write to the end of the buffer.
628 * Move the data to the beginning and erase remaining data
629 * at the original location. */
630 if( 2 * (size_t) ret <= data_size )
631 {
632 memcpy( data, data + data_size - ret, ret );
633 memset( data + data_size - ret, 0, ret );
634 }
635 else if( (size_t) ret < data_size )
636 {
637 memmove( data, data + data_size - ret, ret );
638 memset( data + ret, 0, data_size - ret );
639 }
640
641 *data_length = ret;
642 return( PSA_SUCCESS );
643#else
644 (void) type;
645 (void) rsa;
646 (void) data;
647 (void) data_size;
648 (void) data_length;
649 return( PSA_ERROR_NOT_SUPPORTED );
650#endif /* MBEDTLS_PK_WRITE_C */
651}
652
653/** Import an RSA key from import representation to a slot
654 *
655 * \param[in,out] slot The slot where to store the export representation to
656 * \param[in] data The buffer containing the import representation
657 * \param[in] data_length The amount of bytes in \p data
658 */
659static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
660 const uint8_t *data,
661 size_t data_length )
662{
663 psa_status_t status;
664 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200665 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200666
Steven Cooremana01795d2020-07-24 22:48:15 +0200667 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200668 status = psa_load_rsa_representation( slot->attr.type,
669 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200670 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200671 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200672 if( status != PSA_SUCCESS )
673 goto exit;
674
675 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200676 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200677
Steven Cooreman75b74362020-07-28 14:30:13 +0200678 /* Re-export the data to PSA export format, such that we can store export
679 * representation in the key slot. Export representation in case of RSA is
680 * the smallest representation that's allowed as input, so a straight-up
681 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200682 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200683 if( output == NULL )
684 {
685 status = PSA_ERROR_INSUFFICIENT_MEMORY;
686 goto exit;
687 }
688
Steven Cooremana01795d2020-07-24 22:48:15 +0200689 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200690 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200691 output,
692 data_length,
693 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200694exit:
695 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200696 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200697 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200698
699 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000700 if( status != PSA_SUCCESS )
701 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200702 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000703 return( status );
704 }
705
Steven Cooremana01795d2020-07-24 22:48:15 +0200706 /* On success, store the allocated export-formatted key. */
707 slot->data.key.data = output;
708 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000709
710 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200711}
Steven Cooremana01795d2020-07-24 22:48:15 +0200712#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200713
Jaeden Ameroccdce902019-01-10 11:42:27 +0000714#if defined(MBEDTLS_ECP_C)
Steven Cooreman7f391872020-07-30 14:57:44 +0200715/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200716 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200717 * \param[in] type The type of key contained in \p data.
718 * \param[in] data The buffer from which to load the representation.
719 * \param[in] data_length The size in bytes of \p data.
720 * \param[out] p_ecp Returns a pointer to an ECP context on success.
721 * The caller is responsible for freeing both the
722 * contents of the context and the context itself
723 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200724 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200725static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
726 const uint8_t *data,
727 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200728 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100729{
730 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200731 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200732 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200733 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100734
Steven Cooreman4fed4552020-08-03 14:46:03 +0200735 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
736 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100737 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200738 /* A Weierstrass public key is represented as:
739 * - The byte 0x04;
740 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
741 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200742 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200743 */
744 if( ( data_length & 1 ) == 0 )
745 return( PSA_ERROR_INVALID_ARGUMENT );
746 curve_size = data_length / 2;
747
748 /* Montgomery public keys are represented in compressed format, meaning
749 * their curve_size is equal to the amount of input. */
750
751 /* Private keys are represented in uncompressed private random integer
752 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100753 }
754
Steven Cooreman6d839f02020-07-30 11:36:45 +0200755 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200756 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200757 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200758 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200759 mbedtls_ecp_keypair_init( ecp );
760
Gilles Peskine4cd32772019-12-02 20:49:42 +0100761 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200762 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
763 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100764 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200765 {
766 status = PSA_ERROR_INVALID_ARGUMENT;
767 goto exit;
768 }
769
Steven Cooremanacda8342020-07-24 23:09:52 +0200770 status = mbedtls_to_psa_error(
771 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
772 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200773 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200774
Steven Cooreman6d839f02020-07-30 11:36:45 +0200775 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200776 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200777 {
778 /* Load the public value. */
779 status = mbedtls_to_psa_error(
780 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200781 data,
782 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200783 if( status != PSA_SUCCESS )
784 goto exit;
785
786 /* Check that the point is on the curve. */
787 status = mbedtls_to_psa_error(
788 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
789 if( status != PSA_SUCCESS )
790 goto exit;
791 }
792 else
793 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200794 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200795 status = mbedtls_to_psa_error(
796 mbedtls_ecp_read_key( ecp->grp.id,
797 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200798 data,
799 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200800 if( status != PSA_SUCCESS )
801 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200802 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200803
804 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200805exit:
806 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200807 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200808 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200809 mbedtls_free( ecp );
810 }
811
Steven Cooreman29149862020-08-05 15:43:42 +0200812 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100813}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000814
Steven Cooreman4fed4552020-08-03 14:46:03 +0200815/** Export an ECP key to export representation
816 *
817 * \param[in] type The type of key (public/private) to export
818 * \param[in] ecp The internal ECP representation from which to export
819 * \param[out] data The buffer to export to
820 * \param[in] data_size The length of the buffer to export to
821 * \param[out] data_length The amount of bytes written to \p data
822 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200823static psa_status_t psa_export_ecp_key( psa_key_type_t type,
824 mbedtls_ecp_keypair *ecp,
825 uint8_t *data,
826 size_t data_size,
827 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200828{
Steven Cooremanacda8342020-07-24 23:09:52 +0200829 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000830
Steven Cooremanacda8342020-07-24 23:09:52 +0200831 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200832 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200833 /* Check whether the public part is loaded */
834 if( mbedtls_ecp_is_zero( &ecp->Q ) )
835 {
836 /* Calculate the public key */
837 status = mbedtls_to_psa_error(
838 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
839 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
840 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200841 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200842 }
843
Steven Cooreman4fed4552020-08-03 14:46:03 +0200844 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200845 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
846 MBEDTLS_ECP_PF_UNCOMPRESSED,
847 data_length,
848 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200849 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200850 if( status != PSA_SUCCESS )
851 memset( data, 0, data_size );
852
Steven Cooreman29149862020-08-05 15:43:42 +0200853 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200854 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200855 else
856 {
Steven Cooreman29149862020-08-05 15:43:42 +0200857 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200858 return( PSA_ERROR_BUFFER_TOO_SMALL );
859
860 status = mbedtls_to_psa_error(
861 mbedtls_ecp_write_key( ecp,
862 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200863 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200864 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200865 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200866 else
867 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200868
869 return( status );
870 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200871}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200872
Steven Cooreman4fed4552020-08-03 14:46:03 +0200873/** Import an ECP key from import representation to a slot
874 *
875 * \param[in,out] slot The slot where to store the export representation to
876 * \param[in] data The buffer containing the import representation
877 * \param[in] data_length The amount of bytes in \p data
878 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200879static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
880 const uint8_t *data,
881 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100882{
Steven Cooremanacda8342020-07-24 23:09:52 +0200883 psa_status_t status;
884 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200885 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100886
Steven Cooremanacda8342020-07-24 23:09:52 +0200887 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200888 status = psa_load_ecp_representation( slot->attr.type,
889 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200890 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200891 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100892 if( status != PSA_SUCCESS )
893 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100894
Steven Cooremanacda8342020-07-24 23:09:52 +0200895 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200896 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200897 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200898 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200899
Steven Cooremanacda8342020-07-24 23:09:52 +0200900 /* Re-export the data to PSA export format. There is currently no support
901 * for other input formats then the export format, so this is a 1-1
902 * copy operation. */
903 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200904 if( output == NULL )
905 {
906 status = PSA_ERROR_INSUFFICIENT_MEMORY;
907 goto exit;
908 }
909
910 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200911 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200912 output,
913 data_length,
914 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100915exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200916 /* Always free the PK object (will also free contained ECP context) */
917 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200918 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200919
920 /* Free the allocated buffer only on error. */
921 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100922 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200923 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200924 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100925 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200926
927 /* On success, store the allocated export-formatted key. */
928 slot->data.key.data = output;
929 slot->data.key.bytes = data_length;
930
931 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100932}
933#endif /* defined(MBEDTLS_ECP_C) */
934
Gilles Peskineb46bef22019-07-30 21:32:04 +0200935/** Return the size of the key in the given slot, in bits.
936 *
937 * \param[in] slot A key slot.
938 *
939 * \return The key size in bits, read from the metadata in the slot.
940 */
941static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
942{
943 return( slot->attr.bits );
944}
945
Steven Cooreman75b74362020-07-28 14:30:13 +0200946/** Try to allocate a buffer to an empty key slot.
947 *
948 * \param[in,out] slot Key slot to attach buffer to.
949 * \param[in] buffer_length Requested size of the buffer.
950 *
951 * \retval #PSA_SUCCESS
952 * The buffer has been successfully allocated.
953 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
954 * Not enough memory was available for allocation.
955 * \retval #PSA_ERROR_ALREADY_EXISTS
956 * Trying to allocate a buffer to a non-empty key slot.
957 */
958static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
959 size_t buffer_length )
960{
961 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200962 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200963
964 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
965 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200966 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200967
968 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200969 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200970}
971
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200972psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
973 const uint8_t* data,
974 size_t data_length )
975{
976 psa_status_t status = psa_allocate_buffer_to_slot( slot,
977 data_length );
978 if( status != PSA_SUCCESS )
979 return( status );
980
981 memcpy( slot->data.key.data, data, data_length );
982 return( PSA_SUCCESS );
983}
984
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200985psa_status_t psa_detect_bit_size_in_slot( psa_key_slot_t *slot )
986{
987 if( slot->attr.bits != 0 )
988 return( PSA_SUCCESS );
989
990 if( key_type_is_raw_bytes( slot->attr.type ) )
991 {
992 slot->attr.bits =
993 (psa_key_bits_t) PSA_BYTES_TO_BITS( slot->data.key.bytes );
994 return( PSA_SUCCESS );
995 }
996 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
997 {
998 /* Keys are stored in export format, and we are currently
999 * restricted to known curves, so do the reverse lookup based
1000 * on data length. */
1001 size_t byte_length = slot->data.key.bytes;
1002 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) &&
1003 PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) !=
1004 PSA_ECC_FAMILY_MONTGOMERY )
1005 {
1006 /* A Weierstrass public key is represented as:
1007 * - The byte 0x04;
1008 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
1009 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
1010 * So its data length is 2m+1 where m is the curve size in bits.
1011 */
1012 if( ( byte_length & 1 ) == 0 )
1013 return( PSA_ERROR_BAD_STATE );
1014 byte_length = byte_length / 2;
1015
1016 /* Montgomery public keys are represented in compressed format,
1017 * meaning their curve_size is equal to the amount of input. */
1018
1019 /* Private keys are represented in uncompressed private random
1020 * integer format, meaning their curve_size is equal to the
1021 * amount of input. */
1022 }
1023
1024 switch( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) )
1025 {
1026 case PSA_ECC_FAMILY_SECP_R1:
1027 switch( byte_length )
1028 {
1029 case PSA_BITS_TO_BYTES( 192 ):
1030 slot->attr.bits = 192;
1031 break;
1032 case PSA_BITS_TO_BYTES( 224 ):
1033 slot->attr.bits = 224;
1034 break;
1035 case PSA_BITS_TO_BYTES( 256 ):
1036 slot->attr.bits = 256;
1037 break;
1038 case PSA_BITS_TO_BYTES( 384 ):
1039 slot->attr.bits = 384;
1040 break;
1041 case PSA_BITS_TO_BYTES( 521 ):
1042 slot->attr.bits = 521;
1043 break;
1044 default:
1045 return( PSA_ERROR_BAD_STATE );
1046 }
1047 break;
1048
1049 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
1050 switch( byte_length )
1051 {
1052 case PSA_BITS_TO_BYTES( 256 ):
1053 slot->attr.bits = 256;
1054 break;
1055 case PSA_BITS_TO_BYTES( 384 ):
1056 slot->attr.bits = 384;
1057 break;
1058 case PSA_BITS_TO_BYTES( 512 ):
1059 slot->attr.bits = 512;
1060 break;
1061 default:
1062 return( PSA_ERROR_BAD_STATE );
1063 }
1064 break;
1065
1066 case PSA_ECC_FAMILY_MONTGOMERY:
1067 switch( byte_length )
1068 {
1069 case PSA_BITS_TO_BYTES( 255 ):
1070 slot->attr.bits = 255;
1071 break;
1072 case PSA_BITS_TO_BYTES( 448 ):
1073 slot->attr.bits = 448;
1074 break;
1075 default:
1076 return( PSA_ERROR_BAD_STATE );
1077 }
1078 break;
1079
1080 case PSA_ECC_FAMILY_SECP_K1:
1081 switch( byte_length )
1082 {
1083 case PSA_BITS_TO_BYTES( 192 ):
1084 slot->attr.bits = 192;
1085 break;
1086 case PSA_BITS_TO_BYTES( 224 ):
1087 slot->attr.bits = 224;
1088 break;
1089 case PSA_BITS_TO_BYTES( 256 ):
1090 slot->attr.bits = 256;
1091 break;
1092 default:
1093 return( PSA_ERROR_BAD_STATE );
1094 }
1095 break;
1096
1097 default:
1098 return( PSA_ERROR_BAD_STATE );
1099 }
1100
1101 return( PSA_SUCCESS );
1102 }
1103 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1104 {
1105 /* There's no easy way of figuring out the RSA bit size from
1106 * the data length of the export representation. For now, use
1107 * the mbed TLS software implementation to figure it out. */
1108 psa_key_attributes_t attributes = {
1109 .core = slot->attr
1110 };
1111 size_t bits;
1112 psa_status_t status = psa_driver_wrapper_validate_key(
1113 &attributes,
1114 slot->data.key.data,
1115 slot->data.key.bytes,
1116 &bits );
1117 if( status == PSA_SUCCESS )
1118 slot->attr.bits = (psa_key_bits_t) bits;
1119 if( status != PSA_ERROR_NOT_SUPPORTED )
1120 return( status );
1121
1122 /* If no accelerator was able to figure it out, try software. */
1123#if defined(MBEDTLS_RSA_C)
1124 mbedtls_rsa_context *rsa = NULL;
1125
1126 /* Parse input */
1127 status = psa_load_rsa_representation( slot->attr.type,
1128 slot->data.key.data,
1129 slot->data.key.bytes,
1130 &rsa );
1131 if( status != PSA_SUCCESS )
1132 {
1133 mbedtls_rsa_free( rsa );
1134 mbedtls_free( rsa );
1135 return( status );
1136 }
1137
1138 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
1139 mbedtls_rsa_get_len( rsa ) );
1140
1141 mbedtls_rsa_free( rsa );
1142 mbedtls_free( rsa );
1143
1144 return( PSA_SUCCESS );
1145#else
1146 return( PSA_ERROR_NOT_SUPPORTED );
1147#endif
1148 }
1149 else
1150 return( PSA_ERROR_NOT_SUPPORTED );
1151}
1152
1153/** Import key data into a slot.
1154 *
1155 * `slot->type` must have been set previously.
1156 * This function assumes that the slot does not contain any key material yet.
1157 * On failure, the slot content is unchanged.
1158 *
1159 * Persistent storage is not affected.
1160 *
1161 * \param[in,out] slot The key slot to import data into.
1162 * Its `type` field must have previously been set to
1163 * the desired key type.
1164 * It must not contain any key material yet.
1165 * \param[in] data Buffer containing the key material to parse and import.
1166 * \param data_length Size of \p data in bytes.
1167 *
1168 * \retval #PSA_SUCCESS
1169 * \retval #PSA_ERROR_INVALID_ARGUMENT
1170 * \retval #PSA_ERROR_NOT_SUPPORTED
1171 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1172 */
1173static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1174 const uint8_t *data,
1175 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001176{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001177 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001178 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001179
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001180 /* zero-length keys are never supported. */
1181 if( data_length == 0 )
1182 return( PSA_ERROR_NOT_SUPPORTED );
1183
Gilles Peskine8e338702019-07-30 20:06:31 +02001184 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001185 {
Steven Cooreman04524762020-10-13 17:43:44 +02001186 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001187
Steven Cooreman75b74362020-07-28 14:30:13 +02001188 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1189 if( data_length > SIZE_MAX / 8 )
1190 return( PSA_ERROR_NOT_SUPPORTED );
1191
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001192 /* Enforce a size limit, and in particular ensure that the bit
1193 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001194 if( bit_size > PSA_MAX_KEY_BITS )
1195 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001196
1197 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001198 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001199 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001200
1201 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001202 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001203 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001204 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001205
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001206 /* Write the actual key size to the slot.
1207 * psa_start_key_creation() wrote the size declared by the
1208 * caller, which may be 0 (meaning unspecified) or wrong. */
1209 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001210 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001211 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001212 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001213 /* Try validation through accelerators first. */
1214 bit_size = slot->attr.bits;
1215 psa_key_attributes_t attributes = {
1216 .core = slot->attr
1217 };
1218 status = psa_driver_wrapper_validate_key( &attributes,
1219 data,
1220 data_length,
1221 &bit_size );
1222 if( status == PSA_SUCCESS )
1223 {
1224 /* Key has been validated successfully by an accelerator.
1225 * Copy key material into slot. */
1226 status = psa_copy_key_material_into_slot( slot, data, data_length );
1227 if( status != PSA_SUCCESS )
1228 return( status );
1229
1230 slot->attr.bits = (psa_key_bits_t) bit_size;
1231 return( PSA_SUCCESS );
1232 }
1233 else if( status != PSA_ERROR_NOT_SUPPORTED )
1234 return( status );
1235
1236 /* Key format is not supported by any accelerator, try software fallback
1237 * if present. */
1238 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1239 {
Gilles Peskinef76aa772018-10-29 19:24:33 +01001240#if defined(MBEDTLS_ECP_C)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001241 status = psa_import_ecp_key( slot,
1242 data, data_length );
Steven Cooreman19fd5742020-07-24 23:31:01 +02001243#else
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001244 /* No drivers have been implemented yet, so without mbed TLS backing
1245 * there's no way to do ECP with the current library. */
1246 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman19fd5742020-07-24 23:31:01 +02001247#endif /* defined(MBEDTLS_ECP_C) */
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001248 }
1249 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1250 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001251#if defined(MBEDTLS_RSA_C)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001252 status = psa_import_rsa_key( slot,
1253 data, data_length );
Steven Cooreman19fd5742020-07-24 23:31:01 +02001254#else
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001255 /* No drivers have been implemented yet, so without mbed TLS backing
1256 * there's no way to do RSA with the current library. */
1257 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman19fd5742020-07-24 23:31:01 +02001258#endif /* defined(MBEDTLS_RSA_C) */
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001259 }
1260 else
1261 {
1262 /* Unsupported asymmetric key type */
1263 status = PSA_ERROR_NOT_SUPPORTED;
1264 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001265 }
1266 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001267 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001268 /* Unknown key type */
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001269 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine969ac722018-01-28 18:16:59 +01001270 }
Darryl Green940d72c2018-07-13 13:18:51 +01001271
Darryl Green06fd18d2018-07-16 11:21:11 +01001272 return( status );
1273}
1274
Gilles Peskinef603c712019-01-19 13:40:11 +01001275/** Calculate the intersection of two algorithm usage policies.
1276 *
1277 * Return 0 (which allows no operation) on incompatibility.
1278 */
1279static psa_algorithm_t psa_key_policy_algorithm_intersection(
1280 psa_algorithm_t alg1,
1281 psa_algorithm_t alg2 )
1282{
Gilles Peskine549ea862019-05-22 11:45:59 +02001283 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001284 if( alg1 == alg2 )
1285 return( alg1 );
1286 /* If the policies are from the same hash-and-sign family, check
1287 * if one is a wildcard. If so the other has the specific algorithm. */
1288 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1289 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1290 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1291 {
1292 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1293 return( alg2 );
1294 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1295 return( alg1 );
1296 }
1297 /* If the policies are incompatible, allow nothing. */
1298 return( 0 );
1299}
1300
Gilles Peskined6f371b2019-05-10 19:33:38 +02001301static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1302 psa_algorithm_t requested_alg )
1303{
Gilles Peskine549ea862019-05-22 11:45:59 +02001304 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001305 if( requested_alg == policy_alg )
1306 return( 1 );
1307 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001308 * and requested_alg is the same hash-and-sign family with any hash,
1309 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001310 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1311 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1312 {
1313 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1314 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1315 }
Steven Cooremance48e852020-10-05 16:02:45 +02001316 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001317 * a key derivation with that key agreement should also be allowed. This
1318 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001319 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1320 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1321 {
1322 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1323 policy_alg );
1324 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001325 /* If it isn't permitted, it's forbidden. */
1326 return( 0 );
1327}
1328
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001329/** Test whether a policy permits an algorithm.
1330 *
1331 * The caller must test usage flags separately.
1332 */
1333static int psa_key_policy_permits( const psa_key_policy_t *policy,
1334 psa_algorithm_t alg )
1335{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001336 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1337 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001338}
1339
Gilles Peskinef603c712019-01-19 13:40:11 +01001340/** Restrict a key policy based on a constraint.
1341 *
1342 * \param[in,out] policy The policy to restrict.
1343 * \param[in] constraint The policy constraint to apply.
1344 *
1345 * \retval #PSA_SUCCESS
1346 * \c *policy contains the intersection of the original value of
1347 * \c *policy and \c *constraint.
1348 * \retval #PSA_ERROR_INVALID_ARGUMENT
1349 * \c *policy and \c *constraint are incompatible.
1350 * \c *policy is unchanged.
1351 */
1352static psa_status_t psa_restrict_key_policy(
1353 psa_key_policy_t *policy,
1354 const psa_key_policy_t *constraint )
1355{
1356 psa_algorithm_t intersection_alg =
1357 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001358 psa_algorithm_t intersection_alg2 =
1359 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001360 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1361 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001362 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1363 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001364 policy->usage &= constraint->usage;
1365 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001366 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001367 return( PSA_SUCCESS );
1368}
1369
Darryl Green06fd18d2018-07-16 11:21:11 +01001370/** Retrieve a slot which must contain a key. The key must have allow all the
1371 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1372 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001373static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001374 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001375 psa_key_usage_t usage,
1376 psa_algorithm_t alg )
1377{
1378 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001379 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001380
1381 *p_slot = NULL;
1382
Gilles Peskinec5487a82018-12-03 18:08:14 +01001383 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001384 if( status != PSA_SUCCESS )
1385 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001386
1387 /* Enforce that usage policy for the key slot contains all the flags
1388 * required by the usage parameter. There is one exception: public
1389 * keys can always be exported, so we treat public key objects as
1390 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001391 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001392 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001393 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001394 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001395
1396 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001397 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001398 return( PSA_ERROR_NOT_PERMITTED );
1399
1400 *p_slot = slot;
1401 return( PSA_SUCCESS );
1402}
Darryl Green940d72c2018-07-13 13:18:51 +01001403
Gilles Peskine28f8f302019-07-24 13:30:31 +02001404/** Retrieve a slot which must contain a transparent key.
1405 *
1406 * A transparent key is a key for which the key material is directly
1407 * available, as opposed to a key in a secure element.
1408 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001409 * This is a temporary function to use instead of psa_get_key_from_slot()
1410 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001411 */
1412#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1413static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1414 psa_key_slot_t **p_slot,
1415 psa_key_usage_t usage,
1416 psa_algorithm_t alg )
1417{
1418 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1419 if( status != PSA_SUCCESS )
1420 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001421 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001422 {
1423 *p_slot = NULL;
1424 return( PSA_ERROR_NOT_SUPPORTED );
1425 }
1426 return( PSA_SUCCESS );
1427}
1428#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1429/* With no secure element support, all keys are transparent. */
1430#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1431 psa_get_key_from_slot( handle, p_slot, usage, alg )
1432#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1433
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001434/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001435static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001436{
Gilles Peskine73167e12019-07-12 23:44:37 +02001437#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1438 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001439 {
1440 /* No key material to clean. */
1441 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001442 else
1443#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001444 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001445 /* Data pointer will always be either a valid pointer or NULL in an
1446 * initialized slot, so we can just free it. */
1447 if( slot->data.key.data != NULL )
1448 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001449 mbedtls_free( slot->data.key.data );
1450 slot->data.key.data = NULL;
1451 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001452 }
Darryl Green40225ba2018-11-15 14:48:15 +00001453
1454 return( PSA_SUCCESS );
1455}
1456
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001457/** Completely wipe a slot in memory, including its policy.
1458 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001459psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001460{
1461 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001462 /* Multipart operations may still be using the key. This is safe
1463 * because all multipart operation objects are independent from
1464 * the key slot: if they need to access the key after the setup
1465 * phase, they have a copy of the key. Note that this means that
1466 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001467 /* At this point, key material and other type-specific content has
1468 * been wiped. Clear remaining metadata. We can call memset and not
1469 * zeroize because the metadata is not particularly sensitive. */
1470 memset( slot, 0, sizeof( *slot ) );
1471 return( status );
1472}
1473
Gilles Peskinec5487a82018-12-03 18:08:14 +01001474psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001475{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001476 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001477 psa_status_t status; /* status of the last operation */
1478 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001479#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1480 psa_se_drv_table_entry_t *driver;
1481#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001482
Gilles Peskine1841cf42019-10-08 15:48:25 +02001483 if( handle == 0 )
1484 return( PSA_SUCCESS );
1485
Gilles Peskinec5487a82018-12-03 18:08:14 +01001486 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001487 if( status != PSA_SUCCESS )
1488 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001489
1490#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001491 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001492 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001493 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001494 /* For a key in a secure element, we need to do three things:
1495 * remove the key file in internal storage, destroy the
1496 * key inside the secure element, and update the driver's
1497 * persistent data. Start a transaction that will encompass these
1498 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001499 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001500 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001501 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001502 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001503 status = psa_crypto_save_transaction( );
1504 if( status != PSA_SUCCESS )
1505 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001506 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001507 /* We should still try to destroy the key in the secure
1508 * element and the key metadata in storage. This is especially
1509 * important if the error is that the storage is full.
1510 * But how to do it exactly without risking an inconsistent
1511 * state after a reset?
1512 * https://github.com/ARMmbed/mbed-crypto/issues/215
1513 */
1514 overall_status = status;
1515 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001516 }
1517
Gilles Peskine354f7672019-07-12 23:46:38 +02001518 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001519 if( overall_status == PSA_SUCCESS )
1520 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001521 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001522#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1523
Darryl Greend49a4992018-06-18 17:27:26 +01001524#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001525 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001526 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001527 status = psa_destroy_persistent_key( slot->attr.id );
1528 if( overall_status == PSA_SUCCESS )
1529 overall_status = status;
1530
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001531 /* TODO: other slots may have a copy of the same key. We should
1532 * invalidate them.
1533 * https://github.com/ARMmbed/mbed-crypto/issues/214
1534 */
Darryl Greend49a4992018-06-18 17:27:26 +01001535 }
1536#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001537
Gilles Peskinefc762652019-07-22 19:30:34 +02001538#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1539 if( driver != NULL )
1540 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001541 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001542 if( overall_status == PSA_SUCCESS )
1543 overall_status = status;
1544 status = psa_crypto_stop_transaction( );
1545 if( overall_status == PSA_SUCCESS )
1546 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001547 }
1548#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1549
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001550#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1551exit:
1552#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001553 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001554 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1555 if( overall_status == PSA_SUCCESS )
1556 overall_status = status;
1557 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001558}
1559
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001560void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001561{
Gilles Peskineb699f072019-04-26 16:06:02 +02001562 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001563 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001564}
1565
Gilles Peskineb699f072019-04-26 16:06:02 +02001566psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1567 psa_key_type_t type,
1568 const uint8_t *data,
1569 size_t data_length )
1570{
1571 uint8_t *copy = NULL;
1572
1573 if( data_length != 0 )
1574 {
1575 copy = mbedtls_calloc( 1, data_length );
1576 if( copy == NULL )
1577 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1578 memcpy( copy, data, data_length );
1579 }
1580 /* After this point, this function is guaranteed to succeed, so it
1581 * can start modifying `*attributes`. */
1582
1583 if( attributes->domain_parameters != NULL )
1584 {
1585 mbedtls_free( attributes->domain_parameters );
1586 attributes->domain_parameters = NULL;
1587 attributes->domain_parameters_size = 0;
1588 }
1589
1590 attributes->domain_parameters = copy;
1591 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001592 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001593 return( PSA_SUCCESS );
1594}
1595
1596psa_status_t psa_get_key_domain_parameters(
1597 const psa_key_attributes_t *attributes,
1598 uint8_t *data, size_t data_size, size_t *data_length )
1599{
1600 if( attributes->domain_parameters_size > data_size )
1601 return( PSA_ERROR_BUFFER_TOO_SMALL );
1602 *data_length = attributes->domain_parameters_size;
1603 if( attributes->domain_parameters_size != 0 )
1604 memcpy( data, attributes->domain_parameters,
1605 attributes->domain_parameters_size );
1606 return( PSA_SUCCESS );
1607}
1608
1609#if defined(MBEDTLS_RSA_C)
1610static psa_status_t psa_get_rsa_public_exponent(
1611 const mbedtls_rsa_context *rsa,
1612 psa_key_attributes_t *attributes )
1613{
1614 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001615 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001616 uint8_t *buffer = NULL;
1617 size_t buflen;
1618 mbedtls_mpi_init( &mpi );
1619
1620 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1621 if( ret != 0 )
1622 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001623 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1624 {
1625 /* It's the default value, which is reported as an empty string,
1626 * so there's nothing to do. */
1627 goto exit;
1628 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001629
1630 buflen = mbedtls_mpi_size( &mpi );
1631 buffer = mbedtls_calloc( 1, buflen );
1632 if( buffer == NULL )
1633 {
1634 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1635 goto exit;
1636 }
1637 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1638 if( ret != 0 )
1639 goto exit;
1640 attributes->domain_parameters = buffer;
1641 attributes->domain_parameters_size = buflen;
1642
1643exit:
1644 mbedtls_mpi_free( &mpi );
1645 if( ret != 0 )
1646 mbedtls_free( buffer );
1647 return( mbedtls_to_psa_error( ret ) );
1648}
1649#endif /* MBEDTLS_RSA_C */
1650
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001651/** Retrieve all the publicly-accessible attributes of a key.
1652 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001653psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1654 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001655{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001656 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001657 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001658
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001659 psa_reset_key_attributes( attributes );
1660
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001661 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001662 if( status != PSA_SUCCESS )
1663 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001664
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001665 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001666 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1667 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1668
1669#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1670 if( psa_key_slot_is_external( slot ) )
1671 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1672#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001673
Gilles Peskine8e338702019-07-30 20:06:31 +02001674 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001675 {
1676#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001677 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001678 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001679#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001680 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001681 * is not yet implemented.
1682 * https://github.com/ARMmbed/mbed-crypto/issues/216
1683 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001684 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001685 break;
1686#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001687 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001688 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001689
Steven Cooreman4fed4552020-08-03 14:46:03 +02001690 status = psa_load_rsa_representation( slot->attr.type,
1691 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001692 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001693 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001694 if( status != PSA_SUCCESS )
1695 break;
1696
Steven Cooremana2371e52020-07-28 14:30:39 +02001697 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001698 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001699 mbedtls_rsa_free( rsa );
1700 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001701 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001702 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001703#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001704 default:
1705 /* Nothing else to do. */
1706 break;
1707 }
1708
1709 if( status != PSA_SUCCESS )
1710 psa_reset_key_attributes( attributes );
1711 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001712}
1713
Gilles Peskinec8000c02019-08-02 20:15:51 +02001714#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1715psa_status_t psa_get_key_slot_number(
1716 const psa_key_attributes_t *attributes,
1717 psa_key_slot_number_t *slot_number )
1718{
1719 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1720 {
1721 *slot_number = attributes->slot_number;
1722 return( PSA_SUCCESS );
1723 }
1724 else
1725 return( PSA_ERROR_INVALID_ARGUMENT );
1726}
1727#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1728
Steven Cooremana01795d2020-07-24 22:48:15 +02001729static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1730 uint8_t *data,
1731 size_t data_size,
1732 size_t *data_length )
1733{
1734 if( slot->data.key.bytes > data_size )
1735 return( PSA_ERROR_BUFFER_TOO_SMALL );
1736 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1737 memset( data + slot->data.key.bytes, 0,
1738 data_size - slot->data.key.bytes );
1739 *data_length = slot->data.key.bytes;
1740 return( PSA_SUCCESS );
1741}
1742
Gilles Peskinef603c712019-01-19 13:40:11 +01001743static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1744 uint8_t *data,
1745 size_t data_size,
1746 size_t *data_length,
1747 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001748{
Gilles Peskine5d309672019-07-12 23:47:28 +02001749#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1750 const psa_drv_se_t *drv;
1751 psa_drv_se_context_t *drv_context;
1752#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1753
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001754 *data_length = 0;
1755
Gilles Peskine8e338702019-07-30 20:06:31 +02001756 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001757 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001758
Gilles Peskinef9168942019-09-12 19:20:29 +02001759 /* Reject a zero-length output buffer now, since this can never be a
1760 * valid key representation. This way we know that data must be a valid
1761 * pointer and we can do things like memset(data, ..., data_size). */
1762 if( data_size == 0 )
1763 return( PSA_ERROR_BUFFER_TOO_SMALL );
1764
Gilles Peskine5d309672019-07-12 23:47:28 +02001765#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001766 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001767 {
1768 psa_drv_se_export_key_t method;
1769 if( drv->key_management == NULL )
1770 return( PSA_ERROR_NOT_SUPPORTED );
1771 method = ( export_public_key ?
1772 drv->key_management->p_export_public :
1773 drv->key_management->p_export );
1774 if( method == NULL )
1775 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001776 return( method( drv_context,
1777 slot->data.se.slot_number,
1778 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001779 }
1780#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1781
Gilles Peskine8e338702019-07-30 20:06:31 +02001782 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001783 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001784 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001785 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001786 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1787 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001788 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001789 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001790 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001791 /* Exporting public -> public */
1792 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1793 }
1794 else if( !export_public_key )
1795 {
1796 /* Exporting private -> private */
1797 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1798 }
1799 /* Need to export the public part of a private key,
1800 * so conversion is needed */
1801 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1802 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001803#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001804 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001805 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001806 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001807 slot->data.key.data,
1808 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001809 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001810 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001811 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001812
Steven Cooreman560c28a2020-07-24 23:20:24 +02001813 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001814 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001815 data,
1816 data_size,
1817 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001818
Steven Cooremana2371e52020-07-28 14:30:39 +02001819 mbedtls_rsa_free( rsa );
1820 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001821
Steven Cooreman560c28a2020-07-24 23:20:24 +02001822 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001823#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001824 /* We don't know how to convert a private RSA key to public. */
1825 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001826#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001827 }
1828 else
Moran Pekera998bc62018-04-16 18:16:20 +03001829 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001830#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001831 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001832 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001833 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001834 slot->data.key.data,
1835 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001836 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001837 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001838 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001839
1840 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1841 PSA_KEY_TYPE_ECC_GET_FAMILY(
1842 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001843 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001844 data,
1845 data_size,
1846 data_length );
1847
Steven Cooremana2371e52020-07-28 14:30:39 +02001848 mbedtls_ecp_keypair_free( ecp );
1849 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001850 return( status );
1851#else
1852 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001853 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001854#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001855 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001856 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001857 else
1858 {
1859 /* This shouldn't happen in the reference implementation, but
1860 it is valid for a special-purpose implementation to omit
1861 support for exporting certain key types. */
1862 return( PSA_ERROR_NOT_SUPPORTED );
1863 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001864}
1865
Gilles Peskinec5487a82018-12-03 18:08:14 +01001866psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001867 uint8_t *data,
1868 size_t data_size,
1869 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001870{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001871 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001872 psa_status_t status;
1873
1874 /* Set the key to empty now, so that even when there are errors, we always
1875 * set data_length to a value between 0 and data_size. On error, setting
1876 * the key to empty is a good choice because an empty key representation is
1877 * unlikely to be accepted anywhere. */
1878 *data_length = 0;
1879
1880 /* Export requires the EXPORT flag. There is an exception for public keys,
1881 * which don't require any flag, but psa_get_key_from_slot takes
1882 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001883 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001884 if( status != PSA_SUCCESS )
1885 return( status );
1886 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001887 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001888}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001889
Gilles Peskinec5487a82018-12-03 18:08:14 +01001890psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001891 uint8_t *data,
1892 size_t data_size,
1893 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001894{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001895 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001896 psa_status_t status;
1897
1898 /* Set the key to empty now, so that even when there are errors, we always
1899 * set data_length to a value between 0 and data_size. On error, setting
1900 * the key to empty is a good choice because an empty key representation is
1901 * unlikely to be accepted anywhere. */
1902 *data_length = 0;
1903
1904 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001905 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001906 if( status != PSA_SUCCESS )
1907 return( status );
1908 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001909 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001910}
1911
Gilles Peskine91e8c332019-08-02 19:19:39 +02001912#if defined(static_assert)
1913static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1914 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001915static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001916 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001917static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001918 "One or more key attribute flag is listed as both internal-only and external-only" );
1919#endif
1920
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001921/** Validate that a key policy is internally well-formed.
1922 *
1923 * This function only rejects invalid policies. It does not validate the
1924 * consistency of the policy with respect to other attributes of the key
1925 * such as the key type.
1926 */
1927static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001928{
1929 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001930 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001931 PSA_KEY_USAGE_ENCRYPT |
1932 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001933 PSA_KEY_USAGE_SIGN_HASH |
1934 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001935 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1936 return( PSA_ERROR_INVALID_ARGUMENT );
1937
Gilles Peskine4747d192019-04-17 15:05:45 +02001938 return( PSA_SUCCESS );
1939}
1940
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001941/** Validate the internal consistency of key attributes.
1942 *
1943 * This function only rejects invalid attribute values. If does not
1944 * validate the consistency of the attributes with any key data that may
1945 * be involved in the creation of the key.
1946 *
1947 * Call this function early in the key creation process.
1948 *
1949 * \param[in] attributes Key attributes for the new key.
1950 * \param[out] p_drv On any return, the driver for the key, if any.
1951 * NULL for a transparent key.
1952 *
1953 */
1954static psa_status_t psa_validate_key_attributes(
1955 const psa_key_attributes_t *attributes,
1956 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001957{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001958 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001959
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001960 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1961 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001962 if( status != PSA_SUCCESS )
1963 return( status );
1964
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001965 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1966 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001967 if( status != PSA_SUCCESS )
1968 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001969
1970 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001971 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001972 return( status );
1973
1974 /* Refuse to create overly large keys.
1975 * Note that this doesn't trigger on import if the attributes don't
1976 * explicitly specify a size (so psa_get_key_bits returns 0), so
1977 * psa_import_key() needs its own checks. */
1978 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1979 return( PSA_ERROR_NOT_SUPPORTED );
1980
Gilles Peskine91e8c332019-08-02 19:19:39 +02001981 /* Reject invalid flags. These should not be reachable through the API. */
1982 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1983 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1984 return( PSA_ERROR_INVALID_ARGUMENT );
1985
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001986 return( PSA_SUCCESS );
1987}
1988
Gilles Peskine4747d192019-04-17 15:05:45 +02001989/** Prepare a key slot to receive key material.
1990 *
1991 * This function allocates a key slot and sets its metadata.
1992 *
1993 * If this function fails, call psa_fail_key_creation().
1994 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001995 * This function is intended to be used as follows:
1996 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1997 * it with the specified attributes, and assign it a handle.
1998 * -# Populate the slot with the key material.
1999 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
2000 * In case of failure at any step, stop the sequence and call
2001 * psa_fail_key_creation().
2002 *
Gilles Peskinedf179142019-07-15 22:02:14 +02002003 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02002004 * \param[in] attributes Key attributes for the new key.
2005 * \param[out] handle On success, a handle for the allocated slot.
2006 * \param[out] p_slot On success, a pointer to the prepared slot.
2007 * \param[out] p_drv On any return, the driver for the key, if any.
2008 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002009 *
2010 * \retval #PSA_SUCCESS
2011 * The key slot is ready to receive key material.
2012 * \return If this function fails, the key slot is an invalid state.
2013 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002014 */
2015static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02002016 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02002017 const psa_key_attributes_t *attributes,
2018 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02002019 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002020 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02002021{
2022 psa_status_t status;
2023 psa_key_slot_t *slot;
2024
Gilles Peskinedf179142019-07-15 22:02:14 +02002025 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02002026 *p_drv = NULL;
2027
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002028 status = psa_validate_key_attributes( attributes, p_drv );
2029 if( status != PSA_SUCCESS )
2030 return( status );
2031
Gilles Peskineedbed562019-08-07 18:19:59 +02002032 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02002033 if( status != PSA_SUCCESS )
2034 return( status );
2035 slot = *p_slot;
2036
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002037 /* We're storing the declared bit-size of the key. It's up to each
2038 * creation mechanism to verify that this information is correct.
2039 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02002040 * an input (generate, device) but not for those where the bit-size
2041 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002042
2043 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02002044
Gilles Peskine91e8c332019-08-02 19:19:39 +02002045 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02002046 * external-only flags, query `attributes`. Thanks to the check
2047 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02002048 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02002049 * may have set. */
2050 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02002051
Gilles Peskinecbaff462019-07-12 23:46:04 +02002052#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002053 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002054 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02002055 * create the key file in internal storage, create the
2056 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002057 * persistent data. This is done by starting a transaction that will
2058 * encompass these three actions.
2059 * For registering a volatile key, we just need to find an appropriate
2060 * slot number inside the SE. Since the key is designated volatile, creating
2061 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02002062 /* The first thing to do is to find a slot number for the new key.
2063 * We save the slot number in persistent storage as part of the
2064 * transaction data. It will be needed to recover if the power
2065 * fails during the key creation process, to clean up on the secure
2066 * element side after restarting. Obtaining a slot number from the
2067 * secure element driver updates its persistent state, but we do not yet
2068 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02002069 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002070 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00002071 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02002072 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02002073 &slot->data.se.slot_number );
2074 if( status != PSA_SUCCESS )
2075 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002076
2077 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002078 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002079 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2080 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
2081 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
2082 psa_crypto_transaction.key.id = slot->attr.id;
2083 status = psa_crypto_save_transaction( );
2084 if( status != PSA_SUCCESS )
2085 {
2086 (void) psa_crypto_stop_transaction( );
2087 return( status );
2088 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002089 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002090 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002091
2092 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2093 {
2094 /* Key registration only makes sense with a secure element. */
2095 return( PSA_ERROR_INVALID_ARGUMENT );
2096 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002097#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2098
Darryl Green0c6575a2018-11-07 16:05:30 +00002099 return( status );
2100}
Gilles Peskine4747d192019-04-17 15:05:45 +02002101
2102/** Finalize the creation of a key once its key material has been set.
2103 *
2104 * This entails writing the key to persistent storage.
2105 *
2106 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002107 * See the documentation of psa_start_key_creation() for the intended use
2108 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002109 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002110 * \param[in,out] slot Pointer to the slot with key material.
2111 * \param[in] driver The secure element driver for the key,
2112 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002113 *
2114 * \retval #PSA_SUCCESS
2115 * The key was successfully created. The handle is now valid.
2116 * \return If this function fails, the key slot is an invalid state.
2117 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002118 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002119static psa_status_t psa_finish_key_creation(
2120 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002121 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002122{
2123 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002124 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002125 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002126
2127#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002128 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002129 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002130#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2131 if( driver != NULL )
2132 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002133 psa_se_key_data_storage_t data;
2134#if defined(static_assert)
2135 static_assert( sizeof( slot->data.se.slot_number ) ==
2136 sizeof( data.slot_number ),
2137 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002138#endif
2139 memcpy( &data.slot_number, &slot->data.se.slot_number,
2140 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002141 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002142 (uint8_t*) &data,
2143 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002144 }
2145 else
2146#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2147 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02002148 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02002149 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002150 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02002151 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
2152 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02002153 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02002154 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2155 status = psa_internal_export_key( slot,
2156 buffer, buffer_size, &length,
2157 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02002158 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002159 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02002160 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02002161
Gilles Peskinef9168942019-09-12 19:20:29 +02002162 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002163 mbedtls_free( buffer );
2164 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002165 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002166#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2167
Gilles Peskinecbaff462019-07-12 23:46:04 +02002168#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002169 /* Finish the transaction for a key creation. This does not
2170 * happen when registering an existing key. Detect this case
2171 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002172 * creation of a persistent key in a secure element requires a transaction,
2173 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002174 if( driver != NULL &&
2175 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002176 {
2177 status = psa_save_se_persistent_data( driver );
2178 if( status != PSA_SUCCESS )
2179 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002180 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002181 return( status );
2182 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002183 status = psa_crypto_stop_transaction( );
2184 if( status != PSA_SUCCESS )
2185 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002186 }
2187#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2188
Gilles Peskine4747d192019-04-17 15:05:45 +02002189 return( status );
2190}
2191
2192/** Abort the creation of a key.
2193 *
2194 * You may call this function after calling psa_start_key_creation(),
2195 * or after psa_finish_key_creation() fails. In other circumstances, this
2196 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002197 * See the documentation of psa_start_key_creation() for the intended use
2198 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002199 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002200 * \param[in,out] slot Pointer to the slot with key material.
2201 * \param[in] driver The secure element driver for the key,
2202 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002203 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002204static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002205 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002206{
Gilles Peskine011e4282019-06-26 18:34:38 +02002207 (void) driver;
2208
Gilles Peskine4747d192019-04-17 15:05:45 +02002209 if( slot == NULL )
2210 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002211
2212#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002213 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002214 * element, and the failure happened later (when saving metadata
2215 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002216 * element.
2217 * https://github.com/ARMmbed/mbed-crypto/issues/217
2218 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002219
Gilles Peskined7729582019-08-05 15:55:54 +02002220 /* Abort the ongoing transaction if any (there may not be one if
2221 * the creation process failed before starting one, or if the
2222 * key creation is a registration of a key in a secure element).
2223 * Earlier functions must already have done what it takes to undo any
2224 * partial creation. All that's left is to update the transaction data
2225 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002226 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002227#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2228
Gilles Peskine4747d192019-04-17 15:05:45 +02002229 psa_wipe_key_slot( slot );
2230}
2231
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002232/** Validate optional attributes during key creation.
2233 *
2234 * Some key attributes are optional during key creation. If they are
2235 * specified in the attributes structure, check that they are consistent
2236 * with the data in the slot.
2237 *
2238 * This function should be called near the end of key creation, after
2239 * the slot in memory is fully populated but before saving persistent data.
2240 */
2241static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002242 const psa_key_slot_t *slot,
2243 const psa_key_attributes_t *attributes )
2244{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002245 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002246 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002247 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002248 return( PSA_ERROR_INVALID_ARGUMENT );
2249 }
2250
2251 if( attributes->domain_parameters_size != 0 )
2252 {
2253#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002254 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002255 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002256 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002257 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002258 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002259
Steven Cooreman7f391872020-07-30 14:57:44 +02002260 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002261 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002262 slot->data.key.data,
2263 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002264 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002265 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002266 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002267
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002268 mbedtls_mpi_init( &actual );
2269 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002270 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002271 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002272 mbedtls_rsa_free( rsa );
2273 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002274 if( ret != 0 )
2275 goto rsa_exit;
2276 ret = mbedtls_mpi_read_binary( &required,
2277 attributes->domain_parameters,
2278 attributes->domain_parameters_size );
2279 if( ret != 0 )
2280 goto rsa_exit;
2281 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2282 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2283 rsa_exit:
2284 mbedtls_mpi_free( &actual );
2285 mbedtls_mpi_free( &required );
2286 if( ret != 0)
2287 return( mbedtls_to_psa_error( ret ) );
2288 }
2289 else
2290#endif
2291 {
2292 return( PSA_ERROR_INVALID_ARGUMENT );
2293 }
2294 }
2295
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002296 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002297 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002298 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002299 return( PSA_ERROR_INVALID_ARGUMENT );
2300 }
2301
2302 return( PSA_SUCCESS );
2303}
2304
Gilles Peskine4747d192019-04-17 15:05:45 +02002305psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002306 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002307 size_t data_length,
2308 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002309{
2310 psa_status_t status;
2311 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002312 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002313
Gilles Peskine0f84d622019-09-12 19:03:13 +02002314 /* Reject zero-length symmetric keys (including raw data key objects).
2315 * This also rejects any key which might be encoded as an empty string,
2316 * which is never valid. */
2317 if( data_length == 0 )
2318 return( PSA_ERROR_INVALID_ARGUMENT );
2319
Gilles Peskinedf179142019-07-15 22:02:14 +02002320 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2321 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002322 if( status != PSA_SUCCESS )
2323 goto exit;
2324
Gilles Peskine5d309672019-07-12 23:47:28 +02002325#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2326 if( driver != NULL )
2327 {
2328 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002329 /* The driver should set the number of key bits, however in
2330 * case it doesn't, we initialize bits to an invalid value. */
2331 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002332 if( drv->key_management == NULL ||
2333 drv->key_management->p_import == NULL )
2334 {
2335 status = PSA_ERROR_NOT_SUPPORTED;
2336 goto exit;
2337 }
2338 status = drv->key_management->p_import(
2339 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002340 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002341 &bits );
2342 if( status != PSA_SUCCESS )
2343 goto exit;
2344 if( bits > PSA_MAX_KEY_BITS )
2345 {
2346 status = PSA_ERROR_NOT_SUPPORTED;
2347 goto exit;
2348 }
2349 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002350 }
2351 else
2352#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2353 {
2354 status = psa_import_key_into_slot( slot, data, data_length );
2355 if( status != PSA_SUCCESS )
2356 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002357 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002358 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002359 if( status != PSA_SUCCESS )
2360 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002361
Gilles Peskine011e4282019-06-26 18:34:38 +02002362 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002363exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002364 if( status != PSA_SUCCESS )
2365 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002366 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002367 *handle = 0;
2368 }
2369 return( status );
2370}
2371
Gilles Peskined7729582019-08-05 15:55:54 +02002372#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2373psa_status_t mbedtls_psa_register_se_key(
2374 const psa_key_attributes_t *attributes )
2375{
2376 psa_status_t status;
2377 psa_key_slot_t *slot = NULL;
2378 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002379 psa_key_handle_t handle = 0;
2380
2381 /* Leaving attributes unspecified is not currently supported.
2382 * It could make sense to query the key type and size from the
2383 * secure element, but not all secure elements support this
2384 * and the driver HAL doesn't currently support it. */
2385 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2386 return( PSA_ERROR_NOT_SUPPORTED );
2387 if( psa_get_key_bits( attributes ) == 0 )
2388 return( PSA_ERROR_NOT_SUPPORTED );
2389
2390 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2391 &handle, &slot, &driver );
2392 if( status != PSA_SUCCESS )
2393 goto exit;
2394
Gilles Peskined7729582019-08-05 15:55:54 +02002395 status = psa_finish_key_creation( slot, driver );
2396
2397exit:
2398 if( status != PSA_SUCCESS )
2399 {
2400 psa_fail_key_creation( slot, driver );
2401 }
2402 /* Registration doesn't keep the key in RAM. */
2403 psa_close_key( handle );
2404 return( status );
2405}
2406#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2407
Gilles Peskinef603c712019-01-19 13:40:11 +01002408static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002409 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002410{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002411 psa_status_t status = psa_copy_key_material_into_slot( target,
2412 source->data.key.data,
2413 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002414 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002415 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002416
Steven Cooreman398aee52020-10-13 14:35:45 +02002417 target->attr.type = source->attr.type;
2418 target->attr.bits = source->attr.bits;
2419
2420 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002421}
2422
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002423psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2424 const psa_key_attributes_t *specified_attributes,
2425 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002426{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002427 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002428 psa_key_slot_t *source_slot = NULL;
2429 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002430 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002431 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002432
Gilles Peskine28f8f302019-07-24 13:30:31 +02002433 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002434 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002435 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002436 goto exit;
2437
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002438 status = psa_validate_optional_attributes( source_slot,
2439 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002440 if( status != PSA_SUCCESS )
2441 goto exit;
2442
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002443 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002444 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002445 if( status != PSA_SUCCESS )
2446 goto exit;
2447
Gilles Peskinedf179142019-07-15 22:02:14 +02002448 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2449 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002450 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002451 if( status != PSA_SUCCESS )
2452 goto exit;
2453
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002454#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2455 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002456 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002457 /* Copying to a secure element is not implemented yet. */
2458 status = PSA_ERROR_NOT_SUPPORTED;
2459 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002460 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002461#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002462
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002463 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002464 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002465 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002466
Gilles Peskine011e4282019-06-26 18:34:38 +02002467 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002468exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002469 if( status != PSA_SUCCESS )
2470 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002471 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002472 *target_handle = 0;
2473 }
2474 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002475}
2476
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002477
2478
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002479/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002480/* Message digests */
2481/****************************************************************/
2482
Gilles Peskineb16841e2019-10-10 20:36:12 +02002483#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002484static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002485{
2486 switch( alg )
2487 {
2488#if defined(MBEDTLS_MD2_C)
2489 case PSA_ALG_MD2:
2490 return( &mbedtls_md2_info );
2491#endif
2492#if defined(MBEDTLS_MD4_C)
2493 case PSA_ALG_MD4:
2494 return( &mbedtls_md4_info );
2495#endif
2496#if defined(MBEDTLS_MD5_C)
2497 case PSA_ALG_MD5:
2498 return( &mbedtls_md5_info );
2499#endif
2500#if defined(MBEDTLS_RIPEMD160_C)
2501 case PSA_ALG_RIPEMD160:
2502 return( &mbedtls_ripemd160_info );
2503#endif
2504#if defined(MBEDTLS_SHA1_C)
2505 case PSA_ALG_SHA_1:
2506 return( &mbedtls_sha1_info );
2507#endif
2508#if defined(MBEDTLS_SHA256_C)
2509 case PSA_ALG_SHA_224:
2510 return( &mbedtls_sha224_info );
2511 case PSA_ALG_SHA_256:
2512 return( &mbedtls_sha256_info );
2513#endif
2514#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002515#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002516 case PSA_ALG_SHA_384:
2517 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002518#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002519 case PSA_ALG_SHA_512:
2520 return( &mbedtls_sha512_info );
2521#endif
2522 default:
2523 return( NULL );
2524 }
2525}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002526#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002527
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002528psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2529{
2530 switch( operation->alg )
2531 {
Gilles Peskine81736312018-06-26 15:04:31 +02002532 case 0:
2533 /* The object has (apparently) been initialized but it is not
2534 * in use. It's ok to call abort on such an object, and there's
2535 * nothing to do. */
2536 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002537#if defined(MBEDTLS_MD2_C)
2538 case PSA_ALG_MD2:
2539 mbedtls_md2_free( &operation->ctx.md2 );
2540 break;
2541#endif
2542#if defined(MBEDTLS_MD4_C)
2543 case PSA_ALG_MD4:
2544 mbedtls_md4_free( &operation->ctx.md4 );
2545 break;
2546#endif
2547#if defined(MBEDTLS_MD5_C)
2548 case PSA_ALG_MD5:
2549 mbedtls_md5_free( &operation->ctx.md5 );
2550 break;
2551#endif
2552#if defined(MBEDTLS_RIPEMD160_C)
2553 case PSA_ALG_RIPEMD160:
2554 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2555 break;
2556#endif
2557#if defined(MBEDTLS_SHA1_C)
2558 case PSA_ALG_SHA_1:
2559 mbedtls_sha1_free( &operation->ctx.sha1 );
2560 break;
2561#endif
2562#if defined(MBEDTLS_SHA256_C)
2563 case PSA_ALG_SHA_224:
2564 case PSA_ALG_SHA_256:
2565 mbedtls_sha256_free( &operation->ctx.sha256 );
2566 break;
2567#endif
2568#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002569#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002570 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002571#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002572 case PSA_ALG_SHA_512:
2573 mbedtls_sha512_free( &operation->ctx.sha512 );
2574 break;
2575#endif
2576 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002577 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002578 }
2579 operation->alg = 0;
2580 return( PSA_SUCCESS );
2581}
2582
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002583psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002584 psa_algorithm_t alg )
2585{
Janos Follath24eed8d2019-11-22 13:21:35 +00002586 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002587
2588 /* A context must be freshly initialized before it can be set up. */
2589 if( operation->alg != 0 )
2590 {
2591 return( PSA_ERROR_BAD_STATE );
2592 }
2593
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002594 switch( alg )
2595 {
2596#if defined(MBEDTLS_MD2_C)
2597 case PSA_ALG_MD2:
2598 mbedtls_md2_init( &operation->ctx.md2 );
2599 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2600 break;
2601#endif
2602#if defined(MBEDTLS_MD4_C)
2603 case PSA_ALG_MD4:
2604 mbedtls_md4_init( &operation->ctx.md4 );
2605 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2606 break;
2607#endif
2608#if defined(MBEDTLS_MD5_C)
2609 case PSA_ALG_MD5:
2610 mbedtls_md5_init( &operation->ctx.md5 );
2611 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2612 break;
2613#endif
2614#if defined(MBEDTLS_RIPEMD160_C)
2615 case PSA_ALG_RIPEMD160:
2616 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2617 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2618 break;
2619#endif
2620#if defined(MBEDTLS_SHA1_C)
2621 case PSA_ALG_SHA_1:
2622 mbedtls_sha1_init( &operation->ctx.sha1 );
2623 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2624 break;
2625#endif
2626#if defined(MBEDTLS_SHA256_C)
2627 case PSA_ALG_SHA_224:
2628 mbedtls_sha256_init( &operation->ctx.sha256 );
2629 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2630 break;
2631 case PSA_ALG_SHA_256:
2632 mbedtls_sha256_init( &operation->ctx.sha256 );
2633 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2634 break;
2635#endif
2636#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002637#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002638 case PSA_ALG_SHA_384:
2639 mbedtls_sha512_init( &operation->ctx.sha512 );
2640 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2641 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002642#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002643 case PSA_ALG_SHA_512:
2644 mbedtls_sha512_init( &operation->ctx.sha512 );
2645 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2646 break;
2647#endif
2648 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002649 return( PSA_ALG_IS_HASH( alg ) ?
2650 PSA_ERROR_NOT_SUPPORTED :
2651 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002652 }
2653 if( ret == 0 )
2654 operation->alg = alg;
2655 else
2656 psa_hash_abort( operation );
2657 return( mbedtls_to_psa_error( ret ) );
2658}
2659
2660psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2661 const uint8_t *input,
2662 size_t input_length )
2663{
Janos Follath24eed8d2019-11-22 13:21:35 +00002664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002665
2666 /* Don't require hash implementations to behave correctly on a
2667 * zero-length input, which may have an invalid pointer. */
2668 if( input_length == 0 )
2669 return( PSA_SUCCESS );
2670
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002671 switch( operation->alg )
2672 {
2673#if defined(MBEDTLS_MD2_C)
2674 case PSA_ALG_MD2:
2675 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2676 input, input_length );
2677 break;
2678#endif
2679#if defined(MBEDTLS_MD4_C)
2680 case PSA_ALG_MD4:
2681 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2682 input, input_length );
2683 break;
2684#endif
2685#if defined(MBEDTLS_MD5_C)
2686 case PSA_ALG_MD5:
2687 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2688 input, input_length );
2689 break;
2690#endif
2691#if defined(MBEDTLS_RIPEMD160_C)
2692 case PSA_ALG_RIPEMD160:
2693 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2694 input, input_length );
2695 break;
2696#endif
2697#if defined(MBEDTLS_SHA1_C)
2698 case PSA_ALG_SHA_1:
2699 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2700 input, input_length );
2701 break;
2702#endif
2703#if defined(MBEDTLS_SHA256_C)
2704 case PSA_ALG_SHA_224:
2705 case PSA_ALG_SHA_256:
2706 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2707 input, input_length );
2708 break;
2709#endif
2710#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002711#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002712 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002713#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002714 case PSA_ALG_SHA_512:
2715 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2716 input, input_length );
2717 break;
2718#endif
2719 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002720 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002721 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002722
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002723 if( ret != 0 )
2724 psa_hash_abort( operation );
2725 return( mbedtls_to_psa_error( ret ) );
2726}
2727
2728psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2729 uint8_t *hash,
2730 size_t hash_size,
2731 size_t *hash_length )
2732{
itayzafrir40835d42018-08-02 13:14:17 +03002733 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002735 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002736
2737 /* Fill the output buffer with something that isn't a valid hash
2738 * (barring an attack on the hash and deliberately-crafted input),
2739 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002740 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002741 /* If hash_size is 0 then hash may be NULL and then the
2742 * call to memset would have undefined behavior. */
2743 if( hash_size != 0 )
2744 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002745
2746 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002747 {
2748 status = PSA_ERROR_BUFFER_TOO_SMALL;
2749 goto exit;
2750 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002751
2752 switch( operation->alg )
2753 {
2754#if defined(MBEDTLS_MD2_C)
2755 case PSA_ALG_MD2:
2756 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2757 break;
2758#endif
2759#if defined(MBEDTLS_MD4_C)
2760 case PSA_ALG_MD4:
2761 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2762 break;
2763#endif
2764#if defined(MBEDTLS_MD5_C)
2765 case PSA_ALG_MD5:
2766 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2767 break;
2768#endif
2769#if defined(MBEDTLS_RIPEMD160_C)
2770 case PSA_ALG_RIPEMD160:
2771 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2772 break;
2773#endif
2774#if defined(MBEDTLS_SHA1_C)
2775 case PSA_ALG_SHA_1:
2776 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2777 break;
2778#endif
2779#if defined(MBEDTLS_SHA256_C)
2780 case PSA_ALG_SHA_224:
2781 case PSA_ALG_SHA_256:
2782 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2783 break;
2784#endif
2785#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002786#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002787 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002788#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002789 case PSA_ALG_SHA_512:
2790 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2791 break;
2792#endif
2793 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002794 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002795 }
itayzafrir40835d42018-08-02 13:14:17 +03002796 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002797
itayzafrir40835d42018-08-02 13:14:17 +03002798exit:
2799 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002800 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002801 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002802 return( psa_hash_abort( operation ) );
2803 }
2804 else
2805 {
2806 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002807 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002808 }
2809}
2810
Gilles Peskine2d277862018-06-18 15:41:12 +02002811psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2812 const uint8_t *hash,
2813 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002814{
2815 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2816 size_t actual_hash_length;
2817 psa_status_t status = psa_hash_finish( operation,
2818 actual_hash, sizeof( actual_hash ),
2819 &actual_hash_length );
2820 if( status != PSA_SUCCESS )
2821 return( status );
2822 if( actual_hash_length != hash_length )
2823 return( PSA_ERROR_INVALID_SIGNATURE );
2824 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2825 return( PSA_ERROR_INVALID_SIGNATURE );
2826 return( PSA_SUCCESS );
2827}
2828
Gilles Peskine0a749c82019-11-28 19:33:58 +01002829psa_status_t psa_hash_compute( psa_algorithm_t alg,
2830 const uint8_t *input, size_t input_length,
2831 uint8_t *hash, size_t hash_size,
2832 size_t *hash_length )
2833{
2834 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2835 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2836
2837 *hash_length = hash_size;
2838 status = psa_hash_setup( &operation, alg );
2839 if( status != PSA_SUCCESS )
2840 goto exit;
2841 status = psa_hash_update( &operation, input, input_length );
2842 if( status != PSA_SUCCESS )
2843 goto exit;
2844 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2845 if( status != PSA_SUCCESS )
2846 goto exit;
2847
2848exit:
2849 if( status == PSA_SUCCESS )
2850 status = psa_hash_abort( &operation );
2851 else
2852 psa_hash_abort( &operation );
2853 return( status );
2854}
2855
2856psa_status_t psa_hash_compare( psa_algorithm_t alg,
2857 const uint8_t *input, size_t input_length,
2858 const uint8_t *hash, size_t hash_length )
2859{
2860 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2861 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2862
2863 status = psa_hash_setup( &operation, alg );
2864 if( status != PSA_SUCCESS )
2865 goto exit;
2866 status = psa_hash_update( &operation, input, input_length );
2867 if( status != PSA_SUCCESS )
2868 goto exit;
2869 status = psa_hash_verify( &operation, hash, hash_length );
2870 if( status != PSA_SUCCESS )
2871 goto exit;
2872
2873exit:
2874 if( status == PSA_SUCCESS )
2875 status = psa_hash_abort( &operation );
2876 else
2877 psa_hash_abort( &operation );
2878 return( status );
2879}
2880
Gilles Peskineeb35d782019-01-22 17:56:16 +01002881psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2882 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002883{
2884 if( target_operation->alg != 0 )
2885 return( PSA_ERROR_BAD_STATE );
2886
2887 switch( source_operation->alg )
2888 {
2889 case 0:
2890 return( PSA_ERROR_BAD_STATE );
2891#if defined(MBEDTLS_MD2_C)
2892 case PSA_ALG_MD2:
2893 mbedtls_md2_clone( &target_operation->ctx.md2,
2894 &source_operation->ctx.md2 );
2895 break;
2896#endif
2897#if defined(MBEDTLS_MD4_C)
2898 case PSA_ALG_MD4:
2899 mbedtls_md4_clone( &target_operation->ctx.md4,
2900 &source_operation->ctx.md4 );
2901 break;
2902#endif
2903#if defined(MBEDTLS_MD5_C)
2904 case PSA_ALG_MD5:
2905 mbedtls_md5_clone( &target_operation->ctx.md5,
2906 &source_operation->ctx.md5 );
2907 break;
2908#endif
2909#if defined(MBEDTLS_RIPEMD160_C)
2910 case PSA_ALG_RIPEMD160:
2911 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2912 &source_operation->ctx.ripemd160 );
2913 break;
2914#endif
2915#if defined(MBEDTLS_SHA1_C)
2916 case PSA_ALG_SHA_1:
2917 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2918 &source_operation->ctx.sha1 );
2919 break;
2920#endif
2921#if defined(MBEDTLS_SHA256_C)
2922 case PSA_ALG_SHA_224:
2923 case PSA_ALG_SHA_256:
2924 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2925 &source_operation->ctx.sha256 );
2926 break;
2927#endif
2928#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002929#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002930 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002931#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002932 case PSA_ALG_SHA_512:
2933 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2934 &source_operation->ctx.sha512 );
2935 break;
2936#endif
2937 default:
2938 return( PSA_ERROR_NOT_SUPPORTED );
2939 }
2940
2941 target_operation->alg = source_operation->alg;
2942 return( PSA_SUCCESS );
2943}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002944
2945
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002946/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002947/* MAC */
2948/****************************************************************/
2949
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002950static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002951 psa_algorithm_t alg,
2952 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002953 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002954 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002955{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002956 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002957 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002958
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002959 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002960 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002961
Gilles Peskine8c9def32018-02-08 10:02:12 +01002962 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2963 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002964 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002965 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002966 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002967 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002968 mode = MBEDTLS_MODE_STREAM;
2969 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002970 case PSA_ALG_CTR:
2971 mode = MBEDTLS_MODE_CTR;
2972 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002973 case PSA_ALG_CFB:
2974 mode = MBEDTLS_MODE_CFB;
2975 break;
2976 case PSA_ALG_OFB:
2977 mode = MBEDTLS_MODE_OFB;
2978 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002979 case PSA_ALG_ECB_NO_PADDING:
2980 mode = MBEDTLS_MODE_ECB;
2981 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002982 case PSA_ALG_CBC_NO_PADDING:
2983 mode = MBEDTLS_MODE_CBC;
2984 break;
2985 case PSA_ALG_CBC_PKCS7:
2986 mode = MBEDTLS_MODE_CBC;
2987 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002988 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002989 mode = MBEDTLS_MODE_CCM;
2990 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002991 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002992 mode = MBEDTLS_MODE_GCM;
2993 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002994 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2995 mode = MBEDTLS_MODE_CHACHAPOLY;
2996 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002997 default:
2998 return( NULL );
2999 }
3000 }
3001 else if( alg == PSA_ALG_CMAC )
3002 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003003 else
3004 return( NULL );
3005
3006 switch( key_type )
3007 {
3008 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03003009 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003010 break;
3011 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003012 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3013 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003014 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003015 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003016 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003017 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003018 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3019 * but two-key Triple-DES is functionally three-key Triple-DES
3020 * with K1=K3, so that's how we present it to mbedtls. */
3021 if( key_bits == 128 )
3022 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003023 break;
3024 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003025 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003026 break;
3027 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003028 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003029 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003030 case PSA_KEY_TYPE_CHACHA20:
3031 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3032 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003033 default:
3034 return( NULL );
3035 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003036 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003037 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003038
Jaeden Amero23bbb752018-06-26 14:16:54 +01003039 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3040 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003041}
3042
Gilles Peskinea05219c2018-11-16 16:02:56 +01003043#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003044static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003045{
Gilles Peskine2d277862018-06-18 15:41:12 +02003046 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003047 {
3048 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003049 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003050 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003051 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003052 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003053 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003054 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003055 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003056 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003057 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003058 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003059 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003060 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003061 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003062 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003063 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003064 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003065 return( 128 );
3066 default:
3067 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003068 }
3069}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003070#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003071
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003072/* Initialize the MAC operation structure. Once this function has been
3073 * called, psa_mac_abort can run and will do the right thing. */
3074static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3075 psa_algorithm_t alg )
3076{
3077 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3078
3079 operation->alg = alg;
3080 operation->key_set = 0;
3081 operation->iv_set = 0;
3082 operation->iv_required = 0;
3083 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003084 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003085
3086#if defined(MBEDTLS_CMAC_C)
3087 if( alg == PSA_ALG_CMAC )
3088 {
3089 operation->iv_required = 0;
3090 mbedtls_cipher_init( &operation->ctx.cmac );
3091 status = PSA_SUCCESS;
3092 }
3093 else
3094#endif /* MBEDTLS_CMAC_C */
3095#if defined(MBEDTLS_MD_C)
3096 if( PSA_ALG_IS_HMAC( operation->alg ) )
3097 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003098 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3099 operation->ctx.hmac.hash_ctx.alg = 0;
3100 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003101 }
3102 else
3103#endif /* MBEDTLS_MD_C */
3104 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003105 if( ! PSA_ALG_IS_MAC( alg ) )
3106 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003107 }
3108
3109 if( status != PSA_SUCCESS )
3110 memset( operation, 0, sizeof( *operation ) );
3111 return( status );
3112}
3113
Gilles Peskine01126fa2018-07-12 17:04:55 +02003114#if defined(MBEDTLS_MD_C)
3115static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3116{
Gilles Peskine3f108122018-12-07 18:14:53 +01003117 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003118 return( psa_hash_abort( &hmac->hash_ctx ) );
3119}
3120#endif /* MBEDTLS_MD_C */
3121
Gilles Peskine8c9def32018-02-08 10:02:12 +01003122psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3123{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003124 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003125 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003126 /* The object has (apparently) been initialized but it is not
3127 * in use. It's ok to call abort on such an object, and there's
3128 * nothing to do. */
3129 return( PSA_SUCCESS );
3130 }
3131 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003132#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003133 if( operation->alg == PSA_ALG_CMAC )
3134 {
3135 mbedtls_cipher_free( &operation->ctx.cmac );
3136 }
3137 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003138#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003139#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003140 if( PSA_ALG_IS_HMAC( operation->alg ) )
3141 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003142 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003143 }
3144 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003145#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003146 {
3147 /* Sanity check (shouldn't happen: operation->alg should
3148 * always have been initialized to a valid value). */
3149 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003150 }
Moran Peker41deec42018-04-04 15:43:05 +03003151
Gilles Peskine8c9def32018-02-08 10:02:12 +01003152 operation->alg = 0;
3153 operation->key_set = 0;
3154 operation->iv_set = 0;
3155 operation->iv_required = 0;
3156 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003157 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003158
Gilles Peskine8c9def32018-02-08 10:02:12 +01003159 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003160
3161bad_state:
3162 /* If abort is called on an uninitialized object, we can't trust
3163 * anything. Wipe the object in case it contains confidential data.
3164 * This may result in a memory leak if a pointer gets overwritten,
3165 * but it's too late to do anything about this. */
3166 memset( operation, 0, sizeof( *operation ) );
3167 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003168}
3169
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003170#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003171static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003172 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003173 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003174 const mbedtls_cipher_info_t *cipher_info )
3175{
Janos Follath24eed8d2019-11-22 13:21:35 +00003176 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003177
3178 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003179
3180 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3181 if( ret != 0 )
3182 return( ret );
3183
3184 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003185 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003186 key_bits );
3187 return( ret );
3188}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003189#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003190
Gilles Peskine248051a2018-06-20 16:09:38 +02003191#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003192static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3193 const uint8_t *key,
3194 size_t key_length,
3195 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003196{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003197 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003198 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003199 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003200 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003201 psa_status_t status;
3202
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003203 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3204 * overflow below. This should never trigger if the hash algorithm
3205 * is implemented correctly. */
3206 /* The size checks against the ipad and opad buffers cannot be written
3207 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3208 * because that triggers -Wlogical-op on GCC 7.3. */
3209 if( block_size > sizeof( ipad ) )
3210 return( PSA_ERROR_NOT_SUPPORTED );
3211 if( block_size > sizeof( hmac->opad ) )
3212 return( PSA_ERROR_NOT_SUPPORTED );
3213 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003214 return( PSA_ERROR_NOT_SUPPORTED );
3215
Gilles Peskined223b522018-06-11 18:12:58 +02003216 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003217 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003218 status = psa_hash_compute( hash_alg, key, key_length,
3219 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003220 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003221 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003222 }
Gilles Peskine96889972018-07-12 17:07:03 +02003223 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3224 * but it is permitted. It is common when HMAC is used in HKDF, for
3225 * example. Don't call `memcpy` in the 0-length because `key` could be
3226 * an invalid pointer which would make the behavior undefined. */
3227 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003228 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003229
Gilles Peskined223b522018-06-11 18:12:58 +02003230 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3231 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003232 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003233 ipad[i] ^= 0x36;
3234 memset( ipad + key_length, 0x36, block_size - key_length );
3235
3236 /* Copy the key material from ipad to opad, flipping the requisite bits,
3237 * and filling the rest of opad with the requisite constant. */
3238 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003239 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3240 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003241
Gilles Peskine01126fa2018-07-12 17:04:55 +02003242 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003243 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003244 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003245
Gilles Peskine01126fa2018-07-12 17:04:55 +02003246 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003247
3248cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003249 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003250
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003251 return( status );
3252}
Gilles Peskine248051a2018-06-20 16:09:38 +02003253#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003254
Gilles Peskine89167cb2018-07-08 20:12:23 +02003255static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003256 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003257 psa_algorithm_t alg,
3258 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003259{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003260 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003261 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003262 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003263 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003264 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003265 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003266 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003267
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003268 /* A context must be freshly initialized before it can be set up. */
3269 if( operation->alg != 0 )
3270 {
3271 return( PSA_ERROR_BAD_STATE );
3272 }
3273
Gilles Peskined911eb72018-08-14 15:18:45 +02003274 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003275 if( status != PSA_SUCCESS )
3276 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003277 if( is_sign )
3278 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003279
Gilles Peskine28f8f302019-07-24 13:30:31 +02003280 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003281 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003282 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003283 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003284
Gilles Peskine8c9def32018-02-08 10:02:12 +01003285#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003286 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003287 {
3288 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003289 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003290 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003292 if( cipher_info == NULL )
3293 {
3294 status = PSA_ERROR_NOT_SUPPORTED;
3295 goto exit;
3296 }
3297 operation->mac_size = cipher_info->block_size;
3298 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3299 status = mbedtls_to_psa_error( ret );
3300 }
3301 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003302#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003303#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003304 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003305 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003306 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003307 if( hash_alg == 0 )
3308 {
3309 status = PSA_ERROR_NOT_SUPPORTED;
3310 goto exit;
3311 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003312
3313 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3314 /* Sanity check. This shouldn't fail on a valid configuration. */
3315 if( operation->mac_size == 0 ||
3316 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3317 {
3318 status = PSA_ERROR_NOT_SUPPORTED;
3319 goto exit;
3320 }
3321
Gilles Peskine8e338702019-07-30 20:06:31 +02003322 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003323 {
3324 status = PSA_ERROR_INVALID_ARGUMENT;
3325 goto exit;
3326 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003327
Gilles Peskine01126fa2018-07-12 17:04:55 +02003328 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003329 slot->data.key.data,
3330 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003331 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003332 }
3333 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003334#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003335 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003336 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003337 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003338 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003339
Gilles Peskined911eb72018-08-14 15:18:45 +02003340 if( truncated == 0 )
3341 {
3342 /* The "normal" case: untruncated algorithm. Nothing to do. */
3343 }
3344 else if( truncated < 4 )
3345 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003346 /* A very short MAC is too short for security since it can be
3347 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3348 * so we make this our minimum, even though 32 bits is still
3349 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003350 status = PSA_ERROR_NOT_SUPPORTED;
3351 }
3352 else if( truncated > operation->mac_size )
3353 {
3354 /* It's impossible to "truncate" to a larger length. */
3355 status = PSA_ERROR_INVALID_ARGUMENT;
3356 }
3357 else
3358 operation->mac_size = truncated;
3359
Gilles Peskinefbfac682018-07-08 20:51:54 +02003360exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003361 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003362 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003363 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003364 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003365 else
3366 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003367 operation->key_set = 1;
3368 }
3369 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003370}
3371
Gilles Peskine89167cb2018-07-08 20:12:23 +02003372psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003373 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003374 psa_algorithm_t alg )
3375{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003376 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003377}
3378
3379psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003380 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003381 psa_algorithm_t alg )
3382{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003383 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003384}
3385
Gilles Peskine8c9def32018-02-08 10:02:12 +01003386psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3387 const uint8_t *input,
3388 size_t input_length )
3389{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003390 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003391 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003392 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003393 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003394 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003395 operation->has_input = 1;
3396
Gilles Peskine8c9def32018-02-08 10:02:12 +01003397#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003398 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003399 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003400 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3401 input, input_length );
3402 status = mbedtls_to_psa_error( ret );
3403 }
3404 else
3405#endif /* MBEDTLS_CMAC_C */
3406#if defined(MBEDTLS_MD_C)
3407 if( PSA_ALG_IS_HMAC( operation->alg ) )
3408 {
3409 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3410 input_length );
3411 }
3412 else
3413#endif /* MBEDTLS_MD_C */
3414 {
3415 /* This shouldn't happen if `operation` was initialized by
3416 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003417 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003418 }
3419
Gilles Peskinefbfac682018-07-08 20:51:54 +02003420 if( status != PSA_SUCCESS )
3421 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003422 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003423}
3424
Gilles Peskine01126fa2018-07-12 17:04:55 +02003425#if defined(MBEDTLS_MD_C)
3426static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3427 uint8_t *mac,
3428 size_t mac_size )
3429{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003430 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003431 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3432 size_t hash_size = 0;
3433 size_t block_size = psa_get_hash_block_size( hash_alg );
3434 psa_status_t status;
3435
Gilles Peskine01126fa2018-07-12 17:04:55 +02003436 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3437 if( status != PSA_SUCCESS )
3438 return( status );
3439 /* From here on, tmp needs to be wiped. */
3440
3441 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3442 if( status != PSA_SUCCESS )
3443 goto exit;
3444
3445 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3446 if( status != PSA_SUCCESS )
3447 goto exit;
3448
3449 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3450 if( status != PSA_SUCCESS )
3451 goto exit;
3452
Gilles Peskined911eb72018-08-14 15:18:45 +02003453 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3454 if( status != PSA_SUCCESS )
3455 goto exit;
3456
3457 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003458
3459exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003460 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003461 return( status );
3462}
3463#endif /* MBEDTLS_MD_C */
3464
mohammad16036df908f2018-04-02 08:34:15 -07003465static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003466 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003467 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003468{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003469 if( ! operation->key_set )
3470 return( PSA_ERROR_BAD_STATE );
3471 if( operation->iv_required && ! operation->iv_set )
3472 return( PSA_ERROR_BAD_STATE );
3473
Gilles Peskine8c9def32018-02-08 10:02:12 +01003474 if( mac_size < operation->mac_size )
3475 return( PSA_ERROR_BUFFER_TOO_SMALL );
3476
Gilles Peskine8c9def32018-02-08 10:02:12 +01003477#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003478 if( operation->alg == PSA_ALG_CMAC )
3479 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003480 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3481 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3482 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003483 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003484 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003485 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003486 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003487 else
3488#endif /* MBEDTLS_CMAC_C */
3489#if defined(MBEDTLS_MD_C)
3490 if( PSA_ALG_IS_HMAC( operation->alg ) )
3491 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003492 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003493 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003494 }
3495 else
3496#endif /* MBEDTLS_MD_C */
3497 {
3498 /* This shouldn't happen if `operation` was initialized by
3499 * a setup function. */
3500 return( PSA_ERROR_BAD_STATE );
3501 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003502}
3503
Gilles Peskineacd4be32018-07-08 19:56:25 +02003504psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3505 uint8_t *mac,
3506 size_t mac_size,
3507 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003508{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003509 psa_status_t status;
3510
Jaeden Amero252ef282019-02-15 14:05:35 +00003511 if( operation->alg == 0 )
3512 {
3513 return( PSA_ERROR_BAD_STATE );
3514 }
3515
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003516 /* Fill the output buffer with something that isn't a valid mac
3517 * (barring an attack on the mac and deliberately-crafted input),
3518 * in case the caller doesn't check the return status properly. */
3519 *mac_length = mac_size;
3520 /* If mac_size is 0 then mac may be NULL and then the
3521 * call to memset would have undefined behavior. */
3522 if( mac_size != 0 )
3523 memset( mac, '!', mac_size );
3524
Gilles Peskine89167cb2018-07-08 20:12:23 +02003525 if( ! operation->is_sign )
3526 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003527 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003528 }
mohammad16036df908f2018-04-02 08:34:15 -07003529
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003530 status = psa_mac_finish_internal( operation, mac, mac_size );
3531
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003532 if( status == PSA_SUCCESS )
3533 {
3534 status = psa_mac_abort( operation );
3535 if( status == PSA_SUCCESS )
3536 *mac_length = operation->mac_size;
3537 else
3538 memset( mac, '!', mac_size );
3539 }
3540 else
3541 psa_mac_abort( operation );
3542 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003543}
3544
Gilles Peskineacd4be32018-07-08 19:56:25 +02003545psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3546 const uint8_t *mac,
3547 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003548{
Gilles Peskine828ed142018-06-18 23:25:51 +02003549 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003550 psa_status_t status;
3551
Jaeden Amero252ef282019-02-15 14:05:35 +00003552 if( operation->alg == 0 )
3553 {
3554 return( PSA_ERROR_BAD_STATE );
3555 }
3556
Gilles Peskine89167cb2018-07-08 20:12:23 +02003557 if( operation->is_sign )
3558 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003559 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003560 }
3561 if( operation->mac_size != mac_length )
3562 {
3563 status = PSA_ERROR_INVALID_SIGNATURE;
3564 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003565 }
mohammad16036df908f2018-04-02 08:34:15 -07003566
3567 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003568 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003569 if( status != PSA_SUCCESS )
3570 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003571
3572 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3573 status = PSA_ERROR_INVALID_SIGNATURE;
3574
3575cleanup:
3576 if( status == PSA_SUCCESS )
3577 status = psa_mac_abort( operation );
3578 else
3579 psa_mac_abort( operation );
3580
Gilles Peskine3f108122018-12-07 18:14:53 +01003581 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003582
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003583 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003584}
3585
3586
Gilles Peskine20035e32018-02-03 22:44:14 +01003587
Gilles Peskine20035e32018-02-03 22:44:14 +01003588/****************************************************************/
3589/* Asymmetric cryptography */
3590/****************************************************************/
3591
Gilles Peskine2b450e32018-06-27 15:42:46 +02003592#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003593/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003594 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003595static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3596 size_t hash_length,
3597 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003598{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003599 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003600 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003601 *md_alg = mbedtls_md_get_type( md_info );
3602
3603 /* The Mbed TLS RSA module uses an unsigned int for hash length
3604 * parameters. Validate that it fits so that we don't risk an
3605 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003606#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003607 if( hash_length > UINT_MAX )
3608 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003609#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003610
3611#if defined(MBEDTLS_PKCS1_V15)
3612 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3613 * must be correct. */
3614 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3615 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003616 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003617 if( md_info == NULL )
3618 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003619 if( mbedtls_md_get_size( md_info ) != hash_length )
3620 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003621 }
3622#endif /* MBEDTLS_PKCS1_V15 */
3623
3624#if defined(MBEDTLS_PKCS1_V21)
3625 /* PSS requires a hash internally. */
3626 if( PSA_ALG_IS_RSA_PSS( alg ) )
3627 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003628 if( md_info == NULL )
3629 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003630 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003631#endif /* MBEDTLS_PKCS1_V21 */
3632
Gilles Peskine61b91d42018-06-08 16:09:36 +02003633 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003634}
3635
Gilles Peskine2b450e32018-06-27 15:42:46 +02003636static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3637 psa_algorithm_t alg,
3638 const uint8_t *hash,
3639 size_t hash_length,
3640 uint8_t *signature,
3641 size_t signature_size,
3642 size_t *signature_length )
3643{
3644 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003646 mbedtls_md_type_t md_alg;
3647
3648 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3649 if( status != PSA_SUCCESS )
3650 return( status );
3651
Gilles Peskine630a18a2018-06-29 17:49:35 +02003652 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003653 return( PSA_ERROR_BUFFER_TOO_SMALL );
3654
3655#if defined(MBEDTLS_PKCS1_V15)
3656 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3657 {
3658 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3659 MBEDTLS_MD_NONE );
3660 ret = mbedtls_rsa_pkcs1_sign( rsa,
3661 mbedtls_ctr_drbg_random,
3662 &global_data.ctr_drbg,
3663 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003664 md_alg,
3665 (unsigned int) hash_length,
3666 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003667 signature );
3668 }
3669 else
3670#endif /* MBEDTLS_PKCS1_V15 */
3671#if defined(MBEDTLS_PKCS1_V21)
3672 if( PSA_ALG_IS_RSA_PSS( alg ) )
3673 {
3674 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3675 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3676 mbedtls_ctr_drbg_random,
3677 &global_data.ctr_drbg,
3678 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003679 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003680 (unsigned int) hash_length,
3681 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003682 signature );
3683 }
3684 else
3685#endif /* MBEDTLS_PKCS1_V21 */
3686 {
3687 return( PSA_ERROR_INVALID_ARGUMENT );
3688 }
3689
3690 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003691 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003692 return( mbedtls_to_psa_error( ret ) );
3693}
3694
3695static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3696 psa_algorithm_t alg,
3697 const uint8_t *hash,
3698 size_t hash_length,
3699 const uint8_t *signature,
3700 size_t signature_length )
3701{
3702 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003703 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003704 mbedtls_md_type_t md_alg;
3705
3706 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3707 if( status != PSA_SUCCESS )
3708 return( status );
3709
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003710 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3711 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003712
3713#if defined(MBEDTLS_PKCS1_V15)
3714 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3715 {
3716 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3717 MBEDTLS_MD_NONE );
3718 ret = mbedtls_rsa_pkcs1_verify( rsa,
3719 mbedtls_ctr_drbg_random,
3720 &global_data.ctr_drbg,
3721 MBEDTLS_RSA_PUBLIC,
3722 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003723 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003724 hash,
3725 signature );
3726 }
3727 else
3728#endif /* MBEDTLS_PKCS1_V15 */
3729#if defined(MBEDTLS_PKCS1_V21)
3730 if( PSA_ALG_IS_RSA_PSS( alg ) )
3731 {
3732 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3733 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3734 mbedtls_ctr_drbg_random,
3735 &global_data.ctr_drbg,
3736 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003737 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003738 (unsigned int) hash_length,
3739 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003740 signature );
3741 }
3742 else
3743#endif /* MBEDTLS_PKCS1_V21 */
3744 {
3745 return( PSA_ERROR_INVALID_ARGUMENT );
3746 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003747
3748 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3749 * the rest of the signature is invalid". This has little use in
3750 * practice and PSA doesn't report this distinction. */
3751 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3752 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003753 return( mbedtls_to_psa_error( ret ) );
3754}
3755#endif /* MBEDTLS_RSA_C */
3756
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003757#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003758/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3759 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3760 * (even though these functions don't modify it). */
3761static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3762 psa_algorithm_t alg,
3763 const uint8_t *hash,
3764 size_t hash_length,
3765 uint8_t *signature,
3766 size_t signature_size,
3767 size_t *signature_length )
3768{
Janos Follath24eed8d2019-11-22 13:21:35 +00003769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003770 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003771 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003772 mbedtls_mpi_init( &r );
3773 mbedtls_mpi_init( &s );
3774
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003775 if( signature_size < 2 * curve_bytes )
3776 {
3777 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3778 goto cleanup;
3779 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003780
Gilles Peskinea05219c2018-11-16 16:02:56 +01003781#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003782 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3783 {
3784 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3785 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3786 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003787 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3788 &ecp->d, hash,
3789 hash_length, md_alg,
3790 mbedtls_ctr_drbg_random,
3791 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003792 }
3793 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003794#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003795 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003796 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003797 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3798 hash, hash_length,
3799 mbedtls_ctr_drbg_random,
3800 &global_data.ctr_drbg ) );
3801 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003802
3803 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3804 signature,
3805 curve_bytes ) );
3806 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3807 signature + curve_bytes,
3808 curve_bytes ) );
3809
3810cleanup:
3811 mbedtls_mpi_free( &r );
3812 mbedtls_mpi_free( &s );
3813 if( ret == 0 )
3814 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003815 return( mbedtls_to_psa_error( ret ) );
3816}
3817
3818static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3819 const uint8_t *hash,
3820 size_t hash_length,
3821 const uint8_t *signature,
3822 size_t signature_length )
3823{
Janos Follath24eed8d2019-11-22 13:21:35 +00003824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003825 mbedtls_mpi r, s;
3826 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3827 mbedtls_mpi_init( &r );
3828 mbedtls_mpi_init( &s );
3829
3830 if( signature_length != 2 * curve_bytes )
3831 return( PSA_ERROR_INVALID_SIGNATURE );
3832
3833 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3834 signature,
3835 curve_bytes ) );
3836 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3837 signature + curve_bytes,
3838 curve_bytes ) );
3839
Steven Cooremanacda8342020-07-24 23:09:52 +02003840 /* Check whether the public part is loaded. If not, load it. */
3841 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3842 {
3843 MBEDTLS_MPI_CHK(
3844 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3845 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3846 }
3847
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003848 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3849 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003850
3851cleanup:
3852 mbedtls_mpi_free( &r );
3853 mbedtls_mpi_free( &s );
3854 return( mbedtls_to_psa_error( ret ) );
3855}
3856#endif /* MBEDTLS_ECDSA_C */
3857
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003858psa_status_t psa_sign_hash( psa_key_handle_t handle,
3859 psa_algorithm_t alg,
3860 const uint8_t *hash,
3861 size_t hash_length,
3862 uint8_t *signature,
3863 size_t signature_size,
3864 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003865{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003866 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003867 psa_status_t status;
3868
3869 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003870 /* Immediately reject a zero-length signature buffer. This guarantees
3871 * that signature must be a valid pointer. (On the other hand, the hash
3872 * buffer can in principle be empty since it doesn't actually have
3873 * to be a hash.) */
3874 if( signature_size == 0 )
3875 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003876
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003877 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003878 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003879 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003880 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003881 {
3882 status = PSA_ERROR_INVALID_ARGUMENT;
3883 goto exit;
3884 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003885
Steven Cooremancd84cb42020-07-16 20:28:36 +02003886 /* Try any of the available accelerators first */
3887 status = psa_driver_wrapper_sign_hash( slot,
3888 alg,
3889 hash,
3890 hash_length,
3891 signature,
3892 signature_size,
3893 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003894 if( status != PSA_ERROR_NOT_SUPPORTED ||
3895 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003896 goto exit;
3897
Steven Cooreman7a250572020-07-17 16:43:05 +02003898 /* If the operation was not supported by any accelerator, try fallback. */
Gilles Peskine20035e32018-02-03 22:44:14 +01003899#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003900 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003901 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003902 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003903
Steven Cooreman4fed4552020-08-03 14:46:03 +02003904 status = psa_load_rsa_representation( slot->attr.type,
3905 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003906 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003907 &rsa );
3908 if( status != PSA_SUCCESS )
3909 goto exit;
3910
Steven Cooremana2371e52020-07-28 14:30:39 +02003911 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003912 alg,
3913 hash, hash_length,
3914 signature, signature_size,
3915 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003916
Steven Cooremana2371e52020-07-28 14:30:39 +02003917 mbedtls_rsa_free( rsa );
3918 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003919 }
3920 else
3921#endif /* defined(MBEDTLS_RSA_C) */
3922#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003923 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003924 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003925#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003926 if(
3927#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3928 PSA_ALG_IS_ECDSA( alg )
3929#else
3930 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3931#endif
3932 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003933 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003934 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003935 status = psa_load_ecp_representation( slot->attr.type,
3936 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003937 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003938 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003939 if( status != PSA_SUCCESS )
3940 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003941 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003942 alg,
3943 hash, hash_length,
3944 signature, signature_size,
3945 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003946 mbedtls_ecp_keypair_free( ecp );
3947 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003948 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003949 else
3950#endif /* defined(MBEDTLS_ECDSA_C) */
3951 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003952 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003953 }
itayzafrir5c753392018-05-08 11:18:38 +03003954 }
3955 else
3956#endif /* defined(MBEDTLS_ECP_C) */
3957 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003958 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003959 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003960
3961exit:
3962 /* Fill the unused part of the output buffer (the whole buffer on error,
3963 * the trailing part on success) with something that isn't a valid mac
3964 * (barring an attack on the mac and deliberately-crafted input),
3965 * in case the caller doesn't check the return status properly. */
3966 if( status == PSA_SUCCESS )
3967 memset( signature + *signature_length, '!',
3968 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003969 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003970 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003971 /* If signature_size is 0 then we have nothing to do. We must not call
3972 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003973 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003974}
3975
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003976psa_status_t psa_verify_hash( psa_key_handle_t handle,
3977 psa_algorithm_t alg,
3978 const uint8_t *hash,
3979 size_t hash_length,
3980 const uint8_t *signature,
3981 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003982{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003983 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003984 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003985
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003986 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003987 if( status != PSA_SUCCESS )
3988 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003989
Steven Cooreman55ae2172020-07-17 19:46:15 +02003990 /* Try any of the available accelerators first */
3991 status = psa_driver_wrapper_verify_hash( slot,
3992 alg,
3993 hash,
3994 hash_length,
3995 signature,
3996 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003997 if( status != PSA_ERROR_NOT_SUPPORTED ||
3998 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +02003999 return status;
4000
Gilles Peskine61b91d42018-06-08 16:09:36 +02004001#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004002 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004003 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004004 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004005
Steven Cooreman4fed4552020-08-03 14:46:03 +02004006 status = psa_load_rsa_representation( slot->attr.type,
4007 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004008 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004009 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004010 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004011 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02004012
Steven Cooremana2371e52020-07-28 14:30:39 +02004013 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004014 alg,
4015 hash, hash_length,
4016 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004017 mbedtls_rsa_free( rsa );
4018 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004019 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004020 }
4021 else
4022#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03004023#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004024 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004025 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004026#if defined(MBEDTLS_ECDSA_C)
4027 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004028 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004029 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004030 status = psa_load_ecp_representation( slot->attr.type,
4031 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004032 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004033 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004034 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004035 return( status );
Steven Cooremana2371e52020-07-28 14:30:39 +02004036 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004037 hash, hash_length,
4038 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004039 mbedtls_ecp_keypair_free( ecp );
4040 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02004041 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02004042 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004043 else
4044#endif /* defined(MBEDTLS_ECDSA_C) */
4045 {
4046 return( PSA_ERROR_INVALID_ARGUMENT );
4047 }
itayzafrir5c753392018-05-08 11:18:38 +03004048 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004049 else
4050#endif /* defined(MBEDTLS_ECP_C) */
4051 {
4052 return( PSA_ERROR_NOT_SUPPORTED );
4053 }
4054}
4055
Gilles Peskine072ac562018-06-30 00:21:29 +02004056#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
4057static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4058 mbedtls_rsa_context *rsa )
4059{
4060 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4061 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4062 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4063 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4064}
4065#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
4066
Gilles Peskinec5487a82018-12-03 18:08:14 +01004067psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004068 psa_algorithm_t alg,
4069 const uint8_t *input,
4070 size_t input_length,
4071 const uint8_t *salt,
4072 size_t salt_length,
4073 uint8_t *output,
4074 size_t output_size,
4075 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004076{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004077 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004078 psa_status_t status;
4079
Darryl Green5cc689a2018-07-24 15:34:10 +01004080 (void) input;
4081 (void) input_length;
4082 (void) salt;
4083 (void) output;
4084 (void) output_size;
4085
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004086 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004087
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004088 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4089 return( PSA_ERROR_INVALID_ARGUMENT );
4090
Gilles Peskine28f8f302019-07-24 13:30:31 +02004091 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004092 if( status != PSA_SUCCESS )
4093 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004094 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4095 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004096 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004097
4098#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004099 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004100 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004101 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004102 status = psa_load_rsa_representation( slot->attr.type,
4103 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004104 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004105 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004106 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004107 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004108
4109 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004110 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004111 status = PSA_ERROR_BUFFER_TOO_SMALL;
4112 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004113 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004114#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004115 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004116 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004117 status = mbedtls_to_psa_error(
4118 mbedtls_rsa_pkcs1_encrypt( rsa,
4119 mbedtls_ctr_drbg_random,
4120 &global_data.ctr_drbg,
4121 MBEDTLS_RSA_PUBLIC,
4122 input_length,
4123 input,
4124 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004125 }
4126 else
4127#endif /* MBEDTLS_PKCS1_V15 */
4128#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004129 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004130 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004131 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004132 status = mbedtls_to_psa_error(
4133 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
4134 mbedtls_ctr_drbg_random,
4135 &global_data.ctr_drbg,
4136 MBEDTLS_RSA_PUBLIC,
4137 salt, salt_length,
4138 input_length,
4139 input,
4140 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004141 }
4142 else
4143#endif /* MBEDTLS_PKCS1_V21 */
4144 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004145 status = PSA_ERROR_INVALID_ARGUMENT;
4146 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004147 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004148rsa_exit:
4149 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004150 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004151
Steven Cooremana2371e52020-07-28 14:30:39 +02004152 mbedtls_rsa_free( rsa );
4153 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004154 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004155 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004156 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004157#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004158 {
4159 return( PSA_ERROR_NOT_SUPPORTED );
4160 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004161}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004162
Gilles Peskinec5487a82018-12-03 18:08:14 +01004163psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004164 psa_algorithm_t alg,
4165 const uint8_t *input,
4166 size_t input_length,
4167 const uint8_t *salt,
4168 size_t salt_length,
4169 uint8_t *output,
4170 size_t output_size,
4171 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004172{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004173 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004174 psa_status_t status;
4175
Darryl Green5cc689a2018-07-24 15:34:10 +01004176 (void) input;
4177 (void) input_length;
4178 (void) salt;
4179 (void) output;
4180 (void) output_size;
4181
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004182 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004183
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004184 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4185 return( PSA_ERROR_INVALID_ARGUMENT );
4186
Gilles Peskine28f8f302019-07-24 13:30:31 +02004187 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004188 if( status != PSA_SUCCESS )
4189 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004190 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004191 return( PSA_ERROR_INVALID_ARGUMENT );
4192
4193#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004194 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004195 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004196 mbedtls_rsa_context *rsa = NULL;
4197 status = psa_load_rsa_representation( slot->attr.type,
4198 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004199 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004200 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004201 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004202 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004203
Steven Cooremana2371e52020-07-28 14:30:39 +02004204 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004205 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004206 status = PSA_ERROR_INVALID_ARGUMENT;
4207 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004208 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004209
4210#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004211 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004212 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004213 status = mbedtls_to_psa_error(
4214 mbedtls_rsa_pkcs1_decrypt( rsa,
4215 mbedtls_ctr_drbg_random,
4216 &global_data.ctr_drbg,
4217 MBEDTLS_RSA_PRIVATE,
4218 output_length,
4219 input,
4220 output,
4221 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004222 }
4223 else
4224#endif /* MBEDTLS_PKCS1_V15 */
4225#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004226 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004227 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004228 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004229 status = mbedtls_to_psa_error(
4230 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4231 mbedtls_ctr_drbg_random,
4232 &global_data.ctr_drbg,
4233 MBEDTLS_RSA_PRIVATE,
4234 salt, salt_length,
4235 output_length,
4236 input,
4237 output,
4238 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004239 }
4240 else
4241#endif /* MBEDTLS_PKCS1_V21 */
4242 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004243 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004244 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004245
Steven Cooreman4fed4552020-08-03 14:46:03 +02004246rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004247 mbedtls_rsa_free( rsa );
4248 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004249 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004250 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004251 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004252#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004253 {
4254 return( PSA_ERROR_NOT_SUPPORTED );
4255 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004256}
Gilles Peskine20035e32018-02-03 22:44:14 +01004257
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004258
4259
mohammad1603503973b2018-03-12 15:59:30 +02004260/****************************************************************/
4261/* Symmetric cryptography */
4262/****************************************************************/
4263
Gilles Peskinee553c652018-06-04 16:22:46 +02004264static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004265 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004266 psa_algorithm_t alg,
4267 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004268{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004269 int ret = 0;
4270 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004271 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004272 size_t key_bits;
4273 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004274 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4275 PSA_KEY_USAGE_ENCRYPT :
4276 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004277
Steven Cooremand3feccd2020-09-01 15:56:14 +02004278 /* A context must be freshly initialized before it can be set up. */
4279 if( operation->alg != 0 )
4280 return( PSA_ERROR_BAD_STATE );
4281
Steven Cooremana07b9972020-09-10 14:54:14 +02004282 /* The requested algorithm must be one that can be processed by cipher. */
4283 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004284 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004285
Steven Cooremanef8575e2020-09-11 11:44:50 +02004286 /* Fetch key material from key storage. */
4287 status = psa_get_key_from_slot( handle, &slot, usage, alg );
4288 if( status != PSA_SUCCESS )
4289 goto exit;
4290
4291 /* Initialize the operation struct members, except for alg. The alg member
4292 * is used to indicate to psa_cipher_abort that there are resources to free,
4293 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004294 operation->key_set = 0;
4295 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004296 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004297 operation->iv_size = 0;
4298 operation->block_size = 0;
4299 if( alg == PSA_ALG_ECB_NO_PADDING )
4300 operation->iv_required = 0;
4301 else
4302 operation->iv_required = 1;
4303
Steven Cooremana07b9972020-09-10 14:54:14 +02004304 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004305 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004306 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004307 slot,
4308 alg );
4309 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004310 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004311 slot,
4312 alg );
4313
Steven Cooremanef8575e2020-09-11 11:44:50 +02004314 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004315 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004316 /* Once the driver context is initialised, it needs to be freed using
4317 * psa_cipher_abort. Indicate this through setting alg. */
4318 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004319 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004320
Steven Cooremand3feccd2020-09-01 15:56:14 +02004321 if( status != PSA_ERROR_NOT_SUPPORTED ||
4322 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4323 goto exit;
4324
Steven Cooremana07b9972020-09-10 14:54:14 +02004325 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004326 * available for the given algorithm & key. */
4327 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004328
Steven Cooremana07b9972020-09-10 14:54:14 +02004329 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004330 * psa_cipher_abort. Indicate there is something to be freed through setting
4331 * alg, and indicate the operation is being done using mbedtls crypto through
4332 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004333 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004334 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004335
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004336 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004337 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004338 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004339 {
4340 status = PSA_ERROR_NOT_SUPPORTED;
4341 goto exit;
4342 }
mohammad1603503973b2018-03-12 15:59:30 +02004343
mohammad1603503973b2018-03-12 15:59:30 +02004344 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004345 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004346 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004347
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004348#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004349 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004350 {
4351 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004352 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004353 memcpy( keys, slot->data.key.data, 16 );
4354 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004355 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4356 keys,
4357 192, cipher_operation );
4358 }
4359 else
4360#endif
4361 {
4362 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004363 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004364 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004365 }
Moran Peker41deec42018-04-04 15:43:05 +03004366 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004367 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004368
mohammad16038481e742018-03-18 13:57:31 +02004369#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004370 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004371 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004372 case PSA_ALG_CBC_NO_PADDING:
4373 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4374 MBEDTLS_PADDING_NONE );
4375 break;
4376 case PSA_ALG_CBC_PKCS7:
4377 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4378 MBEDTLS_PADDING_PKCS7 );
4379 break;
4380 default:
4381 /* The algorithm doesn't involve padding. */
4382 ret = 0;
4383 break;
4384 }
4385 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004386 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004387#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4388
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004389 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004390 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004391 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4392 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004393 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004394 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004395 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004396#if defined(MBEDTLS_CHACHA20_C)
4397 else
4398 if( alg == PSA_ALG_CHACHA20 )
4399 operation->iv_size = 12;
4400#endif
mohammad1603503973b2018-03-12 15:59:30 +02004401
Steven Cooreman7df02922020-09-09 15:28:49 +02004402 status = PSA_SUCCESS;
4403
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004404exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004405 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004406 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004407 if( status == PSA_SUCCESS )
4408 {
4409 /* Update operation flags for both driver and software implementations */
4410 operation->key_set = 1;
4411 }
4412 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004413 psa_cipher_abort( operation );
4414 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004415}
4416
Gilles Peskinefe119512018-07-08 21:39:34 +02004417psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004418 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004419 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004420{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004421 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004422}
4423
Gilles Peskinefe119512018-07-08 21:39:34 +02004424psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004425 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004426 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004427{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004428 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004429}
4430
Gilles Peskinefe119512018-07-08 21:39:34 +02004431psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004432 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004433 size_t iv_size,
4434 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004435{
itayzafrir534bd7c2018-08-02 13:56:32 +03004436 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004438 if( operation->iv_set || ! operation->iv_required )
4439 {
4440 return( PSA_ERROR_BAD_STATE );
4441 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004442
Steven Cooremanef8575e2020-09-11 11:44:50 +02004443 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004444 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004445 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004446 iv,
4447 iv_size,
4448 iv_length );
4449 goto exit;
4450 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004451
Moran Peker41deec42018-04-04 15:43:05 +03004452 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004453 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004454 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004455 goto exit;
4456 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004457 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4458 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004459 if( ret != 0 )
4460 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004461 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004462 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004463 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004464
mohammad16038481e742018-03-18 13:57:31 +02004465 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004466 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004467
Moran Peker395db872018-05-31 14:07:14 +03004468exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004469 if( status == PSA_SUCCESS )
4470 operation->iv_set = 1;
4471 else
Moran Peker395db872018-05-31 14:07:14 +03004472 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004473 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004474}
4475
Gilles Peskinefe119512018-07-08 21:39:34 +02004476psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004477 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004478 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004479{
itayzafrir534bd7c2018-08-02 13:56:32 +03004480 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004481 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004482 if( operation->iv_set || ! operation->iv_required )
4483 {
4484 return( PSA_ERROR_BAD_STATE );
4485 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004486
Steven Cooremanef8575e2020-09-11 11:44:50 +02004487 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004488 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004489 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004490 iv,
4491 iv_length );
4492 goto exit;
4493 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004494
Moran Pekera28258c2018-05-29 16:25:04 +03004495 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004496 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004497 status = PSA_ERROR_INVALID_ARGUMENT;
4498 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004499 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004500 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4501 status = mbedtls_to_psa_error( ret );
4502exit:
4503 if( status == PSA_SUCCESS )
4504 operation->iv_set = 1;
4505 else
mohammad1603503973b2018-03-12 15:59:30 +02004506 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004507 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004508}
4509
Steven Cooreman177deba2020-09-07 17:14:14 +02004510/* Process input for which the algorithm is set to ECB mode. This requires
4511 * manual processing, since the PSA API is defined as being able to process
4512 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4513 * underlying mbedtls_cipher_update only takes full blocks. */
4514static psa_status_t psa_cipher_update_ecb_internal(
4515 mbedtls_cipher_context_t *ctx,
4516 const uint8_t *input,
4517 size_t input_length,
4518 uint8_t *output,
4519 size_t output_size,
4520 size_t *output_length )
4521{
4522 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4523 size_t block_size = ctx->cipher_info->block_size;
4524 size_t internal_output_length = 0;
4525 *output_length = 0;
4526
4527 if( input_length == 0 )
4528 {
4529 status = PSA_SUCCESS;
4530 goto exit;
4531 }
4532
4533 if( ctx->unprocessed_len > 0 )
4534 {
4535 /* Fill up to block size, and run the block if there's a full one. */
4536 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4537
4538 if( input_length < bytes_to_copy )
4539 bytes_to_copy = input_length;
4540
4541 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4542 input, bytes_to_copy );
4543 input_length -= bytes_to_copy;
4544 input += bytes_to_copy;
4545 ctx->unprocessed_len += bytes_to_copy;
4546
4547 if( ctx->unprocessed_len == block_size )
4548 {
4549 status = mbedtls_to_psa_error(
4550 mbedtls_cipher_update( ctx,
4551 ctx->unprocessed_data,
4552 block_size,
4553 output, &internal_output_length ) );
4554
4555 if( status != PSA_SUCCESS )
4556 goto exit;
4557
4558 output += internal_output_length;
4559 output_size -= internal_output_length;
4560 *output_length += internal_output_length;
4561 ctx->unprocessed_len = 0;
4562 }
4563 }
4564
4565 while( input_length >= block_size )
4566 {
4567 /* Run all full blocks we have, one by one */
4568 status = mbedtls_to_psa_error(
4569 mbedtls_cipher_update( ctx, input,
4570 block_size,
4571 output, &internal_output_length ) );
4572
4573 if( status != PSA_SUCCESS )
4574 goto exit;
4575
4576 input_length -= block_size;
4577 input += block_size;
4578
4579 output += internal_output_length;
4580 output_size -= internal_output_length;
4581 *output_length += internal_output_length;
4582 }
4583
4584 if( input_length > 0 )
4585 {
4586 /* Save unprocessed bytes for later processing */
4587 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4588 input, input_length );
4589 ctx->unprocessed_len += input_length;
4590 }
4591
4592 status = PSA_SUCCESS;
4593
4594exit:
4595 return( status );
4596}
4597
Gilles Peskinee553c652018-06-04 16:22:46 +02004598psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4599 const uint8_t *input,
4600 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004601 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004602 size_t output_size,
4603 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004604{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004605 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004606 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004607 if( operation->alg == 0 )
4608 {
4609 return( PSA_ERROR_BAD_STATE );
4610 }
4611 if( operation->iv_required && ! operation->iv_set )
4612 {
4613 return( PSA_ERROR_BAD_STATE );
4614 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004615
Steven Cooremanef8575e2020-09-11 11:44:50 +02004616 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004617 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004618 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004619 input,
4620 input_length,
4621 output,
4622 output_size,
4623 output_length );
4624 goto exit;
4625 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004626
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004627 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004628 {
4629 /* Take the unprocessed partial block left over from previous
4630 * update calls, if any, plus the input to this call. Remove
4631 * the last partial block, if any. You get the data that will be
4632 * output in this call. */
4633 expected_output_size =
4634 ( operation->ctx.cipher.unprocessed_len + input_length )
4635 / operation->block_size * operation->block_size;
4636 }
4637 else
4638 {
4639 expected_output_size = input_length;
4640 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004641
Gilles Peskine89d789c2018-06-04 17:17:16 +02004642 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004643 {
4644 status = PSA_ERROR_BUFFER_TOO_SMALL;
4645 goto exit;
4646 }
mohammad160382759612018-03-12 18:16:40 +02004647
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004648 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4649 {
4650 /* mbedtls_cipher_update has an API inconsistency: it will only
4651 * process a single block at a time in ECB mode. Abstract away that
4652 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004653 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4654 input,
4655 input_length,
4656 output,
4657 output_size,
4658 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004659 }
4660 else
4661 {
4662 status = mbedtls_to_psa_error(
4663 mbedtls_cipher_update( &operation->ctx.cipher, input,
4664 input_length, output, output_length ) );
4665 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004666exit:
4667 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004668 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004669 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004670}
4671
Gilles Peskinee553c652018-06-04 16:22:46 +02004672psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4673 uint8_t *output,
4674 size_t output_size,
4675 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004676{
David Saadab4ecc272019-02-14 13:48:10 +02004677 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004678 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004679 if( operation->alg == 0 )
4680 {
4681 return( PSA_ERROR_BAD_STATE );
4682 }
4683 if( operation->iv_required && ! operation->iv_set )
4684 {
4685 return( PSA_ERROR_BAD_STATE );
4686 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004687
Steven Cooremanef8575e2020-09-11 11:44:50 +02004688 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004689 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004690 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004691 output,
4692 output_size,
4693 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004694 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004695 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004696
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004697 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004698 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004699 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004700 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004701 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004702 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004703 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004704 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004705 }
4706
Steven Cooremanef8575e2020-09-11 11:44:50 +02004707 status = mbedtls_to_psa_error(
4708 mbedtls_cipher_finish( &operation->ctx.cipher,
4709 temp_output_buffer,
4710 output_length ) );
4711 if( status != PSA_SUCCESS )
4712 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004713
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004714 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004715 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004716 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004717 memcpy( output, temp_output_buffer, *output_length );
4718 else
Janos Follath315b51c2018-07-09 16:04:51 +01004719 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004720
Steven Cooremanef8575e2020-09-11 11:44:50 +02004721exit:
4722 if( operation->mbedtls_in_use == 1 )
4723 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004724
Steven Cooremanef8575e2020-09-11 11:44:50 +02004725 if( status == PSA_SUCCESS )
4726 return( psa_cipher_abort( operation ) );
4727 else
4728 {
4729 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004730 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004731
Steven Cooremanef8575e2020-09-11 11:44:50 +02004732 return( status );
4733 }
mohammad1603503973b2018-03-12 15:59:30 +02004734}
4735
Gilles Peskinee553c652018-06-04 16:22:46 +02004736psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4737{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004738 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004739 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004740 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004741 * in use. It's ok to call abort on such an object, and there's
4742 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004743 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004744 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004745
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004746 /* Sanity check (shouldn't happen: operation->alg should
4747 * always have been initialized to a valid value). */
4748 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4749 return( PSA_ERROR_BAD_STATE );
4750
Steven Cooremanef8575e2020-09-11 11:44:50 +02004751 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004752 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004753 else
4754 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004755
Moran Peker41deec42018-04-04 15:43:05 +03004756 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004757 operation->key_set = 0;
4758 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004759 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004760 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004761 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004762 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004763
Moran Peker395db872018-05-31 14:07:14 +03004764 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004765}
4766
Gilles Peskinea0655c32018-04-30 17:06:50 +02004767
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004768
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004769
mohammad16035955c982018-04-26 00:53:03 +03004770/****************************************************************/
4771/* AEAD */
4772/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004773
Gilles Peskineedf9a652018-08-17 18:11:56 +02004774typedef struct
4775{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004776 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004777 const mbedtls_cipher_info_t *cipher_info;
4778 union
4779 {
4780#if defined(MBEDTLS_CCM_C)
4781 mbedtls_ccm_context ccm;
4782#endif /* MBEDTLS_CCM_C */
4783#if defined(MBEDTLS_GCM_C)
4784 mbedtls_gcm_context gcm;
4785#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004786#if defined(MBEDTLS_CHACHAPOLY_C)
4787 mbedtls_chachapoly_context chachapoly;
4788#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004789 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004790 psa_algorithm_t core_alg;
4791 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004792 uint8_t tag_length;
4793} aead_operation_t;
4794
Gilles Peskine30a9e412019-01-14 18:36:12 +01004795static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004796{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004797 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004798 {
4799#if defined(MBEDTLS_CCM_C)
4800 case PSA_ALG_CCM:
4801 mbedtls_ccm_free( &operation->ctx.ccm );
4802 break;
4803#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004804#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004805 case PSA_ALG_GCM:
4806 mbedtls_gcm_free( &operation->ctx.gcm );
4807 break;
4808#endif /* MBEDTLS_GCM_C */
4809 }
4810}
4811
4812static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004813 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004814 psa_key_usage_t usage,
4815 psa_algorithm_t alg )
4816{
4817 psa_status_t status;
4818 size_t key_bits;
4819 mbedtls_cipher_id_t cipher_id;
4820
Gilles Peskine28f8f302019-07-24 13:30:31 +02004821 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004822 if( status != PSA_SUCCESS )
4823 return( status );
4824
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004825 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004826
4827 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004828 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004829 &cipher_id );
4830 if( operation->cipher_info == NULL )
4831 return( PSA_ERROR_NOT_SUPPORTED );
4832
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004833 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004834 {
4835#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004836 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4837 operation->core_alg = PSA_ALG_CCM;
4838 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004839 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4840 * The call to mbedtls_ccm_encrypt_and_tag or
4841 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004842 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004843 return( PSA_ERROR_INVALID_ARGUMENT );
4844 mbedtls_ccm_init( &operation->ctx.ccm );
4845 status = mbedtls_to_psa_error(
4846 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004847 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004848 (unsigned int) key_bits ) );
4849 if( status != 0 )
4850 goto cleanup;
4851 break;
4852#endif /* MBEDTLS_CCM_C */
4853
4854#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004855 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4856 operation->core_alg = PSA_ALG_GCM;
4857 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004858 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4859 * The call to mbedtls_gcm_crypt_and_tag or
4860 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004861 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004862 return( PSA_ERROR_INVALID_ARGUMENT );
4863 mbedtls_gcm_init( &operation->ctx.gcm );
4864 status = mbedtls_to_psa_error(
4865 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004866 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004867 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004868 if( status != 0 )
4869 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004870 break;
4871#endif /* MBEDTLS_GCM_C */
4872
Gilles Peskine26869f22019-05-06 15:25:00 +02004873#if defined(MBEDTLS_CHACHAPOLY_C)
4874 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4875 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4876 operation->full_tag_length = 16;
4877 /* We only support the default tag length. */
4878 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4879 return( PSA_ERROR_NOT_SUPPORTED );
4880 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4881 status = mbedtls_to_psa_error(
4882 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004883 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004884 if( status != 0 )
4885 goto cleanup;
4886 break;
4887#endif /* MBEDTLS_CHACHAPOLY_C */
4888
Gilles Peskineedf9a652018-08-17 18:11:56 +02004889 default:
4890 return( PSA_ERROR_NOT_SUPPORTED );
4891 }
4892
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004893 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4894 {
4895 status = PSA_ERROR_INVALID_ARGUMENT;
4896 goto cleanup;
4897 }
4898 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004899
Gilles Peskineedf9a652018-08-17 18:11:56 +02004900 return( PSA_SUCCESS );
4901
4902cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004903 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004904 return( status );
4905}
4906
Gilles Peskinec5487a82018-12-03 18:08:14 +01004907psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004908 psa_algorithm_t alg,
4909 const uint8_t *nonce,
4910 size_t nonce_length,
4911 const uint8_t *additional_data,
4912 size_t additional_data_length,
4913 const uint8_t *plaintext,
4914 size_t plaintext_length,
4915 uint8_t *ciphertext,
4916 size_t ciphertext_size,
4917 size_t *ciphertext_length )
4918{
mohammad16035955c982018-04-26 00:53:03 +03004919 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004920 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004921 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004922
mohammad1603f08a5502018-06-03 15:05:47 +03004923 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004924
Gilles Peskinec5487a82018-12-03 18:08:14 +01004925 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004926 if( status != PSA_SUCCESS )
4927 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004928
Gilles Peskineedf9a652018-08-17 18:11:56 +02004929 /* For all currently supported modes, the tag is at the end of the
4930 * ciphertext. */
4931 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4932 {
4933 status = PSA_ERROR_BUFFER_TOO_SMALL;
4934 goto exit;
4935 }
4936 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004937
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004938#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004939 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004940 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004941 status = mbedtls_to_psa_error(
4942 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4943 MBEDTLS_GCM_ENCRYPT,
4944 plaintext_length,
4945 nonce, nonce_length,
4946 additional_data, additional_data_length,
4947 plaintext, ciphertext,
4948 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004949 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004950 else
4951#endif /* MBEDTLS_GCM_C */
4952#if defined(MBEDTLS_CCM_C)
4953 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004954 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004955 status = mbedtls_to_psa_error(
4956 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4957 plaintext_length,
4958 nonce, nonce_length,
4959 additional_data,
4960 additional_data_length,
4961 plaintext, ciphertext,
4962 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004963 }
mohammad16035c8845f2018-05-09 05:40:09 -07004964 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004965#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004966#if defined(MBEDTLS_CHACHAPOLY_C)
4967 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4968 {
4969 if( nonce_length != 12 || operation.tag_length != 16 )
4970 {
4971 status = PSA_ERROR_NOT_SUPPORTED;
4972 goto exit;
4973 }
4974 status = mbedtls_to_psa_error(
4975 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4976 plaintext_length,
4977 nonce,
4978 additional_data,
4979 additional_data_length,
4980 plaintext,
4981 ciphertext,
4982 tag ) );
4983 }
4984 else
4985#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004986 {
mohammad1603554faad2018-06-03 15:07:38 +03004987 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004988 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004989
Gilles Peskineedf9a652018-08-17 18:11:56 +02004990 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4991 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004992
Gilles Peskineedf9a652018-08-17 18:11:56 +02004993exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004994 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004995 if( status == PSA_SUCCESS )
4996 *ciphertext_length = plaintext_length + operation.tag_length;
4997 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004998}
4999
Gilles Peskineee652a32018-06-01 19:23:52 +02005000/* Locate the tag in a ciphertext buffer containing the encrypted data
5001 * followed by the tag. Return the length of the part preceding the tag in
5002 * *plaintext_length. This is the size of the plaintext in modes where
5003 * the encrypted data has the same size as the plaintext, such as
5004 * CCM and GCM. */
5005static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5006 const uint8_t *ciphertext,
5007 size_t ciphertext_length,
5008 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005009 const uint8_t **p_tag )
5010{
5011 size_t payload_length;
5012 if( tag_length > ciphertext_length )
5013 return( PSA_ERROR_INVALID_ARGUMENT );
5014 payload_length = ciphertext_length - tag_length;
5015 if( payload_length > plaintext_size )
5016 return( PSA_ERROR_BUFFER_TOO_SMALL );
5017 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005018 return( PSA_SUCCESS );
5019}
5020
Gilles Peskinec5487a82018-12-03 18:08:14 +01005021psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03005022 psa_algorithm_t alg,
5023 const uint8_t *nonce,
5024 size_t nonce_length,
5025 const uint8_t *additional_data,
5026 size_t additional_data_length,
5027 const uint8_t *ciphertext,
5028 size_t ciphertext_length,
5029 uint8_t *plaintext,
5030 size_t plaintext_size,
5031 size_t *plaintext_length )
5032{
mohammad16035955c982018-04-26 00:53:03 +03005033 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005034 aead_operation_t operation;
5035 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005036
Gilles Peskineee652a32018-06-01 19:23:52 +02005037 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005038
Gilles Peskinec5487a82018-12-03 18:08:14 +01005039 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005040 if( status != PSA_SUCCESS )
5041 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005042
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005043 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5044 ciphertext, ciphertext_length,
5045 plaintext_size, &tag );
5046 if( status != PSA_SUCCESS )
5047 goto exit;
5048
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005049#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005050 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005051 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005052 status = mbedtls_to_psa_error(
5053 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5054 ciphertext_length - operation.tag_length,
5055 nonce, nonce_length,
5056 additional_data,
5057 additional_data_length,
5058 tag, operation.tag_length,
5059 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005060 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005061 else
5062#endif /* MBEDTLS_GCM_C */
5063#if defined(MBEDTLS_CCM_C)
5064 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005065 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005066 status = mbedtls_to_psa_error(
5067 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5068 ciphertext_length - operation.tag_length,
5069 nonce, nonce_length,
5070 additional_data,
5071 additional_data_length,
5072 ciphertext, plaintext,
5073 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005074 }
mohammad160339574652018-06-01 04:39:53 -07005075 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005076#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005077#if defined(MBEDTLS_CHACHAPOLY_C)
5078 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5079 {
5080 if( nonce_length != 12 || operation.tag_length != 16 )
5081 {
5082 status = PSA_ERROR_NOT_SUPPORTED;
5083 goto exit;
5084 }
5085 status = mbedtls_to_psa_error(
5086 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5087 ciphertext_length - operation.tag_length,
5088 nonce,
5089 additional_data,
5090 additional_data_length,
5091 tag,
5092 ciphertext,
5093 plaintext ) );
5094 }
5095 else
5096#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005097 {
mohammad1603554faad2018-06-03 15:07:38 +03005098 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005099 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005100
Gilles Peskineedf9a652018-08-17 18:11:56 +02005101 if( status != PSA_SUCCESS && plaintext_size != 0 )
5102 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005103
Gilles Peskineedf9a652018-08-17 18:11:56 +02005104exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005105 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005106 if( status == PSA_SUCCESS )
5107 *plaintext_length = ciphertext_length - operation.tag_length;
5108 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005109}
5110
Gilles Peskinea0655c32018-04-30 17:06:50 +02005111
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005112
Gilles Peskine20035e32018-02-03 22:44:14 +01005113/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005114/* Generators */
5115/****************************************************************/
5116
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005117#define HKDF_STATE_INIT 0 /* no input yet */
5118#define HKDF_STATE_STARTED 1 /* got salt */
5119#define HKDF_STATE_KEYED 2 /* got key */
5120#define HKDF_STATE_OUTPUT 3 /* output started */
5121
Gilles Peskinecbe66502019-05-16 16:59:18 +02005122static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005123 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005124{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005125 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5126 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005127 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005129}
5130
5131
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005132psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005133{
5134 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005135 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005136 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005137 {
5138 /* The object has (apparently) been initialized but it is not
5139 * in use. It's ok to call abort on such an object, and there's
5140 * nothing to do. */
5141 }
5142 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02005143#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005144 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005145 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005146 mbedtls_free( operation->ctx.hkdf.info );
5147 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005148 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005149 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005150 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005151 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005152 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005153 if( operation->ctx.tls12_prf.seed != NULL )
5154 {
5155 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5156 operation->ctx.tls12_prf.seed_length );
5157 mbedtls_free( operation->ctx.tls12_prf.seed );
5158 }
5159
5160 if( operation->ctx.tls12_prf.label != NULL )
5161 {
5162 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5163 operation->ctx.tls12_prf.label_length );
5164 mbedtls_free( operation->ctx.tls12_prf.label );
5165 }
5166
5167 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5168
5169 /* We leave the fields Ai and output_block to be erased safely by the
5170 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005171 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005172 else
5173#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005174 {
5175 status = PSA_ERROR_BAD_STATE;
5176 }
Janos Follath083036a2019-06-11 10:22:26 +01005177 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005178 return( status );
5179}
5180
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005181psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005182 size_t *capacity)
5183{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005184 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005185 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005186 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005187 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005188 }
5189
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005190 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005191 return( PSA_SUCCESS );
5192}
5193
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005194psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005195 size_t capacity )
5196{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005197 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005198 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005199 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005200 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005201 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005202 return( PSA_SUCCESS );
5203}
5204
Gilles Peskinebef7f142018-07-12 17:22:21 +02005205#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005206/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005207 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005208static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005209 psa_algorithm_t hash_alg,
5210 uint8_t *output,
5211 size_t output_length )
5212{
5213 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5214 psa_status_t status;
5215
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005216 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5217 return( PSA_ERROR_BAD_STATE );
5218 hkdf->state = HKDF_STATE_OUTPUT;
5219
Gilles Peskinebef7f142018-07-12 17:22:21 +02005220 while( output_length != 0 )
5221 {
5222 /* Copy what remains of the current block */
5223 uint8_t n = hash_length - hkdf->offset_in_block;
5224 if( n > output_length )
5225 n = (uint8_t) output_length;
5226 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5227 output += n;
5228 output_length -= n;
5229 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005230 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005231 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005232 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005233 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005234 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005235 * object was corrupted or if this function is called directly
5236 * inside the library. */
5237 if( hkdf->block_number == 0xff )
5238 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005239
5240 /* We need a new block */
5241 ++hkdf->block_number;
5242 hkdf->offset_in_block = 0;
5243 status = psa_hmac_setup_internal( &hkdf->hmac,
5244 hkdf->prk, hash_length,
5245 hash_alg );
5246 if( status != PSA_SUCCESS )
5247 return( status );
5248 if( hkdf->block_number != 1 )
5249 {
5250 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5251 hkdf->output_block,
5252 hash_length );
5253 if( status != PSA_SUCCESS )
5254 return( status );
5255 }
5256 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5257 hkdf->info,
5258 hkdf->info_length );
5259 if( status != PSA_SUCCESS )
5260 return( status );
5261 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5262 &hkdf->block_number, 1 );
5263 if( status != PSA_SUCCESS )
5264 return( status );
5265 status = psa_hmac_finish_internal( &hkdf->hmac,
5266 hkdf->output_block,
5267 sizeof( hkdf->output_block ) );
5268 if( status != PSA_SUCCESS )
5269 return( status );
5270 }
5271
5272 return( PSA_SUCCESS );
5273}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005274
Janos Follath7742fee2019-06-17 12:58:10 +01005275static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5276 psa_tls12_prf_key_derivation_t *tls12_prf,
5277 psa_algorithm_t alg )
5278{
5279 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5280 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005281 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5282 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005283
Janos Follath7742fee2019-06-17 12:58:10 +01005284 /* We can't be wanting more output after block 0xff, otherwise
5285 * the capacity check in psa_key_derivation_output_bytes() would have
5286 * prevented this call. It could happen only if the operation
5287 * object was corrupted or if this function is called directly
5288 * inside the library. */
5289 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005290 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005291
5292 /* We need a new block */
5293 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005294 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005295
5296 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5297 *
5298 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5299 *
5300 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5301 * HMAC_hash(secret, A(2) + seed) +
5302 * HMAC_hash(secret, A(3) + seed) + ...
5303 *
5304 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005305 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005306 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005307 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005308 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005309 * is currently extracted as `output_block` and where i is
5310 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005311 */
5312
Janos Follathea29bfb2019-06-19 12:21:20 +01005313 /* Save the hash context before using it, to preserve the hash state with
5314 * only the inner padding in it. We need this, because inner padding depends
5315 * on the key (secret in the RFC's terminology). */
5316 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5317 if( status != PSA_SUCCESS )
5318 goto cleanup;
5319
5320 /* Calculate A(i) where i = tls12_prf->block_number. */
5321 if( tls12_prf->block_number == 1 )
5322 {
5323 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5324 * the variable seed and in this instance means it in the context of the
5325 * P_hash function, where seed = label + seed.) */
5326 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5327 tls12_prf->label, tls12_prf->label_length );
5328 if( status != PSA_SUCCESS )
5329 goto cleanup;
5330 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5331 tls12_prf->seed, tls12_prf->seed_length );
5332 if( status != PSA_SUCCESS )
5333 goto cleanup;
5334 }
5335 else
5336 {
5337 /* A(i) = HMAC_hash(secret, A(i-1)) */
5338 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5339 tls12_prf->Ai, hash_length );
5340 if( status != PSA_SUCCESS )
5341 goto cleanup;
5342 }
5343
5344 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5345 tls12_prf->Ai, hash_length );
5346 if( status != PSA_SUCCESS )
5347 goto cleanup;
5348 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5349 if( status != PSA_SUCCESS )
5350 goto cleanup;
5351
5352 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5353 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5354 tls12_prf->Ai, hash_length );
5355 if( status != PSA_SUCCESS )
5356 goto cleanup;
5357 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5358 tls12_prf->label, tls12_prf->label_length );
5359 if( status != PSA_SUCCESS )
5360 goto cleanup;
5361 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5362 tls12_prf->seed, tls12_prf->seed_length );
5363 if( status != PSA_SUCCESS )
5364 goto cleanup;
5365 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5366 tls12_prf->output_block, hash_length );
5367 if( status != PSA_SUCCESS )
5368 goto cleanup;
5369 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5370 if( status != PSA_SUCCESS )
5371 goto cleanup;
5372
Janos Follath7742fee2019-06-17 12:58:10 +01005373
5374cleanup:
5375
Janos Follathea29bfb2019-06-19 12:21:20 +01005376 cleanup_status = psa_hash_abort( &backup );
5377 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5378 status = cleanup_status;
5379
5380 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005381}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005382
Janos Follath844eb0e2019-06-19 12:10:49 +01005383static psa_status_t psa_key_derivation_tls12_prf_read(
5384 psa_tls12_prf_key_derivation_t *tls12_prf,
5385 psa_algorithm_t alg,
5386 uint8_t *output,
5387 size_t output_length )
5388{
5389 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5390 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5391 psa_status_t status;
5392 uint8_t offset, length;
5393
5394 while( output_length != 0 )
5395 {
5396 /* Check if we have fully processed the current block. */
5397 if( tls12_prf->left_in_block == 0 )
5398 {
5399 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5400 alg );
5401 if( status != PSA_SUCCESS )
5402 return( status );
5403
5404 continue;
5405 }
5406
5407 if( tls12_prf->left_in_block > output_length )
5408 length = (uint8_t) output_length;
5409 else
5410 length = tls12_prf->left_in_block;
5411
5412 offset = hash_length - tls12_prf->left_in_block;
5413 memcpy( output, tls12_prf->output_block + offset, length );
5414 output += length;
5415 output_length -= length;
5416 tls12_prf->left_in_block -= length;
5417 }
5418
5419 return( PSA_SUCCESS );
5420}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005421#endif /* MBEDTLS_MD_C */
5422
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005423psa_status_t psa_key_derivation_output_bytes(
5424 psa_key_derivation_operation_t *operation,
5425 uint8_t *output,
5426 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005427{
5428 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005429 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005430
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005431 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005432 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005433 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005434 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005435 }
5436
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005437 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005438 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005439 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005440 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005441 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005442 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005443 goto exit;
5444 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005445 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005446 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005447 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005448 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005449 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5450 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005451 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005452 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005453 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005454 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005455 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005456
Gilles Peskinebef7f142018-07-12 17:22:21 +02005457#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005458 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005459 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005460 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005461 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005462 output, output_length );
5463 }
Janos Follathadbec812019-06-14 11:05:39 +01005464 else
Janos Follathadbec812019-06-14 11:05:39 +01005465 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005466 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005467 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005468 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005469 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005470 output_length );
5471 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005472 else
5473#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005474 {
5475 return( PSA_ERROR_BAD_STATE );
5476 }
5477
5478exit:
5479 if( status != PSA_SUCCESS )
5480 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005481 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005482 * This allows us to differentiate between exhausted operations and
5483 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5484 * operations. */
5485 psa_algorithm_t alg = operation->alg;
5486 psa_key_derivation_abort( operation );
5487 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005488 memset( output, '!', output_length );
5489 }
5490 return( status );
5491}
5492
Gilles Peskine08542d82018-07-19 17:05:42 +02005493#if defined(MBEDTLS_DES_C)
5494static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5495{
5496 if( data_size >= 8 )
5497 mbedtls_des_key_set_parity( data );
5498 if( data_size >= 16 )
5499 mbedtls_des_key_set_parity( data + 8 );
5500 if( data_size >= 24 )
5501 mbedtls_des_key_set_parity( data + 16 );
5502}
5503#endif /* MBEDTLS_DES_C */
5504
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005505static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005506 psa_key_slot_t *slot,
5507 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005508 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005509{
5510 uint8_t *data = NULL;
5511 size_t bytes = PSA_BITS_TO_BYTES( bits );
5512 psa_status_t status;
5513
Gilles Peskine8e338702019-07-30 20:06:31 +02005514 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005515 return( PSA_ERROR_INVALID_ARGUMENT );
5516 if( bits % 8 != 0 )
5517 return( PSA_ERROR_INVALID_ARGUMENT );
5518 data = mbedtls_calloc( 1, bytes );
5519 if( data == NULL )
5520 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5521
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005522 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005523 if( status != PSA_SUCCESS )
5524 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005525#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005526 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005527 psa_des_set_key_parity( data, bytes );
5528#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005529 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005530
5531exit:
5532 mbedtls_free( data );
5533 return( status );
5534}
5535
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005536psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005537 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005538 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005539{
5540 psa_status_t status;
5541 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005542 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005543
5544 /* Reject any attempt to create a zero-length key so that we don't
5545 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5546 if( psa_get_key_bits( attributes ) == 0 )
5547 return( PSA_ERROR_INVALID_ARGUMENT );
5548
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005549 if( ! operation->can_output_key )
5550 return( PSA_ERROR_NOT_PERMITTED );
5551
Gilles Peskinedf179142019-07-15 22:02:14 +02005552 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5553 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005554#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5555 if( driver != NULL )
5556 {
5557 /* Deriving a key in a secure element is not implemented yet. */
5558 status = PSA_ERROR_NOT_SUPPORTED;
5559 }
5560#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005561 if( status == PSA_SUCCESS )
5562 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005563 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005564 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005565 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005566 }
5567 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005568 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005569 if( status != PSA_SUCCESS )
5570 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005571 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005572 *handle = 0;
5573 }
5574 return( status );
5575}
5576
Gilles Peskineeab56e42018-07-12 17:12:33 +02005577
5578
5579/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005580/* Key derivation */
5581/****************************************************************/
5582
Gilles Peskine969c5d62019-01-16 15:53:06 +01005583static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005584 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005585 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005586{
Janos Follath5fe19732019-06-20 15:09:30 +01005587 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5588 * initialisers for this union leave some bytes unspecified.) */
5589 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5590
Gilles Peskine969c5d62019-01-16 15:53:06 +01005591 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005592#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005593 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5594 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5595 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005596 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005597 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005598 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5599 if( hash_size == 0 )
5600 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005601 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5602 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005603 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005604 {
5605 return( PSA_ERROR_NOT_SUPPORTED );
5606 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005607 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005608 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005609 }
5610#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005611 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005612 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005613}
5614
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005615psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005616 psa_algorithm_t alg )
5617{
5618 psa_status_t status;
5619
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005620 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005621 return( PSA_ERROR_BAD_STATE );
5622
Gilles Peskine6843c292019-01-18 16:44:49 +01005623 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5624 return( PSA_ERROR_INVALID_ARGUMENT );
5625 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005626 {
5627 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005628 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005629 }
5630 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5631 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005632 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005633 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005634 else
5635 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005636
5637 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005638 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005639 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005640}
5641
5642#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005643static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005644 psa_algorithm_t hash_alg,
5645 psa_key_derivation_step_t step,
5646 const uint8_t *data,
5647 size_t data_length )
5648{
5649 psa_status_t status;
5650 switch( step )
5651 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005652 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005653 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005654 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005655 status = psa_hmac_setup_internal( &hkdf->hmac,
5656 data, data_length,
5657 hash_alg );
5658 if( status != PSA_SUCCESS )
5659 return( status );
5660 hkdf->state = HKDF_STATE_STARTED;
5661 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005662 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005663 /* If no salt was provided, use an empty salt. */
5664 if( hkdf->state == HKDF_STATE_INIT )
5665 {
5666 status = psa_hmac_setup_internal( &hkdf->hmac,
5667 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005668 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005669 if( status != PSA_SUCCESS )
5670 return( status );
5671 hkdf->state = HKDF_STATE_STARTED;
5672 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005673 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005674 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005675 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5676 data, data_length );
5677 if( status != PSA_SUCCESS )
5678 return( status );
5679 status = psa_hmac_finish_internal( &hkdf->hmac,
5680 hkdf->prk,
5681 sizeof( hkdf->prk ) );
5682 if( status != PSA_SUCCESS )
5683 return( status );
5684 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5685 hkdf->block_number = 0;
5686 hkdf->state = HKDF_STATE_KEYED;
5687 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005688 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005689 if( hkdf->state == HKDF_STATE_OUTPUT )
5690 return( PSA_ERROR_BAD_STATE );
5691 if( hkdf->info_set )
5692 return( PSA_ERROR_BAD_STATE );
5693 hkdf->info_length = data_length;
5694 if( data_length != 0 )
5695 {
5696 hkdf->info = mbedtls_calloc( 1, data_length );
5697 if( hkdf->info == NULL )
5698 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5699 memcpy( hkdf->info, data, data_length );
5700 }
5701 hkdf->info_set = 1;
5702 return( PSA_SUCCESS );
5703 default:
5704 return( PSA_ERROR_INVALID_ARGUMENT );
5705 }
5706}
Janos Follathb03233e2019-06-11 15:30:30 +01005707
Janos Follathf08e2652019-06-13 09:05:41 +01005708static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5709 const uint8_t *data,
5710 size_t data_length )
5711{
5712 if( prf->state != TLS12_PRF_STATE_INIT )
5713 return( PSA_ERROR_BAD_STATE );
5714
Janos Follathd6dce9f2019-07-04 09:11:38 +01005715 if( data_length != 0 )
5716 {
5717 prf->seed = mbedtls_calloc( 1, data_length );
5718 if( prf->seed == NULL )
5719 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005720
Janos Follathd6dce9f2019-07-04 09:11:38 +01005721 memcpy( prf->seed, data, data_length );
5722 prf->seed_length = data_length;
5723 }
Janos Follathf08e2652019-06-13 09:05:41 +01005724
5725 prf->state = TLS12_PRF_STATE_SEED_SET;
5726
5727 return( PSA_SUCCESS );
5728}
5729
Janos Follath81550542019-06-13 14:26:34 +01005730static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5731 psa_algorithm_t hash_alg,
5732 const uint8_t *data,
5733 size_t data_length )
5734{
5735 psa_status_t status;
5736 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5737 return( PSA_ERROR_BAD_STATE );
5738
5739 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5740 if( status != PSA_SUCCESS )
5741 return( status );
5742
5743 prf->state = TLS12_PRF_STATE_KEY_SET;
5744
5745 return( PSA_SUCCESS );
5746}
5747
Janos Follath6660f0e2019-06-17 08:44:03 +01005748static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5749 psa_tls12_prf_key_derivation_t *prf,
5750 psa_algorithm_t hash_alg,
5751 const uint8_t *data,
5752 size_t data_length )
5753{
5754 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005755 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5756 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005757
5758 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5759 return( PSA_ERROR_INVALID_ARGUMENT );
5760
5761 /* Quoting RFC 4279, Section 2:
5762 *
5763 * The premaster secret is formed as follows: if the PSK is N octets
5764 * long, concatenate a uint16 with the value N, N zero octets, a second
5765 * uint16 with the value N, and the PSK itself.
5766 */
5767
Janos Follath40e13932019-06-26 13:22:29 +01005768 *cur++ = ( data_length >> 8 ) & 0xff;
5769 *cur++ = ( data_length >> 0 ) & 0xff;
5770 memset( cur, 0, data_length );
5771 cur += data_length;
5772 *cur++ = pms[0];
5773 *cur++ = pms[1];
5774 memcpy( cur, data, data_length );
5775 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005776
Janos Follath40e13932019-06-26 13:22:29 +01005777 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005778
5779 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5780 return( status );
5781}
5782
Janos Follath63028dd2019-06-13 09:15:47 +01005783static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5784 const uint8_t *data,
5785 size_t data_length )
5786{
5787 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5788 return( PSA_ERROR_BAD_STATE );
5789
Janos Follathd6dce9f2019-07-04 09:11:38 +01005790 if( data_length != 0 )
5791 {
5792 prf->label = mbedtls_calloc( 1, data_length );
5793 if( prf->label == NULL )
5794 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005795
Janos Follathd6dce9f2019-07-04 09:11:38 +01005796 memcpy( prf->label, data, data_length );
5797 prf->label_length = data_length;
5798 }
Janos Follath63028dd2019-06-13 09:15:47 +01005799
5800 prf->state = TLS12_PRF_STATE_LABEL_SET;
5801
5802 return( PSA_SUCCESS );
5803}
5804
Janos Follathb03233e2019-06-11 15:30:30 +01005805static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5806 psa_algorithm_t hash_alg,
5807 psa_key_derivation_step_t step,
5808 const uint8_t *data,
5809 size_t data_length )
5810{
Janos Follathb03233e2019-06-11 15:30:30 +01005811 switch( step )
5812 {
Janos Follathf08e2652019-06-13 09:05:41 +01005813 case PSA_KEY_DERIVATION_INPUT_SEED:
5814 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005815 case PSA_KEY_DERIVATION_INPUT_SECRET:
5816 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005817 case PSA_KEY_DERIVATION_INPUT_LABEL:
5818 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005819 default:
5820 return( PSA_ERROR_INVALID_ARGUMENT );
5821 }
5822}
Janos Follath6660f0e2019-06-17 08:44:03 +01005823
5824static psa_status_t psa_tls12_prf_psk_to_ms_input(
5825 psa_tls12_prf_key_derivation_t *prf,
5826 psa_algorithm_t hash_alg,
5827 psa_key_derivation_step_t step,
5828 const uint8_t *data,
5829 size_t data_length )
5830{
5831 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005832 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005833 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5834 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005835 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005836
5837 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5838}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005839#endif /* MBEDTLS_MD_C */
5840
Gilles Peskineb8965192019-09-24 16:21:10 +02005841/** Check whether the given key type is acceptable for the given
5842 * input step of a key derivation.
5843 *
5844 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5845 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5846 * Both secret and non-secret inputs can alternatively have the type
5847 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5848 * that the input was passed as a buffer rather than via a key object.
5849 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005850static int psa_key_derivation_check_input_type(
5851 psa_key_derivation_step_t step,
5852 psa_key_type_t key_type )
5853{
5854 switch( step )
5855 {
5856 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005857 if( key_type == PSA_KEY_TYPE_DERIVE )
5858 return( PSA_SUCCESS );
5859 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005860 return( PSA_SUCCESS );
5861 break;
5862 case PSA_KEY_DERIVATION_INPUT_LABEL:
5863 case PSA_KEY_DERIVATION_INPUT_SALT:
5864 case PSA_KEY_DERIVATION_INPUT_INFO:
5865 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005866 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5867 return( PSA_SUCCESS );
5868 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005869 return( PSA_SUCCESS );
5870 break;
5871 }
5872 return( PSA_ERROR_INVALID_ARGUMENT );
5873}
5874
Janos Follathb80a94e2019-06-12 15:54:46 +01005875static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005876 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005877 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005878 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005879 const uint8_t *data,
5880 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005881{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005882 psa_status_t status;
5883 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5884
5885 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005886 if( status != PSA_SUCCESS )
5887 goto exit;
5888
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005889#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005890 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005891 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005892 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005893 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005894 step, data, data_length );
5895 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005896 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005897 {
Janos Follathb03233e2019-06-11 15:30:30 +01005898 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5899 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5900 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005901 }
5902 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5903 {
5904 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5905 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5906 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005907 }
5908 else
5909#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005910 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005911 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005912 return( PSA_ERROR_BAD_STATE );
5913 }
5914
Gilles Peskine224b0d62019-09-23 18:13:17 +02005915exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005916 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005917 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005918 return( status );
5919}
5920
Janos Follath51f4a0f2019-06-14 11:35:55 +01005921psa_status_t psa_key_derivation_input_bytes(
5922 psa_key_derivation_operation_t *operation,
5923 psa_key_derivation_step_t step,
5924 const uint8_t *data,
5925 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005926{
Gilles Peskineb8965192019-09-24 16:21:10 +02005927 return( psa_key_derivation_input_internal( operation, step,
5928 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005929 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005930}
5931
Janos Follath51f4a0f2019-06-14 11:35:55 +01005932psa_status_t psa_key_derivation_input_key(
5933 psa_key_derivation_operation_t *operation,
5934 psa_key_derivation_step_t step,
5935 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005936{
5937 psa_key_slot_t *slot;
5938 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005939
Gilles Peskine28f8f302019-07-24 13:30:31 +02005940 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005941 PSA_KEY_USAGE_DERIVE,
5942 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005943 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005944 {
5945 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005946 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005947 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005948
5949 /* Passing a key object as a SECRET input unlocks the permission
5950 * to output to a key object. */
5951 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5952 operation->can_output_key = 1;
5953
Janos Follathb80a94e2019-06-12 15:54:46 +01005954 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005955 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005956 slot->data.key.data,
5957 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005958}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005959
5960
5961
5962/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005963/* Key agreement */
5964/****************************************************************/
5965
Gilles Peskinea05219c2018-11-16 16:02:56 +01005966#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005967static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5968 size_t peer_key_length,
5969 const mbedtls_ecp_keypair *our_key,
5970 uint8_t *shared_secret,
5971 size_t shared_secret_size,
5972 size_t *shared_secret_length )
5973{
Steven Cooremand4867872020-08-05 16:31:39 +02005974 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005975 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005976 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005977 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005978 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005979 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005980
Steven Cooreman4fed4552020-08-03 14:46:03 +02005981 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5982 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005983 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005984 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005985 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005986 goto exit;
5987
Jaeden Amero97271b32019-01-10 19:38:51 +00005988 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005989 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005990 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005991 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005992 status = mbedtls_to_psa_error(
5993 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5994 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005995 goto exit;
5996
Jaeden Amero97271b32019-01-10 19:38:51 +00005997 status = mbedtls_to_psa_error(
5998 mbedtls_ecdh_calc_secret( &ecdh,
5999 shared_secret_length,
6000 shared_secret, shared_secret_size,
6001 mbedtls_ctr_drbg_random,
6002 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006003 if( status != PSA_SUCCESS )
6004 goto exit;
6005 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6006 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006007
6008exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006009 if( status != PSA_SUCCESS )
6010 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006011 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006012 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006013 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006014
Jaeden Amero97271b32019-01-10 19:38:51 +00006015 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006016}
Gilles Peskinea05219c2018-11-16 16:02:56 +01006017#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006018
Gilles Peskine01d718c2018-09-18 12:01:02 +02006019#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6020
Gilles Peskine0216fe12019-04-11 21:23:21 +02006021static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6022 psa_key_slot_t *private_key,
6023 const uint8_t *peer_key,
6024 size_t peer_key_length,
6025 uint8_t *shared_secret,
6026 size_t shared_secret_size,
6027 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006028{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006029 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006030 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006031#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006032 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006033 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006034 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006035 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006036 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006037 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006038 private_key->data.key.data,
6039 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006040 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006041 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006042 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006043 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006044 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006045 shared_secret, shared_secret_size,
6046 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006047 mbedtls_ecp_keypair_free( ecp );
6048 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006049 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006050#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006051 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006052 (void) private_key;
6053 (void) peer_key;
6054 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006055 (void) shared_secret;
6056 (void) shared_secret_size;
6057 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006058 return( PSA_ERROR_NOT_SUPPORTED );
6059 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006060}
6061
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006062/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006063 * to potentially free embedded data structures and wipe confidential data.
6064 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006065static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006066 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006067 psa_key_slot_t *private_key,
6068 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006069 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006070{
6071 psa_status_t status;
6072 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6073 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006074 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006075
6076 /* Step 1: run the secret agreement algorithm to generate the shared
6077 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006078 status = psa_key_agreement_raw_internal( ka_alg,
6079 private_key,
6080 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006081 shared_secret,
6082 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006083 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006084 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006085 goto exit;
6086
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006087 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006088 * the shared secret. A shared secret is permitted wherever a key
6089 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006090 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006091 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006092 shared_secret,
6093 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006094exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006095 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006096 return( status );
6097}
6098
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006099psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006100 psa_key_derivation_step_t step,
6101 psa_key_handle_t private_key,
6102 const uint8_t *peer_key,
6103 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006104{
Gilles Peskine2f060a82018-12-04 17:12:32 +01006105 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02006106 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006107 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006108 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02006109 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006110 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006111 if( status != PSA_SUCCESS )
6112 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006113 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006114 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006115 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006116 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006117 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006118 else
6119 {
6120 /* If a private key has been added as SECRET, we allow the derived
6121 * key material to be used as a key in PSA Crypto. */
6122 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6123 operation->can_output_key = 1;
6124 }
Gilles Peskine346797d2018-11-16 16:05:06 +01006125 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006126}
6127
Gilles Peskinebe697d82019-05-16 18:00:41 +02006128psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
6129 psa_key_handle_t private_key,
6130 const uint8_t *peer_key,
6131 size_t peer_key_length,
6132 uint8_t *output,
6133 size_t output_size,
6134 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006135{
6136 psa_key_slot_t *slot;
6137 psa_status_t status;
6138
6139 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6140 {
6141 status = PSA_ERROR_INVALID_ARGUMENT;
6142 goto exit;
6143 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02006144 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006145 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006146 if( status != PSA_SUCCESS )
6147 goto exit;
6148
6149 status = psa_key_agreement_raw_internal( alg, slot,
6150 peer_key, peer_key_length,
6151 output, output_size,
6152 output_length );
6153
6154exit:
6155 if( status != PSA_SUCCESS )
6156 {
6157 /* If an error happens and is not handled properly, the output
6158 * may be used as a key to protect sensitive data. Arrange for such
6159 * a key to be random, which is likely to result in decryption or
6160 * verification errors. This is better than filling the buffer with
6161 * some constant data such as zeros, which would result in the data
6162 * being protected with a reproducible, easily knowable key.
6163 */
6164 psa_generate_random( output, output_size );
6165 *output_length = output_size;
6166 }
6167 return( status );
6168}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006169
6170
6171/****************************************************************/
6172/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006173/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006174
Gilles Peskine4c317f42018-07-12 01:24:09 +02006175psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006176 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006177{
Janos Follath24eed8d2019-11-22 13:21:35 +00006178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006179 GUARD_MODULE_INITIALIZED;
6180
Gilles Peskinef181eca2019-08-07 13:49:00 +02006181 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6182 {
6183 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6184 output,
6185 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6186 if( ret != 0 )
6187 return( mbedtls_to_psa_error( ret ) );
6188 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6189 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6190 }
6191
Gilles Peskinee59236f2018-01-27 23:32:46 +01006192 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6193 return( mbedtls_to_psa_error( ret ) );
6194}
6195
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006196#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6197#include "mbedtls/entropy_poll.h"
6198
Gilles Peskine7228da22019-07-15 11:06:15 +02006199psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006200 size_t seed_size )
6201{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006202 if( global_data.initialized )
6203 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006204
6205 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6206 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6207 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6208 return( PSA_ERROR_INVALID_ARGUMENT );
6209
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006210 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006211}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006212#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006213
Gilles Peskinee56e8782019-04-26 17:34:02 +02006214#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
6215static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6216 size_t domain_parameters_size,
6217 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006218{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006219 size_t i;
6220 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006221
Gilles Peskinee56e8782019-04-26 17:34:02 +02006222 if( domain_parameters_size == 0 )
6223 {
6224 *exponent = 65537;
6225 return( PSA_SUCCESS );
6226 }
6227
6228 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6229 * support values that fit in a 32-bit integer, which is larger than
6230 * int on just about every platform anyway. */
6231 if( domain_parameters_size > sizeof( acc ) )
6232 return( PSA_ERROR_NOT_SUPPORTED );
6233 for( i = 0; i < domain_parameters_size; i++ )
6234 acc = ( acc << 8 ) | domain_parameters[i];
6235 if( acc > INT_MAX )
6236 return( PSA_ERROR_NOT_SUPPORTED );
6237 *exponent = acc;
6238 return( PSA_SUCCESS );
6239}
6240#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6241
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006242static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006243 psa_key_slot_t *slot, size_t bits,
6244 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006245{
Gilles Peskine8e338702019-07-30 20:06:31 +02006246 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006247
Gilles Peskinee56e8782019-04-26 17:34:02 +02006248 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006249 return( PSA_ERROR_INVALID_ARGUMENT );
6250
Gilles Peskinee59236f2018-01-27 23:32:46 +01006251 if( key_type_is_raw_bytes( type ) )
6252 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006253 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006254
6255 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006256 if( status != PSA_SUCCESS )
6257 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006258
6259 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006260 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6261 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006262 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006263
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006264 status = psa_generate_random( slot->data.key.data,
6265 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006266 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006267 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006268
6269 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006270#if defined(MBEDTLS_DES_C)
6271 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006272 psa_des_set_key_parity( slot->data.key.data,
6273 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006274#endif /* MBEDTLS_DES_C */
6275 }
6276 else
6277
6278#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006279 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006280 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006281 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006282 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006283 int exponent;
6284 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006285 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6286 return( PSA_ERROR_NOT_SUPPORTED );
6287 /* Accept only byte-aligned keys, for the same reasons as
6288 * in psa_import_rsa_key(). */
6289 if( bits % 8 != 0 )
6290 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006291 status = psa_read_rsa_exponent( domain_parameters,
6292 domain_parameters_size,
6293 &exponent );
6294 if( status != PSA_SUCCESS )
6295 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006296 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6297 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006298 mbedtls_ctr_drbg_random,
6299 &global_data.ctr_drbg,
6300 (unsigned int) bits,
6301 exponent );
6302 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006303 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006304
6305 /* Make sure to always have an export representation available */
6306 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6307
Steven Cooreman75b74362020-07-28 14:30:13 +02006308 status = psa_allocate_buffer_to_slot( slot, bytes );
6309 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006310 {
6311 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006312 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006313 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006314
6315 status = psa_export_rsa_key( type,
6316 &rsa,
6317 slot->data.key.data,
6318 bytes,
6319 &slot->data.key.bytes );
6320 mbedtls_rsa_free( &rsa );
6321 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006322 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006323 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006324 }
6325 else
6326#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6327
6328#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006329 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006330 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006331 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006332 mbedtls_ecp_group_id grp_id =
6333 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006334 const mbedtls_ecp_curve_info *curve_info =
6335 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006336 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006338 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006339 return( PSA_ERROR_NOT_SUPPORTED );
6340 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6341 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006342 mbedtls_ecp_keypair_init( &ecp );
6343 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006344 mbedtls_ctr_drbg_random,
6345 &global_data.ctr_drbg );
6346 if( ret != 0 )
6347 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006348 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006349 return( mbedtls_to_psa_error( ret ) );
6350 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006351
6352
6353 /* Make sure to always have an export representation available */
6354 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006355 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6356 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006357 {
6358 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006359 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006360 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006361
6362 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006363 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6364
6365 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006366 if( status != PSA_SUCCESS ) {
6367 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006368 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006369 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006370 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006371 }
6372 else
6373#endif /* MBEDTLS_ECP_C */
Steven Cooreman29149862020-08-05 15:43:42 +02006374 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006375 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006376 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006377
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006378 return( PSA_SUCCESS );
6379}
Darryl Green0c6575a2018-11-07 16:05:30 +00006380
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006381psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006382 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006383{
6384 psa_status_t status;
6385 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006386 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006387
Gilles Peskine0f84d622019-09-12 19:03:13 +02006388 /* Reject any attempt to create a zero-length key so that we don't
6389 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6390 if( psa_get_key_bits( attributes ) == 0 )
6391 return( PSA_ERROR_INVALID_ARGUMENT );
6392
Gilles Peskinedf179142019-07-15 22:02:14 +02006393 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6394 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006395 if( status != PSA_SUCCESS )
6396 goto exit;
6397
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006398 status = psa_driver_wrapper_generate_key( attributes,
6399 slot );
6400 if( status != PSA_ERROR_NOT_SUPPORTED ||
6401 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6402 goto exit;
6403
6404 status = psa_generate_key_internal(
6405 slot, attributes->core.bits,
6406 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006407
6408exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006409 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006410 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006411 if( status != PSA_SUCCESS )
6412 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006413 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006414 *handle = 0;
6415 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006416 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006417}
6418
6419
Gilles Peskinee59236f2018-01-27 23:32:46 +01006420
6421/****************************************************************/
6422/* Module setup */
6423/****************************************************************/
6424
Gilles Peskine5e769522018-11-20 21:59:56 +01006425psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6426 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6427 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6428{
6429 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6430 return( PSA_ERROR_BAD_STATE );
6431 global_data.entropy_init = entropy_init;
6432 global_data.entropy_free = entropy_free;
6433 return( PSA_SUCCESS );
6434}
6435
Gilles Peskinee59236f2018-01-27 23:32:46 +01006436void mbedtls_psa_crypto_free( void )
6437{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006438 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006439 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6440 {
6441 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006442 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006443 }
6444 /* Wipe all remaining data, including configuration.
6445 * In particular, this sets all state indicator to the value
6446 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006447 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006448#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006449 /* Unregister all secure element drivers, so that we restart from
6450 * a pristine state. */
6451 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006452#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006453}
6454
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006455#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6456/** Recover a transaction that was interrupted by a power failure.
6457 *
6458 * This function is called during initialization, before psa_crypto_init()
6459 * returns. If this function returns a failure status, the initialization
6460 * fails.
6461 */
6462static psa_status_t psa_crypto_recover_transaction(
6463 const psa_crypto_transaction_t *transaction )
6464{
6465 switch( transaction->unknown.type )
6466 {
6467 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6468 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006469 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006470 * is implemented.
6471 * https://github.com/ARMmbed/mbed-crypto/issues/218
6472 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006473 default:
6474 /* We found an unsupported transaction in the storage.
6475 * We don't know what state the storage is in. Give up. */
6476 return( PSA_ERROR_STORAGE_FAILURE );
6477 }
6478}
6479#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6480
Gilles Peskinee59236f2018-01-27 23:32:46 +01006481psa_status_t psa_crypto_init( void )
6482{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006483 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006484 const unsigned char drbg_seed[] = "PSA";
6485
Gilles Peskinec6b69072018-11-20 21:42:52 +01006486 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006487 if( global_data.initialized != 0 )
6488 return( PSA_SUCCESS );
6489
Gilles Peskine5e769522018-11-20 21:59:56 +01006490 /* Set default configuration if
6491 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6492 if( global_data.entropy_init == NULL )
6493 global_data.entropy_init = mbedtls_entropy_init;
6494 if( global_data.entropy_free == NULL )
6495 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006496
Gilles Peskinec6b69072018-11-20 21:42:52 +01006497 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006498 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006499#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6500 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6501 /* The PSA entropy injection feature depends on using NV seed as an entropy
6502 * source. Add NV seed as an entropy source for PSA entropy injection. */
6503 mbedtls_entropy_add_source( &global_data.entropy,
6504 mbedtls_nv_seed_poll, NULL,
6505 MBEDTLS_ENTROPY_BLOCK_SIZE,
6506 MBEDTLS_ENTROPY_SOURCE_STRONG );
6507#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006508 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006509 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006510 status = mbedtls_to_psa_error(
6511 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6512 mbedtls_entropy_func,
6513 &global_data.entropy,
6514 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6515 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006516 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006517 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006518
Gilles Peskine66fb1262018-12-10 16:29:04 +01006519 status = psa_initialize_key_slots( );
6520 if( status != PSA_SUCCESS )
6521 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006522
Gilles Peskined9348f22019-10-01 15:22:29 +02006523#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6524 status = psa_init_all_se_drivers( );
6525 if( status != PSA_SUCCESS )
6526 goto exit;
6527#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6528
Gilles Peskine4b734222019-07-24 15:56:31 +02006529#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006530 status = psa_crypto_load_transaction( );
6531 if( status == PSA_SUCCESS )
6532 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006533 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6534 if( status != PSA_SUCCESS )
6535 goto exit;
6536 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006537 }
6538 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6539 {
6540 /* There's no transaction to complete. It's all good. */
6541 status = PSA_SUCCESS;
6542 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006543#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006544
Gilles Peskinec6b69072018-11-20 21:42:52 +01006545 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006546 global_data.initialized = 1;
6547
6548exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006549 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006550 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006551 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006552}
6553
6554#endif /* MBEDTLS_PSA_CRYPTO_C */