blob: 04a6514d5ddf8c31b21f6af07f7527773f662d04 [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 Croncf56a0a2020-08-04 09:51:30 +02001347 status = psa_get_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001348 if( status != PSA_SUCCESS )
1349 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001350
1351#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001352 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001353 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001354 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001355 /* For a key in a secure element, we need to do three things:
1356 * remove the key file in internal storage, destroy the
1357 * key inside the secure element, and update the driver's
1358 * persistent data. Start a transaction that will encompass these
1359 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001360 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001361 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001362 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001363 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001364 status = psa_crypto_save_transaction( );
1365 if( status != PSA_SUCCESS )
1366 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001367 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001368 /* We should still try to destroy the key in the secure
1369 * element and the key metadata in storage. This is especially
1370 * important if the error is that the storage is full.
1371 * But how to do it exactly without risking an inconsistent
1372 * state after a reset?
1373 * https://github.com/ARMmbed/mbed-crypto/issues/215
1374 */
1375 overall_status = status;
1376 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001377 }
1378
Gilles Peskine354f7672019-07-12 23:46:38 +02001379 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001380 if( overall_status == PSA_SUCCESS )
1381 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001382 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001383#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1384
Darryl Greend49a4992018-06-18 17:27:26 +01001385#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001386 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001387 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001388 status = psa_destroy_persistent_key( slot->attr.id );
1389 if( overall_status == PSA_SUCCESS )
1390 overall_status = status;
1391
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001392 /* TODO: other slots may have a copy of the same key. We should
1393 * invalidate them.
1394 * https://github.com/ARMmbed/mbed-crypto/issues/214
1395 */
Darryl Greend49a4992018-06-18 17:27:26 +01001396 }
1397#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001398
Gilles Peskinefc762652019-07-22 19:30:34 +02001399#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1400 if( driver != NULL )
1401 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001402 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001403 if( overall_status == PSA_SUCCESS )
1404 overall_status = status;
1405 status = psa_crypto_stop_transaction( );
1406 if( overall_status == PSA_SUCCESS )
1407 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001408 }
1409#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1410
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001411#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1412exit:
1413#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001414 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001415 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1416 if( overall_status == PSA_SUCCESS )
1417 overall_status = status;
1418 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001419}
1420
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001421void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001422{
Gilles Peskineb699f072019-04-26 16:06:02 +02001423 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001424 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001425}
1426
Gilles Peskineb699f072019-04-26 16:06:02 +02001427psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1428 psa_key_type_t type,
1429 const uint8_t *data,
1430 size_t data_length )
1431{
1432 uint8_t *copy = NULL;
1433
1434 if( data_length != 0 )
1435 {
1436 copy = mbedtls_calloc( 1, data_length );
1437 if( copy == NULL )
1438 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1439 memcpy( copy, data, data_length );
1440 }
1441 /* After this point, this function is guaranteed to succeed, so it
1442 * can start modifying `*attributes`. */
1443
1444 if( attributes->domain_parameters != NULL )
1445 {
1446 mbedtls_free( attributes->domain_parameters );
1447 attributes->domain_parameters = NULL;
1448 attributes->domain_parameters_size = 0;
1449 }
1450
1451 attributes->domain_parameters = copy;
1452 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001453 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001454 return( PSA_SUCCESS );
1455}
1456
1457psa_status_t psa_get_key_domain_parameters(
1458 const psa_key_attributes_t *attributes,
1459 uint8_t *data, size_t data_size, size_t *data_length )
1460{
1461 if( attributes->domain_parameters_size > data_size )
1462 return( PSA_ERROR_BUFFER_TOO_SMALL );
1463 *data_length = attributes->domain_parameters_size;
1464 if( attributes->domain_parameters_size != 0 )
1465 memcpy( data, attributes->domain_parameters,
1466 attributes->domain_parameters_size );
1467 return( PSA_SUCCESS );
1468}
1469
1470#if defined(MBEDTLS_RSA_C)
1471static psa_status_t psa_get_rsa_public_exponent(
1472 const mbedtls_rsa_context *rsa,
1473 psa_key_attributes_t *attributes )
1474{
1475 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001476 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001477 uint8_t *buffer = NULL;
1478 size_t buflen;
1479 mbedtls_mpi_init( &mpi );
1480
1481 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1482 if( ret != 0 )
1483 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001484 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1485 {
1486 /* It's the default value, which is reported as an empty string,
1487 * so there's nothing to do. */
1488 goto exit;
1489 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001490
1491 buflen = mbedtls_mpi_size( &mpi );
1492 buffer = mbedtls_calloc( 1, buflen );
1493 if( buffer == NULL )
1494 {
1495 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1496 goto exit;
1497 }
1498 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1499 if( ret != 0 )
1500 goto exit;
1501 attributes->domain_parameters = buffer;
1502 attributes->domain_parameters_size = buflen;
1503
1504exit:
1505 mbedtls_mpi_free( &mpi );
1506 if( ret != 0 )
1507 mbedtls_free( buffer );
1508 return( mbedtls_to_psa_error( ret ) );
1509}
1510#endif /* MBEDTLS_RSA_C */
1511
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001512/** Retrieve all the publicly-accessible attributes of a key.
1513 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001514psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001515 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001516{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001517 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1518 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001519 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001520
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001521 psa_reset_key_attributes( attributes );
1522
Ronald Croncf56a0a2020-08-04 09:51:30 +02001523 status = psa_get_key_from_slot( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001524 if( status != PSA_SUCCESS )
1525 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001526
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001527 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001528 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1529 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1530
1531#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1532 if( psa_key_slot_is_external( slot ) )
1533 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1534#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001535
Gilles Peskine8e338702019-07-30 20:06:31 +02001536 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001537 {
1538#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001539 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001540 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001541#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001542 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001543 * is not yet implemented.
1544 * https://github.com/ARMmbed/mbed-crypto/issues/216
1545 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001546 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001547 break;
1548#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001549 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001550 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001551
Steven Cooreman4fed4552020-08-03 14:46:03 +02001552 status = psa_load_rsa_representation( slot->attr.type,
1553 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001554 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001555 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001556 if( status != PSA_SUCCESS )
1557 break;
1558
Steven Cooremana2371e52020-07-28 14:30:39 +02001559 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001560 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001561 mbedtls_rsa_free( rsa );
1562 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001563 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001564 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001565#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001566 default:
1567 /* Nothing else to do. */
1568 break;
1569 }
1570
1571 if( status != PSA_SUCCESS )
1572 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001573
1574 decrement_status = psa_decrement_key_slot_access_count( slot );
1575
1576 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001577}
1578
Gilles Peskinec8000c02019-08-02 20:15:51 +02001579#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1580psa_status_t psa_get_key_slot_number(
1581 const psa_key_attributes_t *attributes,
1582 psa_key_slot_number_t *slot_number )
1583{
1584 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1585 {
1586 *slot_number = attributes->slot_number;
1587 return( PSA_SUCCESS );
1588 }
1589 else
1590 return( PSA_ERROR_INVALID_ARGUMENT );
1591}
1592#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1593
Steven Cooremana01795d2020-07-24 22:48:15 +02001594static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1595 uint8_t *data,
1596 size_t data_size,
1597 size_t *data_length )
1598{
1599 if( slot->data.key.bytes > data_size )
1600 return( PSA_ERROR_BUFFER_TOO_SMALL );
1601 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1602 memset( data + slot->data.key.bytes, 0,
1603 data_size - slot->data.key.bytes );
1604 *data_length = slot->data.key.bytes;
1605 return( PSA_SUCCESS );
1606}
1607
Gilles Peskinef603c712019-01-19 13:40:11 +01001608static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1609 uint8_t *data,
1610 size_t data_size,
1611 size_t *data_length,
1612 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001613{
Gilles Peskine5d309672019-07-12 23:47:28 +02001614#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1615 const psa_drv_se_t *drv;
1616 psa_drv_se_context_t *drv_context;
1617#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1618
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001619 *data_length = 0;
1620
Gilles Peskine8e338702019-07-30 20:06:31 +02001621 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001622 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001623
Gilles Peskinef9168942019-09-12 19:20:29 +02001624 /* Reject a zero-length output buffer now, since this can never be a
1625 * valid key representation. This way we know that data must be a valid
1626 * pointer and we can do things like memset(data, ..., data_size). */
1627 if( data_size == 0 )
1628 return( PSA_ERROR_BUFFER_TOO_SMALL );
1629
Gilles Peskine5d309672019-07-12 23:47:28 +02001630#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001631 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001632 {
1633 psa_drv_se_export_key_t method;
1634 if( drv->key_management == NULL )
1635 return( PSA_ERROR_NOT_SUPPORTED );
1636 method = ( export_public_key ?
1637 drv->key_management->p_export_public :
1638 drv->key_management->p_export );
1639 if( method == NULL )
1640 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001641 return( method( drv_context,
1642 slot->data.se.slot_number,
1643 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001644 }
1645#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1646
Gilles Peskine8e338702019-07-30 20:06:31 +02001647 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001648 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001649 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001650 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001651 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1652 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001653 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001654 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001655 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001656 /* Exporting public -> public */
1657 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1658 }
1659 else if( !export_public_key )
1660 {
1661 /* Exporting private -> private */
1662 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1663 }
1664 /* Need to export the public part of a private key,
1665 * so conversion is needed */
1666 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1667 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001668#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001669 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001670 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001671 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001672 slot->data.key.data,
1673 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001674 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001675 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001676 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001677
Steven Cooreman560c28a2020-07-24 23:20:24 +02001678 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001679 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001680 data,
1681 data_size,
1682 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001683
Steven Cooremana2371e52020-07-28 14:30:39 +02001684 mbedtls_rsa_free( rsa );
1685 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001686
Steven Cooreman560c28a2020-07-24 23:20:24 +02001687 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001688#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001689 /* We don't know how to convert a private RSA key to public. */
1690 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001691#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001692 }
1693 else
Moran Pekera998bc62018-04-16 18:16:20 +03001694 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001695#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001696 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001697 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001698 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001699 slot->data.key.data,
1700 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001701 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001702 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001703 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001704
1705 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1706 PSA_KEY_TYPE_ECC_GET_FAMILY(
1707 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001708 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001709 data,
1710 data_size,
1711 data_length );
1712
Steven Cooremana2371e52020-07-28 14:30:39 +02001713 mbedtls_ecp_keypair_free( ecp );
1714 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001715 return( status );
1716#else
1717 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001718 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001719#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001720 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001721 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001722 else
1723 {
1724 /* This shouldn't happen in the reference implementation, but
1725 it is valid for a special-purpose implementation to omit
1726 support for exporting certain key types. */
1727 return( PSA_ERROR_NOT_SUPPORTED );
1728 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001729}
1730
Ronald Croncf56a0a2020-08-04 09:51:30 +02001731psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001732 uint8_t *data,
1733 size_t data_size,
1734 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001735{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001736 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1737 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001738 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001739
1740 /* Set the key to empty now, so that even when there are errors, we always
1741 * set data_length to a value between 0 and data_size. On error, setting
1742 * the key to empty is a good choice because an empty key representation is
1743 * unlikely to be accepted anywhere. */
1744 *data_length = 0;
1745
1746 /* Export requires the EXPORT flag. There is an exception for public keys,
1747 * which don't require any flag, but psa_get_key_from_slot takes
1748 * care of this. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001749 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001750 if( status != PSA_SUCCESS )
1751 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001752
1753 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
1754 decrement_status = psa_decrement_key_slot_access_count( slot );
1755
1756 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001757}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001758
Ronald Croncf56a0a2020-08-04 09:51:30 +02001759psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001760 uint8_t *data,
1761 size_t data_size,
1762 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001763{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001764 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1765 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001766 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001767
1768 /* Set the key to empty now, so that even when there are errors, we always
1769 * set data_length to a value between 0 and data_size. On error, setting
1770 * the key to empty is a good choice because an empty key representation is
1771 * unlikely to be accepted anywhere. */
1772 *data_length = 0;
1773
1774 /* Exporting a public key doesn't require a usage flag. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001775 status = psa_get_key_from_slot( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001776 if( status != PSA_SUCCESS )
1777 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001778
1779 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
1780 decrement_status = psa_decrement_key_slot_access_count( slot );
1781
1782 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001783}
1784
Gilles Peskine91e8c332019-08-02 19:19:39 +02001785#if defined(static_assert)
1786static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1787 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001788static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001789 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001790static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001791 "One or more key attribute flag is listed as both internal-only and external-only" );
1792#endif
1793
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001794/** Validate that a key policy is internally well-formed.
1795 *
1796 * This function only rejects invalid policies. It does not validate the
1797 * consistency of the policy with respect to other attributes of the key
1798 * such as the key type.
1799 */
1800static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001801{
1802 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001803 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001804 PSA_KEY_USAGE_ENCRYPT |
1805 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001806 PSA_KEY_USAGE_SIGN_HASH |
1807 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001808 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1809 return( PSA_ERROR_INVALID_ARGUMENT );
1810
Gilles Peskine4747d192019-04-17 15:05:45 +02001811 return( PSA_SUCCESS );
1812}
1813
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001814/** Validate the internal consistency of key attributes.
1815 *
1816 * This function only rejects invalid attribute values. If does not
1817 * validate the consistency of the attributes with any key data that may
1818 * be involved in the creation of the key.
1819 *
1820 * Call this function early in the key creation process.
1821 *
1822 * \param[in] attributes Key attributes for the new key.
1823 * \param[out] p_drv On any return, the driver for the key, if any.
1824 * NULL for a transparent key.
1825 *
1826 */
1827static psa_status_t psa_validate_key_attributes(
1828 const psa_key_attributes_t *attributes,
1829 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001830{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001831 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001832 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001833 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001834
Ronald Cron54b90082020-10-29 15:26:43 +01001835 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001836 if( status != PSA_SUCCESS )
1837 return( status );
1838
Ronald Crond2ed4812020-07-17 16:11:30 +02001839 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001840 if( status != PSA_SUCCESS )
1841 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001842
Ronald Cron65f38a32020-10-23 17:11:13 +02001843 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1844 {
1845 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1846 return( PSA_ERROR_INVALID_ARGUMENT );
1847 }
1848 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001849 {
Ronald Cron54b90082020-10-29 15:26:43 +01001850 status = psa_validate_key_id( psa_get_key_id( attributes ), 0, 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001851 if( status != PSA_SUCCESS )
1852 return( status );
1853 }
1854
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001855 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001856 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001857 return( status );
1858
1859 /* Refuse to create overly large keys.
1860 * Note that this doesn't trigger on import if the attributes don't
1861 * explicitly specify a size (so psa_get_key_bits returns 0), so
1862 * psa_import_key() needs its own checks. */
1863 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1864 return( PSA_ERROR_NOT_SUPPORTED );
1865
Gilles Peskine91e8c332019-08-02 19:19:39 +02001866 /* Reject invalid flags. These should not be reachable through the API. */
1867 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1868 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1869 return( PSA_ERROR_INVALID_ARGUMENT );
1870
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001871 return( PSA_SUCCESS );
1872}
1873
Gilles Peskine4747d192019-04-17 15:05:45 +02001874/** Prepare a key slot to receive key material.
1875 *
1876 * This function allocates a key slot and sets its metadata.
1877 *
1878 * If this function fails, call psa_fail_key_creation().
1879 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001880 * This function is intended to be used as follows:
1881 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001882 * it with the specified attributes, and in case of a volatile key assign it
1883 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001884 * -# Populate the slot with the key material.
1885 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1886 * In case of failure at any step, stop the sequence and call
1887 * psa_fail_key_creation().
1888 *
Ronald Cronf95a2b12020-10-22 15:24:49 +02001889 * On success, the access counter of the returned key slot is incremented by
1890 * one. It is the responsibility of the caller to call
1891 * psa_decrement_key_slot_access_count() when it does not access the key slot
1892 * anymore.
1893 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001894 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001895 * \param[in] attributes Key attributes for the new key.
Ronald Cron3c76a422020-10-16 12:17:04 +02001896 * \param[out] key On success, identifier of the key. Note that the
1897 * key identifier is also stored in the prepared
1898 * slot.
Gilles Peskine011e4282019-06-26 18:34:38 +02001899 * \param[out] p_slot On success, a pointer to the prepared slot.
1900 * \param[out] p_drv On any return, the driver for the key, if any.
1901 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001902 *
1903 * \retval #PSA_SUCCESS
1904 * The key slot is ready to receive key material.
1905 * \return If this function fails, the key slot is an invalid state.
1906 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001907 */
1908static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001909 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001910 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001911 mbedtls_svc_key_id_t *key,
Gilles Peskine011e4282019-06-26 18:34:38 +02001912 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001913 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001914{
1915 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001916 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001917 psa_key_slot_t *slot;
1918
Gilles Peskinedf179142019-07-15 22:02:14 +02001919 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001920 *p_drv = NULL;
1921
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001922 status = psa_validate_key_attributes( attributes, p_drv );
1923 if( status != PSA_SUCCESS )
1924 return( status );
1925
Ronald Cronc4d1b512020-07-31 11:26:37 +02001926 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001927 if( status != PSA_SUCCESS )
1928 return( status );
1929 slot = *p_slot;
1930
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001931 /* We're storing the declared bit-size of the key. It's up to each
1932 * creation mechanism to verify that this information is correct.
1933 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001934 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001935 * is optional (import, copy). In case of a volatile key, assign it the
1936 * volatile key identifier associated to the slot returned to contain its
1937 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001938
1939 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001940 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1941 {
1942#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1943 slot->attr.id = volatile_key_id;
1944#else
1945 slot->attr.id.key_id = volatile_key_id;
1946#endif
1947 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001948
Gilles Peskine91e8c332019-08-02 19:19:39 +02001949 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001950 * external-only flags, query `attributes`. Thanks to the check
1951 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001952 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001953 * may have set. */
1954 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001955
Gilles Peskinecbaff462019-07-12 23:46:04 +02001956#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001957 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001958 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001959 * create the key file in internal storage, create the
1960 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001961 * persistent data. This is done by starting a transaction that will
1962 * encompass these three actions.
1963 * For registering a volatile key, we just need to find an appropriate
1964 * slot number inside the SE. Since the key is designated volatile, creating
1965 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001966 /* The first thing to do is to find a slot number for the new key.
1967 * We save the slot number in persistent storage as part of the
1968 * transaction data. It will be needed to recover if the power
1969 * fails during the key creation process, to clean up on the secure
1970 * element side after restarting. Obtaining a slot number from the
1971 * secure element driver updates its persistent state, but we do not yet
1972 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001973 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001974 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001975 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001976 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001977 &slot->data.se.slot_number );
1978 if( status != PSA_SUCCESS )
1979 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001980
1981 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001982 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001983 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1984 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1985 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1986 psa_crypto_transaction.key.id = slot->attr.id;
1987 status = psa_crypto_save_transaction( );
1988 if( status != PSA_SUCCESS )
1989 {
1990 (void) psa_crypto_stop_transaction( );
1991 return( status );
1992 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001993 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001994 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001995
1996 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1997 {
1998 /* Key registration only makes sense with a secure element. */
1999 return( PSA_ERROR_INVALID_ARGUMENT );
2000 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002001#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2002
Ronald Croncf56a0a2020-08-04 09:51:30 +02002003 *key = slot->attr.id;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002004 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002005}
Gilles Peskine4747d192019-04-17 15:05:45 +02002006
2007/** Finalize the creation of a key once its key material has been set.
2008 *
2009 * This entails writing the key to persistent storage.
2010 *
2011 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002012 * See the documentation of psa_start_key_creation() for the intended use
2013 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002014 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002015 * \param[in,out] slot Pointer to the slot with key material.
2016 * \param[in] driver The secure element driver for the key,
2017 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002018 *
2019 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002020 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002021 * \return If this function fails, the key slot is an invalid state.
2022 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002023 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002024static psa_status_t psa_finish_key_creation(
2025 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002026 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002027{
2028 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002029 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002030 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002031
2032#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002033 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002034 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2036 if( driver != NULL )
2037 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002038 psa_se_key_data_storage_t data;
2039#if defined(static_assert)
2040 static_assert( sizeof( slot->data.se.slot_number ) ==
2041 sizeof( data.slot_number ),
2042 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002043#endif
2044 memcpy( &data.slot_number, &slot->data.se.slot_number,
2045 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002046 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002047 (uint8_t*) &data,
2048 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002049 }
2050 else
2051#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2052 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002053 /* Key material is saved in export representation in the slot, so
2054 * just pass the slot buffer for storage. */
2055 status = psa_save_persistent_key( &slot->attr,
2056 slot->data.key.data,
2057 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002058 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002059 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002060#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2061
Gilles Peskinecbaff462019-07-12 23:46:04 +02002062#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002063 /* Finish the transaction for a key creation. This does not
2064 * happen when registering an existing key. Detect this case
2065 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002066 * creation of a persistent key in a secure element requires a transaction,
2067 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002068 if( driver != NULL &&
2069 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002070 {
2071 status = psa_save_se_persistent_data( driver );
2072 if( status != PSA_SUCCESS )
2073 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002074 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002075 return( status );
2076 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002077 status = psa_crypto_stop_transaction( );
2078 if( status != PSA_SUCCESS )
2079 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002080 }
2081#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2082
Gilles Peskine4747d192019-04-17 15:05:45 +02002083 return( status );
2084}
2085
2086/** Abort the creation of a key.
2087 *
2088 * You may call this function after calling psa_start_key_creation(),
2089 * or after psa_finish_key_creation() fails. In other circumstances, this
2090 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002091 * See the documentation of psa_start_key_creation() for the intended use
2092 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002093 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002094 * \param[in,out] slot Pointer to the slot with key material.
2095 * \param[in] driver The secure element driver for the key,
2096 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002097 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002098static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002099 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002100{
Gilles Peskine011e4282019-06-26 18:34:38 +02002101 (void) driver;
2102
Gilles Peskine4747d192019-04-17 15:05:45 +02002103 if( slot == NULL )
2104 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002105
2106#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002107 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002108 * element, and the failure happened later (when saving metadata
2109 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002110 * element.
2111 * https://github.com/ARMmbed/mbed-crypto/issues/217
2112 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002113
Gilles Peskined7729582019-08-05 15:55:54 +02002114 /* Abort the ongoing transaction if any (there may not be one if
2115 * the creation process failed before starting one, or if the
2116 * key creation is a registration of a key in a secure element).
2117 * Earlier functions must already have done what it takes to undo any
2118 * partial creation. All that's left is to update the transaction data
2119 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002120 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002121#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2122
Gilles Peskine4747d192019-04-17 15:05:45 +02002123 psa_wipe_key_slot( slot );
2124}
2125
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002126/** Validate optional attributes during key creation.
2127 *
2128 * Some key attributes are optional during key creation. If they are
2129 * specified in the attributes structure, check that they are consistent
2130 * with the data in the slot.
2131 *
2132 * This function should be called near the end of key creation, after
2133 * the slot in memory is fully populated but before saving persistent data.
2134 */
2135static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002136 const psa_key_slot_t *slot,
2137 const psa_key_attributes_t *attributes )
2138{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002139 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002140 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002141 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002142 return( PSA_ERROR_INVALID_ARGUMENT );
2143 }
2144
2145 if( attributes->domain_parameters_size != 0 )
2146 {
2147#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002148 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002149 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002150 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002151 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002153
Steven Cooreman7f391872020-07-30 14:57:44 +02002154 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002155 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002156 slot->data.key.data,
2157 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002158 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002159 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002160 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002161
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002162 mbedtls_mpi_init( &actual );
2163 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002164 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002165 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002166 mbedtls_rsa_free( rsa );
2167 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002168 if( ret != 0 )
2169 goto rsa_exit;
2170 ret = mbedtls_mpi_read_binary( &required,
2171 attributes->domain_parameters,
2172 attributes->domain_parameters_size );
2173 if( ret != 0 )
2174 goto rsa_exit;
2175 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2176 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2177 rsa_exit:
2178 mbedtls_mpi_free( &actual );
2179 mbedtls_mpi_free( &required );
2180 if( ret != 0)
2181 return( mbedtls_to_psa_error( ret ) );
2182 }
2183 else
2184#endif
2185 {
2186 return( PSA_ERROR_INVALID_ARGUMENT );
2187 }
2188 }
2189
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002190 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002191 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002192 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002193 return( PSA_ERROR_INVALID_ARGUMENT );
2194 }
2195
2196 return( PSA_SUCCESS );
2197}
2198
Gilles Peskine4747d192019-04-17 15:05:45 +02002199psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002200 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002201 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002202 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002203{
2204 psa_status_t status;
2205 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002206 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002207
Gilles Peskine0f84d622019-09-12 19:03:13 +02002208 /* Reject zero-length symmetric keys (including raw data key objects).
2209 * This also rejects any key which might be encoded as an empty string,
2210 * which is never valid. */
2211 if( data_length == 0 )
2212 return( PSA_ERROR_INVALID_ARGUMENT );
2213
Gilles Peskinedf179142019-07-15 22:02:14 +02002214 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002215 key, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002216 if( status != PSA_SUCCESS )
2217 goto exit;
2218
Gilles Peskine5d309672019-07-12 23:47:28 +02002219#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2220 if( driver != NULL )
2221 {
2222 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002223 /* The driver should set the number of key bits, however in
2224 * case it doesn't, we initialize bits to an invalid value. */
2225 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002226 if( drv->key_management == NULL ||
2227 drv->key_management->p_import == NULL )
2228 {
2229 status = PSA_ERROR_NOT_SUPPORTED;
2230 goto exit;
2231 }
2232 status = drv->key_management->p_import(
2233 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002234 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002235 &bits );
2236 if( status != PSA_SUCCESS )
2237 goto exit;
2238 if( bits > PSA_MAX_KEY_BITS )
2239 {
2240 status = PSA_ERROR_NOT_SUPPORTED;
2241 goto exit;
2242 }
2243 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002244 }
2245 else
2246#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2247 {
2248 status = psa_import_key_into_slot( slot, data, data_length );
2249 if( status != PSA_SUCCESS )
2250 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002251 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002252 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002253 if( status != PSA_SUCCESS )
2254 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002255
Gilles Peskine011e4282019-06-26 18:34:38 +02002256 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002257exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002258 if( status != PSA_SUCCESS )
2259 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002260 psa_fail_key_creation( slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002261 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4747d192019-04-17 15:05:45 +02002262 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002263 else
2264 status = psa_decrement_key_slot_access_count( slot );
2265
Gilles Peskine4747d192019-04-17 15:05:45 +02002266 return( status );
2267}
2268
Gilles Peskined7729582019-08-05 15:55:54 +02002269#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2270psa_status_t mbedtls_psa_register_se_key(
2271 const psa_key_attributes_t *attributes )
2272{
2273 psa_status_t status;
2274 psa_key_slot_t *slot = NULL;
2275 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002276 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002277
2278 /* Leaving attributes unspecified is not currently supported.
2279 * It could make sense to query the key type and size from the
2280 * secure element, but not all secure elements support this
2281 * and the driver HAL doesn't currently support it. */
2282 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2283 return( PSA_ERROR_NOT_SUPPORTED );
2284 if( psa_get_key_bits( attributes ) == 0 )
2285 return( PSA_ERROR_NOT_SUPPORTED );
2286
2287 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002288 &key, &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002289 if( status != PSA_SUCCESS )
2290 goto exit;
2291
Gilles Peskined7729582019-08-05 15:55:54 +02002292 status = psa_finish_key_creation( slot, driver );
2293
2294exit:
2295 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002296 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002297 else
2298 status = psa_decrement_key_slot_access_count( slot );
2299
Gilles Peskined7729582019-08-05 15:55:54 +02002300 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002301 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002302 return( status );
2303}
2304#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2305
Gilles Peskinef603c712019-01-19 13:40:11 +01002306static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002307 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002308{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002309 psa_status_t status = psa_copy_key_material_into_slot( target,
2310 source->data.key.data,
2311 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002312 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002313 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002314
Steven Cooreman398aee52020-10-13 14:35:45 +02002315 target->attr.type = source->attr.type;
2316 target->attr.bits = source->attr.bits;
2317
2318 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002319}
2320
Ronald Croncf56a0a2020-08-04 09:51:30 +02002321psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002322 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002323 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002324{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002325 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2326 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002327 psa_key_slot_t *source_slot = NULL;
2328 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002329 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002330 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002331
Ronald Croncf56a0a2020-08-04 09:51:30 +02002332 status = psa_get_transparent_key( source_key, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002333 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002334 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002335 goto exit;
2336
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002337 status = psa_validate_optional_attributes( source_slot,
2338 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002339 if( status != PSA_SUCCESS )
2340 goto exit;
2341
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002342 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002343 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002344 if( status != PSA_SUCCESS )
2345 goto exit;
2346
Gilles Peskinedf179142019-07-15 22:02:14 +02002347 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2348 &actual_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002349 target_key, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002350 if( status != PSA_SUCCESS )
2351 goto exit;
2352
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002353#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2354 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002355 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002356 /* Copying to a secure element is not implemented yet. */
2357 status = PSA_ERROR_NOT_SUPPORTED;
2358 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002359 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002360#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002361
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002362 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002363 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002364 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002365
Gilles Peskine011e4282019-06-26 18:34:38 +02002366 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002367exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002368 if( status != PSA_SUCCESS )
2369 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002370 psa_fail_key_creation( target_slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002371 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002372 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002373 else
2374 status = psa_decrement_key_slot_access_count( target_slot );
2375
2376 decrement_status = psa_decrement_key_slot_access_count( source_slot );
2377
2378 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002379}
2380
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002381
2382
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002383/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002384/* Message digests */
2385/****************************************************************/
2386
John Durkop0ea39e02020-10-13 19:58:20 -07002387#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002388static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002389{
2390 switch( alg )
2391 {
2392#if defined(MBEDTLS_MD2_C)
2393 case PSA_ALG_MD2:
2394 return( &mbedtls_md2_info );
2395#endif
2396#if defined(MBEDTLS_MD4_C)
2397 case PSA_ALG_MD4:
2398 return( &mbedtls_md4_info );
2399#endif
2400#if defined(MBEDTLS_MD5_C)
2401 case PSA_ALG_MD5:
2402 return( &mbedtls_md5_info );
2403#endif
2404#if defined(MBEDTLS_RIPEMD160_C)
2405 case PSA_ALG_RIPEMD160:
2406 return( &mbedtls_ripemd160_info );
2407#endif
2408#if defined(MBEDTLS_SHA1_C)
2409 case PSA_ALG_SHA_1:
2410 return( &mbedtls_sha1_info );
2411#endif
2412#if defined(MBEDTLS_SHA256_C)
2413 case PSA_ALG_SHA_224:
2414 return( &mbedtls_sha224_info );
2415 case PSA_ALG_SHA_256:
2416 return( &mbedtls_sha256_info );
2417#endif
2418#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002419#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002420 case PSA_ALG_SHA_384:
2421 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002422#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002423 case PSA_ALG_SHA_512:
2424 return( &mbedtls_sha512_info );
2425#endif
2426 default:
2427 return( NULL );
2428 }
2429}
John Durkop0ea39e02020-10-13 19:58:20 -07002430#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002431
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002432psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2433{
2434 switch( operation->alg )
2435 {
Gilles Peskine81736312018-06-26 15:04:31 +02002436 case 0:
2437 /* The object has (apparently) been initialized but it is not
2438 * in use. It's ok to call abort on such an object, and there's
2439 * nothing to do. */
2440 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002441#if defined(MBEDTLS_MD2_C)
2442 case PSA_ALG_MD2:
2443 mbedtls_md2_free( &operation->ctx.md2 );
2444 break;
2445#endif
2446#if defined(MBEDTLS_MD4_C)
2447 case PSA_ALG_MD4:
2448 mbedtls_md4_free( &operation->ctx.md4 );
2449 break;
2450#endif
2451#if defined(MBEDTLS_MD5_C)
2452 case PSA_ALG_MD5:
2453 mbedtls_md5_free( &operation->ctx.md5 );
2454 break;
2455#endif
2456#if defined(MBEDTLS_RIPEMD160_C)
2457 case PSA_ALG_RIPEMD160:
2458 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2459 break;
2460#endif
2461#if defined(MBEDTLS_SHA1_C)
2462 case PSA_ALG_SHA_1:
2463 mbedtls_sha1_free( &operation->ctx.sha1 );
2464 break;
2465#endif
2466#if defined(MBEDTLS_SHA256_C)
2467 case PSA_ALG_SHA_224:
2468 case PSA_ALG_SHA_256:
2469 mbedtls_sha256_free( &operation->ctx.sha256 );
2470 break;
2471#endif
2472#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002473#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002474 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002475#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002476 case PSA_ALG_SHA_512:
2477 mbedtls_sha512_free( &operation->ctx.sha512 );
2478 break;
2479#endif
2480 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002481 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002482 }
2483 operation->alg = 0;
2484 return( PSA_SUCCESS );
2485}
2486
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002487psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488 psa_algorithm_t alg )
2489{
Janos Follath24eed8d2019-11-22 13:21:35 +00002490 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002491
2492 /* A context must be freshly initialized before it can be set up. */
2493 if( operation->alg != 0 )
2494 {
2495 return( PSA_ERROR_BAD_STATE );
2496 }
2497
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002498 switch( alg )
2499 {
2500#if defined(MBEDTLS_MD2_C)
2501 case PSA_ALG_MD2:
2502 mbedtls_md2_init( &operation->ctx.md2 );
2503 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2504 break;
2505#endif
2506#if defined(MBEDTLS_MD4_C)
2507 case PSA_ALG_MD4:
2508 mbedtls_md4_init( &operation->ctx.md4 );
2509 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2510 break;
2511#endif
2512#if defined(MBEDTLS_MD5_C)
2513 case PSA_ALG_MD5:
2514 mbedtls_md5_init( &operation->ctx.md5 );
2515 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2516 break;
2517#endif
2518#if defined(MBEDTLS_RIPEMD160_C)
2519 case PSA_ALG_RIPEMD160:
2520 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2521 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2522 break;
2523#endif
2524#if defined(MBEDTLS_SHA1_C)
2525 case PSA_ALG_SHA_1:
2526 mbedtls_sha1_init( &operation->ctx.sha1 );
2527 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2528 break;
2529#endif
2530#if defined(MBEDTLS_SHA256_C)
2531 case PSA_ALG_SHA_224:
2532 mbedtls_sha256_init( &operation->ctx.sha256 );
2533 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2534 break;
2535 case PSA_ALG_SHA_256:
2536 mbedtls_sha256_init( &operation->ctx.sha256 );
2537 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2538 break;
2539#endif
2540#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002541#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002542 case PSA_ALG_SHA_384:
2543 mbedtls_sha512_init( &operation->ctx.sha512 );
2544 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2545 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002546#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002547 case PSA_ALG_SHA_512:
2548 mbedtls_sha512_init( &operation->ctx.sha512 );
2549 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2550 break;
2551#endif
2552 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002553 return( PSA_ALG_IS_HASH( alg ) ?
2554 PSA_ERROR_NOT_SUPPORTED :
2555 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002556 }
2557 if( ret == 0 )
2558 operation->alg = alg;
2559 else
2560 psa_hash_abort( operation );
2561 return( mbedtls_to_psa_error( ret ) );
2562}
2563
2564psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2565 const uint8_t *input,
2566 size_t input_length )
2567{
Janos Follath24eed8d2019-11-22 13:21:35 +00002568 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002569
2570 /* Don't require hash implementations to behave correctly on a
2571 * zero-length input, which may have an invalid pointer. */
2572 if( input_length == 0 )
2573 return( PSA_SUCCESS );
2574
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002575 switch( operation->alg )
2576 {
2577#if defined(MBEDTLS_MD2_C)
2578 case PSA_ALG_MD2:
2579 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2580 input, input_length );
2581 break;
2582#endif
2583#if defined(MBEDTLS_MD4_C)
2584 case PSA_ALG_MD4:
2585 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2586 input, input_length );
2587 break;
2588#endif
2589#if defined(MBEDTLS_MD5_C)
2590 case PSA_ALG_MD5:
2591 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2592 input, input_length );
2593 break;
2594#endif
2595#if defined(MBEDTLS_RIPEMD160_C)
2596 case PSA_ALG_RIPEMD160:
2597 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2598 input, input_length );
2599 break;
2600#endif
2601#if defined(MBEDTLS_SHA1_C)
2602 case PSA_ALG_SHA_1:
2603 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2604 input, input_length );
2605 break;
2606#endif
2607#if defined(MBEDTLS_SHA256_C)
2608 case PSA_ALG_SHA_224:
2609 case PSA_ALG_SHA_256:
2610 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2611 input, input_length );
2612 break;
2613#endif
2614#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002615#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002616 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002617#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002618 case PSA_ALG_SHA_512:
2619 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2620 input, input_length );
2621 break;
2622#endif
2623 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002624 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002625 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002626
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002627 if( ret != 0 )
2628 psa_hash_abort( operation );
2629 return( mbedtls_to_psa_error( ret ) );
2630}
2631
2632psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2633 uint8_t *hash,
2634 size_t hash_size,
2635 size_t *hash_length )
2636{
itayzafrir40835d42018-08-02 13:14:17 +03002637 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002638 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002639 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002640
2641 /* Fill the output buffer with something that isn't a valid hash
2642 * (barring an attack on the hash and deliberately-crafted input),
2643 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002644 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002645 /* If hash_size is 0 then hash may be NULL and then the
2646 * call to memset would have undefined behavior. */
2647 if( hash_size != 0 )
2648 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002649
2650 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002651 {
2652 status = PSA_ERROR_BUFFER_TOO_SMALL;
2653 goto exit;
2654 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002655
2656 switch( operation->alg )
2657 {
2658#if defined(MBEDTLS_MD2_C)
2659 case PSA_ALG_MD2:
2660 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2661 break;
2662#endif
2663#if defined(MBEDTLS_MD4_C)
2664 case PSA_ALG_MD4:
2665 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2666 break;
2667#endif
2668#if defined(MBEDTLS_MD5_C)
2669 case PSA_ALG_MD5:
2670 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2671 break;
2672#endif
2673#if defined(MBEDTLS_RIPEMD160_C)
2674 case PSA_ALG_RIPEMD160:
2675 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2676 break;
2677#endif
2678#if defined(MBEDTLS_SHA1_C)
2679 case PSA_ALG_SHA_1:
2680 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2681 break;
2682#endif
2683#if defined(MBEDTLS_SHA256_C)
2684 case PSA_ALG_SHA_224:
2685 case PSA_ALG_SHA_256:
2686 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2687 break;
2688#endif
2689#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002690#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002691 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002692#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002693 case PSA_ALG_SHA_512:
2694 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2695 break;
2696#endif
2697 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002698 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002699 }
itayzafrir40835d42018-08-02 13:14:17 +03002700 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002701
itayzafrir40835d42018-08-02 13:14:17 +03002702exit:
2703 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002704 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002705 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002706 return( psa_hash_abort( operation ) );
2707 }
2708 else
2709 {
2710 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002711 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002712 }
2713}
2714
Gilles Peskine2d277862018-06-18 15:41:12 +02002715psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2716 const uint8_t *hash,
2717 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002718{
2719 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2720 size_t actual_hash_length;
2721 psa_status_t status = psa_hash_finish( operation,
2722 actual_hash, sizeof( actual_hash ),
2723 &actual_hash_length );
2724 if( status != PSA_SUCCESS )
2725 return( status );
2726 if( actual_hash_length != hash_length )
2727 return( PSA_ERROR_INVALID_SIGNATURE );
2728 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2729 return( PSA_ERROR_INVALID_SIGNATURE );
2730 return( PSA_SUCCESS );
2731}
2732
Gilles Peskine0a749c82019-11-28 19:33:58 +01002733psa_status_t psa_hash_compute( psa_algorithm_t alg,
2734 const uint8_t *input, size_t input_length,
2735 uint8_t *hash, size_t hash_size,
2736 size_t *hash_length )
2737{
2738 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2739 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2740
2741 *hash_length = hash_size;
2742 status = psa_hash_setup( &operation, alg );
2743 if( status != PSA_SUCCESS )
2744 goto exit;
2745 status = psa_hash_update( &operation, input, input_length );
2746 if( status != PSA_SUCCESS )
2747 goto exit;
2748 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2749 if( status != PSA_SUCCESS )
2750 goto exit;
2751
2752exit:
2753 if( status == PSA_SUCCESS )
2754 status = psa_hash_abort( &operation );
2755 else
2756 psa_hash_abort( &operation );
2757 return( status );
2758}
2759
2760psa_status_t psa_hash_compare( psa_algorithm_t alg,
2761 const uint8_t *input, size_t input_length,
2762 const uint8_t *hash, size_t hash_length )
2763{
2764 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2765 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2766
2767 status = psa_hash_setup( &operation, alg );
2768 if( status != PSA_SUCCESS )
2769 goto exit;
2770 status = psa_hash_update( &operation, input, input_length );
2771 if( status != PSA_SUCCESS )
2772 goto exit;
2773 status = psa_hash_verify( &operation, hash, hash_length );
2774 if( status != PSA_SUCCESS )
2775 goto exit;
2776
2777exit:
2778 if( status == PSA_SUCCESS )
2779 status = psa_hash_abort( &operation );
2780 else
2781 psa_hash_abort( &operation );
2782 return( status );
2783}
2784
Gilles Peskineeb35d782019-01-22 17:56:16 +01002785psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2786 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002787{
2788 if( target_operation->alg != 0 )
2789 return( PSA_ERROR_BAD_STATE );
2790
2791 switch( source_operation->alg )
2792 {
2793 case 0:
2794 return( PSA_ERROR_BAD_STATE );
2795#if defined(MBEDTLS_MD2_C)
2796 case PSA_ALG_MD2:
2797 mbedtls_md2_clone( &target_operation->ctx.md2,
2798 &source_operation->ctx.md2 );
2799 break;
2800#endif
2801#if defined(MBEDTLS_MD4_C)
2802 case PSA_ALG_MD4:
2803 mbedtls_md4_clone( &target_operation->ctx.md4,
2804 &source_operation->ctx.md4 );
2805 break;
2806#endif
2807#if defined(MBEDTLS_MD5_C)
2808 case PSA_ALG_MD5:
2809 mbedtls_md5_clone( &target_operation->ctx.md5,
2810 &source_operation->ctx.md5 );
2811 break;
2812#endif
2813#if defined(MBEDTLS_RIPEMD160_C)
2814 case PSA_ALG_RIPEMD160:
2815 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2816 &source_operation->ctx.ripemd160 );
2817 break;
2818#endif
2819#if defined(MBEDTLS_SHA1_C)
2820 case PSA_ALG_SHA_1:
2821 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2822 &source_operation->ctx.sha1 );
2823 break;
2824#endif
2825#if defined(MBEDTLS_SHA256_C)
2826 case PSA_ALG_SHA_224:
2827 case PSA_ALG_SHA_256:
2828 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2829 &source_operation->ctx.sha256 );
2830 break;
2831#endif
2832#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002833#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002834 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002835#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002836 case PSA_ALG_SHA_512:
2837 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2838 &source_operation->ctx.sha512 );
2839 break;
2840#endif
2841 default:
2842 return( PSA_ERROR_NOT_SUPPORTED );
2843 }
2844
2845 target_operation->alg = source_operation->alg;
2846 return( PSA_SUCCESS );
2847}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002848
2849
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002850/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002851/* MAC */
2852/****************************************************************/
2853
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002854static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002855 psa_algorithm_t alg,
2856 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002857 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002858 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002859{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002860 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002861 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002862
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002863 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002864 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002865
Gilles Peskine8c9def32018-02-08 10:02:12 +01002866 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2867 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002868 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002869 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002870 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002871 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002872 mode = MBEDTLS_MODE_STREAM;
2873 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002874 case PSA_ALG_CTR:
2875 mode = MBEDTLS_MODE_CTR;
2876 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002877 case PSA_ALG_CFB:
2878 mode = MBEDTLS_MODE_CFB;
2879 break;
2880 case PSA_ALG_OFB:
2881 mode = MBEDTLS_MODE_OFB;
2882 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002883 case PSA_ALG_ECB_NO_PADDING:
2884 mode = MBEDTLS_MODE_ECB;
2885 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002886 case PSA_ALG_CBC_NO_PADDING:
2887 mode = MBEDTLS_MODE_CBC;
2888 break;
2889 case PSA_ALG_CBC_PKCS7:
2890 mode = MBEDTLS_MODE_CBC;
2891 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002892 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002893 mode = MBEDTLS_MODE_CCM;
2894 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002895 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002896 mode = MBEDTLS_MODE_GCM;
2897 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002898 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2899 mode = MBEDTLS_MODE_CHACHAPOLY;
2900 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002901 default:
2902 return( NULL );
2903 }
2904 }
2905 else if( alg == PSA_ALG_CMAC )
2906 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002907 else
2908 return( NULL );
2909
2910 switch( key_type )
2911 {
2912 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002913 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002914 break;
2915 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002916 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2917 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002918 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002919 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002920 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002921 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002922 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2923 * but two-key Triple-DES is functionally three-key Triple-DES
2924 * with K1=K3, so that's how we present it to mbedtls. */
2925 if( key_bits == 128 )
2926 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002927 break;
2928 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002929 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002930 break;
2931 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002932 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002933 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002934 case PSA_KEY_TYPE_CHACHA20:
2935 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2936 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002937 default:
2938 return( NULL );
2939 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002940 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002941 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002942
Jaeden Amero23bbb752018-06-26 14:16:54 +01002943 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2944 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002945}
2946
Gilles Peskinea05219c2018-11-16 16:02:56 +01002947#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002948static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002949{
Gilles Peskine2d277862018-06-18 15:41:12 +02002950 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002951 {
2952 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002953 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002954 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002955 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002956 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002957 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002958 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002959 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002960 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002961 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002962 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002963 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002964 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002965 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002966 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002967 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002968 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002969 return( 128 );
2970 default:
2971 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002972 }
2973}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002974#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002975
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002976/* Initialize the MAC operation structure. Once this function has been
2977 * called, psa_mac_abort can run and will do the right thing. */
2978static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2979 psa_algorithm_t alg )
2980{
2981 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2982
2983 operation->alg = alg;
2984 operation->key_set = 0;
2985 operation->iv_set = 0;
2986 operation->iv_required = 0;
2987 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002988 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002989
2990#if defined(MBEDTLS_CMAC_C)
2991 if( alg == PSA_ALG_CMAC )
2992 {
2993 operation->iv_required = 0;
2994 mbedtls_cipher_init( &operation->ctx.cmac );
2995 status = PSA_SUCCESS;
2996 }
2997 else
2998#endif /* MBEDTLS_CMAC_C */
2999#if defined(MBEDTLS_MD_C)
3000 if( PSA_ALG_IS_HMAC( operation->alg ) )
3001 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003002 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3003 operation->ctx.hmac.hash_ctx.alg = 0;
3004 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003005 }
3006 else
3007#endif /* MBEDTLS_MD_C */
3008 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003009 if( ! PSA_ALG_IS_MAC( alg ) )
3010 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003011 }
3012
3013 if( status != PSA_SUCCESS )
3014 memset( operation, 0, sizeof( *operation ) );
3015 return( status );
3016}
3017
Gilles Peskine01126fa2018-07-12 17:04:55 +02003018#if defined(MBEDTLS_MD_C)
3019static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3020{
Gilles Peskine3f108122018-12-07 18:14:53 +01003021 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003022 return( psa_hash_abort( &hmac->hash_ctx ) );
3023}
3024#endif /* MBEDTLS_MD_C */
3025
Gilles Peskine8c9def32018-02-08 10:02:12 +01003026psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3027{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003028 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003029 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003030 /* The object has (apparently) been initialized but it is not
3031 * in use. It's ok to call abort on such an object, and there's
3032 * nothing to do. */
3033 return( PSA_SUCCESS );
3034 }
3035 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003036#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003037 if( operation->alg == PSA_ALG_CMAC )
3038 {
3039 mbedtls_cipher_free( &operation->ctx.cmac );
3040 }
3041 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003042#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003044 if( PSA_ALG_IS_HMAC( operation->alg ) )
3045 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003046 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003047 }
3048 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003049#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003050 {
3051 /* Sanity check (shouldn't happen: operation->alg should
3052 * always have been initialized to a valid value). */
3053 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003054 }
Moran Peker41deec42018-04-04 15:43:05 +03003055
Gilles Peskine8c9def32018-02-08 10:02:12 +01003056 operation->alg = 0;
3057 operation->key_set = 0;
3058 operation->iv_set = 0;
3059 operation->iv_required = 0;
3060 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003061 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003062
Gilles Peskine8c9def32018-02-08 10:02:12 +01003063 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003064
3065bad_state:
3066 /* If abort is called on an uninitialized object, we can't trust
3067 * anything. Wipe the object in case it contains confidential data.
3068 * This may result in a memory leak if a pointer gets overwritten,
3069 * but it's too late to do anything about this. */
3070 memset( operation, 0, sizeof( *operation ) );
3071 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003072}
3073
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003074#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003075static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003076 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003077 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003078 const mbedtls_cipher_info_t *cipher_info )
3079{
Janos Follath24eed8d2019-11-22 13:21:35 +00003080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003081
3082 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003083
3084 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3085 if( ret != 0 )
3086 return( ret );
3087
3088 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003089 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003090 key_bits );
3091 return( ret );
3092}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003093#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003094
Gilles Peskine248051a2018-06-20 16:09:38 +02003095#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003096static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3097 const uint8_t *key,
3098 size_t key_length,
3099 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003100{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003101 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003102 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003103 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003104 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003105 psa_status_t status;
3106
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003107 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3108 * overflow below. This should never trigger if the hash algorithm
3109 * is implemented correctly. */
3110 /* The size checks against the ipad and opad buffers cannot be written
3111 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3112 * because that triggers -Wlogical-op on GCC 7.3. */
3113 if( block_size > sizeof( ipad ) )
3114 return( PSA_ERROR_NOT_SUPPORTED );
3115 if( block_size > sizeof( hmac->opad ) )
3116 return( PSA_ERROR_NOT_SUPPORTED );
3117 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003118 return( PSA_ERROR_NOT_SUPPORTED );
3119
Gilles Peskined223b522018-06-11 18:12:58 +02003120 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003121 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003122 status = psa_hash_compute( hash_alg, key, key_length,
3123 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003124 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003125 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003126 }
Gilles Peskine96889972018-07-12 17:07:03 +02003127 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3128 * but it is permitted. It is common when HMAC is used in HKDF, for
3129 * example. Don't call `memcpy` in the 0-length because `key` could be
3130 * an invalid pointer which would make the behavior undefined. */
3131 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003132 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003133
Gilles Peskined223b522018-06-11 18:12:58 +02003134 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3135 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003136 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003137 ipad[i] ^= 0x36;
3138 memset( ipad + key_length, 0x36, block_size - key_length );
3139
3140 /* Copy the key material from ipad to opad, flipping the requisite bits,
3141 * and filling the rest of opad with the requisite constant. */
3142 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003143 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3144 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003145
Gilles Peskine01126fa2018-07-12 17:04:55 +02003146 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003147 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003148 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003149
Gilles Peskine01126fa2018-07-12 17:04:55 +02003150 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003151
3152cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003153 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003154
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003155 return( status );
3156}
Gilles Peskine248051a2018-06-20 16:09:38 +02003157#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003158
Gilles Peskine89167cb2018-07-08 20:12:23 +02003159static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003160 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003161 psa_algorithm_t alg,
3162 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003163{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003164 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3165 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003166 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003167 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003168 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003169 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003170 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003171 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003172
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003173 /* A context must be freshly initialized before it can be set up. */
3174 if( operation->alg != 0 )
3175 {
3176 return( PSA_ERROR_BAD_STATE );
3177 }
3178
Gilles Peskined911eb72018-08-14 15:18:45 +02003179 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003180 if( status != PSA_SUCCESS )
3181 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003182 if( is_sign )
3183 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003184
Ronald Croncf56a0a2020-08-04 09:51:30 +02003185 status = psa_get_transparent_key( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003186 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003187 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003188 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003189
Gilles Peskine8c9def32018-02-08 10:02:12 +01003190#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003191 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003192 {
3193 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003194 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003195 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003196 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003197 if( cipher_info == NULL )
3198 {
3199 status = PSA_ERROR_NOT_SUPPORTED;
3200 goto exit;
3201 }
3202 operation->mac_size = cipher_info->block_size;
3203 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3204 status = mbedtls_to_psa_error( ret );
3205 }
3206 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003207#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003208#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003209 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003210 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003211 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003212 if( hash_alg == 0 )
3213 {
3214 status = PSA_ERROR_NOT_SUPPORTED;
3215 goto exit;
3216 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003217
3218 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3219 /* Sanity check. This shouldn't fail on a valid configuration. */
3220 if( operation->mac_size == 0 ||
3221 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3222 {
3223 status = PSA_ERROR_NOT_SUPPORTED;
3224 goto exit;
3225 }
3226
Gilles Peskine8e338702019-07-30 20:06:31 +02003227 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003228 {
3229 status = PSA_ERROR_INVALID_ARGUMENT;
3230 goto exit;
3231 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003232
Gilles Peskine01126fa2018-07-12 17:04:55 +02003233 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003234 slot->data.key.data,
3235 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003236 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003237 }
3238 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003239#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003240 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003241 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003242 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003243 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003244
Gilles Peskined911eb72018-08-14 15:18:45 +02003245 if( truncated == 0 )
3246 {
3247 /* The "normal" case: untruncated algorithm. Nothing to do. */
3248 }
3249 else if( truncated < 4 )
3250 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003251 /* A very short MAC is too short for security since it can be
3252 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3253 * so we make this our minimum, even though 32 bits is still
3254 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003255 status = PSA_ERROR_NOT_SUPPORTED;
3256 }
3257 else if( truncated > operation->mac_size )
3258 {
3259 /* It's impossible to "truncate" to a larger length. */
3260 status = PSA_ERROR_INVALID_ARGUMENT;
3261 }
3262 else
3263 operation->mac_size = truncated;
3264
Gilles Peskinefbfac682018-07-08 20:51:54 +02003265exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003266 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003267 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003268 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003269 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003270 else
3271 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003272 operation->key_set = 1;
3273 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003274
3275 decrement_status = psa_decrement_key_slot_access_count( slot );
3276
3277 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003278}
3279
Gilles Peskine89167cb2018-07-08 20:12:23 +02003280psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003281 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003282 psa_algorithm_t alg )
3283{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003284 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003285}
3286
3287psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003288 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003289 psa_algorithm_t alg )
3290{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003291 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003292}
3293
Gilles Peskine8c9def32018-02-08 10:02:12 +01003294psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3295 const uint8_t *input,
3296 size_t input_length )
3297{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003298 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003299 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003300 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003301 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003302 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003303 operation->has_input = 1;
3304
Gilles Peskine8c9def32018-02-08 10:02:12 +01003305#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003306 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003307 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003308 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3309 input, input_length );
3310 status = mbedtls_to_psa_error( ret );
3311 }
3312 else
3313#endif /* MBEDTLS_CMAC_C */
3314#if defined(MBEDTLS_MD_C)
3315 if( PSA_ALG_IS_HMAC( operation->alg ) )
3316 {
3317 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3318 input_length );
3319 }
3320 else
3321#endif /* MBEDTLS_MD_C */
3322 {
3323 /* This shouldn't happen if `operation` was initialized by
3324 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003325 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003326 }
3327
Gilles Peskinefbfac682018-07-08 20:51:54 +02003328 if( status != PSA_SUCCESS )
3329 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003330 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003331}
3332
Gilles Peskine01126fa2018-07-12 17:04:55 +02003333#if defined(MBEDTLS_MD_C)
3334static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3335 uint8_t *mac,
3336 size_t mac_size )
3337{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003338 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003339 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3340 size_t hash_size = 0;
3341 size_t block_size = psa_get_hash_block_size( hash_alg );
3342 psa_status_t status;
3343
Gilles Peskine01126fa2018-07-12 17:04:55 +02003344 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3345 if( status != PSA_SUCCESS )
3346 return( status );
3347 /* From here on, tmp needs to be wiped. */
3348
3349 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3350 if( status != PSA_SUCCESS )
3351 goto exit;
3352
3353 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3354 if( status != PSA_SUCCESS )
3355 goto exit;
3356
3357 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3358 if( status != PSA_SUCCESS )
3359 goto exit;
3360
Gilles Peskined911eb72018-08-14 15:18:45 +02003361 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3362 if( status != PSA_SUCCESS )
3363 goto exit;
3364
3365 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003366
3367exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003368 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003369 return( status );
3370}
3371#endif /* MBEDTLS_MD_C */
3372
mohammad16036df908f2018-04-02 08:34:15 -07003373static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003374 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003375 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003376{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003377 if( ! operation->key_set )
3378 return( PSA_ERROR_BAD_STATE );
3379 if( operation->iv_required && ! operation->iv_set )
3380 return( PSA_ERROR_BAD_STATE );
3381
Gilles Peskine8c9def32018-02-08 10:02:12 +01003382 if( mac_size < operation->mac_size )
3383 return( PSA_ERROR_BUFFER_TOO_SMALL );
3384
Gilles Peskine8c9def32018-02-08 10:02:12 +01003385#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003386 if( operation->alg == PSA_ALG_CMAC )
3387 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003388 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3389 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3390 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003391 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003392 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003393 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003394 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003395 else
3396#endif /* MBEDTLS_CMAC_C */
3397#if defined(MBEDTLS_MD_C)
3398 if( PSA_ALG_IS_HMAC( operation->alg ) )
3399 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003400 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003401 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003402 }
3403 else
3404#endif /* MBEDTLS_MD_C */
3405 {
3406 /* This shouldn't happen if `operation` was initialized by
3407 * a setup function. */
3408 return( PSA_ERROR_BAD_STATE );
3409 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003410}
3411
Gilles Peskineacd4be32018-07-08 19:56:25 +02003412psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3413 uint8_t *mac,
3414 size_t mac_size,
3415 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003416{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003417 psa_status_t status;
3418
Jaeden Amero252ef282019-02-15 14:05:35 +00003419 if( operation->alg == 0 )
3420 {
3421 return( PSA_ERROR_BAD_STATE );
3422 }
3423
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003424 /* Fill the output buffer with something that isn't a valid mac
3425 * (barring an attack on the mac and deliberately-crafted input),
3426 * in case the caller doesn't check the return status properly. */
3427 *mac_length = mac_size;
3428 /* If mac_size is 0 then mac may be NULL and then the
3429 * call to memset would have undefined behavior. */
3430 if( mac_size != 0 )
3431 memset( mac, '!', mac_size );
3432
Gilles Peskine89167cb2018-07-08 20:12:23 +02003433 if( ! operation->is_sign )
3434 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003435 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003436 }
mohammad16036df908f2018-04-02 08:34:15 -07003437
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003438 status = psa_mac_finish_internal( operation, mac, mac_size );
3439
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003440 if( status == PSA_SUCCESS )
3441 {
3442 status = psa_mac_abort( operation );
3443 if( status == PSA_SUCCESS )
3444 *mac_length = operation->mac_size;
3445 else
3446 memset( mac, '!', mac_size );
3447 }
3448 else
3449 psa_mac_abort( operation );
3450 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003451}
3452
Gilles Peskineacd4be32018-07-08 19:56:25 +02003453psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3454 const uint8_t *mac,
3455 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003456{
Gilles Peskine828ed142018-06-18 23:25:51 +02003457 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003458 psa_status_t status;
3459
Jaeden Amero252ef282019-02-15 14:05:35 +00003460 if( operation->alg == 0 )
3461 {
3462 return( PSA_ERROR_BAD_STATE );
3463 }
3464
Gilles Peskine89167cb2018-07-08 20:12:23 +02003465 if( operation->is_sign )
3466 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003467 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003468 }
3469 if( operation->mac_size != mac_length )
3470 {
3471 status = PSA_ERROR_INVALID_SIGNATURE;
3472 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003473 }
mohammad16036df908f2018-04-02 08:34:15 -07003474
3475 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003476 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003477 if( status != PSA_SUCCESS )
3478 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003479
3480 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3481 status = PSA_ERROR_INVALID_SIGNATURE;
3482
3483cleanup:
3484 if( status == PSA_SUCCESS )
3485 status = psa_mac_abort( operation );
3486 else
3487 psa_mac_abort( operation );
3488
Gilles Peskine3f108122018-12-07 18:14:53 +01003489 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003490
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003491 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003492}
3493
3494
Gilles Peskine20035e32018-02-03 22:44:14 +01003495
Gilles Peskine20035e32018-02-03 22:44:14 +01003496/****************************************************************/
3497/* Asymmetric cryptography */
3498/****************************************************************/
3499
Gilles Peskine2b450e32018-06-27 15:42:46 +02003500#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003501/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003502 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003503static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3504 size_t hash_length,
3505 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003506{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003507 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003508 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003509 *md_alg = mbedtls_md_get_type( md_info );
3510
3511 /* The Mbed TLS RSA module uses an unsigned int for hash length
3512 * parameters. Validate that it fits so that we don't risk an
3513 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003514#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003515 if( hash_length > UINT_MAX )
3516 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003517#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003518
3519#if defined(MBEDTLS_PKCS1_V15)
3520 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3521 * must be correct. */
3522 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3523 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003524 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003525 if( md_info == NULL )
3526 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003527 if( mbedtls_md_get_size( md_info ) != hash_length )
3528 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003529 }
3530#endif /* MBEDTLS_PKCS1_V15 */
3531
3532#if defined(MBEDTLS_PKCS1_V21)
3533 /* PSS requires a hash internally. */
3534 if( PSA_ALG_IS_RSA_PSS( alg ) )
3535 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003536 if( md_info == NULL )
3537 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003538 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003539#endif /* MBEDTLS_PKCS1_V21 */
3540
Gilles Peskine61b91d42018-06-08 16:09:36 +02003541 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003542}
3543
Gilles Peskine2b450e32018-06-27 15:42:46 +02003544static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3545 psa_algorithm_t alg,
3546 const uint8_t *hash,
3547 size_t hash_length,
3548 uint8_t *signature,
3549 size_t signature_size,
3550 size_t *signature_length )
3551{
3552 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003554 mbedtls_md_type_t md_alg;
3555
3556 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3557 if( status != PSA_SUCCESS )
3558 return( status );
3559
Gilles Peskine630a18a2018-06-29 17:49:35 +02003560 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003561 return( PSA_ERROR_BUFFER_TOO_SMALL );
3562
3563#if defined(MBEDTLS_PKCS1_V15)
3564 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3565 {
3566 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3567 MBEDTLS_MD_NONE );
3568 ret = mbedtls_rsa_pkcs1_sign( rsa,
3569 mbedtls_ctr_drbg_random,
3570 &global_data.ctr_drbg,
3571 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003572 md_alg,
3573 (unsigned int) hash_length,
3574 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003575 signature );
3576 }
3577 else
3578#endif /* MBEDTLS_PKCS1_V15 */
3579#if defined(MBEDTLS_PKCS1_V21)
3580 if( PSA_ALG_IS_RSA_PSS( alg ) )
3581 {
3582 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3583 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3584 mbedtls_ctr_drbg_random,
3585 &global_data.ctr_drbg,
3586 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003587 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003588 (unsigned int) hash_length,
3589 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003590 signature );
3591 }
3592 else
3593#endif /* MBEDTLS_PKCS1_V21 */
3594 {
3595 return( PSA_ERROR_INVALID_ARGUMENT );
3596 }
3597
3598 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003599 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003600 return( mbedtls_to_psa_error( ret ) );
3601}
3602
3603static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3604 psa_algorithm_t alg,
3605 const uint8_t *hash,
3606 size_t hash_length,
3607 const uint8_t *signature,
3608 size_t signature_length )
3609{
3610 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003611 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003612 mbedtls_md_type_t md_alg;
3613
3614 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3615 if( status != PSA_SUCCESS )
3616 return( status );
3617
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003618 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3619 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003620
3621#if defined(MBEDTLS_PKCS1_V15)
3622 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3623 {
3624 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3625 MBEDTLS_MD_NONE );
3626 ret = mbedtls_rsa_pkcs1_verify( rsa,
3627 mbedtls_ctr_drbg_random,
3628 &global_data.ctr_drbg,
3629 MBEDTLS_RSA_PUBLIC,
3630 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003631 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003632 hash,
3633 signature );
3634 }
3635 else
3636#endif /* MBEDTLS_PKCS1_V15 */
3637#if defined(MBEDTLS_PKCS1_V21)
3638 if( PSA_ALG_IS_RSA_PSS( alg ) )
3639 {
3640 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3641 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3642 mbedtls_ctr_drbg_random,
3643 &global_data.ctr_drbg,
3644 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003645 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003646 (unsigned int) hash_length,
3647 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003648 signature );
3649 }
3650 else
3651#endif /* MBEDTLS_PKCS1_V21 */
3652 {
3653 return( PSA_ERROR_INVALID_ARGUMENT );
3654 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003655
3656 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3657 * the rest of the signature is invalid". This has little use in
3658 * practice and PSA doesn't report this distinction. */
3659 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3660 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003661 return( mbedtls_to_psa_error( ret ) );
3662}
3663#endif /* MBEDTLS_RSA_C */
3664
John Durkopf87e3ae2020-10-26 15:25:23 -07003665#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003666/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3667 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3668 * (even though these functions don't modify it). */
3669static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3670 psa_algorithm_t alg,
3671 const uint8_t *hash,
3672 size_t hash_length,
3673 uint8_t *signature,
3674 size_t signature_size,
3675 size_t *signature_length )
3676{
Janos Follath24eed8d2019-11-22 13:21:35 +00003677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003678 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003679 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003680 mbedtls_mpi_init( &r );
3681 mbedtls_mpi_init( &s );
3682
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003683 if( signature_size < 2 * curve_bytes )
3684 {
3685 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3686 goto cleanup;
3687 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003688
John Durkop0ea39e02020-10-13 19:58:20 -07003689#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003690 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3691 {
3692 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3693 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3694 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003695 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3696 &ecp->d, hash,
3697 hash_length, md_alg,
3698 mbedtls_ctr_drbg_random,
3699 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003700 }
3701 else
John Durkop0ea39e02020-10-13 19:58:20 -07003702#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003703 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003704 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003705 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3706 hash, hash_length,
3707 mbedtls_ctr_drbg_random,
3708 &global_data.ctr_drbg ) );
3709 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003710
3711 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3712 signature,
3713 curve_bytes ) );
3714 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3715 signature + curve_bytes,
3716 curve_bytes ) );
3717
3718cleanup:
3719 mbedtls_mpi_free( &r );
3720 mbedtls_mpi_free( &s );
3721 if( ret == 0 )
3722 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003723 return( mbedtls_to_psa_error( ret ) );
3724}
3725
3726static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3727 const uint8_t *hash,
3728 size_t hash_length,
3729 const uint8_t *signature,
3730 size_t signature_length )
3731{
Janos Follath24eed8d2019-11-22 13:21:35 +00003732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003733 mbedtls_mpi r, s;
3734 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3735 mbedtls_mpi_init( &r );
3736 mbedtls_mpi_init( &s );
3737
3738 if( signature_length != 2 * curve_bytes )
3739 return( PSA_ERROR_INVALID_SIGNATURE );
3740
3741 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3742 signature,
3743 curve_bytes ) );
3744 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3745 signature + curve_bytes,
3746 curve_bytes ) );
3747
Steven Cooremanacda8342020-07-24 23:09:52 +02003748 /* Check whether the public part is loaded. If not, load it. */
3749 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3750 {
3751 MBEDTLS_MPI_CHK(
3752 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3753 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3754 }
3755
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003756 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3757 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003758
3759cleanup:
3760 mbedtls_mpi_free( &r );
3761 mbedtls_mpi_free( &s );
3762 return( mbedtls_to_psa_error( ret ) );
3763}
John Durkopf87e3ae2020-10-26 15:25:23 -07003764#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003765
Ronald Croncf56a0a2020-08-04 09:51:30 +02003766psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003767 psa_algorithm_t alg,
3768 const uint8_t *hash,
3769 size_t hash_length,
3770 uint8_t *signature,
3771 size_t signature_size,
3772 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003773{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003774 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3775 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003776 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003777
3778 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003779 /* Immediately reject a zero-length signature buffer. This guarantees
3780 * that signature must be a valid pointer. (On the other hand, the hash
3781 * buffer can in principle be empty since it doesn't actually have
3782 * to be a hash.) */
3783 if( signature_size == 0 )
3784 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003785
Ronald Croncf56a0a2020-08-04 09:51:30 +02003786 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003787 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003788 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003789 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003790 {
3791 status = PSA_ERROR_INVALID_ARGUMENT;
3792 goto exit;
3793 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003794
Steven Cooremancd84cb42020-07-16 20:28:36 +02003795 /* Try any of the available accelerators first */
3796 status = psa_driver_wrapper_sign_hash( slot,
3797 alg,
3798 hash,
3799 hash_length,
3800 signature,
3801 signature_size,
3802 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003803 if( status != PSA_ERROR_NOT_SUPPORTED ||
3804 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003805 goto exit;
3806
Steven Cooreman7a250572020-07-17 16:43:05 +02003807 /* If the operation was not supported by any accelerator, try fallback. */
Gilles Peskine20035e32018-02-03 22:44:14 +01003808#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003809 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003810 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003811 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003812
Steven Cooreman4fed4552020-08-03 14:46:03 +02003813 status = psa_load_rsa_representation( slot->attr.type,
3814 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003815 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003816 &rsa );
3817 if( status != PSA_SUCCESS )
3818 goto exit;
3819
Steven Cooremana2371e52020-07-28 14:30:39 +02003820 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003821 alg,
3822 hash, hash_length,
3823 signature, signature_size,
3824 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003825
Steven Cooremana2371e52020-07-28 14:30:39 +02003826 mbedtls_rsa_free( rsa );
3827 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003828 }
3829 else
3830#endif /* defined(MBEDTLS_RSA_C) */
3831#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003832 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003833 {
John Durkop0ea39e02020-10-13 19:58:20 -07003834#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003835 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003836#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003837 PSA_ALG_IS_ECDSA( alg )
3838#else
3839 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3840#endif
3841 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003842 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003843 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003844 status = psa_load_ecp_representation( slot->attr.type,
3845 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003846 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003847 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003848 if( status != PSA_SUCCESS )
3849 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003850 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003851 alg,
3852 hash, hash_length,
3853 signature, signature_size,
3854 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003855 mbedtls_ecp_keypair_free( ecp );
3856 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003857 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003858 else
John Durkop0ea39e02020-10-13 19:58:20 -07003859#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003860 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003861 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003862 }
itayzafrir5c753392018-05-08 11:18:38 +03003863 }
3864 else
3865#endif /* defined(MBEDTLS_ECP_C) */
3866 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003867 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003868 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003869
3870exit:
3871 /* Fill the unused part of the output buffer (the whole buffer on error,
3872 * the trailing part on success) with something that isn't a valid mac
3873 * (barring an attack on the mac and deliberately-crafted input),
3874 * in case the caller doesn't check the return status properly. */
3875 if( status == PSA_SUCCESS )
3876 memset( signature + *signature_length, '!',
3877 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003878 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003879 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003880 /* If signature_size is 0 then we have nothing to do. We must not call
3881 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003882
3883 decrement_status = psa_decrement_key_slot_access_count( slot );
3884
3885 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003886}
3887
Ronald Croncf56a0a2020-08-04 09:51:30 +02003888psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003889 psa_algorithm_t alg,
3890 const uint8_t *hash,
3891 size_t hash_length,
3892 const uint8_t *signature,
3893 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003894{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003895 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3896 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003897 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003898
Ronald Croncf56a0a2020-08-04 09:51:30 +02003899 status = psa_get_key_from_slot( key, &slot,
3900 PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003901 if( status != PSA_SUCCESS )
3902 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003903
Steven Cooreman55ae2172020-07-17 19:46:15 +02003904 /* Try any of the available accelerators first */
3905 status = psa_driver_wrapper_verify_hash( slot,
3906 alg,
3907 hash,
3908 hash_length,
3909 signature,
3910 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003911 if( status != PSA_ERROR_NOT_SUPPORTED ||
3912 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003913 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003914
Gilles Peskine61b91d42018-06-08 16:09:36 +02003915#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003916 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003917 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003918 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003919
Steven Cooreman4fed4552020-08-03 14:46:03 +02003920 status = psa_load_rsa_representation( slot->attr.type,
3921 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003922 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003923 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003924 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003925 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003926
Steven Cooremana2371e52020-07-28 14:30:39 +02003927 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003928 alg,
3929 hash, hash_length,
3930 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003931 mbedtls_rsa_free( rsa );
3932 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003933 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003934 }
3935 else
3936#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003937#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003938 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003939 {
John Durkopf87e3ae2020-10-26 15:25:23 -07003940#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003941 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003942 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003943 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003944 status = psa_load_ecp_representation( slot->attr.type,
3945 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003946 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003947 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003948 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003949 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003950 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003951 hash, hash_length,
3952 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003953 mbedtls_ecp_keypair_free( ecp );
3954 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003955 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003956 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003957 else
John Durkopf87e3ae2020-10-26 15:25:23 -07003958#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003959 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003960 status = PSA_ERROR_INVALID_ARGUMENT;
3961 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003962 }
itayzafrir5c753392018-05-08 11:18:38 +03003963 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003964 else
3965#endif /* defined(MBEDTLS_ECP_C) */
3966 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003967 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003968 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003969
3970exit:
3971 decrement_status = psa_decrement_key_slot_access_count( slot );
3972
3973 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003974}
3975
Gilles Peskine072ac562018-06-30 00:21:29 +02003976#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3977static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3978 mbedtls_rsa_context *rsa )
3979{
3980 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3981 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3982 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3983 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3984}
3985#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3986
Ronald Croncf56a0a2020-08-04 09:51:30 +02003987psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003988 psa_algorithm_t alg,
3989 const uint8_t *input,
3990 size_t input_length,
3991 const uint8_t *salt,
3992 size_t salt_length,
3993 uint8_t *output,
3994 size_t output_size,
3995 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003996{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003997 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3998 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003999 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004000
Darryl Green5cc689a2018-07-24 15:34:10 +01004001 (void) input;
4002 (void) input_length;
4003 (void) salt;
4004 (void) output;
4005 (void) output_size;
4006
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004007 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004008
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004009 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4010 return( PSA_ERROR_INVALID_ARGUMENT );
4011
Ronald Croncf56a0a2020-08-04 09:51:30 +02004012 status = psa_get_transparent_key( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004013 if( status != PSA_SUCCESS )
4014 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004015 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4016 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004017 {
4018 status = PSA_ERROR_INVALID_ARGUMENT;
4019 goto exit;
4020 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004021
4022#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004023 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004024 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004025 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004026 status = psa_load_rsa_representation( slot->attr.type,
4027 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004028 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004029 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004030 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004031 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004032
4033 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004034 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004035 status = PSA_ERROR_BUFFER_TOO_SMALL;
4036 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004037 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004038#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004039 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004040 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004041 status = mbedtls_to_psa_error(
4042 mbedtls_rsa_pkcs1_encrypt( rsa,
4043 mbedtls_ctr_drbg_random,
4044 &global_data.ctr_drbg,
4045 MBEDTLS_RSA_PUBLIC,
4046 input_length,
4047 input,
4048 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004049 }
4050 else
4051#endif /* MBEDTLS_PKCS1_V15 */
4052#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004053 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004054 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004055 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004056 status = mbedtls_to_psa_error(
4057 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
4058 mbedtls_ctr_drbg_random,
4059 &global_data.ctr_drbg,
4060 MBEDTLS_RSA_PUBLIC,
4061 salt, salt_length,
4062 input_length,
4063 input,
4064 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004065 }
4066 else
4067#endif /* MBEDTLS_PKCS1_V21 */
4068 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004069 status = PSA_ERROR_INVALID_ARGUMENT;
4070 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004071 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004072rsa_exit:
4073 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004074 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004075
Steven Cooremana2371e52020-07-28 14:30:39 +02004076 mbedtls_rsa_free( rsa );
4077 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004078 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004079 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004080#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004081 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004082 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004083 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004084
4085exit:
4086 decrement_status = psa_decrement_key_slot_access_count( slot );
4087
4088 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004089}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004090
Ronald Croncf56a0a2020-08-04 09:51:30 +02004091psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004092 psa_algorithm_t alg,
4093 const uint8_t *input,
4094 size_t input_length,
4095 const uint8_t *salt,
4096 size_t salt_length,
4097 uint8_t *output,
4098 size_t output_size,
4099 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004100{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004101 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4102 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004103 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004104
Darryl Green5cc689a2018-07-24 15:34:10 +01004105 (void) input;
4106 (void) input_length;
4107 (void) salt;
4108 (void) output;
4109 (void) output_size;
4110
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004111 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004112
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004113 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4114 return( PSA_ERROR_INVALID_ARGUMENT );
4115
Ronald Croncf56a0a2020-08-04 09:51:30 +02004116 status = psa_get_transparent_key( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004117 if( status != PSA_SUCCESS )
4118 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004119 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004120 {
4121 status = PSA_ERROR_INVALID_ARGUMENT;
4122 goto exit;
4123 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004124
4125#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004126 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004127 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004128 mbedtls_rsa_context *rsa = NULL;
4129 status = psa_load_rsa_representation( slot->attr.type,
4130 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004131 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004132 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004133 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004134 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004135
Steven Cooremana2371e52020-07-28 14:30:39 +02004136 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004137 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004138 status = PSA_ERROR_INVALID_ARGUMENT;
4139 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004140 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004141
4142#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004143 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004144 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004145 status = mbedtls_to_psa_error(
4146 mbedtls_rsa_pkcs1_decrypt( rsa,
4147 mbedtls_ctr_drbg_random,
4148 &global_data.ctr_drbg,
4149 MBEDTLS_RSA_PRIVATE,
4150 output_length,
4151 input,
4152 output,
4153 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004154 }
4155 else
4156#endif /* MBEDTLS_PKCS1_V15 */
4157#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004158 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004159 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004160 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004161 status = mbedtls_to_psa_error(
4162 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4163 mbedtls_ctr_drbg_random,
4164 &global_data.ctr_drbg,
4165 MBEDTLS_RSA_PRIVATE,
4166 salt, salt_length,
4167 output_length,
4168 input,
4169 output,
4170 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004171 }
4172 else
4173#endif /* MBEDTLS_PKCS1_V21 */
4174 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004175 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004176 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004177
Steven Cooreman4fed4552020-08-03 14:46:03 +02004178rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004179 mbedtls_rsa_free( rsa );
4180 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004181 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004182 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004183#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004184 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004185 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004186 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004187
4188exit:
4189 decrement_status = psa_decrement_key_slot_access_count( slot );
4190
4191 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004192}
Gilles Peskine20035e32018-02-03 22:44:14 +01004193
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004194
4195
mohammad1603503973b2018-03-12 15:59:30 +02004196/****************************************************************/
4197/* Symmetric cryptography */
4198/****************************************************************/
4199
Gilles Peskinee553c652018-06-04 16:22:46 +02004200static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004201 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004202 psa_algorithm_t alg,
4203 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004204{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004205 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4206 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004207 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004208 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004209 size_t key_bits;
4210 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004211 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4212 PSA_KEY_USAGE_ENCRYPT :
4213 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004214
Steven Cooremand3feccd2020-09-01 15:56:14 +02004215 /* A context must be freshly initialized before it can be set up. */
4216 if( operation->alg != 0 )
4217 return( PSA_ERROR_BAD_STATE );
4218
Steven Cooremana07b9972020-09-10 14:54:14 +02004219 /* The requested algorithm must be one that can be processed by cipher. */
4220 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004221 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004222
Steven Cooremanef8575e2020-09-11 11:44:50 +02004223 /* Fetch key material from key storage. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02004224 status = psa_get_key_from_slot( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004225 if( status != PSA_SUCCESS )
4226 goto exit;
4227
4228 /* Initialize the operation struct members, except for alg. The alg member
4229 * is used to indicate to psa_cipher_abort that there are resources to free,
4230 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004231 operation->key_set = 0;
4232 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004233 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004234 operation->iv_size = 0;
4235 operation->block_size = 0;
4236 if( alg == PSA_ALG_ECB_NO_PADDING )
4237 operation->iv_required = 0;
4238 else
4239 operation->iv_required = 1;
4240
Steven Cooremana07b9972020-09-10 14:54:14 +02004241 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004242 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004243 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004244 slot,
4245 alg );
4246 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004247 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004248 slot,
4249 alg );
4250
Steven Cooremanef8575e2020-09-11 11:44:50 +02004251 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004252 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004253 /* Once the driver context is initialised, it needs to be freed using
4254 * psa_cipher_abort. Indicate this through setting alg. */
4255 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004256 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004257
Steven Cooremand3feccd2020-09-01 15:56:14 +02004258 if( status != PSA_ERROR_NOT_SUPPORTED ||
4259 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4260 goto exit;
4261
Steven Cooremana07b9972020-09-10 14:54:14 +02004262 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004263 * available for the given algorithm & key. */
4264 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004265
Steven Cooremana07b9972020-09-10 14:54:14 +02004266 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004267 * psa_cipher_abort. Indicate there is something to be freed through setting
4268 * alg, and indicate the operation is being done using mbedtls crypto through
4269 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004270 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004271 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004272
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004273 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004274 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004275 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004276 {
4277 status = PSA_ERROR_NOT_SUPPORTED;
4278 goto exit;
4279 }
mohammad1603503973b2018-03-12 15:59:30 +02004280
mohammad1603503973b2018-03-12 15:59:30 +02004281 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004282 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004283 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004284
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004285#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004286 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004287 {
4288 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004289 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004290 memcpy( keys, slot->data.key.data, 16 );
4291 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004292 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4293 keys,
4294 192, cipher_operation );
4295 }
4296 else
4297#endif
4298 {
4299 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004300 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004301 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004302 }
Moran Peker41deec42018-04-04 15:43:05 +03004303 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004304 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004305
mohammad16038481e742018-03-18 13:57:31 +02004306#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004307 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004308 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004309 case PSA_ALG_CBC_NO_PADDING:
4310 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4311 MBEDTLS_PADDING_NONE );
4312 break;
4313 case PSA_ALG_CBC_PKCS7:
4314 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4315 MBEDTLS_PADDING_PKCS7 );
4316 break;
4317 default:
4318 /* The algorithm doesn't involve padding. */
4319 ret = 0;
4320 break;
4321 }
4322 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004323 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004324#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4325
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004326 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004327 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004328 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4329 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004330 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004331 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004332 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004333#if defined(MBEDTLS_CHACHA20_C)
4334 else
4335 if( alg == PSA_ALG_CHACHA20 )
4336 operation->iv_size = 12;
4337#endif
mohammad1603503973b2018-03-12 15:59:30 +02004338
Steven Cooreman7df02922020-09-09 15:28:49 +02004339 status = PSA_SUCCESS;
4340
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004341exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004342 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004343 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004344 if( status == PSA_SUCCESS )
4345 {
4346 /* Update operation flags for both driver and software implementations */
4347 operation->key_set = 1;
4348 }
4349 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004350 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004351
4352 decrement_status = psa_decrement_key_slot_access_count( slot );
4353
4354 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004355}
4356
Gilles Peskinefe119512018-07-08 21:39:34 +02004357psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004358 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004359 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004360{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004361 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004362}
4363
Gilles Peskinefe119512018-07-08 21:39:34 +02004364psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004365 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004366 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004367{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004368 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004369}
4370
Gilles Peskinefe119512018-07-08 21:39:34 +02004371psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004372 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004373 size_t iv_size,
4374 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004375{
itayzafrir534bd7c2018-08-02 13:56:32 +03004376 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004378 if( operation->iv_set || ! operation->iv_required )
4379 {
4380 return( PSA_ERROR_BAD_STATE );
4381 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004382
Steven Cooremanef8575e2020-09-11 11:44:50 +02004383 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004384 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004385 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004386 iv,
4387 iv_size,
4388 iv_length );
4389 goto exit;
4390 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004391
Moran Peker41deec42018-04-04 15:43:05 +03004392 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004393 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004394 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004395 goto exit;
4396 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004397 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4398 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004399 if( ret != 0 )
4400 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004401 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004402 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004403 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004404
mohammad16038481e742018-03-18 13:57:31 +02004405 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004406 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004407
Moran Peker395db872018-05-31 14:07:14 +03004408exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004409 if( status == PSA_SUCCESS )
4410 operation->iv_set = 1;
4411 else
Moran Peker395db872018-05-31 14:07:14 +03004412 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004413 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004414}
4415
Gilles Peskinefe119512018-07-08 21:39:34 +02004416psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004417 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004418 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004419{
itayzafrir534bd7c2018-08-02 13:56:32 +03004420 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004421 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004422 if( operation->iv_set || ! operation->iv_required )
4423 {
4424 return( PSA_ERROR_BAD_STATE );
4425 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004426
Steven Cooremanef8575e2020-09-11 11:44:50 +02004427 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004428 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004429 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004430 iv,
4431 iv_length );
4432 goto exit;
4433 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004434
Moran Pekera28258c2018-05-29 16:25:04 +03004435 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004436 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004437 status = PSA_ERROR_INVALID_ARGUMENT;
4438 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004439 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004440 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4441 status = mbedtls_to_psa_error( ret );
4442exit:
4443 if( status == PSA_SUCCESS )
4444 operation->iv_set = 1;
4445 else
mohammad1603503973b2018-03-12 15:59:30 +02004446 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004447 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004448}
4449
Steven Cooreman177deba2020-09-07 17:14:14 +02004450/* Process input for which the algorithm is set to ECB mode. This requires
4451 * manual processing, since the PSA API is defined as being able to process
4452 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4453 * underlying mbedtls_cipher_update only takes full blocks. */
4454static psa_status_t psa_cipher_update_ecb_internal(
4455 mbedtls_cipher_context_t *ctx,
4456 const uint8_t *input,
4457 size_t input_length,
4458 uint8_t *output,
4459 size_t output_size,
4460 size_t *output_length )
4461{
4462 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4463 size_t block_size = ctx->cipher_info->block_size;
4464 size_t internal_output_length = 0;
4465 *output_length = 0;
4466
4467 if( input_length == 0 )
4468 {
4469 status = PSA_SUCCESS;
4470 goto exit;
4471 }
4472
4473 if( ctx->unprocessed_len > 0 )
4474 {
4475 /* Fill up to block size, and run the block if there's a full one. */
4476 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4477
4478 if( input_length < bytes_to_copy )
4479 bytes_to_copy = input_length;
4480
4481 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4482 input, bytes_to_copy );
4483 input_length -= bytes_to_copy;
4484 input += bytes_to_copy;
4485 ctx->unprocessed_len += bytes_to_copy;
4486
4487 if( ctx->unprocessed_len == block_size )
4488 {
4489 status = mbedtls_to_psa_error(
4490 mbedtls_cipher_update( ctx,
4491 ctx->unprocessed_data,
4492 block_size,
4493 output, &internal_output_length ) );
4494
4495 if( status != PSA_SUCCESS )
4496 goto exit;
4497
4498 output += internal_output_length;
4499 output_size -= internal_output_length;
4500 *output_length += internal_output_length;
4501 ctx->unprocessed_len = 0;
4502 }
4503 }
4504
4505 while( input_length >= block_size )
4506 {
4507 /* Run all full blocks we have, one by one */
4508 status = mbedtls_to_psa_error(
4509 mbedtls_cipher_update( ctx, input,
4510 block_size,
4511 output, &internal_output_length ) );
4512
4513 if( status != PSA_SUCCESS )
4514 goto exit;
4515
4516 input_length -= block_size;
4517 input += block_size;
4518
4519 output += internal_output_length;
4520 output_size -= internal_output_length;
4521 *output_length += internal_output_length;
4522 }
4523
4524 if( input_length > 0 )
4525 {
4526 /* Save unprocessed bytes for later processing */
4527 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4528 input, input_length );
4529 ctx->unprocessed_len += input_length;
4530 }
4531
4532 status = PSA_SUCCESS;
4533
4534exit:
4535 return( status );
4536}
4537
Gilles Peskinee553c652018-06-04 16:22:46 +02004538psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4539 const uint8_t *input,
4540 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004541 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004542 size_t output_size,
4543 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004544{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004545 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004546 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004547 if( operation->alg == 0 )
4548 {
4549 return( PSA_ERROR_BAD_STATE );
4550 }
4551 if( operation->iv_required && ! operation->iv_set )
4552 {
4553 return( PSA_ERROR_BAD_STATE );
4554 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004555
Steven Cooremanef8575e2020-09-11 11:44:50 +02004556 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004557 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004558 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004559 input,
4560 input_length,
4561 output,
4562 output_size,
4563 output_length );
4564 goto exit;
4565 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004566
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004567 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004568 {
4569 /* Take the unprocessed partial block left over from previous
4570 * update calls, if any, plus the input to this call. Remove
4571 * the last partial block, if any. You get the data that will be
4572 * output in this call. */
4573 expected_output_size =
4574 ( operation->ctx.cipher.unprocessed_len + input_length )
4575 / operation->block_size * operation->block_size;
4576 }
4577 else
4578 {
4579 expected_output_size = input_length;
4580 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004581
Gilles Peskine89d789c2018-06-04 17:17:16 +02004582 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004583 {
4584 status = PSA_ERROR_BUFFER_TOO_SMALL;
4585 goto exit;
4586 }
mohammad160382759612018-03-12 18:16:40 +02004587
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004588 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4589 {
4590 /* mbedtls_cipher_update has an API inconsistency: it will only
4591 * process a single block at a time in ECB mode. Abstract away that
4592 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004593 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4594 input,
4595 input_length,
4596 output,
4597 output_size,
4598 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004599 }
4600 else
4601 {
4602 status = mbedtls_to_psa_error(
4603 mbedtls_cipher_update( &operation->ctx.cipher, input,
4604 input_length, output, output_length ) );
4605 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004606exit:
4607 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004608 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004609 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004610}
4611
Gilles Peskinee553c652018-06-04 16:22:46 +02004612psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4613 uint8_t *output,
4614 size_t output_size,
4615 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004616{
David Saadab4ecc272019-02-14 13:48:10 +02004617 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004618 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004619 if( operation->alg == 0 )
4620 {
4621 return( PSA_ERROR_BAD_STATE );
4622 }
4623 if( operation->iv_required && ! operation->iv_set )
4624 {
4625 return( PSA_ERROR_BAD_STATE );
4626 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004627
Steven Cooremanef8575e2020-09-11 11:44:50 +02004628 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004629 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004630 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004631 output,
4632 output_size,
4633 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004634 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004635 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004636
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004637 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004638 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004639 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004640 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004641 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004642 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004643 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004644 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004645 }
4646
Steven Cooremanef8575e2020-09-11 11:44:50 +02004647 status = mbedtls_to_psa_error(
4648 mbedtls_cipher_finish( &operation->ctx.cipher,
4649 temp_output_buffer,
4650 output_length ) );
4651 if( status != PSA_SUCCESS )
4652 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004653
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004654 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004655 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004656 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004657 memcpy( output, temp_output_buffer, *output_length );
4658 else
Janos Follath315b51c2018-07-09 16:04:51 +01004659 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004660
Steven Cooremanef8575e2020-09-11 11:44:50 +02004661exit:
4662 if( operation->mbedtls_in_use == 1 )
4663 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004664
Steven Cooremanef8575e2020-09-11 11:44:50 +02004665 if( status == PSA_SUCCESS )
4666 return( psa_cipher_abort( operation ) );
4667 else
4668 {
4669 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004670 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004671
Steven Cooremanef8575e2020-09-11 11:44:50 +02004672 return( status );
4673 }
mohammad1603503973b2018-03-12 15:59:30 +02004674}
4675
Gilles Peskinee553c652018-06-04 16:22:46 +02004676psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4677{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004678 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004679 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004680 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004681 * in use. It's ok to call abort on such an object, and there's
4682 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004683 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004684 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004685
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004686 /* Sanity check (shouldn't happen: operation->alg should
4687 * always have been initialized to a valid value). */
4688 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4689 return( PSA_ERROR_BAD_STATE );
4690
Steven Cooremanef8575e2020-09-11 11:44:50 +02004691 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004692 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004693 else
4694 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004695
Moran Peker41deec42018-04-04 15:43:05 +03004696 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004697 operation->key_set = 0;
4698 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004699 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004700 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004701 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004702 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004703
Moran Peker395db872018-05-31 14:07:14 +03004704 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004705}
4706
Gilles Peskinea0655c32018-04-30 17:06:50 +02004707
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004708
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004709
mohammad16035955c982018-04-26 00:53:03 +03004710/****************************************************************/
4711/* AEAD */
4712/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004713
Gilles Peskineedf9a652018-08-17 18:11:56 +02004714typedef struct
4715{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004716 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004717 const mbedtls_cipher_info_t *cipher_info;
4718 union
4719 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004720 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004721#if defined(MBEDTLS_CCM_C)
4722 mbedtls_ccm_context ccm;
4723#endif /* MBEDTLS_CCM_C */
4724#if defined(MBEDTLS_GCM_C)
4725 mbedtls_gcm_context gcm;
4726#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004727#if defined(MBEDTLS_CHACHAPOLY_C)
4728 mbedtls_chachapoly_context chachapoly;
4729#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004730 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004731 psa_algorithm_t core_alg;
4732 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004733 uint8_t tag_length;
4734} aead_operation_t;
4735
Ronald Cronf95a2b12020-10-22 15:24:49 +02004736#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4737
Gilles Peskine30a9e412019-01-14 18:36:12 +01004738static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004739{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004740 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004741 {
4742#if defined(MBEDTLS_CCM_C)
4743 case PSA_ALG_CCM:
4744 mbedtls_ccm_free( &operation->ctx.ccm );
4745 break;
4746#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004747#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004748 case PSA_ALG_GCM:
4749 mbedtls_gcm_free( &operation->ctx.gcm );
4750 break;
4751#endif /* MBEDTLS_GCM_C */
4752 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004753
4754 psa_decrement_key_slot_access_count( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004755}
4756
4757static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004758 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004759 psa_key_usage_t usage,
4760 psa_algorithm_t alg )
4761{
4762 psa_status_t status;
4763 size_t key_bits;
4764 mbedtls_cipher_id_t cipher_id;
4765
Ronald Croncf56a0a2020-08-04 09:51:30 +02004766 status = psa_get_transparent_key( key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004767 if( status != PSA_SUCCESS )
4768 return( status );
4769
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004770 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004771
4772 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004773 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004774 &cipher_id );
4775 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004776 {
4777 status = PSA_ERROR_NOT_SUPPORTED;
4778 goto cleanup;
4779 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004780
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004781 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004782 {
4783#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004784 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4785 operation->core_alg = PSA_ALG_CCM;
4786 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004787 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4788 * The call to mbedtls_ccm_encrypt_and_tag or
4789 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004790 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004791 {
4792 status = PSA_ERROR_INVALID_ARGUMENT;
4793 goto cleanup;
4794 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004795 mbedtls_ccm_init( &operation->ctx.ccm );
4796 status = mbedtls_to_psa_error(
4797 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004798 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004799 (unsigned int) key_bits ) );
4800 if( status != 0 )
4801 goto cleanup;
4802 break;
4803#endif /* MBEDTLS_CCM_C */
4804
4805#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004806 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4807 operation->core_alg = PSA_ALG_GCM;
4808 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004809 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4810 * The call to mbedtls_gcm_crypt_and_tag or
4811 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004812 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004813 {
4814 status = PSA_ERROR_INVALID_ARGUMENT;
4815 goto cleanup;
4816 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004817 mbedtls_gcm_init( &operation->ctx.gcm );
4818 status = mbedtls_to_psa_error(
4819 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004820 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004821 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004822 if( status != 0 )
4823 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004824 break;
4825#endif /* MBEDTLS_GCM_C */
4826
Gilles Peskine26869f22019-05-06 15:25:00 +02004827#if defined(MBEDTLS_CHACHAPOLY_C)
4828 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4829 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4830 operation->full_tag_length = 16;
4831 /* We only support the default tag length. */
4832 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004833 {
4834 status = PSA_ERROR_NOT_SUPPORTED;
4835 goto cleanup;
4836 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004837 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4838 status = mbedtls_to_psa_error(
4839 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004840 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004841 if( status != 0 )
4842 goto cleanup;
4843 break;
4844#endif /* MBEDTLS_CHACHAPOLY_C */
4845
Gilles Peskineedf9a652018-08-17 18:11:56 +02004846 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004847 status = PSA_ERROR_NOT_SUPPORTED;
4848 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004849 }
4850
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004851 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4852 {
4853 status = PSA_ERROR_INVALID_ARGUMENT;
4854 goto cleanup;
4855 }
4856 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004857
Gilles Peskineedf9a652018-08-17 18:11:56 +02004858 return( PSA_SUCCESS );
4859
4860cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004861 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004862 return( status );
4863}
4864
Ronald Croncf56a0a2020-08-04 09:51:30 +02004865psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004866 psa_algorithm_t alg,
4867 const uint8_t *nonce,
4868 size_t nonce_length,
4869 const uint8_t *additional_data,
4870 size_t additional_data_length,
4871 const uint8_t *plaintext,
4872 size_t plaintext_length,
4873 uint8_t *ciphertext,
4874 size_t ciphertext_size,
4875 size_t *ciphertext_length )
4876{
mohammad16035955c982018-04-26 00:53:03 +03004877 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004878 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004879 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004880
mohammad1603f08a5502018-06-03 15:05:47 +03004881 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004882
Ronald Croncf56a0a2020-08-04 09:51:30 +02004883 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004884 if( status != PSA_SUCCESS )
4885 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004886
Gilles Peskineedf9a652018-08-17 18:11:56 +02004887 /* For all currently supported modes, the tag is at the end of the
4888 * ciphertext. */
4889 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4890 {
4891 status = PSA_ERROR_BUFFER_TOO_SMALL;
4892 goto exit;
4893 }
4894 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004895
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004896#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004897 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004898 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004899 status = mbedtls_to_psa_error(
4900 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4901 MBEDTLS_GCM_ENCRYPT,
4902 plaintext_length,
4903 nonce, nonce_length,
4904 additional_data, additional_data_length,
4905 plaintext, ciphertext,
4906 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004907 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004908 else
4909#endif /* MBEDTLS_GCM_C */
4910#if defined(MBEDTLS_CCM_C)
4911 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004912 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004913 status = mbedtls_to_psa_error(
4914 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4915 plaintext_length,
4916 nonce, nonce_length,
4917 additional_data,
4918 additional_data_length,
4919 plaintext, ciphertext,
4920 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004921 }
mohammad16035c8845f2018-05-09 05:40:09 -07004922 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004923#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004924#if defined(MBEDTLS_CHACHAPOLY_C)
4925 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4926 {
4927 if( nonce_length != 12 || operation.tag_length != 16 )
4928 {
4929 status = PSA_ERROR_NOT_SUPPORTED;
4930 goto exit;
4931 }
4932 status = mbedtls_to_psa_error(
4933 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4934 plaintext_length,
4935 nonce,
4936 additional_data,
4937 additional_data_length,
4938 plaintext,
4939 ciphertext,
4940 tag ) );
4941 }
4942 else
4943#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004944 {
mohammad1603554faad2018-06-03 15:07:38 +03004945 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004946 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004947
Gilles Peskineedf9a652018-08-17 18:11:56 +02004948 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4949 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004950
Gilles Peskineedf9a652018-08-17 18:11:56 +02004951exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004952 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004953 if( status == PSA_SUCCESS )
4954 *ciphertext_length = plaintext_length + operation.tag_length;
4955 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004956}
4957
Gilles Peskineee652a32018-06-01 19:23:52 +02004958/* Locate the tag in a ciphertext buffer containing the encrypted data
4959 * followed by the tag. Return the length of the part preceding the tag in
4960 * *plaintext_length. This is the size of the plaintext in modes where
4961 * the encrypted data has the same size as the plaintext, such as
4962 * CCM and GCM. */
4963static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4964 const uint8_t *ciphertext,
4965 size_t ciphertext_length,
4966 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004967 const uint8_t **p_tag )
4968{
4969 size_t payload_length;
4970 if( tag_length > ciphertext_length )
4971 return( PSA_ERROR_INVALID_ARGUMENT );
4972 payload_length = ciphertext_length - tag_length;
4973 if( payload_length > plaintext_size )
4974 return( PSA_ERROR_BUFFER_TOO_SMALL );
4975 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004976 return( PSA_SUCCESS );
4977}
4978
Ronald Croncf56a0a2020-08-04 09:51:30 +02004979psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004980 psa_algorithm_t alg,
4981 const uint8_t *nonce,
4982 size_t nonce_length,
4983 const uint8_t *additional_data,
4984 size_t additional_data_length,
4985 const uint8_t *ciphertext,
4986 size_t ciphertext_length,
4987 uint8_t *plaintext,
4988 size_t plaintext_size,
4989 size_t *plaintext_length )
4990{
mohammad16035955c982018-04-26 00:53:03 +03004991 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004992 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004993 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004994
Gilles Peskineee652a32018-06-01 19:23:52 +02004995 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004996
Ronald Croncf56a0a2020-08-04 09:51:30 +02004997 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004998 if( status != PSA_SUCCESS )
4999 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005000
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005001 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5002 ciphertext, ciphertext_length,
5003 plaintext_size, &tag );
5004 if( status != PSA_SUCCESS )
5005 goto exit;
5006
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005007#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005008 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005009 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005010 status = mbedtls_to_psa_error(
5011 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5012 ciphertext_length - operation.tag_length,
5013 nonce, nonce_length,
5014 additional_data,
5015 additional_data_length,
5016 tag, operation.tag_length,
5017 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005018 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005019 else
5020#endif /* MBEDTLS_GCM_C */
5021#if defined(MBEDTLS_CCM_C)
5022 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005023 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005024 status = mbedtls_to_psa_error(
5025 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5026 ciphertext_length - operation.tag_length,
5027 nonce, nonce_length,
5028 additional_data,
5029 additional_data_length,
5030 ciphertext, plaintext,
5031 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005032 }
mohammad160339574652018-06-01 04:39:53 -07005033 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005034#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005035#if defined(MBEDTLS_CHACHAPOLY_C)
5036 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5037 {
5038 if( nonce_length != 12 || operation.tag_length != 16 )
5039 {
5040 status = PSA_ERROR_NOT_SUPPORTED;
5041 goto exit;
5042 }
5043 status = mbedtls_to_psa_error(
5044 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5045 ciphertext_length - operation.tag_length,
5046 nonce,
5047 additional_data,
5048 additional_data_length,
5049 tag,
5050 ciphertext,
5051 plaintext ) );
5052 }
5053 else
5054#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005055 {
mohammad1603554faad2018-06-03 15:07:38 +03005056 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005057 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005058
Gilles Peskineedf9a652018-08-17 18:11:56 +02005059 if( status != PSA_SUCCESS && plaintext_size != 0 )
5060 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005061
Gilles Peskineedf9a652018-08-17 18:11:56 +02005062exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005063 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005064 if( status == PSA_SUCCESS )
5065 *plaintext_length = ciphertext_length - operation.tag_length;
5066 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005067}
5068
Gilles Peskinea0655c32018-04-30 17:06:50 +02005069
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005070
Gilles Peskine20035e32018-02-03 22:44:14 +01005071/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005072/* Generators */
5073/****************************************************************/
5074
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005075#define HKDF_STATE_INIT 0 /* no input yet */
5076#define HKDF_STATE_STARTED 1 /* got salt */
5077#define HKDF_STATE_KEYED 2 /* got key */
5078#define HKDF_STATE_OUTPUT 3 /* output started */
5079
Gilles Peskinecbe66502019-05-16 16:59:18 +02005080static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005081 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005082{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005083 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5084 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005085 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005086 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005087}
5088
5089
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005090psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005091{
5092 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005093 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005094 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005095 {
5096 /* The object has (apparently) been initialized but it is not
5097 * in use. It's ok to call abort on such an object, and there's
5098 * nothing to do. */
5099 }
5100 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02005101#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005102 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005103 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005104 mbedtls_free( operation->ctx.hkdf.info );
5105 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005106 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005107 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005109 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005110 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005111 if( operation->ctx.tls12_prf.seed != NULL )
5112 {
5113 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5114 operation->ctx.tls12_prf.seed_length );
5115 mbedtls_free( operation->ctx.tls12_prf.seed );
5116 }
5117
5118 if( operation->ctx.tls12_prf.label != NULL )
5119 {
5120 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5121 operation->ctx.tls12_prf.label_length );
5122 mbedtls_free( operation->ctx.tls12_prf.label );
5123 }
5124
5125 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5126
5127 /* We leave the fields Ai and output_block to be erased safely by the
5128 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005129 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005130 else
5131#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005132 {
5133 status = PSA_ERROR_BAD_STATE;
5134 }
Janos Follath083036a2019-06-11 10:22:26 +01005135 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005136 return( status );
5137}
5138
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005139psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005140 size_t *capacity)
5141{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005142 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005143 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005144 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005145 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005146 }
5147
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005148 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005149 return( PSA_SUCCESS );
5150}
5151
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005152psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005153 size_t capacity )
5154{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005155 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005156 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005157 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005158 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005159 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005160 return( PSA_SUCCESS );
5161}
5162
Gilles Peskinebef7f142018-07-12 17:22:21 +02005163#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005164/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005165 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005166static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005167 psa_algorithm_t hash_alg,
5168 uint8_t *output,
5169 size_t output_length )
5170{
5171 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5172 psa_status_t status;
5173
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005174 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5175 return( PSA_ERROR_BAD_STATE );
5176 hkdf->state = HKDF_STATE_OUTPUT;
5177
Gilles Peskinebef7f142018-07-12 17:22:21 +02005178 while( output_length != 0 )
5179 {
5180 /* Copy what remains of the current block */
5181 uint8_t n = hash_length - hkdf->offset_in_block;
5182 if( n > output_length )
5183 n = (uint8_t) output_length;
5184 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5185 output += n;
5186 output_length -= n;
5187 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005188 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005189 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005190 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005191 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005192 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005193 * object was corrupted or if this function is called directly
5194 * inside the library. */
5195 if( hkdf->block_number == 0xff )
5196 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005197
5198 /* We need a new block */
5199 ++hkdf->block_number;
5200 hkdf->offset_in_block = 0;
5201 status = psa_hmac_setup_internal( &hkdf->hmac,
5202 hkdf->prk, hash_length,
5203 hash_alg );
5204 if( status != PSA_SUCCESS )
5205 return( status );
5206 if( hkdf->block_number != 1 )
5207 {
5208 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5209 hkdf->output_block,
5210 hash_length );
5211 if( status != PSA_SUCCESS )
5212 return( status );
5213 }
5214 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5215 hkdf->info,
5216 hkdf->info_length );
5217 if( status != PSA_SUCCESS )
5218 return( status );
5219 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5220 &hkdf->block_number, 1 );
5221 if( status != PSA_SUCCESS )
5222 return( status );
5223 status = psa_hmac_finish_internal( &hkdf->hmac,
5224 hkdf->output_block,
5225 sizeof( hkdf->output_block ) );
5226 if( status != PSA_SUCCESS )
5227 return( status );
5228 }
5229
5230 return( PSA_SUCCESS );
5231}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005232
Janos Follath7742fee2019-06-17 12:58:10 +01005233static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5234 psa_tls12_prf_key_derivation_t *tls12_prf,
5235 psa_algorithm_t alg )
5236{
5237 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5238 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005239 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5240 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005241
Janos Follath7742fee2019-06-17 12:58:10 +01005242 /* We can't be wanting more output after block 0xff, otherwise
5243 * the capacity check in psa_key_derivation_output_bytes() would have
5244 * prevented this call. It could happen only if the operation
5245 * object was corrupted or if this function is called directly
5246 * inside the library. */
5247 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005248 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005249
5250 /* We need a new block */
5251 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005252 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005253
5254 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5255 *
5256 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5257 *
5258 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5259 * HMAC_hash(secret, A(2) + seed) +
5260 * HMAC_hash(secret, A(3) + seed) + ...
5261 *
5262 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005263 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005264 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005265 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005266 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005267 * is currently extracted as `output_block` and where i is
5268 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005269 */
5270
Janos Follathea29bfb2019-06-19 12:21:20 +01005271 /* Save the hash context before using it, to preserve the hash state with
5272 * only the inner padding in it. We need this, because inner padding depends
5273 * on the key (secret in the RFC's terminology). */
5274 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5275 if( status != PSA_SUCCESS )
5276 goto cleanup;
5277
5278 /* Calculate A(i) where i = tls12_prf->block_number. */
5279 if( tls12_prf->block_number == 1 )
5280 {
5281 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5282 * the variable seed and in this instance means it in the context of the
5283 * P_hash function, where seed = label + seed.) */
5284 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5285 tls12_prf->label, tls12_prf->label_length );
5286 if( status != PSA_SUCCESS )
5287 goto cleanup;
5288 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5289 tls12_prf->seed, tls12_prf->seed_length );
5290 if( status != PSA_SUCCESS )
5291 goto cleanup;
5292 }
5293 else
5294 {
5295 /* A(i) = HMAC_hash(secret, A(i-1)) */
5296 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5297 tls12_prf->Ai, hash_length );
5298 if( status != PSA_SUCCESS )
5299 goto cleanup;
5300 }
5301
5302 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5303 tls12_prf->Ai, hash_length );
5304 if( status != PSA_SUCCESS )
5305 goto cleanup;
5306 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5307 if( status != PSA_SUCCESS )
5308 goto cleanup;
5309
5310 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5311 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5312 tls12_prf->Ai, hash_length );
5313 if( status != PSA_SUCCESS )
5314 goto cleanup;
5315 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5316 tls12_prf->label, tls12_prf->label_length );
5317 if( status != PSA_SUCCESS )
5318 goto cleanup;
5319 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5320 tls12_prf->seed, tls12_prf->seed_length );
5321 if( status != PSA_SUCCESS )
5322 goto cleanup;
5323 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5324 tls12_prf->output_block, hash_length );
5325 if( status != PSA_SUCCESS )
5326 goto cleanup;
5327 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5328 if( status != PSA_SUCCESS )
5329 goto cleanup;
5330
Janos Follath7742fee2019-06-17 12:58:10 +01005331
5332cleanup:
5333
Janos Follathea29bfb2019-06-19 12:21:20 +01005334 cleanup_status = psa_hash_abort( &backup );
5335 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5336 status = cleanup_status;
5337
5338 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005339}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005340
Janos Follath844eb0e2019-06-19 12:10:49 +01005341static psa_status_t psa_key_derivation_tls12_prf_read(
5342 psa_tls12_prf_key_derivation_t *tls12_prf,
5343 psa_algorithm_t alg,
5344 uint8_t *output,
5345 size_t output_length )
5346{
5347 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5348 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5349 psa_status_t status;
5350 uint8_t offset, length;
5351
5352 while( output_length != 0 )
5353 {
5354 /* Check if we have fully processed the current block. */
5355 if( tls12_prf->left_in_block == 0 )
5356 {
5357 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5358 alg );
5359 if( status != PSA_SUCCESS )
5360 return( status );
5361
5362 continue;
5363 }
5364
5365 if( tls12_prf->left_in_block > output_length )
5366 length = (uint8_t) output_length;
5367 else
5368 length = tls12_prf->left_in_block;
5369
5370 offset = hash_length - tls12_prf->left_in_block;
5371 memcpy( output, tls12_prf->output_block + offset, length );
5372 output += length;
5373 output_length -= length;
5374 tls12_prf->left_in_block -= length;
5375 }
5376
5377 return( PSA_SUCCESS );
5378}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005379#endif /* MBEDTLS_MD_C */
5380
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005381psa_status_t psa_key_derivation_output_bytes(
5382 psa_key_derivation_operation_t *operation,
5383 uint8_t *output,
5384 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005385{
5386 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005387 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005388
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005389 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005390 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005391 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005392 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005393 }
5394
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005395 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005396 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005397 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005398 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005399 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005400 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005401 goto exit;
5402 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005403 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005404 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005405 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005406 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005407 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5408 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005409 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005410 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005411 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005412 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005413 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005414
Gilles Peskinebef7f142018-07-12 17:22:21 +02005415#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005416 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005417 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005418 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005419 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005420 output, output_length );
5421 }
Janos Follathadbec812019-06-14 11:05:39 +01005422 else
Janos Follathadbec812019-06-14 11:05:39 +01005423 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005424 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005425 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005426 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005427 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005428 output_length );
5429 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005430 else
5431#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005432 {
5433 return( PSA_ERROR_BAD_STATE );
5434 }
5435
5436exit:
5437 if( status != PSA_SUCCESS )
5438 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005439 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005440 * This allows us to differentiate between exhausted operations and
5441 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5442 * operations. */
5443 psa_algorithm_t alg = operation->alg;
5444 psa_key_derivation_abort( operation );
5445 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005446 memset( output, '!', output_length );
5447 }
5448 return( status );
5449}
5450
Gilles Peskine08542d82018-07-19 17:05:42 +02005451#if defined(MBEDTLS_DES_C)
5452static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5453{
5454 if( data_size >= 8 )
5455 mbedtls_des_key_set_parity( data );
5456 if( data_size >= 16 )
5457 mbedtls_des_key_set_parity( data + 8 );
5458 if( data_size >= 24 )
5459 mbedtls_des_key_set_parity( data + 16 );
5460}
5461#endif /* MBEDTLS_DES_C */
5462
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005463static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005464 psa_key_slot_t *slot,
5465 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005466 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005467{
5468 uint8_t *data = NULL;
5469 size_t bytes = PSA_BITS_TO_BYTES( bits );
5470 psa_status_t status;
5471
Gilles Peskine8e338702019-07-30 20:06:31 +02005472 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005473 return( PSA_ERROR_INVALID_ARGUMENT );
5474 if( bits % 8 != 0 )
5475 return( PSA_ERROR_INVALID_ARGUMENT );
5476 data = mbedtls_calloc( 1, bytes );
5477 if( data == NULL )
5478 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5479
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005480 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005481 if( status != PSA_SUCCESS )
5482 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005483#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005484 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005485 psa_des_set_key_parity( data, bytes );
5486#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005487 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005488
5489exit:
5490 mbedtls_free( data );
5491 return( status );
5492}
5493
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005494psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005495 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005496 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005497{
5498 psa_status_t status;
5499 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005500 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005501
5502 /* Reject any attempt to create a zero-length key so that we don't
5503 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5504 if( psa_get_key_bits( attributes ) == 0 )
5505 return( PSA_ERROR_INVALID_ARGUMENT );
5506
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005507 if( ! operation->can_output_key )
5508 return( PSA_ERROR_NOT_PERMITTED );
5509
Gilles Peskinedf179142019-07-15 22:02:14 +02005510 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005511 attributes, key, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005512#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5513 if( driver != NULL )
5514 {
5515 /* Deriving a key in a secure element is not implemented yet. */
5516 status = PSA_ERROR_NOT_SUPPORTED;
5517 }
5518#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005519 if( status == PSA_SUCCESS )
5520 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005521 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005522 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005524 }
5525 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005526 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005527 if( status != PSA_SUCCESS )
5528 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005529 psa_fail_key_creation( slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02005530 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005531 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005532 else
5533 status = psa_decrement_key_slot_access_count( slot );
5534
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005535 return( status );
5536}
5537
Gilles Peskineeab56e42018-07-12 17:12:33 +02005538
5539
5540/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005541/* Key derivation */
5542/****************************************************************/
5543
Gilles Peskine969c5d62019-01-16 15:53:06 +01005544static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005545 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005546 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005547{
Janos Follath5fe19732019-06-20 15:09:30 +01005548 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5549 * initialisers for this union leave some bytes unspecified.) */
5550 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5551
Gilles Peskine969c5d62019-01-16 15:53:06 +01005552 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005553#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005554 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5555 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5556 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005557 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005558 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005559 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5560 if( hash_size == 0 )
5561 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005562 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5563 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005564 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005565 {
5566 return( PSA_ERROR_NOT_SUPPORTED );
5567 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005568 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005569 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005570 }
5571#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005572 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005573 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005574}
5575
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005576psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005577 psa_algorithm_t alg )
5578{
5579 psa_status_t status;
5580
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005581 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005582 return( PSA_ERROR_BAD_STATE );
5583
Gilles Peskine6843c292019-01-18 16:44:49 +01005584 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5585 return( PSA_ERROR_INVALID_ARGUMENT );
5586 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005587 {
5588 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005589 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005590 }
5591 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5592 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005593 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005594 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005595 else
5596 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005597
5598 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005599 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005600 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005601}
5602
5603#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005604static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005605 psa_algorithm_t hash_alg,
5606 psa_key_derivation_step_t step,
5607 const uint8_t *data,
5608 size_t data_length )
5609{
5610 psa_status_t status;
5611 switch( step )
5612 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005613 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005614 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005615 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005616 status = psa_hmac_setup_internal( &hkdf->hmac,
5617 data, data_length,
5618 hash_alg );
5619 if( status != PSA_SUCCESS )
5620 return( status );
5621 hkdf->state = HKDF_STATE_STARTED;
5622 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005623 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005624 /* If no salt was provided, use an empty salt. */
5625 if( hkdf->state == HKDF_STATE_INIT )
5626 {
5627 status = psa_hmac_setup_internal( &hkdf->hmac,
5628 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005629 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005630 if( status != PSA_SUCCESS )
5631 return( status );
5632 hkdf->state = HKDF_STATE_STARTED;
5633 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005634 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005635 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005636 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5637 data, data_length );
5638 if( status != PSA_SUCCESS )
5639 return( status );
5640 status = psa_hmac_finish_internal( &hkdf->hmac,
5641 hkdf->prk,
5642 sizeof( hkdf->prk ) );
5643 if( status != PSA_SUCCESS )
5644 return( status );
5645 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5646 hkdf->block_number = 0;
5647 hkdf->state = HKDF_STATE_KEYED;
5648 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005649 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005650 if( hkdf->state == HKDF_STATE_OUTPUT )
5651 return( PSA_ERROR_BAD_STATE );
5652 if( hkdf->info_set )
5653 return( PSA_ERROR_BAD_STATE );
5654 hkdf->info_length = data_length;
5655 if( data_length != 0 )
5656 {
5657 hkdf->info = mbedtls_calloc( 1, data_length );
5658 if( hkdf->info == NULL )
5659 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5660 memcpy( hkdf->info, data, data_length );
5661 }
5662 hkdf->info_set = 1;
5663 return( PSA_SUCCESS );
5664 default:
5665 return( PSA_ERROR_INVALID_ARGUMENT );
5666 }
5667}
Janos Follathb03233e2019-06-11 15:30:30 +01005668
Janos Follathf08e2652019-06-13 09:05:41 +01005669static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5670 const uint8_t *data,
5671 size_t data_length )
5672{
5673 if( prf->state != TLS12_PRF_STATE_INIT )
5674 return( PSA_ERROR_BAD_STATE );
5675
Janos Follathd6dce9f2019-07-04 09:11:38 +01005676 if( data_length != 0 )
5677 {
5678 prf->seed = mbedtls_calloc( 1, data_length );
5679 if( prf->seed == NULL )
5680 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005681
Janos Follathd6dce9f2019-07-04 09:11:38 +01005682 memcpy( prf->seed, data, data_length );
5683 prf->seed_length = data_length;
5684 }
Janos Follathf08e2652019-06-13 09:05:41 +01005685
5686 prf->state = TLS12_PRF_STATE_SEED_SET;
5687
5688 return( PSA_SUCCESS );
5689}
5690
Janos Follath81550542019-06-13 14:26:34 +01005691static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5692 psa_algorithm_t hash_alg,
5693 const uint8_t *data,
5694 size_t data_length )
5695{
5696 psa_status_t status;
5697 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5698 return( PSA_ERROR_BAD_STATE );
5699
5700 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5701 if( status != PSA_SUCCESS )
5702 return( status );
5703
5704 prf->state = TLS12_PRF_STATE_KEY_SET;
5705
5706 return( PSA_SUCCESS );
5707}
5708
Janos Follath6660f0e2019-06-17 08:44:03 +01005709static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5710 psa_tls12_prf_key_derivation_t *prf,
5711 psa_algorithm_t hash_alg,
5712 const uint8_t *data,
5713 size_t data_length )
5714{
5715 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005716 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5717 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005718
5719 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5720 return( PSA_ERROR_INVALID_ARGUMENT );
5721
5722 /* Quoting RFC 4279, Section 2:
5723 *
5724 * The premaster secret is formed as follows: if the PSK is N octets
5725 * long, concatenate a uint16 with the value N, N zero octets, a second
5726 * uint16 with the value N, and the PSK itself.
5727 */
5728
Janos Follath40e13932019-06-26 13:22:29 +01005729 *cur++ = ( data_length >> 8 ) & 0xff;
5730 *cur++ = ( data_length >> 0 ) & 0xff;
5731 memset( cur, 0, data_length );
5732 cur += data_length;
5733 *cur++ = pms[0];
5734 *cur++ = pms[1];
5735 memcpy( cur, data, data_length );
5736 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005737
Janos Follath40e13932019-06-26 13:22:29 +01005738 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005739
5740 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5741 return( status );
5742}
5743
Janos Follath63028dd2019-06-13 09:15:47 +01005744static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5745 const uint8_t *data,
5746 size_t data_length )
5747{
5748 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5749 return( PSA_ERROR_BAD_STATE );
5750
Janos Follathd6dce9f2019-07-04 09:11:38 +01005751 if( data_length != 0 )
5752 {
5753 prf->label = mbedtls_calloc( 1, data_length );
5754 if( prf->label == NULL )
5755 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005756
Janos Follathd6dce9f2019-07-04 09:11:38 +01005757 memcpy( prf->label, data, data_length );
5758 prf->label_length = data_length;
5759 }
Janos Follath63028dd2019-06-13 09:15:47 +01005760
5761 prf->state = TLS12_PRF_STATE_LABEL_SET;
5762
5763 return( PSA_SUCCESS );
5764}
5765
Janos Follathb03233e2019-06-11 15:30:30 +01005766static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5767 psa_algorithm_t hash_alg,
5768 psa_key_derivation_step_t step,
5769 const uint8_t *data,
5770 size_t data_length )
5771{
Janos Follathb03233e2019-06-11 15:30:30 +01005772 switch( step )
5773 {
Janos Follathf08e2652019-06-13 09:05:41 +01005774 case PSA_KEY_DERIVATION_INPUT_SEED:
5775 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005776 case PSA_KEY_DERIVATION_INPUT_SECRET:
5777 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005778 case PSA_KEY_DERIVATION_INPUT_LABEL:
5779 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005780 default:
5781 return( PSA_ERROR_INVALID_ARGUMENT );
5782 }
5783}
Janos Follath6660f0e2019-06-17 08:44:03 +01005784
5785static psa_status_t psa_tls12_prf_psk_to_ms_input(
5786 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{
5792 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005793 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005794 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5795 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005796 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005797
5798 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5799}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005800#endif /* MBEDTLS_MD_C */
5801
Gilles Peskineb8965192019-09-24 16:21:10 +02005802/** Check whether the given key type is acceptable for the given
5803 * input step of a key derivation.
5804 *
5805 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5806 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5807 * Both secret and non-secret inputs can alternatively have the type
5808 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5809 * that the input was passed as a buffer rather than via a key object.
5810 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005811static int psa_key_derivation_check_input_type(
5812 psa_key_derivation_step_t step,
5813 psa_key_type_t key_type )
5814{
5815 switch( step )
5816 {
5817 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005818 if( key_type == PSA_KEY_TYPE_DERIVE )
5819 return( PSA_SUCCESS );
5820 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005821 return( PSA_SUCCESS );
5822 break;
5823 case PSA_KEY_DERIVATION_INPUT_LABEL:
5824 case PSA_KEY_DERIVATION_INPUT_SALT:
5825 case PSA_KEY_DERIVATION_INPUT_INFO:
5826 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005827 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5828 return( PSA_SUCCESS );
5829 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005830 return( PSA_SUCCESS );
5831 break;
5832 }
5833 return( PSA_ERROR_INVALID_ARGUMENT );
5834}
5835
Janos Follathb80a94e2019-06-12 15:54:46 +01005836static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005837 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005838 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005839 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005840 const uint8_t *data,
5841 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005842{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005843 psa_status_t status;
5844 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5845
5846 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005847 if( status != PSA_SUCCESS )
5848 goto exit;
5849
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005850#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005851 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005852 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005853 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005854 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005855 step, data, data_length );
5856 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005857 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005858 {
Janos Follathb03233e2019-06-11 15:30:30 +01005859 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5860 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5861 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005862 }
5863 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5864 {
5865 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5866 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5867 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005868 }
5869 else
5870#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005871 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005872 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005873 return( PSA_ERROR_BAD_STATE );
5874 }
5875
Gilles Peskine224b0d62019-09-23 18:13:17 +02005876exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005877 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005878 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005879 return( status );
5880}
5881
Janos Follath51f4a0f2019-06-14 11:35:55 +01005882psa_status_t psa_key_derivation_input_bytes(
5883 psa_key_derivation_operation_t *operation,
5884 psa_key_derivation_step_t step,
5885 const uint8_t *data,
5886 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005887{
Gilles Peskineb8965192019-09-24 16:21:10 +02005888 return( psa_key_derivation_input_internal( operation, step,
5889 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005890 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005891}
5892
Janos Follath51f4a0f2019-06-14 11:35:55 +01005893psa_status_t psa_key_derivation_input_key(
5894 psa_key_derivation_operation_t *operation,
5895 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005896 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005897{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005898 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5899 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005900 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005901
Ronald Croncf56a0a2020-08-04 09:51:30 +02005902 status = psa_get_transparent_key( key, &slot,
5903 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005904 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005905 {
5906 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005907 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005908 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005909
5910 /* Passing a key object as a SECRET input unlocks the permission
5911 * to output to a key object. */
5912 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5913 operation->can_output_key = 1;
5914
Ronald Cronf95a2b12020-10-22 15:24:49 +02005915 status = psa_key_derivation_input_internal( operation,
5916 step, slot->attr.type,
5917 slot->data.key.data,
5918 slot->data.key.bytes );
5919
5920 decrement_status = psa_decrement_key_slot_access_count( slot );
5921
5922 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005923}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005924
5925
5926
5927/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005928/* Key agreement */
5929/****************************************************************/
5930
Gilles Peskinea05219c2018-11-16 16:02:56 +01005931#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005932static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5933 size_t peer_key_length,
5934 const mbedtls_ecp_keypair *our_key,
5935 uint8_t *shared_secret,
5936 size_t shared_secret_size,
5937 size_t *shared_secret_length )
5938{
Steven Cooremand4867872020-08-05 16:31:39 +02005939 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005940 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005941 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005942 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005943 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005944 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005945
Steven Cooreman4fed4552020-08-03 14:46:03 +02005946 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5947 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005948 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005949 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005950 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005951 goto exit;
5952
Jaeden Amero97271b32019-01-10 19:38:51 +00005953 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005954 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005955 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005956 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005957 status = mbedtls_to_psa_error(
5958 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5959 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005960 goto exit;
5961
Jaeden Amero97271b32019-01-10 19:38:51 +00005962 status = mbedtls_to_psa_error(
5963 mbedtls_ecdh_calc_secret( &ecdh,
5964 shared_secret_length,
5965 shared_secret, shared_secret_size,
5966 mbedtls_ctr_drbg_random,
5967 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005968 if( status != PSA_SUCCESS )
5969 goto exit;
5970 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5971 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005972
5973exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005974 if( status != PSA_SUCCESS )
5975 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005976 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005977 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005978 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005979
Jaeden Amero97271b32019-01-10 19:38:51 +00005980 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005981}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005982#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005983
Gilles Peskine01d718c2018-09-18 12:01:02 +02005984#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5985
Gilles Peskine0216fe12019-04-11 21:23:21 +02005986static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5987 psa_key_slot_t *private_key,
5988 const uint8_t *peer_key,
5989 size_t peer_key_length,
5990 uint8_t *shared_secret,
5991 size_t shared_secret_size,
5992 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005993{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005994 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005995 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005996#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005997 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005998 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005999 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006000 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006001 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006002 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006003 private_key->data.key.data,
6004 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006005 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006006 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006007 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006008 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006009 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006010 shared_secret, shared_secret_size,
6011 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006012 mbedtls_ecp_keypair_free( ecp );
6013 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006014 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006015#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006016 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006017 (void) private_key;
6018 (void) peer_key;
6019 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006020 (void) shared_secret;
6021 (void) shared_secret_size;
6022 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006023 return( PSA_ERROR_NOT_SUPPORTED );
6024 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006025}
6026
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006027/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006028 * to potentially free embedded data structures and wipe confidential data.
6029 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006030static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006031 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006032 psa_key_slot_t *private_key,
6033 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006034 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006035{
6036 psa_status_t status;
6037 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6038 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006039 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006040
6041 /* Step 1: run the secret agreement algorithm to generate the shared
6042 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006043 status = psa_key_agreement_raw_internal( ka_alg,
6044 private_key,
6045 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006046 shared_secret,
6047 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006048 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006049 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006050 goto exit;
6051
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006052 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006053 * the shared secret. A shared secret is permitted wherever a key
6054 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006055 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006056 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006057 shared_secret,
6058 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006059exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006060 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006061 return( status );
6062}
6063
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006064psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006065 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006066 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006067 const uint8_t *peer_key,
6068 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006069{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006070 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6071 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006072 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006073
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006074 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006075 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02006076 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006077 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006078 if( status != PSA_SUCCESS )
6079 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006080 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006081 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006082 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006083 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006084 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006085 else
6086 {
6087 /* If a private key has been added as SECRET, we allow the derived
6088 * key material to be used as a key in PSA Crypto. */
6089 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6090 operation->can_output_key = 1;
6091 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006092
6093 decrement_status = psa_decrement_key_slot_access_count( slot );
6094
6095 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006096}
6097
Gilles Peskinebe697d82019-05-16 18:00:41 +02006098psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006099 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006100 const uint8_t *peer_key,
6101 size_t peer_key_length,
6102 uint8_t *output,
6103 size_t output_size,
6104 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006105{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006106 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6107 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
6108 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006109
6110 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6111 {
6112 status = PSA_ERROR_INVALID_ARGUMENT;
6113 goto exit;
6114 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02006115 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006116 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006117 if( status != PSA_SUCCESS )
6118 goto exit;
6119
6120 status = psa_key_agreement_raw_internal( alg, slot,
6121 peer_key, peer_key_length,
6122 output, output_size,
6123 output_length );
6124
6125exit:
6126 if( status != PSA_SUCCESS )
6127 {
6128 /* If an error happens and is not handled properly, the output
6129 * may be used as a key to protect sensitive data. Arrange for such
6130 * a key to be random, which is likely to result in decryption or
6131 * verification errors. This is better than filling the buffer with
6132 * some constant data such as zeros, which would result in the data
6133 * being protected with a reproducible, easily knowable key.
6134 */
6135 psa_generate_random( output, output_size );
6136 *output_length = output_size;
6137 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006138
6139 decrement_status = psa_decrement_key_slot_access_count( slot );
6140
6141 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006142}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006143
6144
6145/****************************************************************/
6146/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006147/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006148
Gilles Peskine4c317f42018-07-12 01:24:09 +02006149psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006150 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006151{
Janos Follath24eed8d2019-11-22 13:21:35 +00006152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006153 GUARD_MODULE_INITIALIZED;
6154
Gilles Peskinef181eca2019-08-07 13:49:00 +02006155 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6156 {
6157 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6158 output,
6159 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6160 if( ret != 0 )
6161 return( mbedtls_to_psa_error( ret ) );
6162 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6163 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6164 }
6165
Gilles Peskinee59236f2018-01-27 23:32:46 +01006166 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6167 return( mbedtls_to_psa_error( ret ) );
6168}
6169
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006170#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6171#include "mbedtls/entropy_poll.h"
6172
Gilles Peskine7228da22019-07-15 11:06:15 +02006173psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006174 size_t seed_size )
6175{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006176 if( global_data.initialized )
6177 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006178
6179 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6180 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6181 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6182 return( PSA_ERROR_INVALID_ARGUMENT );
6183
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006184 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006185}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006186#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006187
Gilles Peskinee56e8782019-04-26 17:34:02 +02006188#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
6189static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6190 size_t domain_parameters_size,
6191 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006192{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006193 size_t i;
6194 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006195
Gilles Peskinee56e8782019-04-26 17:34:02 +02006196 if( domain_parameters_size == 0 )
6197 {
6198 *exponent = 65537;
6199 return( PSA_SUCCESS );
6200 }
6201
6202 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6203 * support values that fit in a 32-bit integer, which is larger than
6204 * int on just about every platform anyway. */
6205 if( domain_parameters_size > sizeof( acc ) )
6206 return( PSA_ERROR_NOT_SUPPORTED );
6207 for( i = 0; i < domain_parameters_size; i++ )
6208 acc = ( acc << 8 ) | domain_parameters[i];
6209 if( acc > INT_MAX )
6210 return( PSA_ERROR_NOT_SUPPORTED );
6211 *exponent = acc;
6212 return( PSA_SUCCESS );
6213}
6214#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6215
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006216static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006217 psa_key_slot_t *slot, size_t bits,
6218 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006219{
Gilles Peskine8e338702019-07-30 20:06:31 +02006220 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006221
Gilles Peskinee56e8782019-04-26 17:34:02 +02006222 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006223 return( PSA_ERROR_INVALID_ARGUMENT );
6224
Gilles Peskinee59236f2018-01-27 23:32:46 +01006225 if( key_type_is_raw_bytes( type ) )
6226 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006227 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006228
6229 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006230 if( status != PSA_SUCCESS )
6231 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006232
6233 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006234 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6235 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006236 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006237
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006238 status = psa_generate_random( slot->data.key.data,
6239 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006240 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006241 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006242
6243 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006244#if defined(MBEDTLS_DES_C)
6245 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006246 psa_des_set_key_parity( slot->data.key.data,
6247 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006248#endif /* MBEDTLS_DES_C */
6249 }
6250 else
6251
6252#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006253 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006254 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006255 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006257 int exponent;
6258 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006259 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6260 return( PSA_ERROR_NOT_SUPPORTED );
6261 /* Accept only byte-aligned keys, for the same reasons as
6262 * in psa_import_rsa_key(). */
6263 if( bits % 8 != 0 )
6264 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006265 status = psa_read_rsa_exponent( domain_parameters,
6266 domain_parameters_size,
6267 &exponent );
6268 if( status != PSA_SUCCESS )
6269 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006270 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6271 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006272 mbedtls_ctr_drbg_random,
6273 &global_data.ctr_drbg,
6274 (unsigned int) bits,
6275 exponent );
6276 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006277 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006278
6279 /* Make sure to always have an export representation available */
6280 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6281
Steven Cooreman75b74362020-07-28 14:30:13 +02006282 status = psa_allocate_buffer_to_slot( slot, bytes );
6283 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006284 {
6285 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006286 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006287 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006288
6289 status = psa_export_rsa_key( type,
6290 &rsa,
6291 slot->data.key.data,
6292 bytes,
6293 &slot->data.key.bytes );
6294 mbedtls_rsa_free( &rsa );
6295 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006296 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006297 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006298 }
6299 else
6300#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6301
6302#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006303 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006304 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006305 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006306 mbedtls_ecp_group_id grp_id =
6307 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006308 const mbedtls_ecp_curve_info *curve_info =
6309 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006310 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006311 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006312 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006313 return( PSA_ERROR_NOT_SUPPORTED );
6314 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6315 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006316 mbedtls_ecp_keypair_init( &ecp );
6317 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006318 mbedtls_ctr_drbg_random,
6319 &global_data.ctr_drbg );
6320 if( ret != 0 )
6321 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006322 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006323 return( mbedtls_to_psa_error( ret ) );
6324 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006325
6326
6327 /* Make sure to always have an export representation available */
6328 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006329 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6330 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006331 {
6332 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006333 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006334 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006335
6336 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006337 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6338
6339 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006340 if( status != PSA_SUCCESS ) {
6341 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006342 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006343 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006344 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006345 }
6346 else
6347#endif /* MBEDTLS_ECP_C */
Steven Cooreman29149862020-08-05 15:43:42 +02006348 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006349 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006350 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006351
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006352 return( PSA_SUCCESS );
6353}
Darryl Green0c6575a2018-11-07 16:05:30 +00006354
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006355psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006356 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006357{
6358 psa_status_t status;
6359 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006360 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006361
Gilles Peskine0f84d622019-09-12 19:03:13 +02006362 /* Reject any attempt to create a zero-length key so that we don't
6363 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6364 if( psa_get_key_bits( attributes ) == 0 )
6365 return( PSA_ERROR_INVALID_ARGUMENT );
6366
Gilles Peskinedf179142019-07-15 22:02:14 +02006367 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006368 attributes, key, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006369 if( status != PSA_SUCCESS )
6370 goto exit;
6371
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006372 status = psa_driver_wrapper_generate_key( attributes,
6373 slot );
6374 if( status != PSA_ERROR_NOT_SUPPORTED ||
6375 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6376 goto exit;
6377
6378 status = psa_generate_key_internal(
6379 slot, attributes->core.bits,
6380 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006381
6382exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006383 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006384 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006385 if( status != PSA_SUCCESS )
6386 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006387 psa_fail_key_creation( slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02006388 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006389 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006390 else
6391 status = psa_decrement_key_slot_access_count( slot );
6392
Darryl Green0c6575a2018-11-07 16:05:30 +00006393 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006394}
6395
6396
Gilles Peskinee59236f2018-01-27 23:32:46 +01006397
6398/****************************************************************/
6399/* Module setup */
6400/****************************************************************/
6401
Gilles Peskine5e769522018-11-20 21:59:56 +01006402psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6403 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6404 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6405{
6406 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6407 return( PSA_ERROR_BAD_STATE );
6408 global_data.entropy_init = entropy_init;
6409 global_data.entropy_free = entropy_free;
6410 return( PSA_SUCCESS );
6411}
6412
Gilles Peskinee59236f2018-01-27 23:32:46 +01006413void mbedtls_psa_crypto_free( void )
6414{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006415 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006416 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6417 {
6418 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006419 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006420 }
6421 /* Wipe all remaining data, including configuration.
6422 * In particular, this sets all state indicator to the value
6423 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006424 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006425#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006426 /* Unregister all secure element drivers, so that we restart from
6427 * a pristine state. */
6428 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006429#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006430}
6431
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006432#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6433/** Recover a transaction that was interrupted by a power failure.
6434 *
6435 * This function is called during initialization, before psa_crypto_init()
6436 * returns. If this function returns a failure status, the initialization
6437 * fails.
6438 */
6439static psa_status_t psa_crypto_recover_transaction(
6440 const psa_crypto_transaction_t *transaction )
6441{
6442 switch( transaction->unknown.type )
6443 {
6444 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6445 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006446 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006447 * is implemented.
6448 * https://github.com/ARMmbed/mbed-crypto/issues/218
6449 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006450 default:
6451 /* We found an unsupported transaction in the storage.
6452 * We don't know what state the storage is in. Give up. */
6453 return( PSA_ERROR_STORAGE_FAILURE );
6454 }
6455}
6456#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6457
Gilles Peskinee59236f2018-01-27 23:32:46 +01006458psa_status_t psa_crypto_init( void )
6459{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006460 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006461 const unsigned char drbg_seed[] = "PSA";
6462
Gilles Peskinec6b69072018-11-20 21:42:52 +01006463 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006464 if( global_data.initialized != 0 )
6465 return( PSA_SUCCESS );
6466
Gilles Peskine5e769522018-11-20 21:59:56 +01006467 /* Set default configuration if
6468 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6469 if( global_data.entropy_init == NULL )
6470 global_data.entropy_init = mbedtls_entropy_init;
6471 if( global_data.entropy_free == NULL )
6472 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006473
Gilles Peskinec6b69072018-11-20 21:42:52 +01006474 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006475 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006476#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6477 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6478 /* The PSA entropy injection feature depends on using NV seed as an entropy
6479 * source. Add NV seed as an entropy source for PSA entropy injection. */
6480 mbedtls_entropy_add_source( &global_data.entropy,
6481 mbedtls_nv_seed_poll, NULL,
6482 MBEDTLS_ENTROPY_BLOCK_SIZE,
6483 MBEDTLS_ENTROPY_SOURCE_STRONG );
6484#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006485 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006486 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006487 status = mbedtls_to_psa_error(
6488 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6489 mbedtls_entropy_func,
6490 &global_data.entropy,
6491 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6492 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006493 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006494 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006495
Gilles Peskine66fb1262018-12-10 16:29:04 +01006496 status = psa_initialize_key_slots( );
6497 if( status != PSA_SUCCESS )
6498 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006499
Gilles Peskined9348f22019-10-01 15:22:29 +02006500#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6501 status = psa_init_all_se_drivers( );
6502 if( status != PSA_SUCCESS )
6503 goto exit;
6504#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6505
Gilles Peskine4b734222019-07-24 15:56:31 +02006506#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006507 status = psa_crypto_load_transaction( );
6508 if( status == PSA_SUCCESS )
6509 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006510 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6511 if( status != PSA_SUCCESS )
6512 goto exit;
6513 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006514 }
6515 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6516 {
6517 /* There's no transaction to complete. It's all good. */
6518 status = PSA_SUCCESS;
6519 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006520#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006521
Gilles Peskinec6b69072018-11-20 21:42:52 +01006522 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006523 global_data.initialized = 1;
6524
6525exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006526 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006527 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006528 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006529}
6530
6531#endif /* MBEDTLS_PSA_CRYPTO_C */