blob: 3e174f9c2e5cd8923450c5940866ffae29fd5783 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
itayzafrir7723ab12019-02-14 10:28:02 +020025#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010026#include "psa/crypto.h"
27
Gilles Peskine039b90c2018-12-07 18:24:41 +010028#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010029#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020030#include "psa_crypto_driver_wrappers.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020031#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020032#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020033#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010034#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010035/* Include internal declarations that are useful for implementing persistently
36 * stored keys. */
37#include "psa_crypto_storage.h"
38
Gilles Peskineb46bef22019-07-30 21:32:04 +020039#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include <stdlib.h>
41#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020043#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#define mbedtls_calloc calloc
45#define mbedtls_free free
46#endif
47
Gilles Peskinea5905292018-02-07 20:59:33 +010048#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020049#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000050#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020051#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/blowfish.h"
53#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020054#include "mbedtls/chacha20.h"
55#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/cipher.h"
57#include "mbedtls/ccm.h"
58#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010059#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020061#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010062#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/error.h"
65#include "mbedtls/gcm.h"
66#include "mbedtls/md2.h"
67#include "mbedtls/md4.h"
68#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010069#include "mbedtls/md.h"
70#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010071#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010073#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000074#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/sha1.h"
78#include "mbedtls/sha256.h"
79#include "mbedtls/sha512.h"
80#include "mbedtls/xtea.h"
81
Gilles Peskine996deb12018-08-01 15:45:45 +020082#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
83
Gilles Peskine9ef733f2018-02-07 21:05:37 +010084/* constant-time buffer comparison */
85static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
86{
87 size_t i;
88 unsigned char diff = 0;
89
90 for( i = 0; i < n; i++ )
91 diff |= a[i] ^ b[i];
92
93 return( diff );
94}
95
96
97
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010098/****************************************************************/
99/* Global data, support functions and library management */
100/****************************************************************/
101
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102static int key_type_is_raw_bytes( psa_key_type_t type )
103{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200104 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105}
106
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100107/* Values for psa_global_data_t::rng_state */
108#define RNG_NOT_INITIALIZED 0
109#define RNG_INITIALIZED 1
110#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100111
Gilles Peskine2d277862018-06-18 15:41:12 +0200112typedef struct
113{
Gilles Peskine5e769522018-11-20 21:59:56 +0100114 void (* entropy_init )( mbedtls_entropy_context *ctx );
115 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100116 mbedtls_entropy_context entropy;
117 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100118 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100119 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120} psa_global_data_t;
121
122static psa_global_data_t global_data;
123
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124#define GUARD_MODULE_INITIALIZED \
125 if( global_data.initialized == 0 ) \
126 return( PSA_ERROR_BAD_STATE );
127
Steven Cooreman01164162020-07-20 15:31:37 +0200128psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100129{
Gilles Peskinea5905292018-02-07 20:59:33 +0100130 /* If there's both a high-level code and low-level code, dispatch on
131 * the high-level code. */
132 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133 {
134 case 0:
135 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100136
137 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
138 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
139 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
140 return( PSA_ERROR_NOT_SUPPORTED );
141 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
142 return( PSA_ERROR_HARDWARE_FAILURE );
143
144 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
Gilles Peskine9a944802018-06-21 09:35:35 +0200147 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
148 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
149 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
150 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
151 case MBEDTLS_ERR_ASN1_INVALID_DATA:
152 return( PSA_ERROR_INVALID_ARGUMENT );
153 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
154 return( PSA_ERROR_INSUFFICIENT_MEMORY );
155 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
156 return( PSA_ERROR_BUFFER_TOO_SMALL );
157
Jaeden Amero93e21112019-02-20 13:57:28 +0000158#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000159 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
161 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
162#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
164 return( PSA_ERROR_NOT_SUPPORTED );
165 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
166 return( PSA_ERROR_HARDWARE_FAILURE );
167
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000169 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
171 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
174 return( PSA_ERROR_NOT_SUPPORTED );
175 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
176 return( PSA_ERROR_HARDWARE_FAILURE );
177
178 case MBEDTLS_ERR_CCM_BAD_INPUT:
179 return( PSA_ERROR_INVALID_ARGUMENT );
180 case MBEDTLS_ERR_CCM_AUTH_FAILED:
181 return( PSA_ERROR_INVALID_SIGNATURE );
182 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
183 return( PSA_ERROR_HARDWARE_FAILURE );
184
Gilles Peskine26869f22019-05-06 15:25:00 +0200185 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
186 return( PSA_ERROR_INVALID_ARGUMENT );
187
188 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
189 return( PSA_ERROR_BAD_STATE );
190 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
191 return( PSA_ERROR_INVALID_SIGNATURE );
192
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
194 return( PSA_ERROR_NOT_SUPPORTED );
195 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
198 return( PSA_ERROR_INSUFFICIENT_MEMORY );
199 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
200 return( PSA_ERROR_INVALID_PADDING );
201 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200202 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
205 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200206 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
208 return( PSA_ERROR_HARDWARE_FAILURE );
209
210 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
214 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
215 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
216 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
217 return( PSA_ERROR_NOT_SUPPORTED );
218 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
220
221 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
Gilles Peskinee59236f2018-01-27 23:32:46 +0100226 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
227 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
228 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
229 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100230
231 case MBEDTLS_ERR_GCM_AUTH_FAILED:
232 return( PSA_ERROR_INVALID_SIGNATURE );
233 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200234 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
243 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
244 return( PSA_ERROR_NOT_SUPPORTED );
245 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
246 return( PSA_ERROR_INVALID_ARGUMENT );
247 case MBEDTLS_ERR_MD_ALLOC_FAILED:
248 return( PSA_ERROR_INSUFFICIENT_MEMORY );
249 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
250 return( PSA_ERROR_STORAGE_FAILURE );
251 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
255 return( PSA_ERROR_STORAGE_FAILURE );
256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
261 return( PSA_ERROR_BUFFER_TOO_SMALL );
262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
269 return( PSA_ERROR_INSUFFICIENT_MEMORY );
270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
282 return( PSA_ERROR_NOT_SUPPORTED );
283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
285 return( PSA_ERROR_NOT_PERMITTED );
286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
291 return( PSA_ERROR_NOT_SUPPORTED );
292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
293 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
300 return( PSA_ERROR_NOT_SUPPORTED );
301
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
303 return( PSA_ERROR_HARDWARE_FAILURE );
304
305 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_RSA_INVALID_PADDING:
308 return( PSA_ERROR_INVALID_PADDING );
309 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
312 return( PSA_ERROR_INVALID_ARGUMENT );
313 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
314 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200315 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100316 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
317 return( PSA_ERROR_INVALID_SIGNATURE );
318 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
319 return( PSA_ERROR_BUFFER_TOO_SMALL );
320 case MBEDTLS_ERR_RSA_RNG_FAILED:
321 return( PSA_ERROR_INSUFFICIENT_MEMORY );
322 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
323 return( PSA_ERROR_NOT_SUPPORTED );
324 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
327 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
328 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
329 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
330 return( PSA_ERROR_HARDWARE_FAILURE );
331
332 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
333 return( PSA_ERROR_INVALID_ARGUMENT );
334 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
336
itayzafrir5c753392018-05-08 11:18:38 +0300337 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300338 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300339 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
341 return( PSA_ERROR_BUFFER_TOO_SMALL );
342 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
343 return( PSA_ERROR_NOT_SUPPORTED );
344 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
345 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
346 return( PSA_ERROR_INVALID_SIGNATURE );
347 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
348 return( PSA_ERROR_INSUFFICIENT_MEMORY );
349 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
350 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000351 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
352 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
Gilles Peskine8e338702019-07-30 20:06:31 +0200369 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100374mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100375 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200376{
377 switch( curve )
378 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100379 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100380 switch( byte_length )
381 {
382 case PSA_BITS_TO_BYTES( 192 ):
383 return( MBEDTLS_ECP_DP_SECP192R1 );
384 case PSA_BITS_TO_BYTES( 224 ):
385 return( MBEDTLS_ECP_DP_SECP224R1 );
386 case PSA_BITS_TO_BYTES( 256 ):
387 return( MBEDTLS_ECP_DP_SECP256R1 );
388 case PSA_BITS_TO_BYTES( 384 ):
389 return( MBEDTLS_ECP_DP_SECP384R1 );
390 case PSA_BITS_TO_BYTES( 521 ):
391 return( MBEDTLS_ECP_DP_SECP521R1 );
392 default:
393 return( MBEDTLS_ECP_DP_NONE );
394 }
395 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100396
Paul Elliott8ff510a2020-06-02 17:19:28 +0100397 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100398 switch( byte_length )
399 {
400 case PSA_BITS_TO_BYTES( 256 ):
401 return( MBEDTLS_ECP_DP_BP256R1 );
402 case PSA_BITS_TO_BYTES( 384 ):
403 return( MBEDTLS_ECP_DP_BP384R1 );
404 case PSA_BITS_TO_BYTES( 512 ):
405 return( MBEDTLS_ECP_DP_BP512R1 );
406 default:
407 return( MBEDTLS_ECP_DP_NONE );
408 }
409 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100410
Paul Elliott8ff510a2020-06-02 17:19:28 +0100411 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 switch( byte_length )
413 {
414 case PSA_BITS_TO_BYTES( 255 ):
415 return( MBEDTLS_ECP_DP_CURVE25519 );
416 case PSA_BITS_TO_BYTES( 448 ):
417 return( MBEDTLS_ECP_DP_CURVE448 );
418 default:
419 return( MBEDTLS_ECP_DP_NONE );
420 }
421 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100422
Paul Elliott8ff510a2020-06-02 17:19:28 +0100423 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 switch( byte_length )
425 {
426 case PSA_BITS_TO_BYTES( 192 ):
427 return( MBEDTLS_ECP_DP_SECP192K1 );
428 case PSA_BITS_TO_BYTES( 224 ):
429 return( MBEDTLS_ECP_DP_SECP224K1 );
430 case PSA_BITS_TO_BYTES( 256 ):
431 return( MBEDTLS_ECP_DP_SECP256K1 );
432 default:
433 return( MBEDTLS_ECP_DP_NONE );
434 }
435 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100436
Gilles Peskine12313cd2018-06-20 00:20:32 +0200437 default:
438 return( MBEDTLS_ECP_DP_NONE );
439 }
440}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100441#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200442
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200443static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
444 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445{
446 /* Check that the bit size is acceptable for the key type */
447 switch( type )
448 {
449 case PSA_KEY_TYPE_RAW_DATA:
450#if defined(MBEDTLS_MD_C)
451 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200452#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200453 case PSA_KEY_TYPE_DERIVE:
454 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200455#if defined(MBEDTLS_AES_C)
456 case PSA_KEY_TYPE_AES:
457 if( bits != 128 && bits != 192 && bits != 256 )
458 return( PSA_ERROR_INVALID_ARGUMENT );
459 break;
460#endif
461#if defined(MBEDTLS_CAMELLIA_C)
462 case PSA_KEY_TYPE_CAMELLIA:
463 if( bits != 128 && bits != 192 && bits != 256 )
464 return( PSA_ERROR_INVALID_ARGUMENT );
465 break;
466#endif
467#if defined(MBEDTLS_DES_C)
468 case PSA_KEY_TYPE_DES:
469 if( bits != 64 && bits != 128 && bits != 192 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
471 break;
472#endif
473#if defined(MBEDTLS_ARC4_C)
474 case PSA_KEY_TYPE_ARC4:
475 if( bits < 8 || bits > 2048 )
476 return( PSA_ERROR_INVALID_ARGUMENT );
477 break;
478#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200479#if defined(MBEDTLS_CHACHA20_C)
480 case PSA_KEY_TYPE_CHACHA20:
481 if( bits != 256 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200485 default:
486 return( PSA_ERROR_NOT_SUPPORTED );
487 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200488 if( bits % 8 != 0 )
489 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491 return( PSA_SUCCESS );
492}
493
Steven Cooremana01795d2020-07-24 22:48:15 +0200494#if defined(MBEDTLS_RSA_C)
495
496#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100497/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
498 * that are not a multiple of 8) well. For example, there is only
499 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
500 * way to return the exact bit size of a key.
501 * To keep things simple, reject non-byte-aligned key sizes. */
502static psa_status_t psa_check_rsa_key_byte_aligned(
503 const mbedtls_rsa_context *rsa )
504{
505 mbedtls_mpi n;
506 psa_status_t status;
507 mbedtls_mpi_init( &n );
508 status = mbedtls_to_psa_error(
509 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
510 if( status == PSA_SUCCESS )
511 {
512 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
513 status = PSA_ERROR_NOT_SUPPORTED;
514 }
515 mbedtls_mpi_free( &n );
516 return( status );
517}
Steven Cooremana01795d2020-07-24 22:48:15 +0200518#endif /* MBEDTLS_PK_PARSE_C */
Gilles Peskine86a440b2018-11-12 18:39:40 +0100519
Steven Cooreman7f391872020-07-30 14:57:44 +0200520/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200521 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200522 * \param[in] type The type of key contained in \p data.
523 * \param[in] data The buffer from which to load the representation.
524 * \param[in] data_length The size in bytes of \p data.
525 * \param[out] p_rsa Returns a pointer to an RSA context on success.
526 * The caller is responsible for freeing both the
527 * contents of the context and the context itself
528 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200529 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200530static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
531 const uint8_t *data,
532 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200533 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200534{
Steven Cooremana01795d2020-07-24 22:48:15 +0200535#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000536 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200537 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000538 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200539 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000540
541 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200542 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200544 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200547 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000548 if( status != PSA_SUCCESS )
549 goto exit;
550
551 /* We have something that the pkparse module recognizes. If it is a
552 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200553 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555 status = PSA_ERROR_INVALID_ARGUMENT;
556 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
560 * supports non-byte-aligned key sizes, but not well. For example,
561 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200562 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000563 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
564 {
565 status = PSA_ERROR_NOT_SUPPORTED;
566 goto exit;
567 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200568 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200569 if( status != PSA_SUCCESS )
570 goto exit;
571
Steven Cooremana2371e52020-07-28 14:30:39 +0200572 /* Copy out the pointer to the RSA context, and reset the PK context
573 * such that pk_free doesn't free the RSA context we just grabbed. */
574 *p_rsa = mbedtls_pk_rsa( ctx );
575 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000576
577exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200578 mbedtls_pk_free( &ctx );
579 return( status );
580#else
Steven Cooreman4fed4552020-08-03 14:46:03 +0200581 (void) data;
582 (void) data_length;
Steven Cooreman7f391872020-07-30 14:57:44 +0200583 (void) type;
Steven Cooremana01795d2020-07-24 22:48:15 +0200584 (void) rsa;
585 return( PSA_ERROR_NOT_SUPPORTED );
586#endif /* MBEDTLS_PK_PARSE_C */
587}
588
589/** Export an RSA key to export representation
590 *
591 * \param[in] type The type of key (public/private) to export
592 * \param[in] rsa The internal RSA representation from which to export
593 * \param[out] data The buffer to export to
594 * \param[in] data_size The length of the buffer to export to
595 * \param[out] data_length The amount of bytes written to \p data
596 */
597static psa_status_t psa_export_rsa_key( psa_key_type_t type,
598 mbedtls_rsa_context *rsa,
599 uint8_t *data,
600 size_t data_size,
601 size_t *data_length )
602{
603#if defined(MBEDTLS_PK_WRITE_C)
604 int ret;
605 mbedtls_pk_context pk;
606 uint8_t *pos = data + data_size;
607
608 mbedtls_pk_init( &pk );
609 pk.pk_info = &mbedtls_rsa_info;
610 pk.pk_ctx = rsa;
611
612 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200613 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
614 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200615 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
616 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
617 else
618 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
619
620 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200621 {
622 /* Clean up in case pk_write failed halfway through. */
623 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200624 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200625 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200626
627 /* The mbedtls_pk_xxx functions write to the end of the buffer.
628 * Move the data to the beginning and erase remaining data
629 * at the original location. */
630 if( 2 * (size_t) ret <= data_size )
631 {
632 memcpy( data, data + data_size - ret, ret );
633 memset( data + data_size - ret, 0, ret );
634 }
635 else if( (size_t) ret < data_size )
636 {
637 memmove( data, data + data_size - ret, ret );
638 memset( data + ret, 0, data_size - ret );
639 }
640
641 *data_length = ret;
642 return( PSA_SUCCESS );
643#else
644 (void) type;
645 (void) rsa;
646 (void) data;
647 (void) data_size;
648 (void) data_length;
649 return( PSA_ERROR_NOT_SUPPORTED );
650#endif /* MBEDTLS_PK_WRITE_C */
651}
652
653/** Import an RSA key from import representation to a slot
654 *
655 * \param[in,out] slot The slot where to store the export representation to
656 * \param[in] data The buffer containing the import representation
657 * \param[in] data_length The amount of bytes in \p data
658 */
659static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
660 const uint8_t *data,
661 size_t data_length )
662{
663 psa_status_t status;
664 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200665 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200666
Steven Cooremana01795d2020-07-24 22:48:15 +0200667 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200668 status = psa_load_rsa_representation( slot->attr.type,
669 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200670 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200671 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200672 if( status != PSA_SUCCESS )
673 goto exit;
674
675 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200676 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200677
Steven Cooreman75b74362020-07-28 14:30:13 +0200678 /* Re-export the data to PSA export format, such that we can store export
679 * representation in the key slot. Export representation in case of RSA is
680 * the smallest representation that's allowed as input, so a straight-up
681 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200682 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200683 if( output == NULL )
684 {
685 status = PSA_ERROR_INSUFFICIENT_MEMORY;
686 goto exit;
687 }
688
Steven Cooremana01795d2020-07-24 22:48:15 +0200689 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200690 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200691 output,
692 data_length,
693 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200694exit:
695 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200696 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200697 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200698
699 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000700 if( status != PSA_SUCCESS )
701 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200702 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000703 return( status );
704 }
705
Steven Cooremana01795d2020-07-24 22:48:15 +0200706 /* On success, store the allocated export-formatted key. */
707 slot->data.key.data = output;
708 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000709
710 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200711}
Steven Cooremana01795d2020-07-24 22:48:15 +0200712#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200713
Jaeden Ameroccdce902019-01-10 11:42:27 +0000714#if defined(MBEDTLS_ECP_C)
Steven Cooreman7f391872020-07-30 14:57:44 +0200715/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200716 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200717 * \param[in] type The type of key contained in \p data.
718 * \param[in] data The buffer from which to load the representation.
719 * \param[in] data_length The size in bytes of \p data.
720 * \param[out] p_ecp Returns a pointer to an ECP context on success.
721 * The caller is responsible for freeing both the
722 * contents of the context and the context itself
723 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200724 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200725static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
726 const uint8_t *data,
727 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200728 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100729{
730 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200731 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200732 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200733 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100734
Steven Cooreman4fed4552020-08-03 14:46:03 +0200735 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
736 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100737 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200738 /* A Weierstrass public key is represented as:
739 * - The byte 0x04;
740 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
741 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200742 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200743 */
744 if( ( data_length & 1 ) == 0 )
745 return( PSA_ERROR_INVALID_ARGUMENT );
746 curve_size = data_length / 2;
747
748 /* Montgomery public keys are represented in compressed format, meaning
749 * their curve_size is equal to the amount of input. */
750
751 /* Private keys are represented in uncompressed private random integer
752 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100753 }
754
Steven Cooreman6d839f02020-07-30 11:36:45 +0200755 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200756 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200757 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200758 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200759 mbedtls_ecp_keypair_init( ecp );
760
Gilles Peskine4cd32772019-12-02 20:49:42 +0100761 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200762 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
763 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100764 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200765 {
766 status = PSA_ERROR_INVALID_ARGUMENT;
767 goto exit;
768 }
769
Steven Cooremanacda8342020-07-24 23:09:52 +0200770 status = mbedtls_to_psa_error(
771 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
772 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200773 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200774
Steven Cooreman6d839f02020-07-30 11:36:45 +0200775 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200776 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200777 {
778 /* Load the public value. */
779 status = mbedtls_to_psa_error(
780 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200781 data,
782 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200783 if( status != PSA_SUCCESS )
784 goto exit;
785
786 /* Check that the point is on the curve. */
787 status = mbedtls_to_psa_error(
788 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
789 if( status != PSA_SUCCESS )
790 goto exit;
791 }
792 else
793 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200794 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200795 status = mbedtls_to_psa_error(
796 mbedtls_ecp_read_key( ecp->grp.id,
797 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200798 data,
799 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200800 if( status != PSA_SUCCESS )
801 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200802 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200803
804 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200805exit:
806 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200807 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200808 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200809 mbedtls_free( ecp );
810 }
811
Steven Cooreman29149862020-08-05 15:43:42 +0200812 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100813}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000814
Steven Cooreman4fed4552020-08-03 14:46:03 +0200815/** Export an ECP key to export representation
816 *
817 * \param[in] type The type of key (public/private) to export
818 * \param[in] ecp The internal ECP representation from which to export
819 * \param[out] data The buffer to export to
820 * \param[in] data_size The length of the buffer to export to
821 * \param[out] data_length The amount of bytes written to \p data
822 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200823static psa_status_t psa_export_ecp_key( psa_key_type_t type,
824 mbedtls_ecp_keypair *ecp,
825 uint8_t *data,
826 size_t data_size,
827 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200828{
Steven Cooremanacda8342020-07-24 23:09:52 +0200829 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000830
Steven Cooremanacda8342020-07-24 23:09:52 +0200831 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200832 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200833 /* Check whether the public part is loaded */
834 if( mbedtls_ecp_is_zero( &ecp->Q ) )
835 {
836 /* Calculate the public key */
837 status = mbedtls_to_psa_error(
838 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
839 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
840 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200841 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200842 }
843
Steven Cooreman4fed4552020-08-03 14:46:03 +0200844 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200845 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
846 MBEDTLS_ECP_PF_UNCOMPRESSED,
847 data_length,
848 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200849 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200850 if( status != PSA_SUCCESS )
851 memset( data, 0, data_size );
852
Steven Cooreman29149862020-08-05 15:43:42 +0200853 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200854 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200855 else
856 {
Steven Cooreman29149862020-08-05 15:43:42 +0200857 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200858 return( PSA_ERROR_BUFFER_TOO_SMALL );
859
860 status = mbedtls_to_psa_error(
861 mbedtls_ecp_write_key( ecp,
862 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200863 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200864 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200865 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200866 else
867 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200868
869 return( status );
870 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200871}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200872
Steven Cooreman4fed4552020-08-03 14:46:03 +0200873/** Import an ECP key from import representation to a slot
874 *
875 * \param[in,out] slot The slot where to store the export representation to
876 * \param[in] data The buffer containing the import representation
877 * \param[in] data_length The amount of bytes in \p data
878 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200879static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
880 const uint8_t *data,
881 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100882{
Steven Cooremanacda8342020-07-24 23:09:52 +0200883 psa_status_t status;
884 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200885 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100886
Steven Cooremanacda8342020-07-24 23:09:52 +0200887 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200888 status = psa_load_ecp_representation( slot->attr.type,
889 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200890 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200891 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100892 if( status != PSA_SUCCESS )
893 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100894
Steven Cooremanacda8342020-07-24 23:09:52 +0200895 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200896 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200897 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200898 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200899
Steven Cooremanacda8342020-07-24 23:09:52 +0200900 /* Re-export the data to PSA export format. There is currently no support
901 * for other input formats then the export format, so this is a 1-1
902 * copy operation. */
903 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200904 if( output == NULL )
905 {
906 status = PSA_ERROR_INSUFFICIENT_MEMORY;
907 goto exit;
908 }
909
910 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200911 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200912 output,
913 data_length,
914 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100915exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200916 /* Always free the PK object (will also free contained ECP context) */
917 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200918 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200919
920 /* Free the allocated buffer only on error. */
921 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100922 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200923 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200924 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100925 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200926
927 /* On success, store the allocated export-formatted key. */
928 slot->data.key.data = output;
929 slot->data.key.bytes = data_length;
930
931 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100932}
933#endif /* defined(MBEDTLS_ECP_C) */
934
Gilles Peskineb46bef22019-07-30 21:32:04 +0200935/** Return the size of the key in the given slot, in bits.
936 *
937 * \param[in] slot A key slot.
938 *
939 * \return The key size in bits, read from the metadata in the slot.
940 */
941static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
942{
943 return( slot->attr.bits );
944}
945
Steven Cooreman75b74362020-07-28 14:30:13 +0200946/** Try to allocate a buffer to an empty key slot.
947 *
948 * \param[in,out] slot Key slot to attach buffer to.
949 * \param[in] buffer_length Requested size of the buffer.
950 *
951 * \retval #PSA_SUCCESS
952 * The buffer has been successfully allocated.
953 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
954 * Not enough memory was available for allocation.
955 * \retval #PSA_ERROR_ALREADY_EXISTS
956 * Trying to allocate a buffer to a non-empty key slot.
957 */
958static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
959 size_t buffer_length )
960{
961 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200962 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200963
964 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
965 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200966 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200967
968 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200969 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200970}
971
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200972psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
973 const uint8_t* data,
974 size_t data_length )
975{
976 psa_status_t status = psa_allocate_buffer_to_slot( slot,
977 data_length );
978 if( status != PSA_SUCCESS )
979 return( status );
980
981 memcpy( slot->data.key.data, data, data_length );
982 return( PSA_SUCCESS );
983}
984
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200985/** Import key data into a slot.
986 *
987 * `slot->type` must have been set previously.
988 * This function assumes that the slot does not contain any key material yet.
989 * On failure, the slot content is unchanged.
990 *
991 * Persistent storage is not affected.
992 *
993 * \param[in,out] slot The key slot to import data into.
994 * Its `type` field must have previously been set to
995 * the desired key type.
996 * It must not contain any key material yet.
997 * \param[in] data Buffer containing the key material to parse and import.
998 * \param data_length Size of \p data in bytes.
999 *
1000 * \retval #PSA_SUCCESS
1001 * \retval #PSA_ERROR_INVALID_ARGUMENT
1002 * \retval #PSA_ERROR_NOT_SUPPORTED
1003 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1004 */
1005static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1006 const uint8_t *data,
1007 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001009 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001010 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001012 /* zero-length keys are never supported. */
1013 if( data_length == 0 )
1014 return( PSA_ERROR_NOT_SUPPORTED );
1015
Gilles Peskine8e338702019-07-30 20:06:31 +02001016 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017 {
Steven Cooreman04524762020-10-13 17:43:44 +02001018 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001019
Steven Cooreman75b74362020-07-28 14:30:13 +02001020 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1021 if( data_length > SIZE_MAX / 8 )
1022 return( PSA_ERROR_NOT_SUPPORTED );
1023
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001024 /* Enforce a size limit, and in particular ensure that the bit
1025 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001026 if( bit_size > PSA_MAX_KEY_BITS )
1027 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001028
1029 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001030 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001031 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001032
1033 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001034 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001035 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001036 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001037
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001038 /* Write the actual key size to the slot.
1039 * psa_start_key_creation() wrote the size declared by the
1040 * caller, which may be 0 (meaning unspecified) or wrong. */
1041 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001042
1043 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001044 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001045 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001046 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001047 /* Try validation through accelerators first. */
1048 bit_size = slot->attr.bits;
1049 psa_key_attributes_t attributes = {
1050 .core = slot->attr
1051 };
1052 status = psa_driver_wrapper_validate_key( &attributes,
1053 data,
1054 data_length,
1055 &bit_size );
1056 if( status == PSA_SUCCESS )
1057 {
1058 /* Key has been validated successfully by an accelerator.
1059 * Copy key material into slot. */
1060 status = psa_copy_key_material_into_slot( slot, data, data_length );
1061 if( status != PSA_SUCCESS )
1062 return( status );
1063
1064 slot->attr.bits = (psa_key_bits_t) bit_size;
1065 return( PSA_SUCCESS );
1066 }
1067 else if( status != PSA_ERROR_NOT_SUPPORTED )
1068 return( status );
1069
1070 /* Key format is not supported by any accelerator, try software fallback
1071 * if present. */
Gilles Peskinef76aa772018-10-29 19:24:33 +01001072#if defined(MBEDTLS_ECP_C)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001073 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1074 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001075 return( psa_import_ecp_key( slot, data, data_length ) );
1076 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001077#endif /* defined(MBEDTLS_ECP_C) */
Steven Cooreman19fd5742020-07-24 23:31:01 +02001078#if defined(MBEDTLS_RSA_C)
Steven Cooreman40120f62020-10-29 11:42:22 +01001079 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001080 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001081 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001082 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001083#endif /* defined(MBEDTLS_RSA_C) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001084
1085 /* Fell through the fallback as well, so have nothing else to try. */
1086 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001087 }
1088 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001089 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001090 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001092 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001093}
1094
Gilles Peskinef603c712019-01-19 13:40:11 +01001095/** Calculate the intersection of two algorithm usage policies.
1096 *
1097 * Return 0 (which allows no operation) on incompatibility.
1098 */
1099static psa_algorithm_t psa_key_policy_algorithm_intersection(
1100 psa_algorithm_t alg1,
1101 psa_algorithm_t alg2 )
1102{
Gilles Peskine549ea862019-05-22 11:45:59 +02001103 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001104 if( alg1 == alg2 )
1105 return( alg1 );
1106 /* If the policies are from the same hash-and-sign family, check
1107 * if one is a wildcard. If so the other has the specific algorithm. */
1108 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1109 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1110 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1111 {
1112 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1113 return( alg2 );
1114 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1115 return( alg1 );
1116 }
1117 /* If the policies are incompatible, allow nothing. */
1118 return( 0 );
1119}
1120
Gilles Peskined6f371b2019-05-10 19:33:38 +02001121static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1122 psa_algorithm_t requested_alg )
1123{
Gilles Peskine549ea862019-05-22 11:45:59 +02001124 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001125 if( requested_alg == policy_alg )
1126 return( 1 );
1127 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001128 * and requested_alg is the same hash-and-sign family with any hash,
1129 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001130 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1131 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1132 {
1133 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1134 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1135 }
Steven Cooremance48e852020-10-05 16:02:45 +02001136 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001137 * a key derivation with that key agreement should also be allowed. This
1138 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001139 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1140 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1141 {
1142 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1143 policy_alg );
1144 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001145 /* If it isn't permitted, it's forbidden. */
1146 return( 0 );
1147}
1148
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001149/** Test whether a policy permits an algorithm.
1150 *
1151 * The caller must test usage flags separately.
1152 */
1153static int psa_key_policy_permits( const psa_key_policy_t *policy,
1154 psa_algorithm_t alg )
1155{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001156 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1157 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001158}
1159
Gilles Peskinef603c712019-01-19 13:40:11 +01001160/** Restrict a key policy based on a constraint.
1161 *
1162 * \param[in,out] policy The policy to restrict.
1163 * \param[in] constraint The policy constraint to apply.
1164 *
1165 * \retval #PSA_SUCCESS
1166 * \c *policy contains the intersection of the original value of
1167 * \c *policy and \c *constraint.
1168 * \retval #PSA_ERROR_INVALID_ARGUMENT
1169 * \c *policy and \c *constraint are incompatible.
1170 * \c *policy is unchanged.
1171 */
1172static psa_status_t psa_restrict_key_policy(
1173 psa_key_policy_t *policy,
1174 const psa_key_policy_t *constraint )
1175{
1176 psa_algorithm_t intersection_alg =
1177 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001178 psa_algorithm_t intersection_alg2 =
1179 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001180 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1181 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001182 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1183 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001184 policy->usage &= constraint->usage;
1185 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001186 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001187 return( PSA_SUCCESS );
1188}
1189
Darryl Green06fd18d2018-07-16 11:21:11 +01001190/** Retrieve a slot which must contain a key. The key must have allow all the
1191 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
Ronald Cronf95a2b12020-10-22 15:24:49 +02001192 * operations with this algorithm.
1193 *
1194 * On success, the access counter of the returned key slot is incremented by
1195 * one. It is the responsibility of the caller to call
1196 * psa_decrement_key_slot_access_count() when it does not access the key slot
1197 * anymore.
1198 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001199static psa_status_t psa_get_key_from_slot( mbedtls_svc_key_id_t key,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001200 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001201 psa_key_usage_t usage,
1202 psa_algorithm_t alg )
1203{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001204 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1205 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001206
Ronald Cronf95a2b12020-10-22 15:24:49 +02001207 status = psa_get_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001208 if( status != PSA_SUCCESS )
1209 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001210 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001211
1212 /* Enforce that usage policy for the key slot contains all the flags
1213 * required by the usage parameter. There is one exception: public
1214 * keys can always be exported, so we treat public key objects as
1215 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001216 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001217 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001218
1219 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001220 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001221 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001222
1223 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001224 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001225 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001226
Darryl Green06fd18d2018-07-16 11:21:11 +01001227 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001228
1229error:
1230 *p_slot = NULL;
1231 psa_decrement_key_slot_access_count( slot );
1232
1233 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001234}
Darryl Green940d72c2018-07-13 13:18:51 +01001235
Gilles Peskine28f8f302019-07-24 13:30:31 +02001236/** Retrieve a slot which must contain a transparent key.
1237 *
1238 * A transparent key is a key for which the key material is directly
1239 * available, as opposed to a key in a secure element.
1240 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001241 * This is a temporary function to use instead of psa_get_key_from_slot()
1242 * until secure element support is fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001243 *
1244 * On success, the access counter of the returned key slot is incremented by
1245 * one. It is the responsibility of the caller to call
1246 * psa_decrement_key_slot_access_count() when it does not access the key slot
1247 * anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001248 */
1249#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001250static psa_status_t psa_get_transparent_key( mbedtls_svc_key_id_t key,
Gilles Peskine28f8f302019-07-24 13:30:31 +02001251 psa_key_slot_t **p_slot,
1252 psa_key_usage_t usage,
1253 psa_algorithm_t alg )
1254{
Ronald Croncf56a0a2020-08-04 09:51:30 +02001255 psa_status_t status = psa_get_key_from_slot( key, p_slot, usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001256 if( status != PSA_SUCCESS )
1257 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001258
Gilles Peskineadad8132019-07-25 11:31:23 +02001259 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001260 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02001261 psa_decrement_key_slot_access_count( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001262 *p_slot = NULL;
1263 return( PSA_ERROR_NOT_SUPPORTED );
1264 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001265
Gilles Peskine28f8f302019-07-24 13:30:31 +02001266 return( PSA_SUCCESS );
1267}
1268#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1269/* With no secure element support, all keys are transparent. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001270#define psa_get_transparent_key( key, p_slot, usage, alg ) \
1271 psa_get_key_from_slot( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001272#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1273
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001274/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001275static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001276{
Gilles Peskine73167e12019-07-12 23:44:37 +02001277#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1278 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001279 {
1280 /* No key material to clean. */
1281 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001282 else
1283#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001284 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001285 /* Data pointer will always be either a valid pointer or NULL in an
1286 * initialized slot, so we can just free it. */
1287 if( slot->data.key.data != NULL )
1288 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001289 mbedtls_free( slot->data.key.data );
1290 slot->data.key.data = NULL;
1291 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001292 }
Darryl Green40225ba2018-11-15 14:48:15 +00001293
1294 return( PSA_SUCCESS );
1295}
1296
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001297/** Completely wipe a slot in memory, including its policy.
1298 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001299psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001300{
1301 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001302
1303 /*
1304 * As the return error code may not be handled in case of multiple errors,
1305 * do our best to report an unexpected access counter: if available
1306 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1307 * part of the execution of a test suite this will stop the test suite
1308 * execution) and if MBEDTLS_PARAM_FAILED does not terminate execution
1309 * ouput an error message on standard error output.
1310 */
1311 if( slot->access_count != 1 )
1312 {
1313#ifdef MBEDTLS_CHECK_PARAMS
1314 MBEDTLS_PARAM_FAILED( slot->access_count == 1 );
1315#endif
1316#ifdef MBEDTLS_PLATFORM_C
1317 mbedtls_fprintf( stderr,
1318 "\nFATAL psa_wipe_key_slot Unexpected access counter value\n.");
1319#endif
1320 status = PSA_ERROR_CORRUPTION_DETECTED;
1321 }
1322
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001323 /* Multipart operations may still be using the key. This is safe
1324 * because all multipart operation objects are independent from
1325 * the key slot: if they need to access the key after the setup
1326 * phase, they have a copy of the key. Note that this means that
1327 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001328 /* At this point, key material and other type-specific content has
1329 * been wiped. Clear remaining metadata. We can call memset and not
1330 * zeroize because the metadata is not particularly sensitive. */
1331 memset( slot, 0, sizeof( *slot ) );
1332 return( status );
1333}
1334
Ronald Croncf56a0a2020-08-04 09:51:30 +02001335psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001336{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001337 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001338 psa_status_t status; /* status of the last operation */
1339 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001340#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1341 psa_se_drv_table_entry_t *driver;
1342#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001343
Ronald Croncf56a0a2020-08-04 09:51:30 +02001344 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001345 return( PSA_SUCCESS );
1346
Ronald Cronf2911112020-10-29 17:51:10 +01001347 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001348 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001349 * key, this will load the key description from persistent memory if not
1350 * done yet. We cannot avoid this loading as without it we don't know if
1351 * the key is operated by an SE or not and this information is needed by
1352 * the current implementation.
1353 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001354 status = psa_get_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001355 if( status != PSA_SUCCESS )
1356 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001357
Ronald Cronf2911112020-10-29 17:51:10 +01001358 /*
1359 * If the key slot containing the key description is under access by the
1360 * library (apart from the present access), the key cannot be destroyed
1361 * yet. For the time being, just return in error. Eventually (to be
1362 * implemented), the key should be destroyed when all accesses have
1363 * stopped.
1364 */
1365 if( slot->access_count > 1 )
1366 {
1367 psa_decrement_key_slot_access_count( slot );
1368 return( PSA_ERROR_GENERIC_ERROR );
1369 }
1370
Gilles Peskine354f7672019-07-12 23:46:38 +02001371#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001372 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001373 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001374 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001375 /* For a key in a secure element, we need to do three things:
1376 * remove the key file in internal storage, destroy the
1377 * key inside the secure element, and update the driver's
1378 * persistent data. Start a transaction that will encompass these
1379 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001380 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001381 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001382 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001383 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001384 status = psa_crypto_save_transaction( );
1385 if( status != PSA_SUCCESS )
1386 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001387 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001388 /* We should still try to destroy the key in the secure
1389 * element and the key metadata in storage. This is especially
1390 * important if the error is that the storage is full.
1391 * But how to do it exactly without risking an inconsistent
1392 * state after a reset?
1393 * https://github.com/ARMmbed/mbed-crypto/issues/215
1394 */
1395 overall_status = status;
1396 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001397 }
1398
Gilles Peskine354f7672019-07-12 23:46:38 +02001399 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001400 if( overall_status == PSA_SUCCESS )
1401 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001402 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001403#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1404
Darryl Greend49a4992018-06-18 17:27:26 +01001405#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001406 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001407 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001408 status = psa_destroy_persistent_key( slot->attr.id );
1409 if( overall_status == PSA_SUCCESS )
1410 overall_status = status;
1411
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001412 /* TODO: other slots may have a copy of the same key. We should
1413 * invalidate them.
1414 * https://github.com/ARMmbed/mbed-crypto/issues/214
1415 */
Darryl Greend49a4992018-06-18 17:27:26 +01001416 }
1417#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001418
Gilles Peskinefc762652019-07-22 19:30:34 +02001419#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1420 if( driver != NULL )
1421 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001422 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001423 if( overall_status == PSA_SUCCESS )
1424 overall_status = status;
1425 status = psa_crypto_stop_transaction( );
1426 if( overall_status == PSA_SUCCESS )
1427 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001428 }
1429#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1430
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001431#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1432exit:
1433#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001434 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001435 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1436 if( overall_status == PSA_SUCCESS )
1437 overall_status = status;
1438 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001439}
1440
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001441void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001442{
Gilles Peskineb699f072019-04-26 16:06:02 +02001443 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001444 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001445}
1446
Gilles Peskineb699f072019-04-26 16:06:02 +02001447psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1448 psa_key_type_t type,
1449 const uint8_t *data,
1450 size_t data_length )
1451{
1452 uint8_t *copy = NULL;
1453
1454 if( data_length != 0 )
1455 {
1456 copy = mbedtls_calloc( 1, data_length );
1457 if( copy == NULL )
1458 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1459 memcpy( copy, data, data_length );
1460 }
1461 /* After this point, this function is guaranteed to succeed, so it
1462 * can start modifying `*attributes`. */
1463
1464 if( attributes->domain_parameters != NULL )
1465 {
1466 mbedtls_free( attributes->domain_parameters );
1467 attributes->domain_parameters = NULL;
1468 attributes->domain_parameters_size = 0;
1469 }
1470
1471 attributes->domain_parameters = copy;
1472 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001473 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001474 return( PSA_SUCCESS );
1475}
1476
1477psa_status_t psa_get_key_domain_parameters(
1478 const psa_key_attributes_t *attributes,
1479 uint8_t *data, size_t data_size, size_t *data_length )
1480{
1481 if( attributes->domain_parameters_size > data_size )
1482 return( PSA_ERROR_BUFFER_TOO_SMALL );
1483 *data_length = attributes->domain_parameters_size;
1484 if( attributes->domain_parameters_size != 0 )
1485 memcpy( data, attributes->domain_parameters,
1486 attributes->domain_parameters_size );
1487 return( PSA_SUCCESS );
1488}
1489
1490#if defined(MBEDTLS_RSA_C)
1491static psa_status_t psa_get_rsa_public_exponent(
1492 const mbedtls_rsa_context *rsa,
1493 psa_key_attributes_t *attributes )
1494{
1495 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001496 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001497 uint8_t *buffer = NULL;
1498 size_t buflen;
1499 mbedtls_mpi_init( &mpi );
1500
1501 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1502 if( ret != 0 )
1503 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001504 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1505 {
1506 /* It's the default value, which is reported as an empty string,
1507 * so there's nothing to do. */
1508 goto exit;
1509 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001510
1511 buflen = mbedtls_mpi_size( &mpi );
1512 buffer = mbedtls_calloc( 1, buflen );
1513 if( buffer == NULL )
1514 {
1515 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1516 goto exit;
1517 }
1518 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1519 if( ret != 0 )
1520 goto exit;
1521 attributes->domain_parameters = buffer;
1522 attributes->domain_parameters_size = buflen;
1523
1524exit:
1525 mbedtls_mpi_free( &mpi );
1526 if( ret != 0 )
1527 mbedtls_free( buffer );
1528 return( mbedtls_to_psa_error( ret ) );
1529}
1530#endif /* MBEDTLS_RSA_C */
1531
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001532/** Retrieve all the publicly-accessible attributes of a key.
1533 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001534psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001535 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001536{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001537 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1538 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001539 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001540
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001541 psa_reset_key_attributes( attributes );
1542
Ronald Croncf56a0a2020-08-04 09:51:30 +02001543 status = psa_get_key_from_slot( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001544 if( status != PSA_SUCCESS )
1545 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001546
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001547 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001548 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1549 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1550
1551#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1552 if( psa_key_slot_is_external( slot ) )
1553 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1554#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001555
Gilles Peskine8e338702019-07-30 20:06:31 +02001556 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001557 {
1558#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001559 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001560 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001561#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001562 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001563 * is not yet implemented.
1564 * https://github.com/ARMmbed/mbed-crypto/issues/216
1565 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001566 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001567 break;
1568#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001569 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001570 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001571
Steven Cooreman4fed4552020-08-03 14:46:03 +02001572 status = psa_load_rsa_representation( slot->attr.type,
1573 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001574 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001575 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001576 if( status != PSA_SUCCESS )
1577 break;
1578
Steven Cooremana2371e52020-07-28 14:30:39 +02001579 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001580 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001581 mbedtls_rsa_free( rsa );
1582 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001583 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001584 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001585#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001586 default:
1587 /* Nothing else to do. */
1588 break;
1589 }
1590
1591 if( status != PSA_SUCCESS )
1592 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001593
1594 decrement_status = psa_decrement_key_slot_access_count( slot );
1595
1596 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001597}
1598
Gilles Peskinec8000c02019-08-02 20:15:51 +02001599#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1600psa_status_t psa_get_key_slot_number(
1601 const psa_key_attributes_t *attributes,
1602 psa_key_slot_number_t *slot_number )
1603{
1604 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1605 {
1606 *slot_number = attributes->slot_number;
1607 return( PSA_SUCCESS );
1608 }
1609 else
1610 return( PSA_ERROR_INVALID_ARGUMENT );
1611}
1612#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1613
Steven Cooremana01795d2020-07-24 22:48:15 +02001614static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1615 uint8_t *data,
1616 size_t data_size,
1617 size_t *data_length )
1618{
1619 if( slot->data.key.bytes > data_size )
1620 return( PSA_ERROR_BUFFER_TOO_SMALL );
1621 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1622 memset( data + slot->data.key.bytes, 0,
1623 data_size - slot->data.key.bytes );
1624 *data_length = slot->data.key.bytes;
1625 return( PSA_SUCCESS );
1626}
1627
Gilles Peskinef603c712019-01-19 13:40:11 +01001628static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1629 uint8_t *data,
1630 size_t data_size,
1631 size_t *data_length,
1632 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001633{
Gilles Peskine5d309672019-07-12 23:47:28 +02001634#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1635 const psa_drv_se_t *drv;
1636 psa_drv_se_context_t *drv_context;
1637#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1638
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001639 *data_length = 0;
1640
Gilles Peskine8e338702019-07-30 20:06:31 +02001641 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001642 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001643
Gilles Peskinef9168942019-09-12 19:20:29 +02001644 /* Reject a zero-length output buffer now, since this can never be a
1645 * valid key representation. This way we know that data must be a valid
1646 * pointer and we can do things like memset(data, ..., data_size). */
1647 if( data_size == 0 )
1648 return( PSA_ERROR_BUFFER_TOO_SMALL );
1649
Gilles Peskine5d309672019-07-12 23:47:28 +02001650#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001651 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001652 {
1653 psa_drv_se_export_key_t method;
1654 if( drv->key_management == NULL )
1655 return( PSA_ERROR_NOT_SUPPORTED );
1656 method = ( export_public_key ?
1657 drv->key_management->p_export_public :
1658 drv->key_management->p_export );
1659 if( method == NULL )
1660 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001661 return( method( drv_context,
1662 slot->data.se.slot_number,
1663 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001664 }
1665#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1666
Gilles Peskine8e338702019-07-30 20:06:31 +02001667 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001668 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001669 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001670 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001671 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1672 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001673 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001674 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001675 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001676 /* Exporting public -> public */
1677 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1678 }
1679 else if( !export_public_key )
1680 {
1681 /* Exporting private -> private */
1682 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1683 }
1684 /* Need to export the public part of a private key,
1685 * so conversion is needed */
1686 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1687 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001688#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001689 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001690 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001691 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001692 slot->data.key.data,
1693 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001694 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001695 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001696 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001697
Steven Cooreman560c28a2020-07-24 23:20:24 +02001698 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001699 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001700 data,
1701 data_size,
1702 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001703
Steven Cooremana2371e52020-07-28 14:30:39 +02001704 mbedtls_rsa_free( rsa );
1705 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001706
Steven Cooreman560c28a2020-07-24 23:20:24 +02001707 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001708#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001709 /* We don't know how to convert a private RSA key to public. */
1710 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001711#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001712 }
1713 else
Moran Pekera998bc62018-04-16 18:16:20 +03001714 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001715#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001716 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001717 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001718 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001719 slot->data.key.data,
1720 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001721 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001722 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001723 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001724
1725 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1726 PSA_KEY_TYPE_ECC_GET_FAMILY(
1727 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001728 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001729 data,
1730 data_size,
1731 data_length );
1732
Steven Cooremana2371e52020-07-28 14:30:39 +02001733 mbedtls_ecp_keypair_free( ecp );
1734 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001735 return( status );
1736#else
1737 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001738 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001739#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001740 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001741 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001742 else
1743 {
1744 /* This shouldn't happen in the reference implementation, but
1745 it is valid for a special-purpose implementation to omit
1746 support for exporting certain key types. */
1747 return( PSA_ERROR_NOT_SUPPORTED );
1748 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001749}
1750
Ronald Croncf56a0a2020-08-04 09:51:30 +02001751psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001752 uint8_t *data,
1753 size_t data_size,
1754 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001755{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001756 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1757 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001758 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001759
1760 /* Set the key to empty now, so that even when there are errors, we always
1761 * set data_length to a value between 0 and data_size. On error, setting
1762 * the key to empty is a good choice because an empty key representation is
1763 * unlikely to be accepted anywhere. */
1764 *data_length = 0;
1765
1766 /* Export requires the EXPORT flag. There is an exception for public keys,
1767 * which don't require any flag, but psa_get_key_from_slot takes
1768 * care of this. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001769 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001770 if( status != PSA_SUCCESS )
1771 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001772
1773 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
1774 decrement_status = psa_decrement_key_slot_access_count( slot );
1775
1776 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001777}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001778
Ronald Croncf56a0a2020-08-04 09:51:30 +02001779psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001780 uint8_t *data,
1781 size_t data_size,
1782 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001783{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001784 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1785 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001786 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001787
1788 /* Set the key to empty now, so that even when there are errors, we always
1789 * set data_length to a value between 0 and data_size. On error, setting
1790 * the key to empty is a good choice because an empty key representation is
1791 * unlikely to be accepted anywhere. */
1792 *data_length = 0;
1793
1794 /* Exporting a public key doesn't require a usage flag. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001795 status = psa_get_key_from_slot( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001796 if( status != PSA_SUCCESS )
1797 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001798
1799 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
1800 decrement_status = psa_decrement_key_slot_access_count( slot );
1801
1802 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001803}
1804
Gilles Peskine91e8c332019-08-02 19:19:39 +02001805#if defined(static_assert)
1806static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1807 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001808static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001809 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001810static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001811 "One or more key attribute flag is listed as both internal-only and external-only" );
1812#endif
1813
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001814/** Validate that a key policy is internally well-formed.
1815 *
1816 * This function only rejects invalid policies. It does not validate the
1817 * consistency of the policy with respect to other attributes of the key
1818 * such as the key type.
1819 */
1820static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001821{
1822 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001823 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001824 PSA_KEY_USAGE_ENCRYPT |
1825 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001826 PSA_KEY_USAGE_SIGN_HASH |
1827 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001828 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1829 return( PSA_ERROR_INVALID_ARGUMENT );
1830
Gilles Peskine4747d192019-04-17 15:05:45 +02001831 return( PSA_SUCCESS );
1832}
1833
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001834/** Validate the internal consistency of key attributes.
1835 *
1836 * This function only rejects invalid attribute values. If does not
1837 * validate the consistency of the attributes with any key data that may
1838 * be involved in the creation of the key.
1839 *
1840 * Call this function early in the key creation process.
1841 *
1842 * \param[in] attributes Key attributes for the new key.
1843 * \param[out] p_drv On any return, the driver for the key, if any.
1844 * NULL for a transparent key.
1845 *
1846 */
1847static psa_status_t psa_validate_key_attributes(
1848 const psa_key_attributes_t *attributes,
1849 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001850{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001851 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001852 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001853 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001854
Ronald Cron54b90082020-10-29 15:26:43 +01001855 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001856 if( status != PSA_SUCCESS )
1857 return( status );
1858
Ronald Crond2ed4812020-07-17 16:11:30 +02001859 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001860 if( status != PSA_SUCCESS )
1861 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001862
Ronald Cron65f38a32020-10-23 17:11:13 +02001863 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1864 {
1865 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1866 return( PSA_ERROR_INVALID_ARGUMENT );
1867 }
1868 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001869 {
Ronald Cron54b90082020-10-29 15:26:43 +01001870 status = psa_validate_key_id( psa_get_key_id( attributes ), 0, 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001871 if( status != PSA_SUCCESS )
1872 return( status );
1873 }
1874
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001875 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001876 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001877 return( status );
1878
1879 /* Refuse to create overly large keys.
1880 * Note that this doesn't trigger on import if the attributes don't
1881 * explicitly specify a size (so psa_get_key_bits returns 0), so
1882 * psa_import_key() needs its own checks. */
1883 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1884 return( PSA_ERROR_NOT_SUPPORTED );
1885
Gilles Peskine91e8c332019-08-02 19:19:39 +02001886 /* Reject invalid flags. These should not be reachable through the API. */
1887 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1888 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1889 return( PSA_ERROR_INVALID_ARGUMENT );
1890
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001891 return( PSA_SUCCESS );
1892}
1893
Gilles Peskine4747d192019-04-17 15:05:45 +02001894/** Prepare a key slot to receive key material.
1895 *
1896 * This function allocates a key slot and sets its metadata.
1897 *
1898 * If this function fails, call psa_fail_key_creation().
1899 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001900 * This function is intended to be used as follows:
1901 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001902 * it with the specified attributes, and in case of a volatile key assign it
1903 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001904 * -# Populate the slot with the key material.
1905 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1906 * In case of failure at any step, stop the sequence and call
1907 * psa_fail_key_creation().
1908 *
Ronald Cronf95a2b12020-10-22 15:24:49 +02001909 * On success, the access counter of the returned key slot is incremented by
1910 * one. It is the responsibility of the caller to call
1911 * psa_decrement_key_slot_access_count() when it does not access the key slot
1912 * anymore.
1913 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001914 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001915 * \param[in] attributes Key attributes for the new key.
Ronald Cron3c76a422020-10-16 12:17:04 +02001916 * \param[out] key On success, identifier of the key. Note that the
1917 * key identifier is also stored in the prepared
1918 * slot.
Gilles Peskine011e4282019-06-26 18:34:38 +02001919 * \param[out] p_slot On success, a pointer to the prepared slot.
1920 * \param[out] p_drv On any return, the driver for the key, if any.
1921 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001922 *
1923 * \retval #PSA_SUCCESS
1924 * The key slot is ready to receive key material.
1925 * \return If this function fails, the key slot is an invalid state.
1926 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001927 */
1928static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001929 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001930 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001931 mbedtls_svc_key_id_t *key,
Gilles Peskine011e4282019-06-26 18:34:38 +02001932 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001933 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001934{
1935 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001936 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001937 psa_key_slot_t *slot;
1938
Gilles Peskinedf179142019-07-15 22:02:14 +02001939 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001940 *p_drv = NULL;
1941
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001942 status = psa_validate_key_attributes( attributes, p_drv );
1943 if( status != PSA_SUCCESS )
1944 return( status );
1945
Ronald Cronc4d1b512020-07-31 11:26:37 +02001946 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001947 if( status != PSA_SUCCESS )
1948 return( status );
1949 slot = *p_slot;
1950
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001951 /* We're storing the declared bit-size of the key. It's up to each
1952 * creation mechanism to verify that this information is correct.
1953 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001954 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001955 * is optional (import, copy). In case of a volatile key, assign it the
1956 * volatile key identifier associated to the slot returned to contain its
1957 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001958
1959 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001960 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1961 {
1962#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1963 slot->attr.id = volatile_key_id;
1964#else
1965 slot->attr.id.key_id = volatile_key_id;
1966#endif
1967 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001968
Gilles Peskine91e8c332019-08-02 19:19:39 +02001969 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001970 * external-only flags, query `attributes`. Thanks to the check
1971 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001972 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001973 * may have set. */
1974 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001975
Gilles Peskinecbaff462019-07-12 23:46:04 +02001976#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001977 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001978 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001979 * create the key file in internal storage, create the
1980 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001981 * persistent data. This is done by starting a transaction that will
1982 * encompass these three actions.
1983 * For registering a volatile key, we just need to find an appropriate
1984 * slot number inside the SE. Since the key is designated volatile, creating
1985 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001986 /* The first thing to do is to find a slot number for the new key.
1987 * We save the slot number in persistent storage as part of the
1988 * transaction data. It will be needed to recover if the power
1989 * fails during the key creation process, to clean up on the secure
1990 * element side after restarting. Obtaining a slot number from the
1991 * secure element driver updates its persistent state, but we do not yet
1992 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001993 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001994 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001995 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001996 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001997 &slot->data.se.slot_number );
1998 if( status != PSA_SUCCESS )
1999 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002000
2001 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002002 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002003 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2004 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
2005 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
2006 psa_crypto_transaction.key.id = slot->attr.id;
2007 status = psa_crypto_save_transaction( );
2008 if( status != PSA_SUCCESS )
2009 {
2010 (void) psa_crypto_stop_transaction( );
2011 return( status );
2012 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002013 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002014 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002015
2016 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2017 {
2018 /* Key registration only makes sense with a secure element. */
2019 return( PSA_ERROR_INVALID_ARGUMENT );
2020 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002021#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2022
Ronald Croncf56a0a2020-08-04 09:51:30 +02002023 *key = slot->attr.id;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002024 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002025}
Gilles Peskine4747d192019-04-17 15:05:45 +02002026
2027/** Finalize the creation of a key once its key material has been set.
2028 *
2029 * This entails writing the key to persistent storage.
2030 *
2031 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002032 * See the documentation of psa_start_key_creation() for the intended use
2033 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002034 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002035 * \param[in,out] slot Pointer to the slot with key material.
2036 * \param[in] driver The secure element driver for the key,
2037 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002038 *
2039 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002040 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002041 * \return If this function fails, the key slot is an invalid state.
2042 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002043 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002044static psa_status_t psa_finish_key_creation(
2045 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002046 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002047{
2048 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002049 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002050 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002051
2052#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002053 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002054 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002055#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2056 if( driver != NULL )
2057 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002058 psa_se_key_data_storage_t data;
2059#if defined(static_assert)
2060 static_assert( sizeof( slot->data.se.slot_number ) ==
2061 sizeof( data.slot_number ),
2062 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002063#endif
2064 memcpy( &data.slot_number, &slot->data.se.slot_number,
2065 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002066 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002067 (uint8_t*) &data,
2068 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002069 }
2070 else
2071#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2072 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002073 /* Key material is saved in export representation in the slot, so
2074 * just pass the slot buffer for storage. */
2075 status = psa_save_persistent_key( &slot->attr,
2076 slot->data.key.data,
2077 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002078 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002079 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002080#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2081
Gilles Peskinecbaff462019-07-12 23:46:04 +02002082#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002083 /* Finish the transaction for a key creation. This does not
2084 * happen when registering an existing key. Detect this case
2085 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002086 * creation of a persistent key in a secure element requires a transaction,
2087 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002088 if( driver != NULL &&
2089 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002090 {
2091 status = psa_save_se_persistent_data( driver );
2092 if( status != PSA_SUCCESS )
2093 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002094 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002095 return( status );
2096 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002097 status = psa_crypto_stop_transaction( );
2098 if( status != PSA_SUCCESS )
2099 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002100 }
2101#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2102
Gilles Peskine4747d192019-04-17 15:05:45 +02002103 return( status );
2104}
2105
2106/** Abort the creation of a key.
2107 *
2108 * You may call this function after calling psa_start_key_creation(),
2109 * or after psa_finish_key_creation() fails. In other circumstances, this
2110 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002111 * See the documentation of psa_start_key_creation() for the intended use
2112 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002113 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002114 * \param[in,out] slot Pointer to the slot with key material.
2115 * \param[in] driver The secure element driver for the key,
2116 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002117 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002118static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002119 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002120{
Gilles Peskine011e4282019-06-26 18:34:38 +02002121 (void) driver;
2122
Gilles Peskine4747d192019-04-17 15:05:45 +02002123 if( slot == NULL )
2124 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002125
2126#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002127 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002128 * element, and the failure happened later (when saving metadata
2129 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002130 * element.
2131 * https://github.com/ARMmbed/mbed-crypto/issues/217
2132 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002133
Gilles Peskined7729582019-08-05 15:55:54 +02002134 /* Abort the ongoing transaction if any (there may not be one if
2135 * the creation process failed before starting one, or if the
2136 * key creation is a registration of a key in a secure element).
2137 * Earlier functions must already have done what it takes to undo any
2138 * partial creation. All that's left is to update the transaction data
2139 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002140 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002141#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2142
Gilles Peskine4747d192019-04-17 15:05:45 +02002143 psa_wipe_key_slot( slot );
2144}
2145
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002146/** Validate optional attributes during key creation.
2147 *
2148 * Some key attributes are optional during key creation. If they are
2149 * specified in the attributes structure, check that they are consistent
2150 * with the data in the slot.
2151 *
2152 * This function should be called near the end of key creation, after
2153 * the slot in memory is fully populated but before saving persistent data.
2154 */
2155static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002156 const psa_key_slot_t *slot,
2157 const psa_key_attributes_t *attributes )
2158{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002159 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002160 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002161 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002162 return( PSA_ERROR_INVALID_ARGUMENT );
2163 }
2164
2165 if( attributes->domain_parameters_size != 0 )
2166 {
2167#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002168 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002169 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002170 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002171 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002173
Steven Cooreman7f391872020-07-30 14:57:44 +02002174 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002175 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002176 slot->data.key.data,
2177 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002178 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002179 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002180 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002181
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002182 mbedtls_mpi_init( &actual );
2183 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002184 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002185 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002186 mbedtls_rsa_free( rsa );
2187 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002188 if( ret != 0 )
2189 goto rsa_exit;
2190 ret = mbedtls_mpi_read_binary( &required,
2191 attributes->domain_parameters,
2192 attributes->domain_parameters_size );
2193 if( ret != 0 )
2194 goto rsa_exit;
2195 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2196 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2197 rsa_exit:
2198 mbedtls_mpi_free( &actual );
2199 mbedtls_mpi_free( &required );
2200 if( ret != 0)
2201 return( mbedtls_to_psa_error( ret ) );
2202 }
2203 else
2204#endif
2205 {
2206 return( PSA_ERROR_INVALID_ARGUMENT );
2207 }
2208 }
2209
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002210 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002211 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002212 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002213 return( PSA_ERROR_INVALID_ARGUMENT );
2214 }
2215
2216 return( PSA_SUCCESS );
2217}
2218
Gilles Peskine4747d192019-04-17 15:05:45 +02002219psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002220 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002221 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002222 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002223{
2224 psa_status_t status;
2225 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002226 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002227
Gilles Peskine0f84d622019-09-12 19:03:13 +02002228 /* Reject zero-length symmetric keys (including raw data key objects).
2229 * This also rejects any key which might be encoded as an empty string,
2230 * which is never valid. */
2231 if( data_length == 0 )
2232 return( PSA_ERROR_INVALID_ARGUMENT );
2233
Gilles Peskinedf179142019-07-15 22:02:14 +02002234 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002235 key, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002236 if( status != PSA_SUCCESS )
2237 goto exit;
2238
Gilles Peskine5d309672019-07-12 23:47:28 +02002239#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2240 if( driver != NULL )
2241 {
2242 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002243 /* The driver should set the number of key bits, however in
2244 * case it doesn't, we initialize bits to an invalid value. */
2245 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002246 if( drv->key_management == NULL ||
2247 drv->key_management->p_import == NULL )
2248 {
2249 status = PSA_ERROR_NOT_SUPPORTED;
2250 goto exit;
2251 }
2252 status = drv->key_management->p_import(
2253 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002254 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002255 &bits );
2256 if( status != PSA_SUCCESS )
2257 goto exit;
2258 if( bits > PSA_MAX_KEY_BITS )
2259 {
2260 status = PSA_ERROR_NOT_SUPPORTED;
2261 goto exit;
2262 }
2263 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002264 }
2265 else
2266#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2267 {
2268 status = psa_import_key_into_slot( slot, data, data_length );
2269 if( status != PSA_SUCCESS )
2270 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002271 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002272 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002273 if( status != PSA_SUCCESS )
2274 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002275
Gilles Peskine011e4282019-06-26 18:34:38 +02002276 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002277exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002278 if( status != PSA_SUCCESS )
2279 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002280 psa_fail_key_creation( slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002281 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4747d192019-04-17 15:05:45 +02002282 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002283 else
2284 status = psa_decrement_key_slot_access_count( slot );
2285
Gilles Peskine4747d192019-04-17 15:05:45 +02002286 return( status );
2287}
2288
Gilles Peskined7729582019-08-05 15:55:54 +02002289#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2290psa_status_t mbedtls_psa_register_se_key(
2291 const psa_key_attributes_t *attributes )
2292{
2293 psa_status_t status;
2294 psa_key_slot_t *slot = NULL;
2295 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002296 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002297
2298 /* Leaving attributes unspecified is not currently supported.
2299 * It could make sense to query the key type and size from the
2300 * secure element, but not all secure elements support this
2301 * and the driver HAL doesn't currently support it. */
2302 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2303 return( PSA_ERROR_NOT_SUPPORTED );
2304 if( psa_get_key_bits( attributes ) == 0 )
2305 return( PSA_ERROR_NOT_SUPPORTED );
2306
2307 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002308 &key, &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002309 if( status != PSA_SUCCESS )
2310 goto exit;
2311
Gilles Peskined7729582019-08-05 15:55:54 +02002312 status = psa_finish_key_creation( slot, driver );
2313
2314exit:
2315 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002316 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002317 else
2318 status = psa_decrement_key_slot_access_count( slot );
2319
Gilles Peskined7729582019-08-05 15:55:54 +02002320 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002321 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002322 return( status );
2323}
2324#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2325
Gilles Peskinef603c712019-01-19 13:40:11 +01002326static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002327 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002328{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002329 psa_status_t status = psa_copy_key_material_into_slot( target,
2330 source->data.key.data,
2331 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002332 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002333 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002334
Steven Cooreman398aee52020-10-13 14:35:45 +02002335 target->attr.type = source->attr.type;
2336 target->attr.bits = source->attr.bits;
2337
2338 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002339}
2340
Ronald Croncf56a0a2020-08-04 09:51:30 +02002341psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002342 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002343 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002344{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002345 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2346 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002347 psa_key_slot_t *source_slot = NULL;
2348 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002349 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002350 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002351
Ronald Croncf56a0a2020-08-04 09:51:30 +02002352 status = psa_get_transparent_key( source_key, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002353 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002354 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002355 goto exit;
2356
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002357 status = psa_validate_optional_attributes( source_slot,
2358 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002359 if( status != PSA_SUCCESS )
2360 goto exit;
2361
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002362 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002363 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002364 if( status != PSA_SUCCESS )
2365 goto exit;
2366
Gilles Peskinedf179142019-07-15 22:02:14 +02002367 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2368 &actual_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002369 target_key, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002370 if( status != PSA_SUCCESS )
2371 goto exit;
2372
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002373#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2374 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002375 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002376 /* Copying to a secure element is not implemented yet. */
2377 status = PSA_ERROR_NOT_SUPPORTED;
2378 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002379 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002380#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002381
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002382 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002383 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002384 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002385
Gilles Peskine011e4282019-06-26 18:34:38 +02002386 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002387exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002388 if( status != PSA_SUCCESS )
2389 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002390 psa_fail_key_creation( target_slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002391 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002392 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002393 else
2394 status = psa_decrement_key_slot_access_count( target_slot );
2395
2396 decrement_status = psa_decrement_key_slot_access_count( source_slot );
2397
2398 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002399}
2400
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002401
2402
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002403/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002404/* Message digests */
2405/****************************************************************/
2406
John Durkop0ea39e02020-10-13 19:58:20 -07002407#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002408static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002409{
2410 switch( alg )
2411 {
2412#if defined(MBEDTLS_MD2_C)
2413 case PSA_ALG_MD2:
2414 return( &mbedtls_md2_info );
2415#endif
2416#if defined(MBEDTLS_MD4_C)
2417 case PSA_ALG_MD4:
2418 return( &mbedtls_md4_info );
2419#endif
2420#if defined(MBEDTLS_MD5_C)
2421 case PSA_ALG_MD5:
2422 return( &mbedtls_md5_info );
2423#endif
2424#if defined(MBEDTLS_RIPEMD160_C)
2425 case PSA_ALG_RIPEMD160:
2426 return( &mbedtls_ripemd160_info );
2427#endif
2428#if defined(MBEDTLS_SHA1_C)
2429 case PSA_ALG_SHA_1:
2430 return( &mbedtls_sha1_info );
2431#endif
2432#if defined(MBEDTLS_SHA256_C)
2433 case PSA_ALG_SHA_224:
2434 return( &mbedtls_sha224_info );
2435 case PSA_ALG_SHA_256:
2436 return( &mbedtls_sha256_info );
2437#endif
2438#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002439#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002440 case PSA_ALG_SHA_384:
2441 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002442#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002443 case PSA_ALG_SHA_512:
2444 return( &mbedtls_sha512_info );
2445#endif
2446 default:
2447 return( NULL );
2448 }
2449}
John Durkop0ea39e02020-10-13 19:58:20 -07002450#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002451
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002452psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2453{
2454 switch( operation->alg )
2455 {
Gilles Peskine81736312018-06-26 15:04:31 +02002456 case 0:
2457 /* The object has (apparently) been initialized but it is not
2458 * in use. It's ok to call abort on such an object, and there's
2459 * nothing to do. */
2460 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002461#if defined(MBEDTLS_MD2_C)
2462 case PSA_ALG_MD2:
2463 mbedtls_md2_free( &operation->ctx.md2 );
2464 break;
2465#endif
2466#if defined(MBEDTLS_MD4_C)
2467 case PSA_ALG_MD4:
2468 mbedtls_md4_free( &operation->ctx.md4 );
2469 break;
2470#endif
2471#if defined(MBEDTLS_MD5_C)
2472 case PSA_ALG_MD5:
2473 mbedtls_md5_free( &operation->ctx.md5 );
2474 break;
2475#endif
2476#if defined(MBEDTLS_RIPEMD160_C)
2477 case PSA_ALG_RIPEMD160:
2478 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2479 break;
2480#endif
2481#if defined(MBEDTLS_SHA1_C)
2482 case PSA_ALG_SHA_1:
2483 mbedtls_sha1_free( &operation->ctx.sha1 );
2484 break;
2485#endif
2486#if defined(MBEDTLS_SHA256_C)
2487 case PSA_ALG_SHA_224:
2488 case PSA_ALG_SHA_256:
2489 mbedtls_sha256_free( &operation->ctx.sha256 );
2490 break;
2491#endif
2492#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002493#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002494 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002495#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002496 case PSA_ALG_SHA_512:
2497 mbedtls_sha512_free( &operation->ctx.sha512 );
2498 break;
2499#endif
2500 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002501 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002502 }
2503 operation->alg = 0;
2504 return( PSA_SUCCESS );
2505}
2506
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002507psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002508 psa_algorithm_t alg )
2509{
Janos Follath24eed8d2019-11-22 13:21:35 +00002510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002511
2512 /* A context must be freshly initialized before it can be set up. */
2513 if( operation->alg != 0 )
2514 {
2515 return( PSA_ERROR_BAD_STATE );
2516 }
2517
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002518 switch( alg )
2519 {
2520#if defined(MBEDTLS_MD2_C)
2521 case PSA_ALG_MD2:
2522 mbedtls_md2_init( &operation->ctx.md2 );
2523 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2524 break;
2525#endif
2526#if defined(MBEDTLS_MD4_C)
2527 case PSA_ALG_MD4:
2528 mbedtls_md4_init( &operation->ctx.md4 );
2529 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2530 break;
2531#endif
2532#if defined(MBEDTLS_MD5_C)
2533 case PSA_ALG_MD5:
2534 mbedtls_md5_init( &operation->ctx.md5 );
2535 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2536 break;
2537#endif
2538#if defined(MBEDTLS_RIPEMD160_C)
2539 case PSA_ALG_RIPEMD160:
2540 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2541 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2542 break;
2543#endif
2544#if defined(MBEDTLS_SHA1_C)
2545 case PSA_ALG_SHA_1:
2546 mbedtls_sha1_init( &operation->ctx.sha1 );
2547 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2548 break;
2549#endif
2550#if defined(MBEDTLS_SHA256_C)
2551 case PSA_ALG_SHA_224:
2552 mbedtls_sha256_init( &operation->ctx.sha256 );
2553 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2554 break;
2555 case PSA_ALG_SHA_256:
2556 mbedtls_sha256_init( &operation->ctx.sha256 );
2557 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2558 break;
2559#endif
2560#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002561#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002562 case PSA_ALG_SHA_384:
2563 mbedtls_sha512_init( &operation->ctx.sha512 );
2564 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2565 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002566#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002567 case PSA_ALG_SHA_512:
2568 mbedtls_sha512_init( &operation->ctx.sha512 );
2569 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2570 break;
2571#endif
2572 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002573 return( PSA_ALG_IS_HASH( alg ) ?
2574 PSA_ERROR_NOT_SUPPORTED :
2575 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002576 }
2577 if( ret == 0 )
2578 operation->alg = alg;
2579 else
2580 psa_hash_abort( operation );
2581 return( mbedtls_to_psa_error( ret ) );
2582}
2583
2584psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2585 const uint8_t *input,
2586 size_t input_length )
2587{
Janos Follath24eed8d2019-11-22 13:21:35 +00002588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002589
2590 /* Don't require hash implementations to behave correctly on a
2591 * zero-length input, which may have an invalid pointer. */
2592 if( input_length == 0 )
2593 return( PSA_SUCCESS );
2594
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002595 switch( operation->alg )
2596 {
2597#if defined(MBEDTLS_MD2_C)
2598 case PSA_ALG_MD2:
2599 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2600 input, input_length );
2601 break;
2602#endif
2603#if defined(MBEDTLS_MD4_C)
2604 case PSA_ALG_MD4:
2605 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2606 input, input_length );
2607 break;
2608#endif
2609#if defined(MBEDTLS_MD5_C)
2610 case PSA_ALG_MD5:
2611 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2612 input, input_length );
2613 break;
2614#endif
2615#if defined(MBEDTLS_RIPEMD160_C)
2616 case PSA_ALG_RIPEMD160:
2617 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2618 input, input_length );
2619 break;
2620#endif
2621#if defined(MBEDTLS_SHA1_C)
2622 case PSA_ALG_SHA_1:
2623 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2624 input, input_length );
2625 break;
2626#endif
2627#if defined(MBEDTLS_SHA256_C)
2628 case PSA_ALG_SHA_224:
2629 case PSA_ALG_SHA_256:
2630 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2631 input, input_length );
2632 break;
2633#endif
2634#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002635#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002636 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002637#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002638 case PSA_ALG_SHA_512:
2639 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2640 input, input_length );
2641 break;
2642#endif
2643 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002644 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002645 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002646
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002647 if( ret != 0 )
2648 psa_hash_abort( operation );
2649 return( mbedtls_to_psa_error( ret ) );
2650}
2651
2652psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2653 uint8_t *hash,
2654 size_t hash_size,
2655 size_t *hash_length )
2656{
itayzafrir40835d42018-08-02 13:14:17 +03002657 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002658 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002659 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002660
2661 /* Fill the output buffer with something that isn't a valid hash
2662 * (barring an attack on the hash and deliberately-crafted input),
2663 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002664 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002665 /* If hash_size is 0 then hash may be NULL and then the
2666 * call to memset would have undefined behavior. */
2667 if( hash_size != 0 )
2668 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002669
2670 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002671 {
2672 status = PSA_ERROR_BUFFER_TOO_SMALL;
2673 goto exit;
2674 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002675
2676 switch( operation->alg )
2677 {
2678#if defined(MBEDTLS_MD2_C)
2679 case PSA_ALG_MD2:
2680 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2681 break;
2682#endif
2683#if defined(MBEDTLS_MD4_C)
2684 case PSA_ALG_MD4:
2685 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2686 break;
2687#endif
2688#if defined(MBEDTLS_MD5_C)
2689 case PSA_ALG_MD5:
2690 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2691 break;
2692#endif
2693#if defined(MBEDTLS_RIPEMD160_C)
2694 case PSA_ALG_RIPEMD160:
2695 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2696 break;
2697#endif
2698#if defined(MBEDTLS_SHA1_C)
2699 case PSA_ALG_SHA_1:
2700 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2701 break;
2702#endif
2703#if defined(MBEDTLS_SHA256_C)
2704 case PSA_ALG_SHA_224:
2705 case PSA_ALG_SHA_256:
2706 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2707 break;
2708#endif
2709#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002710#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002711 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002712#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002713 case PSA_ALG_SHA_512:
2714 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2715 break;
2716#endif
2717 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002718 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002719 }
itayzafrir40835d42018-08-02 13:14:17 +03002720 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002721
itayzafrir40835d42018-08-02 13:14:17 +03002722exit:
2723 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002724 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002725 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002726 return( psa_hash_abort( operation ) );
2727 }
2728 else
2729 {
2730 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002731 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002732 }
2733}
2734
Gilles Peskine2d277862018-06-18 15:41:12 +02002735psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2736 const uint8_t *hash,
2737 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002738{
2739 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2740 size_t actual_hash_length;
2741 psa_status_t status = psa_hash_finish( operation,
2742 actual_hash, sizeof( actual_hash ),
2743 &actual_hash_length );
2744 if( status != PSA_SUCCESS )
2745 return( status );
2746 if( actual_hash_length != hash_length )
2747 return( PSA_ERROR_INVALID_SIGNATURE );
2748 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2749 return( PSA_ERROR_INVALID_SIGNATURE );
2750 return( PSA_SUCCESS );
2751}
2752
Gilles Peskine0a749c82019-11-28 19:33:58 +01002753psa_status_t psa_hash_compute( psa_algorithm_t alg,
2754 const uint8_t *input, size_t input_length,
2755 uint8_t *hash, size_t hash_size,
2756 size_t *hash_length )
2757{
2758 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2759 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2760
2761 *hash_length = hash_size;
2762 status = psa_hash_setup( &operation, alg );
2763 if( status != PSA_SUCCESS )
2764 goto exit;
2765 status = psa_hash_update( &operation, input, input_length );
2766 if( status != PSA_SUCCESS )
2767 goto exit;
2768 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2769 if( status != PSA_SUCCESS )
2770 goto exit;
2771
2772exit:
2773 if( status == PSA_SUCCESS )
2774 status = psa_hash_abort( &operation );
2775 else
2776 psa_hash_abort( &operation );
2777 return( status );
2778}
2779
2780psa_status_t psa_hash_compare( psa_algorithm_t alg,
2781 const uint8_t *input, size_t input_length,
2782 const uint8_t *hash, size_t hash_length )
2783{
2784 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2785 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2786
2787 status = psa_hash_setup( &operation, alg );
2788 if( status != PSA_SUCCESS )
2789 goto exit;
2790 status = psa_hash_update( &operation, input, input_length );
2791 if( status != PSA_SUCCESS )
2792 goto exit;
2793 status = psa_hash_verify( &operation, hash, hash_length );
2794 if( status != PSA_SUCCESS )
2795 goto exit;
2796
2797exit:
2798 if( status == PSA_SUCCESS )
2799 status = psa_hash_abort( &operation );
2800 else
2801 psa_hash_abort( &operation );
2802 return( status );
2803}
2804
Gilles Peskineeb35d782019-01-22 17:56:16 +01002805psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2806 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002807{
2808 if( target_operation->alg != 0 )
2809 return( PSA_ERROR_BAD_STATE );
2810
2811 switch( source_operation->alg )
2812 {
2813 case 0:
2814 return( PSA_ERROR_BAD_STATE );
2815#if defined(MBEDTLS_MD2_C)
2816 case PSA_ALG_MD2:
2817 mbedtls_md2_clone( &target_operation->ctx.md2,
2818 &source_operation->ctx.md2 );
2819 break;
2820#endif
2821#if defined(MBEDTLS_MD4_C)
2822 case PSA_ALG_MD4:
2823 mbedtls_md4_clone( &target_operation->ctx.md4,
2824 &source_operation->ctx.md4 );
2825 break;
2826#endif
2827#if defined(MBEDTLS_MD5_C)
2828 case PSA_ALG_MD5:
2829 mbedtls_md5_clone( &target_operation->ctx.md5,
2830 &source_operation->ctx.md5 );
2831 break;
2832#endif
2833#if defined(MBEDTLS_RIPEMD160_C)
2834 case PSA_ALG_RIPEMD160:
2835 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2836 &source_operation->ctx.ripemd160 );
2837 break;
2838#endif
2839#if defined(MBEDTLS_SHA1_C)
2840 case PSA_ALG_SHA_1:
2841 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2842 &source_operation->ctx.sha1 );
2843 break;
2844#endif
2845#if defined(MBEDTLS_SHA256_C)
2846 case PSA_ALG_SHA_224:
2847 case PSA_ALG_SHA_256:
2848 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2849 &source_operation->ctx.sha256 );
2850 break;
2851#endif
2852#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002853#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002854 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002855#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002856 case PSA_ALG_SHA_512:
2857 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2858 &source_operation->ctx.sha512 );
2859 break;
2860#endif
2861 default:
2862 return( PSA_ERROR_NOT_SUPPORTED );
2863 }
2864
2865 target_operation->alg = source_operation->alg;
2866 return( PSA_SUCCESS );
2867}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002868
2869
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002870/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002871/* MAC */
2872/****************************************************************/
2873
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002874static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002875 psa_algorithm_t alg,
2876 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002877 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002878 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002879{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002880 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002881 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002882
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002883 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002884 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002885
Gilles Peskine8c9def32018-02-08 10:02:12 +01002886 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2887 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002888 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002889 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002890 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002891 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002892 mode = MBEDTLS_MODE_STREAM;
2893 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002894 case PSA_ALG_CTR:
2895 mode = MBEDTLS_MODE_CTR;
2896 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002897 case PSA_ALG_CFB:
2898 mode = MBEDTLS_MODE_CFB;
2899 break;
2900 case PSA_ALG_OFB:
2901 mode = MBEDTLS_MODE_OFB;
2902 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002903 case PSA_ALG_ECB_NO_PADDING:
2904 mode = MBEDTLS_MODE_ECB;
2905 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002906 case PSA_ALG_CBC_NO_PADDING:
2907 mode = MBEDTLS_MODE_CBC;
2908 break;
2909 case PSA_ALG_CBC_PKCS7:
2910 mode = MBEDTLS_MODE_CBC;
2911 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002912 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002913 mode = MBEDTLS_MODE_CCM;
2914 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002915 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002916 mode = MBEDTLS_MODE_GCM;
2917 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002918 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2919 mode = MBEDTLS_MODE_CHACHAPOLY;
2920 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002921 default:
2922 return( NULL );
2923 }
2924 }
2925 else if( alg == PSA_ALG_CMAC )
2926 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002927 else
2928 return( NULL );
2929
2930 switch( key_type )
2931 {
2932 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002933 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002934 break;
2935 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002936 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2937 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002938 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002939 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002940 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002941 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002942 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2943 * but two-key Triple-DES is functionally three-key Triple-DES
2944 * with K1=K3, so that's how we present it to mbedtls. */
2945 if( key_bits == 128 )
2946 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002947 break;
2948 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002949 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002950 break;
2951 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002952 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002953 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002954 case PSA_KEY_TYPE_CHACHA20:
2955 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2956 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002957 default:
2958 return( NULL );
2959 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002960 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002961 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002962
Jaeden Amero23bbb752018-06-26 14:16:54 +01002963 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2964 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002965}
2966
Gilles Peskinea05219c2018-11-16 16:02:56 +01002967#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002968static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002969{
Gilles Peskine2d277862018-06-18 15:41:12 +02002970 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002971 {
2972 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002973 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002974 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002975 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002976 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002977 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002978 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002979 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002980 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002981 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002982 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002983 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002984 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002985 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002986 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002987 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002988 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002989 return( 128 );
2990 default:
2991 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002992 }
2993}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002994#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002995
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002996/* Initialize the MAC operation structure. Once this function has been
2997 * called, psa_mac_abort can run and will do the right thing. */
2998static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2999 psa_algorithm_t alg )
3000{
3001 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3002
3003 operation->alg = alg;
3004 operation->key_set = 0;
3005 operation->iv_set = 0;
3006 operation->iv_required = 0;
3007 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003008 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003009
3010#if defined(MBEDTLS_CMAC_C)
3011 if( alg == PSA_ALG_CMAC )
3012 {
3013 operation->iv_required = 0;
3014 mbedtls_cipher_init( &operation->ctx.cmac );
3015 status = PSA_SUCCESS;
3016 }
3017 else
3018#endif /* MBEDTLS_CMAC_C */
3019#if defined(MBEDTLS_MD_C)
3020 if( PSA_ALG_IS_HMAC( operation->alg ) )
3021 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003022 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3023 operation->ctx.hmac.hash_ctx.alg = 0;
3024 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003025 }
3026 else
3027#endif /* MBEDTLS_MD_C */
3028 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003029 if( ! PSA_ALG_IS_MAC( alg ) )
3030 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003031 }
3032
3033 if( status != PSA_SUCCESS )
3034 memset( operation, 0, sizeof( *operation ) );
3035 return( status );
3036}
3037
Gilles Peskine01126fa2018-07-12 17:04:55 +02003038#if defined(MBEDTLS_MD_C)
3039static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3040{
Gilles Peskine3f108122018-12-07 18:14:53 +01003041 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003042 return( psa_hash_abort( &hmac->hash_ctx ) );
3043}
3044#endif /* MBEDTLS_MD_C */
3045
Gilles Peskine8c9def32018-02-08 10:02:12 +01003046psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3047{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003048 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003049 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003050 /* The object has (apparently) been initialized but it is not
3051 * in use. It's ok to call abort on such an object, and there's
3052 * nothing to do. */
3053 return( PSA_SUCCESS );
3054 }
3055 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003056#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003057 if( operation->alg == PSA_ALG_CMAC )
3058 {
3059 mbedtls_cipher_free( &operation->ctx.cmac );
3060 }
3061 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003062#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003063#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003064 if( PSA_ALG_IS_HMAC( operation->alg ) )
3065 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003066 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003067 }
3068 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003069#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003070 {
3071 /* Sanity check (shouldn't happen: operation->alg should
3072 * always have been initialized to a valid value). */
3073 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003074 }
Moran Peker41deec42018-04-04 15:43:05 +03003075
Gilles Peskine8c9def32018-02-08 10:02:12 +01003076 operation->alg = 0;
3077 operation->key_set = 0;
3078 operation->iv_set = 0;
3079 operation->iv_required = 0;
3080 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003081 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003082
Gilles Peskine8c9def32018-02-08 10:02:12 +01003083 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003084
3085bad_state:
3086 /* If abort is called on an uninitialized object, we can't trust
3087 * anything. Wipe the object in case it contains confidential data.
3088 * This may result in a memory leak if a pointer gets overwritten,
3089 * but it's too late to do anything about this. */
3090 memset( operation, 0, sizeof( *operation ) );
3091 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003092}
3093
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003094#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003095static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003096 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003097 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003098 const mbedtls_cipher_info_t *cipher_info )
3099{
Janos Follath24eed8d2019-11-22 13:21:35 +00003100 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003101
3102 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003103
3104 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3105 if( ret != 0 )
3106 return( ret );
3107
3108 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003109 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003110 key_bits );
3111 return( ret );
3112}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003113#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003114
Gilles Peskine248051a2018-06-20 16:09:38 +02003115#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003116static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3117 const uint8_t *key,
3118 size_t key_length,
3119 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003120{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003121 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003122 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003123 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003124 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003125 psa_status_t status;
3126
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003127 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3128 * overflow below. This should never trigger if the hash algorithm
3129 * is implemented correctly. */
3130 /* The size checks against the ipad and opad buffers cannot be written
3131 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3132 * because that triggers -Wlogical-op on GCC 7.3. */
3133 if( block_size > sizeof( ipad ) )
3134 return( PSA_ERROR_NOT_SUPPORTED );
3135 if( block_size > sizeof( hmac->opad ) )
3136 return( PSA_ERROR_NOT_SUPPORTED );
3137 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003138 return( PSA_ERROR_NOT_SUPPORTED );
3139
Gilles Peskined223b522018-06-11 18:12:58 +02003140 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003141 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003142 status = psa_hash_compute( hash_alg, key, key_length,
3143 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003144 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003145 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003146 }
Gilles Peskine96889972018-07-12 17:07:03 +02003147 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3148 * but it is permitted. It is common when HMAC is used in HKDF, for
3149 * example. Don't call `memcpy` in the 0-length because `key` could be
3150 * an invalid pointer which would make the behavior undefined. */
3151 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003152 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003153
Gilles Peskined223b522018-06-11 18:12:58 +02003154 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3155 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003156 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003157 ipad[i] ^= 0x36;
3158 memset( ipad + key_length, 0x36, block_size - key_length );
3159
3160 /* Copy the key material from ipad to opad, flipping the requisite bits,
3161 * and filling the rest of opad with the requisite constant. */
3162 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003163 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3164 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003165
Gilles Peskine01126fa2018-07-12 17:04:55 +02003166 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003167 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003168 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003169
Gilles Peskine01126fa2018-07-12 17:04:55 +02003170 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003171
3172cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003173 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003174
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003175 return( status );
3176}
Gilles Peskine248051a2018-06-20 16:09:38 +02003177#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003178
Gilles Peskine89167cb2018-07-08 20:12:23 +02003179static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003180 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003181 psa_algorithm_t alg,
3182 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003183{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003184 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3185 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003186 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003187 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003188 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003189 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003190 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003191 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003192
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003193 /* A context must be freshly initialized before it can be set up. */
3194 if( operation->alg != 0 )
3195 {
3196 return( PSA_ERROR_BAD_STATE );
3197 }
3198
Gilles Peskined911eb72018-08-14 15:18:45 +02003199 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003200 if( status != PSA_SUCCESS )
3201 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003202 if( is_sign )
3203 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003204
Ronald Croncf56a0a2020-08-04 09:51:30 +02003205 status = psa_get_transparent_key( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003206 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003207 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003208 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003209
Gilles Peskine8c9def32018-02-08 10:02:12 +01003210#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003211 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003212 {
3213 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003214 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003215 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003217 if( cipher_info == NULL )
3218 {
3219 status = PSA_ERROR_NOT_SUPPORTED;
3220 goto exit;
3221 }
3222 operation->mac_size = cipher_info->block_size;
3223 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3224 status = mbedtls_to_psa_error( ret );
3225 }
3226 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003227#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003228#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003229 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003230 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003231 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003232 if( hash_alg == 0 )
3233 {
3234 status = PSA_ERROR_NOT_SUPPORTED;
3235 goto exit;
3236 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003237
3238 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3239 /* Sanity check. This shouldn't fail on a valid configuration. */
3240 if( operation->mac_size == 0 ||
3241 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3242 {
3243 status = PSA_ERROR_NOT_SUPPORTED;
3244 goto exit;
3245 }
3246
Gilles Peskine8e338702019-07-30 20:06:31 +02003247 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003248 {
3249 status = PSA_ERROR_INVALID_ARGUMENT;
3250 goto exit;
3251 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003252
Gilles Peskine01126fa2018-07-12 17:04:55 +02003253 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003254 slot->data.key.data,
3255 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003256 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003257 }
3258 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003259#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003260 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003261 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003262 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003263 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003264
Gilles Peskined911eb72018-08-14 15:18:45 +02003265 if( truncated == 0 )
3266 {
3267 /* The "normal" case: untruncated algorithm. Nothing to do. */
3268 }
3269 else if( truncated < 4 )
3270 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003271 /* A very short MAC is too short for security since it can be
3272 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3273 * so we make this our minimum, even though 32 bits is still
3274 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003275 status = PSA_ERROR_NOT_SUPPORTED;
3276 }
3277 else if( truncated > operation->mac_size )
3278 {
3279 /* It's impossible to "truncate" to a larger length. */
3280 status = PSA_ERROR_INVALID_ARGUMENT;
3281 }
3282 else
3283 operation->mac_size = truncated;
3284
Gilles Peskinefbfac682018-07-08 20:51:54 +02003285exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003286 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003287 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003288 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003289 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003290 else
3291 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003292 operation->key_set = 1;
3293 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003294
3295 decrement_status = psa_decrement_key_slot_access_count( slot );
3296
3297 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003298}
3299
Gilles Peskine89167cb2018-07-08 20:12:23 +02003300psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003301 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003302 psa_algorithm_t alg )
3303{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003304 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003305}
3306
3307psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003308 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003309 psa_algorithm_t alg )
3310{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003311 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003312}
3313
Gilles Peskine8c9def32018-02-08 10:02:12 +01003314psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3315 const uint8_t *input,
3316 size_t input_length )
3317{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003318 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003319 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003320 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003321 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003322 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003323 operation->has_input = 1;
3324
Gilles Peskine8c9def32018-02-08 10:02:12 +01003325#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003326 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003327 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003328 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3329 input, input_length );
3330 status = mbedtls_to_psa_error( ret );
3331 }
3332 else
3333#endif /* MBEDTLS_CMAC_C */
3334#if defined(MBEDTLS_MD_C)
3335 if( PSA_ALG_IS_HMAC( operation->alg ) )
3336 {
3337 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3338 input_length );
3339 }
3340 else
3341#endif /* MBEDTLS_MD_C */
3342 {
3343 /* This shouldn't happen if `operation` was initialized by
3344 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003345 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003346 }
3347
Gilles Peskinefbfac682018-07-08 20:51:54 +02003348 if( status != PSA_SUCCESS )
3349 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003350 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003351}
3352
Gilles Peskine01126fa2018-07-12 17:04:55 +02003353#if defined(MBEDTLS_MD_C)
3354static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3355 uint8_t *mac,
3356 size_t mac_size )
3357{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003358 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003359 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3360 size_t hash_size = 0;
3361 size_t block_size = psa_get_hash_block_size( hash_alg );
3362 psa_status_t status;
3363
Gilles Peskine01126fa2018-07-12 17:04:55 +02003364 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3365 if( status != PSA_SUCCESS )
3366 return( status );
3367 /* From here on, tmp needs to be wiped. */
3368
3369 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3370 if( status != PSA_SUCCESS )
3371 goto exit;
3372
3373 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3374 if( status != PSA_SUCCESS )
3375 goto exit;
3376
3377 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3378 if( status != PSA_SUCCESS )
3379 goto exit;
3380
Gilles Peskined911eb72018-08-14 15:18:45 +02003381 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3382 if( status != PSA_SUCCESS )
3383 goto exit;
3384
3385 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003386
3387exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003388 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003389 return( status );
3390}
3391#endif /* MBEDTLS_MD_C */
3392
mohammad16036df908f2018-04-02 08:34:15 -07003393static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003394 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003395 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003396{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003397 if( ! operation->key_set )
3398 return( PSA_ERROR_BAD_STATE );
3399 if( operation->iv_required && ! operation->iv_set )
3400 return( PSA_ERROR_BAD_STATE );
3401
Gilles Peskine8c9def32018-02-08 10:02:12 +01003402 if( mac_size < operation->mac_size )
3403 return( PSA_ERROR_BUFFER_TOO_SMALL );
3404
Gilles Peskine8c9def32018-02-08 10:02:12 +01003405#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003406 if( operation->alg == PSA_ALG_CMAC )
3407 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003408 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3409 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3410 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003411 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003412 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003413 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003414 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003415 else
3416#endif /* MBEDTLS_CMAC_C */
3417#if defined(MBEDTLS_MD_C)
3418 if( PSA_ALG_IS_HMAC( operation->alg ) )
3419 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003420 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003421 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003422 }
3423 else
3424#endif /* MBEDTLS_MD_C */
3425 {
3426 /* This shouldn't happen if `operation` was initialized by
3427 * a setup function. */
3428 return( PSA_ERROR_BAD_STATE );
3429 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003430}
3431
Gilles Peskineacd4be32018-07-08 19:56:25 +02003432psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3433 uint8_t *mac,
3434 size_t mac_size,
3435 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003436{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003437 psa_status_t status;
3438
Jaeden Amero252ef282019-02-15 14:05:35 +00003439 if( operation->alg == 0 )
3440 {
3441 return( PSA_ERROR_BAD_STATE );
3442 }
3443
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003444 /* Fill the output buffer with something that isn't a valid mac
3445 * (barring an attack on the mac and deliberately-crafted input),
3446 * in case the caller doesn't check the return status properly. */
3447 *mac_length = mac_size;
3448 /* If mac_size is 0 then mac may be NULL and then the
3449 * call to memset would have undefined behavior. */
3450 if( mac_size != 0 )
3451 memset( mac, '!', mac_size );
3452
Gilles Peskine89167cb2018-07-08 20:12:23 +02003453 if( ! operation->is_sign )
3454 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003455 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003456 }
mohammad16036df908f2018-04-02 08:34:15 -07003457
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003458 status = psa_mac_finish_internal( operation, mac, mac_size );
3459
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003460 if( status == PSA_SUCCESS )
3461 {
3462 status = psa_mac_abort( operation );
3463 if( status == PSA_SUCCESS )
3464 *mac_length = operation->mac_size;
3465 else
3466 memset( mac, '!', mac_size );
3467 }
3468 else
3469 psa_mac_abort( operation );
3470 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003471}
3472
Gilles Peskineacd4be32018-07-08 19:56:25 +02003473psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3474 const uint8_t *mac,
3475 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003476{
Gilles Peskine828ed142018-06-18 23:25:51 +02003477 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003478 psa_status_t status;
3479
Jaeden Amero252ef282019-02-15 14:05:35 +00003480 if( operation->alg == 0 )
3481 {
3482 return( PSA_ERROR_BAD_STATE );
3483 }
3484
Gilles Peskine89167cb2018-07-08 20:12:23 +02003485 if( operation->is_sign )
3486 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003487 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003488 }
3489 if( operation->mac_size != mac_length )
3490 {
3491 status = PSA_ERROR_INVALID_SIGNATURE;
3492 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003493 }
mohammad16036df908f2018-04-02 08:34:15 -07003494
3495 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003496 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003497 if( status != PSA_SUCCESS )
3498 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003499
3500 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3501 status = PSA_ERROR_INVALID_SIGNATURE;
3502
3503cleanup:
3504 if( status == PSA_SUCCESS )
3505 status = psa_mac_abort( operation );
3506 else
3507 psa_mac_abort( operation );
3508
Gilles Peskine3f108122018-12-07 18:14:53 +01003509 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003510
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003511 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003512}
3513
3514
Gilles Peskine20035e32018-02-03 22:44:14 +01003515
Gilles Peskine20035e32018-02-03 22:44:14 +01003516/****************************************************************/
3517/* Asymmetric cryptography */
3518/****************************************************************/
3519
Gilles Peskine2b450e32018-06-27 15:42:46 +02003520#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003521/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003522 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003523static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3524 size_t hash_length,
3525 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003526{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003527 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003528 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003529 *md_alg = mbedtls_md_get_type( md_info );
3530
3531 /* The Mbed TLS RSA module uses an unsigned int for hash length
3532 * parameters. Validate that it fits so that we don't risk an
3533 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003534#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003535 if( hash_length > UINT_MAX )
3536 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003537#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003538
3539#if defined(MBEDTLS_PKCS1_V15)
3540 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3541 * must be correct. */
3542 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3543 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003544 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003545 if( md_info == NULL )
3546 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003547 if( mbedtls_md_get_size( md_info ) != hash_length )
3548 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003549 }
3550#endif /* MBEDTLS_PKCS1_V15 */
3551
3552#if defined(MBEDTLS_PKCS1_V21)
3553 /* PSS requires a hash internally. */
3554 if( PSA_ALG_IS_RSA_PSS( alg ) )
3555 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003556 if( md_info == NULL )
3557 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003558 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003559#endif /* MBEDTLS_PKCS1_V21 */
3560
Gilles Peskine61b91d42018-06-08 16:09:36 +02003561 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003562}
3563
Gilles Peskine2b450e32018-06-27 15:42:46 +02003564static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3565 psa_algorithm_t alg,
3566 const uint8_t *hash,
3567 size_t hash_length,
3568 uint8_t *signature,
3569 size_t signature_size,
3570 size_t *signature_length )
3571{
3572 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003574 mbedtls_md_type_t md_alg;
3575
3576 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3577 if( status != PSA_SUCCESS )
3578 return( status );
3579
Gilles Peskine630a18a2018-06-29 17:49:35 +02003580 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003581 return( PSA_ERROR_BUFFER_TOO_SMALL );
3582
3583#if defined(MBEDTLS_PKCS1_V15)
3584 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3585 {
3586 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3587 MBEDTLS_MD_NONE );
3588 ret = mbedtls_rsa_pkcs1_sign( rsa,
3589 mbedtls_ctr_drbg_random,
3590 &global_data.ctr_drbg,
3591 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003592 md_alg,
3593 (unsigned int) hash_length,
3594 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003595 signature );
3596 }
3597 else
3598#endif /* MBEDTLS_PKCS1_V15 */
3599#if defined(MBEDTLS_PKCS1_V21)
3600 if( PSA_ALG_IS_RSA_PSS( alg ) )
3601 {
3602 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3603 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3604 mbedtls_ctr_drbg_random,
3605 &global_data.ctr_drbg,
3606 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003607 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003608 (unsigned int) hash_length,
3609 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003610 signature );
3611 }
3612 else
3613#endif /* MBEDTLS_PKCS1_V21 */
3614 {
3615 return( PSA_ERROR_INVALID_ARGUMENT );
3616 }
3617
3618 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003619 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003620 return( mbedtls_to_psa_error( ret ) );
3621}
3622
3623static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3624 psa_algorithm_t alg,
3625 const uint8_t *hash,
3626 size_t hash_length,
3627 const uint8_t *signature,
3628 size_t signature_length )
3629{
3630 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003631 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003632 mbedtls_md_type_t md_alg;
3633
3634 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3635 if( status != PSA_SUCCESS )
3636 return( status );
3637
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003638 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3639 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003640
3641#if defined(MBEDTLS_PKCS1_V15)
3642 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3643 {
3644 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3645 MBEDTLS_MD_NONE );
3646 ret = mbedtls_rsa_pkcs1_verify( rsa,
3647 mbedtls_ctr_drbg_random,
3648 &global_data.ctr_drbg,
3649 MBEDTLS_RSA_PUBLIC,
3650 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003651 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003652 hash,
3653 signature );
3654 }
3655 else
3656#endif /* MBEDTLS_PKCS1_V15 */
3657#if defined(MBEDTLS_PKCS1_V21)
3658 if( PSA_ALG_IS_RSA_PSS( alg ) )
3659 {
3660 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3661 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3662 mbedtls_ctr_drbg_random,
3663 &global_data.ctr_drbg,
3664 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003665 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003666 (unsigned int) hash_length,
3667 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003668 signature );
3669 }
3670 else
3671#endif /* MBEDTLS_PKCS1_V21 */
3672 {
3673 return( PSA_ERROR_INVALID_ARGUMENT );
3674 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003675
3676 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3677 * the rest of the signature is invalid". This has little use in
3678 * practice and PSA doesn't report this distinction. */
3679 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3680 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003681 return( mbedtls_to_psa_error( ret ) );
3682}
3683#endif /* MBEDTLS_RSA_C */
3684
John Durkopf87e3ae2020-10-26 15:25:23 -07003685#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003686/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3687 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3688 * (even though these functions don't modify it). */
3689static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3690 psa_algorithm_t alg,
3691 const uint8_t *hash,
3692 size_t hash_length,
3693 uint8_t *signature,
3694 size_t signature_size,
3695 size_t *signature_length )
3696{
Janos Follath24eed8d2019-11-22 13:21:35 +00003697 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003698 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003699 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003700 mbedtls_mpi_init( &r );
3701 mbedtls_mpi_init( &s );
3702
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003703 if( signature_size < 2 * curve_bytes )
3704 {
3705 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3706 goto cleanup;
3707 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003708
John Durkop0ea39e02020-10-13 19:58:20 -07003709#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003710 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3711 {
3712 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3713 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3714 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003715 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3716 &ecp->d, hash,
3717 hash_length, md_alg,
3718 mbedtls_ctr_drbg_random,
3719 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003720 }
3721 else
John Durkop0ea39e02020-10-13 19:58:20 -07003722#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003723 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003724 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003725 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3726 hash, hash_length,
3727 mbedtls_ctr_drbg_random,
3728 &global_data.ctr_drbg ) );
3729 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003730
3731 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3732 signature,
3733 curve_bytes ) );
3734 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3735 signature + curve_bytes,
3736 curve_bytes ) );
3737
3738cleanup:
3739 mbedtls_mpi_free( &r );
3740 mbedtls_mpi_free( &s );
3741 if( ret == 0 )
3742 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003743 return( mbedtls_to_psa_error( ret ) );
3744}
3745
3746static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3747 const uint8_t *hash,
3748 size_t hash_length,
3749 const uint8_t *signature,
3750 size_t signature_length )
3751{
Janos Follath24eed8d2019-11-22 13:21:35 +00003752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003753 mbedtls_mpi r, s;
3754 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3755 mbedtls_mpi_init( &r );
3756 mbedtls_mpi_init( &s );
3757
3758 if( signature_length != 2 * curve_bytes )
3759 return( PSA_ERROR_INVALID_SIGNATURE );
3760
3761 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3762 signature,
3763 curve_bytes ) );
3764 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3765 signature + curve_bytes,
3766 curve_bytes ) );
3767
Steven Cooremanacda8342020-07-24 23:09:52 +02003768 /* Check whether the public part is loaded. If not, load it. */
3769 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3770 {
3771 MBEDTLS_MPI_CHK(
3772 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3773 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3774 }
3775
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003776 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3777 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003778
3779cleanup:
3780 mbedtls_mpi_free( &r );
3781 mbedtls_mpi_free( &s );
3782 return( mbedtls_to_psa_error( ret ) );
3783}
John Durkopf87e3ae2020-10-26 15:25:23 -07003784#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003785
Ronald Croncf56a0a2020-08-04 09:51:30 +02003786psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003787 psa_algorithm_t alg,
3788 const uint8_t *hash,
3789 size_t hash_length,
3790 uint8_t *signature,
3791 size_t signature_size,
3792 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003793{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003794 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3795 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003796 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003797
3798 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003799 /* Immediately reject a zero-length signature buffer. This guarantees
3800 * that signature must be a valid pointer. (On the other hand, the hash
3801 * buffer can in principle be empty since it doesn't actually have
3802 * to be a hash.) */
3803 if( signature_size == 0 )
3804 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003805
Ronald Croncf56a0a2020-08-04 09:51:30 +02003806 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003807 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003808 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003809 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003810 {
3811 status = PSA_ERROR_INVALID_ARGUMENT;
3812 goto exit;
3813 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003814
Steven Cooremancd84cb42020-07-16 20:28:36 +02003815 /* Try any of the available accelerators first */
3816 status = psa_driver_wrapper_sign_hash( slot,
3817 alg,
3818 hash,
3819 hash_length,
3820 signature,
3821 signature_size,
3822 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003823 if( status != PSA_ERROR_NOT_SUPPORTED ||
3824 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003825 goto exit;
3826
Steven Cooreman7a250572020-07-17 16:43:05 +02003827 /* If the operation was not supported by any accelerator, try fallback. */
Gilles Peskine20035e32018-02-03 22:44:14 +01003828#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003829 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003830 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003831 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003832
Steven Cooreman4fed4552020-08-03 14:46:03 +02003833 status = psa_load_rsa_representation( slot->attr.type,
3834 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003835 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003836 &rsa );
3837 if( status != PSA_SUCCESS )
3838 goto exit;
3839
Steven Cooremana2371e52020-07-28 14:30:39 +02003840 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003841 alg,
3842 hash, hash_length,
3843 signature, signature_size,
3844 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003845
Steven Cooremana2371e52020-07-28 14:30:39 +02003846 mbedtls_rsa_free( rsa );
3847 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003848 }
3849 else
3850#endif /* defined(MBEDTLS_RSA_C) */
3851#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003852 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003853 {
John Durkop0ea39e02020-10-13 19:58:20 -07003854#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003855 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003856#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003857 PSA_ALG_IS_ECDSA( alg )
3858#else
3859 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3860#endif
3861 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003862 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003863 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003864 status = psa_load_ecp_representation( slot->attr.type,
3865 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003866 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003867 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003868 if( status != PSA_SUCCESS )
3869 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003870 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003871 alg,
3872 hash, hash_length,
3873 signature, signature_size,
3874 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003875 mbedtls_ecp_keypair_free( ecp );
3876 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003877 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003878 else
John Durkop0ea39e02020-10-13 19:58:20 -07003879#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003880 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003881 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003882 }
itayzafrir5c753392018-05-08 11:18:38 +03003883 }
3884 else
3885#endif /* defined(MBEDTLS_ECP_C) */
3886 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003887 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003888 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003889
3890exit:
3891 /* Fill the unused part of the output buffer (the whole buffer on error,
3892 * the trailing part on success) with something that isn't a valid mac
3893 * (barring an attack on the mac and deliberately-crafted input),
3894 * in case the caller doesn't check the return status properly. */
3895 if( status == PSA_SUCCESS )
3896 memset( signature + *signature_length, '!',
3897 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003898 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003899 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003900 /* If signature_size is 0 then we have nothing to do. We must not call
3901 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003902
3903 decrement_status = psa_decrement_key_slot_access_count( slot );
3904
3905 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003906}
3907
Ronald Croncf56a0a2020-08-04 09:51:30 +02003908psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003909 psa_algorithm_t alg,
3910 const uint8_t *hash,
3911 size_t hash_length,
3912 const uint8_t *signature,
3913 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003914{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003915 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3916 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003917 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003918
Ronald Croncf56a0a2020-08-04 09:51:30 +02003919 status = psa_get_key_from_slot( key, &slot,
3920 PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003921 if( status != PSA_SUCCESS )
3922 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003923
Steven Cooreman55ae2172020-07-17 19:46:15 +02003924 /* Try any of the available accelerators first */
3925 status = psa_driver_wrapper_verify_hash( slot,
3926 alg,
3927 hash,
3928 hash_length,
3929 signature,
3930 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003931 if( status != PSA_ERROR_NOT_SUPPORTED ||
3932 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003933 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003934
Gilles Peskine61b91d42018-06-08 16:09:36 +02003935#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003936 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003937 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003938 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003939
Steven Cooreman4fed4552020-08-03 14:46:03 +02003940 status = psa_load_rsa_representation( slot->attr.type,
3941 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003942 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003943 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003944 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003945 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003946
Steven Cooremana2371e52020-07-28 14:30:39 +02003947 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003948 alg,
3949 hash, hash_length,
3950 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003951 mbedtls_rsa_free( rsa );
3952 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003953 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003954 }
3955 else
3956#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003957#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003958 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003959 {
John Durkopf87e3ae2020-10-26 15:25:23 -07003960#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003961 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003962 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003963 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003964 status = psa_load_ecp_representation( slot->attr.type,
3965 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003966 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003967 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003968 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003969 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003970 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003971 hash, hash_length,
3972 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003973 mbedtls_ecp_keypair_free( ecp );
3974 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003975 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003976 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003977 else
John Durkopf87e3ae2020-10-26 15:25:23 -07003978#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003979 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003980 status = PSA_ERROR_INVALID_ARGUMENT;
3981 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003982 }
itayzafrir5c753392018-05-08 11:18:38 +03003983 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003984 else
3985#endif /* defined(MBEDTLS_ECP_C) */
3986 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003987 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003988 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003989
3990exit:
3991 decrement_status = psa_decrement_key_slot_access_count( slot );
3992
3993 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003994}
3995
Gilles Peskine072ac562018-06-30 00:21:29 +02003996#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3997static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3998 mbedtls_rsa_context *rsa )
3999{
4000 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4001 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4002 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4003 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4004}
4005#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
4006
Ronald Croncf56a0a2020-08-04 09:51:30 +02004007psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004008 psa_algorithm_t alg,
4009 const uint8_t *input,
4010 size_t input_length,
4011 const uint8_t *salt,
4012 size_t salt_length,
4013 uint8_t *output,
4014 size_t output_size,
4015 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004016{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004017 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4018 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004019 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004020
Darryl Green5cc689a2018-07-24 15:34:10 +01004021 (void) input;
4022 (void) input_length;
4023 (void) salt;
4024 (void) output;
4025 (void) output_size;
4026
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004027 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004028
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004029 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4030 return( PSA_ERROR_INVALID_ARGUMENT );
4031
Ronald Croncf56a0a2020-08-04 09:51:30 +02004032 status = psa_get_transparent_key( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004033 if( status != PSA_SUCCESS )
4034 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004035 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4036 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004037 {
4038 status = PSA_ERROR_INVALID_ARGUMENT;
4039 goto exit;
4040 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004041
4042#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004043 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004044 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004045 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004046 status = psa_load_rsa_representation( slot->attr.type,
4047 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004048 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004049 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004050 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004051 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004052
4053 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004054 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004055 status = PSA_ERROR_BUFFER_TOO_SMALL;
4056 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004057 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004058#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004059 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004060 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004061 status = mbedtls_to_psa_error(
4062 mbedtls_rsa_pkcs1_encrypt( rsa,
4063 mbedtls_ctr_drbg_random,
4064 &global_data.ctr_drbg,
4065 MBEDTLS_RSA_PUBLIC,
4066 input_length,
4067 input,
4068 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004069 }
4070 else
4071#endif /* MBEDTLS_PKCS1_V15 */
4072#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004073 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004074 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004075 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004076 status = mbedtls_to_psa_error(
4077 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
4078 mbedtls_ctr_drbg_random,
4079 &global_data.ctr_drbg,
4080 MBEDTLS_RSA_PUBLIC,
4081 salt, salt_length,
4082 input_length,
4083 input,
4084 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004085 }
4086 else
4087#endif /* MBEDTLS_PKCS1_V21 */
4088 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004089 status = PSA_ERROR_INVALID_ARGUMENT;
4090 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004091 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004092rsa_exit:
4093 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004094 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004095
Steven Cooremana2371e52020-07-28 14:30:39 +02004096 mbedtls_rsa_free( rsa );
4097 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004098 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004099 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004100#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004101 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004102 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004103 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004104
4105exit:
4106 decrement_status = psa_decrement_key_slot_access_count( slot );
4107
4108 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004109}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004110
Ronald Croncf56a0a2020-08-04 09:51:30 +02004111psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004112 psa_algorithm_t alg,
4113 const uint8_t *input,
4114 size_t input_length,
4115 const uint8_t *salt,
4116 size_t salt_length,
4117 uint8_t *output,
4118 size_t output_size,
4119 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004120{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004121 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4122 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004123 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004124
Darryl Green5cc689a2018-07-24 15:34:10 +01004125 (void) input;
4126 (void) input_length;
4127 (void) salt;
4128 (void) output;
4129 (void) output_size;
4130
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004131 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004132
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004133 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4134 return( PSA_ERROR_INVALID_ARGUMENT );
4135
Ronald Croncf56a0a2020-08-04 09:51:30 +02004136 status = psa_get_transparent_key( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004137 if( status != PSA_SUCCESS )
4138 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004139 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004140 {
4141 status = PSA_ERROR_INVALID_ARGUMENT;
4142 goto exit;
4143 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004144
4145#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004146 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004147 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004148 mbedtls_rsa_context *rsa = NULL;
4149 status = psa_load_rsa_representation( slot->attr.type,
4150 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004151 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004152 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004153 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004154 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004155
Steven Cooremana2371e52020-07-28 14:30:39 +02004156 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004157 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004158 status = PSA_ERROR_INVALID_ARGUMENT;
4159 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004160 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004161
4162#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004163 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004164 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004165 status = mbedtls_to_psa_error(
4166 mbedtls_rsa_pkcs1_decrypt( rsa,
4167 mbedtls_ctr_drbg_random,
4168 &global_data.ctr_drbg,
4169 MBEDTLS_RSA_PRIVATE,
4170 output_length,
4171 input,
4172 output,
4173 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004174 }
4175 else
4176#endif /* MBEDTLS_PKCS1_V15 */
4177#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004178 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004179 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004180 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004181 status = mbedtls_to_psa_error(
4182 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4183 mbedtls_ctr_drbg_random,
4184 &global_data.ctr_drbg,
4185 MBEDTLS_RSA_PRIVATE,
4186 salt, salt_length,
4187 output_length,
4188 input,
4189 output,
4190 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004191 }
4192 else
4193#endif /* MBEDTLS_PKCS1_V21 */
4194 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004195 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004196 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004197
Steven Cooreman4fed4552020-08-03 14:46:03 +02004198rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004199 mbedtls_rsa_free( rsa );
4200 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004201 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004202 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004203#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004204 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004205 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004206 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004207
4208exit:
4209 decrement_status = psa_decrement_key_slot_access_count( slot );
4210
4211 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004212}
Gilles Peskine20035e32018-02-03 22:44:14 +01004213
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004214
4215
mohammad1603503973b2018-03-12 15:59:30 +02004216/****************************************************************/
4217/* Symmetric cryptography */
4218/****************************************************************/
4219
Gilles Peskinee553c652018-06-04 16:22:46 +02004220static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004221 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004222 psa_algorithm_t alg,
4223 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004224{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004225 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4226 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004227 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004228 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004229 size_t key_bits;
4230 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004231 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4232 PSA_KEY_USAGE_ENCRYPT :
4233 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004234
Steven Cooremand3feccd2020-09-01 15:56:14 +02004235 /* A context must be freshly initialized before it can be set up. */
4236 if( operation->alg != 0 )
4237 return( PSA_ERROR_BAD_STATE );
4238
Steven Cooremana07b9972020-09-10 14:54:14 +02004239 /* The requested algorithm must be one that can be processed by cipher. */
4240 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004241 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004242
Steven Cooremanef8575e2020-09-11 11:44:50 +02004243 /* Fetch key material from key storage. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02004244 status = psa_get_key_from_slot( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004245 if( status != PSA_SUCCESS )
4246 goto exit;
4247
4248 /* Initialize the operation struct members, except for alg. The alg member
4249 * is used to indicate to psa_cipher_abort that there are resources to free,
4250 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004251 operation->key_set = 0;
4252 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004253 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004254 operation->iv_size = 0;
4255 operation->block_size = 0;
4256 if( alg == PSA_ALG_ECB_NO_PADDING )
4257 operation->iv_required = 0;
4258 else
4259 operation->iv_required = 1;
4260
Steven Cooremana07b9972020-09-10 14:54:14 +02004261 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004262 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004263 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004264 slot,
4265 alg );
4266 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004267 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004268 slot,
4269 alg );
4270
Steven Cooremanef8575e2020-09-11 11:44:50 +02004271 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004272 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004273 /* Once the driver context is initialised, it needs to be freed using
4274 * psa_cipher_abort. Indicate this through setting alg. */
4275 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004276 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004277
Steven Cooremand3feccd2020-09-01 15:56:14 +02004278 if( status != PSA_ERROR_NOT_SUPPORTED ||
4279 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4280 goto exit;
4281
Steven Cooremana07b9972020-09-10 14:54:14 +02004282 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004283 * available for the given algorithm & key. */
4284 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004285
Steven Cooremana07b9972020-09-10 14:54:14 +02004286 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004287 * psa_cipher_abort. Indicate there is something to be freed through setting
4288 * alg, and indicate the operation is being done using mbedtls crypto through
4289 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004290 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004291 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004292
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004293 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004294 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004295 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004296 {
4297 status = PSA_ERROR_NOT_SUPPORTED;
4298 goto exit;
4299 }
mohammad1603503973b2018-03-12 15:59:30 +02004300
mohammad1603503973b2018-03-12 15:59:30 +02004301 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004302 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004303 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004304
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004305#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004306 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004307 {
4308 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004309 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004310 memcpy( keys, slot->data.key.data, 16 );
4311 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004312 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4313 keys,
4314 192, cipher_operation );
4315 }
4316 else
4317#endif
4318 {
4319 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004320 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004321 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004322 }
Moran Peker41deec42018-04-04 15:43:05 +03004323 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004324 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004325
mohammad16038481e742018-03-18 13:57:31 +02004326#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004327 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004328 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004329 case PSA_ALG_CBC_NO_PADDING:
4330 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4331 MBEDTLS_PADDING_NONE );
4332 break;
4333 case PSA_ALG_CBC_PKCS7:
4334 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4335 MBEDTLS_PADDING_PKCS7 );
4336 break;
4337 default:
4338 /* The algorithm doesn't involve padding. */
4339 ret = 0;
4340 break;
4341 }
4342 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004343 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004344#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4345
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004346 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004347 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004348 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4349 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004350 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004351 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004352 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004353#if defined(MBEDTLS_CHACHA20_C)
4354 else
4355 if( alg == PSA_ALG_CHACHA20 )
4356 operation->iv_size = 12;
4357#endif
mohammad1603503973b2018-03-12 15:59:30 +02004358
Steven Cooreman7df02922020-09-09 15:28:49 +02004359 status = PSA_SUCCESS;
4360
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004361exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004362 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004363 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004364 if( status == PSA_SUCCESS )
4365 {
4366 /* Update operation flags for both driver and software implementations */
4367 operation->key_set = 1;
4368 }
4369 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004370 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004371
4372 decrement_status = psa_decrement_key_slot_access_count( slot );
4373
4374 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004375}
4376
Gilles Peskinefe119512018-07-08 21:39:34 +02004377psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004378 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004379 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004380{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004381 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004382}
4383
Gilles Peskinefe119512018-07-08 21:39:34 +02004384psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004385 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004386 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004387{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004388 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004389}
4390
Gilles Peskinefe119512018-07-08 21:39:34 +02004391psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004392 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004393 size_t iv_size,
4394 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004395{
itayzafrir534bd7c2018-08-02 13:56:32 +03004396 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004398 if( operation->iv_set || ! operation->iv_required )
4399 {
4400 return( PSA_ERROR_BAD_STATE );
4401 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004402
Steven Cooremanef8575e2020-09-11 11:44:50 +02004403 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004404 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004405 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004406 iv,
4407 iv_size,
4408 iv_length );
4409 goto exit;
4410 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004411
Moran Peker41deec42018-04-04 15:43:05 +03004412 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004413 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004414 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004415 goto exit;
4416 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004417 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4418 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004419 if( ret != 0 )
4420 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004421 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004422 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004423 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004424
mohammad16038481e742018-03-18 13:57:31 +02004425 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004426 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004427
Moran Peker395db872018-05-31 14:07:14 +03004428exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004429 if( status == PSA_SUCCESS )
4430 operation->iv_set = 1;
4431 else
Moran Peker395db872018-05-31 14:07:14 +03004432 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004433 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004434}
4435
Gilles Peskinefe119512018-07-08 21:39:34 +02004436psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004437 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004438 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004439{
itayzafrir534bd7c2018-08-02 13:56:32 +03004440 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004442 if( operation->iv_set || ! operation->iv_required )
4443 {
4444 return( PSA_ERROR_BAD_STATE );
4445 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004446
Steven Cooremanef8575e2020-09-11 11:44:50 +02004447 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004448 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004449 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004450 iv,
4451 iv_length );
4452 goto exit;
4453 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004454
Moran Pekera28258c2018-05-29 16:25:04 +03004455 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004456 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004457 status = PSA_ERROR_INVALID_ARGUMENT;
4458 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004459 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004460 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4461 status = mbedtls_to_psa_error( ret );
4462exit:
4463 if( status == PSA_SUCCESS )
4464 operation->iv_set = 1;
4465 else
mohammad1603503973b2018-03-12 15:59:30 +02004466 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004467 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004468}
4469
Steven Cooreman177deba2020-09-07 17:14:14 +02004470/* Process input for which the algorithm is set to ECB mode. This requires
4471 * manual processing, since the PSA API is defined as being able to process
4472 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4473 * underlying mbedtls_cipher_update only takes full blocks. */
4474static psa_status_t psa_cipher_update_ecb_internal(
4475 mbedtls_cipher_context_t *ctx,
4476 const uint8_t *input,
4477 size_t input_length,
4478 uint8_t *output,
4479 size_t output_size,
4480 size_t *output_length )
4481{
4482 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4483 size_t block_size = ctx->cipher_info->block_size;
4484 size_t internal_output_length = 0;
4485 *output_length = 0;
4486
4487 if( input_length == 0 )
4488 {
4489 status = PSA_SUCCESS;
4490 goto exit;
4491 }
4492
4493 if( ctx->unprocessed_len > 0 )
4494 {
4495 /* Fill up to block size, and run the block if there's a full one. */
4496 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4497
4498 if( input_length < bytes_to_copy )
4499 bytes_to_copy = input_length;
4500
4501 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4502 input, bytes_to_copy );
4503 input_length -= bytes_to_copy;
4504 input += bytes_to_copy;
4505 ctx->unprocessed_len += bytes_to_copy;
4506
4507 if( ctx->unprocessed_len == block_size )
4508 {
4509 status = mbedtls_to_psa_error(
4510 mbedtls_cipher_update( ctx,
4511 ctx->unprocessed_data,
4512 block_size,
4513 output, &internal_output_length ) );
4514
4515 if( status != PSA_SUCCESS )
4516 goto exit;
4517
4518 output += internal_output_length;
4519 output_size -= internal_output_length;
4520 *output_length += internal_output_length;
4521 ctx->unprocessed_len = 0;
4522 }
4523 }
4524
4525 while( input_length >= block_size )
4526 {
4527 /* Run all full blocks we have, one by one */
4528 status = mbedtls_to_psa_error(
4529 mbedtls_cipher_update( ctx, input,
4530 block_size,
4531 output, &internal_output_length ) );
4532
4533 if( status != PSA_SUCCESS )
4534 goto exit;
4535
4536 input_length -= block_size;
4537 input += block_size;
4538
4539 output += internal_output_length;
4540 output_size -= internal_output_length;
4541 *output_length += internal_output_length;
4542 }
4543
4544 if( input_length > 0 )
4545 {
4546 /* Save unprocessed bytes for later processing */
4547 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4548 input, input_length );
4549 ctx->unprocessed_len += input_length;
4550 }
4551
4552 status = PSA_SUCCESS;
4553
4554exit:
4555 return( status );
4556}
4557
Gilles Peskinee553c652018-06-04 16:22:46 +02004558psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4559 const uint8_t *input,
4560 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004561 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004562 size_t output_size,
4563 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004564{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004565 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004566 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004567 if( operation->alg == 0 )
4568 {
4569 return( PSA_ERROR_BAD_STATE );
4570 }
4571 if( operation->iv_required && ! operation->iv_set )
4572 {
4573 return( PSA_ERROR_BAD_STATE );
4574 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004575
Steven Cooremanef8575e2020-09-11 11:44:50 +02004576 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004577 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004578 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004579 input,
4580 input_length,
4581 output,
4582 output_size,
4583 output_length );
4584 goto exit;
4585 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004586
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004587 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004588 {
4589 /* Take the unprocessed partial block left over from previous
4590 * update calls, if any, plus the input to this call. Remove
4591 * the last partial block, if any. You get the data that will be
4592 * output in this call. */
4593 expected_output_size =
4594 ( operation->ctx.cipher.unprocessed_len + input_length )
4595 / operation->block_size * operation->block_size;
4596 }
4597 else
4598 {
4599 expected_output_size = input_length;
4600 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004601
Gilles Peskine89d789c2018-06-04 17:17:16 +02004602 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004603 {
4604 status = PSA_ERROR_BUFFER_TOO_SMALL;
4605 goto exit;
4606 }
mohammad160382759612018-03-12 18:16:40 +02004607
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004608 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4609 {
4610 /* mbedtls_cipher_update has an API inconsistency: it will only
4611 * process a single block at a time in ECB mode. Abstract away that
4612 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004613 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4614 input,
4615 input_length,
4616 output,
4617 output_size,
4618 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004619 }
4620 else
4621 {
4622 status = mbedtls_to_psa_error(
4623 mbedtls_cipher_update( &operation->ctx.cipher, input,
4624 input_length, output, output_length ) );
4625 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004626exit:
4627 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004628 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004629 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004630}
4631
Gilles Peskinee553c652018-06-04 16:22:46 +02004632psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4633 uint8_t *output,
4634 size_t output_size,
4635 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004636{
David Saadab4ecc272019-02-14 13:48:10 +02004637 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004638 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004639 if( operation->alg == 0 )
4640 {
4641 return( PSA_ERROR_BAD_STATE );
4642 }
4643 if( operation->iv_required && ! operation->iv_set )
4644 {
4645 return( PSA_ERROR_BAD_STATE );
4646 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004647
Steven Cooremanef8575e2020-09-11 11:44:50 +02004648 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004649 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004650 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004651 output,
4652 output_size,
4653 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004654 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004655 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004656
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004657 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004658 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004659 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004660 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004661 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004662 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004663 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004664 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004665 }
4666
Steven Cooremanef8575e2020-09-11 11:44:50 +02004667 status = mbedtls_to_psa_error(
4668 mbedtls_cipher_finish( &operation->ctx.cipher,
4669 temp_output_buffer,
4670 output_length ) );
4671 if( status != PSA_SUCCESS )
4672 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004673
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004674 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004675 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004676 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004677 memcpy( output, temp_output_buffer, *output_length );
4678 else
Janos Follath315b51c2018-07-09 16:04:51 +01004679 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004680
Steven Cooremanef8575e2020-09-11 11:44:50 +02004681exit:
4682 if( operation->mbedtls_in_use == 1 )
4683 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004684
Steven Cooremanef8575e2020-09-11 11:44:50 +02004685 if( status == PSA_SUCCESS )
4686 return( psa_cipher_abort( operation ) );
4687 else
4688 {
4689 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004690 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004691
Steven Cooremanef8575e2020-09-11 11:44:50 +02004692 return( status );
4693 }
mohammad1603503973b2018-03-12 15:59:30 +02004694}
4695
Gilles Peskinee553c652018-06-04 16:22:46 +02004696psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4697{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004698 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004699 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004700 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004701 * in use. It's ok to call abort on such an object, and there's
4702 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004703 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004704 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004705
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004706 /* Sanity check (shouldn't happen: operation->alg should
4707 * always have been initialized to a valid value). */
4708 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4709 return( PSA_ERROR_BAD_STATE );
4710
Steven Cooremanef8575e2020-09-11 11:44:50 +02004711 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004712 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004713 else
4714 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004715
Moran Peker41deec42018-04-04 15:43:05 +03004716 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004717 operation->key_set = 0;
4718 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004719 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004720 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004721 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004722 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004723
Moran Peker395db872018-05-31 14:07:14 +03004724 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004725}
4726
Gilles Peskinea0655c32018-04-30 17:06:50 +02004727
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004728
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004729
mohammad16035955c982018-04-26 00:53:03 +03004730/****************************************************************/
4731/* AEAD */
4732/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004733
Gilles Peskineedf9a652018-08-17 18:11:56 +02004734typedef struct
4735{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004736 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004737 const mbedtls_cipher_info_t *cipher_info;
4738 union
4739 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004740 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004741#if defined(MBEDTLS_CCM_C)
4742 mbedtls_ccm_context ccm;
4743#endif /* MBEDTLS_CCM_C */
4744#if defined(MBEDTLS_GCM_C)
4745 mbedtls_gcm_context gcm;
4746#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004747#if defined(MBEDTLS_CHACHAPOLY_C)
4748 mbedtls_chachapoly_context chachapoly;
4749#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004750 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004751 psa_algorithm_t core_alg;
4752 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004753 uint8_t tag_length;
4754} aead_operation_t;
4755
Ronald Cronf95a2b12020-10-22 15:24:49 +02004756#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4757
Gilles Peskine30a9e412019-01-14 18:36:12 +01004758static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004759{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004760 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004761 {
4762#if defined(MBEDTLS_CCM_C)
4763 case PSA_ALG_CCM:
4764 mbedtls_ccm_free( &operation->ctx.ccm );
4765 break;
4766#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004767#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004768 case PSA_ALG_GCM:
4769 mbedtls_gcm_free( &operation->ctx.gcm );
4770 break;
4771#endif /* MBEDTLS_GCM_C */
4772 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004773
4774 psa_decrement_key_slot_access_count( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004775}
4776
4777static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004778 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004779 psa_key_usage_t usage,
4780 psa_algorithm_t alg )
4781{
4782 psa_status_t status;
4783 size_t key_bits;
4784 mbedtls_cipher_id_t cipher_id;
4785
Ronald Croncf56a0a2020-08-04 09:51:30 +02004786 status = psa_get_transparent_key( key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004787 if( status != PSA_SUCCESS )
4788 return( status );
4789
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004790 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004791
4792 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004793 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004794 &cipher_id );
4795 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004796 {
4797 status = PSA_ERROR_NOT_SUPPORTED;
4798 goto cleanup;
4799 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004800
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004801 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004802 {
4803#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004804 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4805 operation->core_alg = PSA_ALG_CCM;
4806 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004807 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4808 * The call to mbedtls_ccm_encrypt_and_tag or
4809 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004810 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004811 {
4812 status = PSA_ERROR_INVALID_ARGUMENT;
4813 goto cleanup;
4814 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004815 mbedtls_ccm_init( &operation->ctx.ccm );
4816 status = mbedtls_to_psa_error(
4817 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004818 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004819 (unsigned int) key_bits ) );
4820 if( status != 0 )
4821 goto cleanup;
4822 break;
4823#endif /* MBEDTLS_CCM_C */
4824
4825#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004826 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4827 operation->core_alg = PSA_ALG_GCM;
4828 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004829 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4830 * The call to mbedtls_gcm_crypt_and_tag or
4831 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004832 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004833 {
4834 status = PSA_ERROR_INVALID_ARGUMENT;
4835 goto cleanup;
4836 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004837 mbedtls_gcm_init( &operation->ctx.gcm );
4838 status = mbedtls_to_psa_error(
4839 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004840 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004841 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004842 if( status != 0 )
4843 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004844 break;
4845#endif /* MBEDTLS_GCM_C */
4846
Gilles Peskine26869f22019-05-06 15:25:00 +02004847#if defined(MBEDTLS_CHACHAPOLY_C)
4848 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4849 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4850 operation->full_tag_length = 16;
4851 /* We only support the default tag length. */
4852 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004853 {
4854 status = PSA_ERROR_NOT_SUPPORTED;
4855 goto cleanup;
4856 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004857 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4858 status = mbedtls_to_psa_error(
4859 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004860 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004861 if( status != 0 )
4862 goto cleanup;
4863 break;
4864#endif /* MBEDTLS_CHACHAPOLY_C */
4865
Gilles Peskineedf9a652018-08-17 18:11:56 +02004866 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004867 status = PSA_ERROR_NOT_SUPPORTED;
4868 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004869 }
4870
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004871 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4872 {
4873 status = PSA_ERROR_INVALID_ARGUMENT;
4874 goto cleanup;
4875 }
4876 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004877
Gilles Peskineedf9a652018-08-17 18:11:56 +02004878 return( PSA_SUCCESS );
4879
4880cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004881 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004882 return( status );
4883}
4884
Ronald Croncf56a0a2020-08-04 09:51:30 +02004885psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004886 psa_algorithm_t alg,
4887 const uint8_t *nonce,
4888 size_t nonce_length,
4889 const uint8_t *additional_data,
4890 size_t additional_data_length,
4891 const uint8_t *plaintext,
4892 size_t plaintext_length,
4893 uint8_t *ciphertext,
4894 size_t ciphertext_size,
4895 size_t *ciphertext_length )
4896{
mohammad16035955c982018-04-26 00:53:03 +03004897 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004898 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004899 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004900
mohammad1603f08a5502018-06-03 15:05:47 +03004901 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004902
Ronald Croncf56a0a2020-08-04 09:51:30 +02004903 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004904 if( status != PSA_SUCCESS )
4905 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004906
Gilles Peskineedf9a652018-08-17 18:11:56 +02004907 /* For all currently supported modes, the tag is at the end of the
4908 * ciphertext. */
4909 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4910 {
4911 status = PSA_ERROR_BUFFER_TOO_SMALL;
4912 goto exit;
4913 }
4914 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004915
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004916#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004917 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004918 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004919 status = mbedtls_to_psa_error(
4920 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4921 MBEDTLS_GCM_ENCRYPT,
4922 plaintext_length,
4923 nonce, nonce_length,
4924 additional_data, additional_data_length,
4925 plaintext, ciphertext,
4926 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004927 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004928 else
4929#endif /* MBEDTLS_GCM_C */
4930#if defined(MBEDTLS_CCM_C)
4931 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004932 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004933 status = mbedtls_to_psa_error(
4934 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4935 plaintext_length,
4936 nonce, nonce_length,
4937 additional_data,
4938 additional_data_length,
4939 plaintext, ciphertext,
4940 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004941 }
mohammad16035c8845f2018-05-09 05:40:09 -07004942 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004943#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004944#if defined(MBEDTLS_CHACHAPOLY_C)
4945 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4946 {
4947 if( nonce_length != 12 || operation.tag_length != 16 )
4948 {
4949 status = PSA_ERROR_NOT_SUPPORTED;
4950 goto exit;
4951 }
4952 status = mbedtls_to_psa_error(
4953 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4954 plaintext_length,
4955 nonce,
4956 additional_data,
4957 additional_data_length,
4958 plaintext,
4959 ciphertext,
4960 tag ) );
4961 }
4962 else
4963#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004964 {
mohammad1603554faad2018-06-03 15:07:38 +03004965 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004966 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004967
Gilles Peskineedf9a652018-08-17 18:11:56 +02004968 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4969 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004970
Gilles Peskineedf9a652018-08-17 18:11:56 +02004971exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004972 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004973 if( status == PSA_SUCCESS )
4974 *ciphertext_length = plaintext_length + operation.tag_length;
4975 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004976}
4977
Gilles Peskineee652a32018-06-01 19:23:52 +02004978/* Locate the tag in a ciphertext buffer containing the encrypted data
4979 * followed by the tag. Return the length of the part preceding the tag in
4980 * *plaintext_length. This is the size of the plaintext in modes where
4981 * the encrypted data has the same size as the plaintext, such as
4982 * CCM and GCM. */
4983static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4984 const uint8_t *ciphertext,
4985 size_t ciphertext_length,
4986 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004987 const uint8_t **p_tag )
4988{
4989 size_t payload_length;
4990 if( tag_length > ciphertext_length )
4991 return( PSA_ERROR_INVALID_ARGUMENT );
4992 payload_length = ciphertext_length - tag_length;
4993 if( payload_length > plaintext_size )
4994 return( PSA_ERROR_BUFFER_TOO_SMALL );
4995 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004996 return( PSA_SUCCESS );
4997}
4998
Ronald Croncf56a0a2020-08-04 09:51:30 +02004999psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005000 psa_algorithm_t alg,
5001 const uint8_t *nonce,
5002 size_t nonce_length,
5003 const uint8_t *additional_data,
5004 size_t additional_data_length,
5005 const uint8_t *ciphertext,
5006 size_t ciphertext_length,
5007 uint8_t *plaintext,
5008 size_t plaintext_size,
5009 size_t *plaintext_length )
5010{
mohammad16035955c982018-04-26 00:53:03 +03005011 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005012 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005013 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005014
Gilles Peskineee652a32018-06-01 19:23:52 +02005015 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005016
Ronald Croncf56a0a2020-08-04 09:51:30 +02005017 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005018 if( status != PSA_SUCCESS )
5019 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005020
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005021 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5022 ciphertext, ciphertext_length,
5023 plaintext_size, &tag );
5024 if( status != PSA_SUCCESS )
5025 goto exit;
5026
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005027#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005028 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005029 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005030 status = mbedtls_to_psa_error(
5031 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5032 ciphertext_length - operation.tag_length,
5033 nonce, nonce_length,
5034 additional_data,
5035 additional_data_length,
5036 tag, operation.tag_length,
5037 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005038 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005039 else
5040#endif /* MBEDTLS_GCM_C */
5041#if defined(MBEDTLS_CCM_C)
5042 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005043 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005044 status = mbedtls_to_psa_error(
5045 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5046 ciphertext_length - operation.tag_length,
5047 nonce, nonce_length,
5048 additional_data,
5049 additional_data_length,
5050 ciphertext, plaintext,
5051 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005052 }
mohammad160339574652018-06-01 04:39:53 -07005053 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005054#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005055#if defined(MBEDTLS_CHACHAPOLY_C)
5056 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5057 {
5058 if( nonce_length != 12 || operation.tag_length != 16 )
5059 {
5060 status = PSA_ERROR_NOT_SUPPORTED;
5061 goto exit;
5062 }
5063 status = mbedtls_to_psa_error(
5064 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5065 ciphertext_length - operation.tag_length,
5066 nonce,
5067 additional_data,
5068 additional_data_length,
5069 tag,
5070 ciphertext,
5071 plaintext ) );
5072 }
5073 else
5074#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005075 {
mohammad1603554faad2018-06-03 15:07:38 +03005076 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005077 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005078
Gilles Peskineedf9a652018-08-17 18:11:56 +02005079 if( status != PSA_SUCCESS && plaintext_size != 0 )
5080 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005081
Gilles Peskineedf9a652018-08-17 18:11:56 +02005082exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005083 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005084 if( status == PSA_SUCCESS )
5085 *plaintext_length = ciphertext_length - operation.tag_length;
5086 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005087}
5088
Gilles Peskinea0655c32018-04-30 17:06:50 +02005089
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005090
Gilles Peskine20035e32018-02-03 22:44:14 +01005091/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005092/* Generators */
5093/****************************************************************/
5094
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005095#define HKDF_STATE_INIT 0 /* no input yet */
5096#define HKDF_STATE_STARTED 1 /* got salt */
5097#define HKDF_STATE_KEYED 2 /* got key */
5098#define HKDF_STATE_OUTPUT 3 /* output started */
5099
Gilles Peskinecbe66502019-05-16 16:59:18 +02005100static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005101 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005102{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005103 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5104 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005105 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005106 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005107}
5108
5109
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005111{
5112 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005113 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005114 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005115 {
5116 /* The object has (apparently) been initialized but it is not
5117 * in use. It's ok to call abort on such an object, and there's
5118 * nothing to do. */
5119 }
5120 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02005121#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005122 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005123 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005124 mbedtls_free( operation->ctx.hkdf.info );
5125 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005126 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005127 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005129 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005130 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005131 if( operation->ctx.tls12_prf.seed != NULL )
5132 {
5133 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5134 operation->ctx.tls12_prf.seed_length );
5135 mbedtls_free( operation->ctx.tls12_prf.seed );
5136 }
5137
5138 if( operation->ctx.tls12_prf.label != NULL )
5139 {
5140 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5141 operation->ctx.tls12_prf.label_length );
5142 mbedtls_free( operation->ctx.tls12_prf.label );
5143 }
5144
5145 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5146
5147 /* We leave the fields Ai and output_block to be erased safely by the
5148 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005149 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005150 else
5151#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005152 {
5153 status = PSA_ERROR_BAD_STATE;
5154 }
Janos Follath083036a2019-06-11 10:22:26 +01005155 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005156 return( status );
5157}
5158
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005159psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005160 size_t *capacity)
5161{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005162 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005163 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005164 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005165 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005166 }
5167
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005168 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005169 return( PSA_SUCCESS );
5170}
5171
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005172psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005173 size_t capacity )
5174{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005175 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005176 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005177 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005178 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005179 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005180 return( PSA_SUCCESS );
5181}
5182
Gilles Peskinebef7f142018-07-12 17:22:21 +02005183#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005184/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005185 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005186static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005187 psa_algorithm_t hash_alg,
5188 uint8_t *output,
5189 size_t output_length )
5190{
5191 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5192 psa_status_t status;
5193
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005194 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5195 return( PSA_ERROR_BAD_STATE );
5196 hkdf->state = HKDF_STATE_OUTPUT;
5197
Gilles Peskinebef7f142018-07-12 17:22:21 +02005198 while( output_length != 0 )
5199 {
5200 /* Copy what remains of the current block */
5201 uint8_t n = hash_length - hkdf->offset_in_block;
5202 if( n > output_length )
5203 n = (uint8_t) output_length;
5204 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5205 output += n;
5206 output_length -= n;
5207 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005208 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005209 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005210 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005211 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005212 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005213 * object was corrupted or if this function is called directly
5214 * inside the library. */
5215 if( hkdf->block_number == 0xff )
5216 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005217
5218 /* We need a new block */
5219 ++hkdf->block_number;
5220 hkdf->offset_in_block = 0;
5221 status = psa_hmac_setup_internal( &hkdf->hmac,
5222 hkdf->prk, hash_length,
5223 hash_alg );
5224 if( status != PSA_SUCCESS )
5225 return( status );
5226 if( hkdf->block_number != 1 )
5227 {
5228 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5229 hkdf->output_block,
5230 hash_length );
5231 if( status != PSA_SUCCESS )
5232 return( status );
5233 }
5234 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5235 hkdf->info,
5236 hkdf->info_length );
5237 if( status != PSA_SUCCESS )
5238 return( status );
5239 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5240 &hkdf->block_number, 1 );
5241 if( status != PSA_SUCCESS )
5242 return( status );
5243 status = psa_hmac_finish_internal( &hkdf->hmac,
5244 hkdf->output_block,
5245 sizeof( hkdf->output_block ) );
5246 if( status != PSA_SUCCESS )
5247 return( status );
5248 }
5249
5250 return( PSA_SUCCESS );
5251}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005252
Janos Follath7742fee2019-06-17 12:58:10 +01005253static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5254 psa_tls12_prf_key_derivation_t *tls12_prf,
5255 psa_algorithm_t alg )
5256{
5257 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5258 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005259 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5260 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005261
Janos Follath7742fee2019-06-17 12:58:10 +01005262 /* We can't be wanting more output after block 0xff, otherwise
5263 * the capacity check in psa_key_derivation_output_bytes() would have
5264 * prevented this call. It could happen only if the operation
5265 * object was corrupted or if this function is called directly
5266 * inside the library. */
5267 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005268 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005269
5270 /* We need a new block */
5271 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005272 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005273
5274 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5275 *
5276 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5277 *
5278 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5279 * HMAC_hash(secret, A(2) + seed) +
5280 * HMAC_hash(secret, A(3) + seed) + ...
5281 *
5282 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005283 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005284 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005285 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005286 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005287 * is currently extracted as `output_block` and where i is
5288 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005289 */
5290
Janos Follathea29bfb2019-06-19 12:21:20 +01005291 /* Save the hash context before using it, to preserve the hash state with
5292 * only the inner padding in it. We need this, because inner padding depends
5293 * on the key (secret in the RFC's terminology). */
5294 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5295 if( status != PSA_SUCCESS )
5296 goto cleanup;
5297
5298 /* Calculate A(i) where i = tls12_prf->block_number. */
5299 if( tls12_prf->block_number == 1 )
5300 {
5301 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5302 * the variable seed and in this instance means it in the context of the
5303 * P_hash function, where seed = label + seed.) */
5304 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5305 tls12_prf->label, tls12_prf->label_length );
5306 if( status != PSA_SUCCESS )
5307 goto cleanup;
5308 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5309 tls12_prf->seed, tls12_prf->seed_length );
5310 if( status != PSA_SUCCESS )
5311 goto cleanup;
5312 }
5313 else
5314 {
5315 /* A(i) = HMAC_hash(secret, A(i-1)) */
5316 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5317 tls12_prf->Ai, hash_length );
5318 if( status != PSA_SUCCESS )
5319 goto cleanup;
5320 }
5321
5322 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5323 tls12_prf->Ai, hash_length );
5324 if( status != PSA_SUCCESS )
5325 goto cleanup;
5326 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5327 if( status != PSA_SUCCESS )
5328 goto cleanup;
5329
5330 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5331 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5332 tls12_prf->Ai, hash_length );
5333 if( status != PSA_SUCCESS )
5334 goto cleanup;
5335 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5336 tls12_prf->label, tls12_prf->label_length );
5337 if( status != PSA_SUCCESS )
5338 goto cleanup;
5339 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5340 tls12_prf->seed, tls12_prf->seed_length );
5341 if( status != PSA_SUCCESS )
5342 goto cleanup;
5343 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5344 tls12_prf->output_block, hash_length );
5345 if( status != PSA_SUCCESS )
5346 goto cleanup;
5347 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5348 if( status != PSA_SUCCESS )
5349 goto cleanup;
5350
Janos Follath7742fee2019-06-17 12:58:10 +01005351
5352cleanup:
5353
Janos Follathea29bfb2019-06-19 12:21:20 +01005354 cleanup_status = psa_hash_abort( &backup );
5355 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5356 status = cleanup_status;
5357
5358 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005359}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005360
Janos Follath844eb0e2019-06-19 12:10:49 +01005361static psa_status_t psa_key_derivation_tls12_prf_read(
5362 psa_tls12_prf_key_derivation_t *tls12_prf,
5363 psa_algorithm_t alg,
5364 uint8_t *output,
5365 size_t output_length )
5366{
5367 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5368 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5369 psa_status_t status;
5370 uint8_t offset, length;
5371
5372 while( output_length != 0 )
5373 {
5374 /* Check if we have fully processed the current block. */
5375 if( tls12_prf->left_in_block == 0 )
5376 {
5377 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5378 alg );
5379 if( status != PSA_SUCCESS )
5380 return( status );
5381
5382 continue;
5383 }
5384
5385 if( tls12_prf->left_in_block > output_length )
5386 length = (uint8_t) output_length;
5387 else
5388 length = tls12_prf->left_in_block;
5389
5390 offset = hash_length - tls12_prf->left_in_block;
5391 memcpy( output, tls12_prf->output_block + offset, length );
5392 output += length;
5393 output_length -= length;
5394 tls12_prf->left_in_block -= length;
5395 }
5396
5397 return( PSA_SUCCESS );
5398}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005399#endif /* MBEDTLS_MD_C */
5400
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005401psa_status_t psa_key_derivation_output_bytes(
5402 psa_key_derivation_operation_t *operation,
5403 uint8_t *output,
5404 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005405{
5406 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005407 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005408
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005409 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005410 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005411 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005412 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005413 }
5414
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005415 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005416 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005417 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005418 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005419 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005420 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005421 goto exit;
5422 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005423 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005424 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005425 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005426 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005427 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5428 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005429 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005430 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005431 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005432 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005433 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005434
Gilles Peskinebef7f142018-07-12 17:22:21 +02005435#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005436 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005437 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005438 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005439 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005440 output, output_length );
5441 }
Janos Follathadbec812019-06-14 11:05:39 +01005442 else
Janos Follathadbec812019-06-14 11:05:39 +01005443 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005444 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005445 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005446 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005447 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005448 output_length );
5449 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005450 else
5451#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005452 {
5453 return( PSA_ERROR_BAD_STATE );
5454 }
5455
5456exit:
5457 if( status != PSA_SUCCESS )
5458 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005459 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005460 * This allows us to differentiate between exhausted operations and
5461 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5462 * operations. */
5463 psa_algorithm_t alg = operation->alg;
5464 psa_key_derivation_abort( operation );
5465 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005466 memset( output, '!', output_length );
5467 }
5468 return( status );
5469}
5470
Gilles Peskine08542d82018-07-19 17:05:42 +02005471#if defined(MBEDTLS_DES_C)
5472static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5473{
5474 if( data_size >= 8 )
5475 mbedtls_des_key_set_parity( data );
5476 if( data_size >= 16 )
5477 mbedtls_des_key_set_parity( data + 8 );
5478 if( data_size >= 24 )
5479 mbedtls_des_key_set_parity( data + 16 );
5480}
5481#endif /* MBEDTLS_DES_C */
5482
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005483static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005484 psa_key_slot_t *slot,
5485 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005486 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005487{
5488 uint8_t *data = NULL;
5489 size_t bytes = PSA_BITS_TO_BYTES( bits );
5490 psa_status_t status;
5491
Gilles Peskine8e338702019-07-30 20:06:31 +02005492 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005493 return( PSA_ERROR_INVALID_ARGUMENT );
5494 if( bits % 8 != 0 )
5495 return( PSA_ERROR_INVALID_ARGUMENT );
5496 data = mbedtls_calloc( 1, bytes );
5497 if( data == NULL )
5498 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5499
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005500 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005501 if( status != PSA_SUCCESS )
5502 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005503#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005504 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005505 psa_des_set_key_parity( data, bytes );
5506#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005507 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005508
5509exit:
5510 mbedtls_free( data );
5511 return( status );
5512}
5513
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005514psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005515 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005516 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005517{
5518 psa_status_t status;
5519 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005520 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005521
5522 /* Reject any attempt to create a zero-length key so that we don't
5523 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5524 if( psa_get_key_bits( attributes ) == 0 )
5525 return( PSA_ERROR_INVALID_ARGUMENT );
5526
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005527 if( ! operation->can_output_key )
5528 return( PSA_ERROR_NOT_PERMITTED );
5529
Gilles Peskinedf179142019-07-15 22:02:14 +02005530 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005531 attributes, key, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005532#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5533 if( driver != NULL )
5534 {
5535 /* Deriving a key in a secure element is not implemented yet. */
5536 status = PSA_ERROR_NOT_SUPPORTED;
5537 }
5538#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005539 if( status == PSA_SUCCESS )
5540 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005541 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005542 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005543 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005544 }
5545 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005546 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005547 if( status != PSA_SUCCESS )
5548 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005549 psa_fail_key_creation( slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02005550 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005551 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005552 else
5553 status = psa_decrement_key_slot_access_count( slot );
5554
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005555 return( status );
5556}
5557
Gilles Peskineeab56e42018-07-12 17:12:33 +02005558
5559
5560/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005561/* Key derivation */
5562/****************************************************************/
5563
Gilles Peskine969c5d62019-01-16 15:53:06 +01005564static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005565 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005566 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005567{
Janos Follath5fe19732019-06-20 15:09:30 +01005568 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5569 * initialisers for this union leave some bytes unspecified.) */
5570 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5571
Gilles Peskine969c5d62019-01-16 15:53:06 +01005572 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005573#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005574 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5575 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5576 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005577 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005578 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005579 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5580 if( hash_size == 0 )
5581 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005582 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5583 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005584 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005585 {
5586 return( PSA_ERROR_NOT_SUPPORTED );
5587 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005588 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005589 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005590 }
5591#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005592 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005593 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005594}
5595
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005596psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005597 psa_algorithm_t alg )
5598{
5599 psa_status_t status;
5600
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005601 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005602 return( PSA_ERROR_BAD_STATE );
5603
Gilles Peskine6843c292019-01-18 16:44:49 +01005604 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5605 return( PSA_ERROR_INVALID_ARGUMENT );
5606 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005607 {
5608 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005609 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005610 }
5611 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5612 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005613 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005614 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005615 else
5616 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005617
5618 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005619 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005620 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005621}
5622
5623#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005624static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005625 psa_algorithm_t hash_alg,
5626 psa_key_derivation_step_t step,
5627 const uint8_t *data,
5628 size_t data_length )
5629{
5630 psa_status_t status;
5631 switch( step )
5632 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005633 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005634 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005635 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005636 status = psa_hmac_setup_internal( &hkdf->hmac,
5637 data, data_length,
5638 hash_alg );
5639 if( status != PSA_SUCCESS )
5640 return( status );
5641 hkdf->state = HKDF_STATE_STARTED;
5642 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005643 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005644 /* If no salt was provided, use an empty salt. */
5645 if( hkdf->state == HKDF_STATE_INIT )
5646 {
5647 status = psa_hmac_setup_internal( &hkdf->hmac,
5648 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005649 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005650 if( status != PSA_SUCCESS )
5651 return( status );
5652 hkdf->state = HKDF_STATE_STARTED;
5653 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005654 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005655 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005656 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5657 data, data_length );
5658 if( status != PSA_SUCCESS )
5659 return( status );
5660 status = psa_hmac_finish_internal( &hkdf->hmac,
5661 hkdf->prk,
5662 sizeof( hkdf->prk ) );
5663 if( status != PSA_SUCCESS )
5664 return( status );
5665 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5666 hkdf->block_number = 0;
5667 hkdf->state = HKDF_STATE_KEYED;
5668 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005669 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005670 if( hkdf->state == HKDF_STATE_OUTPUT )
5671 return( PSA_ERROR_BAD_STATE );
5672 if( hkdf->info_set )
5673 return( PSA_ERROR_BAD_STATE );
5674 hkdf->info_length = data_length;
5675 if( data_length != 0 )
5676 {
5677 hkdf->info = mbedtls_calloc( 1, data_length );
5678 if( hkdf->info == NULL )
5679 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5680 memcpy( hkdf->info, data, data_length );
5681 }
5682 hkdf->info_set = 1;
5683 return( PSA_SUCCESS );
5684 default:
5685 return( PSA_ERROR_INVALID_ARGUMENT );
5686 }
5687}
Janos Follathb03233e2019-06-11 15:30:30 +01005688
Janos Follathf08e2652019-06-13 09:05:41 +01005689static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5690 const uint8_t *data,
5691 size_t data_length )
5692{
5693 if( prf->state != TLS12_PRF_STATE_INIT )
5694 return( PSA_ERROR_BAD_STATE );
5695
Janos Follathd6dce9f2019-07-04 09:11:38 +01005696 if( data_length != 0 )
5697 {
5698 prf->seed = mbedtls_calloc( 1, data_length );
5699 if( prf->seed == NULL )
5700 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005701
Janos Follathd6dce9f2019-07-04 09:11:38 +01005702 memcpy( prf->seed, data, data_length );
5703 prf->seed_length = data_length;
5704 }
Janos Follathf08e2652019-06-13 09:05:41 +01005705
5706 prf->state = TLS12_PRF_STATE_SEED_SET;
5707
5708 return( PSA_SUCCESS );
5709}
5710
Janos Follath81550542019-06-13 14:26:34 +01005711static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5712 psa_algorithm_t hash_alg,
5713 const uint8_t *data,
5714 size_t data_length )
5715{
5716 psa_status_t status;
5717 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5718 return( PSA_ERROR_BAD_STATE );
5719
5720 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5721 if( status != PSA_SUCCESS )
5722 return( status );
5723
5724 prf->state = TLS12_PRF_STATE_KEY_SET;
5725
5726 return( PSA_SUCCESS );
5727}
5728
Janos Follath6660f0e2019-06-17 08:44:03 +01005729static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5730 psa_tls12_prf_key_derivation_t *prf,
5731 psa_algorithm_t hash_alg,
5732 const uint8_t *data,
5733 size_t data_length )
5734{
5735 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005736 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5737 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005738
5739 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5740 return( PSA_ERROR_INVALID_ARGUMENT );
5741
5742 /* Quoting RFC 4279, Section 2:
5743 *
5744 * The premaster secret is formed as follows: if the PSK is N octets
5745 * long, concatenate a uint16 with the value N, N zero octets, a second
5746 * uint16 with the value N, and the PSK itself.
5747 */
5748
Janos Follath40e13932019-06-26 13:22:29 +01005749 *cur++ = ( data_length >> 8 ) & 0xff;
5750 *cur++ = ( data_length >> 0 ) & 0xff;
5751 memset( cur, 0, data_length );
5752 cur += data_length;
5753 *cur++ = pms[0];
5754 *cur++ = pms[1];
5755 memcpy( cur, data, data_length );
5756 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005757
Janos Follath40e13932019-06-26 13:22:29 +01005758 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005759
5760 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5761 return( status );
5762}
5763
Janos Follath63028dd2019-06-13 09:15:47 +01005764static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5765 const uint8_t *data,
5766 size_t data_length )
5767{
5768 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5769 return( PSA_ERROR_BAD_STATE );
5770
Janos Follathd6dce9f2019-07-04 09:11:38 +01005771 if( data_length != 0 )
5772 {
5773 prf->label = mbedtls_calloc( 1, data_length );
5774 if( prf->label == NULL )
5775 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005776
Janos Follathd6dce9f2019-07-04 09:11:38 +01005777 memcpy( prf->label, data, data_length );
5778 prf->label_length = data_length;
5779 }
Janos Follath63028dd2019-06-13 09:15:47 +01005780
5781 prf->state = TLS12_PRF_STATE_LABEL_SET;
5782
5783 return( PSA_SUCCESS );
5784}
5785
Janos Follathb03233e2019-06-11 15:30:30 +01005786static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5787 psa_algorithm_t hash_alg,
5788 psa_key_derivation_step_t step,
5789 const uint8_t *data,
5790 size_t data_length )
5791{
Janos Follathb03233e2019-06-11 15:30:30 +01005792 switch( step )
5793 {
Janos Follathf08e2652019-06-13 09:05:41 +01005794 case PSA_KEY_DERIVATION_INPUT_SEED:
5795 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005796 case PSA_KEY_DERIVATION_INPUT_SECRET:
5797 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005798 case PSA_KEY_DERIVATION_INPUT_LABEL:
5799 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005800 default:
5801 return( PSA_ERROR_INVALID_ARGUMENT );
5802 }
5803}
Janos Follath6660f0e2019-06-17 08:44:03 +01005804
5805static psa_status_t psa_tls12_prf_psk_to_ms_input(
5806 psa_tls12_prf_key_derivation_t *prf,
5807 psa_algorithm_t hash_alg,
5808 psa_key_derivation_step_t step,
5809 const uint8_t *data,
5810 size_t data_length )
5811{
5812 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005813 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005814 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5815 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005816 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005817
5818 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5819}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005820#endif /* MBEDTLS_MD_C */
5821
Gilles Peskineb8965192019-09-24 16:21:10 +02005822/** Check whether the given key type is acceptable for the given
5823 * input step of a key derivation.
5824 *
5825 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5826 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5827 * Both secret and non-secret inputs can alternatively have the type
5828 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5829 * that the input was passed as a buffer rather than via a key object.
5830 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005831static int psa_key_derivation_check_input_type(
5832 psa_key_derivation_step_t step,
5833 psa_key_type_t key_type )
5834{
5835 switch( step )
5836 {
5837 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005838 if( key_type == PSA_KEY_TYPE_DERIVE )
5839 return( PSA_SUCCESS );
5840 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005841 return( PSA_SUCCESS );
5842 break;
5843 case PSA_KEY_DERIVATION_INPUT_LABEL:
5844 case PSA_KEY_DERIVATION_INPUT_SALT:
5845 case PSA_KEY_DERIVATION_INPUT_INFO:
5846 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005847 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5848 return( PSA_SUCCESS );
5849 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005850 return( PSA_SUCCESS );
5851 break;
5852 }
5853 return( PSA_ERROR_INVALID_ARGUMENT );
5854}
5855
Janos Follathb80a94e2019-06-12 15:54:46 +01005856static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005857 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005858 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005859 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005860 const uint8_t *data,
5861 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005862{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005863 psa_status_t status;
5864 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5865
5866 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005867 if( status != PSA_SUCCESS )
5868 goto exit;
5869
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005870#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005871 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005872 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005873 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005874 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005875 step, data, data_length );
5876 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005877 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005878 {
Janos Follathb03233e2019-06-11 15:30:30 +01005879 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5880 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5881 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005882 }
5883 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5884 {
5885 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5886 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5887 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005888 }
5889 else
5890#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005891 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005892 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005893 return( PSA_ERROR_BAD_STATE );
5894 }
5895
Gilles Peskine224b0d62019-09-23 18:13:17 +02005896exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005897 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005898 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005899 return( status );
5900}
5901
Janos Follath51f4a0f2019-06-14 11:35:55 +01005902psa_status_t psa_key_derivation_input_bytes(
5903 psa_key_derivation_operation_t *operation,
5904 psa_key_derivation_step_t step,
5905 const uint8_t *data,
5906 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005907{
Gilles Peskineb8965192019-09-24 16:21:10 +02005908 return( psa_key_derivation_input_internal( operation, step,
5909 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005910 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005911}
5912
Janos Follath51f4a0f2019-06-14 11:35:55 +01005913psa_status_t psa_key_derivation_input_key(
5914 psa_key_derivation_operation_t *operation,
5915 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005916 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005917{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005918 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5919 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005920 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005921
Ronald Croncf56a0a2020-08-04 09:51:30 +02005922 status = psa_get_transparent_key( key, &slot,
5923 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005924 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005925 {
5926 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005927 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005928 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005929
5930 /* Passing a key object as a SECRET input unlocks the permission
5931 * to output to a key object. */
5932 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5933 operation->can_output_key = 1;
5934
Ronald Cronf95a2b12020-10-22 15:24:49 +02005935 status = psa_key_derivation_input_internal( operation,
5936 step, slot->attr.type,
5937 slot->data.key.data,
5938 slot->data.key.bytes );
5939
5940 decrement_status = psa_decrement_key_slot_access_count( slot );
5941
5942 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005943}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005944
5945
5946
5947/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005948/* Key agreement */
5949/****************************************************************/
5950
Gilles Peskinea05219c2018-11-16 16:02:56 +01005951#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005952static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5953 size_t peer_key_length,
5954 const mbedtls_ecp_keypair *our_key,
5955 uint8_t *shared_secret,
5956 size_t shared_secret_size,
5957 size_t *shared_secret_length )
5958{
Steven Cooremand4867872020-08-05 16:31:39 +02005959 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005960 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005961 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005962 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005963 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005964 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005965
Steven Cooreman4fed4552020-08-03 14:46:03 +02005966 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5967 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005968 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005969 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005970 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005971 goto exit;
5972
Jaeden Amero97271b32019-01-10 19:38:51 +00005973 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005974 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005975 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005976 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005977 status = mbedtls_to_psa_error(
5978 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5979 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005980 goto exit;
5981
Jaeden Amero97271b32019-01-10 19:38:51 +00005982 status = mbedtls_to_psa_error(
5983 mbedtls_ecdh_calc_secret( &ecdh,
5984 shared_secret_length,
5985 shared_secret, shared_secret_size,
5986 mbedtls_ctr_drbg_random,
5987 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005988 if( status != PSA_SUCCESS )
5989 goto exit;
5990 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5991 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005992
5993exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005994 if( status != PSA_SUCCESS )
5995 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005996 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005997 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005998 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005999
Jaeden Amero97271b32019-01-10 19:38:51 +00006000 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006001}
Gilles Peskinea05219c2018-11-16 16:02:56 +01006002#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006003
Gilles Peskine01d718c2018-09-18 12:01:02 +02006004#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6005
Gilles Peskine0216fe12019-04-11 21:23:21 +02006006static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6007 psa_key_slot_t *private_key,
6008 const uint8_t *peer_key,
6009 size_t peer_key_length,
6010 uint8_t *shared_secret,
6011 size_t shared_secret_size,
6012 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006013{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006014 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006015 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006016#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006017 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006018 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006019 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006020 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006021 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006022 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006023 private_key->data.key.data,
6024 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006025 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006026 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006027 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006028 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006029 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006030 shared_secret, shared_secret_size,
6031 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006032 mbedtls_ecp_keypair_free( ecp );
6033 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006034 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006035#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006036 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006037 (void) private_key;
6038 (void) peer_key;
6039 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006040 (void) shared_secret;
6041 (void) shared_secret_size;
6042 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006043 return( PSA_ERROR_NOT_SUPPORTED );
6044 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006045}
6046
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006047/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006048 * to potentially free embedded data structures and wipe confidential data.
6049 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006050static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006051 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006052 psa_key_slot_t *private_key,
6053 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006054 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006055{
6056 psa_status_t status;
6057 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6058 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006059 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006060
6061 /* Step 1: run the secret agreement algorithm to generate the shared
6062 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006063 status = psa_key_agreement_raw_internal( ka_alg,
6064 private_key,
6065 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006066 shared_secret,
6067 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006068 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006069 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006070 goto exit;
6071
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006072 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006073 * the shared secret. A shared secret is permitted wherever a key
6074 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006075 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006076 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006077 shared_secret,
6078 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006079exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006080 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006081 return( status );
6082}
6083
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006084psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006085 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006086 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006087 const uint8_t *peer_key,
6088 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006089{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006090 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6091 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006092 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006093
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006094 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006095 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02006096 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006097 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006098 if( status != PSA_SUCCESS )
6099 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006100 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006101 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006102 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006103 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006104 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006105 else
6106 {
6107 /* If a private key has been added as SECRET, we allow the derived
6108 * key material to be used as a key in PSA Crypto. */
6109 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6110 operation->can_output_key = 1;
6111 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006112
6113 decrement_status = psa_decrement_key_slot_access_count( slot );
6114
6115 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006116}
6117
Gilles Peskinebe697d82019-05-16 18:00:41 +02006118psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006119 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006120 const uint8_t *peer_key,
6121 size_t peer_key_length,
6122 uint8_t *output,
6123 size_t output_size,
6124 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006125{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006126 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6127 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
6128 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006129
6130 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6131 {
6132 status = PSA_ERROR_INVALID_ARGUMENT;
6133 goto exit;
6134 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02006135 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006136 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006137 if( status != PSA_SUCCESS )
6138 goto exit;
6139
6140 status = psa_key_agreement_raw_internal( alg, slot,
6141 peer_key, peer_key_length,
6142 output, output_size,
6143 output_length );
6144
6145exit:
6146 if( status != PSA_SUCCESS )
6147 {
6148 /* If an error happens and is not handled properly, the output
6149 * may be used as a key to protect sensitive data. Arrange for such
6150 * a key to be random, which is likely to result in decryption or
6151 * verification errors. This is better than filling the buffer with
6152 * some constant data such as zeros, which would result in the data
6153 * being protected with a reproducible, easily knowable key.
6154 */
6155 psa_generate_random( output, output_size );
6156 *output_length = output_size;
6157 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006158
6159 decrement_status = psa_decrement_key_slot_access_count( slot );
6160
6161 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006162}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006163
6164
6165/****************************************************************/
6166/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006167/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006168
Gilles Peskine4c317f42018-07-12 01:24:09 +02006169psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006170 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006171{
Janos Follath24eed8d2019-11-22 13:21:35 +00006172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006173 GUARD_MODULE_INITIALIZED;
6174
Gilles Peskinef181eca2019-08-07 13:49:00 +02006175 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6176 {
6177 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6178 output,
6179 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6180 if( ret != 0 )
6181 return( mbedtls_to_psa_error( ret ) );
6182 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6183 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6184 }
6185
Gilles Peskinee59236f2018-01-27 23:32:46 +01006186 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6187 return( mbedtls_to_psa_error( ret ) );
6188}
6189
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006190#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6191#include "mbedtls/entropy_poll.h"
6192
Gilles Peskine7228da22019-07-15 11:06:15 +02006193psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006194 size_t seed_size )
6195{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006196 if( global_data.initialized )
6197 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006198
6199 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6200 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6201 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6202 return( PSA_ERROR_INVALID_ARGUMENT );
6203
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006204 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006205}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006206#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006207
Gilles Peskinee56e8782019-04-26 17:34:02 +02006208#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
6209static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6210 size_t domain_parameters_size,
6211 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006212{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006213 size_t i;
6214 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006215
Gilles Peskinee56e8782019-04-26 17:34:02 +02006216 if( domain_parameters_size == 0 )
6217 {
6218 *exponent = 65537;
6219 return( PSA_SUCCESS );
6220 }
6221
6222 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6223 * support values that fit in a 32-bit integer, which is larger than
6224 * int on just about every platform anyway. */
6225 if( domain_parameters_size > sizeof( acc ) )
6226 return( PSA_ERROR_NOT_SUPPORTED );
6227 for( i = 0; i < domain_parameters_size; i++ )
6228 acc = ( acc << 8 ) | domain_parameters[i];
6229 if( acc > INT_MAX )
6230 return( PSA_ERROR_NOT_SUPPORTED );
6231 *exponent = acc;
6232 return( PSA_SUCCESS );
6233}
6234#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6235
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006236static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006237 psa_key_slot_t *slot, size_t bits,
6238 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006239{
Gilles Peskine8e338702019-07-30 20:06:31 +02006240 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006241
Gilles Peskinee56e8782019-04-26 17:34:02 +02006242 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006243 return( PSA_ERROR_INVALID_ARGUMENT );
6244
Gilles Peskinee59236f2018-01-27 23:32:46 +01006245 if( key_type_is_raw_bytes( type ) )
6246 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006247 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006248
6249 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006250 if( status != PSA_SUCCESS )
6251 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006252
6253 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006254 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6255 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006256 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006257
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006258 status = psa_generate_random( slot->data.key.data,
6259 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006260 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006261 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006262
6263 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006264#if defined(MBEDTLS_DES_C)
6265 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006266 psa_des_set_key_parity( slot->data.key.data,
6267 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006268#endif /* MBEDTLS_DES_C */
6269 }
6270 else
6271
6272#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006273 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006274 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006275 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006276 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006277 int exponent;
6278 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006279 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6280 return( PSA_ERROR_NOT_SUPPORTED );
6281 /* Accept only byte-aligned keys, for the same reasons as
6282 * in psa_import_rsa_key(). */
6283 if( bits % 8 != 0 )
6284 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006285 status = psa_read_rsa_exponent( domain_parameters,
6286 domain_parameters_size,
6287 &exponent );
6288 if( status != PSA_SUCCESS )
6289 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006290 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6291 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006292 mbedtls_ctr_drbg_random,
6293 &global_data.ctr_drbg,
6294 (unsigned int) bits,
6295 exponent );
6296 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006297 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006298
6299 /* Make sure to always have an export representation available */
6300 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6301
Steven Cooreman75b74362020-07-28 14:30:13 +02006302 status = psa_allocate_buffer_to_slot( slot, bytes );
6303 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006304 {
6305 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006306 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006307 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006308
6309 status = psa_export_rsa_key( type,
6310 &rsa,
6311 slot->data.key.data,
6312 bytes,
6313 &slot->data.key.bytes );
6314 mbedtls_rsa_free( &rsa );
6315 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006316 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006317 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006318 }
6319 else
6320#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6321
6322#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006323 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006324 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006325 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006326 mbedtls_ecp_group_id grp_id =
6327 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006328 const mbedtls_ecp_curve_info *curve_info =
6329 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006330 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006331 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006332 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006333 return( PSA_ERROR_NOT_SUPPORTED );
6334 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6335 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006336 mbedtls_ecp_keypair_init( &ecp );
6337 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006338 mbedtls_ctr_drbg_random,
6339 &global_data.ctr_drbg );
6340 if( ret != 0 )
6341 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006342 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006343 return( mbedtls_to_psa_error( ret ) );
6344 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006345
6346
6347 /* Make sure to always have an export representation available */
6348 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006349 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6350 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006351 {
6352 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006353 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006354 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006355
6356 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006357 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6358
6359 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006360 if( status != PSA_SUCCESS ) {
6361 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006362 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006363 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006364 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006365 }
6366 else
6367#endif /* MBEDTLS_ECP_C */
Steven Cooreman29149862020-08-05 15:43:42 +02006368 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006369 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006370 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006371
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006372 return( PSA_SUCCESS );
6373}
Darryl Green0c6575a2018-11-07 16:05:30 +00006374
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006375psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006376 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006377{
6378 psa_status_t status;
6379 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006380 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006381
Gilles Peskine0f84d622019-09-12 19:03:13 +02006382 /* Reject any attempt to create a zero-length key so that we don't
6383 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6384 if( psa_get_key_bits( attributes ) == 0 )
6385 return( PSA_ERROR_INVALID_ARGUMENT );
6386
Gilles Peskinedf179142019-07-15 22:02:14 +02006387 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006388 attributes, key, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006389 if( status != PSA_SUCCESS )
6390 goto exit;
6391
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006392 status = psa_driver_wrapper_generate_key( attributes,
6393 slot );
6394 if( status != PSA_ERROR_NOT_SUPPORTED ||
6395 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6396 goto exit;
6397
6398 status = psa_generate_key_internal(
6399 slot, attributes->core.bits,
6400 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006401
6402exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006403 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006404 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006405 if( status != PSA_SUCCESS )
6406 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006407 psa_fail_key_creation( slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02006408 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006409 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006410 else
6411 status = psa_decrement_key_slot_access_count( slot );
6412
Darryl Green0c6575a2018-11-07 16:05:30 +00006413 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006414}
6415
6416
Gilles Peskinee59236f2018-01-27 23:32:46 +01006417
6418/****************************************************************/
6419/* Module setup */
6420/****************************************************************/
6421
Gilles Peskine5e769522018-11-20 21:59:56 +01006422psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6423 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6424 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6425{
6426 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6427 return( PSA_ERROR_BAD_STATE );
6428 global_data.entropy_init = entropy_init;
6429 global_data.entropy_free = entropy_free;
6430 return( PSA_SUCCESS );
6431}
6432
Gilles Peskinee59236f2018-01-27 23:32:46 +01006433void mbedtls_psa_crypto_free( void )
6434{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006435 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006436 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6437 {
6438 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006439 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006440 }
6441 /* Wipe all remaining data, including configuration.
6442 * In particular, this sets all state indicator to the value
6443 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006444 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006445#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006446 /* Unregister all secure element drivers, so that we restart from
6447 * a pristine state. */
6448 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006449#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006450}
6451
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006452#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6453/** Recover a transaction that was interrupted by a power failure.
6454 *
6455 * This function is called during initialization, before psa_crypto_init()
6456 * returns. If this function returns a failure status, the initialization
6457 * fails.
6458 */
6459static psa_status_t psa_crypto_recover_transaction(
6460 const psa_crypto_transaction_t *transaction )
6461{
6462 switch( transaction->unknown.type )
6463 {
6464 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6465 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006466 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006467 * is implemented.
6468 * https://github.com/ARMmbed/mbed-crypto/issues/218
6469 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006470 default:
6471 /* We found an unsupported transaction in the storage.
6472 * We don't know what state the storage is in. Give up. */
6473 return( PSA_ERROR_STORAGE_FAILURE );
6474 }
6475}
6476#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6477
Gilles Peskinee59236f2018-01-27 23:32:46 +01006478psa_status_t psa_crypto_init( void )
6479{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006480 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006481 const unsigned char drbg_seed[] = "PSA";
6482
Gilles Peskinec6b69072018-11-20 21:42:52 +01006483 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006484 if( global_data.initialized != 0 )
6485 return( PSA_SUCCESS );
6486
Gilles Peskine5e769522018-11-20 21:59:56 +01006487 /* Set default configuration if
6488 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6489 if( global_data.entropy_init == NULL )
6490 global_data.entropy_init = mbedtls_entropy_init;
6491 if( global_data.entropy_free == NULL )
6492 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006493
Gilles Peskinec6b69072018-11-20 21:42:52 +01006494 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006495 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006496#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6497 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6498 /* The PSA entropy injection feature depends on using NV seed as an entropy
6499 * source. Add NV seed as an entropy source for PSA entropy injection. */
6500 mbedtls_entropy_add_source( &global_data.entropy,
6501 mbedtls_nv_seed_poll, NULL,
6502 MBEDTLS_ENTROPY_BLOCK_SIZE,
6503 MBEDTLS_ENTROPY_SOURCE_STRONG );
6504#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006505 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006506 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006507 status = mbedtls_to_psa_error(
6508 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6509 mbedtls_entropy_func,
6510 &global_data.entropy,
6511 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6512 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006513 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006514 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006515
Gilles Peskine66fb1262018-12-10 16:29:04 +01006516 status = psa_initialize_key_slots( );
6517 if( status != PSA_SUCCESS )
6518 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006519
Gilles Peskined9348f22019-10-01 15:22:29 +02006520#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6521 status = psa_init_all_se_drivers( );
6522 if( status != PSA_SUCCESS )
6523 goto exit;
6524#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6525
Gilles Peskine4b734222019-07-24 15:56:31 +02006526#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006527 status = psa_crypto_load_transaction( );
6528 if( status == PSA_SUCCESS )
6529 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006530 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6531 if( status != PSA_SUCCESS )
6532 goto exit;
6533 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006534 }
6535 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6536 {
6537 /* There's no transaction to complete. It's all good. */
6538 status = PSA_SUCCESS;
6539 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006540#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006541
Gilles Peskinec6b69072018-11-20 21:42:52 +01006542 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006543 global_data.initialized = 1;
6544
6545exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006546 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006547 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006548 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006549}
6550
6551#endif /* MBEDTLS_PSA_CRYPTO_C */