blob: ac525d538dbfc82848f44651ddd2912b775a4d44 [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 *
Ronald Cron7587ae42020-11-11 15:04:25 +01001194 * In case of a persistent key, the function loads the description of the key
1195 * into a key slot if not already done.
1196 *
Ronald Cronf95a2b12020-10-22 15:24:49 +02001197 * On success, the access counter of the returned key slot is incremented by
1198 * one. It is the responsibility of the caller to call
1199 * psa_decrement_key_slot_access_count() when it does not access the key slot
1200 * anymore.
1201 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001202static psa_status_t psa_get_key_from_slot( mbedtls_svc_key_id_t key,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001203 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001204 psa_key_usage_t usage,
1205 psa_algorithm_t alg )
1206{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001207 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1208 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001209
Ronald Cronf95a2b12020-10-22 15:24:49 +02001210 status = psa_get_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001211 if( status != PSA_SUCCESS )
1212 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001213 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001214
1215 /* Enforce that usage policy for the key slot contains all the flags
1216 * required by the usage parameter. There is one exception: public
1217 * keys can always be exported, so we treat public key objects as
1218 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001219 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001220 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001221
1222 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001223 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001224 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001225
1226 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001227 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001228 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001229
Darryl Green06fd18d2018-07-16 11:21:11 +01001230 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001231
1232error:
1233 *p_slot = NULL;
1234 psa_decrement_key_slot_access_count( slot );
1235
1236 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001237}
Darryl Green940d72c2018-07-13 13:18:51 +01001238
Gilles Peskine28f8f302019-07-24 13:30:31 +02001239/** Retrieve a slot which must contain a transparent key.
1240 *
1241 * A transparent key is a key for which the key material is directly
1242 * available, as opposed to a key in a secure element.
1243 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001244 * This is a temporary function to use instead of psa_get_key_from_slot()
1245 * until secure element support is fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001246 *
1247 * On success, the access counter of the returned key slot is incremented by
1248 * one. It is the responsibility of the caller to call
1249 * psa_decrement_key_slot_access_count() when it does not access the key slot
1250 * anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001251 */
1252#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001253static psa_status_t psa_get_transparent_key( mbedtls_svc_key_id_t key,
Gilles Peskine28f8f302019-07-24 13:30:31 +02001254 psa_key_slot_t **p_slot,
1255 psa_key_usage_t usage,
1256 psa_algorithm_t alg )
1257{
Ronald Croncf56a0a2020-08-04 09:51:30 +02001258 psa_status_t status = psa_get_key_from_slot( key, p_slot, usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001259 if( status != PSA_SUCCESS )
1260 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001261
Gilles Peskineadad8132019-07-25 11:31:23 +02001262 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001263 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02001264 psa_decrement_key_slot_access_count( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001265 *p_slot = NULL;
1266 return( PSA_ERROR_NOT_SUPPORTED );
1267 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001268
Gilles Peskine28f8f302019-07-24 13:30:31 +02001269 return( PSA_SUCCESS );
1270}
1271#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1272/* With no secure element support, all keys are transparent. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001273#define psa_get_transparent_key( key, p_slot, usage, alg ) \
1274 psa_get_key_from_slot( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001275#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1276
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001277/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001278static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001279{
Gilles Peskine73167e12019-07-12 23:44:37 +02001280#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1281 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001282 {
1283 /* No key material to clean. */
1284 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001285 else
1286#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001287 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001288 /* Data pointer will always be either a valid pointer or NULL in an
1289 * initialized slot, so we can just free it. */
1290 if( slot->data.key.data != NULL )
1291 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001292 mbedtls_free( slot->data.key.data );
1293 slot->data.key.data = NULL;
1294 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001295 }
Darryl Green40225ba2018-11-15 14:48:15 +00001296
1297 return( PSA_SUCCESS );
1298}
1299
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001300/** Completely wipe a slot in memory, including its policy.
1301 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001302psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001303{
1304 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001305
1306 /*
1307 * As the return error code may not be handled in case of multiple errors,
1308 * do our best to report an unexpected access counter: if available
1309 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1310 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001311 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001312 */
1313 if( slot->access_count != 1 )
1314 {
1315#ifdef MBEDTLS_CHECK_PARAMS
1316 MBEDTLS_PARAM_FAILED( slot->access_count == 1 );
1317#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001318 status = PSA_ERROR_CORRUPTION_DETECTED;
1319 }
1320
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001321 /* Multipart operations may still be using the key. This is safe
1322 * because all multipart operation objects are independent from
1323 * the key slot: if they need to access the key after the setup
1324 * phase, they have a copy of the key. Note that this means that
1325 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001326 /* At this point, key material and other type-specific content has
1327 * been wiped. Clear remaining metadata. We can call memset and not
1328 * zeroize because the metadata is not particularly sensitive. */
1329 memset( slot, 0, sizeof( *slot ) );
1330 return( status );
1331}
1332
Ronald Croncf56a0a2020-08-04 09:51:30 +02001333psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001334{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001335 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001336 psa_status_t status; /* status of the last operation */
1337 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001338#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1339 psa_se_drv_table_entry_t *driver;
1340#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001341
Ronald Croncf56a0a2020-08-04 09:51:30 +02001342 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001343 return( PSA_SUCCESS );
1344
Ronald Cronf2911112020-10-29 17:51:10 +01001345 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001346 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001347 * key, this will load the key description from persistent memory if not
1348 * done yet. We cannot avoid this loading as without it we don't know if
1349 * the key is operated by an SE or not and this information is needed by
1350 * the current implementation.
1351 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001352 status = psa_get_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001353 if( status != PSA_SUCCESS )
1354 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001355
Ronald Cronf2911112020-10-29 17:51:10 +01001356 /*
1357 * If the key slot containing the key description is under access by the
1358 * library (apart from the present access), the key cannot be destroyed
1359 * yet. For the time being, just return in error. Eventually (to be
1360 * implemented), the key should be destroyed when all accesses have
1361 * stopped.
1362 */
1363 if( slot->access_count > 1 )
1364 {
1365 psa_decrement_key_slot_access_count( slot );
1366 return( PSA_ERROR_GENERIC_ERROR );
1367 }
1368
Gilles Peskine354f7672019-07-12 23:46:38 +02001369#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001370 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001371 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001372 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001373 /* For a key in a secure element, we need to do three things:
1374 * remove the key file in internal storage, destroy the
1375 * key inside the secure element, and update the driver's
1376 * persistent data. Start a transaction that will encompass these
1377 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001378 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001379 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001380 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001381 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001382 status = psa_crypto_save_transaction( );
1383 if( status != PSA_SUCCESS )
1384 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001385 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001386 /* We should still try to destroy the key in the secure
1387 * element and the key metadata in storage. This is especially
1388 * important if the error is that the storage is full.
1389 * But how to do it exactly without risking an inconsistent
1390 * state after a reset?
1391 * https://github.com/ARMmbed/mbed-crypto/issues/215
1392 */
1393 overall_status = status;
1394 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001395 }
1396
Gilles Peskine354f7672019-07-12 23:46:38 +02001397 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001398 if( overall_status == PSA_SUCCESS )
1399 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001400 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001401#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1402
Darryl Greend49a4992018-06-18 17:27:26 +01001403#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001404 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001405 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001406 status = psa_destroy_persistent_key( slot->attr.id );
1407 if( overall_status == PSA_SUCCESS )
1408 overall_status = status;
1409
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001410 /* TODO: other slots may have a copy of the same key. We should
1411 * invalidate them.
1412 * https://github.com/ARMmbed/mbed-crypto/issues/214
1413 */
Darryl Greend49a4992018-06-18 17:27:26 +01001414 }
1415#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001416
Gilles Peskinefc762652019-07-22 19:30:34 +02001417#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1418 if( driver != NULL )
1419 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001420 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001421 if( overall_status == PSA_SUCCESS )
1422 overall_status = status;
1423 status = psa_crypto_stop_transaction( );
1424 if( overall_status == PSA_SUCCESS )
1425 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001426 }
1427#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1428
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001429#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1430exit:
1431#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001432 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001433 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1434 if( overall_status == PSA_SUCCESS )
1435 overall_status = status;
1436 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001437}
1438
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001439void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001440{
Gilles Peskineb699f072019-04-26 16:06:02 +02001441 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001442 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001443}
1444
Gilles Peskineb699f072019-04-26 16:06:02 +02001445psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1446 psa_key_type_t type,
1447 const uint8_t *data,
1448 size_t data_length )
1449{
1450 uint8_t *copy = NULL;
1451
1452 if( data_length != 0 )
1453 {
1454 copy = mbedtls_calloc( 1, data_length );
1455 if( copy == NULL )
1456 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1457 memcpy( copy, data, data_length );
1458 }
1459 /* After this point, this function is guaranteed to succeed, so it
1460 * can start modifying `*attributes`. */
1461
1462 if( attributes->domain_parameters != NULL )
1463 {
1464 mbedtls_free( attributes->domain_parameters );
1465 attributes->domain_parameters = NULL;
1466 attributes->domain_parameters_size = 0;
1467 }
1468
1469 attributes->domain_parameters = copy;
1470 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001471 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001472 return( PSA_SUCCESS );
1473}
1474
1475psa_status_t psa_get_key_domain_parameters(
1476 const psa_key_attributes_t *attributes,
1477 uint8_t *data, size_t data_size, size_t *data_length )
1478{
1479 if( attributes->domain_parameters_size > data_size )
1480 return( PSA_ERROR_BUFFER_TOO_SMALL );
1481 *data_length = attributes->domain_parameters_size;
1482 if( attributes->domain_parameters_size != 0 )
1483 memcpy( data, attributes->domain_parameters,
1484 attributes->domain_parameters_size );
1485 return( PSA_SUCCESS );
1486}
1487
1488#if defined(MBEDTLS_RSA_C)
1489static psa_status_t psa_get_rsa_public_exponent(
1490 const mbedtls_rsa_context *rsa,
1491 psa_key_attributes_t *attributes )
1492{
1493 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001494 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001495 uint8_t *buffer = NULL;
1496 size_t buflen;
1497 mbedtls_mpi_init( &mpi );
1498
1499 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1500 if( ret != 0 )
1501 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001502 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1503 {
1504 /* It's the default value, which is reported as an empty string,
1505 * so there's nothing to do. */
1506 goto exit;
1507 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001508
1509 buflen = mbedtls_mpi_size( &mpi );
1510 buffer = mbedtls_calloc( 1, buflen );
1511 if( buffer == NULL )
1512 {
1513 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1514 goto exit;
1515 }
1516 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1517 if( ret != 0 )
1518 goto exit;
1519 attributes->domain_parameters = buffer;
1520 attributes->domain_parameters_size = buflen;
1521
1522exit:
1523 mbedtls_mpi_free( &mpi );
1524 if( ret != 0 )
1525 mbedtls_free( buffer );
1526 return( mbedtls_to_psa_error( ret ) );
1527}
1528#endif /* MBEDTLS_RSA_C */
1529
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001530/** Retrieve all the publicly-accessible attributes of a key.
1531 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001532psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001533 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001534{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001535 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1536 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001537 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001538
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001539 psa_reset_key_attributes( attributes );
1540
Ronald Croncf56a0a2020-08-04 09:51:30 +02001541 status = psa_get_key_from_slot( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001542 if( status != PSA_SUCCESS )
1543 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001544
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001545 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001546 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1547 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1548
1549#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1550 if( psa_key_slot_is_external( slot ) )
1551 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1552#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001553
Gilles Peskine8e338702019-07-30 20:06:31 +02001554 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001555 {
1556#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001557 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001558 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001559#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001560 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001561 * is not yet implemented.
1562 * https://github.com/ARMmbed/mbed-crypto/issues/216
1563 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001564 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001565 break;
1566#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001567 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001568 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001569
Steven Cooreman4fed4552020-08-03 14:46:03 +02001570 status = psa_load_rsa_representation( slot->attr.type,
1571 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001572 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001573 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001574 if( status != PSA_SUCCESS )
1575 break;
1576
Steven Cooremana2371e52020-07-28 14:30:39 +02001577 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001578 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001579 mbedtls_rsa_free( rsa );
1580 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001581 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001582 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001583#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001584 default:
1585 /* Nothing else to do. */
1586 break;
1587 }
1588
1589 if( status != PSA_SUCCESS )
1590 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001591
1592 decrement_status = psa_decrement_key_slot_access_count( slot );
1593
1594 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001595}
1596
Gilles Peskinec8000c02019-08-02 20:15:51 +02001597#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1598psa_status_t psa_get_key_slot_number(
1599 const psa_key_attributes_t *attributes,
1600 psa_key_slot_number_t *slot_number )
1601{
1602 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1603 {
1604 *slot_number = attributes->slot_number;
1605 return( PSA_SUCCESS );
1606 }
1607 else
1608 return( PSA_ERROR_INVALID_ARGUMENT );
1609}
1610#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1611
Steven Cooremana01795d2020-07-24 22:48:15 +02001612static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1613 uint8_t *data,
1614 size_t data_size,
1615 size_t *data_length )
1616{
1617 if( slot->data.key.bytes > data_size )
1618 return( PSA_ERROR_BUFFER_TOO_SMALL );
1619 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1620 memset( data + slot->data.key.bytes, 0,
1621 data_size - slot->data.key.bytes );
1622 *data_length = slot->data.key.bytes;
1623 return( PSA_SUCCESS );
1624}
1625
Gilles Peskinef603c712019-01-19 13:40:11 +01001626static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1627 uint8_t *data,
1628 size_t data_size,
1629 size_t *data_length,
1630 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001631{
Gilles Peskine5d309672019-07-12 23:47:28 +02001632#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1633 const psa_drv_se_t *drv;
1634 psa_drv_se_context_t *drv_context;
1635#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1636
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001637 *data_length = 0;
1638
Gilles Peskine8e338702019-07-30 20:06:31 +02001639 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001640 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001641
Gilles Peskinef9168942019-09-12 19:20:29 +02001642 /* Reject a zero-length output buffer now, since this can never be a
1643 * valid key representation. This way we know that data must be a valid
1644 * pointer and we can do things like memset(data, ..., data_size). */
1645 if( data_size == 0 )
1646 return( PSA_ERROR_BUFFER_TOO_SMALL );
1647
Gilles Peskine5d309672019-07-12 23:47:28 +02001648#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001649 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001650 {
1651 psa_drv_se_export_key_t method;
1652 if( drv->key_management == NULL )
1653 return( PSA_ERROR_NOT_SUPPORTED );
1654 method = ( export_public_key ?
1655 drv->key_management->p_export_public :
1656 drv->key_management->p_export );
1657 if( method == NULL )
1658 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001659 return( method( drv_context,
1660 slot->data.se.slot_number,
1661 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001662 }
1663#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1664
Gilles Peskine8e338702019-07-30 20:06:31 +02001665 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001666 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001667 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001668 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001669 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1670 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001671 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001672 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001673 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001674 /* Exporting public -> public */
1675 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1676 }
1677 else if( !export_public_key )
1678 {
1679 /* Exporting private -> private */
1680 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1681 }
1682 /* Need to export the public part of a private key,
1683 * so conversion is needed */
1684 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1685 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001686#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001687 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001688 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001689 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001690 slot->data.key.data,
1691 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001692 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001693 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001694 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001695
Steven Cooreman560c28a2020-07-24 23:20:24 +02001696 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001697 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001698 data,
1699 data_size,
1700 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001701
Steven Cooremana2371e52020-07-28 14:30:39 +02001702 mbedtls_rsa_free( rsa );
1703 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001704
Steven Cooreman560c28a2020-07-24 23:20:24 +02001705 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001706#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001707 /* We don't know how to convert a private RSA key to public. */
1708 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001709#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001710 }
1711 else
Moran Pekera998bc62018-04-16 18:16:20 +03001712 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001713#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001714 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001715 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001716 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001717 slot->data.key.data,
1718 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001719 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001720 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001721 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001722
1723 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1724 PSA_KEY_TYPE_ECC_GET_FAMILY(
1725 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001726 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001727 data,
1728 data_size,
1729 data_length );
1730
Steven Cooremana2371e52020-07-28 14:30:39 +02001731 mbedtls_ecp_keypair_free( ecp );
1732 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001733 return( status );
1734#else
1735 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001736 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001737#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001738 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001739 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001740 else
1741 {
1742 /* This shouldn't happen in the reference implementation, but
1743 it is valid for a special-purpose implementation to omit
1744 support for exporting certain key types. */
1745 return( PSA_ERROR_NOT_SUPPORTED );
1746 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001747}
1748
Ronald Croncf56a0a2020-08-04 09:51:30 +02001749psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001750 uint8_t *data,
1751 size_t data_size,
1752 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001753{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001754 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1755 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001756 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001757
1758 /* Set the key to empty now, so that even when there are errors, we always
1759 * set data_length to a value between 0 and data_size. On error, setting
1760 * the key to empty is a good choice because an empty key representation is
1761 * unlikely to be accepted anywhere. */
1762 *data_length = 0;
1763
1764 /* Export requires the EXPORT flag. There is an exception for public keys,
1765 * which don't require any flag, but psa_get_key_from_slot takes
1766 * care of this. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001767 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001768 if( status != PSA_SUCCESS )
1769 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001770
1771 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
1772 decrement_status = psa_decrement_key_slot_access_count( slot );
1773
1774 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001775}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001776
Ronald Croncf56a0a2020-08-04 09:51:30 +02001777psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001778 uint8_t *data,
1779 size_t data_size,
1780 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001781{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001782 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1783 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001784 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001785
1786 /* Set the key to empty now, so that even when there are errors, we always
1787 * set data_length to a value between 0 and data_size. On error, setting
1788 * the key to empty is a good choice because an empty key representation is
1789 * unlikely to be accepted anywhere. */
1790 *data_length = 0;
1791
1792 /* Exporting a public key doesn't require a usage flag. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001793 status = psa_get_key_from_slot( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001794 if( status != PSA_SUCCESS )
1795 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001796
1797 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
1798 decrement_status = psa_decrement_key_slot_access_count( slot );
1799
1800 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001801}
1802
Gilles Peskine91e8c332019-08-02 19:19:39 +02001803#if defined(static_assert)
1804static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1805 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001806static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001807 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001808static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001809 "One or more key attribute flag is listed as both internal-only and external-only" );
1810#endif
1811
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001812/** Validate that a key policy is internally well-formed.
1813 *
1814 * This function only rejects invalid policies. It does not validate the
1815 * consistency of the policy with respect to other attributes of the key
1816 * such as the key type.
1817 */
1818static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001819{
1820 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001821 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001822 PSA_KEY_USAGE_ENCRYPT |
1823 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001824 PSA_KEY_USAGE_SIGN_HASH |
1825 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001826 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1827 return( PSA_ERROR_INVALID_ARGUMENT );
1828
Gilles Peskine4747d192019-04-17 15:05:45 +02001829 return( PSA_SUCCESS );
1830}
1831
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001832/** Validate the internal consistency of key attributes.
1833 *
1834 * This function only rejects invalid attribute values. If does not
1835 * validate the consistency of the attributes with any key data that may
1836 * be involved in the creation of the key.
1837 *
1838 * Call this function early in the key creation process.
1839 *
1840 * \param[in] attributes Key attributes for the new key.
1841 * \param[out] p_drv On any return, the driver for the key, if any.
1842 * NULL for a transparent key.
1843 *
1844 */
1845static psa_status_t psa_validate_key_attributes(
1846 const psa_key_attributes_t *attributes,
1847 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001848{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001849 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001850 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001851 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001852
Ronald Cron54b90082020-10-29 15:26:43 +01001853 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001854 if( status != PSA_SUCCESS )
1855 return( status );
1856
Ronald Crond2ed4812020-07-17 16:11:30 +02001857 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001858 if( status != PSA_SUCCESS )
1859 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001860
Ronald Cron65f38a32020-10-23 17:11:13 +02001861 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1862 {
1863 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1864 return( PSA_ERROR_INVALID_ARGUMENT );
1865 }
1866 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001867 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001868 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001869 if( status != PSA_SUCCESS )
1870 return( status );
1871 }
1872
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001873 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001874 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001875 return( status );
1876
1877 /* Refuse to create overly large keys.
1878 * Note that this doesn't trigger on import if the attributes don't
1879 * explicitly specify a size (so psa_get_key_bits returns 0), so
1880 * psa_import_key() needs its own checks. */
1881 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1882 return( PSA_ERROR_NOT_SUPPORTED );
1883
Gilles Peskine91e8c332019-08-02 19:19:39 +02001884 /* Reject invalid flags. These should not be reachable through the API. */
1885 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1886 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1887 return( PSA_ERROR_INVALID_ARGUMENT );
1888
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001889 return( PSA_SUCCESS );
1890}
1891
Gilles Peskine4747d192019-04-17 15:05:45 +02001892/** Prepare a key slot to receive key material.
1893 *
1894 * This function allocates a key slot and sets its metadata.
1895 *
1896 * If this function fails, call psa_fail_key_creation().
1897 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001898 * This function is intended to be used as follows:
1899 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001900 * it with the specified attributes, and in case of a volatile key assign it
1901 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001902 * -# Populate the slot with the key material.
1903 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1904 * In case of failure at any step, stop the sequence and call
1905 * psa_fail_key_creation().
1906 *
Ronald Cronf95a2b12020-10-22 15:24:49 +02001907 * On success, the access counter of the returned key slot is incremented by
1908 * one. It is the responsibility of the caller to call
1909 * psa_decrement_key_slot_access_count() when it does not access the key slot
1910 * anymore.
1911 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001912 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001913 * \param[in] attributes Key attributes for the new key.
Ronald Cron3c76a422020-10-16 12:17:04 +02001914 * \param[out] key On success, identifier of the key. Note that the
1915 * key identifier is also stored in the prepared
1916 * slot.
Gilles Peskine011e4282019-06-26 18:34:38 +02001917 * \param[out] p_slot On success, a pointer to the prepared slot.
1918 * \param[out] p_drv On any return, the driver for the key, if any.
1919 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001920 *
1921 * \retval #PSA_SUCCESS
1922 * The key slot is ready to receive key material.
1923 * \return If this function fails, the key slot is an invalid state.
1924 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001925 */
1926static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001927 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001928 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001929 mbedtls_svc_key_id_t *key,
Gilles Peskine011e4282019-06-26 18:34:38 +02001930 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001931 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001932{
1933 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001934 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001935 psa_key_slot_t *slot;
1936
Gilles Peskinedf179142019-07-15 22:02:14 +02001937 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001938 *p_drv = NULL;
1939
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001940 status = psa_validate_key_attributes( attributes, p_drv );
1941 if( status != PSA_SUCCESS )
1942 return( status );
1943
Ronald Cronc4d1b512020-07-31 11:26:37 +02001944 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001945 if( status != PSA_SUCCESS )
1946 return( status );
1947 slot = *p_slot;
1948
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001949 /* We're storing the declared bit-size of the key. It's up to each
1950 * creation mechanism to verify that this information is correct.
1951 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001952 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001953 * is optional (import, copy). In case of a volatile key, assign it the
1954 * volatile key identifier associated to the slot returned to contain its
1955 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001956
1957 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001958 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1959 {
1960#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1961 slot->attr.id = volatile_key_id;
1962#else
1963 slot->attr.id.key_id = volatile_key_id;
1964#endif
1965 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001966
Gilles Peskine91e8c332019-08-02 19:19:39 +02001967 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001968 * external-only flags, query `attributes`. Thanks to the check
1969 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001970 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001971 * may have set. */
1972 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001973
Gilles Peskinecbaff462019-07-12 23:46:04 +02001974#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001975 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001976 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001977 * create the key file in internal storage, create the
1978 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001979 * persistent data. This is done by starting a transaction that will
1980 * encompass these three actions.
1981 * For registering a volatile key, we just need to find an appropriate
1982 * slot number inside the SE. Since the key is designated volatile, creating
1983 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001984 /* The first thing to do is to find a slot number for the new key.
1985 * We save the slot number in persistent storage as part of the
1986 * transaction data. It will be needed to recover if the power
1987 * fails during the key creation process, to clean up on the secure
1988 * element side after restarting. Obtaining a slot number from the
1989 * secure element driver updates its persistent state, but we do not yet
1990 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001991 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001992 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001993 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001994 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001995 &slot->data.se.slot_number );
1996 if( status != PSA_SUCCESS )
1997 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001998
1999 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002000 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002001 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2002 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
2003 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
2004 psa_crypto_transaction.key.id = slot->attr.id;
2005 status = psa_crypto_save_transaction( );
2006 if( status != PSA_SUCCESS )
2007 {
2008 (void) psa_crypto_stop_transaction( );
2009 return( status );
2010 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002011 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002012 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002013
2014 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2015 {
2016 /* Key registration only makes sense with a secure element. */
2017 return( PSA_ERROR_INVALID_ARGUMENT );
2018 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002019#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2020
Ronald Croncf56a0a2020-08-04 09:51:30 +02002021 *key = slot->attr.id;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002022 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002023}
Gilles Peskine4747d192019-04-17 15:05:45 +02002024
2025/** Finalize the creation of a key once its key material has been set.
2026 *
2027 * This entails writing the key to persistent storage.
2028 *
2029 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002030 * See the documentation of psa_start_key_creation() for the intended use
2031 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002032 *
Ronald Cron50972942020-11-14 11:28:25 +01002033 * If the finalization succeeds, the function decreases the slot access
2034 * counter (that was incremented as part of psa_start_key_creation()) and the
2035 * slot cannot be accessed anymore as part of the key creation process.
2036 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002037 * \param[in,out] slot Pointer to the slot with key material.
2038 * \param[in] driver The secure element driver for the key,
2039 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002040 *
2041 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002042 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002043 * \return If this function fails, the key slot is an invalid state.
2044 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002045 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002046static psa_status_t psa_finish_key_creation(
2047 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002048 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002049{
2050 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002051 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002052 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002053
2054#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002055 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002056 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002057#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2058 if( driver != NULL )
2059 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002060 psa_se_key_data_storage_t data;
2061#if defined(static_assert)
2062 static_assert( sizeof( slot->data.se.slot_number ) ==
2063 sizeof( data.slot_number ),
2064 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002065#endif
2066 memcpy( &data.slot_number, &slot->data.se.slot_number,
2067 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002068 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002069 (uint8_t*) &data,
2070 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002071 }
2072 else
2073#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2074 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002075 /* Key material is saved in export representation in the slot, so
2076 * just pass the slot buffer for storage. */
2077 status = psa_save_persistent_key( &slot->attr,
2078 slot->data.key.data,
2079 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002080 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002081 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002082#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2083
Gilles Peskinecbaff462019-07-12 23:46:04 +02002084#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002085 /* Finish the transaction for a key creation. This does not
2086 * happen when registering an existing key. Detect this case
2087 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002088 * creation of a persistent key in a secure element requires a transaction,
2089 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002090 if( driver != NULL &&
2091 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002092 {
2093 status = psa_save_se_persistent_data( driver );
2094 if( status != PSA_SUCCESS )
2095 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002096 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002097 return( status );
2098 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002099 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002100 }
2101#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2102
Ronald Cron50972942020-11-14 11:28:25 +01002103 if( status == PSA_SUCCESS )
2104 status = psa_decrement_key_slot_access_count( slot );
2105
Gilles Peskine4747d192019-04-17 15:05:45 +02002106 return( status );
2107}
2108
2109/** Abort the creation of a key.
2110 *
2111 * You may call this function after calling psa_start_key_creation(),
2112 * or after psa_finish_key_creation() fails. In other circumstances, this
2113 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002114 * See the documentation of psa_start_key_creation() for the intended use
2115 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002116 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002117 * \param[in,out] slot Pointer to the slot with key material.
2118 * \param[in] driver The secure element driver for the key,
2119 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002120 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002121static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002122 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002123{
Gilles Peskine011e4282019-06-26 18:34:38 +02002124 (void) driver;
2125
Gilles Peskine4747d192019-04-17 15:05:45 +02002126 if( slot == NULL )
2127 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002128
2129#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002130 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002131 * element, and the failure happened later (when saving metadata
2132 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002133 * element.
2134 * https://github.com/ARMmbed/mbed-crypto/issues/217
2135 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002136
Gilles Peskined7729582019-08-05 15:55:54 +02002137 /* Abort the ongoing transaction if any (there may not be one if
2138 * the creation process failed before starting one, or if the
2139 * key creation is a registration of a key in a secure element).
2140 * Earlier functions must already have done what it takes to undo any
2141 * partial creation. All that's left is to update the transaction data
2142 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002143 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002144#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2145
Gilles Peskine4747d192019-04-17 15:05:45 +02002146 psa_wipe_key_slot( slot );
2147}
2148
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002149/** Validate optional attributes during key creation.
2150 *
2151 * Some key attributes are optional during key creation. If they are
2152 * specified in the attributes structure, check that they are consistent
2153 * with the data in the slot.
2154 *
2155 * This function should be called near the end of key creation, after
2156 * the slot in memory is fully populated but before saving persistent data.
2157 */
2158static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002159 const psa_key_slot_t *slot,
2160 const psa_key_attributes_t *attributes )
2161{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002162 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002163 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002164 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002165 return( PSA_ERROR_INVALID_ARGUMENT );
2166 }
2167
2168 if( attributes->domain_parameters_size != 0 )
2169 {
2170#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002171 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002172 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002173 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002174 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002175 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002176
Steven Cooreman7f391872020-07-30 14:57:44 +02002177 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002178 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002179 slot->data.key.data,
2180 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002181 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002182 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002183 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002184
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002185 mbedtls_mpi_init( &actual );
2186 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002187 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002188 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002189 mbedtls_rsa_free( rsa );
2190 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002191 if( ret != 0 )
2192 goto rsa_exit;
2193 ret = mbedtls_mpi_read_binary( &required,
2194 attributes->domain_parameters,
2195 attributes->domain_parameters_size );
2196 if( ret != 0 )
2197 goto rsa_exit;
2198 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2199 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2200 rsa_exit:
2201 mbedtls_mpi_free( &actual );
2202 mbedtls_mpi_free( &required );
2203 if( ret != 0)
2204 return( mbedtls_to_psa_error( ret ) );
2205 }
2206 else
2207#endif
2208 {
2209 return( PSA_ERROR_INVALID_ARGUMENT );
2210 }
2211 }
2212
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002213 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002214 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002215 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002216 return( PSA_ERROR_INVALID_ARGUMENT );
2217 }
2218
2219 return( PSA_SUCCESS );
2220}
2221
Gilles Peskine4747d192019-04-17 15:05:45 +02002222psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002223 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002224 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002225 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002226{
2227 psa_status_t status;
2228 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002229 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002230
Gilles Peskine0f84d622019-09-12 19:03:13 +02002231 /* Reject zero-length symmetric keys (including raw data key objects).
2232 * This also rejects any key which might be encoded as an empty string,
2233 * which is never valid. */
2234 if( data_length == 0 )
2235 return( PSA_ERROR_INVALID_ARGUMENT );
2236
Gilles Peskinedf179142019-07-15 22:02:14 +02002237 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002238 key, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002239 if( status != PSA_SUCCESS )
2240 goto exit;
2241
Gilles Peskine5d309672019-07-12 23:47:28 +02002242#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2243 if( driver != NULL )
2244 {
2245 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002246 /* The driver should set the number of key bits, however in
2247 * case it doesn't, we initialize bits to an invalid value. */
2248 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002249 if( drv->key_management == NULL ||
2250 drv->key_management->p_import == NULL )
2251 {
2252 status = PSA_ERROR_NOT_SUPPORTED;
2253 goto exit;
2254 }
2255 status = drv->key_management->p_import(
2256 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002257 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002258 &bits );
2259 if( status != PSA_SUCCESS )
2260 goto exit;
2261 if( bits > PSA_MAX_KEY_BITS )
2262 {
2263 status = PSA_ERROR_NOT_SUPPORTED;
2264 goto exit;
2265 }
2266 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002267 }
2268 else
2269#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2270 {
2271 status = psa_import_key_into_slot( slot, data, data_length );
2272 if( status != PSA_SUCCESS )
2273 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002274 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002275 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002276 if( status != PSA_SUCCESS )
2277 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002278
Gilles Peskine011e4282019-06-26 18:34:38 +02002279 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002280exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002281 if( status != PSA_SUCCESS )
2282 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002283 psa_fail_key_creation( slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002284 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4747d192019-04-17 15:05:45 +02002285 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002286
Gilles Peskine4747d192019-04-17 15:05:45 +02002287 return( status );
2288}
2289
Gilles Peskined7729582019-08-05 15:55:54 +02002290#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2291psa_status_t mbedtls_psa_register_se_key(
2292 const psa_key_attributes_t *attributes )
2293{
2294 psa_status_t status;
2295 psa_key_slot_t *slot = NULL;
2296 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002297 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002298
2299 /* Leaving attributes unspecified is not currently supported.
2300 * It could make sense to query the key type and size from the
2301 * secure element, but not all secure elements support this
2302 * and the driver HAL doesn't currently support it. */
2303 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2304 return( PSA_ERROR_NOT_SUPPORTED );
2305 if( psa_get_key_bits( attributes ) == 0 )
2306 return( PSA_ERROR_NOT_SUPPORTED );
2307
2308 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002309 &key, &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002310 if( status != PSA_SUCCESS )
2311 goto exit;
2312
Gilles Peskined7729582019-08-05 15:55:54 +02002313 status = psa_finish_key_creation( slot, driver );
2314
2315exit:
2316 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002317 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002318
Gilles Peskined7729582019-08-05 15:55:54 +02002319 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002320 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002321 return( status );
2322}
2323#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2324
Gilles Peskinef603c712019-01-19 13:40:11 +01002325static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002326 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002327{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002328 psa_status_t status = psa_copy_key_material_into_slot( target,
2329 source->data.key.data,
2330 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002331 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002332 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002333
Steven Cooreman398aee52020-10-13 14:35:45 +02002334 target->attr.type = source->attr.type;
2335 target->attr.bits = source->attr.bits;
2336
2337 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002338}
2339
Ronald Croncf56a0a2020-08-04 09:51:30 +02002340psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002341 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002342 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002343{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002344 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2345 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002346 psa_key_slot_t *source_slot = NULL;
2347 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002348 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002349 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002350
Ronald Croncf56a0a2020-08-04 09:51:30 +02002351 status = psa_get_transparent_key( source_key, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002352 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002353 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002354 goto exit;
2355
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002356 status = psa_validate_optional_attributes( source_slot,
2357 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002358 if( status != PSA_SUCCESS )
2359 goto exit;
2360
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002361 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002362 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002363 if( status != PSA_SUCCESS )
2364 goto exit;
2365
Gilles Peskinedf179142019-07-15 22:02:14 +02002366 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2367 &actual_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002368 target_key, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002369 if( status != PSA_SUCCESS )
2370 goto exit;
2371
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002372#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2373 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002374 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002375 /* Copying to a secure element is not implemented yet. */
2376 status = PSA_ERROR_NOT_SUPPORTED;
2377 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002378 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002379#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002380
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002381 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002382 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002383 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002384
Gilles Peskine011e4282019-06-26 18:34:38 +02002385 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002386exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002387 if( status != PSA_SUCCESS )
2388 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002389 psa_fail_key_creation( target_slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002390 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002391 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002392
2393 decrement_status = psa_decrement_key_slot_access_count( source_slot );
2394
2395 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002396}
2397
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002398
2399
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002400/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002401/* Message digests */
2402/****************************************************************/
2403
John Durkop0ea39e02020-10-13 19:58:20 -07002404#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002405static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002406{
2407 switch( alg )
2408 {
2409#if defined(MBEDTLS_MD2_C)
2410 case PSA_ALG_MD2:
2411 return( &mbedtls_md2_info );
2412#endif
2413#if defined(MBEDTLS_MD4_C)
2414 case PSA_ALG_MD4:
2415 return( &mbedtls_md4_info );
2416#endif
2417#if defined(MBEDTLS_MD5_C)
2418 case PSA_ALG_MD5:
2419 return( &mbedtls_md5_info );
2420#endif
2421#if defined(MBEDTLS_RIPEMD160_C)
2422 case PSA_ALG_RIPEMD160:
2423 return( &mbedtls_ripemd160_info );
2424#endif
2425#if defined(MBEDTLS_SHA1_C)
2426 case PSA_ALG_SHA_1:
2427 return( &mbedtls_sha1_info );
2428#endif
2429#if defined(MBEDTLS_SHA256_C)
2430 case PSA_ALG_SHA_224:
2431 return( &mbedtls_sha224_info );
2432 case PSA_ALG_SHA_256:
2433 return( &mbedtls_sha256_info );
2434#endif
2435#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002436#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002437 case PSA_ALG_SHA_384:
2438 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002439#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002440 case PSA_ALG_SHA_512:
2441 return( &mbedtls_sha512_info );
2442#endif
2443 default:
2444 return( NULL );
2445 }
2446}
John Durkop0ea39e02020-10-13 19:58:20 -07002447#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002448
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002449psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2450{
2451 switch( operation->alg )
2452 {
Gilles Peskine81736312018-06-26 15:04:31 +02002453 case 0:
2454 /* The object has (apparently) been initialized but it is not
2455 * in use. It's ok to call abort on such an object, and there's
2456 * nothing to do. */
2457 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002458#if defined(MBEDTLS_MD2_C)
2459 case PSA_ALG_MD2:
2460 mbedtls_md2_free( &operation->ctx.md2 );
2461 break;
2462#endif
2463#if defined(MBEDTLS_MD4_C)
2464 case PSA_ALG_MD4:
2465 mbedtls_md4_free( &operation->ctx.md4 );
2466 break;
2467#endif
2468#if defined(MBEDTLS_MD5_C)
2469 case PSA_ALG_MD5:
2470 mbedtls_md5_free( &operation->ctx.md5 );
2471 break;
2472#endif
2473#if defined(MBEDTLS_RIPEMD160_C)
2474 case PSA_ALG_RIPEMD160:
2475 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2476 break;
2477#endif
2478#if defined(MBEDTLS_SHA1_C)
2479 case PSA_ALG_SHA_1:
2480 mbedtls_sha1_free( &operation->ctx.sha1 );
2481 break;
2482#endif
2483#if defined(MBEDTLS_SHA256_C)
2484 case PSA_ALG_SHA_224:
2485 case PSA_ALG_SHA_256:
2486 mbedtls_sha256_free( &operation->ctx.sha256 );
2487 break;
2488#endif
2489#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002490#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002491 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002492#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002493 case PSA_ALG_SHA_512:
2494 mbedtls_sha512_free( &operation->ctx.sha512 );
2495 break;
2496#endif
2497 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002498 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002499 }
2500 operation->alg = 0;
2501 return( PSA_SUCCESS );
2502}
2503
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002504psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002505 psa_algorithm_t alg )
2506{
Janos Follath24eed8d2019-11-22 13:21:35 +00002507 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002508
2509 /* A context must be freshly initialized before it can be set up. */
2510 if( operation->alg != 0 )
2511 {
2512 return( PSA_ERROR_BAD_STATE );
2513 }
2514
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002515 switch( alg )
2516 {
2517#if defined(MBEDTLS_MD2_C)
2518 case PSA_ALG_MD2:
2519 mbedtls_md2_init( &operation->ctx.md2 );
2520 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2521 break;
2522#endif
2523#if defined(MBEDTLS_MD4_C)
2524 case PSA_ALG_MD4:
2525 mbedtls_md4_init( &operation->ctx.md4 );
2526 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2527 break;
2528#endif
2529#if defined(MBEDTLS_MD5_C)
2530 case PSA_ALG_MD5:
2531 mbedtls_md5_init( &operation->ctx.md5 );
2532 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2533 break;
2534#endif
2535#if defined(MBEDTLS_RIPEMD160_C)
2536 case PSA_ALG_RIPEMD160:
2537 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2538 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2539 break;
2540#endif
2541#if defined(MBEDTLS_SHA1_C)
2542 case PSA_ALG_SHA_1:
2543 mbedtls_sha1_init( &operation->ctx.sha1 );
2544 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2545 break;
2546#endif
2547#if defined(MBEDTLS_SHA256_C)
2548 case PSA_ALG_SHA_224:
2549 mbedtls_sha256_init( &operation->ctx.sha256 );
2550 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2551 break;
2552 case PSA_ALG_SHA_256:
2553 mbedtls_sha256_init( &operation->ctx.sha256 );
2554 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2555 break;
2556#endif
2557#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002558#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002559 case PSA_ALG_SHA_384:
2560 mbedtls_sha512_init( &operation->ctx.sha512 );
2561 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2562 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002563#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002564 case PSA_ALG_SHA_512:
2565 mbedtls_sha512_init( &operation->ctx.sha512 );
2566 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2567 break;
2568#endif
2569 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002570 return( PSA_ALG_IS_HASH( alg ) ?
2571 PSA_ERROR_NOT_SUPPORTED :
2572 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002573 }
2574 if( ret == 0 )
2575 operation->alg = alg;
2576 else
2577 psa_hash_abort( operation );
2578 return( mbedtls_to_psa_error( ret ) );
2579}
2580
2581psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2582 const uint8_t *input,
2583 size_t input_length )
2584{
Janos Follath24eed8d2019-11-22 13:21:35 +00002585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002586
2587 /* Don't require hash implementations to behave correctly on a
2588 * zero-length input, which may have an invalid pointer. */
2589 if( input_length == 0 )
2590 return( PSA_SUCCESS );
2591
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002592 switch( operation->alg )
2593 {
2594#if defined(MBEDTLS_MD2_C)
2595 case PSA_ALG_MD2:
2596 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2597 input, input_length );
2598 break;
2599#endif
2600#if defined(MBEDTLS_MD4_C)
2601 case PSA_ALG_MD4:
2602 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2603 input, input_length );
2604 break;
2605#endif
2606#if defined(MBEDTLS_MD5_C)
2607 case PSA_ALG_MD5:
2608 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2609 input, input_length );
2610 break;
2611#endif
2612#if defined(MBEDTLS_RIPEMD160_C)
2613 case PSA_ALG_RIPEMD160:
2614 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2615 input, input_length );
2616 break;
2617#endif
2618#if defined(MBEDTLS_SHA1_C)
2619 case PSA_ALG_SHA_1:
2620 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2621 input, input_length );
2622 break;
2623#endif
2624#if defined(MBEDTLS_SHA256_C)
2625 case PSA_ALG_SHA_224:
2626 case PSA_ALG_SHA_256:
2627 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2628 input, input_length );
2629 break;
2630#endif
2631#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002632#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002633 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002634#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002635 case PSA_ALG_SHA_512:
2636 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2637 input, input_length );
2638 break;
2639#endif
2640 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002641 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002642 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002643
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002644 if( ret != 0 )
2645 psa_hash_abort( operation );
2646 return( mbedtls_to_psa_error( ret ) );
2647}
2648
2649psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2650 uint8_t *hash,
2651 size_t hash_size,
2652 size_t *hash_length )
2653{
itayzafrir40835d42018-08-02 13:14:17 +03002654 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002656 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002657
2658 /* Fill the output buffer with something that isn't a valid hash
2659 * (barring an attack on the hash and deliberately-crafted input),
2660 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002661 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002662 /* If hash_size is 0 then hash may be NULL and then the
2663 * call to memset would have undefined behavior. */
2664 if( hash_size != 0 )
2665 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002666
2667 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002668 {
2669 status = PSA_ERROR_BUFFER_TOO_SMALL;
2670 goto exit;
2671 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002672
2673 switch( operation->alg )
2674 {
2675#if defined(MBEDTLS_MD2_C)
2676 case PSA_ALG_MD2:
2677 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2678 break;
2679#endif
2680#if defined(MBEDTLS_MD4_C)
2681 case PSA_ALG_MD4:
2682 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2683 break;
2684#endif
2685#if defined(MBEDTLS_MD5_C)
2686 case PSA_ALG_MD5:
2687 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2688 break;
2689#endif
2690#if defined(MBEDTLS_RIPEMD160_C)
2691 case PSA_ALG_RIPEMD160:
2692 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2693 break;
2694#endif
2695#if defined(MBEDTLS_SHA1_C)
2696 case PSA_ALG_SHA_1:
2697 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2698 break;
2699#endif
2700#if defined(MBEDTLS_SHA256_C)
2701 case PSA_ALG_SHA_224:
2702 case PSA_ALG_SHA_256:
2703 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2704 break;
2705#endif
2706#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002707#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002708 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002709#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002710 case PSA_ALG_SHA_512:
2711 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2712 break;
2713#endif
2714 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002715 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002716 }
itayzafrir40835d42018-08-02 13:14:17 +03002717 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002718
itayzafrir40835d42018-08-02 13:14:17 +03002719exit:
2720 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002721 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002722 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002723 return( psa_hash_abort( operation ) );
2724 }
2725 else
2726 {
2727 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002728 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002729 }
2730}
2731
Gilles Peskine2d277862018-06-18 15:41:12 +02002732psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2733 const uint8_t *hash,
2734 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002735{
2736 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2737 size_t actual_hash_length;
2738 psa_status_t status = psa_hash_finish( operation,
2739 actual_hash, sizeof( actual_hash ),
2740 &actual_hash_length );
2741 if( status != PSA_SUCCESS )
2742 return( status );
2743 if( actual_hash_length != hash_length )
2744 return( PSA_ERROR_INVALID_SIGNATURE );
2745 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2746 return( PSA_ERROR_INVALID_SIGNATURE );
2747 return( PSA_SUCCESS );
2748}
2749
Gilles Peskine0a749c82019-11-28 19:33:58 +01002750psa_status_t psa_hash_compute( psa_algorithm_t alg,
2751 const uint8_t *input, size_t input_length,
2752 uint8_t *hash, size_t hash_size,
2753 size_t *hash_length )
2754{
2755 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2756 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2757
2758 *hash_length = hash_size;
2759 status = psa_hash_setup( &operation, alg );
2760 if( status != PSA_SUCCESS )
2761 goto exit;
2762 status = psa_hash_update( &operation, input, input_length );
2763 if( status != PSA_SUCCESS )
2764 goto exit;
2765 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2766 if( status != PSA_SUCCESS )
2767 goto exit;
2768
2769exit:
2770 if( status == PSA_SUCCESS )
2771 status = psa_hash_abort( &operation );
2772 else
2773 psa_hash_abort( &operation );
2774 return( status );
2775}
2776
2777psa_status_t psa_hash_compare( psa_algorithm_t alg,
2778 const uint8_t *input, size_t input_length,
2779 const uint8_t *hash, size_t hash_length )
2780{
2781 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2782 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2783
2784 status = psa_hash_setup( &operation, alg );
2785 if( status != PSA_SUCCESS )
2786 goto exit;
2787 status = psa_hash_update( &operation, input, input_length );
2788 if( status != PSA_SUCCESS )
2789 goto exit;
2790 status = psa_hash_verify( &operation, hash, hash_length );
2791 if( status != PSA_SUCCESS )
2792 goto exit;
2793
2794exit:
2795 if( status == PSA_SUCCESS )
2796 status = psa_hash_abort( &operation );
2797 else
2798 psa_hash_abort( &operation );
2799 return( status );
2800}
2801
Gilles Peskineeb35d782019-01-22 17:56:16 +01002802psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2803 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002804{
2805 if( target_operation->alg != 0 )
2806 return( PSA_ERROR_BAD_STATE );
2807
2808 switch( source_operation->alg )
2809 {
2810 case 0:
2811 return( PSA_ERROR_BAD_STATE );
2812#if defined(MBEDTLS_MD2_C)
2813 case PSA_ALG_MD2:
2814 mbedtls_md2_clone( &target_operation->ctx.md2,
2815 &source_operation->ctx.md2 );
2816 break;
2817#endif
2818#if defined(MBEDTLS_MD4_C)
2819 case PSA_ALG_MD4:
2820 mbedtls_md4_clone( &target_operation->ctx.md4,
2821 &source_operation->ctx.md4 );
2822 break;
2823#endif
2824#if defined(MBEDTLS_MD5_C)
2825 case PSA_ALG_MD5:
2826 mbedtls_md5_clone( &target_operation->ctx.md5,
2827 &source_operation->ctx.md5 );
2828 break;
2829#endif
2830#if defined(MBEDTLS_RIPEMD160_C)
2831 case PSA_ALG_RIPEMD160:
2832 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2833 &source_operation->ctx.ripemd160 );
2834 break;
2835#endif
2836#if defined(MBEDTLS_SHA1_C)
2837 case PSA_ALG_SHA_1:
2838 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2839 &source_operation->ctx.sha1 );
2840 break;
2841#endif
2842#if defined(MBEDTLS_SHA256_C)
2843 case PSA_ALG_SHA_224:
2844 case PSA_ALG_SHA_256:
2845 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2846 &source_operation->ctx.sha256 );
2847 break;
2848#endif
2849#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002850#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002851 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002852#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002853 case PSA_ALG_SHA_512:
2854 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2855 &source_operation->ctx.sha512 );
2856 break;
2857#endif
2858 default:
2859 return( PSA_ERROR_NOT_SUPPORTED );
2860 }
2861
2862 target_operation->alg = source_operation->alg;
2863 return( PSA_SUCCESS );
2864}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002865
2866
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002867/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002868/* MAC */
2869/****************************************************************/
2870
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002871static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002872 psa_algorithm_t alg,
2873 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002874 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002875 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002876{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002877 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002878 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002879
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002880 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002881 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002882
Gilles Peskine8c9def32018-02-08 10:02:12 +01002883 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2884 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002885 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002886 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002887 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002888 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002889 mode = MBEDTLS_MODE_STREAM;
2890 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002891 case PSA_ALG_CTR:
2892 mode = MBEDTLS_MODE_CTR;
2893 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002894 case PSA_ALG_CFB:
2895 mode = MBEDTLS_MODE_CFB;
2896 break;
2897 case PSA_ALG_OFB:
2898 mode = MBEDTLS_MODE_OFB;
2899 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002900 case PSA_ALG_ECB_NO_PADDING:
2901 mode = MBEDTLS_MODE_ECB;
2902 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002903 case PSA_ALG_CBC_NO_PADDING:
2904 mode = MBEDTLS_MODE_CBC;
2905 break;
2906 case PSA_ALG_CBC_PKCS7:
2907 mode = MBEDTLS_MODE_CBC;
2908 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002909 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002910 mode = MBEDTLS_MODE_CCM;
2911 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002912 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002913 mode = MBEDTLS_MODE_GCM;
2914 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002915 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2916 mode = MBEDTLS_MODE_CHACHAPOLY;
2917 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002918 default:
2919 return( NULL );
2920 }
2921 }
2922 else if( alg == PSA_ALG_CMAC )
2923 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002924 else
2925 return( NULL );
2926
2927 switch( key_type )
2928 {
2929 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002930 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002931 break;
2932 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002933 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2934 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002935 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002936 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002937 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002938 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002939 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2940 * but two-key Triple-DES is functionally three-key Triple-DES
2941 * with K1=K3, so that's how we present it to mbedtls. */
2942 if( key_bits == 128 )
2943 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002944 break;
2945 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002946 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002947 break;
2948 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002949 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002950 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002951 case PSA_KEY_TYPE_CHACHA20:
2952 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2953 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002954 default:
2955 return( NULL );
2956 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002957 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002958 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002959
Jaeden Amero23bbb752018-06-26 14:16:54 +01002960 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2961 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002962}
2963
Gilles Peskinea05219c2018-11-16 16:02:56 +01002964#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002965static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002966{
Gilles Peskine2d277862018-06-18 15:41:12 +02002967 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002968 {
2969 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002970 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002971 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002972 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002973 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002974 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002975 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002976 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002977 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002978 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002979 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002980 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002981 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002982 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002983 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002984 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002985 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002986 return( 128 );
2987 default:
2988 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002989 }
2990}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002991#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002992
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002993/* Initialize the MAC operation structure. Once this function has been
2994 * called, psa_mac_abort can run and will do the right thing. */
2995static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2996 psa_algorithm_t alg )
2997{
2998 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2999
3000 operation->alg = alg;
3001 operation->key_set = 0;
3002 operation->iv_set = 0;
3003 operation->iv_required = 0;
3004 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003005 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003006
3007#if defined(MBEDTLS_CMAC_C)
3008 if( alg == PSA_ALG_CMAC )
3009 {
3010 operation->iv_required = 0;
3011 mbedtls_cipher_init( &operation->ctx.cmac );
3012 status = PSA_SUCCESS;
3013 }
3014 else
3015#endif /* MBEDTLS_CMAC_C */
3016#if defined(MBEDTLS_MD_C)
3017 if( PSA_ALG_IS_HMAC( operation->alg ) )
3018 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003019 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3020 operation->ctx.hmac.hash_ctx.alg = 0;
3021 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003022 }
3023 else
3024#endif /* MBEDTLS_MD_C */
3025 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003026 if( ! PSA_ALG_IS_MAC( alg ) )
3027 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003028 }
3029
3030 if( status != PSA_SUCCESS )
3031 memset( operation, 0, sizeof( *operation ) );
3032 return( status );
3033}
3034
Gilles Peskine01126fa2018-07-12 17:04:55 +02003035#if defined(MBEDTLS_MD_C)
3036static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3037{
Gilles Peskine3f108122018-12-07 18:14:53 +01003038 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003039 return( psa_hash_abort( &hmac->hash_ctx ) );
3040}
3041#endif /* MBEDTLS_MD_C */
3042
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3044{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003045 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003046 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003047 /* The object has (apparently) been initialized but it is not
3048 * in use. It's ok to call abort on such an object, and there's
3049 * nothing to do. */
3050 return( PSA_SUCCESS );
3051 }
3052 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003053#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003054 if( operation->alg == PSA_ALG_CMAC )
3055 {
3056 mbedtls_cipher_free( &operation->ctx.cmac );
3057 }
3058 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003059#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003060#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003061 if( PSA_ALG_IS_HMAC( operation->alg ) )
3062 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003063 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003064 }
3065 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003066#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003067 {
3068 /* Sanity check (shouldn't happen: operation->alg should
3069 * always have been initialized to a valid value). */
3070 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003071 }
Moran Peker41deec42018-04-04 15:43:05 +03003072
Gilles Peskine8c9def32018-02-08 10:02:12 +01003073 operation->alg = 0;
3074 operation->key_set = 0;
3075 operation->iv_set = 0;
3076 operation->iv_required = 0;
3077 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003078 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003079
Gilles Peskine8c9def32018-02-08 10:02:12 +01003080 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003081
3082bad_state:
3083 /* If abort is called on an uninitialized object, we can't trust
3084 * anything. Wipe the object in case it contains confidential data.
3085 * This may result in a memory leak if a pointer gets overwritten,
3086 * but it's too late to do anything about this. */
3087 memset( operation, 0, sizeof( *operation ) );
3088 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003089}
3090
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003091#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003092static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003093 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003094 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003095 const mbedtls_cipher_info_t *cipher_info )
3096{
Janos Follath24eed8d2019-11-22 13:21:35 +00003097 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003098
3099 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003100
3101 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3102 if( ret != 0 )
3103 return( ret );
3104
3105 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003106 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003107 key_bits );
3108 return( ret );
3109}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003110#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003111
Gilles Peskine248051a2018-06-20 16:09:38 +02003112#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003113static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3114 const uint8_t *key,
3115 size_t key_length,
3116 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003117{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003118 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003119 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003120 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003121 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003122 psa_status_t status;
3123
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003124 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3125 * overflow below. This should never trigger if the hash algorithm
3126 * is implemented correctly. */
3127 /* The size checks against the ipad and opad buffers cannot be written
3128 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3129 * because that triggers -Wlogical-op on GCC 7.3. */
3130 if( block_size > sizeof( ipad ) )
3131 return( PSA_ERROR_NOT_SUPPORTED );
3132 if( block_size > sizeof( hmac->opad ) )
3133 return( PSA_ERROR_NOT_SUPPORTED );
3134 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003135 return( PSA_ERROR_NOT_SUPPORTED );
3136
Gilles Peskined223b522018-06-11 18:12:58 +02003137 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003138 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003139 status = psa_hash_compute( hash_alg, key, key_length,
3140 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003141 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003142 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003143 }
Gilles Peskine96889972018-07-12 17:07:03 +02003144 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3145 * but it is permitted. It is common when HMAC is used in HKDF, for
3146 * example. Don't call `memcpy` in the 0-length because `key` could be
3147 * an invalid pointer which would make the behavior undefined. */
3148 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003149 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003150
Gilles Peskined223b522018-06-11 18:12:58 +02003151 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3152 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003153 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003154 ipad[i] ^= 0x36;
3155 memset( ipad + key_length, 0x36, block_size - key_length );
3156
3157 /* Copy the key material from ipad to opad, flipping the requisite bits,
3158 * and filling the rest of opad with the requisite constant. */
3159 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003160 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3161 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003162
Gilles Peskine01126fa2018-07-12 17:04:55 +02003163 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003164 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003165 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003166
Gilles Peskine01126fa2018-07-12 17:04:55 +02003167 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003168
3169cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003170 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003171
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003172 return( status );
3173}
Gilles Peskine248051a2018-06-20 16:09:38 +02003174#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003175
Gilles Peskine89167cb2018-07-08 20:12:23 +02003176static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003177 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003178 psa_algorithm_t alg,
3179 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003180{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003181 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3182 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003183 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003184 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003185 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003186 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003187 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003188 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003189
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003190 /* A context must be freshly initialized before it can be set up. */
3191 if( operation->alg != 0 )
3192 {
3193 return( PSA_ERROR_BAD_STATE );
3194 }
3195
Gilles Peskined911eb72018-08-14 15:18:45 +02003196 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003197 if( status != PSA_SUCCESS )
3198 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003199 if( is_sign )
3200 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003201
Ronald Croncf56a0a2020-08-04 09:51:30 +02003202 status = psa_get_transparent_key( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003203 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003204 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003205 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003206
Gilles Peskine8c9def32018-02-08 10:02:12 +01003207#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003208 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003209 {
3210 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003211 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003212 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003214 if( cipher_info == NULL )
3215 {
3216 status = PSA_ERROR_NOT_SUPPORTED;
3217 goto exit;
3218 }
3219 operation->mac_size = cipher_info->block_size;
3220 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3221 status = mbedtls_to_psa_error( ret );
3222 }
3223 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003224#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003225#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003226 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003227 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003228 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003229 if( hash_alg == 0 )
3230 {
3231 status = PSA_ERROR_NOT_SUPPORTED;
3232 goto exit;
3233 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003234
3235 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3236 /* Sanity check. This shouldn't fail on a valid configuration. */
3237 if( operation->mac_size == 0 ||
3238 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3239 {
3240 status = PSA_ERROR_NOT_SUPPORTED;
3241 goto exit;
3242 }
3243
Gilles Peskine8e338702019-07-30 20:06:31 +02003244 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003245 {
3246 status = PSA_ERROR_INVALID_ARGUMENT;
3247 goto exit;
3248 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003249
Gilles Peskine01126fa2018-07-12 17:04:55 +02003250 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003251 slot->data.key.data,
3252 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003253 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003254 }
3255 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003256#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003257 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003258 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003259 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003260 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003261
Gilles Peskined911eb72018-08-14 15:18:45 +02003262 if( truncated == 0 )
3263 {
3264 /* The "normal" case: untruncated algorithm. Nothing to do. */
3265 }
3266 else if( truncated < 4 )
3267 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003268 /* A very short MAC is too short for security since it can be
3269 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3270 * so we make this our minimum, even though 32 bits is still
3271 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003272 status = PSA_ERROR_NOT_SUPPORTED;
3273 }
3274 else if( truncated > operation->mac_size )
3275 {
3276 /* It's impossible to "truncate" to a larger length. */
3277 status = PSA_ERROR_INVALID_ARGUMENT;
3278 }
3279 else
3280 operation->mac_size = truncated;
3281
Gilles Peskinefbfac682018-07-08 20:51:54 +02003282exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003283 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003284 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003285 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003286 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003287 else
3288 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003289 operation->key_set = 1;
3290 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003291
3292 decrement_status = psa_decrement_key_slot_access_count( slot );
3293
3294 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003295}
3296
Gilles Peskine89167cb2018-07-08 20:12:23 +02003297psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003298 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003299 psa_algorithm_t alg )
3300{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003301 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003302}
3303
3304psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003305 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003306 psa_algorithm_t alg )
3307{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003308 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003309}
3310
Gilles Peskine8c9def32018-02-08 10:02:12 +01003311psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3312 const uint8_t *input,
3313 size_t input_length )
3314{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003315 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003316 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003317 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003318 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003319 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003320 operation->has_input = 1;
3321
Gilles Peskine8c9def32018-02-08 10:02:12 +01003322#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003323 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003324 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003325 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3326 input, input_length );
3327 status = mbedtls_to_psa_error( ret );
3328 }
3329 else
3330#endif /* MBEDTLS_CMAC_C */
3331#if defined(MBEDTLS_MD_C)
3332 if( PSA_ALG_IS_HMAC( operation->alg ) )
3333 {
3334 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3335 input_length );
3336 }
3337 else
3338#endif /* MBEDTLS_MD_C */
3339 {
3340 /* This shouldn't happen if `operation` was initialized by
3341 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003342 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003343 }
3344
Gilles Peskinefbfac682018-07-08 20:51:54 +02003345 if( status != PSA_SUCCESS )
3346 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003347 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003348}
3349
Gilles Peskine01126fa2018-07-12 17:04:55 +02003350#if defined(MBEDTLS_MD_C)
3351static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3352 uint8_t *mac,
3353 size_t mac_size )
3354{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003355 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003356 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3357 size_t hash_size = 0;
3358 size_t block_size = psa_get_hash_block_size( hash_alg );
3359 psa_status_t status;
3360
Gilles Peskine01126fa2018-07-12 17:04:55 +02003361 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3362 if( status != PSA_SUCCESS )
3363 return( status );
3364 /* From here on, tmp needs to be wiped. */
3365
3366 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3367 if( status != PSA_SUCCESS )
3368 goto exit;
3369
3370 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3371 if( status != PSA_SUCCESS )
3372 goto exit;
3373
3374 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3375 if( status != PSA_SUCCESS )
3376 goto exit;
3377
Gilles Peskined911eb72018-08-14 15:18:45 +02003378 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3379 if( status != PSA_SUCCESS )
3380 goto exit;
3381
3382 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003383
3384exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003385 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003386 return( status );
3387}
3388#endif /* MBEDTLS_MD_C */
3389
mohammad16036df908f2018-04-02 08:34:15 -07003390static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003391 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003392 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003393{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003394 if( ! operation->key_set )
3395 return( PSA_ERROR_BAD_STATE );
3396 if( operation->iv_required && ! operation->iv_set )
3397 return( PSA_ERROR_BAD_STATE );
3398
Gilles Peskine8c9def32018-02-08 10:02:12 +01003399 if( mac_size < operation->mac_size )
3400 return( PSA_ERROR_BUFFER_TOO_SMALL );
3401
Gilles Peskine8c9def32018-02-08 10:02:12 +01003402#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003403 if( operation->alg == PSA_ALG_CMAC )
3404 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003405 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3406 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3407 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003408 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003409 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003410 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003411 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003412 else
3413#endif /* MBEDTLS_CMAC_C */
3414#if defined(MBEDTLS_MD_C)
3415 if( PSA_ALG_IS_HMAC( operation->alg ) )
3416 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003417 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003418 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003419 }
3420 else
3421#endif /* MBEDTLS_MD_C */
3422 {
3423 /* This shouldn't happen if `operation` was initialized by
3424 * a setup function. */
3425 return( PSA_ERROR_BAD_STATE );
3426 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003427}
3428
Gilles Peskineacd4be32018-07-08 19:56:25 +02003429psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3430 uint8_t *mac,
3431 size_t mac_size,
3432 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003433{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003434 psa_status_t status;
3435
Jaeden Amero252ef282019-02-15 14:05:35 +00003436 if( operation->alg == 0 )
3437 {
3438 return( PSA_ERROR_BAD_STATE );
3439 }
3440
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003441 /* Fill the output buffer with something that isn't a valid mac
3442 * (barring an attack on the mac and deliberately-crafted input),
3443 * in case the caller doesn't check the return status properly. */
3444 *mac_length = mac_size;
3445 /* If mac_size is 0 then mac may be NULL and then the
3446 * call to memset would have undefined behavior. */
3447 if( mac_size != 0 )
3448 memset( mac, '!', mac_size );
3449
Gilles Peskine89167cb2018-07-08 20:12:23 +02003450 if( ! operation->is_sign )
3451 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003452 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003453 }
mohammad16036df908f2018-04-02 08:34:15 -07003454
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003455 status = psa_mac_finish_internal( operation, mac, mac_size );
3456
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003457 if( status == PSA_SUCCESS )
3458 {
3459 status = psa_mac_abort( operation );
3460 if( status == PSA_SUCCESS )
3461 *mac_length = operation->mac_size;
3462 else
3463 memset( mac, '!', mac_size );
3464 }
3465 else
3466 psa_mac_abort( operation );
3467 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003468}
3469
Gilles Peskineacd4be32018-07-08 19:56:25 +02003470psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3471 const uint8_t *mac,
3472 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003473{
Gilles Peskine828ed142018-06-18 23:25:51 +02003474 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003475 psa_status_t status;
3476
Jaeden Amero252ef282019-02-15 14:05:35 +00003477 if( operation->alg == 0 )
3478 {
3479 return( PSA_ERROR_BAD_STATE );
3480 }
3481
Gilles Peskine89167cb2018-07-08 20:12:23 +02003482 if( operation->is_sign )
3483 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003484 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003485 }
3486 if( operation->mac_size != mac_length )
3487 {
3488 status = PSA_ERROR_INVALID_SIGNATURE;
3489 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003490 }
mohammad16036df908f2018-04-02 08:34:15 -07003491
3492 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003493 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003494 if( status != PSA_SUCCESS )
3495 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003496
3497 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3498 status = PSA_ERROR_INVALID_SIGNATURE;
3499
3500cleanup:
3501 if( status == PSA_SUCCESS )
3502 status = psa_mac_abort( operation );
3503 else
3504 psa_mac_abort( operation );
3505
Gilles Peskine3f108122018-12-07 18:14:53 +01003506 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003507
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003508 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003509}
3510
3511
Gilles Peskine20035e32018-02-03 22:44:14 +01003512
Gilles Peskine20035e32018-02-03 22:44:14 +01003513/****************************************************************/
3514/* Asymmetric cryptography */
3515/****************************************************************/
3516
Gilles Peskine2b450e32018-06-27 15:42:46 +02003517#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003518/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003519 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003520static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3521 size_t hash_length,
3522 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003523{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003524 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003525 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003526 *md_alg = mbedtls_md_get_type( md_info );
3527
3528 /* The Mbed TLS RSA module uses an unsigned int for hash length
3529 * parameters. Validate that it fits so that we don't risk an
3530 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003531#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003532 if( hash_length > UINT_MAX )
3533 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003534#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003535
3536#if defined(MBEDTLS_PKCS1_V15)
3537 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3538 * must be correct. */
3539 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3540 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003541 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003542 if( md_info == NULL )
3543 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003544 if( mbedtls_md_get_size( md_info ) != hash_length )
3545 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003546 }
3547#endif /* MBEDTLS_PKCS1_V15 */
3548
3549#if defined(MBEDTLS_PKCS1_V21)
3550 /* PSS requires a hash internally. */
3551 if( PSA_ALG_IS_RSA_PSS( alg ) )
3552 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003553 if( md_info == NULL )
3554 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003555 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003556#endif /* MBEDTLS_PKCS1_V21 */
3557
Gilles Peskine61b91d42018-06-08 16:09:36 +02003558 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003559}
3560
Gilles Peskine2b450e32018-06-27 15:42:46 +02003561static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3562 psa_algorithm_t alg,
3563 const uint8_t *hash,
3564 size_t hash_length,
3565 uint8_t *signature,
3566 size_t signature_size,
3567 size_t *signature_length )
3568{
3569 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003570 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003571 mbedtls_md_type_t md_alg;
3572
3573 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3574 if( status != PSA_SUCCESS )
3575 return( status );
3576
Gilles Peskine630a18a2018-06-29 17:49:35 +02003577 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003578 return( PSA_ERROR_BUFFER_TOO_SMALL );
3579
3580#if defined(MBEDTLS_PKCS1_V15)
3581 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3582 {
3583 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3584 MBEDTLS_MD_NONE );
3585 ret = mbedtls_rsa_pkcs1_sign( rsa,
3586 mbedtls_ctr_drbg_random,
3587 &global_data.ctr_drbg,
3588 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003589 md_alg,
3590 (unsigned int) hash_length,
3591 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003592 signature );
3593 }
3594 else
3595#endif /* MBEDTLS_PKCS1_V15 */
3596#if defined(MBEDTLS_PKCS1_V21)
3597 if( PSA_ALG_IS_RSA_PSS( alg ) )
3598 {
3599 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3600 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3601 mbedtls_ctr_drbg_random,
3602 &global_data.ctr_drbg,
3603 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003604 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003605 (unsigned int) hash_length,
3606 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003607 signature );
3608 }
3609 else
3610#endif /* MBEDTLS_PKCS1_V21 */
3611 {
3612 return( PSA_ERROR_INVALID_ARGUMENT );
3613 }
3614
3615 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003616 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003617 return( mbedtls_to_psa_error( ret ) );
3618}
3619
3620static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3621 psa_algorithm_t alg,
3622 const uint8_t *hash,
3623 size_t hash_length,
3624 const uint8_t *signature,
3625 size_t signature_length )
3626{
3627 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003629 mbedtls_md_type_t md_alg;
3630
3631 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3632 if( status != PSA_SUCCESS )
3633 return( status );
3634
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003635 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3636 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003637
3638#if defined(MBEDTLS_PKCS1_V15)
3639 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3640 {
3641 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3642 MBEDTLS_MD_NONE );
3643 ret = mbedtls_rsa_pkcs1_verify( rsa,
3644 mbedtls_ctr_drbg_random,
3645 &global_data.ctr_drbg,
3646 MBEDTLS_RSA_PUBLIC,
3647 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003648 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003649 hash,
3650 signature );
3651 }
3652 else
3653#endif /* MBEDTLS_PKCS1_V15 */
3654#if defined(MBEDTLS_PKCS1_V21)
3655 if( PSA_ALG_IS_RSA_PSS( alg ) )
3656 {
3657 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3658 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3659 mbedtls_ctr_drbg_random,
3660 &global_data.ctr_drbg,
3661 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003662 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003663 (unsigned int) hash_length,
3664 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003665 signature );
3666 }
3667 else
3668#endif /* MBEDTLS_PKCS1_V21 */
3669 {
3670 return( PSA_ERROR_INVALID_ARGUMENT );
3671 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003672
3673 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3674 * the rest of the signature is invalid". This has little use in
3675 * practice and PSA doesn't report this distinction. */
3676 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3677 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003678 return( mbedtls_to_psa_error( ret ) );
3679}
3680#endif /* MBEDTLS_RSA_C */
3681
John Durkopf87e3ae2020-10-26 15:25:23 -07003682#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003683/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3684 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3685 * (even though these functions don't modify it). */
3686static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3687 psa_algorithm_t alg,
3688 const uint8_t *hash,
3689 size_t hash_length,
3690 uint8_t *signature,
3691 size_t signature_size,
3692 size_t *signature_length )
3693{
Janos Follath24eed8d2019-11-22 13:21:35 +00003694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003695 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003696 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003697 mbedtls_mpi_init( &r );
3698 mbedtls_mpi_init( &s );
3699
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003700 if( signature_size < 2 * curve_bytes )
3701 {
3702 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3703 goto cleanup;
3704 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003705
John Durkop0ea39e02020-10-13 19:58:20 -07003706#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003707 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3708 {
3709 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3710 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3711 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003712 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3713 &ecp->d, hash,
3714 hash_length, md_alg,
3715 mbedtls_ctr_drbg_random,
3716 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003717 }
3718 else
John Durkop0ea39e02020-10-13 19:58:20 -07003719#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003720 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003721 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003722 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3723 hash, hash_length,
3724 mbedtls_ctr_drbg_random,
3725 &global_data.ctr_drbg ) );
3726 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003727
3728 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3729 signature,
3730 curve_bytes ) );
3731 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3732 signature + curve_bytes,
3733 curve_bytes ) );
3734
3735cleanup:
3736 mbedtls_mpi_free( &r );
3737 mbedtls_mpi_free( &s );
3738 if( ret == 0 )
3739 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003740 return( mbedtls_to_psa_error( ret ) );
3741}
3742
3743static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3744 const uint8_t *hash,
3745 size_t hash_length,
3746 const uint8_t *signature,
3747 size_t signature_length )
3748{
Janos Follath24eed8d2019-11-22 13:21:35 +00003749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003750 mbedtls_mpi r, s;
3751 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3752 mbedtls_mpi_init( &r );
3753 mbedtls_mpi_init( &s );
3754
3755 if( signature_length != 2 * curve_bytes )
3756 return( PSA_ERROR_INVALID_SIGNATURE );
3757
3758 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3759 signature,
3760 curve_bytes ) );
3761 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3762 signature + curve_bytes,
3763 curve_bytes ) );
3764
Steven Cooremanacda8342020-07-24 23:09:52 +02003765 /* Check whether the public part is loaded. If not, load it. */
3766 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3767 {
3768 MBEDTLS_MPI_CHK(
3769 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3770 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3771 }
3772
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003773 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3774 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003775
3776cleanup:
3777 mbedtls_mpi_free( &r );
3778 mbedtls_mpi_free( &s );
3779 return( mbedtls_to_psa_error( ret ) );
3780}
John Durkopf87e3ae2020-10-26 15:25:23 -07003781#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003782
Ronald Croncf56a0a2020-08-04 09:51:30 +02003783psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003784 psa_algorithm_t alg,
3785 const uint8_t *hash,
3786 size_t hash_length,
3787 uint8_t *signature,
3788 size_t signature_size,
3789 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003790{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003791 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3792 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003793 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003794
3795 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003796 /* Immediately reject a zero-length signature buffer. This guarantees
3797 * that signature must be a valid pointer. (On the other hand, the hash
3798 * buffer can in principle be empty since it doesn't actually have
3799 * to be a hash.) */
3800 if( signature_size == 0 )
3801 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003802
Ronald Croncf56a0a2020-08-04 09:51:30 +02003803 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003804 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003805 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003806 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003807 {
3808 status = PSA_ERROR_INVALID_ARGUMENT;
3809 goto exit;
3810 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003811
Steven Cooremancd84cb42020-07-16 20:28:36 +02003812 /* Try any of the available accelerators first */
3813 status = psa_driver_wrapper_sign_hash( slot,
3814 alg,
3815 hash,
3816 hash_length,
3817 signature,
3818 signature_size,
3819 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003820 if( status != PSA_ERROR_NOT_SUPPORTED ||
3821 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003822 goto exit;
3823
Steven Cooreman7a250572020-07-17 16:43:05 +02003824 /* If the operation was not supported by any accelerator, try fallback. */
Gilles Peskine20035e32018-02-03 22:44:14 +01003825#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003826 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003827 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003828 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003829
Steven Cooreman4fed4552020-08-03 14:46:03 +02003830 status = psa_load_rsa_representation( slot->attr.type,
3831 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003832 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003833 &rsa );
3834 if( status != PSA_SUCCESS )
3835 goto exit;
3836
Steven Cooremana2371e52020-07-28 14:30:39 +02003837 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003838 alg,
3839 hash, hash_length,
3840 signature, signature_size,
3841 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003842
Steven Cooremana2371e52020-07-28 14:30:39 +02003843 mbedtls_rsa_free( rsa );
3844 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003845 }
3846 else
3847#endif /* defined(MBEDTLS_RSA_C) */
3848#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003849 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003850 {
John Durkop0ea39e02020-10-13 19:58:20 -07003851#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003852 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003853#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003854 PSA_ALG_IS_ECDSA( alg )
3855#else
3856 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3857#endif
3858 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003859 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003860 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003861 status = psa_load_ecp_representation( slot->attr.type,
3862 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003863 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003864 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003865 if( status != PSA_SUCCESS )
3866 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003867 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003868 alg,
3869 hash, hash_length,
3870 signature, signature_size,
3871 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003872 mbedtls_ecp_keypair_free( ecp );
3873 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003874 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003875 else
John Durkop0ea39e02020-10-13 19:58:20 -07003876#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003877 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003878 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003879 }
itayzafrir5c753392018-05-08 11:18:38 +03003880 }
3881 else
3882#endif /* defined(MBEDTLS_ECP_C) */
3883 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003884 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003885 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003886
3887exit:
3888 /* Fill the unused part of the output buffer (the whole buffer on error,
3889 * the trailing part on success) with something that isn't a valid mac
3890 * (barring an attack on the mac and deliberately-crafted input),
3891 * in case the caller doesn't check the return status properly. */
3892 if( status == PSA_SUCCESS )
3893 memset( signature + *signature_length, '!',
3894 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003895 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003896 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003897 /* If signature_size is 0 then we have nothing to do. We must not call
3898 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003899
3900 decrement_status = psa_decrement_key_slot_access_count( slot );
3901
3902 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003903}
3904
Ronald Croncf56a0a2020-08-04 09:51:30 +02003905psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003906 psa_algorithm_t alg,
3907 const uint8_t *hash,
3908 size_t hash_length,
3909 const uint8_t *signature,
3910 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003911{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003912 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3913 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003914 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003915
Ronald Croncf56a0a2020-08-04 09:51:30 +02003916 status = psa_get_key_from_slot( key, &slot,
3917 PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003918 if( status != PSA_SUCCESS )
3919 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003920
Steven Cooreman55ae2172020-07-17 19:46:15 +02003921 /* Try any of the available accelerators first */
3922 status = psa_driver_wrapper_verify_hash( slot,
3923 alg,
3924 hash,
3925 hash_length,
3926 signature,
3927 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003928 if( status != PSA_ERROR_NOT_SUPPORTED ||
3929 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003930 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003931
Gilles Peskine61b91d42018-06-08 16:09:36 +02003932#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003933 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003934 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003935 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003936
Steven Cooreman4fed4552020-08-03 14:46:03 +02003937 status = psa_load_rsa_representation( slot->attr.type,
3938 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003939 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003940 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003941 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003942 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003943
Steven Cooremana2371e52020-07-28 14:30:39 +02003944 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003945 alg,
3946 hash, hash_length,
3947 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003948 mbedtls_rsa_free( rsa );
3949 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003950 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003951 }
3952 else
3953#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003954#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003955 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003956 {
John Durkopf87e3ae2020-10-26 15:25:23 -07003957#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003958 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003959 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003960 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003961 status = psa_load_ecp_representation( slot->attr.type,
3962 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003963 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003964 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003965 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003966 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003967 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003968 hash, hash_length,
3969 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003970 mbedtls_ecp_keypair_free( ecp );
3971 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003972 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003973 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003974 else
John Durkopf87e3ae2020-10-26 15:25:23 -07003975#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003976 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003977 status = PSA_ERROR_INVALID_ARGUMENT;
3978 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003979 }
itayzafrir5c753392018-05-08 11:18:38 +03003980 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003981 else
3982#endif /* defined(MBEDTLS_ECP_C) */
3983 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003984 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003985 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003986
3987exit:
3988 decrement_status = psa_decrement_key_slot_access_count( slot );
3989
3990 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003991}
3992
Gilles Peskine072ac562018-06-30 00:21:29 +02003993#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3994static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3995 mbedtls_rsa_context *rsa )
3996{
3997 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3998 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3999 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4000 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4001}
4002#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
4003
Ronald Croncf56a0a2020-08-04 09:51:30 +02004004psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004005 psa_algorithm_t alg,
4006 const uint8_t *input,
4007 size_t input_length,
4008 const uint8_t *salt,
4009 size_t salt_length,
4010 uint8_t *output,
4011 size_t output_size,
4012 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004013{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004014 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4015 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004016 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004017
Darryl Green5cc689a2018-07-24 15:34:10 +01004018 (void) input;
4019 (void) input_length;
4020 (void) salt;
4021 (void) output;
4022 (void) output_size;
4023
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004024 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004025
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004026 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4027 return( PSA_ERROR_INVALID_ARGUMENT );
4028
Ronald Croncf56a0a2020-08-04 09:51:30 +02004029 status = psa_get_transparent_key( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004030 if( status != PSA_SUCCESS )
4031 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004032 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4033 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004034 {
4035 status = PSA_ERROR_INVALID_ARGUMENT;
4036 goto exit;
4037 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004038
4039#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004040 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004041 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004042 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004043 status = psa_load_rsa_representation( slot->attr.type,
4044 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004045 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004046 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004047 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004048 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004049
4050 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004051 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004052 status = PSA_ERROR_BUFFER_TOO_SMALL;
4053 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004054 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004055#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004056 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004057 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004058 status = mbedtls_to_psa_error(
4059 mbedtls_rsa_pkcs1_encrypt( rsa,
4060 mbedtls_ctr_drbg_random,
4061 &global_data.ctr_drbg,
4062 MBEDTLS_RSA_PUBLIC,
4063 input_length,
4064 input,
4065 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004066 }
4067 else
4068#endif /* MBEDTLS_PKCS1_V15 */
4069#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004070 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004071 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004072 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004073 status = mbedtls_to_psa_error(
4074 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
4075 mbedtls_ctr_drbg_random,
4076 &global_data.ctr_drbg,
4077 MBEDTLS_RSA_PUBLIC,
4078 salt, salt_length,
4079 input_length,
4080 input,
4081 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004082 }
4083 else
4084#endif /* MBEDTLS_PKCS1_V21 */
4085 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004086 status = PSA_ERROR_INVALID_ARGUMENT;
4087 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004088 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004089rsa_exit:
4090 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004091 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004092
Steven Cooremana2371e52020-07-28 14:30:39 +02004093 mbedtls_rsa_free( rsa );
4094 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004095 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004096 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004097#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004098 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004099 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004100 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004101
4102exit:
4103 decrement_status = psa_decrement_key_slot_access_count( slot );
4104
4105 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004106}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004107
Ronald Croncf56a0a2020-08-04 09:51:30 +02004108psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004109 psa_algorithm_t alg,
4110 const uint8_t *input,
4111 size_t input_length,
4112 const uint8_t *salt,
4113 size_t salt_length,
4114 uint8_t *output,
4115 size_t output_size,
4116 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004117{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004118 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4119 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004120 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004121
Darryl Green5cc689a2018-07-24 15:34:10 +01004122 (void) input;
4123 (void) input_length;
4124 (void) salt;
4125 (void) output;
4126 (void) output_size;
4127
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004128 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004129
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004130 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4131 return( PSA_ERROR_INVALID_ARGUMENT );
4132
Ronald Croncf56a0a2020-08-04 09:51:30 +02004133 status = psa_get_transparent_key( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004134 if( status != PSA_SUCCESS )
4135 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004136 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004137 {
4138 status = PSA_ERROR_INVALID_ARGUMENT;
4139 goto exit;
4140 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004141
4142#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004143 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004144 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004145 mbedtls_rsa_context *rsa = NULL;
4146 status = psa_load_rsa_representation( slot->attr.type,
4147 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004148 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004149 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004150 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004151 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004152
Steven Cooremana2371e52020-07-28 14:30:39 +02004153 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004154 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004155 status = PSA_ERROR_INVALID_ARGUMENT;
4156 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004157 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004158
4159#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004160 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004161 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004162 status = mbedtls_to_psa_error(
4163 mbedtls_rsa_pkcs1_decrypt( rsa,
4164 mbedtls_ctr_drbg_random,
4165 &global_data.ctr_drbg,
4166 MBEDTLS_RSA_PRIVATE,
4167 output_length,
4168 input,
4169 output,
4170 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004171 }
4172 else
4173#endif /* MBEDTLS_PKCS1_V15 */
4174#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004175 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004176 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004177 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004178 status = mbedtls_to_psa_error(
4179 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4180 mbedtls_ctr_drbg_random,
4181 &global_data.ctr_drbg,
4182 MBEDTLS_RSA_PRIVATE,
4183 salt, salt_length,
4184 output_length,
4185 input,
4186 output,
4187 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004188 }
4189 else
4190#endif /* MBEDTLS_PKCS1_V21 */
4191 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004192 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004193 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004194
Steven Cooreman4fed4552020-08-03 14:46:03 +02004195rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004196 mbedtls_rsa_free( rsa );
4197 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004198 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004199 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004200#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004201 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004202 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004203 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004204
4205exit:
4206 decrement_status = psa_decrement_key_slot_access_count( slot );
4207
4208 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004209}
Gilles Peskine20035e32018-02-03 22:44:14 +01004210
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004211
4212
mohammad1603503973b2018-03-12 15:59:30 +02004213/****************************************************************/
4214/* Symmetric cryptography */
4215/****************************************************************/
4216
Gilles Peskinee553c652018-06-04 16:22:46 +02004217static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004218 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004219 psa_algorithm_t alg,
4220 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004221{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004222 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4223 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004224 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004225 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004226 size_t key_bits;
4227 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004228 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4229 PSA_KEY_USAGE_ENCRYPT :
4230 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004231
Steven Cooremand3feccd2020-09-01 15:56:14 +02004232 /* A context must be freshly initialized before it can be set up. */
4233 if( operation->alg != 0 )
4234 return( PSA_ERROR_BAD_STATE );
4235
Steven Cooremana07b9972020-09-10 14:54:14 +02004236 /* The requested algorithm must be one that can be processed by cipher. */
4237 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004238 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004239
Steven Cooremanef8575e2020-09-11 11:44:50 +02004240 /* Fetch key material from key storage. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02004241 status = psa_get_key_from_slot( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004242 if( status != PSA_SUCCESS )
4243 goto exit;
4244
4245 /* Initialize the operation struct members, except for alg. The alg member
4246 * is used to indicate to psa_cipher_abort that there are resources to free,
4247 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004248 operation->key_set = 0;
4249 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004250 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004251 operation->iv_size = 0;
4252 operation->block_size = 0;
4253 if( alg == PSA_ALG_ECB_NO_PADDING )
4254 operation->iv_required = 0;
4255 else
4256 operation->iv_required = 1;
4257
Steven Cooremana07b9972020-09-10 14:54:14 +02004258 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004259 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004260 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004261 slot,
4262 alg );
4263 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004264 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004265 slot,
4266 alg );
4267
Steven Cooremanef8575e2020-09-11 11:44:50 +02004268 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004269 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004270 /* Once the driver context is initialised, it needs to be freed using
4271 * psa_cipher_abort. Indicate this through setting alg. */
4272 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004273 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004274
Steven Cooremand3feccd2020-09-01 15:56:14 +02004275 if( status != PSA_ERROR_NOT_SUPPORTED ||
4276 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4277 goto exit;
4278
Steven Cooremana07b9972020-09-10 14:54:14 +02004279 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004280 * available for the given algorithm & key. */
4281 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004282
Steven Cooremana07b9972020-09-10 14:54:14 +02004283 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004284 * psa_cipher_abort. Indicate there is something to be freed through setting
4285 * alg, and indicate the operation is being done using mbedtls crypto through
4286 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004287 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004288 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004289
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004290 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004291 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004292 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004293 {
4294 status = PSA_ERROR_NOT_SUPPORTED;
4295 goto exit;
4296 }
mohammad1603503973b2018-03-12 15:59:30 +02004297
mohammad1603503973b2018-03-12 15:59:30 +02004298 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004299 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004300 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004301
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004302#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004303 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004304 {
4305 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004306 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004307 memcpy( keys, slot->data.key.data, 16 );
4308 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004309 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4310 keys,
4311 192, cipher_operation );
4312 }
4313 else
4314#endif
4315 {
4316 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004317 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004318 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004319 }
Moran Peker41deec42018-04-04 15:43:05 +03004320 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004321 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004322
mohammad16038481e742018-03-18 13:57:31 +02004323#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004324 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004325 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004326 case PSA_ALG_CBC_NO_PADDING:
4327 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4328 MBEDTLS_PADDING_NONE );
4329 break;
4330 case PSA_ALG_CBC_PKCS7:
4331 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4332 MBEDTLS_PADDING_PKCS7 );
4333 break;
4334 default:
4335 /* The algorithm doesn't involve padding. */
4336 ret = 0;
4337 break;
4338 }
4339 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004340 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004341#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4342
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004343 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004344 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004345 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4346 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004347 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004348 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004349 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004350#if defined(MBEDTLS_CHACHA20_C)
4351 else
4352 if( alg == PSA_ALG_CHACHA20 )
4353 operation->iv_size = 12;
4354#endif
mohammad1603503973b2018-03-12 15:59:30 +02004355
Steven Cooreman7df02922020-09-09 15:28:49 +02004356 status = PSA_SUCCESS;
4357
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004358exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004359 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004360 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004361 if( status == PSA_SUCCESS )
4362 {
4363 /* Update operation flags for both driver and software implementations */
4364 operation->key_set = 1;
4365 }
4366 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004367 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004368
4369 decrement_status = psa_decrement_key_slot_access_count( slot );
4370
4371 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004372}
4373
Gilles Peskinefe119512018-07-08 21:39:34 +02004374psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004375 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004376 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004377{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004378 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004379}
4380
Gilles Peskinefe119512018-07-08 21:39:34 +02004381psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004382 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004383 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004384{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004385 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004386}
4387
Gilles Peskinefe119512018-07-08 21:39:34 +02004388psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004389 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004390 size_t iv_size,
4391 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004392{
itayzafrir534bd7c2018-08-02 13:56:32 +03004393 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004395 if( operation->iv_set || ! operation->iv_required )
4396 {
4397 return( PSA_ERROR_BAD_STATE );
4398 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004399
Steven Cooremanef8575e2020-09-11 11:44:50 +02004400 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004401 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004402 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004403 iv,
4404 iv_size,
4405 iv_length );
4406 goto exit;
4407 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004408
Moran Peker41deec42018-04-04 15:43:05 +03004409 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004410 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004411 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004412 goto exit;
4413 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004414 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4415 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004416 if( ret != 0 )
4417 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004418 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004419 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004420 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004421
mohammad16038481e742018-03-18 13:57:31 +02004422 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004423 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004424
Moran Peker395db872018-05-31 14:07:14 +03004425exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004426 if( status == PSA_SUCCESS )
4427 operation->iv_set = 1;
4428 else
Moran Peker395db872018-05-31 14:07:14 +03004429 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004430 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004431}
4432
Gilles Peskinefe119512018-07-08 21:39:34 +02004433psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004434 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004435 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004436{
itayzafrir534bd7c2018-08-02 13:56:32 +03004437 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004439 if( operation->iv_set || ! operation->iv_required )
4440 {
4441 return( PSA_ERROR_BAD_STATE );
4442 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004443
Steven Cooremanef8575e2020-09-11 11:44:50 +02004444 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004445 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004446 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004447 iv,
4448 iv_length );
4449 goto exit;
4450 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004451
Moran Pekera28258c2018-05-29 16:25:04 +03004452 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004453 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004454 status = PSA_ERROR_INVALID_ARGUMENT;
4455 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004456 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004457 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4458 status = mbedtls_to_psa_error( ret );
4459exit:
4460 if( status == PSA_SUCCESS )
4461 operation->iv_set = 1;
4462 else
mohammad1603503973b2018-03-12 15:59:30 +02004463 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004464 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004465}
4466
Steven Cooreman177deba2020-09-07 17:14:14 +02004467/* Process input for which the algorithm is set to ECB mode. This requires
4468 * manual processing, since the PSA API is defined as being able to process
4469 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4470 * underlying mbedtls_cipher_update only takes full blocks. */
4471static psa_status_t psa_cipher_update_ecb_internal(
4472 mbedtls_cipher_context_t *ctx,
4473 const uint8_t *input,
4474 size_t input_length,
4475 uint8_t *output,
4476 size_t output_size,
4477 size_t *output_length )
4478{
4479 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4480 size_t block_size = ctx->cipher_info->block_size;
4481 size_t internal_output_length = 0;
4482 *output_length = 0;
4483
4484 if( input_length == 0 )
4485 {
4486 status = PSA_SUCCESS;
4487 goto exit;
4488 }
4489
4490 if( ctx->unprocessed_len > 0 )
4491 {
4492 /* Fill up to block size, and run the block if there's a full one. */
4493 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4494
4495 if( input_length < bytes_to_copy )
4496 bytes_to_copy = input_length;
4497
4498 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4499 input, bytes_to_copy );
4500 input_length -= bytes_to_copy;
4501 input += bytes_to_copy;
4502 ctx->unprocessed_len += bytes_to_copy;
4503
4504 if( ctx->unprocessed_len == block_size )
4505 {
4506 status = mbedtls_to_psa_error(
4507 mbedtls_cipher_update( ctx,
4508 ctx->unprocessed_data,
4509 block_size,
4510 output, &internal_output_length ) );
4511
4512 if( status != PSA_SUCCESS )
4513 goto exit;
4514
4515 output += internal_output_length;
4516 output_size -= internal_output_length;
4517 *output_length += internal_output_length;
4518 ctx->unprocessed_len = 0;
4519 }
4520 }
4521
4522 while( input_length >= block_size )
4523 {
4524 /* Run all full blocks we have, one by one */
4525 status = mbedtls_to_psa_error(
4526 mbedtls_cipher_update( ctx, input,
4527 block_size,
4528 output, &internal_output_length ) );
4529
4530 if( status != PSA_SUCCESS )
4531 goto exit;
4532
4533 input_length -= block_size;
4534 input += block_size;
4535
4536 output += internal_output_length;
4537 output_size -= internal_output_length;
4538 *output_length += internal_output_length;
4539 }
4540
4541 if( input_length > 0 )
4542 {
4543 /* Save unprocessed bytes for later processing */
4544 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4545 input, input_length );
4546 ctx->unprocessed_len += input_length;
4547 }
4548
4549 status = PSA_SUCCESS;
4550
4551exit:
4552 return( status );
4553}
4554
Gilles Peskinee553c652018-06-04 16:22:46 +02004555psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4556 const uint8_t *input,
4557 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004558 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004559 size_t output_size,
4560 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004561{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004563 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004564 if( operation->alg == 0 )
4565 {
4566 return( PSA_ERROR_BAD_STATE );
4567 }
4568 if( operation->iv_required && ! operation->iv_set )
4569 {
4570 return( PSA_ERROR_BAD_STATE );
4571 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004572
Steven Cooremanef8575e2020-09-11 11:44:50 +02004573 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004574 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004575 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004576 input,
4577 input_length,
4578 output,
4579 output_size,
4580 output_length );
4581 goto exit;
4582 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004583
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004584 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004585 {
4586 /* Take the unprocessed partial block left over from previous
4587 * update calls, if any, plus the input to this call. Remove
4588 * the last partial block, if any. You get the data that will be
4589 * output in this call. */
4590 expected_output_size =
4591 ( operation->ctx.cipher.unprocessed_len + input_length )
4592 / operation->block_size * operation->block_size;
4593 }
4594 else
4595 {
4596 expected_output_size = input_length;
4597 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004598
Gilles Peskine89d789c2018-06-04 17:17:16 +02004599 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004600 {
4601 status = PSA_ERROR_BUFFER_TOO_SMALL;
4602 goto exit;
4603 }
mohammad160382759612018-03-12 18:16:40 +02004604
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004605 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4606 {
4607 /* mbedtls_cipher_update has an API inconsistency: it will only
4608 * process a single block at a time in ECB mode. Abstract away that
4609 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004610 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4611 input,
4612 input_length,
4613 output,
4614 output_size,
4615 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004616 }
4617 else
4618 {
4619 status = mbedtls_to_psa_error(
4620 mbedtls_cipher_update( &operation->ctx.cipher, input,
4621 input_length, output, output_length ) );
4622 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004623exit:
4624 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004625 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004626 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004627}
4628
Gilles Peskinee553c652018-06-04 16:22:46 +02004629psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4630 uint8_t *output,
4631 size_t output_size,
4632 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004633{
David Saadab4ecc272019-02-14 13:48:10 +02004634 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004635 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004636 if( operation->alg == 0 )
4637 {
4638 return( PSA_ERROR_BAD_STATE );
4639 }
4640 if( operation->iv_required && ! operation->iv_set )
4641 {
4642 return( PSA_ERROR_BAD_STATE );
4643 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004644
Steven Cooremanef8575e2020-09-11 11:44:50 +02004645 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004646 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004647 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004648 output,
4649 output_size,
4650 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004651 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004652 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004653
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004654 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004655 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004656 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004657 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004658 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004659 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004660 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004661 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004662 }
4663
Steven Cooremanef8575e2020-09-11 11:44:50 +02004664 status = mbedtls_to_psa_error(
4665 mbedtls_cipher_finish( &operation->ctx.cipher,
4666 temp_output_buffer,
4667 output_length ) );
4668 if( status != PSA_SUCCESS )
4669 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004670
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004671 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004672 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004673 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004674 memcpy( output, temp_output_buffer, *output_length );
4675 else
Janos Follath315b51c2018-07-09 16:04:51 +01004676 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004677
Steven Cooremanef8575e2020-09-11 11:44:50 +02004678exit:
4679 if( operation->mbedtls_in_use == 1 )
4680 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004681
Steven Cooremanef8575e2020-09-11 11:44:50 +02004682 if( status == PSA_SUCCESS )
4683 return( psa_cipher_abort( operation ) );
4684 else
4685 {
4686 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004687 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004688
Steven Cooremanef8575e2020-09-11 11:44:50 +02004689 return( status );
4690 }
mohammad1603503973b2018-03-12 15:59:30 +02004691}
4692
Gilles Peskinee553c652018-06-04 16:22:46 +02004693psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4694{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004695 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004696 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004697 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004698 * in use. It's ok to call abort on such an object, and there's
4699 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004700 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004701 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004702
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004703 /* Sanity check (shouldn't happen: operation->alg should
4704 * always have been initialized to a valid value). */
4705 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4706 return( PSA_ERROR_BAD_STATE );
4707
Steven Cooremanef8575e2020-09-11 11:44:50 +02004708 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004709 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004710 else
4711 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004712
Moran Peker41deec42018-04-04 15:43:05 +03004713 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004714 operation->key_set = 0;
4715 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004716 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004717 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004718 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004719 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004720
Moran Peker395db872018-05-31 14:07:14 +03004721 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004722}
4723
Gilles Peskinea0655c32018-04-30 17:06:50 +02004724
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004725
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004726
mohammad16035955c982018-04-26 00:53:03 +03004727/****************************************************************/
4728/* AEAD */
4729/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004730
Gilles Peskineedf9a652018-08-17 18:11:56 +02004731typedef struct
4732{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004733 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004734 const mbedtls_cipher_info_t *cipher_info;
4735 union
4736 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004737 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004738#if defined(MBEDTLS_CCM_C)
4739 mbedtls_ccm_context ccm;
4740#endif /* MBEDTLS_CCM_C */
4741#if defined(MBEDTLS_GCM_C)
4742 mbedtls_gcm_context gcm;
4743#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004744#if defined(MBEDTLS_CHACHAPOLY_C)
4745 mbedtls_chachapoly_context chachapoly;
4746#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004747 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004748 psa_algorithm_t core_alg;
4749 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004750 uint8_t tag_length;
4751} aead_operation_t;
4752
Ronald Cronf95a2b12020-10-22 15:24:49 +02004753#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4754
Gilles Peskine30a9e412019-01-14 18:36:12 +01004755static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004756{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004757 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004758 {
4759#if defined(MBEDTLS_CCM_C)
4760 case PSA_ALG_CCM:
4761 mbedtls_ccm_free( &operation->ctx.ccm );
4762 break;
4763#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004764#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004765 case PSA_ALG_GCM:
4766 mbedtls_gcm_free( &operation->ctx.gcm );
4767 break;
4768#endif /* MBEDTLS_GCM_C */
4769 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004770
4771 psa_decrement_key_slot_access_count( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004772}
4773
4774static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004775 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004776 psa_key_usage_t usage,
4777 psa_algorithm_t alg )
4778{
4779 psa_status_t status;
4780 size_t key_bits;
4781 mbedtls_cipher_id_t cipher_id;
4782
Ronald Croncf56a0a2020-08-04 09:51:30 +02004783 status = psa_get_transparent_key( key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004784 if( status != PSA_SUCCESS )
4785 return( status );
4786
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004787 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004788
4789 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004790 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004791 &cipher_id );
4792 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004793 {
4794 status = PSA_ERROR_NOT_SUPPORTED;
4795 goto cleanup;
4796 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004797
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004798 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004799 {
4800#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004801 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4802 operation->core_alg = PSA_ALG_CCM;
4803 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004804 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4805 * The call to mbedtls_ccm_encrypt_and_tag or
4806 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004807 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004808 {
4809 status = PSA_ERROR_INVALID_ARGUMENT;
4810 goto cleanup;
4811 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004812 mbedtls_ccm_init( &operation->ctx.ccm );
4813 status = mbedtls_to_psa_error(
4814 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004815 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004816 (unsigned int) key_bits ) );
4817 if( status != 0 )
4818 goto cleanup;
4819 break;
4820#endif /* MBEDTLS_CCM_C */
4821
4822#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004823 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4824 operation->core_alg = PSA_ALG_GCM;
4825 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004826 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4827 * The call to mbedtls_gcm_crypt_and_tag or
4828 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004829 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004830 {
4831 status = PSA_ERROR_INVALID_ARGUMENT;
4832 goto cleanup;
4833 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004834 mbedtls_gcm_init( &operation->ctx.gcm );
4835 status = mbedtls_to_psa_error(
4836 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004837 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004838 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004839 if( status != 0 )
4840 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004841 break;
4842#endif /* MBEDTLS_GCM_C */
4843
Gilles Peskine26869f22019-05-06 15:25:00 +02004844#if defined(MBEDTLS_CHACHAPOLY_C)
4845 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4846 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4847 operation->full_tag_length = 16;
4848 /* We only support the default tag length. */
4849 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004850 {
4851 status = PSA_ERROR_NOT_SUPPORTED;
4852 goto cleanup;
4853 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004854 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4855 status = mbedtls_to_psa_error(
4856 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004857 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004858 if( status != 0 )
4859 goto cleanup;
4860 break;
4861#endif /* MBEDTLS_CHACHAPOLY_C */
4862
Gilles Peskineedf9a652018-08-17 18:11:56 +02004863 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004864 status = PSA_ERROR_NOT_SUPPORTED;
4865 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004866 }
4867
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004868 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4869 {
4870 status = PSA_ERROR_INVALID_ARGUMENT;
4871 goto cleanup;
4872 }
4873 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004874
Gilles Peskineedf9a652018-08-17 18:11:56 +02004875 return( PSA_SUCCESS );
4876
4877cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004878 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004879 return( status );
4880}
4881
Ronald Croncf56a0a2020-08-04 09:51:30 +02004882psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004883 psa_algorithm_t alg,
4884 const uint8_t *nonce,
4885 size_t nonce_length,
4886 const uint8_t *additional_data,
4887 size_t additional_data_length,
4888 const uint8_t *plaintext,
4889 size_t plaintext_length,
4890 uint8_t *ciphertext,
4891 size_t ciphertext_size,
4892 size_t *ciphertext_length )
4893{
mohammad16035955c982018-04-26 00:53:03 +03004894 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004895 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004896 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004897
mohammad1603f08a5502018-06-03 15:05:47 +03004898 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004899
Ronald Croncf56a0a2020-08-04 09:51:30 +02004900 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004901 if( status != PSA_SUCCESS )
4902 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004903
Gilles Peskineedf9a652018-08-17 18:11:56 +02004904 /* For all currently supported modes, the tag is at the end of the
4905 * ciphertext. */
4906 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4907 {
4908 status = PSA_ERROR_BUFFER_TOO_SMALL;
4909 goto exit;
4910 }
4911 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004912
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004913#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004914 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004915 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004916 status = mbedtls_to_psa_error(
4917 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4918 MBEDTLS_GCM_ENCRYPT,
4919 plaintext_length,
4920 nonce, nonce_length,
4921 additional_data, additional_data_length,
4922 plaintext, ciphertext,
4923 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004924 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004925 else
4926#endif /* MBEDTLS_GCM_C */
4927#if defined(MBEDTLS_CCM_C)
4928 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004929 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004930 status = mbedtls_to_psa_error(
4931 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4932 plaintext_length,
4933 nonce, nonce_length,
4934 additional_data,
4935 additional_data_length,
4936 plaintext, ciphertext,
4937 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004938 }
mohammad16035c8845f2018-05-09 05:40:09 -07004939 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004940#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004941#if defined(MBEDTLS_CHACHAPOLY_C)
4942 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4943 {
4944 if( nonce_length != 12 || operation.tag_length != 16 )
4945 {
4946 status = PSA_ERROR_NOT_SUPPORTED;
4947 goto exit;
4948 }
4949 status = mbedtls_to_psa_error(
4950 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4951 plaintext_length,
4952 nonce,
4953 additional_data,
4954 additional_data_length,
4955 plaintext,
4956 ciphertext,
4957 tag ) );
4958 }
4959 else
4960#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004961 {
mohammad1603554faad2018-06-03 15:07:38 +03004962 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004963 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004964
Gilles Peskineedf9a652018-08-17 18:11:56 +02004965 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4966 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004967
Gilles Peskineedf9a652018-08-17 18:11:56 +02004968exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004969 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004970 if( status == PSA_SUCCESS )
4971 *ciphertext_length = plaintext_length + operation.tag_length;
4972 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004973}
4974
Gilles Peskineee652a32018-06-01 19:23:52 +02004975/* Locate the tag in a ciphertext buffer containing the encrypted data
4976 * followed by the tag. Return the length of the part preceding the tag in
4977 * *plaintext_length. This is the size of the plaintext in modes where
4978 * the encrypted data has the same size as the plaintext, such as
4979 * CCM and GCM. */
4980static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4981 const uint8_t *ciphertext,
4982 size_t ciphertext_length,
4983 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004984 const uint8_t **p_tag )
4985{
4986 size_t payload_length;
4987 if( tag_length > ciphertext_length )
4988 return( PSA_ERROR_INVALID_ARGUMENT );
4989 payload_length = ciphertext_length - tag_length;
4990 if( payload_length > plaintext_size )
4991 return( PSA_ERROR_BUFFER_TOO_SMALL );
4992 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004993 return( PSA_SUCCESS );
4994}
4995
Ronald Croncf56a0a2020-08-04 09:51:30 +02004996psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004997 psa_algorithm_t alg,
4998 const uint8_t *nonce,
4999 size_t nonce_length,
5000 const uint8_t *additional_data,
5001 size_t additional_data_length,
5002 const uint8_t *ciphertext,
5003 size_t ciphertext_length,
5004 uint8_t *plaintext,
5005 size_t plaintext_size,
5006 size_t *plaintext_length )
5007{
mohammad16035955c982018-04-26 00:53:03 +03005008 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005009 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005010 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005011
Gilles Peskineee652a32018-06-01 19:23:52 +02005012 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005013
Ronald Croncf56a0a2020-08-04 09:51:30 +02005014 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005015 if( status != PSA_SUCCESS )
5016 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005017
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005018 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5019 ciphertext, ciphertext_length,
5020 plaintext_size, &tag );
5021 if( status != PSA_SUCCESS )
5022 goto exit;
5023
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005024#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005025 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005026 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005027 status = mbedtls_to_psa_error(
5028 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5029 ciphertext_length - operation.tag_length,
5030 nonce, nonce_length,
5031 additional_data,
5032 additional_data_length,
5033 tag, operation.tag_length,
5034 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005035 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005036 else
5037#endif /* MBEDTLS_GCM_C */
5038#if defined(MBEDTLS_CCM_C)
5039 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005040 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005041 status = mbedtls_to_psa_error(
5042 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5043 ciphertext_length - operation.tag_length,
5044 nonce, nonce_length,
5045 additional_data,
5046 additional_data_length,
5047 ciphertext, plaintext,
5048 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005049 }
mohammad160339574652018-06-01 04:39:53 -07005050 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005051#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005052#if defined(MBEDTLS_CHACHAPOLY_C)
5053 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5054 {
5055 if( nonce_length != 12 || operation.tag_length != 16 )
5056 {
5057 status = PSA_ERROR_NOT_SUPPORTED;
5058 goto exit;
5059 }
5060 status = mbedtls_to_psa_error(
5061 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5062 ciphertext_length - operation.tag_length,
5063 nonce,
5064 additional_data,
5065 additional_data_length,
5066 tag,
5067 ciphertext,
5068 plaintext ) );
5069 }
5070 else
5071#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005072 {
mohammad1603554faad2018-06-03 15:07:38 +03005073 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005074 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005075
Gilles Peskineedf9a652018-08-17 18:11:56 +02005076 if( status != PSA_SUCCESS && plaintext_size != 0 )
5077 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005078
Gilles Peskineedf9a652018-08-17 18:11:56 +02005079exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005080 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005081 if( status == PSA_SUCCESS )
5082 *plaintext_length = ciphertext_length - operation.tag_length;
5083 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005084}
5085
Gilles Peskinea0655c32018-04-30 17:06:50 +02005086
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005087
Gilles Peskine20035e32018-02-03 22:44:14 +01005088/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005089/* Generators */
5090/****************************************************************/
5091
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005092#define HKDF_STATE_INIT 0 /* no input yet */
5093#define HKDF_STATE_STARTED 1 /* got salt */
5094#define HKDF_STATE_KEYED 2 /* got key */
5095#define HKDF_STATE_OUTPUT 3 /* output started */
5096
Gilles Peskinecbe66502019-05-16 16:59:18 +02005097static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005098 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005099{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005100 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5101 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005102 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005103 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005104}
5105
5106
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005107psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005108{
5109 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005111 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005112 {
5113 /* The object has (apparently) been initialized but it is not
5114 * in use. It's ok to call abort on such an object, and there's
5115 * nothing to do. */
5116 }
5117 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02005118#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005119 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005120 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 mbedtls_free( operation->ctx.hkdf.info );
5122 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005123 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005124 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005125 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005126 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005127 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005128 if( operation->ctx.tls12_prf.seed != NULL )
5129 {
5130 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5131 operation->ctx.tls12_prf.seed_length );
5132 mbedtls_free( operation->ctx.tls12_prf.seed );
5133 }
5134
5135 if( operation->ctx.tls12_prf.label != NULL )
5136 {
5137 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5138 operation->ctx.tls12_prf.label_length );
5139 mbedtls_free( operation->ctx.tls12_prf.label );
5140 }
5141
5142 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5143
5144 /* We leave the fields Ai and output_block to be erased safely by the
5145 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005146 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005147 else
5148#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005149 {
5150 status = PSA_ERROR_BAD_STATE;
5151 }
Janos Follath083036a2019-06-11 10:22:26 +01005152 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005153 return( status );
5154}
5155
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005156psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005157 size_t *capacity)
5158{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005159 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005160 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005161 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005162 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005163 }
5164
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005165 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005166 return( PSA_SUCCESS );
5167}
5168
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005169psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005170 size_t capacity )
5171{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005172 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005173 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005174 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005175 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005176 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005177 return( PSA_SUCCESS );
5178}
5179
Gilles Peskinebef7f142018-07-12 17:22:21 +02005180#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005181/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005182 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005183static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005184 psa_algorithm_t hash_alg,
5185 uint8_t *output,
5186 size_t output_length )
5187{
5188 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5189 psa_status_t status;
5190
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005191 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5192 return( PSA_ERROR_BAD_STATE );
5193 hkdf->state = HKDF_STATE_OUTPUT;
5194
Gilles Peskinebef7f142018-07-12 17:22:21 +02005195 while( output_length != 0 )
5196 {
5197 /* Copy what remains of the current block */
5198 uint8_t n = hash_length - hkdf->offset_in_block;
5199 if( n > output_length )
5200 n = (uint8_t) output_length;
5201 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5202 output += n;
5203 output_length -= n;
5204 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005205 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005206 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005207 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005208 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005210 * object was corrupted or if this function is called directly
5211 * inside the library. */
5212 if( hkdf->block_number == 0xff )
5213 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005214
5215 /* We need a new block */
5216 ++hkdf->block_number;
5217 hkdf->offset_in_block = 0;
5218 status = psa_hmac_setup_internal( &hkdf->hmac,
5219 hkdf->prk, hash_length,
5220 hash_alg );
5221 if( status != PSA_SUCCESS )
5222 return( status );
5223 if( hkdf->block_number != 1 )
5224 {
5225 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5226 hkdf->output_block,
5227 hash_length );
5228 if( status != PSA_SUCCESS )
5229 return( status );
5230 }
5231 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5232 hkdf->info,
5233 hkdf->info_length );
5234 if( status != PSA_SUCCESS )
5235 return( status );
5236 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5237 &hkdf->block_number, 1 );
5238 if( status != PSA_SUCCESS )
5239 return( status );
5240 status = psa_hmac_finish_internal( &hkdf->hmac,
5241 hkdf->output_block,
5242 sizeof( hkdf->output_block ) );
5243 if( status != PSA_SUCCESS )
5244 return( status );
5245 }
5246
5247 return( PSA_SUCCESS );
5248}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005249
Janos Follath7742fee2019-06-17 12:58:10 +01005250static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5251 psa_tls12_prf_key_derivation_t *tls12_prf,
5252 psa_algorithm_t alg )
5253{
5254 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5255 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005256 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5257 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005258
Janos Follath7742fee2019-06-17 12:58:10 +01005259 /* We can't be wanting more output after block 0xff, otherwise
5260 * the capacity check in psa_key_derivation_output_bytes() would have
5261 * prevented this call. It could happen only if the operation
5262 * object was corrupted or if this function is called directly
5263 * inside the library. */
5264 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005265 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005266
5267 /* We need a new block */
5268 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005269 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005270
5271 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5272 *
5273 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5274 *
5275 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5276 * HMAC_hash(secret, A(2) + seed) +
5277 * HMAC_hash(secret, A(3) + seed) + ...
5278 *
5279 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005280 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005281 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005282 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005283 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005284 * is currently extracted as `output_block` and where i is
5285 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005286 */
5287
Janos Follathea29bfb2019-06-19 12:21:20 +01005288 /* Save the hash context before using it, to preserve the hash state with
5289 * only the inner padding in it. We need this, because inner padding depends
5290 * on the key (secret in the RFC's terminology). */
5291 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5292 if( status != PSA_SUCCESS )
5293 goto cleanup;
5294
5295 /* Calculate A(i) where i = tls12_prf->block_number. */
5296 if( tls12_prf->block_number == 1 )
5297 {
5298 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5299 * the variable seed and in this instance means it in the context of the
5300 * P_hash function, where seed = label + seed.) */
5301 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5302 tls12_prf->label, tls12_prf->label_length );
5303 if( status != PSA_SUCCESS )
5304 goto cleanup;
5305 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5306 tls12_prf->seed, tls12_prf->seed_length );
5307 if( status != PSA_SUCCESS )
5308 goto cleanup;
5309 }
5310 else
5311 {
5312 /* A(i) = HMAC_hash(secret, A(i-1)) */
5313 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5314 tls12_prf->Ai, hash_length );
5315 if( status != PSA_SUCCESS )
5316 goto cleanup;
5317 }
5318
5319 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5320 tls12_prf->Ai, hash_length );
5321 if( status != PSA_SUCCESS )
5322 goto cleanup;
5323 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5324 if( status != PSA_SUCCESS )
5325 goto cleanup;
5326
5327 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5328 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5329 tls12_prf->Ai, hash_length );
5330 if( status != PSA_SUCCESS )
5331 goto cleanup;
5332 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5333 tls12_prf->label, tls12_prf->label_length );
5334 if( status != PSA_SUCCESS )
5335 goto cleanup;
5336 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5337 tls12_prf->seed, tls12_prf->seed_length );
5338 if( status != PSA_SUCCESS )
5339 goto cleanup;
5340 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5341 tls12_prf->output_block, hash_length );
5342 if( status != PSA_SUCCESS )
5343 goto cleanup;
5344 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5345 if( status != PSA_SUCCESS )
5346 goto cleanup;
5347
Janos Follath7742fee2019-06-17 12:58:10 +01005348
5349cleanup:
5350
Janos Follathea29bfb2019-06-19 12:21:20 +01005351 cleanup_status = psa_hash_abort( &backup );
5352 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5353 status = cleanup_status;
5354
5355 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005356}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005357
Janos Follath844eb0e2019-06-19 12:10:49 +01005358static psa_status_t psa_key_derivation_tls12_prf_read(
5359 psa_tls12_prf_key_derivation_t *tls12_prf,
5360 psa_algorithm_t alg,
5361 uint8_t *output,
5362 size_t output_length )
5363{
5364 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5365 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5366 psa_status_t status;
5367 uint8_t offset, length;
5368
5369 while( output_length != 0 )
5370 {
5371 /* Check if we have fully processed the current block. */
5372 if( tls12_prf->left_in_block == 0 )
5373 {
5374 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5375 alg );
5376 if( status != PSA_SUCCESS )
5377 return( status );
5378
5379 continue;
5380 }
5381
5382 if( tls12_prf->left_in_block > output_length )
5383 length = (uint8_t) output_length;
5384 else
5385 length = tls12_prf->left_in_block;
5386
5387 offset = hash_length - tls12_prf->left_in_block;
5388 memcpy( output, tls12_prf->output_block + offset, length );
5389 output += length;
5390 output_length -= length;
5391 tls12_prf->left_in_block -= length;
5392 }
5393
5394 return( PSA_SUCCESS );
5395}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005396#endif /* MBEDTLS_MD_C */
5397
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005398psa_status_t psa_key_derivation_output_bytes(
5399 psa_key_derivation_operation_t *operation,
5400 uint8_t *output,
5401 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005402{
5403 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005404 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005405
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005406 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005407 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005408 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005409 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005410 }
5411
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005412 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005413 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005414 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005415 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005416 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005417 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005418 goto exit;
5419 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005420 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005421 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005422 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005423 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005424 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5425 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005426 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005427 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005428 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005429 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005430 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005431
Gilles Peskinebef7f142018-07-12 17:22:21 +02005432#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005433 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005434 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005435 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005436 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005437 output, output_length );
5438 }
Janos Follathadbec812019-06-14 11:05:39 +01005439 else
Janos Follathadbec812019-06-14 11:05:39 +01005440 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005441 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005442 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005443 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005444 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005445 output_length );
5446 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005447 else
5448#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005449 {
5450 return( PSA_ERROR_BAD_STATE );
5451 }
5452
5453exit:
5454 if( status != PSA_SUCCESS )
5455 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005456 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005457 * This allows us to differentiate between exhausted operations and
5458 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5459 * operations. */
5460 psa_algorithm_t alg = operation->alg;
5461 psa_key_derivation_abort( operation );
5462 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005463 memset( output, '!', output_length );
5464 }
5465 return( status );
5466}
5467
Gilles Peskine08542d82018-07-19 17:05:42 +02005468#if defined(MBEDTLS_DES_C)
5469static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5470{
5471 if( data_size >= 8 )
5472 mbedtls_des_key_set_parity( data );
5473 if( data_size >= 16 )
5474 mbedtls_des_key_set_parity( data + 8 );
5475 if( data_size >= 24 )
5476 mbedtls_des_key_set_parity( data + 16 );
5477}
5478#endif /* MBEDTLS_DES_C */
5479
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005480static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005481 psa_key_slot_t *slot,
5482 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005483 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005484{
5485 uint8_t *data = NULL;
5486 size_t bytes = PSA_BITS_TO_BYTES( bits );
5487 psa_status_t status;
5488
Gilles Peskine8e338702019-07-30 20:06:31 +02005489 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005490 return( PSA_ERROR_INVALID_ARGUMENT );
5491 if( bits % 8 != 0 )
5492 return( PSA_ERROR_INVALID_ARGUMENT );
5493 data = mbedtls_calloc( 1, bytes );
5494 if( data == NULL )
5495 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5496
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005497 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005498 if( status != PSA_SUCCESS )
5499 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005500#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005501 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005502 psa_des_set_key_parity( data, bytes );
5503#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005504 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005505
5506exit:
5507 mbedtls_free( data );
5508 return( status );
5509}
5510
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005511psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005512 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005513 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005514{
5515 psa_status_t status;
5516 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005517 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005518
5519 /* Reject any attempt to create a zero-length key so that we don't
5520 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5521 if( psa_get_key_bits( attributes ) == 0 )
5522 return( PSA_ERROR_INVALID_ARGUMENT );
5523
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005524 if( ! operation->can_output_key )
5525 return( PSA_ERROR_NOT_PERMITTED );
5526
Gilles Peskinedf179142019-07-15 22:02:14 +02005527 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005528 attributes, key, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005529#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5530 if( driver != NULL )
5531 {
5532 /* Deriving a key in a secure element is not implemented yet. */
5533 status = PSA_ERROR_NOT_SUPPORTED;
5534 }
5535#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005536 if( status == PSA_SUCCESS )
5537 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005538 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005539 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005540 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005541 }
5542 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005543 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005544 if( status != PSA_SUCCESS )
5545 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005546 psa_fail_key_creation( slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02005547 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005548 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005549
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005550 return( status );
5551}
5552
Gilles Peskineeab56e42018-07-12 17:12:33 +02005553
5554
5555/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005556/* Key derivation */
5557/****************************************************************/
5558
Gilles Peskine969c5d62019-01-16 15:53:06 +01005559static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005560 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005561 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005562{
Janos Follath5fe19732019-06-20 15:09:30 +01005563 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5564 * initialisers for this union leave some bytes unspecified.) */
5565 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5566
Gilles Peskine969c5d62019-01-16 15:53:06 +01005567 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005568#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005569 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5570 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5571 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005572 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005573 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005574 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5575 if( hash_size == 0 )
5576 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005577 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5578 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005579 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005580 {
5581 return( PSA_ERROR_NOT_SUPPORTED );
5582 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005583 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005584 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005585 }
5586#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005587 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005588 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005589}
5590
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005591psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005592 psa_algorithm_t alg )
5593{
5594 psa_status_t status;
5595
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005596 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005597 return( PSA_ERROR_BAD_STATE );
5598
Gilles Peskine6843c292019-01-18 16:44:49 +01005599 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5600 return( PSA_ERROR_INVALID_ARGUMENT );
5601 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005602 {
5603 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005604 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005605 }
5606 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5607 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005608 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005609 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005610 else
5611 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005612
5613 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005614 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005615 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005616}
5617
5618#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005619static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005620 psa_algorithm_t hash_alg,
5621 psa_key_derivation_step_t step,
5622 const uint8_t *data,
5623 size_t data_length )
5624{
5625 psa_status_t status;
5626 switch( step )
5627 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005628 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005629 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005630 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005631 status = psa_hmac_setup_internal( &hkdf->hmac,
5632 data, data_length,
5633 hash_alg );
5634 if( status != PSA_SUCCESS )
5635 return( status );
5636 hkdf->state = HKDF_STATE_STARTED;
5637 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005638 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005639 /* If no salt was provided, use an empty salt. */
5640 if( hkdf->state == HKDF_STATE_INIT )
5641 {
5642 status = psa_hmac_setup_internal( &hkdf->hmac,
5643 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005644 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005645 if( status != PSA_SUCCESS )
5646 return( status );
5647 hkdf->state = HKDF_STATE_STARTED;
5648 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005649 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005650 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005651 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5652 data, data_length );
5653 if( status != PSA_SUCCESS )
5654 return( status );
5655 status = psa_hmac_finish_internal( &hkdf->hmac,
5656 hkdf->prk,
5657 sizeof( hkdf->prk ) );
5658 if( status != PSA_SUCCESS )
5659 return( status );
5660 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5661 hkdf->block_number = 0;
5662 hkdf->state = HKDF_STATE_KEYED;
5663 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005664 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005665 if( hkdf->state == HKDF_STATE_OUTPUT )
5666 return( PSA_ERROR_BAD_STATE );
5667 if( hkdf->info_set )
5668 return( PSA_ERROR_BAD_STATE );
5669 hkdf->info_length = data_length;
5670 if( data_length != 0 )
5671 {
5672 hkdf->info = mbedtls_calloc( 1, data_length );
5673 if( hkdf->info == NULL )
5674 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5675 memcpy( hkdf->info, data, data_length );
5676 }
5677 hkdf->info_set = 1;
5678 return( PSA_SUCCESS );
5679 default:
5680 return( PSA_ERROR_INVALID_ARGUMENT );
5681 }
5682}
Janos Follathb03233e2019-06-11 15:30:30 +01005683
Janos Follathf08e2652019-06-13 09:05:41 +01005684static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5685 const uint8_t *data,
5686 size_t data_length )
5687{
5688 if( prf->state != TLS12_PRF_STATE_INIT )
5689 return( PSA_ERROR_BAD_STATE );
5690
Janos Follathd6dce9f2019-07-04 09:11:38 +01005691 if( data_length != 0 )
5692 {
5693 prf->seed = mbedtls_calloc( 1, data_length );
5694 if( prf->seed == NULL )
5695 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005696
Janos Follathd6dce9f2019-07-04 09:11:38 +01005697 memcpy( prf->seed, data, data_length );
5698 prf->seed_length = data_length;
5699 }
Janos Follathf08e2652019-06-13 09:05:41 +01005700
5701 prf->state = TLS12_PRF_STATE_SEED_SET;
5702
5703 return( PSA_SUCCESS );
5704}
5705
Janos Follath81550542019-06-13 14:26:34 +01005706static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5707 psa_algorithm_t hash_alg,
5708 const uint8_t *data,
5709 size_t data_length )
5710{
5711 psa_status_t status;
5712 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5713 return( PSA_ERROR_BAD_STATE );
5714
5715 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5716 if( status != PSA_SUCCESS )
5717 return( status );
5718
5719 prf->state = TLS12_PRF_STATE_KEY_SET;
5720
5721 return( PSA_SUCCESS );
5722}
5723
Janos Follath6660f0e2019-06-17 08:44:03 +01005724static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5725 psa_tls12_prf_key_derivation_t *prf,
5726 psa_algorithm_t hash_alg,
5727 const uint8_t *data,
5728 size_t data_length )
5729{
5730 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005731 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5732 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005733
5734 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5735 return( PSA_ERROR_INVALID_ARGUMENT );
5736
5737 /* Quoting RFC 4279, Section 2:
5738 *
5739 * The premaster secret is formed as follows: if the PSK is N octets
5740 * long, concatenate a uint16 with the value N, N zero octets, a second
5741 * uint16 with the value N, and the PSK itself.
5742 */
5743
Janos Follath40e13932019-06-26 13:22:29 +01005744 *cur++ = ( data_length >> 8 ) & 0xff;
5745 *cur++ = ( data_length >> 0 ) & 0xff;
5746 memset( cur, 0, data_length );
5747 cur += data_length;
5748 *cur++ = pms[0];
5749 *cur++ = pms[1];
5750 memcpy( cur, data, data_length );
5751 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005752
Janos Follath40e13932019-06-26 13:22:29 +01005753 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005754
5755 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5756 return( status );
5757}
5758
Janos Follath63028dd2019-06-13 09:15:47 +01005759static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5760 const uint8_t *data,
5761 size_t data_length )
5762{
5763 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5764 return( PSA_ERROR_BAD_STATE );
5765
Janos Follathd6dce9f2019-07-04 09:11:38 +01005766 if( data_length != 0 )
5767 {
5768 prf->label = mbedtls_calloc( 1, data_length );
5769 if( prf->label == NULL )
5770 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005771
Janos Follathd6dce9f2019-07-04 09:11:38 +01005772 memcpy( prf->label, data, data_length );
5773 prf->label_length = data_length;
5774 }
Janos Follath63028dd2019-06-13 09:15:47 +01005775
5776 prf->state = TLS12_PRF_STATE_LABEL_SET;
5777
5778 return( PSA_SUCCESS );
5779}
5780
Janos Follathb03233e2019-06-11 15:30:30 +01005781static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5782 psa_algorithm_t hash_alg,
5783 psa_key_derivation_step_t step,
5784 const uint8_t *data,
5785 size_t data_length )
5786{
Janos Follathb03233e2019-06-11 15:30:30 +01005787 switch( step )
5788 {
Janos Follathf08e2652019-06-13 09:05:41 +01005789 case PSA_KEY_DERIVATION_INPUT_SEED:
5790 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005791 case PSA_KEY_DERIVATION_INPUT_SECRET:
5792 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005793 case PSA_KEY_DERIVATION_INPUT_LABEL:
5794 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005795 default:
5796 return( PSA_ERROR_INVALID_ARGUMENT );
5797 }
5798}
Janos Follath6660f0e2019-06-17 08:44:03 +01005799
5800static psa_status_t psa_tls12_prf_psk_to_ms_input(
5801 psa_tls12_prf_key_derivation_t *prf,
5802 psa_algorithm_t hash_alg,
5803 psa_key_derivation_step_t step,
5804 const uint8_t *data,
5805 size_t data_length )
5806{
5807 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005808 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005809 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5810 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005811 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005812
5813 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5814}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005815#endif /* MBEDTLS_MD_C */
5816
Gilles Peskineb8965192019-09-24 16:21:10 +02005817/** Check whether the given key type is acceptable for the given
5818 * input step of a key derivation.
5819 *
5820 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5821 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5822 * Both secret and non-secret inputs can alternatively have the type
5823 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5824 * that the input was passed as a buffer rather than via a key object.
5825 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005826static int psa_key_derivation_check_input_type(
5827 psa_key_derivation_step_t step,
5828 psa_key_type_t key_type )
5829{
5830 switch( step )
5831 {
5832 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005833 if( key_type == PSA_KEY_TYPE_DERIVE )
5834 return( PSA_SUCCESS );
5835 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005836 return( PSA_SUCCESS );
5837 break;
5838 case PSA_KEY_DERIVATION_INPUT_LABEL:
5839 case PSA_KEY_DERIVATION_INPUT_SALT:
5840 case PSA_KEY_DERIVATION_INPUT_INFO:
5841 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005842 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5843 return( PSA_SUCCESS );
5844 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005845 return( PSA_SUCCESS );
5846 break;
5847 }
5848 return( PSA_ERROR_INVALID_ARGUMENT );
5849}
5850
Janos Follathb80a94e2019-06-12 15:54:46 +01005851static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005852 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005853 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005854 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005855 const uint8_t *data,
5856 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005857{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005858 psa_status_t status;
5859 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5860
5861 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005862 if( status != PSA_SUCCESS )
5863 goto exit;
5864
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005865#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005866 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005867 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005868 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005869 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005870 step, data, data_length );
5871 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005872 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005873 {
Janos Follathb03233e2019-06-11 15:30:30 +01005874 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5875 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5876 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005877 }
5878 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5879 {
5880 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5881 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5882 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005883 }
5884 else
5885#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005886 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005887 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005888 return( PSA_ERROR_BAD_STATE );
5889 }
5890
Gilles Peskine224b0d62019-09-23 18:13:17 +02005891exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005892 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005893 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005894 return( status );
5895}
5896
Janos Follath51f4a0f2019-06-14 11:35:55 +01005897psa_status_t psa_key_derivation_input_bytes(
5898 psa_key_derivation_operation_t *operation,
5899 psa_key_derivation_step_t step,
5900 const uint8_t *data,
5901 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005902{
Gilles Peskineb8965192019-09-24 16:21:10 +02005903 return( psa_key_derivation_input_internal( operation, step,
5904 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005905 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005906}
5907
Janos Follath51f4a0f2019-06-14 11:35:55 +01005908psa_status_t psa_key_derivation_input_key(
5909 psa_key_derivation_operation_t *operation,
5910 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005911 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005912{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005913 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5914 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005915 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005916
Ronald Croncf56a0a2020-08-04 09:51:30 +02005917 status = psa_get_transparent_key( key, &slot,
5918 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005919 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005920 {
5921 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005922 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005923 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005924
5925 /* Passing a key object as a SECRET input unlocks the permission
5926 * to output to a key object. */
5927 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5928 operation->can_output_key = 1;
5929
Ronald Cronf95a2b12020-10-22 15:24:49 +02005930 status = psa_key_derivation_input_internal( operation,
5931 step, slot->attr.type,
5932 slot->data.key.data,
5933 slot->data.key.bytes );
5934
5935 decrement_status = psa_decrement_key_slot_access_count( slot );
5936
5937 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005938}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005939
5940
5941
5942/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005943/* Key agreement */
5944/****************************************************************/
5945
Gilles Peskinea05219c2018-11-16 16:02:56 +01005946#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005947static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5948 size_t peer_key_length,
5949 const mbedtls_ecp_keypair *our_key,
5950 uint8_t *shared_secret,
5951 size_t shared_secret_size,
5952 size_t *shared_secret_length )
5953{
Steven Cooremand4867872020-08-05 16:31:39 +02005954 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005955 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005956 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005957 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005958 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005959 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005960
Steven Cooreman4fed4552020-08-03 14:46:03 +02005961 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5962 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005963 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005964 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005965 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005966 goto exit;
5967
Jaeden Amero97271b32019-01-10 19:38:51 +00005968 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005969 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005970 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005971 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005972 status = mbedtls_to_psa_error(
5973 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5974 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005975 goto exit;
5976
Jaeden Amero97271b32019-01-10 19:38:51 +00005977 status = mbedtls_to_psa_error(
5978 mbedtls_ecdh_calc_secret( &ecdh,
5979 shared_secret_length,
5980 shared_secret, shared_secret_size,
5981 mbedtls_ctr_drbg_random,
5982 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005983 if( status != PSA_SUCCESS )
5984 goto exit;
5985 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5986 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005987
5988exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005989 if( status != PSA_SUCCESS )
5990 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005991 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005992 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005993 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005994
Jaeden Amero97271b32019-01-10 19:38:51 +00005995 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005996}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005997#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005998
Gilles Peskine01d718c2018-09-18 12:01:02 +02005999#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6000
Gilles Peskine0216fe12019-04-11 21:23:21 +02006001static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6002 psa_key_slot_t *private_key,
6003 const uint8_t *peer_key,
6004 size_t peer_key_length,
6005 uint8_t *shared_secret,
6006 size_t shared_secret_size,
6007 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006008{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006009 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006010 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006011#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006012 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006013 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006014 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006015 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006016 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006017 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006018 private_key->data.key.data,
6019 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006020 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006021 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006022 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006023 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006024 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006025 shared_secret, shared_secret_size,
6026 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006027 mbedtls_ecp_keypair_free( ecp );
6028 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006029 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006030#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006031 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006032 (void) private_key;
6033 (void) peer_key;
6034 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006035 (void) shared_secret;
6036 (void) shared_secret_size;
6037 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006038 return( PSA_ERROR_NOT_SUPPORTED );
6039 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006040}
6041
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006042/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006043 * to potentially free embedded data structures and wipe confidential data.
6044 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006045static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006046 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006047 psa_key_slot_t *private_key,
6048 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006049 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006050{
6051 psa_status_t status;
6052 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6053 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006054 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006055
6056 /* Step 1: run the secret agreement algorithm to generate the shared
6057 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006058 status = psa_key_agreement_raw_internal( ka_alg,
6059 private_key,
6060 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006061 shared_secret,
6062 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006063 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006064 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006065 goto exit;
6066
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006067 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006068 * the shared secret. A shared secret is permitted wherever a key
6069 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006070 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006071 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006072 shared_secret,
6073 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006074exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006075 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006076 return( status );
6077}
6078
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006079psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006080 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006081 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006082 const uint8_t *peer_key,
6083 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006084{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006085 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6086 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006087 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006088
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006089 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006090 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02006091 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006092 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006093 if( status != PSA_SUCCESS )
6094 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006095 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006096 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006097 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006098 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006099 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006100 else
6101 {
6102 /* If a private key has been added as SECRET, we allow the derived
6103 * key material to be used as a key in PSA Crypto. */
6104 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6105 operation->can_output_key = 1;
6106 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006107
6108 decrement_status = psa_decrement_key_slot_access_count( slot );
6109
6110 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006111}
6112
Gilles Peskinebe697d82019-05-16 18:00:41 +02006113psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006114 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006115 const uint8_t *peer_key,
6116 size_t peer_key_length,
6117 uint8_t *output,
6118 size_t output_size,
6119 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006120{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006121 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6122 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
6123 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006124
6125 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6126 {
6127 status = PSA_ERROR_INVALID_ARGUMENT;
6128 goto exit;
6129 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02006130 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006131 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006132 if( status != PSA_SUCCESS )
6133 goto exit;
6134
6135 status = psa_key_agreement_raw_internal( alg, slot,
6136 peer_key, peer_key_length,
6137 output, output_size,
6138 output_length );
6139
6140exit:
6141 if( status != PSA_SUCCESS )
6142 {
6143 /* If an error happens and is not handled properly, the output
6144 * may be used as a key to protect sensitive data. Arrange for such
6145 * a key to be random, which is likely to result in decryption or
6146 * verification errors. This is better than filling the buffer with
6147 * some constant data such as zeros, which would result in the data
6148 * being protected with a reproducible, easily knowable key.
6149 */
6150 psa_generate_random( output, output_size );
6151 *output_length = output_size;
6152 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006153
6154 decrement_status = psa_decrement_key_slot_access_count( slot );
6155
6156 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006157}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006158
6159
6160/****************************************************************/
6161/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006162/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006163
Gilles Peskine4c317f42018-07-12 01:24:09 +02006164psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006165 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006166{
Janos Follath24eed8d2019-11-22 13:21:35 +00006167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006168 GUARD_MODULE_INITIALIZED;
6169
Gilles Peskinef181eca2019-08-07 13:49:00 +02006170 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6171 {
6172 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6173 output,
6174 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6175 if( ret != 0 )
6176 return( mbedtls_to_psa_error( ret ) );
6177 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6178 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6179 }
6180
Gilles Peskinee59236f2018-01-27 23:32:46 +01006181 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6182 return( mbedtls_to_psa_error( ret ) );
6183}
6184
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006185#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6186#include "mbedtls/entropy_poll.h"
6187
Gilles Peskine7228da22019-07-15 11:06:15 +02006188psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006189 size_t seed_size )
6190{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006191 if( global_data.initialized )
6192 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006193
6194 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6195 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6196 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6197 return( PSA_ERROR_INVALID_ARGUMENT );
6198
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006199 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006200}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006201#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006202
Gilles Peskinee56e8782019-04-26 17:34:02 +02006203#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
6204static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6205 size_t domain_parameters_size,
6206 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006207{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006208 size_t i;
6209 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006210
Gilles Peskinee56e8782019-04-26 17:34:02 +02006211 if( domain_parameters_size == 0 )
6212 {
6213 *exponent = 65537;
6214 return( PSA_SUCCESS );
6215 }
6216
6217 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6218 * support values that fit in a 32-bit integer, which is larger than
6219 * int on just about every platform anyway. */
6220 if( domain_parameters_size > sizeof( acc ) )
6221 return( PSA_ERROR_NOT_SUPPORTED );
6222 for( i = 0; i < domain_parameters_size; i++ )
6223 acc = ( acc << 8 ) | domain_parameters[i];
6224 if( acc > INT_MAX )
6225 return( PSA_ERROR_NOT_SUPPORTED );
6226 *exponent = acc;
6227 return( PSA_SUCCESS );
6228}
6229#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6230
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006231static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006232 psa_key_slot_t *slot, size_t bits,
6233 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006234{
Gilles Peskine8e338702019-07-30 20:06:31 +02006235 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006236
Gilles Peskinee56e8782019-04-26 17:34:02 +02006237 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006238 return( PSA_ERROR_INVALID_ARGUMENT );
6239
Gilles Peskinee59236f2018-01-27 23:32:46 +01006240 if( key_type_is_raw_bytes( type ) )
6241 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006242 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006243
6244 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006245 if( status != PSA_SUCCESS )
6246 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006247
6248 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006249 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6250 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006251 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006252
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006253 status = psa_generate_random( slot->data.key.data,
6254 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006255 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006256 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006257
6258 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006259#if defined(MBEDTLS_DES_C)
6260 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006261 psa_des_set_key_parity( slot->data.key.data,
6262 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006263#endif /* MBEDTLS_DES_C */
6264 }
6265 else
6266
6267#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006268 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006269 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006270 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006272 int exponent;
6273 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006274 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6275 return( PSA_ERROR_NOT_SUPPORTED );
6276 /* Accept only byte-aligned keys, for the same reasons as
6277 * in psa_import_rsa_key(). */
6278 if( bits % 8 != 0 )
6279 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006280 status = psa_read_rsa_exponent( domain_parameters,
6281 domain_parameters_size,
6282 &exponent );
6283 if( status != PSA_SUCCESS )
6284 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006285 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6286 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006287 mbedtls_ctr_drbg_random,
6288 &global_data.ctr_drbg,
6289 (unsigned int) bits,
6290 exponent );
6291 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006292 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006293
6294 /* Make sure to always have an export representation available */
6295 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6296
Steven Cooreman75b74362020-07-28 14:30:13 +02006297 status = psa_allocate_buffer_to_slot( slot, bytes );
6298 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006299 {
6300 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006301 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006302 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006303
6304 status = psa_export_rsa_key( type,
6305 &rsa,
6306 slot->data.key.data,
6307 bytes,
6308 &slot->data.key.bytes );
6309 mbedtls_rsa_free( &rsa );
6310 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006311 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006312 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006313 }
6314 else
6315#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6316
6317#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006318 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006319 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006320 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006321 mbedtls_ecp_group_id grp_id =
6322 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006323 const mbedtls_ecp_curve_info *curve_info =
6324 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006325 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006327 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006328 return( PSA_ERROR_NOT_SUPPORTED );
6329 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6330 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006331 mbedtls_ecp_keypair_init( &ecp );
6332 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006333 mbedtls_ctr_drbg_random,
6334 &global_data.ctr_drbg );
6335 if( ret != 0 )
6336 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006337 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006338 return( mbedtls_to_psa_error( ret ) );
6339 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006340
6341
6342 /* Make sure to always have an export representation available */
6343 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006344 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6345 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006346 {
6347 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006348 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006349 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006350
6351 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006352 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6353
6354 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006355 if( status != PSA_SUCCESS ) {
6356 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006357 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006358 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006359 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006360 }
6361 else
6362#endif /* MBEDTLS_ECP_C */
Steven Cooreman29149862020-08-05 15:43:42 +02006363 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006364 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006365 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006366
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006367 return( PSA_SUCCESS );
6368}
Darryl Green0c6575a2018-11-07 16:05:30 +00006369
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006370psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006371 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006372{
6373 psa_status_t status;
6374 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006375 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006376
Gilles Peskine0f84d622019-09-12 19:03:13 +02006377 /* Reject any attempt to create a zero-length key so that we don't
6378 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6379 if( psa_get_key_bits( attributes ) == 0 )
6380 return( PSA_ERROR_INVALID_ARGUMENT );
6381
Gilles Peskinedf179142019-07-15 22:02:14 +02006382 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006383 attributes, key, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006384 if( status != PSA_SUCCESS )
6385 goto exit;
6386
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006387 status = psa_driver_wrapper_generate_key( attributes,
6388 slot );
6389 if( status != PSA_ERROR_NOT_SUPPORTED ||
6390 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6391 goto exit;
6392
6393 status = psa_generate_key_internal(
6394 slot, attributes->core.bits,
6395 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006396
6397exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006398 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006399 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006400 if( status != PSA_SUCCESS )
6401 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006402 psa_fail_key_creation( slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02006403 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006404 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006405
Darryl Green0c6575a2018-11-07 16:05:30 +00006406 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006407}
6408
6409
Gilles Peskinee59236f2018-01-27 23:32:46 +01006410
6411/****************************************************************/
6412/* Module setup */
6413/****************************************************************/
6414
Gilles Peskine5e769522018-11-20 21:59:56 +01006415psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6416 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6417 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6418{
6419 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6420 return( PSA_ERROR_BAD_STATE );
6421 global_data.entropy_init = entropy_init;
6422 global_data.entropy_free = entropy_free;
6423 return( PSA_SUCCESS );
6424}
6425
Gilles Peskinee59236f2018-01-27 23:32:46 +01006426void mbedtls_psa_crypto_free( void )
6427{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006428 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006429 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6430 {
6431 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006432 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006433 }
6434 /* Wipe all remaining data, including configuration.
6435 * In particular, this sets all state indicator to the value
6436 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006437 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006438#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006439 /* Unregister all secure element drivers, so that we restart from
6440 * a pristine state. */
6441 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006442#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006443}
6444
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006445#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6446/** Recover a transaction that was interrupted by a power failure.
6447 *
6448 * This function is called during initialization, before psa_crypto_init()
6449 * returns. If this function returns a failure status, the initialization
6450 * fails.
6451 */
6452static psa_status_t psa_crypto_recover_transaction(
6453 const psa_crypto_transaction_t *transaction )
6454{
6455 switch( transaction->unknown.type )
6456 {
6457 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6458 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006459 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006460 * is implemented.
6461 * https://github.com/ARMmbed/mbed-crypto/issues/218
6462 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006463 default:
6464 /* We found an unsupported transaction in the storage.
6465 * We don't know what state the storage is in. Give up. */
6466 return( PSA_ERROR_STORAGE_FAILURE );
6467 }
6468}
6469#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6470
Gilles Peskinee59236f2018-01-27 23:32:46 +01006471psa_status_t psa_crypto_init( void )
6472{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006473 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006474 const unsigned char drbg_seed[] = "PSA";
6475
Gilles Peskinec6b69072018-11-20 21:42:52 +01006476 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006477 if( global_data.initialized != 0 )
6478 return( PSA_SUCCESS );
6479
Gilles Peskine5e769522018-11-20 21:59:56 +01006480 /* Set default configuration if
6481 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6482 if( global_data.entropy_init == NULL )
6483 global_data.entropy_init = mbedtls_entropy_init;
6484 if( global_data.entropy_free == NULL )
6485 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006486
Gilles Peskinec6b69072018-11-20 21:42:52 +01006487 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006488 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006489#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6490 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6491 /* The PSA entropy injection feature depends on using NV seed as an entropy
6492 * source. Add NV seed as an entropy source for PSA entropy injection. */
6493 mbedtls_entropy_add_source( &global_data.entropy,
6494 mbedtls_nv_seed_poll, NULL,
6495 MBEDTLS_ENTROPY_BLOCK_SIZE,
6496 MBEDTLS_ENTROPY_SOURCE_STRONG );
6497#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006498 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006499 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006500 status = mbedtls_to_psa_error(
6501 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6502 mbedtls_entropy_func,
6503 &global_data.entropy,
6504 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6505 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006506 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006507 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006508
Gilles Peskine66fb1262018-12-10 16:29:04 +01006509 status = psa_initialize_key_slots( );
6510 if( status != PSA_SUCCESS )
6511 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006512
Gilles Peskined9348f22019-10-01 15:22:29 +02006513#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6514 status = psa_init_all_se_drivers( );
6515 if( status != PSA_SUCCESS )
6516 goto exit;
6517#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6518
Gilles Peskine4b734222019-07-24 15:56:31 +02006519#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006520 status = psa_crypto_load_transaction( );
6521 if( status == PSA_SUCCESS )
6522 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006523 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6524 if( status != PSA_SUCCESS )
6525 goto exit;
6526 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006527 }
6528 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6529 {
6530 /* There's no transaction to complete. It's all good. */
6531 status = PSA_SUCCESS;
6532 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006533#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006534
Gilles Peskinec6b69072018-11-20 21:42:52 +01006535 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006536 global_data.initialized = 1;
6537
6538exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006539 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006540 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006541 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006542}
6543
6544#endif /* MBEDTLS_PSA_CRYPTO_C */