blob: 71a505c70ad93b19a02ff32b32a18ee6d375b1b0 [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:
202 return( PSA_ERROR_BAD_STATE );
203 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.
742 * So its data length is 2m+1 where n is the key size in bits.
743 */
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
Gilles Peskine8e338702019-07-30 20:06:31 +0200972/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100973 * previously. This function assumes that the slot does not contain
974 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100975psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
976 const uint8_t *data,
977 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100978{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200979 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200981 /* zero-length keys are never supported. */
982 if( data_length == 0 )
983 return( PSA_ERROR_NOT_SUPPORTED );
984
Gilles Peskine8e338702019-07-30 20:06:31 +0200985 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200987 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200988
Steven Cooreman75b74362020-07-28 14:30:13 +0200989 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
990 if( data_length > SIZE_MAX / 8 )
991 return( PSA_ERROR_NOT_SUPPORTED );
992
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200993 /* Enforce a size limit, and in particular ensure that the bit
994 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200995 if( bit_size > PSA_MAX_KEY_BITS )
996 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200997
998 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200999 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001000 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001001
1002 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02001003 status = psa_allocate_buffer_to_slot( slot, data_length );
1004 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001005 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001006
1007 /* copy key into allocated buffer */
Steven Cooreman29149862020-08-05 15:43:42 +02001008 memcpy( slot->data.key.data, data, data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001009
1010 /* Write the actual key size to the slot.
1011 * psa_start_key_creation() wrote the size declared by the
1012 * caller, which may be 0 (meaning unspecified) or wrong. */
1013 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001015 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1016 {
Gilles Peskinef76aa772018-10-29 19:24:33 +01001017#if defined(MBEDTLS_ECP_C)
Steven Cooreman19fd5742020-07-24 23:31:01 +02001018 status = psa_import_ecp_key( slot,
1019 data, data_length );
1020#else
1021 /* No drivers have been implemented yet, so without mbed TLS backing
1022 * there's no way to do ECP with the current library. */
1023 return( PSA_ERROR_NOT_SUPPORTED );
1024#endif /* defined(MBEDTLS_ECP_C) */
1025 }
1026 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +01001027 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001028#if defined(MBEDTLS_RSA_C)
1029 status = psa_import_rsa_key( slot,
1030 data, data_length );
1031#else
1032 /* No drivers have been implemented yet, so without mbed TLS backing
1033 * there's no way to do RSA with the current library. */
1034 status = PSA_ERROR_NOT_SUPPORTED;
1035#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036 }
1037 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001038 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001039 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001041 }
Darryl Green940d72c2018-07-13 13:18:51 +01001042
Darryl Green06fd18d2018-07-16 11:21:11 +01001043 return( status );
1044}
1045
Gilles Peskinef603c712019-01-19 13:40:11 +01001046/** Calculate the intersection of two algorithm usage policies.
1047 *
1048 * Return 0 (which allows no operation) on incompatibility.
1049 */
1050static psa_algorithm_t psa_key_policy_algorithm_intersection(
1051 psa_algorithm_t alg1,
1052 psa_algorithm_t alg2 )
1053{
Gilles Peskine549ea862019-05-22 11:45:59 +02001054 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001055 if( alg1 == alg2 )
1056 return( alg1 );
1057 /* If the policies are from the same hash-and-sign family, check
1058 * if one is a wildcard. If so the other has the specific algorithm. */
1059 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1060 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1061 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1062 {
1063 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1064 return( alg2 );
1065 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1066 return( alg1 );
1067 }
1068 /* If the policies are incompatible, allow nothing. */
1069 return( 0 );
1070}
1071
Gilles Peskined6f371b2019-05-10 19:33:38 +02001072static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1073 psa_algorithm_t requested_alg )
1074{
Gilles Peskine549ea862019-05-22 11:45:59 +02001075 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001076 if( requested_alg == policy_alg )
1077 return( 1 );
1078 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001079 * and requested_alg is the same hash-and-sign family with any hash,
1080 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001081 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1082 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1083 {
1084 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1085 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1086 }
Steven Cooremance48e852020-10-05 16:02:45 +02001087 /* If policy_alg is a generic key agreement operation, then using it for
1088 * a key derivation with that key agreement is also compliant. */
1089 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1090 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1091 {
1092 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1093 policy_alg );
1094 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001095 /* If it isn't permitted, it's forbidden. */
1096 return( 0 );
1097}
1098
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001099/** Test whether a policy permits an algorithm.
1100 *
1101 * The caller must test usage flags separately.
1102 */
1103static int psa_key_policy_permits( const psa_key_policy_t *policy,
1104 psa_algorithm_t alg )
1105{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001106 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1107 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001108}
1109
Gilles Peskinef603c712019-01-19 13:40:11 +01001110/** Restrict a key policy based on a constraint.
1111 *
1112 * \param[in,out] policy The policy to restrict.
1113 * \param[in] constraint The policy constraint to apply.
1114 *
1115 * \retval #PSA_SUCCESS
1116 * \c *policy contains the intersection of the original value of
1117 * \c *policy and \c *constraint.
1118 * \retval #PSA_ERROR_INVALID_ARGUMENT
1119 * \c *policy and \c *constraint are incompatible.
1120 * \c *policy is unchanged.
1121 */
1122static psa_status_t psa_restrict_key_policy(
1123 psa_key_policy_t *policy,
1124 const psa_key_policy_t *constraint )
1125{
1126 psa_algorithm_t intersection_alg =
1127 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001128 psa_algorithm_t intersection_alg2 =
1129 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001130 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1131 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001132 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1133 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001134 policy->usage &= constraint->usage;
1135 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001136 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001137 return( PSA_SUCCESS );
1138}
1139
Darryl Green06fd18d2018-07-16 11:21:11 +01001140/** Retrieve a slot which must contain a key. The key must have allow all the
1141 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1142 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001143static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001144 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001145 psa_key_usage_t usage,
1146 psa_algorithm_t alg )
1147{
1148 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001149 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001150
1151 *p_slot = NULL;
1152
Gilles Peskinec5487a82018-12-03 18:08:14 +01001153 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001154 if( status != PSA_SUCCESS )
1155 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001156
1157 /* Enforce that usage policy for the key slot contains all the flags
1158 * required by the usage parameter. There is one exception: public
1159 * keys can always be exported, so we treat public key objects as
1160 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001161 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001162 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001163 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001164 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001165
1166 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001167 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001168 return( PSA_ERROR_NOT_PERMITTED );
1169
1170 *p_slot = slot;
1171 return( PSA_SUCCESS );
1172}
Darryl Green940d72c2018-07-13 13:18:51 +01001173
Gilles Peskine28f8f302019-07-24 13:30:31 +02001174/** Retrieve a slot which must contain a transparent key.
1175 *
1176 * A transparent key is a key for which the key material is directly
1177 * available, as opposed to a key in a secure element.
1178 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001179 * This is a temporary function to use instead of psa_get_key_from_slot()
1180 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001181 */
1182#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1183static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1184 psa_key_slot_t **p_slot,
1185 psa_key_usage_t usage,
1186 psa_algorithm_t alg )
1187{
1188 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1189 if( status != PSA_SUCCESS )
1190 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001191 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001192 {
1193 *p_slot = NULL;
1194 return( PSA_ERROR_NOT_SUPPORTED );
1195 }
1196 return( PSA_SUCCESS );
1197}
1198#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1199/* With no secure element support, all keys are transparent. */
1200#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1201 psa_get_key_from_slot( handle, p_slot, usage, alg )
1202#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1203
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001204/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001205static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001206{
Gilles Peskine73167e12019-07-12 23:44:37 +02001207#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1208 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001209 {
1210 /* No key material to clean. */
1211 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001212 else
1213#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001214 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001215 /* Data pointer will always be either a valid pointer or NULL in an
1216 * initialized slot, so we can just free it. */
1217 if( slot->data.key.data != NULL )
1218 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001219 mbedtls_free( slot->data.key.data );
1220 slot->data.key.data = NULL;
1221 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001222 }
Darryl Green40225ba2018-11-15 14:48:15 +00001223
1224 return( PSA_SUCCESS );
1225}
1226
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001227/** Completely wipe a slot in memory, including its policy.
1228 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001229psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001230{
1231 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001232 /* Multipart operations may still be using the key. This is safe
1233 * because all multipart operation objects are independent from
1234 * the key slot: if they need to access the key after the setup
1235 * phase, they have a copy of the key. Note that this means that
1236 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001237 /* At this point, key material and other type-specific content has
1238 * been wiped. Clear remaining metadata. We can call memset and not
1239 * zeroize because the metadata is not particularly sensitive. */
1240 memset( slot, 0, sizeof( *slot ) );
1241 return( status );
1242}
1243
Gilles Peskinec5487a82018-12-03 18:08:14 +01001244psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001245{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001246 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001247 psa_status_t status; /* status of the last operation */
1248 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001249#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1250 psa_se_drv_table_entry_t *driver;
1251#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001252
Gilles Peskine1841cf42019-10-08 15:48:25 +02001253 if( handle == 0 )
1254 return( PSA_SUCCESS );
1255
Gilles Peskinec5487a82018-12-03 18:08:14 +01001256 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001257 if( status != PSA_SUCCESS )
1258 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001259
1260#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001261 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001262 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001263 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001264 /* For a key in a secure element, we need to do three things:
1265 * remove the key file in internal storage, destroy the
1266 * key inside the secure element, and update the driver's
1267 * persistent data. Start a transaction that will encompass these
1268 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001269 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001270 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001271 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001272 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001273 status = psa_crypto_save_transaction( );
1274 if( status != PSA_SUCCESS )
1275 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001276 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001277 /* We should still try to destroy the key in the secure
1278 * element and the key metadata in storage. This is especially
1279 * important if the error is that the storage is full.
1280 * But how to do it exactly without risking an inconsistent
1281 * state after a reset?
1282 * https://github.com/ARMmbed/mbed-crypto/issues/215
1283 */
1284 overall_status = status;
1285 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001286 }
1287
Gilles Peskine354f7672019-07-12 23:46:38 +02001288 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001289 if( overall_status == PSA_SUCCESS )
1290 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001291 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001292#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1293
Darryl Greend49a4992018-06-18 17:27:26 +01001294#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001295 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001296 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001297 status = psa_destroy_persistent_key( slot->attr.id );
1298 if( overall_status == PSA_SUCCESS )
1299 overall_status = status;
1300
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001301 /* TODO: other slots may have a copy of the same key. We should
1302 * invalidate them.
1303 * https://github.com/ARMmbed/mbed-crypto/issues/214
1304 */
Darryl Greend49a4992018-06-18 17:27:26 +01001305 }
1306#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001307
Gilles Peskinefc762652019-07-22 19:30:34 +02001308#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1309 if( driver != NULL )
1310 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001311 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001312 if( overall_status == PSA_SUCCESS )
1313 overall_status = status;
1314 status = psa_crypto_stop_transaction( );
1315 if( overall_status == PSA_SUCCESS )
1316 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001317 }
1318#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1319
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001320#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1321exit:
1322#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001323 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001324 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1325 if( overall_status == PSA_SUCCESS )
1326 overall_status = status;
1327 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001328}
1329
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001330void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001331{
Gilles Peskineb699f072019-04-26 16:06:02 +02001332 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001333 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001334}
1335
Gilles Peskineb699f072019-04-26 16:06:02 +02001336psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1337 psa_key_type_t type,
1338 const uint8_t *data,
1339 size_t data_length )
1340{
1341 uint8_t *copy = NULL;
1342
1343 if( data_length != 0 )
1344 {
1345 copy = mbedtls_calloc( 1, data_length );
1346 if( copy == NULL )
1347 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1348 memcpy( copy, data, data_length );
1349 }
1350 /* After this point, this function is guaranteed to succeed, so it
1351 * can start modifying `*attributes`. */
1352
1353 if( attributes->domain_parameters != NULL )
1354 {
1355 mbedtls_free( attributes->domain_parameters );
1356 attributes->domain_parameters = NULL;
1357 attributes->domain_parameters_size = 0;
1358 }
1359
1360 attributes->domain_parameters = copy;
1361 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001362 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001363 return( PSA_SUCCESS );
1364}
1365
1366psa_status_t psa_get_key_domain_parameters(
1367 const psa_key_attributes_t *attributes,
1368 uint8_t *data, size_t data_size, size_t *data_length )
1369{
1370 if( attributes->domain_parameters_size > data_size )
1371 return( PSA_ERROR_BUFFER_TOO_SMALL );
1372 *data_length = attributes->domain_parameters_size;
1373 if( attributes->domain_parameters_size != 0 )
1374 memcpy( data, attributes->domain_parameters,
1375 attributes->domain_parameters_size );
1376 return( PSA_SUCCESS );
1377}
1378
1379#if defined(MBEDTLS_RSA_C)
1380static psa_status_t psa_get_rsa_public_exponent(
1381 const mbedtls_rsa_context *rsa,
1382 psa_key_attributes_t *attributes )
1383{
1384 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001385 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001386 uint8_t *buffer = NULL;
1387 size_t buflen;
1388 mbedtls_mpi_init( &mpi );
1389
1390 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1391 if( ret != 0 )
1392 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001393 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1394 {
1395 /* It's the default value, which is reported as an empty string,
1396 * so there's nothing to do. */
1397 goto exit;
1398 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001399
1400 buflen = mbedtls_mpi_size( &mpi );
1401 buffer = mbedtls_calloc( 1, buflen );
1402 if( buffer == NULL )
1403 {
1404 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1405 goto exit;
1406 }
1407 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1408 if( ret != 0 )
1409 goto exit;
1410 attributes->domain_parameters = buffer;
1411 attributes->domain_parameters_size = buflen;
1412
1413exit:
1414 mbedtls_mpi_free( &mpi );
1415 if( ret != 0 )
1416 mbedtls_free( buffer );
1417 return( mbedtls_to_psa_error( ret ) );
1418}
1419#endif /* MBEDTLS_RSA_C */
1420
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001421/** Retrieve all the publicly-accessible attributes of a key.
1422 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001423psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1424 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001425{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001426 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001427 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001428
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001429 psa_reset_key_attributes( attributes );
1430
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001431 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001432 if( status != PSA_SUCCESS )
1433 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001434
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001435 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001436 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1437 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1438
1439#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1440 if( psa_key_slot_is_external( slot ) )
1441 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1442#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001443
Gilles Peskine8e338702019-07-30 20:06:31 +02001444 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001445 {
1446#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001447 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001448 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001449#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001450 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001451 * is not yet implemented.
1452 * https://github.com/ARMmbed/mbed-crypto/issues/216
1453 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001454 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001455 break;
1456#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001457 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001458 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001459
Steven Cooreman4fed4552020-08-03 14:46:03 +02001460 status = psa_load_rsa_representation( slot->attr.type,
1461 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001462 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001463 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001464 if( status != PSA_SUCCESS )
1465 break;
1466
Steven Cooremana2371e52020-07-28 14:30:39 +02001467 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001468 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001469 mbedtls_rsa_free( rsa );
1470 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001471 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001472 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001473#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001474 default:
1475 /* Nothing else to do. */
1476 break;
1477 }
1478
1479 if( status != PSA_SUCCESS )
1480 psa_reset_key_attributes( attributes );
1481 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001482}
1483
Gilles Peskinec8000c02019-08-02 20:15:51 +02001484#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1485psa_status_t psa_get_key_slot_number(
1486 const psa_key_attributes_t *attributes,
1487 psa_key_slot_number_t *slot_number )
1488{
1489 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1490 {
1491 *slot_number = attributes->slot_number;
1492 return( PSA_SUCCESS );
1493 }
1494 else
1495 return( PSA_ERROR_INVALID_ARGUMENT );
1496}
1497#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1498
Steven Cooremana01795d2020-07-24 22:48:15 +02001499static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1500 uint8_t *data,
1501 size_t data_size,
1502 size_t *data_length )
1503{
1504 if( slot->data.key.bytes > data_size )
1505 return( PSA_ERROR_BUFFER_TOO_SMALL );
1506 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1507 memset( data + slot->data.key.bytes, 0,
1508 data_size - slot->data.key.bytes );
1509 *data_length = slot->data.key.bytes;
1510 return( PSA_SUCCESS );
1511}
1512
Gilles Peskinef603c712019-01-19 13:40:11 +01001513static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1514 uint8_t *data,
1515 size_t data_size,
1516 size_t *data_length,
1517 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001518{
Gilles Peskine5d309672019-07-12 23:47:28 +02001519#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1520 const psa_drv_se_t *drv;
1521 psa_drv_se_context_t *drv_context;
1522#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1523
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001524 *data_length = 0;
1525
Gilles Peskine8e338702019-07-30 20:06:31 +02001526 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001527 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001528
Gilles Peskinef9168942019-09-12 19:20:29 +02001529 /* Reject a zero-length output buffer now, since this can never be a
1530 * valid key representation. This way we know that data must be a valid
1531 * pointer and we can do things like memset(data, ..., data_size). */
1532 if( data_size == 0 )
1533 return( PSA_ERROR_BUFFER_TOO_SMALL );
1534
Gilles Peskine5d309672019-07-12 23:47:28 +02001535#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001536 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001537 {
1538 psa_drv_se_export_key_t method;
1539 if( drv->key_management == NULL )
1540 return( PSA_ERROR_NOT_SUPPORTED );
1541 method = ( export_public_key ?
1542 drv->key_management->p_export_public :
1543 drv->key_management->p_export );
1544 if( method == NULL )
1545 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001546 return( method( drv_context,
1547 slot->data.se.slot_number,
1548 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001549 }
1550#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1551
Gilles Peskine8e338702019-07-30 20:06:31 +02001552 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001553 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001554 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001555 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001556 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1557 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001558 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001559 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001560 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001561 /* Exporting public -> public */
1562 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1563 }
1564 else if( !export_public_key )
1565 {
1566 /* Exporting private -> private */
1567 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1568 }
1569 /* Need to export the public part of a private key,
1570 * so conversion is needed */
1571 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1572 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001573#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001574 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001575 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001576 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001577 slot->data.key.data,
1578 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001579 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001580 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001581 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001582
Steven Cooreman560c28a2020-07-24 23:20:24 +02001583 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001584 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001585 data,
1586 data_size,
1587 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001588
Steven Cooremana2371e52020-07-28 14:30:39 +02001589 mbedtls_rsa_free( rsa );
1590 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001591
Steven Cooreman560c28a2020-07-24 23:20:24 +02001592 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001593#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001594 /* We don't know how to convert a private RSA key to public. */
1595 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001596#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001597 }
1598 else
Moran Pekera998bc62018-04-16 18:16:20 +03001599 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001600#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001601 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001602 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001603 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001604 slot->data.key.data,
1605 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001606 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001607 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001608 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001609
1610 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1611 PSA_KEY_TYPE_ECC_GET_FAMILY(
1612 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001613 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001614 data,
1615 data_size,
1616 data_length );
1617
Steven Cooremana2371e52020-07-28 14:30:39 +02001618 mbedtls_ecp_keypair_free( ecp );
1619 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001620 return( status );
1621#else
1622 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001623 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001624#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001625 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001626 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001627 else
1628 {
1629 /* This shouldn't happen in the reference implementation, but
1630 it is valid for a special-purpose implementation to omit
1631 support for exporting certain key types. */
1632 return( PSA_ERROR_NOT_SUPPORTED );
1633 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001634}
1635
Gilles Peskinec5487a82018-12-03 18:08:14 +01001636psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001637 uint8_t *data,
1638 size_t data_size,
1639 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001640{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001641 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001642 psa_status_t status;
1643
1644 /* Set the key to empty now, so that even when there are errors, we always
1645 * set data_length to a value between 0 and data_size. On error, setting
1646 * the key to empty is a good choice because an empty key representation is
1647 * unlikely to be accepted anywhere. */
1648 *data_length = 0;
1649
1650 /* Export requires the EXPORT flag. There is an exception for public keys,
1651 * which don't require any flag, but psa_get_key_from_slot takes
1652 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001653 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001654 if( status != PSA_SUCCESS )
1655 return( status );
1656 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001657 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001658}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001659
Gilles Peskinec5487a82018-12-03 18:08:14 +01001660psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001661 uint8_t *data,
1662 size_t data_size,
1663 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001664{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001665 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001666 psa_status_t status;
1667
1668 /* Set the key to empty now, so that even when there are errors, we always
1669 * set data_length to a value between 0 and data_size. On error, setting
1670 * the key to empty is a good choice because an empty key representation is
1671 * unlikely to be accepted anywhere. */
1672 *data_length = 0;
1673
1674 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001675 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001676 if( status != PSA_SUCCESS )
1677 return( status );
1678 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001679 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001680}
1681
Gilles Peskine91e8c332019-08-02 19:19:39 +02001682#if defined(static_assert)
1683static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1684 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001685static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001686 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001687static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001688 "One or more key attribute flag is listed as both internal-only and external-only" );
1689#endif
1690
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001691/** Validate that a key policy is internally well-formed.
1692 *
1693 * This function only rejects invalid policies. It does not validate the
1694 * consistency of the policy with respect to other attributes of the key
1695 * such as the key type.
1696 */
1697static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001698{
1699 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001700 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001701 PSA_KEY_USAGE_ENCRYPT |
1702 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001703 PSA_KEY_USAGE_SIGN_HASH |
1704 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001705 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1706 return( PSA_ERROR_INVALID_ARGUMENT );
1707
Gilles Peskine4747d192019-04-17 15:05:45 +02001708 return( PSA_SUCCESS );
1709}
1710
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001711/** Validate the internal consistency of key attributes.
1712 *
1713 * This function only rejects invalid attribute values. If does not
1714 * validate the consistency of the attributes with any key data that may
1715 * be involved in the creation of the key.
1716 *
1717 * Call this function early in the key creation process.
1718 *
1719 * \param[in] attributes Key attributes for the new key.
1720 * \param[out] p_drv On any return, the driver for the key, if any.
1721 * NULL for a transparent key.
1722 *
1723 */
1724static psa_status_t psa_validate_key_attributes(
1725 const psa_key_attributes_t *attributes,
1726 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001727{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001728 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001729
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001730 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1731 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001732 if( status != PSA_SUCCESS )
1733 return( status );
1734
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001735 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1736 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001737 if( status != PSA_SUCCESS )
1738 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001739
1740 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001741 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001742 return( status );
1743
1744 /* Refuse to create overly large keys.
1745 * Note that this doesn't trigger on import if the attributes don't
1746 * explicitly specify a size (so psa_get_key_bits returns 0), so
1747 * psa_import_key() needs its own checks. */
1748 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1749 return( PSA_ERROR_NOT_SUPPORTED );
1750
Gilles Peskine91e8c332019-08-02 19:19:39 +02001751 /* Reject invalid flags. These should not be reachable through the API. */
1752 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1753 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1754 return( PSA_ERROR_INVALID_ARGUMENT );
1755
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001756 return( PSA_SUCCESS );
1757}
1758
Gilles Peskine4747d192019-04-17 15:05:45 +02001759/** Prepare a key slot to receive key material.
1760 *
1761 * This function allocates a key slot and sets its metadata.
1762 *
1763 * If this function fails, call psa_fail_key_creation().
1764 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001765 * This function is intended to be used as follows:
1766 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1767 * it with the specified attributes, and assign it a handle.
1768 * -# Populate the slot with the key material.
1769 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1770 * In case of failure at any step, stop the sequence and call
1771 * psa_fail_key_creation().
1772 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001773 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001774 * \param[in] attributes Key attributes for the new key.
1775 * \param[out] handle On success, a handle for the allocated slot.
1776 * \param[out] p_slot On success, a pointer to the prepared slot.
1777 * \param[out] p_drv On any return, the driver for the key, if any.
1778 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001779 *
1780 * \retval #PSA_SUCCESS
1781 * The key slot is ready to receive key material.
1782 * \return If this function fails, the key slot is an invalid state.
1783 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001784 */
1785static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001786 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001787 const psa_key_attributes_t *attributes,
1788 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001789 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001790 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001791{
1792 psa_status_t status;
1793 psa_key_slot_t *slot;
1794
Gilles Peskinedf179142019-07-15 22:02:14 +02001795 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001796 *p_drv = NULL;
1797
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001798 status = psa_validate_key_attributes( attributes, p_drv );
1799 if( status != PSA_SUCCESS )
1800 return( status );
1801
Gilles Peskineedbed562019-08-07 18:19:59 +02001802 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001803 if( status != PSA_SUCCESS )
1804 return( status );
1805 slot = *p_slot;
1806
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001807 /* We're storing the declared bit-size of the key. It's up to each
1808 * creation mechanism to verify that this information is correct.
1809 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001810 * an input (generate, device) but not for those where the bit-size
1811 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001812
1813 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001814
Gilles Peskine91e8c332019-08-02 19:19:39 +02001815 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001816 * external-only flags, query `attributes`. Thanks to the check
1817 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001818 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001819 * may have set. */
1820 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001821
Gilles Peskinecbaff462019-07-12 23:46:04 +02001822#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001823 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001824 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001825 * create the key file in internal storage, create the
1826 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001827 * persistent data. This is done by starting a transaction that will
1828 * encompass these three actions.
1829 * For registering a volatile key, we just need to find an appropriate
1830 * slot number inside the SE. Since the key is designated volatile, creating
1831 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001832 /* The first thing to do is to find a slot number for the new key.
1833 * We save the slot number in persistent storage as part of the
1834 * transaction data. It will be needed to recover if the power
1835 * fails during the key creation process, to clean up on the secure
1836 * element side after restarting. Obtaining a slot number from the
1837 * secure element driver updates its persistent state, but we do not yet
1838 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001839 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001840 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001841 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001842 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001843 &slot->data.se.slot_number );
1844 if( status != PSA_SUCCESS )
1845 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001846
1847 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001848 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001849 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1850 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1851 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1852 psa_crypto_transaction.key.id = slot->attr.id;
1853 status = psa_crypto_save_transaction( );
1854 if( status != PSA_SUCCESS )
1855 {
1856 (void) psa_crypto_stop_transaction( );
1857 return( status );
1858 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001859 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001860 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001861
1862 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1863 {
1864 /* Key registration only makes sense with a secure element. */
1865 return( PSA_ERROR_INVALID_ARGUMENT );
1866 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001867#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1868
Darryl Green0c6575a2018-11-07 16:05:30 +00001869 return( status );
1870}
Gilles Peskine4747d192019-04-17 15:05:45 +02001871
1872/** Finalize the creation of a key once its key material has been set.
1873 *
1874 * This entails writing the key to persistent storage.
1875 *
1876 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001877 * See the documentation of psa_start_key_creation() for the intended use
1878 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001879 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001880 * \param[in,out] slot Pointer to the slot with key material.
1881 * \param[in] driver The secure element driver for the key,
1882 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001883 *
1884 * \retval #PSA_SUCCESS
1885 * The key was successfully created. The handle is now valid.
1886 * \return If this function fails, the key slot is an invalid state.
1887 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001888 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001889static psa_status_t psa_finish_key_creation(
1890 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001891 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001892{
1893 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001894 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001895 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001896
1897#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001898 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001899 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001900#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1901 if( driver != NULL )
1902 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001903 psa_se_key_data_storage_t data;
1904#if defined(static_assert)
1905 static_assert( sizeof( slot->data.se.slot_number ) ==
1906 sizeof( data.slot_number ),
1907 "Slot number size does not match psa_se_key_data_storage_t" );
1908 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1909 "Bit-size size does not match psa_se_key_data_storage_t" );
1910#endif
1911 memcpy( &data.slot_number, &slot->data.se.slot_number,
1912 sizeof( slot->data.se.slot_number ) );
1913 memcpy( &data.bits, &slot->attr.bits,
1914 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001915 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001916 (uint8_t*) &data,
1917 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001918 }
1919 else
1920#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1921 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001922 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001923 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001924 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001925 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1926 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001927 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001928 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1929 status = psa_internal_export_key( slot,
1930 buffer, buffer_size, &length,
1931 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001932 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001933 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001934 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001935
Gilles Peskinef9168942019-09-12 19:20:29 +02001936 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001937 mbedtls_free( buffer );
1938 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001939 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001940#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1941
Gilles Peskinecbaff462019-07-12 23:46:04 +02001942#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001943 /* Finish the transaction for a key creation. This does not
1944 * happen when registering an existing key. Detect this case
1945 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001946 * creation of a persistent key in a secure element requires a transaction,
1947 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001948 if( driver != NULL &&
1949 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001950 {
1951 status = psa_save_se_persistent_data( driver );
1952 if( status != PSA_SUCCESS )
1953 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001954 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001955 return( status );
1956 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001957 status = psa_crypto_stop_transaction( );
1958 if( status != PSA_SUCCESS )
1959 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001960 }
1961#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1962
Gilles Peskine4747d192019-04-17 15:05:45 +02001963 return( status );
1964}
1965
1966/** Abort the creation of a key.
1967 *
1968 * You may call this function after calling psa_start_key_creation(),
1969 * or after psa_finish_key_creation() fails. In other circumstances, this
1970 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001971 * See the documentation of psa_start_key_creation() for the intended use
1972 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001973 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001974 * \param[in,out] slot Pointer to the slot with key material.
1975 * \param[in] driver The secure element driver for the key,
1976 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001977 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001978static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001979 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001980{
Gilles Peskine011e4282019-06-26 18:34:38 +02001981 (void) driver;
1982
Gilles Peskine4747d192019-04-17 15:05:45 +02001983 if( slot == NULL )
1984 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001985
1986#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001987 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001988 * element, and the failure happened later (when saving metadata
1989 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001990 * element.
1991 * https://github.com/ARMmbed/mbed-crypto/issues/217
1992 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001993
Gilles Peskined7729582019-08-05 15:55:54 +02001994 /* Abort the ongoing transaction if any (there may not be one if
1995 * the creation process failed before starting one, or if the
1996 * key creation is a registration of a key in a secure element).
1997 * Earlier functions must already have done what it takes to undo any
1998 * partial creation. All that's left is to update the transaction data
1999 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002000 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002001#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2002
Gilles Peskine4747d192019-04-17 15:05:45 +02002003 psa_wipe_key_slot( slot );
2004}
2005
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002006/** Validate optional attributes during key creation.
2007 *
2008 * Some key attributes are optional during key creation. If they are
2009 * specified in the attributes structure, check that they are consistent
2010 * with the data in the slot.
2011 *
2012 * This function should be called near the end of key creation, after
2013 * the slot in memory is fully populated but before saving persistent data.
2014 */
2015static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002016 const psa_key_slot_t *slot,
2017 const psa_key_attributes_t *attributes )
2018{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002019 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002020 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002021 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002022 return( PSA_ERROR_INVALID_ARGUMENT );
2023 }
2024
2025 if( attributes->domain_parameters_size != 0 )
2026 {
2027#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002028 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002029 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002030 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002031 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002032 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002033
Steven Cooreman7f391872020-07-30 14:57:44 +02002034 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002035 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002036 slot->data.key.data,
2037 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002038 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002039 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002040 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002041
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002042 mbedtls_mpi_init( &actual );
2043 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002044 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002045 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002046 mbedtls_rsa_free( rsa );
2047 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002048 if( ret != 0 )
2049 goto rsa_exit;
2050 ret = mbedtls_mpi_read_binary( &required,
2051 attributes->domain_parameters,
2052 attributes->domain_parameters_size );
2053 if( ret != 0 )
2054 goto rsa_exit;
2055 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2056 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2057 rsa_exit:
2058 mbedtls_mpi_free( &actual );
2059 mbedtls_mpi_free( &required );
2060 if( ret != 0)
2061 return( mbedtls_to_psa_error( ret ) );
2062 }
2063 else
2064#endif
2065 {
2066 return( PSA_ERROR_INVALID_ARGUMENT );
2067 }
2068 }
2069
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002070 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002071 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002072 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002073 return( PSA_ERROR_INVALID_ARGUMENT );
2074 }
2075
2076 return( PSA_SUCCESS );
2077}
2078
Gilles Peskine4747d192019-04-17 15:05:45 +02002079psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002080 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002081 size_t data_length,
2082 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002083{
2084 psa_status_t status;
2085 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002086 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002087
Gilles Peskine0f84d622019-09-12 19:03:13 +02002088 /* Reject zero-length symmetric keys (including raw data key objects).
2089 * This also rejects any key which might be encoded as an empty string,
2090 * which is never valid. */
2091 if( data_length == 0 )
2092 return( PSA_ERROR_INVALID_ARGUMENT );
2093
Gilles Peskinedf179142019-07-15 22:02:14 +02002094 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2095 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002096 if( status != PSA_SUCCESS )
2097 goto exit;
2098
Gilles Peskine5d309672019-07-12 23:47:28 +02002099#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2100 if( driver != NULL )
2101 {
2102 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002103 /* The driver should set the number of key bits, however in
2104 * case it doesn't, we initialize bits to an invalid value. */
2105 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002106 if( drv->key_management == NULL ||
2107 drv->key_management->p_import == NULL )
2108 {
2109 status = PSA_ERROR_NOT_SUPPORTED;
2110 goto exit;
2111 }
2112 status = drv->key_management->p_import(
2113 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002114 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002115 &bits );
2116 if( status != PSA_SUCCESS )
2117 goto exit;
2118 if( bits > PSA_MAX_KEY_BITS )
2119 {
2120 status = PSA_ERROR_NOT_SUPPORTED;
2121 goto exit;
2122 }
2123 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002124 }
2125 else
2126#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2127 {
2128 status = psa_import_key_into_slot( slot, data, data_length );
2129 if( status != PSA_SUCCESS )
2130 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002131 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002132 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002133 if( status != PSA_SUCCESS )
2134 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002135
Gilles Peskine011e4282019-06-26 18:34:38 +02002136 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002137exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002138 if( status != PSA_SUCCESS )
2139 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002140 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002141 *handle = 0;
2142 }
2143 return( status );
2144}
2145
Gilles Peskined7729582019-08-05 15:55:54 +02002146#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2147psa_status_t mbedtls_psa_register_se_key(
2148 const psa_key_attributes_t *attributes )
2149{
2150 psa_status_t status;
2151 psa_key_slot_t *slot = NULL;
2152 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002153 psa_key_handle_t handle = 0;
2154
2155 /* Leaving attributes unspecified is not currently supported.
2156 * It could make sense to query the key type and size from the
2157 * secure element, but not all secure elements support this
2158 * and the driver HAL doesn't currently support it. */
2159 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2160 return( PSA_ERROR_NOT_SUPPORTED );
2161 if( psa_get_key_bits( attributes ) == 0 )
2162 return( PSA_ERROR_NOT_SUPPORTED );
2163
2164 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2165 &handle, &slot, &driver );
2166 if( status != PSA_SUCCESS )
2167 goto exit;
2168
Gilles Peskined7729582019-08-05 15:55:54 +02002169 status = psa_finish_key_creation( slot, driver );
2170
2171exit:
2172 if( status != PSA_SUCCESS )
2173 {
2174 psa_fail_key_creation( slot, driver );
2175 }
2176 /* Registration doesn't keep the key in RAM. */
2177 psa_close_key( handle );
2178 return( status );
2179}
2180#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2181
Gilles Peskinef603c712019-01-19 13:40:11 +01002182static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002183 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002184{
2185 psa_status_t status;
2186 uint8_t *buffer = NULL;
2187 size_t buffer_size = 0;
2188 size_t length;
2189
Gilles Peskine8e338702019-07-30 20:06:31 +02002190 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002191 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002192 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002193 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002194 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002195 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2196 if( status != PSA_SUCCESS )
2197 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002198 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002199 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002200
2201exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002202 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002203 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002204 return( status );
2205}
2206
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002207psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2208 const psa_key_attributes_t *specified_attributes,
2209 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002210{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002211 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002212 psa_key_slot_t *source_slot = NULL;
2213 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002214 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002215 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002216
Gilles Peskine28f8f302019-07-24 13:30:31 +02002217 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002218 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002219 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002220 goto exit;
2221
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002222 status = psa_validate_optional_attributes( source_slot,
2223 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002224 if( status != PSA_SUCCESS )
2225 goto exit;
2226
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002227 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002228 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002229 if( status != PSA_SUCCESS )
2230 goto exit;
2231
Gilles Peskinedf179142019-07-15 22:02:14 +02002232 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2233 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002234 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002235 if( status != PSA_SUCCESS )
2236 goto exit;
2237
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2239 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002240 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002241 /* Copying to a secure element is not implemented yet. */
2242 status = PSA_ERROR_NOT_SUPPORTED;
2243 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002244 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002245#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002246
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002247 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002248 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002249 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002250
Gilles Peskine011e4282019-06-26 18:34:38 +02002251 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002252exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002253 if( status != PSA_SUCCESS )
2254 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002255 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002256 *target_handle = 0;
2257 }
2258 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002259}
2260
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002261
2262
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002263/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002264/* Message digests */
2265/****************************************************************/
2266
Gilles Peskineb16841e2019-10-10 20:36:12 +02002267#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002268static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002269{
2270 switch( alg )
2271 {
2272#if defined(MBEDTLS_MD2_C)
2273 case PSA_ALG_MD2:
2274 return( &mbedtls_md2_info );
2275#endif
2276#if defined(MBEDTLS_MD4_C)
2277 case PSA_ALG_MD4:
2278 return( &mbedtls_md4_info );
2279#endif
2280#if defined(MBEDTLS_MD5_C)
2281 case PSA_ALG_MD5:
2282 return( &mbedtls_md5_info );
2283#endif
2284#if defined(MBEDTLS_RIPEMD160_C)
2285 case PSA_ALG_RIPEMD160:
2286 return( &mbedtls_ripemd160_info );
2287#endif
2288#if defined(MBEDTLS_SHA1_C)
2289 case PSA_ALG_SHA_1:
2290 return( &mbedtls_sha1_info );
2291#endif
2292#if defined(MBEDTLS_SHA256_C)
2293 case PSA_ALG_SHA_224:
2294 return( &mbedtls_sha224_info );
2295 case PSA_ALG_SHA_256:
2296 return( &mbedtls_sha256_info );
2297#endif
2298#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002299#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002300 case PSA_ALG_SHA_384:
2301 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002302#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002303 case PSA_ALG_SHA_512:
2304 return( &mbedtls_sha512_info );
2305#endif
2306 default:
2307 return( NULL );
2308 }
2309}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002310#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002311
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002312psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2313{
2314 switch( operation->alg )
2315 {
Gilles Peskine81736312018-06-26 15:04:31 +02002316 case 0:
2317 /* The object has (apparently) been initialized but it is not
2318 * in use. It's ok to call abort on such an object, and there's
2319 * nothing to do. */
2320 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321#if defined(MBEDTLS_MD2_C)
2322 case PSA_ALG_MD2:
2323 mbedtls_md2_free( &operation->ctx.md2 );
2324 break;
2325#endif
2326#if defined(MBEDTLS_MD4_C)
2327 case PSA_ALG_MD4:
2328 mbedtls_md4_free( &operation->ctx.md4 );
2329 break;
2330#endif
2331#if defined(MBEDTLS_MD5_C)
2332 case PSA_ALG_MD5:
2333 mbedtls_md5_free( &operation->ctx.md5 );
2334 break;
2335#endif
2336#if defined(MBEDTLS_RIPEMD160_C)
2337 case PSA_ALG_RIPEMD160:
2338 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2339 break;
2340#endif
2341#if defined(MBEDTLS_SHA1_C)
2342 case PSA_ALG_SHA_1:
2343 mbedtls_sha1_free( &operation->ctx.sha1 );
2344 break;
2345#endif
2346#if defined(MBEDTLS_SHA256_C)
2347 case PSA_ALG_SHA_224:
2348 case PSA_ALG_SHA_256:
2349 mbedtls_sha256_free( &operation->ctx.sha256 );
2350 break;
2351#endif
2352#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002353#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002354 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002355#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002356 case PSA_ALG_SHA_512:
2357 mbedtls_sha512_free( &operation->ctx.sha512 );
2358 break;
2359#endif
2360 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002361 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002362 }
2363 operation->alg = 0;
2364 return( PSA_SUCCESS );
2365}
2366
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002367psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002368 psa_algorithm_t alg )
2369{
Janos Follath24eed8d2019-11-22 13:21:35 +00002370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002371
2372 /* A context must be freshly initialized before it can be set up. */
2373 if( operation->alg != 0 )
2374 {
2375 return( PSA_ERROR_BAD_STATE );
2376 }
2377
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002378 switch( alg )
2379 {
2380#if defined(MBEDTLS_MD2_C)
2381 case PSA_ALG_MD2:
2382 mbedtls_md2_init( &operation->ctx.md2 );
2383 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2384 break;
2385#endif
2386#if defined(MBEDTLS_MD4_C)
2387 case PSA_ALG_MD4:
2388 mbedtls_md4_init( &operation->ctx.md4 );
2389 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2390 break;
2391#endif
2392#if defined(MBEDTLS_MD5_C)
2393 case PSA_ALG_MD5:
2394 mbedtls_md5_init( &operation->ctx.md5 );
2395 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2396 break;
2397#endif
2398#if defined(MBEDTLS_RIPEMD160_C)
2399 case PSA_ALG_RIPEMD160:
2400 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2401 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2402 break;
2403#endif
2404#if defined(MBEDTLS_SHA1_C)
2405 case PSA_ALG_SHA_1:
2406 mbedtls_sha1_init( &operation->ctx.sha1 );
2407 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2408 break;
2409#endif
2410#if defined(MBEDTLS_SHA256_C)
2411 case PSA_ALG_SHA_224:
2412 mbedtls_sha256_init( &operation->ctx.sha256 );
2413 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2414 break;
2415 case PSA_ALG_SHA_256:
2416 mbedtls_sha256_init( &operation->ctx.sha256 );
2417 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2418 break;
2419#endif
2420#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002421#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002422 case PSA_ALG_SHA_384:
2423 mbedtls_sha512_init( &operation->ctx.sha512 );
2424 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2425 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002426#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002427 case PSA_ALG_SHA_512:
2428 mbedtls_sha512_init( &operation->ctx.sha512 );
2429 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2430 break;
2431#endif
2432 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002433 return( PSA_ALG_IS_HASH( alg ) ?
2434 PSA_ERROR_NOT_SUPPORTED :
2435 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002436 }
2437 if( ret == 0 )
2438 operation->alg = alg;
2439 else
2440 psa_hash_abort( operation );
2441 return( mbedtls_to_psa_error( ret ) );
2442}
2443
2444psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2445 const uint8_t *input,
2446 size_t input_length )
2447{
Janos Follath24eed8d2019-11-22 13:21:35 +00002448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002449
2450 /* Don't require hash implementations to behave correctly on a
2451 * zero-length input, which may have an invalid pointer. */
2452 if( input_length == 0 )
2453 return( PSA_SUCCESS );
2454
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002455 switch( operation->alg )
2456 {
2457#if defined(MBEDTLS_MD2_C)
2458 case PSA_ALG_MD2:
2459 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2460 input, input_length );
2461 break;
2462#endif
2463#if defined(MBEDTLS_MD4_C)
2464 case PSA_ALG_MD4:
2465 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2466 input, input_length );
2467 break;
2468#endif
2469#if defined(MBEDTLS_MD5_C)
2470 case PSA_ALG_MD5:
2471 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2472 input, input_length );
2473 break;
2474#endif
2475#if defined(MBEDTLS_RIPEMD160_C)
2476 case PSA_ALG_RIPEMD160:
2477 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2478 input, input_length );
2479 break;
2480#endif
2481#if defined(MBEDTLS_SHA1_C)
2482 case PSA_ALG_SHA_1:
2483 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2484 input, input_length );
2485 break;
2486#endif
2487#if defined(MBEDTLS_SHA256_C)
2488 case PSA_ALG_SHA_224:
2489 case PSA_ALG_SHA_256:
2490 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2491 input, input_length );
2492 break;
2493#endif
2494#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002495#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002496 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002497#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002498 case PSA_ALG_SHA_512:
2499 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2500 input, input_length );
2501 break;
2502#endif
2503 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002504 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002505 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002506
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002507 if( ret != 0 )
2508 psa_hash_abort( operation );
2509 return( mbedtls_to_psa_error( ret ) );
2510}
2511
2512psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2513 uint8_t *hash,
2514 size_t hash_size,
2515 size_t *hash_length )
2516{
itayzafrir40835d42018-08-02 13:14:17 +03002517 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002519 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002520
2521 /* Fill the output buffer with something that isn't a valid hash
2522 * (barring an attack on the hash and deliberately-crafted input),
2523 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002524 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002525 /* If hash_size is 0 then hash may be NULL and then the
2526 * call to memset would have undefined behavior. */
2527 if( hash_size != 0 )
2528 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002529
2530 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002531 {
2532 status = PSA_ERROR_BUFFER_TOO_SMALL;
2533 goto exit;
2534 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002535
2536 switch( operation->alg )
2537 {
2538#if defined(MBEDTLS_MD2_C)
2539 case PSA_ALG_MD2:
2540 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2541 break;
2542#endif
2543#if defined(MBEDTLS_MD4_C)
2544 case PSA_ALG_MD4:
2545 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2546 break;
2547#endif
2548#if defined(MBEDTLS_MD5_C)
2549 case PSA_ALG_MD5:
2550 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2551 break;
2552#endif
2553#if defined(MBEDTLS_RIPEMD160_C)
2554 case PSA_ALG_RIPEMD160:
2555 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2556 break;
2557#endif
2558#if defined(MBEDTLS_SHA1_C)
2559 case PSA_ALG_SHA_1:
2560 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2561 break;
2562#endif
2563#if defined(MBEDTLS_SHA256_C)
2564 case PSA_ALG_SHA_224:
2565 case PSA_ALG_SHA_256:
2566 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2567 break;
2568#endif
2569#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002570#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002571 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002572#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002573 case PSA_ALG_SHA_512:
2574 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2575 break;
2576#endif
2577 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002578 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002579 }
itayzafrir40835d42018-08-02 13:14:17 +03002580 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002581
itayzafrir40835d42018-08-02 13:14:17 +03002582exit:
2583 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002584 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002585 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002586 return( psa_hash_abort( operation ) );
2587 }
2588 else
2589 {
2590 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002591 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002592 }
2593}
2594
Gilles Peskine2d277862018-06-18 15:41:12 +02002595psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2596 const uint8_t *hash,
2597 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002598{
2599 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2600 size_t actual_hash_length;
2601 psa_status_t status = psa_hash_finish( operation,
2602 actual_hash, sizeof( actual_hash ),
2603 &actual_hash_length );
2604 if( status != PSA_SUCCESS )
2605 return( status );
2606 if( actual_hash_length != hash_length )
2607 return( PSA_ERROR_INVALID_SIGNATURE );
2608 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2609 return( PSA_ERROR_INVALID_SIGNATURE );
2610 return( PSA_SUCCESS );
2611}
2612
Gilles Peskine0a749c82019-11-28 19:33:58 +01002613psa_status_t psa_hash_compute( psa_algorithm_t alg,
2614 const uint8_t *input, size_t input_length,
2615 uint8_t *hash, size_t hash_size,
2616 size_t *hash_length )
2617{
2618 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2619 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2620
2621 *hash_length = hash_size;
2622 status = psa_hash_setup( &operation, alg );
2623 if( status != PSA_SUCCESS )
2624 goto exit;
2625 status = psa_hash_update( &operation, input, input_length );
2626 if( status != PSA_SUCCESS )
2627 goto exit;
2628 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2629 if( status != PSA_SUCCESS )
2630 goto exit;
2631
2632exit:
2633 if( status == PSA_SUCCESS )
2634 status = psa_hash_abort( &operation );
2635 else
2636 psa_hash_abort( &operation );
2637 return( status );
2638}
2639
2640psa_status_t psa_hash_compare( psa_algorithm_t alg,
2641 const uint8_t *input, size_t input_length,
2642 const uint8_t *hash, size_t hash_length )
2643{
2644 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2645 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2646
2647 status = psa_hash_setup( &operation, alg );
2648 if( status != PSA_SUCCESS )
2649 goto exit;
2650 status = psa_hash_update( &operation, input, input_length );
2651 if( status != PSA_SUCCESS )
2652 goto exit;
2653 status = psa_hash_verify( &operation, hash, hash_length );
2654 if( status != PSA_SUCCESS )
2655 goto exit;
2656
2657exit:
2658 if( status == PSA_SUCCESS )
2659 status = psa_hash_abort( &operation );
2660 else
2661 psa_hash_abort( &operation );
2662 return( status );
2663}
2664
Gilles Peskineeb35d782019-01-22 17:56:16 +01002665psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2666 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002667{
2668 if( target_operation->alg != 0 )
2669 return( PSA_ERROR_BAD_STATE );
2670
2671 switch( source_operation->alg )
2672 {
2673 case 0:
2674 return( PSA_ERROR_BAD_STATE );
2675#if defined(MBEDTLS_MD2_C)
2676 case PSA_ALG_MD2:
2677 mbedtls_md2_clone( &target_operation->ctx.md2,
2678 &source_operation->ctx.md2 );
2679 break;
2680#endif
2681#if defined(MBEDTLS_MD4_C)
2682 case PSA_ALG_MD4:
2683 mbedtls_md4_clone( &target_operation->ctx.md4,
2684 &source_operation->ctx.md4 );
2685 break;
2686#endif
2687#if defined(MBEDTLS_MD5_C)
2688 case PSA_ALG_MD5:
2689 mbedtls_md5_clone( &target_operation->ctx.md5,
2690 &source_operation->ctx.md5 );
2691 break;
2692#endif
2693#if defined(MBEDTLS_RIPEMD160_C)
2694 case PSA_ALG_RIPEMD160:
2695 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2696 &source_operation->ctx.ripemd160 );
2697 break;
2698#endif
2699#if defined(MBEDTLS_SHA1_C)
2700 case PSA_ALG_SHA_1:
2701 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2702 &source_operation->ctx.sha1 );
2703 break;
2704#endif
2705#if defined(MBEDTLS_SHA256_C)
2706 case PSA_ALG_SHA_224:
2707 case PSA_ALG_SHA_256:
2708 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2709 &source_operation->ctx.sha256 );
2710 break;
2711#endif
2712#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002713#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002714 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002715#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002716 case PSA_ALG_SHA_512:
2717 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2718 &source_operation->ctx.sha512 );
2719 break;
2720#endif
2721 default:
2722 return( PSA_ERROR_NOT_SUPPORTED );
2723 }
2724
2725 target_operation->alg = source_operation->alg;
2726 return( PSA_SUCCESS );
2727}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002728
2729
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002730/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002731/* MAC */
2732/****************************************************************/
2733
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002734static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002735 psa_algorithm_t alg,
2736 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002737 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002738 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002739{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002740 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002741 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002742
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002743 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002744 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002745
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2747 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002748 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002749 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002750 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002751 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752 mode = MBEDTLS_MODE_STREAM;
2753 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002754 case PSA_ALG_CTR:
2755 mode = MBEDTLS_MODE_CTR;
2756 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002757 case PSA_ALG_CFB:
2758 mode = MBEDTLS_MODE_CFB;
2759 break;
2760 case PSA_ALG_OFB:
2761 mode = MBEDTLS_MODE_OFB;
2762 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002763 case PSA_ALG_ECB_NO_PADDING:
2764 mode = MBEDTLS_MODE_ECB;
2765 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002766 case PSA_ALG_CBC_NO_PADDING:
2767 mode = MBEDTLS_MODE_CBC;
2768 break;
2769 case PSA_ALG_CBC_PKCS7:
2770 mode = MBEDTLS_MODE_CBC;
2771 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002772 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002773 mode = MBEDTLS_MODE_CCM;
2774 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002775 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002776 mode = MBEDTLS_MODE_GCM;
2777 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002778 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2779 mode = MBEDTLS_MODE_CHACHAPOLY;
2780 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002781 default:
2782 return( NULL );
2783 }
2784 }
2785 else if( alg == PSA_ALG_CMAC )
2786 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002787 else
2788 return( NULL );
2789
2790 switch( key_type )
2791 {
2792 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002793 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002794 break;
2795 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002796 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2797 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002798 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002799 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002800 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002801 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002802 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2803 * but two-key Triple-DES is functionally three-key Triple-DES
2804 * with K1=K3, so that's how we present it to mbedtls. */
2805 if( key_bits == 128 )
2806 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002807 break;
2808 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002809 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002810 break;
2811 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002812 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002813 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002814 case PSA_KEY_TYPE_CHACHA20:
2815 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2816 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002817 default:
2818 return( NULL );
2819 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002820 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002821 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002822
Jaeden Amero23bbb752018-06-26 14:16:54 +01002823 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2824 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002825}
2826
Gilles Peskinea05219c2018-11-16 16:02:56 +01002827#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002828static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002829{
Gilles Peskine2d277862018-06-18 15:41:12 +02002830 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002831 {
2832 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002833 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002834 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002835 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002836 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002837 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002838 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002839 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002840 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002841 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002842 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002843 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002844 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002845 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002846 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002847 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002848 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002849 return( 128 );
2850 default:
2851 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002852 }
2853}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002854#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002855
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002856/* Initialize the MAC operation structure. Once this function has been
2857 * called, psa_mac_abort can run and will do the right thing. */
2858static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2859 psa_algorithm_t alg )
2860{
2861 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2862
2863 operation->alg = alg;
2864 operation->key_set = 0;
2865 operation->iv_set = 0;
2866 operation->iv_required = 0;
2867 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002868 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002869
2870#if defined(MBEDTLS_CMAC_C)
2871 if( alg == PSA_ALG_CMAC )
2872 {
2873 operation->iv_required = 0;
2874 mbedtls_cipher_init( &operation->ctx.cmac );
2875 status = PSA_SUCCESS;
2876 }
2877 else
2878#endif /* MBEDTLS_CMAC_C */
2879#if defined(MBEDTLS_MD_C)
2880 if( PSA_ALG_IS_HMAC( operation->alg ) )
2881 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002882 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2883 operation->ctx.hmac.hash_ctx.alg = 0;
2884 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002885 }
2886 else
2887#endif /* MBEDTLS_MD_C */
2888 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002889 if( ! PSA_ALG_IS_MAC( alg ) )
2890 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002891 }
2892
2893 if( status != PSA_SUCCESS )
2894 memset( operation, 0, sizeof( *operation ) );
2895 return( status );
2896}
2897
Gilles Peskine01126fa2018-07-12 17:04:55 +02002898#if defined(MBEDTLS_MD_C)
2899static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2900{
Gilles Peskine3f108122018-12-07 18:14:53 +01002901 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002902 return( psa_hash_abort( &hmac->hash_ctx ) );
2903}
2904#endif /* MBEDTLS_MD_C */
2905
Gilles Peskine8c9def32018-02-08 10:02:12 +01002906psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2907{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002908 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002909 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002910 /* The object has (apparently) been initialized but it is not
2911 * in use. It's ok to call abort on such an object, and there's
2912 * nothing to do. */
2913 return( PSA_SUCCESS );
2914 }
2915 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002916#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002917 if( operation->alg == PSA_ALG_CMAC )
2918 {
2919 mbedtls_cipher_free( &operation->ctx.cmac );
2920 }
2921 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002922#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002923#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002924 if( PSA_ALG_IS_HMAC( operation->alg ) )
2925 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002926 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002927 }
2928 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002929#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002930 {
2931 /* Sanity check (shouldn't happen: operation->alg should
2932 * always have been initialized to a valid value). */
2933 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002934 }
Moran Peker41deec42018-04-04 15:43:05 +03002935
Gilles Peskine8c9def32018-02-08 10:02:12 +01002936 operation->alg = 0;
2937 operation->key_set = 0;
2938 operation->iv_set = 0;
2939 operation->iv_required = 0;
2940 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002941 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002942
Gilles Peskine8c9def32018-02-08 10:02:12 +01002943 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002944
2945bad_state:
2946 /* If abort is called on an uninitialized object, we can't trust
2947 * anything. Wipe the object in case it contains confidential data.
2948 * This may result in a memory leak if a pointer gets overwritten,
2949 * but it's too late to do anything about this. */
2950 memset( operation, 0, sizeof( *operation ) );
2951 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002952}
2953
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002954#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002955static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002956 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002957 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002958 const mbedtls_cipher_info_t *cipher_info )
2959{
Janos Follath24eed8d2019-11-22 13:21:35 +00002960 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002961
2962 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002963
2964 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2965 if( ret != 0 )
2966 return( ret );
2967
2968 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002969 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002970 key_bits );
2971 return( ret );
2972}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002973#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002974
Gilles Peskine248051a2018-06-20 16:09:38 +02002975#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002976static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2977 const uint8_t *key,
2978 size_t key_length,
2979 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002980{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002981 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002982 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002983 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002984 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002985 psa_status_t status;
2986
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002987 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2988 * overflow below. This should never trigger if the hash algorithm
2989 * is implemented correctly. */
2990 /* The size checks against the ipad and opad buffers cannot be written
2991 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2992 * because that triggers -Wlogical-op on GCC 7.3. */
2993 if( block_size > sizeof( ipad ) )
2994 return( PSA_ERROR_NOT_SUPPORTED );
2995 if( block_size > sizeof( hmac->opad ) )
2996 return( PSA_ERROR_NOT_SUPPORTED );
2997 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002998 return( PSA_ERROR_NOT_SUPPORTED );
2999
Gilles Peskined223b522018-06-11 18:12:58 +02003000 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003001 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003002 status = psa_hash_compute( hash_alg, key, key_length,
3003 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003004 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003005 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003006 }
Gilles Peskine96889972018-07-12 17:07:03 +02003007 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3008 * but it is permitted. It is common when HMAC is used in HKDF, for
3009 * example. Don't call `memcpy` in the 0-length because `key` could be
3010 * an invalid pointer which would make the behavior undefined. */
3011 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003012 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003013
Gilles Peskined223b522018-06-11 18:12:58 +02003014 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3015 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003016 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003017 ipad[i] ^= 0x36;
3018 memset( ipad + key_length, 0x36, block_size - key_length );
3019
3020 /* Copy the key material from ipad to opad, flipping the requisite bits,
3021 * and filling the rest of opad with the requisite constant. */
3022 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003023 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3024 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003025
Gilles Peskine01126fa2018-07-12 17:04:55 +02003026 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003027 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003028 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003029
Gilles Peskine01126fa2018-07-12 17:04:55 +02003030 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003031
3032cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003033 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003034
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003035 return( status );
3036}
Gilles Peskine248051a2018-06-20 16:09:38 +02003037#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003038
Gilles Peskine89167cb2018-07-08 20:12:23 +02003039static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003040 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003041 psa_algorithm_t alg,
3042 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003044 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003045 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003046 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003047 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003048 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003049 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003050 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003051
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003052 /* A context must be freshly initialized before it can be set up. */
3053 if( operation->alg != 0 )
3054 {
3055 return( PSA_ERROR_BAD_STATE );
3056 }
3057
Gilles Peskined911eb72018-08-14 15:18:45 +02003058 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003059 if( status != PSA_SUCCESS )
3060 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003061 if( is_sign )
3062 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003063
Gilles Peskine28f8f302019-07-24 13:30:31 +02003064 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003065 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003066 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003067 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003068
Gilles Peskine8c9def32018-02-08 10:02:12 +01003069#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003070 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003071 {
3072 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003073 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003074 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003076 if( cipher_info == NULL )
3077 {
3078 status = PSA_ERROR_NOT_SUPPORTED;
3079 goto exit;
3080 }
3081 operation->mac_size = cipher_info->block_size;
3082 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3083 status = mbedtls_to_psa_error( ret );
3084 }
3085 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003086#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003087#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003088 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003089 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003090 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003091 if( hash_alg == 0 )
3092 {
3093 status = PSA_ERROR_NOT_SUPPORTED;
3094 goto exit;
3095 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003096
3097 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3098 /* Sanity check. This shouldn't fail on a valid configuration. */
3099 if( operation->mac_size == 0 ||
3100 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3101 {
3102 status = PSA_ERROR_NOT_SUPPORTED;
3103 goto exit;
3104 }
3105
Gilles Peskine8e338702019-07-30 20:06:31 +02003106 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003107 {
3108 status = PSA_ERROR_INVALID_ARGUMENT;
3109 goto exit;
3110 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003111
Gilles Peskine01126fa2018-07-12 17:04:55 +02003112 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003113 slot->data.key.data,
3114 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003115 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003116 }
3117 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003118#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003119 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003120 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003121 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003122 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003123
Gilles Peskined911eb72018-08-14 15:18:45 +02003124 if( truncated == 0 )
3125 {
3126 /* The "normal" case: untruncated algorithm. Nothing to do. */
3127 }
3128 else if( truncated < 4 )
3129 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003130 /* A very short MAC is too short for security since it can be
3131 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3132 * so we make this our minimum, even though 32 bits is still
3133 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003134 status = PSA_ERROR_NOT_SUPPORTED;
3135 }
3136 else if( truncated > operation->mac_size )
3137 {
3138 /* It's impossible to "truncate" to a larger length. */
3139 status = PSA_ERROR_INVALID_ARGUMENT;
3140 }
3141 else
3142 operation->mac_size = truncated;
3143
Gilles Peskinefbfac682018-07-08 20:51:54 +02003144exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003145 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003146 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003147 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003148 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003149 else
3150 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003151 operation->key_set = 1;
3152 }
3153 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003154}
3155
Gilles Peskine89167cb2018-07-08 20:12:23 +02003156psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003157 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003158 psa_algorithm_t alg )
3159{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003160 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003161}
3162
3163psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003164 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003165 psa_algorithm_t alg )
3166{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003167 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003168}
3169
Gilles Peskine8c9def32018-02-08 10:02:12 +01003170psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3171 const uint8_t *input,
3172 size_t input_length )
3173{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003174 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003175 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003176 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003177 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003178 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003179 operation->has_input = 1;
3180
Gilles Peskine8c9def32018-02-08 10:02:12 +01003181#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003182 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003183 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003184 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3185 input, input_length );
3186 status = mbedtls_to_psa_error( ret );
3187 }
3188 else
3189#endif /* MBEDTLS_CMAC_C */
3190#if defined(MBEDTLS_MD_C)
3191 if( PSA_ALG_IS_HMAC( operation->alg ) )
3192 {
3193 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3194 input_length );
3195 }
3196 else
3197#endif /* MBEDTLS_MD_C */
3198 {
3199 /* This shouldn't happen if `operation` was initialized by
3200 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003201 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003202 }
3203
Gilles Peskinefbfac682018-07-08 20:51:54 +02003204 if( status != PSA_SUCCESS )
3205 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003206 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003207}
3208
Gilles Peskine01126fa2018-07-12 17:04:55 +02003209#if defined(MBEDTLS_MD_C)
3210static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3211 uint8_t *mac,
3212 size_t mac_size )
3213{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003214 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003215 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3216 size_t hash_size = 0;
3217 size_t block_size = psa_get_hash_block_size( hash_alg );
3218 psa_status_t status;
3219
Gilles Peskine01126fa2018-07-12 17:04:55 +02003220 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3221 if( status != PSA_SUCCESS )
3222 return( status );
3223 /* From here on, tmp needs to be wiped. */
3224
3225 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3226 if( status != PSA_SUCCESS )
3227 goto exit;
3228
3229 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3230 if( status != PSA_SUCCESS )
3231 goto exit;
3232
3233 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3234 if( status != PSA_SUCCESS )
3235 goto exit;
3236
Gilles Peskined911eb72018-08-14 15:18:45 +02003237 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3238 if( status != PSA_SUCCESS )
3239 goto exit;
3240
3241 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003242
3243exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003244 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003245 return( status );
3246}
3247#endif /* MBEDTLS_MD_C */
3248
mohammad16036df908f2018-04-02 08:34:15 -07003249static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003250 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003251 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003252{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003253 if( ! operation->key_set )
3254 return( PSA_ERROR_BAD_STATE );
3255 if( operation->iv_required && ! operation->iv_set )
3256 return( PSA_ERROR_BAD_STATE );
3257
Gilles Peskine8c9def32018-02-08 10:02:12 +01003258 if( mac_size < operation->mac_size )
3259 return( PSA_ERROR_BUFFER_TOO_SMALL );
3260
Gilles Peskine8c9def32018-02-08 10:02:12 +01003261#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003262 if( operation->alg == PSA_ALG_CMAC )
3263 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003264 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3265 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3266 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003267 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003268 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003269 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003270 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003271 else
3272#endif /* MBEDTLS_CMAC_C */
3273#if defined(MBEDTLS_MD_C)
3274 if( PSA_ALG_IS_HMAC( operation->alg ) )
3275 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003276 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003277 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003278 }
3279 else
3280#endif /* MBEDTLS_MD_C */
3281 {
3282 /* This shouldn't happen if `operation` was initialized by
3283 * a setup function. */
3284 return( PSA_ERROR_BAD_STATE );
3285 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003286}
3287
Gilles Peskineacd4be32018-07-08 19:56:25 +02003288psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3289 uint8_t *mac,
3290 size_t mac_size,
3291 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003292{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003293 psa_status_t status;
3294
Jaeden Amero252ef282019-02-15 14:05:35 +00003295 if( operation->alg == 0 )
3296 {
3297 return( PSA_ERROR_BAD_STATE );
3298 }
3299
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003300 /* Fill the output buffer with something that isn't a valid mac
3301 * (barring an attack on the mac and deliberately-crafted input),
3302 * in case the caller doesn't check the return status properly. */
3303 *mac_length = mac_size;
3304 /* If mac_size is 0 then mac may be NULL and then the
3305 * call to memset would have undefined behavior. */
3306 if( mac_size != 0 )
3307 memset( mac, '!', mac_size );
3308
Gilles Peskine89167cb2018-07-08 20:12:23 +02003309 if( ! operation->is_sign )
3310 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003311 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003312 }
mohammad16036df908f2018-04-02 08:34:15 -07003313
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003314 status = psa_mac_finish_internal( operation, mac, mac_size );
3315
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003316 if( status == PSA_SUCCESS )
3317 {
3318 status = psa_mac_abort( operation );
3319 if( status == PSA_SUCCESS )
3320 *mac_length = operation->mac_size;
3321 else
3322 memset( mac, '!', mac_size );
3323 }
3324 else
3325 psa_mac_abort( operation );
3326 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003327}
3328
Gilles Peskineacd4be32018-07-08 19:56:25 +02003329psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3330 const uint8_t *mac,
3331 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003332{
Gilles Peskine828ed142018-06-18 23:25:51 +02003333 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003334 psa_status_t status;
3335
Jaeden Amero252ef282019-02-15 14:05:35 +00003336 if( operation->alg == 0 )
3337 {
3338 return( PSA_ERROR_BAD_STATE );
3339 }
3340
Gilles Peskine89167cb2018-07-08 20:12:23 +02003341 if( operation->is_sign )
3342 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003343 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003344 }
3345 if( operation->mac_size != mac_length )
3346 {
3347 status = PSA_ERROR_INVALID_SIGNATURE;
3348 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003349 }
mohammad16036df908f2018-04-02 08:34:15 -07003350
3351 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003352 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003353 if( status != PSA_SUCCESS )
3354 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003355
3356 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3357 status = PSA_ERROR_INVALID_SIGNATURE;
3358
3359cleanup:
3360 if( status == PSA_SUCCESS )
3361 status = psa_mac_abort( operation );
3362 else
3363 psa_mac_abort( operation );
3364
Gilles Peskine3f108122018-12-07 18:14:53 +01003365 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003366
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003367 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003368}
3369
3370
Gilles Peskine20035e32018-02-03 22:44:14 +01003371
Gilles Peskine20035e32018-02-03 22:44:14 +01003372/****************************************************************/
3373/* Asymmetric cryptography */
3374/****************************************************************/
3375
Gilles Peskine2b450e32018-06-27 15:42:46 +02003376#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003377/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003378 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003379static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3380 size_t hash_length,
3381 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003382{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003383 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003384 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003385 *md_alg = mbedtls_md_get_type( md_info );
3386
3387 /* The Mbed TLS RSA module uses an unsigned int for hash length
3388 * parameters. Validate that it fits so that we don't risk an
3389 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003390#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003391 if( hash_length > UINT_MAX )
3392 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003393#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003394
3395#if defined(MBEDTLS_PKCS1_V15)
3396 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3397 * must be correct. */
3398 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3399 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003400 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003401 if( md_info == NULL )
3402 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003403 if( mbedtls_md_get_size( md_info ) != hash_length )
3404 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003405 }
3406#endif /* MBEDTLS_PKCS1_V15 */
3407
3408#if defined(MBEDTLS_PKCS1_V21)
3409 /* PSS requires a hash internally. */
3410 if( PSA_ALG_IS_RSA_PSS( alg ) )
3411 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003412 if( md_info == NULL )
3413 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003414 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003415#endif /* MBEDTLS_PKCS1_V21 */
3416
Gilles Peskine61b91d42018-06-08 16:09:36 +02003417 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003418}
3419
Gilles Peskine2b450e32018-06-27 15:42:46 +02003420static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3421 psa_algorithm_t alg,
3422 const uint8_t *hash,
3423 size_t hash_length,
3424 uint8_t *signature,
3425 size_t signature_size,
3426 size_t *signature_length )
3427{
3428 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003430 mbedtls_md_type_t md_alg;
3431
3432 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3433 if( status != PSA_SUCCESS )
3434 return( status );
3435
Gilles Peskine630a18a2018-06-29 17:49:35 +02003436 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003437 return( PSA_ERROR_BUFFER_TOO_SMALL );
3438
3439#if defined(MBEDTLS_PKCS1_V15)
3440 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3441 {
3442 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3443 MBEDTLS_MD_NONE );
3444 ret = mbedtls_rsa_pkcs1_sign( rsa,
3445 mbedtls_ctr_drbg_random,
3446 &global_data.ctr_drbg,
3447 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003448 md_alg,
3449 (unsigned int) hash_length,
3450 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003451 signature );
3452 }
3453 else
3454#endif /* MBEDTLS_PKCS1_V15 */
3455#if defined(MBEDTLS_PKCS1_V21)
3456 if( PSA_ALG_IS_RSA_PSS( alg ) )
3457 {
3458 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3459 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3460 mbedtls_ctr_drbg_random,
3461 &global_data.ctr_drbg,
3462 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003463 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003464 (unsigned int) hash_length,
3465 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003466 signature );
3467 }
3468 else
3469#endif /* MBEDTLS_PKCS1_V21 */
3470 {
3471 return( PSA_ERROR_INVALID_ARGUMENT );
3472 }
3473
3474 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003475 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003476 return( mbedtls_to_psa_error( ret ) );
3477}
3478
3479static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3480 psa_algorithm_t alg,
3481 const uint8_t *hash,
3482 size_t hash_length,
3483 const uint8_t *signature,
3484 size_t signature_length )
3485{
3486 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003487 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003488 mbedtls_md_type_t md_alg;
3489
3490 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3491 if( status != PSA_SUCCESS )
3492 return( status );
3493
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003494 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3495 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003496
3497#if defined(MBEDTLS_PKCS1_V15)
3498 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3499 {
3500 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3501 MBEDTLS_MD_NONE );
3502 ret = mbedtls_rsa_pkcs1_verify( rsa,
3503 mbedtls_ctr_drbg_random,
3504 &global_data.ctr_drbg,
3505 MBEDTLS_RSA_PUBLIC,
3506 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003507 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003508 hash,
3509 signature );
3510 }
3511 else
3512#endif /* MBEDTLS_PKCS1_V15 */
3513#if defined(MBEDTLS_PKCS1_V21)
3514 if( PSA_ALG_IS_RSA_PSS( alg ) )
3515 {
3516 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3517 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3518 mbedtls_ctr_drbg_random,
3519 &global_data.ctr_drbg,
3520 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003521 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003522 (unsigned int) hash_length,
3523 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003524 signature );
3525 }
3526 else
3527#endif /* MBEDTLS_PKCS1_V21 */
3528 {
3529 return( PSA_ERROR_INVALID_ARGUMENT );
3530 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003531
3532 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3533 * the rest of the signature is invalid". This has little use in
3534 * practice and PSA doesn't report this distinction. */
3535 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3536 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003537 return( mbedtls_to_psa_error( ret ) );
3538}
3539#endif /* MBEDTLS_RSA_C */
3540
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003541#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003542/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3543 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3544 * (even though these functions don't modify it). */
3545static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3546 psa_algorithm_t alg,
3547 const uint8_t *hash,
3548 size_t hash_length,
3549 uint8_t *signature,
3550 size_t signature_size,
3551 size_t *signature_length )
3552{
Janos Follath24eed8d2019-11-22 13:21:35 +00003553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003554 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003555 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003556 mbedtls_mpi_init( &r );
3557 mbedtls_mpi_init( &s );
3558
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003559 if( signature_size < 2 * curve_bytes )
3560 {
3561 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3562 goto cleanup;
3563 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003564
Gilles Peskinea05219c2018-11-16 16:02:56 +01003565#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003566 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3567 {
3568 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3569 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3570 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003571 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3572 &ecp->d, hash,
3573 hash_length, md_alg,
3574 mbedtls_ctr_drbg_random,
3575 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003576 }
3577 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003578#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003579 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003580 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003581 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3582 hash, hash_length,
3583 mbedtls_ctr_drbg_random,
3584 &global_data.ctr_drbg ) );
3585 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003586
3587 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3588 signature,
3589 curve_bytes ) );
3590 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3591 signature + curve_bytes,
3592 curve_bytes ) );
3593
3594cleanup:
3595 mbedtls_mpi_free( &r );
3596 mbedtls_mpi_free( &s );
3597 if( ret == 0 )
3598 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003599 return( mbedtls_to_psa_error( ret ) );
3600}
3601
3602static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3603 const uint8_t *hash,
3604 size_t hash_length,
3605 const uint8_t *signature,
3606 size_t signature_length )
3607{
Janos Follath24eed8d2019-11-22 13:21:35 +00003608 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003609 mbedtls_mpi r, s;
3610 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3611 mbedtls_mpi_init( &r );
3612 mbedtls_mpi_init( &s );
3613
3614 if( signature_length != 2 * curve_bytes )
3615 return( PSA_ERROR_INVALID_SIGNATURE );
3616
3617 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3618 signature,
3619 curve_bytes ) );
3620 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3621 signature + curve_bytes,
3622 curve_bytes ) );
3623
Steven Cooremanacda8342020-07-24 23:09:52 +02003624 /* Check whether the public part is loaded. If not, load it. */
3625 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3626 {
3627 MBEDTLS_MPI_CHK(
3628 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3629 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3630 }
3631
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003632 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3633 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003634
3635cleanup:
3636 mbedtls_mpi_free( &r );
3637 mbedtls_mpi_free( &s );
3638 return( mbedtls_to_psa_error( ret ) );
3639}
3640#endif /* MBEDTLS_ECDSA_C */
3641
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003642psa_status_t psa_sign_hash( psa_key_handle_t handle,
3643 psa_algorithm_t alg,
3644 const uint8_t *hash,
3645 size_t hash_length,
3646 uint8_t *signature,
3647 size_t signature_size,
3648 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003649{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003650 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003651 psa_status_t status;
3652
3653 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003654 /* Immediately reject a zero-length signature buffer. This guarantees
3655 * that signature must be a valid pointer. (On the other hand, the hash
3656 * buffer can in principle be empty since it doesn't actually have
3657 * to be a hash.) */
3658 if( signature_size == 0 )
3659 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003660
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003661 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003662 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003663 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003664 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003665 {
3666 status = PSA_ERROR_INVALID_ARGUMENT;
3667 goto exit;
3668 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003669
Steven Cooremancd84cb42020-07-16 20:28:36 +02003670 /* Try any of the available accelerators first */
3671 status = psa_driver_wrapper_sign_hash( slot,
3672 alg,
3673 hash,
3674 hash_length,
3675 signature,
3676 signature_size,
3677 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003678 if( status != PSA_ERROR_NOT_SUPPORTED ||
3679 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003680 goto exit;
3681
Steven Cooreman7a250572020-07-17 16:43:05 +02003682 /* If the operation was not supported by any accelerator, try fallback. */
Gilles Peskine20035e32018-02-03 22:44:14 +01003683#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003684 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003685 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003686 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003687
Steven Cooreman4fed4552020-08-03 14:46:03 +02003688 status = psa_load_rsa_representation( slot->attr.type,
3689 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003690 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003691 &rsa );
3692 if( status != PSA_SUCCESS )
3693 goto exit;
3694
Steven Cooremana2371e52020-07-28 14:30:39 +02003695 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003696 alg,
3697 hash, hash_length,
3698 signature, signature_size,
3699 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003700
Steven Cooremana2371e52020-07-28 14:30:39 +02003701 mbedtls_rsa_free( rsa );
3702 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003703 }
3704 else
3705#endif /* defined(MBEDTLS_RSA_C) */
3706#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003707 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003708 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003709#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003710 if(
3711#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3712 PSA_ALG_IS_ECDSA( alg )
3713#else
3714 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3715#endif
3716 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003717 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003718 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003719 status = psa_load_ecp_representation( slot->attr.type,
3720 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003721 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003722 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003723 if( status != PSA_SUCCESS )
3724 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003725 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003726 alg,
3727 hash, hash_length,
3728 signature, signature_size,
3729 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003730 mbedtls_ecp_keypair_free( ecp );
3731 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003732 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003733 else
3734#endif /* defined(MBEDTLS_ECDSA_C) */
3735 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003736 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003737 }
itayzafrir5c753392018-05-08 11:18:38 +03003738 }
3739 else
3740#endif /* defined(MBEDTLS_ECP_C) */
3741 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003742 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003743 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003744
3745exit:
3746 /* Fill the unused part of the output buffer (the whole buffer on error,
3747 * the trailing part on success) with something that isn't a valid mac
3748 * (barring an attack on the mac and deliberately-crafted input),
3749 * in case the caller doesn't check the return status properly. */
3750 if( status == PSA_SUCCESS )
3751 memset( signature + *signature_length, '!',
3752 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003753 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003754 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003755 /* If signature_size is 0 then we have nothing to do. We must not call
3756 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003757 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003758}
3759
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003760psa_status_t psa_verify_hash( psa_key_handle_t handle,
3761 psa_algorithm_t alg,
3762 const uint8_t *hash,
3763 size_t hash_length,
3764 const uint8_t *signature,
3765 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003766{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003767 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003768 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003769
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003770 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003771 if( status != PSA_SUCCESS )
3772 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003773
Steven Cooreman55ae2172020-07-17 19:46:15 +02003774 /* Try any of the available accelerators first */
3775 status = psa_driver_wrapper_verify_hash( slot,
3776 alg,
3777 hash,
3778 hash_length,
3779 signature,
3780 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003781 if( status != PSA_ERROR_NOT_SUPPORTED ||
3782 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +02003783 return status;
3784
Gilles Peskine61b91d42018-06-08 16:09:36 +02003785#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003786 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003787 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003788 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003789
Steven Cooreman4fed4552020-08-03 14:46:03 +02003790 status = psa_load_rsa_representation( slot->attr.type,
3791 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003792 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003793 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003794 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003795 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02003796
Steven Cooremana2371e52020-07-28 14:30:39 +02003797 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003798 alg,
3799 hash, hash_length,
3800 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003801 mbedtls_rsa_free( rsa );
3802 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003803 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003804 }
3805 else
3806#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003807#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003808 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003809 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003810#if defined(MBEDTLS_ECDSA_C)
3811 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003812 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003813 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003814 status = psa_load_ecp_representation( slot->attr.type,
3815 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003816 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003817 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003818 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003819 return( status );
Steven Cooremana2371e52020-07-28 14:30:39 +02003820 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003821 hash, hash_length,
3822 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003823 mbedtls_ecp_keypair_free( ecp );
3824 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02003825 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02003826 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003827 else
3828#endif /* defined(MBEDTLS_ECDSA_C) */
3829 {
3830 return( PSA_ERROR_INVALID_ARGUMENT );
3831 }
itayzafrir5c753392018-05-08 11:18:38 +03003832 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003833 else
3834#endif /* defined(MBEDTLS_ECP_C) */
3835 {
3836 return( PSA_ERROR_NOT_SUPPORTED );
3837 }
3838}
3839
Gilles Peskine072ac562018-06-30 00:21:29 +02003840#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3841static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3842 mbedtls_rsa_context *rsa )
3843{
3844 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3845 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3846 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3847 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3848}
3849#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3850
Gilles Peskinec5487a82018-12-03 18:08:14 +01003851psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003852 psa_algorithm_t alg,
3853 const uint8_t *input,
3854 size_t input_length,
3855 const uint8_t *salt,
3856 size_t salt_length,
3857 uint8_t *output,
3858 size_t output_size,
3859 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003860{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003861 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003862 psa_status_t status;
3863
Darryl Green5cc689a2018-07-24 15:34:10 +01003864 (void) input;
3865 (void) input_length;
3866 (void) salt;
3867 (void) output;
3868 (void) output_size;
3869
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003870 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003871
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003872 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3873 return( PSA_ERROR_INVALID_ARGUMENT );
3874
Gilles Peskine28f8f302019-07-24 13:30:31 +02003875 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003876 if( status != PSA_SUCCESS )
3877 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003878 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3879 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003880 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003881
3882#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003883 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003884 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003885 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003886 status = psa_load_rsa_representation( slot->attr.type,
3887 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003888 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003889 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003890 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003891 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003892
3893 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003894 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003895 status = PSA_ERROR_BUFFER_TOO_SMALL;
3896 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003897 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003898#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003899 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003900 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003901 status = mbedtls_to_psa_error(
3902 mbedtls_rsa_pkcs1_encrypt( rsa,
3903 mbedtls_ctr_drbg_random,
3904 &global_data.ctr_drbg,
3905 MBEDTLS_RSA_PUBLIC,
3906 input_length,
3907 input,
3908 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003909 }
3910 else
3911#endif /* MBEDTLS_PKCS1_V15 */
3912#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003913 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003914 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003915 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003916 status = mbedtls_to_psa_error(
3917 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3918 mbedtls_ctr_drbg_random,
3919 &global_data.ctr_drbg,
3920 MBEDTLS_RSA_PUBLIC,
3921 salt, salt_length,
3922 input_length,
3923 input,
3924 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003925 }
3926 else
3927#endif /* MBEDTLS_PKCS1_V21 */
3928 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003929 status = PSA_ERROR_INVALID_ARGUMENT;
3930 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003931 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003932rsa_exit:
3933 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003934 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003935
Steven Cooremana2371e52020-07-28 14:30:39 +02003936 mbedtls_rsa_free( rsa );
3937 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003938 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003939 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003940 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003941#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003942 {
3943 return( PSA_ERROR_NOT_SUPPORTED );
3944 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003945}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003946
Gilles Peskinec5487a82018-12-03 18:08:14 +01003947psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003948 psa_algorithm_t alg,
3949 const uint8_t *input,
3950 size_t input_length,
3951 const uint8_t *salt,
3952 size_t salt_length,
3953 uint8_t *output,
3954 size_t output_size,
3955 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003956{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003957 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003958 psa_status_t status;
3959
Darryl Green5cc689a2018-07-24 15:34:10 +01003960 (void) input;
3961 (void) input_length;
3962 (void) salt;
3963 (void) output;
3964 (void) output_size;
3965
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003966 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003967
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003968 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3969 return( PSA_ERROR_INVALID_ARGUMENT );
3970
Gilles Peskine28f8f302019-07-24 13:30:31 +02003971 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003972 if( status != PSA_SUCCESS )
3973 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003974 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003975 return( PSA_ERROR_INVALID_ARGUMENT );
3976
3977#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003978 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003979 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003980 mbedtls_rsa_context *rsa = NULL;
3981 status = psa_load_rsa_representation( slot->attr.type,
3982 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003983 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003984 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003985 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003986 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003987
Steven Cooremana2371e52020-07-28 14:30:39 +02003988 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003989 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003990 status = PSA_ERROR_INVALID_ARGUMENT;
3991 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003992 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003993
3994#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003995 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003996 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003997 status = mbedtls_to_psa_error(
3998 mbedtls_rsa_pkcs1_decrypt( rsa,
3999 mbedtls_ctr_drbg_random,
4000 &global_data.ctr_drbg,
4001 MBEDTLS_RSA_PRIVATE,
4002 output_length,
4003 input,
4004 output,
4005 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004006 }
4007 else
4008#endif /* MBEDTLS_PKCS1_V15 */
4009#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004010 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004011 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004012 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004013 status = mbedtls_to_psa_error(
4014 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4015 mbedtls_ctr_drbg_random,
4016 &global_data.ctr_drbg,
4017 MBEDTLS_RSA_PRIVATE,
4018 salt, salt_length,
4019 output_length,
4020 input,
4021 output,
4022 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004023 }
4024 else
4025#endif /* MBEDTLS_PKCS1_V21 */
4026 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004027 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004028 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004029
Steven Cooreman4fed4552020-08-03 14:46:03 +02004030rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004031 mbedtls_rsa_free( rsa );
4032 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004033 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004034 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004035 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004036#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004037 {
4038 return( PSA_ERROR_NOT_SUPPORTED );
4039 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004040}
Gilles Peskine20035e32018-02-03 22:44:14 +01004041
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004042
4043
mohammad1603503973b2018-03-12 15:59:30 +02004044/****************************************************************/
4045/* Symmetric cryptography */
4046/****************************************************************/
4047
Gilles Peskinee553c652018-06-04 16:22:46 +02004048static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004049 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004050 psa_algorithm_t alg,
4051 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004052{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004053 int ret = 0;
4054 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004055 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004056 size_t key_bits;
4057 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004058 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4059 PSA_KEY_USAGE_ENCRYPT :
4060 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004061
Steven Cooremand3feccd2020-09-01 15:56:14 +02004062 /* A context must be freshly initialized before it can be set up. */
4063 if( operation->alg != 0 )
4064 return( PSA_ERROR_BAD_STATE );
4065
Steven Cooremana07b9972020-09-10 14:54:14 +02004066 /* The requested algorithm must be one that can be processed by cipher. */
4067 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004068 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004069
Steven Cooremanef8575e2020-09-11 11:44:50 +02004070 /* Fetch key material from key storage. */
4071 status = psa_get_key_from_slot( handle, &slot, usage, alg );
4072 if( status != PSA_SUCCESS )
4073 goto exit;
4074
4075 /* Initialize the operation struct members, except for alg. The alg member
4076 * is used to indicate to psa_cipher_abort that there are resources to free,
4077 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004078 operation->key_set = 0;
4079 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004080 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004081 operation->iv_size = 0;
4082 operation->block_size = 0;
4083 if( alg == PSA_ALG_ECB_NO_PADDING )
4084 operation->iv_required = 0;
4085 else
4086 operation->iv_required = 1;
4087
Steven Cooremana07b9972020-09-10 14:54:14 +02004088 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004089 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004090 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004091 slot,
4092 alg );
4093 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004094 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004095 slot,
4096 alg );
4097
Steven Cooremanef8575e2020-09-11 11:44:50 +02004098 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004099 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004100 /* Once the driver context is initialised, it needs to be freed using
4101 * psa_cipher_abort. Indicate this through setting alg. */
4102 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004103 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004104
Steven Cooremand3feccd2020-09-01 15:56:14 +02004105 if( status != PSA_ERROR_NOT_SUPPORTED ||
4106 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4107 goto exit;
4108
Steven Cooremana07b9972020-09-10 14:54:14 +02004109 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004110 * available for the given algorithm & key. */
4111 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004112
Steven Cooremana07b9972020-09-10 14:54:14 +02004113 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004114 * psa_cipher_abort. Indicate there is something to be freed through setting
4115 * alg, and indicate the operation is being done using mbedtls crypto through
4116 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004117 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004118 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004119
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004120 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004121 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004122 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004123 {
4124 status = PSA_ERROR_NOT_SUPPORTED;
4125 goto exit;
4126 }
mohammad1603503973b2018-03-12 15:59:30 +02004127
mohammad1603503973b2018-03-12 15:59:30 +02004128 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004129 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004130 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004131
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004132#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004133 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004134 {
4135 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004136 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004137 memcpy( keys, slot->data.key.data, 16 );
4138 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004139 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4140 keys,
4141 192, cipher_operation );
4142 }
4143 else
4144#endif
4145 {
4146 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004147 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004148 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004149 }
Moran Peker41deec42018-04-04 15:43:05 +03004150 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004151 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004152
mohammad16038481e742018-03-18 13:57:31 +02004153#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004154 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004155 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004156 case PSA_ALG_CBC_NO_PADDING:
4157 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4158 MBEDTLS_PADDING_NONE );
4159 break;
4160 case PSA_ALG_CBC_PKCS7:
4161 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4162 MBEDTLS_PADDING_PKCS7 );
4163 break;
4164 default:
4165 /* The algorithm doesn't involve padding. */
4166 ret = 0;
4167 break;
4168 }
4169 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004170 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004171#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4172
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004173 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004174 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004175 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4176 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004177 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004178 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004179 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004180#if defined(MBEDTLS_CHACHA20_C)
4181 else
4182 if( alg == PSA_ALG_CHACHA20 )
4183 operation->iv_size = 12;
4184#endif
mohammad1603503973b2018-03-12 15:59:30 +02004185
Steven Cooreman7df02922020-09-09 15:28:49 +02004186 status = PSA_SUCCESS;
4187
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004188exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004189 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004190 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004191 if( status == PSA_SUCCESS )
4192 {
4193 /* Update operation flags for both driver and software implementations */
4194 operation->key_set = 1;
4195 }
4196 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004197 psa_cipher_abort( operation );
4198 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004199}
4200
Gilles Peskinefe119512018-07-08 21:39:34 +02004201psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004202 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004203 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004204{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004205 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004206}
4207
Gilles Peskinefe119512018-07-08 21:39:34 +02004208psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004209 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004210 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004211{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004212 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004213}
4214
Gilles Peskinefe119512018-07-08 21:39:34 +02004215psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004216 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004217 size_t iv_size,
4218 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004219{
itayzafrir534bd7c2018-08-02 13:56:32 +03004220 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004221 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004222 if( operation->iv_set || ! operation->iv_required )
4223 {
4224 return( PSA_ERROR_BAD_STATE );
4225 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004226
Steven Cooremanef8575e2020-09-11 11:44:50 +02004227 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004228 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004229 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004230 iv,
4231 iv_size,
4232 iv_length );
4233 goto exit;
4234 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004235
Moran Peker41deec42018-04-04 15:43:05 +03004236 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004237 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004238 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004239 goto exit;
4240 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004241 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4242 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004243 if( ret != 0 )
4244 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004245 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004246 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004247 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004248
mohammad16038481e742018-03-18 13:57:31 +02004249 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004250 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004251
Moran Peker395db872018-05-31 14:07:14 +03004252exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004253 if( status == PSA_SUCCESS )
4254 operation->iv_set = 1;
4255 else
Moran Peker395db872018-05-31 14:07:14 +03004256 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004257 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004258}
4259
Gilles Peskinefe119512018-07-08 21:39:34 +02004260psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004261 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004262 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004263{
itayzafrir534bd7c2018-08-02 13:56:32 +03004264 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004265 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004266 if( operation->iv_set || ! operation->iv_required )
4267 {
4268 return( PSA_ERROR_BAD_STATE );
4269 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004270
Steven Cooremanef8575e2020-09-11 11:44:50 +02004271 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004272 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004273 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004274 iv,
4275 iv_length );
4276 goto exit;
4277 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004278
Moran Pekera28258c2018-05-29 16:25:04 +03004279 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004280 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004281 status = PSA_ERROR_INVALID_ARGUMENT;
4282 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004283 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004284 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4285 status = mbedtls_to_psa_error( ret );
4286exit:
4287 if( status == PSA_SUCCESS )
4288 operation->iv_set = 1;
4289 else
mohammad1603503973b2018-03-12 15:59:30 +02004290 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004291 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004292}
4293
Steven Cooreman177deba2020-09-07 17:14:14 +02004294/* Process input for which the algorithm is set to ECB mode. This requires
4295 * manual processing, since the PSA API is defined as being able to process
4296 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4297 * underlying mbedtls_cipher_update only takes full blocks. */
4298static psa_status_t psa_cipher_update_ecb_internal(
4299 mbedtls_cipher_context_t *ctx,
4300 const uint8_t *input,
4301 size_t input_length,
4302 uint8_t *output,
4303 size_t output_size,
4304 size_t *output_length )
4305{
4306 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4307 size_t block_size = ctx->cipher_info->block_size;
4308 size_t internal_output_length = 0;
4309 *output_length = 0;
4310
4311 if( input_length == 0 )
4312 {
4313 status = PSA_SUCCESS;
4314 goto exit;
4315 }
4316
4317 if( ctx->unprocessed_len > 0 )
4318 {
4319 /* Fill up to block size, and run the block if there's a full one. */
4320 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4321
4322 if( input_length < bytes_to_copy )
4323 bytes_to_copy = input_length;
4324
4325 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4326 input, bytes_to_copy );
4327 input_length -= bytes_to_copy;
4328 input += bytes_to_copy;
4329 ctx->unprocessed_len += bytes_to_copy;
4330
4331 if( ctx->unprocessed_len == block_size )
4332 {
4333 status = mbedtls_to_psa_error(
4334 mbedtls_cipher_update( ctx,
4335 ctx->unprocessed_data,
4336 block_size,
4337 output, &internal_output_length ) );
4338
4339 if( status != PSA_SUCCESS )
4340 goto exit;
4341
4342 output += internal_output_length;
4343 output_size -= internal_output_length;
4344 *output_length += internal_output_length;
4345 ctx->unprocessed_len = 0;
4346 }
4347 }
4348
4349 while( input_length >= block_size )
4350 {
4351 /* Run all full blocks we have, one by one */
4352 status = mbedtls_to_psa_error(
4353 mbedtls_cipher_update( ctx, input,
4354 block_size,
4355 output, &internal_output_length ) );
4356
4357 if( status != PSA_SUCCESS )
4358 goto exit;
4359
4360 input_length -= block_size;
4361 input += block_size;
4362
4363 output += internal_output_length;
4364 output_size -= internal_output_length;
4365 *output_length += internal_output_length;
4366 }
4367
4368 if( input_length > 0 )
4369 {
4370 /* Save unprocessed bytes for later processing */
4371 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4372 input, input_length );
4373 ctx->unprocessed_len += input_length;
4374 }
4375
4376 status = PSA_SUCCESS;
4377
4378exit:
4379 return( status );
4380}
4381
Gilles Peskinee553c652018-06-04 16:22:46 +02004382psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4383 const uint8_t *input,
4384 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004385 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004386 size_t output_size,
4387 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004388{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004389 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004390 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004391 if( operation->alg == 0 )
4392 {
4393 return( PSA_ERROR_BAD_STATE );
4394 }
4395 if( operation->iv_required && ! operation->iv_set )
4396 {
4397 return( PSA_ERROR_BAD_STATE );
4398 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004399
Steven Cooremanef8575e2020-09-11 11:44:50 +02004400 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004401 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004402 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004403 input,
4404 input_length,
4405 output,
4406 output_size,
4407 output_length );
4408 goto exit;
4409 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004410
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004411 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004412 {
4413 /* Take the unprocessed partial block left over from previous
4414 * update calls, if any, plus the input to this call. Remove
4415 * the last partial block, if any. You get the data that will be
4416 * output in this call. */
4417 expected_output_size =
4418 ( operation->ctx.cipher.unprocessed_len + input_length )
4419 / operation->block_size * operation->block_size;
4420 }
4421 else
4422 {
4423 expected_output_size = input_length;
4424 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004425
Gilles Peskine89d789c2018-06-04 17:17:16 +02004426 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004427 {
4428 status = PSA_ERROR_BUFFER_TOO_SMALL;
4429 goto exit;
4430 }
mohammad160382759612018-03-12 18:16:40 +02004431
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004432 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4433 {
4434 /* mbedtls_cipher_update has an API inconsistency: it will only
4435 * process a single block at a time in ECB mode. Abstract away that
4436 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004437 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4438 input,
4439 input_length,
4440 output,
4441 output_size,
4442 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004443 }
4444 else
4445 {
4446 status = mbedtls_to_psa_error(
4447 mbedtls_cipher_update( &operation->ctx.cipher, input,
4448 input_length, output, output_length ) );
4449 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004450exit:
4451 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004452 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004453 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004454}
4455
Gilles Peskinee553c652018-06-04 16:22:46 +02004456psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4457 uint8_t *output,
4458 size_t output_size,
4459 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004460{
David Saadab4ecc272019-02-14 13:48:10 +02004461 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004462 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004463 if( operation->alg == 0 )
4464 {
4465 return( PSA_ERROR_BAD_STATE );
4466 }
4467 if( operation->iv_required && ! operation->iv_set )
4468 {
4469 return( PSA_ERROR_BAD_STATE );
4470 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004471
Steven Cooremanef8575e2020-09-11 11:44:50 +02004472 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004473 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004474 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004475 output,
4476 output_size,
4477 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004478 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004479 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004480
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004481 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004482 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004483 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
4484 ( operation->alg == PSA_ALG_CBC_NO_PADDING &&
Steven Cooremana6033e92020-08-25 11:47:50 +02004485 operation->ctx.cipher.operation == MBEDTLS_ENCRYPT ) )
4486 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004487 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004488 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004489 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004490 }
4491
Steven Cooremanef8575e2020-09-11 11:44:50 +02004492 status = mbedtls_to_psa_error(
4493 mbedtls_cipher_finish( &operation->ctx.cipher,
4494 temp_output_buffer,
4495 output_length ) );
4496 if( status != PSA_SUCCESS )
4497 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004498
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004499 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004500 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004501 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004502 memcpy( output, temp_output_buffer, *output_length );
4503 else
Janos Follath315b51c2018-07-09 16:04:51 +01004504 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004505
Steven Cooremanef8575e2020-09-11 11:44:50 +02004506exit:
4507 if( operation->mbedtls_in_use == 1 )
4508 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004509
Steven Cooremanef8575e2020-09-11 11:44:50 +02004510 if( status == PSA_SUCCESS )
4511 return( psa_cipher_abort( operation ) );
4512 else
4513 {
4514 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004515 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004516
Steven Cooremanef8575e2020-09-11 11:44:50 +02004517 return( status );
4518 }
mohammad1603503973b2018-03-12 15:59:30 +02004519}
4520
Gilles Peskinee553c652018-06-04 16:22:46 +02004521psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4522{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004523 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004524 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004525 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004526 * in use. It's ok to call abort on such an object, and there's
4527 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004528 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004529 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004530
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004531 /* Sanity check (shouldn't happen: operation->alg should
4532 * always have been initialized to a valid value). */
4533 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4534 return( PSA_ERROR_BAD_STATE );
4535
Steven Cooremanef8575e2020-09-11 11:44:50 +02004536 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004537 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004538 else
4539 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004540
Moran Peker41deec42018-04-04 15:43:05 +03004541 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004542 operation->key_set = 0;
4543 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004544 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004545 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004546 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004547 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004548
Moran Peker395db872018-05-31 14:07:14 +03004549 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004550}
4551
Gilles Peskinea0655c32018-04-30 17:06:50 +02004552
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004553
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004554
mohammad16035955c982018-04-26 00:53:03 +03004555/****************************************************************/
4556/* AEAD */
4557/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004558
Gilles Peskineedf9a652018-08-17 18:11:56 +02004559typedef struct
4560{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004561 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004562 const mbedtls_cipher_info_t *cipher_info;
4563 union
4564 {
4565#if defined(MBEDTLS_CCM_C)
4566 mbedtls_ccm_context ccm;
4567#endif /* MBEDTLS_CCM_C */
4568#if defined(MBEDTLS_GCM_C)
4569 mbedtls_gcm_context gcm;
4570#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004571#if defined(MBEDTLS_CHACHAPOLY_C)
4572 mbedtls_chachapoly_context chachapoly;
4573#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004574 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004575 psa_algorithm_t core_alg;
4576 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004577 uint8_t tag_length;
4578} aead_operation_t;
4579
Gilles Peskine30a9e412019-01-14 18:36:12 +01004580static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004581{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004582 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004583 {
4584#if defined(MBEDTLS_CCM_C)
4585 case PSA_ALG_CCM:
4586 mbedtls_ccm_free( &operation->ctx.ccm );
4587 break;
4588#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004589#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004590 case PSA_ALG_GCM:
4591 mbedtls_gcm_free( &operation->ctx.gcm );
4592 break;
4593#endif /* MBEDTLS_GCM_C */
4594 }
4595}
4596
4597static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004598 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004599 psa_key_usage_t usage,
4600 psa_algorithm_t alg )
4601{
4602 psa_status_t status;
4603 size_t key_bits;
4604 mbedtls_cipher_id_t cipher_id;
4605
Gilles Peskine28f8f302019-07-24 13:30:31 +02004606 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004607 if( status != PSA_SUCCESS )
4608 return( status );
4609
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004610 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004611
4612 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004613 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004614 &cipher_id );
4615 if( operation->cipher_info == NULL )
4616 return( PSA_ERROR_NOT_SUPPORTED );
4617
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004618 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004619 {
4620#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004621 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4622 operation->core_alg = PSA_ALG_CCM;
4623 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004624 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4625 * The call to mbedtls_ccm_encrypt_and_tag or
4626 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004627 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004628 return( PSA_ERROR_INVALID_ARGUMENT );
4629 mbedtls_ccm_init( &operation->ctx.ccm );
4630 status = mbedtls_to_psa_error(
4631 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004632 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004633 (unsigned int) key_bits ) );
4634 if( status != 0 )
4635 goto cleanup;
4636 break;
4637#endif /* MBEDTLS_CCM_C */
4638
4639#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004640 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4641 operation->core_alg = PSA_ALG_GCM;
4642 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004643 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4644 * The call to mbedtls_gcm_crypt_and_tag or
4645 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004646 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004647 return( PSA_ERROR_INVALID_ARGUMENT );
4648 mbedtls_gcm_init( &operation->ctx.gcm );
4649 status = mbedtls_to_psa_error(
4650 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004651 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004652 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004653 if( status != 0 )
4654 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004655 break;
4656#endif /* MBEDTLS_GCM_C */
4657
Gilles Peskine26869f22019-05-06 15:25:00 +02004658#if defined(MBEDTLS_CHACHAPOLY_C)
4659 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4660 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4661 operation->full_tag_length = 16;
4662 /* We only support the default tag length. */
4663 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4664 return( PSA_ERROR_NOT_SUPPORTED );
4665 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4666 status = mbedtls_to_psa_error(
4667 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004668 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004669 if( status != 0 )
4670 goto cleanup;
4671 break;
4672#endif /* MBEDTLS_CHACHAPOLY_C */
4673
Gilles Peskineedf9a652018-08-17 18:11:56 +02004674 default:
4675 return( PSA_ERROR_NOT_SUPPORTED );
4676 }
4677
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004678 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4679 {
4680 status = PSA_ERROR_INVALID_ARGUMENT;
4681 goto cleanup;
4682 }
4683 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004684
Gilles Peskineedf9a652018-08-17 18:11:56 +02004685 return( PSA_SUCCESS );
4686
4687cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004688 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004689 return( status );
4690}
4691
Gilles Peskinec5487a82018-12-03 18:08:14 +01004692psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004693 psa_algorithm_t alg,
4694 const uint8_t *nonce,
4695 size_t nonce_length,
4696 const uint8_t *additional_data,
4697 size_t additional_data_length,
4698 const uint8_t *plaintext,
4699 size_t plaintext_length,
4700 uint8_t *ciphertext,
4701 size_t ciphertext_size,
4702 size_t *ciphertext_length )
4703{
mohammad16035955c982018-04-26 00:53:03 +03004704 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004705 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004706 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004707
mohammad1603f08a5502018-06-03 15:05:47 +03004708 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004709
Gilles Peskinec5487a82018-12-03 18:08:14 +01004710 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004711 if( status != PSA_SUCCESS )
4712 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004713
Gilles Peskineedf9a652018-08-17 18:11:56 +02004714 /* For all currently supported modes, the tag is at the end of the
4715 * ciphertext. */
4716 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4717 {
4718 status = PSA_ERROR_BUFFER_TOO_SMALL;
4719 goto exit;
4720 }
4721 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004722
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004723#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004724 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004725 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004726 status = mbedtls_to_psa_error(
4727 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4728 MBEDTLS_GCM_ENCRYPT,
4729 plaintext_length,
4730 nonce, nonce_length,
4731 additional_data, additional_data_length,
4732 plaintext, ciphertext,
4733 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004734 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004735 else
4736#endif /* MBEDTLS_GCM_C */
4737#if defined(MBEDTLS_CCM_C)
4738 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004739 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004740 status = mbedtls_to_psa_error(
4741 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4742 plaintext_length,
4743 nonce, nonce_length,
4744 additional_data,
4745 additional_data_length,
4746 plaintext, ciphertext,
4747 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004748 }
mohammad16035c8845f2018-05-09 05:40:09 -07004749 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004750#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004751#if defined(MBEDTLS_CHACHAPOLY_C)
4752 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4753 {
4754 if( nonce_length != 12 || operation.tag_length != 16 )
4755 {
4756 status = PSA_ERROR_NOT_SUPPORTED;
4757 goto exit;
4758 }
4759 status = mbedtls_to_psa_error(
4760 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4761 plaintext_length,
4762 nonce,
4763 additional_data,
4764 additional_data_length,
4765 plaintext,
4766 ciphertext,
4767 tag ) );
4768 }
4769 else
4770#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004771 {
mohammad1603554faad2018-06-03 15:07:38 +03004772 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004773 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004774
Gilles Peskineedf9a652018-08-17 18:11:56 +02004775 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4776 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004777
Gilles Peskineedf9a652018-08-17 18:11:56 +02004778exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004779 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004780 if( status == PSA_SUCCESS )
4781 *ciphertext_length = plaintext_length + operation.tag_length;
4782 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004783}
4784
Gilles Peskineee652a32018-06-01 19:23:52 +02004785/* Locate the tag in a ciphertext buffer containing the encrypted data
4786 * followed by the tag. Return the length of the part preceding the tag in
4787 * *plaintext_length. This is the size of the plaintext in modes where
4788 * the encrypted data has the same size as the plaintext, such as
4789 * CCM and GCM. */
4790static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4791 const uint8_t *ciphertext,
4792 size_t ciphertext_length,
4793 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004794 const uint8_t **p_tag )
4795{
4796 size_t payload_length;
4797 if( tag_length > ciphertext_length )
4798 return( PSA_ERROR_INVALID_ARGUMENT );
4799 payload_length = ciphertext_length - tag_length;
4800 if( payload_length > plaintext_size )
4801 return( PSA_ERROR_BUFFER_TOO_SMALL );
4802 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004803 return( PSA_SUCCESS );
4804}
4805
Gilles Peskinec5487a82018-12-03 18:08:14 +01004806psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004807 psa_algorithm_t alg,
4808 const uint8_t *nonce,
4809 size_t nonce_length,
4810 const uint8_t *additional_data,
4811 size_t additional_data_length,
4812 const uint8_t *ciphertext,
4813 size_t ciphertext_length,
4814 uint8_t *plaintext,
4815 size_t plaintext_size,
4816 size_t *plaintext_length )
4817{
mohammad16035955c982018-04-26 00:53:03 +03004818 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004819 aead_operation_t operation;
4820 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004821
Gilles Peskineee652a32018-06-01 19:23:52 +02004822 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004823
Gilles Peskinec5487a82018-12-03 18:08:14 +01004824 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004825 if( status != PSA_SUCCESS )
4826 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004827
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004828 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4829 ciphertext, ciphertext_length,
4830 plaintext_size, &tag );
4831 if( status != PSA_SUCCESS )
4832 goto exit;
4833
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004834#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004835 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004836 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004837 status = mbedtls_to_psa_error(
4838 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4839 ciphertext_length - operation.tag_length,
4840 nonce, nonce_length,
4841 additional_data,
4842 additional_data_length,
4843 tag, operation.tag_length,
4844 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004845 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004846 else
4847#endif /* MBEDTLS_GCM_C */
4848#if defined(MBEDTLS_CCM_C)
4849 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004850 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004851 status = mbedtls_to_psa_error(
4852 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4853 ciphertext_length - operation.tag_length,
4854 nonce, nonce_length,
4855 additional_data,
4856 additional_data_length,
4857 ciphertext, plaintext,
4858 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004859 }
mohammad160339574652018-06-01 04:39:53 -07004860 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004861#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004862#if defined(MBEDTLS_CHACHAPOLY_C)
4863 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4864 {
4865 if( nonce_length != 12 || operation.tag_length != 16 )
4866 {
4867 status = PSA_ERROR_NOT_SUPPORTED;
4868 goto exit;
4869 }
4870 status = mbedtls_to_psa_error(
4871 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4872 ciphertext_length - operation.tag_length,
4873 nonce,
4874 additional_data,
4875 additional_data_length,
4876 tag,
4877 ciphertext,
4878 plaintext ) );
4879 }
4880 else
4881#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004882 {
mohammad1603554faad2018-06-03 15:07:38 +03004883 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004884 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004885
Gilles Peskineedf9a652018-08-17 18:11:56 +02004886 if( status != PSA_SUCCESS && plaintext_size != 0 )
4887 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004888
Gilles Peskineedf9a652018-08-17 18:11:56 +02004889exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004890 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004891 if( status == PSA_SUCCESS )
4892 *plaintext_length = ciphertext_length - operation.tag_length;
4893 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004894}
4895
Gilles Peskinea0655c32018-04-30 17:06:50 +02004896
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004897
Gilles Peskine20035e32018-02-03 22:44:14 +01004898/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004899/* Generators */
4900/****************************************************************/
4901
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004902#define HKDF_STATE_INIT 0 /* no input yet */
4903#define HKDF_STATE_STARTED 1 /* got salt */
4904#define HKDF_STATE_KEYED 2 /* got key */
4905#define HKDF_STATE_OUTPUT 3 /* output started */
4906
Gilles Peskinecbe66502019-05-16 16:59:18 +02004907static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004908 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004909{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4911 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004912 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004913 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004914}
4915
4916
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004917psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004918{
4919 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004921 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004922 {
4923 /* The object has (apparently) been initialized but it is not
4924 * in use. It's ok to call abort on such an object, and there's
4925 * nothing to do. */
4926 }
4927 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004928#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004929 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004930 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 mbedtls_free( operation->ctx.hkdf.info );
4932 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004933 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004934 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004935 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004936 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004937 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004938 if( operation->ctx.tls12_prf.seed != NULL )
4939 {
4940 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4941 operation->ctx.tls12_prf.seed_length );
4942 mbedtls_free( operation->ctx.tls12_prf.seed );
4943 }
4944
4945 if( operation->ctx.tls12_prf.label != NULL )
4946 {
4947 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4948 operation->ctx.tls12_prf.label_length );
4949 mbedtls_free( operation->ctx.tls12_prf.label );
4950 }
4951
4952 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4953
4954 /* We leave the fields Ai and output_block to be erased safely by the
4955 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004956 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004957 else
4958#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004959 {
4960 status = PSA_ERROR_BAD_STATE;
4961 }
Janos Follath083036a2019-06-11 10:22:26 +01004962 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004963 return( status );
4964}
4965
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004967 size_t *capacity)
4968{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004969 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004970 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004972 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004973 }
4974
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004975 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004976 return( PSA_SUCCESS );
4977}
4978
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004979psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004980 size_t capacity )
4981{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004982 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004983 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004984 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004985 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004986 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004987 return( PSA_SUCCESS );
4988}
4989
Gilles Peskinebef7f142018-07-12 17:22:21 +02004990#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004991/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004992 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004993static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004994 psa_algorithm_t hash_alg,
4995 uint8_t *output,
4996 size_t output_length )
4997{
4998 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4999 psa_status_t status;
5000
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005001 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5002 return( PSA_ERROR_BAD_STATE );
5003 hkdf->state = HKDF_STATE_OUTPUT;
5004
Gilles Peskinebef7f142018-07-12 17:22:21 +02005005 while( output_length != 0 )
5006 {
5007 /* Copy what remains of the current block */
5008 uint8_t n = hash_length - hkdf->offset_in_block;
5009 if( n > output_length )
5010 n = (uint8_t) output_length;
5011 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5012 output += n;
5013 output_length -= n;
5014 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005015 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005016 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005017 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005018 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005019 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005020 * object was corrupted or if this function is called directly
5021 * inside the library. */
5022 if( hkdf->block_number == 0xff )
5023 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005024
5025 /* We need a new block */
5026 ++hkdf->block_number;
5027 hkdf->offset_in_block = 0;
5028 status = psa_hmac_setup_internal( &hkdf->hmac,
5029 hkdf->prk, hash_length,
5030 hash_alg );
5031 if( status != PSA_SUCCESS )
5032 return( status );
5033 if( hkdf->block_number != 1 )
5034 {
5035 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5036 hkdf->output_block,
5037 hash_length );
5038 if( status != PSA_SUCCESS )
5039 return( status );
5040 }
5041 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5042 hkdf->info,
5043 hkdf->info_length );
5044 if( status != PSA_SUCCESS )
5045 return( status );
5046 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5047 &hkdf->block_number, 1 );
5048 if( status != PSA_SUCCESS )
5049 return( status );
5050 status = psa_hmac_finish_internal( &hkdf->hmac,
5051 hkdf->output_block,
5052 sizeof( hkdf->output_block ) );
5053 if( status != PSA_SUCCESS )
5054 return( status );
5055 }
5056
5057 return( PSA_SUCCESS );
5058}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005059
Janos Follath7742fee2019-06-17 12:58:10 +01005060static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5061 psa_tls12_prf_key_derivation_t *tls12_prf,
5062 psa_algorithm_t alg )
5063{
5064 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5065 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005066 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5067 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005068
Janos Follath7742fee2019-06-17 12:58:10 +01005069 /* We can't be wanting more output after block 0xff, otherwise
5070 * the capacity check in psa_key_derivation_output_bytes() would have
5071 * prevented this call. It could happen only if the operation
5072 * object was corrupted or if this function is called directly
5073 * inside the library. */
5074 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005075 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005076
5077 /* We need a new block */
5078 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005079 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005080
5081 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5082 *
5083 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5084 *
5085 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5086 * HMAC_hash(secret, A(2) + seed) +
5087 * HMAC_hash(secret, A(3) + seed) + ...
5088 *
5089 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005090 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005091 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005092 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005093 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005094 * is currently extracted as `output_block` and where i is
5095 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005096 */
5097
Janos Follathea29bfb2019-06-19 12:21:20 +01005098 /* Save the hash context before using it, to preserve the hash state with
5099 * only the inner padding in it. We need this, because inner padding depends
5100 * on the key (secret in the RFC's terminology). */
5101 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5102 if( status != PSA_SUCCESS )
5103 goto cleanup;
5104
5105 /* Calculate A(i) where i = tls12_prf->block_number. */
5106 if( tls12_prf->block_number == 1 )
5107 {
5108 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5109 * the variable seed and in this instance means it in the context of the
5110 * P_hash function, where seed = label + seed.) */
5111 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5112 tls12_prf->label, tls12_prf->label_length );
5113 if( status != PSA_SUCCESS )
5114 goto cleanup;
5115 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5116 tls12_prf->seed, tls12_prf->seed_length );
5117 if( status != PSA_SUCCESS )
5118 goto cleanup;
5119 }
5120 else
5121 {
5122 /* A(i) = HMAC_hash(secret, A(i-1)) */
5123 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5124 tls12_prf->Ai, hash_length );
5125 if( status != PSA_SUCCESS )
5126 goto cleanup;
5127 }
5128
5129 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5130 tls12_prf->Ai, hash_length );
5131 if( status != PSA_SUCCESS )
5132 goto cleanup;
5133 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5134 if( status != PSA_SUCCESS )
5135 goto cleanup;
5136
5137 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5138 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5139 tls12_prf->Ai, hash_length );
5140 if( status != PSA_SUCCESS )
5141 goto cleanup;
5142 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5143 tls12_prf->label, tls12_prf->label_length );
5144 if( status != PSA_SUCCESS )
5145 goto cleanup;
5146 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5147 tls12_prf->seed, tls12_prf->seed_length );
5148 if( status != PSA_SUCCESS )
5149 goto cleanup;
5150 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5151 tls12_prf->output_block, hash_length );
5152 if( status != PSA_SUCCESS )
5153 goto cleanup;
5154 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5155 if( status != PSA_SUCCESS )
5156 goto cleanup;
5157
Janos Follath7742fee2019-06-17 12:58:10 +01005158
5159cleanup:
5160
Janos Follathea29bfb2019-06-19 12:21:20 +01005161 cleanup_status = psa_hash_abort( &backup );
5162 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5163 status = cleanup_status;
5164
5165 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005166}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005167
Janos Follath844eb0e2019-06-19 12:10:49 +01005168static psa_status_t psa_key_derivation_tls12_prf_read(
5169 psa_tls12_prf_key_derivation_t *tls12_prf,
5170 psa_algorithm_t alg,
5171 uint8_t *output,
5172 size_t output_length )
5173{
5174 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5175 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5176 psa_status_t status;
5177 uint8_t offset, length;
5178
5179 while( output_length != 0 )
5180 {
5181 /* Check if we have fully processed the current block. */
5182 if( tls12_prf->left_in_block == 0 )
5183 {
5184 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5185 alg );
5186 if( status != PSA_SUCCESS )
5187 return( status );
5188
5189 continue;
5190 }
5191
5192 if( tls12_prf->left_in_block > output_length )
5193 length = (uint8_t) output_length;
5194 else
5195 length = tls12_prf->left_in_block;
5196
5197 offset = hash_length - tls12_prf->left_in_block;
5198 memcpy( output, tls12_prf->output_block + offset, length );
5199 output += length;
5200 output_length -= length;
5201 tls12_prf->left_in_block -= length;
5202 }
5203
5204 return( PSA_SUCCESS );
5205}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005206#endif /* MBEDTLS_MD_C */
5207
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005208psa_status_t psa_key_derivation_output_bytes(
5209 psa_key_derivation_operation_t *operation,
5210 uint8_t *output,
5211 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005212{
5213 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005214 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005215
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005216 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005217 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005218 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005219 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005220 }
5221
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005222 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005223 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005224 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005225 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005226 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005227 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005228 goto exit;
5229 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005230 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005231 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005232 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005233 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005234 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5235 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005236 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005237 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005238 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005239 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005240 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005241
Gilles Peskinebef7f142018-07-12 17:22:21 +02005242#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005243 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005244 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005245 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005246 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005247 output, output_length );
5248 }
Janos Follathadbec812019-06-14 11:05:39 +01005249 else
Janos Follathadbec812019-06-14 11:05:39 +01005250 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005251 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005252 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005253 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005254 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005255 output_length );
5256 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005257 else
5258#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005259 {
5260 return( PSA_ERROR_BAD_STATE );
5261 }
5262
5263exit:
5264 if( status != PSA_SUCCESS )
5265 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005266 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 * This allows us to differentiate between exhausted operations and
5268 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5269 * operations. */
5270 psa_algorithm_t alg = operation->alg;
5271 psa_key_derivation_abort( operation );
5272 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005273 memset( output, '!', output_length );
5274 }
5275 return( status );
5276}
5277
Gilles Peskine08542d82018-07-19 17:05:42 +02005278#if defined(MBEDTLS_DES_C)
5279static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5280{
5281 if( data_size >= 8 )
5282 mbedtls_des_key_set_parity( data );
5283 if( data_size >= 16 )
5284 mbedtls_des_key_set_parity( data + 8 );
5285 if( data_size >= 24 )
5286 mbedtls_des_key_set_parity( data + 16 );
5287}
5288#endif /* MBEDTLS_DES_C */
5289
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005290static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005291 psa_key_slot_t *slot,
5292 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005293 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005294{
5295 uint8_t *data = NULL;
5296 size_t bytes = PSA_BITS_TO_BYTES( bits );
5297 psa_status_t status;
5298
Gilles Peskine8e338702019-07-30 20:06:31 +02005299 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005300 return( PSA_ERROR_INVALID_ARGUMENT );
5301 if( bits % 8 != 0 )
5302 return( PSA_ERROR_INVALID_ARGUMENT );
5303 data = mbedtls_calloc( 1, bytes );
5304 if( data == NULL )
5305 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5306
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005307 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005308 if( status != PSA_SUCCESS )
5309 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005310#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005311 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005312 psa_des_set_key_parity( data, bytes );
5313#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005314 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005315
5316exit:
5317 mbedtls_free( data );
5318 return( status );
5319}
5320
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005321psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005322 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005323 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005324{
5325 psa_status_t status;
5326 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005327 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005328
5329 /* Reject any attempt to create a zero-length key so that we don't
5330 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5331 if( psa_get_key_bits( attributes ) == 0 )
5332 return( PSA_ERROR_INVALID_ARGUMENT );
5333
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005334 if( ! operation->can_output_key )
5335 return( PSA_ERROR_NOT_PERMITTED );
5336
Gilles Peskinedf179142019-07-15 22:02:14 +02005337 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5338 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005339#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5340 if( driver != NULL )
5341 {
5342 /* Deriving a key in a secure element is not implemented yet. */
5343 status = PSA_ERROR_NOT_SUPPORTED;
5344 }
5345#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005346 if( status == PSA_SUCCESS )
5347 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005348 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005349 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005350 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005351 }
5352 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005353 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005354 if( status != PSA_SUCCESS )
5355 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005356 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005357 *handle = 0;
5358 }
5359 return( status );
5360}
5361
Gilles Peskineeab56e42018-07-12 17:12:33 +02005362
5363
5364/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005365/* Key derivation */
5366/****************************************************************/
5367
Gilles Peskine969c5d62019-01-16 15:53:06 +01005368static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005369 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005370 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005371{
Janos Follath5fe19732019-06-20 15:09:30 +01005372 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5373 * initialisers for this union leave some bytes unspecified.) */
5374 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5375
Gilles Peskine969c5d62019-01-16 15:53:06 +01005376 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005377#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005378 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5379 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5380 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005381 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005382 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005383 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5384 if( hash_size == 0 )
5385 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005386 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5387 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005388 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005389 {
5390 return( PSA_ERROR_NOT_SUPPORTED );
5391 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005392 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005393 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005394 }
5395#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005396 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005397 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005398}
5399
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005400psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005401 psa_algorithm_t alg )
5402{
5403 psa_status_t status;
5404
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005405 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005406 return( PSA_ERROR_BAD_STATE );
5407
Gilles Peskine6843c292019-01-18 16:44:49 +01005408 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5409 return( PSA_ERROR_INVALID_ARGUMENT );
5410 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005411 {
5412 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005413 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005414 }
5415 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5416 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005417 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005418 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005419 else
5420 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005421
5422 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005423 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005424 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005425}
5426
5427#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005428static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005429 psa_algorithm_t hash_alg,
5430 psa_key_derivation_step_t step,
5431 const uint8_t *data,
5432 size_t data_length )
5433{
5434 psa_status_t status;
5435 switch( step )
5436 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005437 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005438 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005439 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005440 status = psa_hmac_setup_internal( &hkdf->hmac,
5441 data, data_length,
5442 hash_alg );
5443 if( status != PSA_SUCCESS )
5444 return( status );
5445 hkdf->state = HKDF_STATE_STARTED;
5446 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005447 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005448 /* If no salt was provided, use an empty salt. */
5449 if( hkdf->state == HKDF_STATE_INIT )
5450 {
5451 status = psa_hmac_setup_internal( &hkdf->hmac,
5452 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005453 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005454 if( status != PSA_SUCCESS )
5455 return( status );
5456 hkdf->state = HKDF_STATE_STARTED;
5457 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005458 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005459 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005460 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5461 data, data_length );
5462 if( status != PSA_SUCCESS )
5463 return( status );
5464 status = psa_hmac_finish_internal( &hkdf->hmac,
5465 hkdf->prk,
5466 sizeof( hkdf->prk ) );
5467 if( status != PSA_SUCCESS )
5468 return( status );
5469 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5470 hkdf->block_number = 0;
5471 hkdf->state = HKDF_STATE_KEYED;
5472 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005473 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005474 if( hkdf->state == HKDF_STATE_OUTPUT )
5475 return( PSA_ERROR_BAD_STATE );
5476 if( hkdf->info_set )
5477 return( PSA_ERROR_BAD_STATE );
5478 hkdf->info_length = data_length;
5479 if( data_length != 0 )
5480 {
5481 hkdf->info = mbedtls_calloc( 1, data_length );
5482 if( hkdf->info == NULL )
5483 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5484 memcpy( hkdf->info, data, data_length );
5485 }
5486 hkdf->info_set = 1;
5487 return( PSA_SUCCESS );
5488 default:
5489 return( PSA_ERROR_INVALID_ARGUMENT );
5490 }
5491}
Janos Follathb03233e2019-06-11 15:30:30 +01005492
Janos Follathf08e2652019-06-13 09:05:41 +01005493static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5494 const uint8_t *data,
5495 size_t data_length )
5496{
5497 if( prf->state != TLS12_PRF_STATE_INIT )
5498 return( PSA_ERROR_BAD_STATE );
5499
Janos Follathd6dce9f2019-07-04 09:11:38 +01005500 if( data_length != 0 )
5501 {
5502 prf->seed = mbedtls_calloc( 1, data_length );
5503 if( prf->seed == NULL )
5504 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005505
Janos Follathd6dce9f2019-07-04 09:11:38 +01005506 memcpy( prf->seed, data, data_length );
5507 prf->seed_length = data_length;
5508 }
Janos Follathf08e2652019-06-13 09:05:41 +01005509
5510 prf->state = TLS12_PRF_STATE_SEED_SET;
5511
5512 return( PSA_SUCCESS );
5513}
5514
Janos Follath81550542019-06-13 14:26:34 +01005515static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5516 psa_algorithm_t hash_alg,
5517 const uint8_t *data,
5518 size_t data_length )
5519{
5520 psa_status_t status;
5521 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5522 return( PSA_ERROR_BAD_STATE );
5523
5524 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5525 if( status != PSA_SUCCESS )
5526 return( status );
5527
5528 prf->state = TLS12_PRF_STATE_KEY_SET;
5529
5530 return( PSA_SUCCESS );
5531}
5532
Janos Follath6660f0e2019-06-17 08:44:03 +01005533static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5534 psa_tls12_prf_key_derivation_t *prf,
5535 psa_algorithm_t hash_alg,
5536 const uint8_t *data,
5537 size_t data_length )
5538{
5539 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005540 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5541 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005542
5543 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5544 return( PSA_ERROR_INVALID_ARGUMENT );
5545
5546 /* Quoting RFC 4279, Section 2:
5547 *
5548 * The premaster secret is formed as follows: if the PSK is N octets
5549 * long, concatenate a uint16 with the value N, N zero octets, a second
5550 * uint16 with the value N, and the PSK itself.
5551 */
5552
Janos Follath40e13932019-06-26 13:22:29 +01005553 *cur++ = ( data_length >> 8 ) & 0xff;
5554 *cur++ = ( data_length >> 0 ) & 0xff;
5555 memset( cur, 0, data_length );
5556 cur += data_length;
5557 *cur++ = pms[0];
5558 *cur++ = pms[1];
5559 memcpy( cur, data, data_length );
5560 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005561
Janos Follath40e13932019-06-26 13:22:29 +01005562 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005563
5564 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5565 return( status );
5566}
5567
Janos Follath63028dd2019-06-13 09:15:47 +01005568static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5569 const uint8_t *data,
5570 size_t data_length )
5571{
5572 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5573 return( PSA_ERROR_BAD_STATE );
5574
Janos Follathd6dce9f2019-07-04 09:11:38 +01005575 if( data_length != 0 )
5576 {
5577 prf->label = mbedtls_calloc( 1, data_length );
5578 if( prf->label == NULL )
5579 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005580
Janos Follathd6dce9f2019-07-04 09:11:38 +01005581 memcpy( prf->label, data, data_length );
5582 prf->label_length = data_length;
5583 }
Janos Follath63028dd2019-06-13 09:15:47 +01005584
5585 prf->state = TLS12_PRF_STATE_LABEL_SET;
5586
5587 return( PSA_SUCCESS );
5588}
5589
Janos Follathb03233e2019-06-11 15:30:30 +01005590static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5591 psa_algorithm_t hash_alg,
5592 psa_key_derivation_step_t step,
5593 const uint8_t *data,
5594 size_t data_length )
5595{
Janos Follathb03233e2019-06-11 15:30:30 +01005596 switch( step )
5597 {
Janos Follathf08e2652019-06-13 09:05:41 +01005598 case PSA_KEY_DERIVATION_INPUT_SEED:
5599 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005600 case PSA_KEY_DERIVATION_INPUT_SECRET:
5601 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005602 case PSA_KEY_DERIVATION_INPUT_LABEL:
5603 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005604 default:
5605 return( PSA_ERROR_INVALID_ARGUMENT );
5606 }
5607}
Janos Follath6660f0e2019-06-17 08:44:03 +01005608
5609static psa_status_t psa_tls12_prf_psk_to_ms_input(
5610 psa_tls12_prf_key_derivation_t *prf,
5611 psa_algorithm_t hash_alg,
5612 psa_key_derivation_step_t step,
5613 const uint8_t *data,
5614 size_t data_length )
5615{
5616 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005617 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005618 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5619 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005620 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005621
5622 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5623}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005624#endif /* MBEDTLS_MD_C */
5625
Gilles Peskineb8965192019-09-24 16:21:10 +02005626/** Check whether the given key type is acceptable for the given
5627 * input step of a key derivation.
5628 *
5629 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5630 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5631 * Both secret and non-secret inputs can alternatively have the type
5632 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5633 * that the input was passed as a buffer rather than via a key object.
5634 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005635static int psa_key_derivation_check_input_type(
5636 psa_key_derivation_step_t step,
5637 psa_key_type_t key_type )
5638{
5639 switch( step )
5640 {
5641 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005642 if( key_type == PSA_KEY_TYPE_DERIVE )
5643 return( PSA_SUCCESS );
5644 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005645 return( PSA_SUCCESS );
5646 break;
5647 case PSA_KEY_DERIVATION_INPUT_LABEL:
5648 case PSA_KEY_DERIVATION_INPUT_SALT:
5649 case PSA_KEY_DERIVATION_INPUT_INFO:
5650 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005651 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5652 return( PSA_SUCCESS );
5653 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005654 return( PSA_SUCCESS );
5655 break;
5656 }
5657 return( PSA_ERROR_INVALID_ARGUMENT );
5658}
5659
Janos Follathb80a94e2019-06-12 15:54:46 +01005660static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005661 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005662 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005663 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005664 const uint8_t *data,
5665 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005666{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005667 psa_status_t status;
5668 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5669
5670 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005671 if( status != PSA_SUCCESS )
5672 goto exit;
5673
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005674#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005675 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005676 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005677 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005678 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005679 step, data, data_length );
5680 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005681 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005682 {
Janos Follathb03233e2019-06-11 15:30:30 +01005683 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5684 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5685 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005686 }
5687 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5688 {
5689 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5690 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5691 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005692 }
5693 else
5694#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005695 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005696 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005697 return( PSA_ERROR_BAD_STATE );
5698 }
5699
Gilles Peskine224b0d62019-09-23 18:13:17 +02005700exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005701 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005702 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005703 return( status );
5704}
5705
Janos Follath51f4a0f2019-06-14 11:35:55 +01005706psa_status_t psa_key_derivation_input_bytes(
5707 psa_key_derivation_operation_t *operation,
5708 psa_key_derivation_step_t step,
5709 const uint8_t *data,
5710 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005711{
Gilles Peskineb8965192019-09-24 16:21:10 +02005712 return( psa_key_derivation_input_internal( operation, step,
5713 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005714 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005715}
5716
Janos Follath51f4a0f2019-06-14 11:35:55 +01005717psa_status_t psa_key_derivation_input_key(
5718 psa_key_derivation_operation_t *operation,
5719 psa_key_derivation_step_t step,
5720 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005721{
5722 psa_key_slot_t *slot;
5723 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005724
Gilles Peskine28f8f302019-07-24 13:30:31 +02005725 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005726 PSA_KEY_USAGE_DERIVE,
5727 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005728 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005729 {
5730 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005731 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005732 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005733
5734 /* Passing a key object as a SECRET input unlocks the permission
5735 * to output to a key object. */
5736 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5737 operation->can_output_key = 1;
5738
Janos Follathb80a94e2019-06-12 15:54:46 +01005739 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005740 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005741 slot->data.key.data,
5742 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005743}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005744
5745
5746
5747/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005748/* Key agreement */
5749/****************************************************************/
5750
Gilles Peskinea05219c2018-11-16 16:02:56 +01005751#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005752static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5753 size_t peer_key_length,
5754 const mbedtls_ecp_keypair *our_key,
5755 uint8_t *shared_secret,
5756 size_t shared_secret_size,
5757 size_t *shared_secret_length )
5758{
Steven Cooremand4867872020-08-05 16:31:39 +02005759 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005760 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005761 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005762 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005763 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005764 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005765
Steven Cooreman4fed4552020-08-03 14:46:03 +02005766 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5767 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005768 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005769 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005770 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005771 goto exit;
5772
Jaeden Amero97271b32019-01-10 19:38:51 +00005773 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005774 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005775 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005776 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005777 status = mbedtls_to_psa_error(
5778 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5779 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005780 goto exit;
5781
Jaeden Amero97271b32019-01-10 19:38:51 +00005782 status = mbedtls_to_psa_error(
5783 mbedtls_ecdh_calc_secret( &ecdh,
5784 shared_secret_length,
5785 shared_secret, shared_secret_size,
5786 mbedtls_ctr_drbg_random,
5787 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005788 if( status != PSA_SUCCESS )
5789 goto exit;
5790 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5791 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005792
5793exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005794 if( status != PSA_SUCCESS )
5795 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005796 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005797 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005798 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005799
Jaeden Amero97271b32019-01-10 19:38:51 +00005800 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005801}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005802#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005803
Gilles Peskine01d718c2018-09-18 12:01:02 +02005804#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5805
Gilles Peskine0216fe12019-04-11 21:23:21 +02005806static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5807 psa_key_slot_t *private_key,
5808 const uint8_t *peer_key,
5809 size_t peer_key_length,
5810 uint8_t *shared_secret,
5811 size_t shared_secret_size,
5812 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005813{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005814 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005815 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005816#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005817 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005818 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005819 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005820 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005821 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005822 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005823 private_key->data.key.data,
5824 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005825 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005826 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005827 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005828 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005829 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005830 shared_secret, shared_secret_size,
5831 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005832 mbedtls_ecp_keypair_free( ecp );
5833 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005834 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005835#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005836 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005837 (void) private_key;
5838 (void) peer_key;
5839 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005840 (void) shared_secret;
5841 (void) shared_secret_size;
5842 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005843 return( PSA_ERROR_NOT_SUPPORTED );
5844 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005845}
5846
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005847/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005848 * to potentially free embedded data structures and wipe confidential data.
5849 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005850static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005851 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005852 psa_key_slot_t *private_key,
5853 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005854 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005855{
5856 psa_status_t status;
5857 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5858 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005859 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005860
5861 /* Step 1: run the secret agreement algorithm to generate the shared
5862 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005863 status = psa_key_agreement_raw_internal( ka_alg,
5864 private_key,
5865 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005866 shared_secret,
5867 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005868 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005869 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005870 goto exit;
5871
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005872 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005873 * the shared secret. A shared secret is permitted wherever a key
5874 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005875 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005876 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005877 shared_secret,
5878 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005879
Gilles Peskine12313cd2018-06-20 00:20:32 +02005880exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005881 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005882 return( status );
5883}
5884
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005885psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005886 psa_key_derivation_step_t step,
5887 psa_key_handle_t private_key,
5888 const uint8_t *peer_key,
5889 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005890{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005891 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005892 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005893 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005894 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005895 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005896 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005897 if( status != PSA_SUCCESS )
5898 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005899 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005900 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005901 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005902 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005903 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005904 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005905}
5906
Gilles Peskinebe697d82019-05-16 18:00:41 +02005907psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5908 psa_key_handle_t private_key,
5909 const uint8_t *peer_key,
5910 size_t peer_key_length,
5911 uint8_t *output,
5912 size_t output_size,
5913 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005914{
5915 psa_key_slot_t *slot;
5916 psa_status_t status;
5917
5918 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5919 {
5920 status = PSA_ERROR_INVALID_ARGUMENT;
5921 goto exit;
5922 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005923 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005924 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005925 if( status != PSA_SUCCESS )
5926 goto exit;
5927
5928 status = psa_key_agreement_raw_internal( alg, slot,
5929 peer_key, peer_key_length,
5930 output, output_size,
5931 output_length );
5932
5933exit:
5934 if( status != PSA_SUCCESS )
5935 {
5936 /* If an error happens and is not handled properly, the output
5937 * may be used as a key to protect sensitive data. Arrange for such
5938 * a key to be random, which is likely to result in decryption or
5939 * verification errors. This is better than filling the buffer with
5940 * some constant data such as zeros, which would result in the data
5941 * being protected with a reproducible, easily knowable key.
5942 */
5943 psa_generate_random( output, output_size );
5944 *output_length = output_size;
5945 }
5946 return( status );
5947}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005948
5949
5950/****************************************************************/
5951/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005952/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005953
Gilles Peskine4c317f42018-07-12 01:24:09 +02005954psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005955 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005956{
Janos Follath24eed8d2019-11-22 13:21:35 +00005957 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005958 GUARD_MODULE_INITIALIZED;
5959
Gilles Peskinef181eca2019-08-07 13:49:00 +02005960 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5961 {
5962 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5963 output,
5964 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5965 if( ret != 0 )
5966 return( mbedtls_to_psa_error( ret ) );
5967 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5968 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5969 }
5970
Gilles Peskinee59236f2018-01-27 23:32:46 +01005971 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5972 return( mbedtls_to_psa_error( ret ) );
5973}
5974
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005975#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5976#include "mbedtls/entropy_poll.h"
5977
Gilles Peskine7228da22019-07-15 11:06:15 +02005978psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005979 size_t seed_size )
5980{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005981 if( global_data.initialized )
5982 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005983
5984 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5985 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5986 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5987 return( PSA_ERROR_INVALID_ARGUMENT );
5988
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005989 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005990}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005991#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005992
Gilles Peskinee56e8782019-04-26 17:34:02 +02005993#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5994static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5995 size_t domain_parameters_size,
5996 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005997{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005998 size_t i;
5999 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006000
Gilles Peskinee56e8782019-04-26 17:34:02 +02006001 if( domain_parameters_size == 0 )
6002 {
6003 *exponent = 65537;
6004 return( PSA_SUCCESS );
6005 }
6006
6007 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6008 * support values that fit in a 32-bit integer, which is larger than
6009 * int on just about every platform anyway. */
6010 if( domain_parameters_size > sizeof( acc ) )
6011 return( PSA_ERROR_NOT_SUPPORTED );
6012 for( i = 0; i < domain_parameters_size; i++ )
6013 acc = ( acc << 8 ) | domain_parameters[i];
6014 if( acc > INT_MAX )
6015 return( PSA_ERROR_NOT_SUPPORTED );
6016 *exponent = acc;
6017 return( PSA_SUCCESS );
6018}
6019#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6020
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006021static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006022 psa_key_slot_t *slot, size_t bits,
6023 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006024{
Gilles Peskine8e338702019-07-30 20:06:31 +02006025 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006026
Gilles Peskinee56e8782019-04-26 17:34:02 +02006027 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006028 return( PSA_ERROR_INVALID_ARGUMENT );
6029
Gilles Peskinee59236f2018-01-27 23:32:46 +01006030 if( key_type_is_raw_bytes( type ) )
6031 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006032 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006033
6034 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006035 if( status != PSA_SUCCESS )
6036 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006037
6038 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006039 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6040 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006041 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006042
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006043 status = psa_generate_random( slot->data.key.data,
6044 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006045 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006046 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006047
6048 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006049#if defined(MBEDTLS_DES_C)
6050 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006051 psa_des_set_key_parity( slot->data.key.data,
6052 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006053#endif /* MBEDTLS_DES_C */
6054 }
6055 else
6056
6057#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006058 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006059 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006060 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006061 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006062 int exponent;
6063 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006064 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6065 return( PSA_ERROR_NOT_SUPPORTED );
6066 /* Accept only byte-aligned keys, for the same reasons as
6067 * in psa_import_rsa_key(). */
6068 if( bits % 8 != 0 )
6069 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006070 status = psa_read_rsa_exponent( domain_parameters,
6071 domain_parameters_size,
6072 &exponent );
6073 if( status != PSA_SUCCESS )
6074 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006075 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6076 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006077 mbedtls_ctr_drbg_random,
6078 &global_data.ctr_drbg,
6079 (unsigned int) bits,
6080 exponent );
6081 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006082 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006083
6084 /* Make sure to always have an export representation available */
6085 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6086
Steven Cooreman75b74362020-07-28 14:30:13 +02006087 status = psa_allocate_buffer_to_slot( slot, bytes );
6088 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006089 {
6090 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006091 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006092 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006093
6094 status = psa_export_rsa_key( type,
6095 &rsa,
6096 slot->data.key.data,
6097 bytes,
6098 &slot->data.key.bytes );
6099 mbedtls_rsa_free( &rsa );
6100 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006101 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006102 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006103 }
6104 else
6105#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6106
6107#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006108 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006109 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006110 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006111 mbedtls_ecp_group_id grp_id =
6112 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006113 const mbedtls_ecp_curve_info *curve_info =
6114 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006115 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006116 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006117 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006118 return( PSA_ERROR_NOT_SUPPORTED );
6119 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6120 return( PSA_ERROR_NOT_SUPPORTED );
6121 if( curve_info->bit_size != bits )
6122 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02006123 mbedtls_ecp_keypair_init( &ecp );
6124 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006125 mbedtls_ctr_drbg_random,
6126 &global_data.ctr_drbg );
6127 if( ret != 0 )
6128 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006129 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006130 return( mbedtls_to_psa_error( ret ) );
6131 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006132
6133
6134 /* Make sure to always have an export representation available */
6135 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006136 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6137 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006138 {
6139 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006140 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006141 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006142
6143 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006144 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6145
6146 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006147 if( status != PSA_SUCCESS ) {
6148 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006149 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006150 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006151 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006152 }
6153 else
6154#endif /* MBEDTLS_ECP_C */
Steven Cooreman29149862020-08-05 15:43:42 +02006155 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006156 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006157 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006158
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006159 return( PSA_SUCCESS );
6160}
Darryl Green0c6575a2018-11-07 16:05:30 +00006161
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006162psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006163 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006164{
6165 psa_status_t status;
6166 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006167 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006168
Gilles Peskine0f84d622019-09-12 19:03:13 +02006169 /* Reject any attempt to create a zero-length key so that we don't
6170 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6171 if( psa_get_key_bits( attributes ) == 0 )
6172 return( PSA_ERROR_INVALID_ARGUMENT );
6173
Gilles Peskinedf179142019-07-15 22:02:14 +02006174 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6175 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006176 if( status != PSA_SUCCESS )
6177 goto exit;
6178
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006179 status = psa_driver_wrapper_generate_key( attributes,
6180 slot );
6181 if( status != PSA_ERROR_NOT_SUPPORTED ||
6182 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6183 goto exit;
6184
6185 status = psa_generate_key_internal(
6186 slot, attributes->core.bits,
6187 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006188
6189exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006190 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006191 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006192 if( status != PSA_SUCCESS )
6193 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006194 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006195 *handle = 0;
6196 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006197 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006198}
6199
6200
Gilles Peskinee59236f2018-01-27 23:32:46 +01006201
6202/****************************************************************/
6203/* Module setup */
6204/****************************************************************/
6205
Gilles Peskine5e769522018-11-20 21:59:56 +01006206psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6207 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6208 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6209{
6210 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6211 return( PSA_ERROR_BAD_STATE );
6212 global_data.entropy_init = entropy_init;
6213 global_data.entropy_free = entropy_free;
6214 return( PSA_SUCCESS );
6215}
6216
Gilles Peskinee59236f2018-01-27 23:32:46 +01006217void mbedtls_psa_crypto_free( void )
6218{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006219 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006220 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6221 {
6222 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006223 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006224 }
6225 /* Wipe all remaining data, including configuration.
6226 * In particular, this sets all state indicator to the value
6227 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006228 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006229#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006230 /* Unregister all secure element drivers, so that we restart from
6231 * a pristine state. */
6232 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006233#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006234}
6235
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006236#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6237/** Recover a transaction that was interrupted by a power failure.
6238 *
6239 * This function is called during initialization, before psa_crypto_init()
6240 * returns. If this function returns a failure status, the initialization
6241 * fails.
6242 */
6243static psa_status_t psa_crypto_recover_transaction(
6244 const psa_crypto_transaction_t *transaction )
6245{
6246 switch( transaction->unknown.type )
6247 {
6248 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6249 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006250 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006251 * is implemented.
6252 * https://github.com/ARMmbed/mbed-crypto/issues/218
6253 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006254 default:
6255 /* We found an unsupported transaction in the storage.
6256 * We don't know what state the storage is in. Give up. */
6257 return( PSA_ERROR_STORAGE_FAILURE );
6258 }
6259}
6260#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6261
Gilles Peskinee59236f2018-01-27 23:32:46 +01006262psa_status_t psa_crypto_init( void )
6263{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006264 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006265 const unsigned char drbg_seed[] = "PSA";
6266
Gilles Peskinec6b69072018-11-20 21:42:52 +01006267 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006268 if( global_data.initialized != 0 )
6269 return( PSA_SUCCESS );
6270
Gilles Peskine5e769522018-11-20 21:59:56 +01006271 /* Set default configuration if
6272 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6273 if( global_data.entropy_init == NULL )
6274 global_data.entropy_init = mbedtls_entropy_init;
6275 if( global_data.entropy_free == NULL )
6276 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006277
Gilles Peskinec6b69072018-11-20 21:42:52 +01006278 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006279 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006280#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6281 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6282 /* The PSA entropy injection feature depends on using NV seed as an entropy
6283 * source. Add NV seed as an entropy source for PSA entropy injection. */
6284 mbedtls_entropy_add_source( &global_data.entropy,
6285 mbedtls_nv_seed_poll, NULL,
6286 MBEDTLS_ENTROPY_BLOCK_SIZE,
6287 MBEDTLS_ENTROPY_SOURCE_STRONG );
6288#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006289 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006290 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006291 status = mbedtls_to_psa_error(
6292 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6293 mbedtls_entropy_func,
6294 &global_data.entropy,
6295 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6296 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006297 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006298 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006299
Gilles Peskine66fb1262018-12-10 16:29:04 +01006300 status = psa_initialize_key_slots( );
6301 if( status != PSA_SUCCESS )
6302 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006303
Gilles Peskined9348f22019-10-01 15:22:29 +02006304#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6305 status = psa_init_all_se_drivers( );
6306 if( status != PSA_SUCCESS )
6307 goto exit;
6308#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6309
Gilles Peskine4b734222019-07-24 15:56:31 +02006310#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006311 status = psa_crypto_load_transaction( );
6312 if( status == PSA_SUCCESS )
6313 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006314 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6315 if( status != PSA_SUCCESS )
6316 goto exit;
6317 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006318 }
6319 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6320 {
6321 /* There's no transaction to complete. It's all good. */
6322 status = PSA_SUCCESS;
6323 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006324#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006325
Gilles Peskinec6b69072018-11-20 21:42:52 +01006326 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006327 global_data.initialized = 1;
6328
6329exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006330 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006331 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006332 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006333}
6334
6335#endif /* MBEDTLS_PSA_CRYPTO_C */