blob: a510e3c7856eed7cc05736764088c5c4bce4cb31 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
itayzafrir7723ab12019-02-14 10:28:02 +020025#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010026#include "psa/crypto.h"
27
Gilles Peskine039b90c2018-12-07 18:24:41 +010028#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010029#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020030#include "psa_crypto_driver_wrappers.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020031#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020032#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020033#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010034#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010035/* Include internal declarations that are useful for implementing persistently
36 * stored keys. */
37#include "psa_crypto_storage.h"
38
Gilles Peskineb46bef22019-07-30 21:32:04 +020039#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include <stdlib.h>
41#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020043#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#define mbedtls_calloc calloc
45#define mbedtls_free free
46#endif
47
Gilles Peskinea5905292018-02-07 20:59:33 +010048#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020049#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000050#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020051#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/blowfish.h"
53#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020054#include "mbedtls/chacha20.h"
55#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/cipher.h"
57#include "mbedtls/ccm.h"
58#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010059#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020061#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010062#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/error.h"
65#include "mbedtls/gcm.h"
66#include "mbedtls/md2.h"
67#include "mbedtls/md4.h"
68#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010069#include "mbedtls/md.h"
70#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010071#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010073#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000074#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/sha1.h"
78#include "mbedtls/sha256.h"
79#include "mbedtls/sha512.h"
80#include "mbedtls/xtea.h"
81
Gilles Peskine996deb12018-08-01 15:45:45 +020082#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
83
Gilles Peskine9ef733f2018-02-07 21:05:37 +010084/* constant-time buffer comparison */
85static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
86{
87 size_t i;
88 unsigned char diff = 0;
89
90 for( i = 0; i < n; i++ )
91 diff |= a[i] ^ b[i];
92
93 return( diff );
94}
95
96
97
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010098/****************************************************************/
99/* Global data, support functions and library management */
100/****************************************************************/
101
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102static int key_type_is_raw_bytes( psa_key_type_t type )
103{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200104 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105}
106
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100107/* Values for psa_global_data_t::rng_state */
108#define RNG_NOT_INITIALIZED 0
109#define RNG_INITIALIZED 1
110#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100111
Gilles Peskine2d277862018-06-18 15:41:12 +0200112typedef struct
113{
Gilles Peskine5e769522018-11-20 21:59:56 +0100114 void (* entropy_init )( mbedtls_entropy_context *ctx );
115 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100116 mbedtls_entropy_context entropy;
117 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100118 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100119 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120} psa_global_data_t;
121
122static psa_global_data_t global_data;
123
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124#define GUARD_MODULE_INITIALIZED \
125 if( global_data.initialized == 0 ) \
126 return( PSA_ERROR_BAD_STATE );
127
Steven Cooreman01164162020-07-20 15:31:37 +0200128psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100129{
Gilles Peskinea5905292018-02-07 20:59:33 +0100130 /* If there's both a high-level code and low-level code, dispatch on
131 * the high-level code. */
132 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133 {
134 case 0:
135 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100136
137 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
138 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
139 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
140 return( PSA_ERROR_NOT_SUPPORTED );
141 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
142 return( PSA_ERROR_HARDWARE_FAILURE );
143
144 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
Gilles Peskine9a944802018-06-21 09:35:35 +0200147 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
148 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
149 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
150 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
151 case MBEDTLS_ERR_ASN1_INVALID_DATA:
152 return( PSA_ERROR_INVALID_ARGUMENT );
153 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
154 return( PSA_ERROR_INSUFFICIENT_MEMORY );
155 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
156 return( PSA_ERROR_BUFFER_TOO_SMALL );
157
Jaeden Amero93e21112019-02-20 13:57:28 +0000158#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000159 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
161 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
162#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
164 return( PSA_ERROR_NOT_SUPPORTED );
165 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
166 return( PSA_ERROR_HARDWARE_FAILURE );
167
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000169 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
171 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
174 return( PSA_ERROR_NOT_SUPPORTED );
175 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
176 return( PSA_ERROR_HARDWARE_FAILURE );
177
178 case MBEDTLS_ERR_CCM_BAD_INPUT:
179 return( PSA_ERROR_INVALID_ARGUMENT );
180 case MBEDTLS_ERR_CCM_AUTH_FAILED:
181 return( PSA_ERROR_INVALID_SIGNATURE );
182 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
183 return( PSA_ERROR_HARDWARE_FAILURE );
184
Gilles Peskine26869f22019-05-06 15:25:00 +0200185 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
186 return( PSA_ERROR_INVALID_ARGUMENT );
187
188 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
189 return( PSA_ERROR_BAD_STATE );
190 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
191 return( PSA_ERROR_INVALID_SIGNATURE );
192
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
194 return( PSA_ERROR_NOT_SUPPORTED );
195 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
198 return( PSA_ERROR_INSUFFICIENT_MEMORY );
199 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
200 return( PSA_ERROR_INVALID_PADDING );
201 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200202 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
205 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200206 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
208 return( PSA_ERROR_HARDWARE_FAILURE );
209
210 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
214 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
215 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
216 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
217 return( PSA_ERROR_NOT_SUPPORTED );
218 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
220
221 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
Gilles Peskinee59236f2018-01-27 23:32:46 +0100226 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
227 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
228 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
229 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100230
231 case MBEDTLS_ERR_GCM_AUTH_FAILED:
232 return( PSA_ERROR_INVALID_SIGNATURE );
233 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200234 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
243 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
244 return( PSA_ERROR_NOT_SUPPORTED );
245 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
246 return( PSA_ERROR_INVALID_ARGUMENT );
247 case MBEDTLS_ERR_MD_ALLOC_FAILED:
248 return( PSA_ERROR_INSUFFICIENT_MEMORY );
249 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
250 return( PSA_ERROR_STORAGE_FAILURE );
251 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
255 return( PSA_ERROR_STORAGE_FAILURE );
256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
261 return( PSA_ERROR_BUFFER_TOO_SMALL );
262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
269 return( PSA_ERROR_INSUFFICIENT_MEMORY );
270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
282 return( PSA_ERROR_NOT_SUPPORTED );
283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
285 return( PSA_ERROR_NOT_PERMITTED );
286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
291 return( PSA_ERROR_NOT_SUPPORTED );
292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
293 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
300 return( PSA_ERROR_NOT_SUPPORTED );
301
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
303 return( PSA_ERROR_HARDWARE_FAILURE );
304
305 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_RSA_INVALID_PADDING:
308 return( PSA_ERROR_INVALID_PADDING );
309 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
312 return( PSA_ERROR_INVALID_ARGUMENT );
313 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
314 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200315 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100316 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
317 return( PSA_ERROR_INVALID_SIGNATURE );
318 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
319 return( PSA_ERROR_BUFFER_TOO_SMALL );
320 case MBEDTLS_ERR_RSA_RNG_FAILED:
321 return( PSA_ERROR_INSUFFICIENT_MEMORY );
322 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
323 return( PSA_ERROR_NOT_SUPPORTED );
324 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
327 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
328 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
329 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
330 return( PSA_ERROR_HARDWARE_FAILURE );
331
332 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
333 return( PSA_ERROR_INVALID_ARGUMENT );
334 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
336
itayzafrir5c753392018-05-08 11:18:38 +0300337 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300338 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300339 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
341 return( PSA_ERROR_BUFFER_TOO_SMALL );
342 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
343 return( PSA_ERROR_NOT_SUPPORTED );
344 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
345 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
346 return( PSA_ERROR_INVALID_SIGNATURE );
347 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
348 return( PSA_ERROR_INSUFFICIENT_MEMORY );
349 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
350 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000351 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
352 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
Gilles Peskine8e338702019-07-30 20:06:31 +0200369 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100374mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100375 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200376{
377 switch( curve )
378 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100379 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100380 switch( byte_length )
381 {
382 case PSA_BITS_TO_BYTES( 192 ):
383 return( MBEDTLS_ECP_DP_SECP192R1 );
384 case PSA_BITS_TO_BYTES( 224 ):
385 return( MBEDTLS_ECP_DP_SECP224R1 );
386 case PSA_BITS_TO_BYTES( 256 ):
387 return( MBEDTLS_ECP_DP_SECP256R1 );
388 case PSA_BITS_TO_BYTES( 384 ):
389 return( MBEDTLS_ECP_DP_SECP384R1 );
390 case PSA_BITS_TO_BYTES( 521 ):
391 return( MBEDTLS_ECP_DP_SECP521R1 );
392 default:
393 return( MBEDTLS_ECP_DP_NONE );
394 }
395 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100396
Paul Elliott8ff510a2020-06-02 17:19:28 +0100397 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100398 switch( byte_length )
399 {
400 case PSA_BITS_TO_BYTES( 256 ):
401 return( MBEDTLS_ECP_DP_BP256R1 );
402 case PSA_BITS_TO_BYTES( 384 ):
403 return( MBEDTLS_ECP_DP_BP384R1 );
404 case PSA_BITS_TO_BYTES( 512 ):
405 return( MBEDTLS_ECP_DP_BP512R1 );
406 default:
407 return( MBEDTLS_ECP_DP_NONE );
408 }
409 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100410
Paul Elliott8ff510a2020-06-02 17:19:28 +0100411 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 switch( byte_length )
413 {
414 case PSA_BITS_TO_BYTES( 255 ):
415 return( MBEDTLS_ECP_DP_CURVE25519 );
416 case PSA_BITS_TO_BYTES( 448 ):
417 return( MBEDTLS_ECP_DP_CURVE448 );
418 default:
419 return( MBEDTLS_ECP_DP_NONE );
420 }
421 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100422
Paul Elliott8ff510a2020-06-02 17:19:28 +0100423 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 switch( byte_length )
425 {
426 case PSA_BITS_TO_BYTES( 192 ):
427 return( MBEDTLS_ECP_DP_SECP192K1 );
428 case PSA_BITS_TO_BYTES( 224 ):
429 return( MBEDTLS_ECP_DP_SECP224K1 );
430 case PSA_BITS_TO_BYTES( 256 ):
431 return( MBEDTLS_ECP_DP_SECP256K1 );
432 default:
433 return( MBEDTLS_ECP_DP_NONE );
434 }
435 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100436
Gilles Peskine12313cd2018-06-20 00:20:32 +0200437 default:
438 return( MBEDTLS_ECP_DP_NONE );
439 }
440}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100441#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200442
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200443static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
444 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445{
446 /* Check that the bit size is acceptable for the key type */
447 switch( type )
448 {
449 case PSA_KEY_TYPE_RAW_DATA:
450#if defined(MBEDTLS_MD_C)
451 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200452#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200453 case PSA_KEY_TYPE_DERIVE:
454 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200455#if defined(MBEDTLS_AES_C)
456 case PSA_KEY_TYPE_AES:
457 if( bits != 128 && bits != 192 && bits != 256 )
458 return( PSA_ERROR_INVALID_ARGUMENT );
459 break;
460#endif
461#if defined(MBEDTLS_CAMELLIA_C)
462 case PSA_KEY_TYPE_CAMELLIA:
463 if( bits != 128 && bits != 192 && bits != 256 )
464 return( PSA_ERROR_INVALID_ARGUMENT );
465 break;
466#endif
467#if defined(MBEDTLS_DES_C)
468 case PSA_KEY_TYPE_DES:
469 if( bits != 64 && bits != 128 && bits != 192 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
471 break;
472#endif
473#if defined(MBEDTLS_ARC4_C)
474 case PSA_KEY_TYPE_ARC4:
475 if( bits < 8 || bits > 2048 )
476 return( PSA_ERROR_INVALID_ARGUMENT );
477 break;
478#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200479#if defined(MBEDTLS_CHACHA20_C)
480 case PSA_KEY_TYPE_CHACHA20:
481 if( bits != 256 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200485 default:
486 return( PSA_ERROR_NOT_SUPPORTED );
487 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200488 if( bits % 8 != 0 )
489 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491 return( PSA_SUCCESS );
492}
493
Steven Cooremana01795d2020-07-24 22:48:15 +0200494#if defined(MBEDTLS_RSA_C)
495
496#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100497/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
498 * that are not a multiple of 8) well. For example, there is only
499 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
500 * way to return the exact bit size of a key.
501 * To keep things simple, reject non-byte-aligned key sizes. */
502static psa_status_t psa_check_rsa_key_byte_aligned(
503 const mbedtls_rsa_context *rsa )
504{
505 mbedtls_mpi n;
506 psa_status_t status;
507 mbedtls_mpi_init( &n );
508 status = mbedtls_to_psa_error(
509 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
510 if( status == PSA_SUCCESS )
511 {
512 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
513 status = PSA_ERROR_NOT_SUPPORTED;
514 }
515 mbedtls_mpi_free( &n );
516 return( status );
517}
Steven Cooremana01795d2020-07-24 22:48:15 +0200518#endif /* MBEDTLS_PK_PARSE_C */
Gilles Peskine86a440b2018-11-12 18:39:40 +0100519
Steven Cooreman7f391872020-07-30 14:57:44 +0200520/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200521 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200522 * \param[in] type The type of key contained in \p data.
523 * \param[in] data The buffer from which to load the representation.
524 * \param[in] data_length The size in bytes of \p data.
525 * \param[out] p_rsa Returns a pointer to an RSA context on success.
526 * The caller is responsible for freeing both the
527 * contents of the context and the context itself
528 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200529 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200530static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
531 const uint8_t *data,
532 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200533 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200534{
Steven Cooremana01795d2020-07-24 22:48:15 +0200535#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000536 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200537 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000538 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200539 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000540
541 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200542 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200544 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200547 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000548 if( status != PSA_SUCCESS )
549 goto exit;
550
551 /* We have something that the pkparse module recognizes. If it is a
552 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200553 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555 status = PSA_ERROR_INVALID_ARGUMENT;
556 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
560 * supports non-byte-aligned key sizes, but not well. For example,
561 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200562 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000563 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
564 {
565 status = PSA_ERROR_NOT_SUPPORTED;
566 goto exit;
567 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200568 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200569 if( status != PSA_SUCCESS )
570 goto exit;
571
Steven Cooremana2371e52020-07-28 14:30:39 +0200572 /* Copy out the pointer to the RSA context, and reset the PK context
573 * such that pk_free doesn't free the RSA context we just grabbed. */
574 *p_rsa = mbedtls_pk_rsa( ctx );
575 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000576
577exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200578 mbedtls_pk_free( &ctx );
579 return( status );
580#else
Steven Cooreman4fed4552020-08-03 14:46:03 +0200581 (void) data;
582 (void) data_length;
Steven Cooreman7f391872020-07-30 14:57:44 +0200583 (void) type;
Steven Cooremana01795d2020-07-24 22:48:15 +0200584 (void) rsa;
585 return( PSA_ERROR_NOT_SUPPORTED );
586#endif /* MBEDTLS_PK_PARSE_C */
587}
588
589/** Export an RSA key to export representation
590 *
591 * \param[in] type The type of key (public/private) to export
592 * \param[in] rsa The internal RSA representation from which to export
593 * \param[out] data The buffer to export to
594 * \param[in] data_size The length of the buffer to export to
595 * \param[out] data_length The amount of bytes written to \p data
596 */
597static psa_status_t psa_export_rsa_key( psa_key_type_t type,
598 mbedtls_rsa_context *rsa,
599 uint8_t *data,
600 size_t data_size,
601 size_t *data_length )
602{
603#if defined(MBEDTLS_PK_WRITE_C)
604 int ret;
605 mbedtls_pk_context pk;
606 uint8_t *pos = data + data_size;
607
608 mbedtls_pk_init( &pk );
609 pk.pk_info = &mbedtls_rsa_info;
610 pk.pk_ctx = rsa;
611
612 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200613 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
614 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200615 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
616 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
617 else
618 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
619
620 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200621 {
622 /* Clean up in case pk_write failed halfway through. */
623 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200624 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200625 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200626
627 /* The mbedtls_pk_xxx functions write to the end of the buffer.
628 * Move the data to the beginning and erase remaining data
629 * at the original location. */
630 if( 2 * (size_t) ret <= data_size )
631 {
632 memcpy( data, data + data_size - ret, ret );
633 memset( data + data_size - ret, 0, ret );
634 }
635 else if( (size_t) ret < data_size )
636 {
637 memmove( data, data + data_size - ret, ret );
638 memset( data + ret, 0, data_size - ret );
639 }
640
641 *data_length = ret;
642 return( PSA_SUCCESS );
643#else
644 (void) type;
645 (void) rsa;
646 (void) data;
647 (void) data_size;
648 (void) data_length;
649 return( PSA_ERROR_NOT_SUPPORTED );
650#endif /* MBEDTLS_PK_WRITE_C */
651}
652
653/** Import an RSA key from import representation to a slot
654 *
655 * \param[in,out] slot The slot where to store the export representation to
656 * \param[in] data The buffer containing the import representation
657 * \param[in] data_length The amount of bytes in \p data
658 */
659static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
660 const uint8_t *data,
661 size_t data_length )
662{
663 psa_status_t status;
664 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200665 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200666
Steven Cooremana01795d2020-07-24 22:48:15 +0200667 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200668 status = psa_load_rsa_representation( slot->attr.type,
669 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200670 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200671 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200672 if( status != PSA_SUCCESS )
673 goto exit;
674
675 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200676 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200677
Steven Cooreman75b74362020-07-28 14:30:13 +0200678 /* Re-export the data to PSA export format, such that we can store export
679 * representation in the key slot. Export representation in case of RSA is
680 * the smallest representation that's allowed as input, so a straight-up
681 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200682 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200683 if( output == NULL )
684 {
685 status = PSA_ERROR_INSUFFICIENT_MEMORY;
686 goto exit;
687 }
688
Steven Cooremana01795d2020-07-24 22:48:15 +0200689 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200690 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200691 output,
692 data_length,
693 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200694exit:
695 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200696 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200697 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200698
699 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000700 if( status != PSA_SUCCESS )
701 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200702 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000703 return( status );
704 }
705
Steven Cooremana01795d2020-07-24 22:48:15 +0200706 /* On success, store the allocated export-formatted key. */
707 slot->data.key.data = output;
708 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000709
710 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200711}
Steven Cooremana01795d2020-07-24 22:48:15 +0200712#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200713
Jaeden Ameroccdce902019-01-10 11:42:27 +0000714#if defined(MBEDTLS_ECP_C)
Steven Cooreman7f391872020-07-30 14:57:44 +0200715/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200716 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200717 * \param[in] type The type of key contained in \p data.
718 * \param[in] data The buffer from which to load the representation.
719 * \param[in] data_length The size in bytes of \p data.
720 * \param[out] p_ecp Returns a pointer to an ECP context on success.
721 * The caller is responsible for freeing both the
722 * contents of the context and the context itself
723 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200724 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200725static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
726 const uint8_t *data,
727 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200728 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100729{
730 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200731 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200732 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200733 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100734
Steven Cooreman4fed4552020-08-03 14:46:03 +0200735 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
736 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100737 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200738 /* A Weierstrass public key is represented as:
739 * - The byte 0x04;
740 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
741 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200742 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200743 */
744 if( ( data_length & 1 ) == 0 )
745 return( PSA_ERROR_INVALID_ARGUMENT );
746 curve_size = data_length / 2;
747
748 /* Montgomery public keys are represented in compressed format, meaning
749 * their curve_size is equal to the amount of input. */
750
751 /* Private keys are represented in uncompressed private random integer
752 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100753 }
754
Steven Cooreman6d839f02020-07-30 11:36:45 +0200755 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200756 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200757 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200758 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200759 mbedtls_ecp_keypair_init( ecp );
760
Gilles Peskine4cd32772019-12-02 20:49:42 +0100761 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200762 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
763 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100764 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200765 {
766 status = PSA_ERROR_INVALID_ARGUMENT;
767 goto exit;
768 }
769
Steven Cooremanacda8342020-07-24 23:09:52 +0200770 status = mbedtls_to_psa_error(
771 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
772 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200773 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200774
Steven Cooreman6d839f02020-07-30 11:36:45 +0200775 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200776 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200777 {
778 /* Load the public value. */
779 status = mbedtls_to_psa_error(
780 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200781 data,
782 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200783 if( status != PSA_SUCCESS )
784 goto exit;
785
786 /* Check that the point is on the curve. */
787 status = mbedtls_to_psa_error(
788 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
789 if( status != PSA_SUCCESS )
790 goto exit;
791 }
792 else
793 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200794 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200795 status = mbedtls_to_psa_error(
796 mbedtls_ecp_read_key( ecp->grp.id,
797 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200798 data,
799 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200800 if( status != PSA_SUCCESS )
801 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200802 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200803
804 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200805exit:
806 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200807 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200808 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200809 mbedtls_free( ecp );
810 }
811
Steven Cooreman29149862020-08-05 15:43:42 +0200812 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100813}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000814
Steven Cooreman4fed4552020-08-03 14:46:03 +0200815/** Export an ECP key to export representation
816 *
817 * \param[in] type The type of key (public/private) to export
818 * \param[in] ecp The internal ECP representation from which to export
819 * \param[out] data The buffer to export to
820 * \param[in] data_size The length of the buffer to export to
821 * \param[out] data_length The amount of bytes written to \p data
822 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200823static psa_status_t psa_export_ecp_key( psa_key_type_t type,
824 mbedtls_ecp_keypair *ecp,
825 uint8_t *data,
826 size_t data_size,
827 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200828{
Steven Cooremanacda8342020-07-24 23:09:52 +0200829 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000830
Steven Cooremanacda8342020-07-24 23:09:52 +0200831 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200832 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200833 /* Check whether the public part is loaded */
834 if( mbedtls_ecp_is_zero( &ecp->Q ) )
835 {
836 /* Calculate the public key */
837 status = mbedtls_to_psa_error(
838 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
839 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
840 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200841 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200842 }
843
Steven Cooreman4fed4552020-08-03 14:46:03 +0200844 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200845 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
846 MBEDTLS_ECP_PF_UNCOMPRESSED,
847 data_length,
848 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200849 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200850 if( status != PSA_SUCCESS )
851 memset( data, 0, data_size );
852
Steven Cooreman29149862020-08-05 15:43:42 +0200853 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200854 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200855 else
856 {
Steven Cooreman29149862020-08-05 15:43:42 +0200857 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200858 return( PSA_ERROR_BUFFER_TOO_SMALL );
859
860 status = mbedtls_to_psa_error(
861 mbedtls_ecp_write_key( ecp,
862 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200863 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200864 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200865 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200866 else
867 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200868
869 return( status );
870 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200871}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200872
Steven Cooreman4fed4552020-08-03 14:46:03 +0200873/** Import an ECP key from import representation to a slot
874 *
875 * \param[in,out] slot The slot where to store the export representation to
876 * \param[in] data The buffer containing the import representation
877 * \param[in] data_length The amount of bytes in \p data
878 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200879static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
880 const uint8_t *data,
881 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100882{
Steven Cooremanacda8342020-07-24 23:09:52 +0200883 psa_status_t status;
884 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200885 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100886
Steven Cooremanacda8342020-07-24 23:09:52 +0200887 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200888 status = psa_load_ecp_representation( slot->attr.type,
889 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200890 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200891 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100892 if( status != PSA_SUCCESS )
893 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100894
Steven Cooremanacda8342020-07-24 23:09:52 +0200895 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200896 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200897 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200898 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200899
Steven Cooremanacda8342020-07-24 23:09:52 +0200900 /* Re-export the data to PSA export format. There is currently no support
901 * for other input formats then the export format, so this is a 1-1
902 * copy operation. */
903 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200904 if( output == NULL )
905 {
906 status = PSA_ERROR_INSUFFICIENT_MEMORY;
907 goto exit;
908 }
909
910 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200911 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200912 output,
913 data_length,
914 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100915exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200916 /* Always free the PK object (will also free contained ECP context) */
917 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200918 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200919
920 /* Free the allocated buffer only on error. */
921 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100922 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200923 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200924 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100925 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200926
927 /* On success, store the allocated export-formatted key. */
928 slot->data.key.data = output;
929 slot->data.key.bytes = data_length;
930
931 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100932}
933#endif /* defined(MBEDTLS_ECP_C) */
934
Gilles Peskineb46bef22019-07-30 21:32:04 +0200935/** Return the size of the key in the given slot, in bits.
936 *
937 * \param[in] slot A key slot.
938 *
939 * \return The key size in bits, read from the metadata in the slot.
940 */
941static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
942{
943 return( slot->attr.bits );
944}
945
Steven Cooreman75b74362020-07-28 14:30:13 +0200946/** Try to allocate a buffer to an empty key slot.
947 *
948 * \param[in,out] slot Key slot to attach buffer to.
949 * \param[in] buffer_length Requested size of the buffer.
950 *
951 * \retval #PSA_SUCCESS
952 * The buffer has been successfully allocated.
953 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
954 * Not enough memory was available for allocation.
955 * \retval #PSA_ERROR_ALREADY_EXISTS
956 * Trying to allocate a buffer to a non-empty key slot.
957 */
958static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
959 size_t buffer_length )
960{
961 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200962 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200963
964 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
965 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200966 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200967
968 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200969 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200970}
971
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200972psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
973 const uint8_t* data,
974 size_t data_length )
975{
976 psa_status_t status = psa_allocate_buffer_to_slot( slot,
977 data_length );
978 if( status != PSA_SUCCESS )
979 return( status );
980
981 memcpy( slot->data.key.data, data, data_length );
982 return( PSA_SUCCESS );
983}
984
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200985/** Import key data into a slot.
986 *
987 * `slot->type` must have been set previously.
988 * This function assumes that the slot does not contain any key material yet.
989 * On failure, the slot content is unchanged.
990 *
991 * Persistent storage is not affected.
992 *
993 * \param[in,out] slot The key slot to import data into.
994 * Its `type` field must have previously been set to
995 * the desired key type.
996 * It must not contain any key material yet.
997 * \param[in] data Buffer containing the key material to parse and import.
998 * \param data_length Size of \p data in bytes.
999 *
1000 * \retval #PSA_SUCCESS
1001 * \retval #PSA_ERROR_INVALID_ARGUMENT
1002 * \retval #PSA_ERROR_NOT_SUPPORTED
1003 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1004 */
1005static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1006 const uint8_t *data,
1007 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001009 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001010 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001012 /* zero-length keys are never supported. */
1013 if( data_length == 0 )
1014 return( PSA_ERROR_NOT_SUPPORTED );
1015
Gilles Peskine8e338702019-07-30 20:06:31 +02001016 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017 {
Steven Cooreman04524762020-10-13 17:43:44 +02001018 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001019
Steven Cooreman75b74362020-07-28 14:30:13 +02001020 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1021 if( data_length > SIZE_MAX / 8 )
1022 return( PSA_ERROR_NOT_SUPPORTED );
1023
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001024 /* Enforce a size limit, and in particular ensure that the bit
1025 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001026 if( bit_size > PSA_MAX_KEY_BITS )
1027 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001028
1029 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001030 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001031 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001032
1033 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001034 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001035 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001036 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001037
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001038 /* Write the actual key size to the slot.
1039 * psa_start_key_creation() wrote the size declared by the
1040 * caller, which may be 0 (meaning unspecified) or wrong. */
1041 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001042
1043 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001044 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001045 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001046 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001047 /* Try validation through accelerators first. */
1048 bit_size = slot->attr.bits;
1049 psa_key_attributes_t attributes = {
1050 .core = slot->attr
1051 };
1052 status = psa_driver_wrapper_validate_key( &attributes,
1053 data,
1054 data_length,
1055 &bit_size );
1056 if( status == PSA_SUCCESS )
1057 {
1058 /* Key has been validated successfully by an accelerator.
1059 * Copy key material into slot. */
1060 status = psa_copy_key_material_into_slot( slot, data, data_length );
1061 if( status != PSA_SUCCESS )
1062 return( status );
1063
1064 slot->attr.bits = (psa_key_bits_t) bit_size;
1065 return( PSA_SUCCESS );
1066 }
1067 else if( status != PSA_ERROR_NOT_SUPPORTED )
1068 return( status );
1069
1070 /* Key format is not supported by any accelerator, try software fallback
1071 * if present. */
Gilles Peskinef76aa772018-10-29 19:24:33 +01001072#if defined(MBEDTLS_ECP_C)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001073 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1074 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001075 return( psa_import_ecp_key( slot, data, data_length ) );
1076 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001077#endif /* defined(MBEDTLS_ECP_C) */
Steven Cooreman19fd5742020-07-24 23:31:01 +02001078#if defined(MBEDTLS_RSA_C)
Steven Cooreman40120f62020-10-29 11:42:22 +01001079 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001080 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001081 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001082 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001083#endif /* defined(MBEDTLS_RSA_C) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001084
1085 /* Fell through the fallback as well, so have nothing else to try. */
1086 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001087 }
1088 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001089 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001090 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001092 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001093}
1094
Gilles Peskinef603c712019-01-19 13:40:11 +01001095/** Calculate the intersection of two algorithm usage policies.
1096 *
1097 * Return 0 (which allows no operation) on incompatibility.
1098 */
1099static psa_algorithm_t psa_key_policy_algorithm_intersection(
1100 psa_algorithm_t alg1,
1101 psa_algorithm_t alg2 )
1102{
Gilles Peskine549ea862019-05-22 11:45:59 +02001103 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001104 if( alg1 == alg2 )
1105 return( alg1 );
1106 /* If the policies are from the same hash-and-sign family, check
1107 * if one is a wildcard. If so the other has the specific algorithm. */
1108 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1109 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1110 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1111 {
1112 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1113 return( alg2 );
1114 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1115 return( alg1 );
1116 }
1117 /* If the policies are incompatible, allow nothing. */
1118 return( 0 );
1119}
1120
Gilles Peskined6f371b2019-05-10 19:33:38 +02001121static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1122 psa_algorithm_t requested_alg )
1123{
Gilles Peskine549ea862019-05-22 11:45:59 +02001124 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001125 if( requested_alg == policy_alg )
1126 return( 1 );
1127 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001128 * and requested_alg is the same hash-and-sign family with any hash,
1129 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001130 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1131 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1132 {
1133 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1134 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1135 }
Steven Cooremance48e852020-10-05 16:02:45 +02001136 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001137 * a key derivation with that key agreement should also be allowed. This
1138 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001139 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1140 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1141 {
1142 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1143 policy_alg );
1144 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001145 /* If it isn't permitted, it's forbidden. */
1146 return( 0 );
1147}
1148
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001149/** Test whether a policy permits an algorithm.
1150 *
1151 * The caller must test usage flags separately.
1152 */
1153static int psa_key_policy_permits( const psa_key_policy_t *policy,
1154 psa_algorithm_t alg )
1155{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001156 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1157 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001158}
1159
Gilles Peskinef603c712019-01-19 13:40:11 +01001160/** Restrict a key policy based on a constraint.
1161 *
1162 * \param[in,out] policy The policy to restrict.
1163 * \param[in] constraint The policy constraint to apply.
1164 *
1165 * \retval #PSA_SUCCESS
1166 * \c *policy contains the intersection of the original value of
1167 * \c *policy and \c *constraint.
1168 * \retval #PSA_ERROR_INVALID_ARGUMENT
1169 * \c *policy and \c *constraint are incompatible.
1170 * \c *policy is unchanged.
1171 */
1172static psa_status_t psa_restrict_key_policy(
1173 psa_key_policy_t *policy,
1174 const psa_key_policy_t *constraint )
1175{
1176 psa_algorithm_t intersection_alg =
1177 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001178 psa_algorithm_t intersection_alg2 =
1179 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001180 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1181 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001182 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1183 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001184 policy->usage &= constraint->usage;
1185 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001186 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001187 return( PSA_SUCCESS );
1188}
1189
Darryl Green06fd18d2018-07-16 11:21:11 +01001190/** Retrieve a slot which must contain a key. The key must have allow all the
1191 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
Ronald Cronf95a2b12020-10-22 15:24:49 +02001192 * operations with this algorithm.
1193 *
1194 * On success, the access counter of the returned key slot is incremented by
1195 * one. It is the responsibility of the caller to call
1196 * psa_decrement_key_slot_access_count() when it does not access the key slot
1197 * anymore.
1198 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001199static psa_status_t psa_get_key_from_slot( mbedtls_svc_key_id_t key,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001200 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001201 psa_key_usage_t usage,
1202 psa_algorithm_t alg )
1203{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001204 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1205 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001206
Ronald Cronf95a2b12020-10-22 15:24:49 +02001207 status = psa_get_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001208 if( status != PSA_SUCCESS )
1209 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001210 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001211
1212 /* Enforce that usage policy for the key slot contains all the flags
1213 * required by the usage parameter. There is one exception: public
1214 * keys can always be exported, so we treat public key objects as
1215 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001216 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001217 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001218
1219 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001220 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001221 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001222
1223 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001224 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001225 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001226
Darryl Green06fd18d2018-07-16 11:21:11 +01001227 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001228
1229error:
1230 *p_slot = NULL;
1231 psa_decrement_key_slot_access_count( slot );
1232
1233 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001234}
Darryl Green940d72c2018-07-13 13:18:51 +01001235
Gilles Peskine28f8f302019-07-24 13:30:31 +02001236/** Retrieve a slot which must contain a transparent key.
1237 *
1238 * A transparent key is a key for which the key material is directly
1239 * available, as opposed to a key in a secure element.
1240 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001241 * This is a temporary function to use instead of psa_get_key_from_slot()
1242 * until secure element support is fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001243 *
1244 * On success, the access counter of the returned key slot is incremented by
1245 * one. It is the responsibility of the caller to call
1246 * psa_decrement_key_slot_access_count() when it does not access the key slot
1247 * anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001248 */
1249#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001250static psa_status_t psa_get_transparent_key( mbedtls_svc_key_id_t key,
Gilles Peskine28f8f302019-07-24 13:30:31 +02001251 psa_key_slot_t **p_slot,
1252 psa_key_usage_t usage,
1253 psa_algorithm_t alg )
1254{
Ronald Croncf56a0a2020-08-04 09:51:30 +02001255 psa_status_t status = psa_get_key_from_slot( key, p_slot, usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001256 if( status != PSA_SUCCESS )
1257 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001258
Gilles Peskineadad8132019-07-25 11:31:23 +02001259 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001260 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02001261 psa_decrement_key_slot_access_count( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001262 *p_slot = NULL;
1263 return( PSA_ERROR_NOT_SUPPORTED );
1264 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001265
Gilles Peskine28f8f302019-07-24 13:30:31 +02001266 return( PSA_SUCCESS );
1267}
1268#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1269/* With no secure element support, all keys are transparent. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001270#define psa_get_transparent_key( key, p_slot, usage, alg ) \
1271 psa_get_key_from_slot( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001272#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1273
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001274/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001275static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001276{
Gilles Peskine73167e12019-07-12 23:44:37 +02001277#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1278 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001279 {
1280 /* No key material to clean. */
1281 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001282 else
1283#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001284 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001285 /* Data pointer will always be either a valid pointer or NULL in an
1286 * initialized slot, so we can just free it. */
1287 if( slot->data.key.data != NULL )
1288 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001289 mbedtls_free( slot->data.key.data );
1290 slot->data.key.data = NULL;
1291 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001292 }
Darryl Green40225ba2018-11-15 14:48:15 +00001293
1294 return( PSA_SUCCESS );
1295}
1296
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001297/** Completely wipe a slot in memory, including its policy.
1298 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001299psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001300{
1301 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001302 /* Multipart operations may still be using the key. This is safe
1303 * because all multipart operation objects are independent from
1304 * the key slot: if they need to access the key after the setup
1305 * phase, they have a copy of the key. Note that this means that
1306 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001307 /* At this point, key material and other type-specific content has
1308 * been wiped. Clear remaining metadata. We can call memset and not
1309 * zeroize because the metadata is not particularly sensitive. */
1310 memset( slot, 0, sizeof( *slot ) );
1311 return( status );
1312}
1313
Ronald Croncf56a0a2020-08-04 09:51:30 +02001314psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001316 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001317 psa_status_t status; /* status of the last operation */
1318 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001319#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1320 psa_se_drv_table_entry_t *driver;
1321#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001322
Ronald Croncf56a0a2020-08-04 09:51:30 +02001323 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001324 return( PSA_SUCCESS );
1325
Ronald Croncf56a0a2020-08-04 09:51:30 +02001326 status = psa_get_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001327 if( status != PSA_SUCCESS )
1328 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001329
1330#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001331 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001332 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001333 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001334 /* For a key in a secure element, we need to do three things:
1335 * remove the key file in internal storage, destroy the
1336 * key inside the secure element, and update the driver's
1337 * persistent data. Start a transaction that will encompass these
1338 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001339 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001340 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001341 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001342 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001343 status = psa_crypto_save_transaction( );
1344 if( status != PSA_SUCCESS )
1345 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001346 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001347 /* We should still try to destroy the key in the secure
1348 * element and the key metadata in storage. This is especially
1349 * important if the error is that the storage is full.
1350 * But how to do it exactly without risking an inconsistent
1351 * state after a reset?
1352 * https://github.com/ARMmbed/mbed-crypto/issues/215
1353 */
1354 overall_status = status;
1355 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001356 }
1357
Gilles Peskine354f7672019-07-12 23:46:38 +02001358 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001359 if( overall_status == PSA_SUCCESS )
1360 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001361 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001362#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1363
Darryl Greend49a4992018-06-18 17:27:26 +01001364#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001365 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001366 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001367 status = psa_destroy_persistent_key( slot->attr.id );
1368 if( overall_status == PSA_SUCCESS )
1369 overall_status = status;
1370
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001371 /* TODO: other slots may have a copy of the same key. We should
1372 * invalidate them.
1373 * https://github.com/ARMmbed/mbed-crypto/issues/214
1374 */
Darryl Greend49a4992018-06-18 17:27:26 +01001375 }
1376#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001377
Gilles Peskinefc762652019-07-22 19:30:34 +02001378#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1379 if( driver != NULL )
1380 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001381 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001382 if( overall_status == PSA_SUCCESS )
1383 overall_status = status;
1384 status = psa_crypto_stop_transaction( );
1385 if( overall_status == PSA_SUCCESS )
1386 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001387 }
1388#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1389
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001390#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1391exit:
1392#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001393 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001394 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1395 if( overall_status == PSA_SUCCESS )
1396 overall_status = status;
1397 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001398}
1399
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001400void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001401{
Gilles Peskineb699f072019-04-26 16:06:02 +02001402 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001403 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001404}
1405
Gilles Peskineb699f072019-04-26 16:06:02 +02001406psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1407 psa_key_type_t type,
1408 const uint8_t *data,
1409 size_t data_length )
1410{
1411 uint8_t *copy = NULL;
1412
1413 if( data_length != 0 )
1414 {
1415 copy = mbedtls_calloc( 1, data_length );
1416 if( copy == NULL )
1417 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1418 memcpy( copy, data, data_length );
1419 }
1420 /* After this point, this function is guaranteed to succeed, so it
1421 * can start modifying `*attributes`. */
1422
1423 if( attributes->domain_parameters != NULL )
1424 {
1425 mbedtls_free( attributes->domain_parameters );
1426 attributes->domain_parameters = NULL;
1427 attributes->domain_parameters_size = 0;
1428 }
1429
1430 attributes->domain_parameters = copy;
1431 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001432 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001433 return( PSA_SUCCESS );
1434}
1435
1436psa_status_t psa_get_key_domain_parameters(
1437 const psa_key_attributes_t *attributes,
1438 uint8_t *data, size_t data_size, size_t *data_length )
1439{
1440 if( attributes->domain_parameters_size > data_size )
1441 return( PSA_ERROR_BUFFER_TOO_SMALL );
1442 *data_length = attributes->domain_parameters_size;
1443 if( attributes->domain_parameters_size != 0 )
1444 memcpy( data, attributes->domain_parameters,
1445 attributes->domain_parameters_size );
1446 return( PSA_SUCCESS );
1447}
1448
1449#if defined(MBEDTLS_RSA_C)
1450static psa_status_t psa_get_rsa_public_exponent(
1451 const mbedtls_rsa_context *rsa,
1452 psa_key_attributes_t *attributes )
1453{
1454 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001456 uint8_t *buffer = NULL;
1457 size_t buflen;
1458 mbedtls_mpi_init( &mpi );
1459
1460 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1461 if( ret != 0 )
1462 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001463 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1464 {
1465 /* It's the default value, which is reported as an empty string,
1466 * so there's nothing to do. */
1467 goto exit;
1468 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001469
1470 buflen = mbedtls_mpi_size( &mpi );
1471 buffer = mbedtls_calloc( 1, buflen );
1472 if( buffer == NULL )
1473 {
1474 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1475 goto exit;
1476 }
1477 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1478 if( ret != 0 )
1479 goto exit;
1480 attributes->domain_parameters = buffer;
1481 attributes->domain_parameters_size = buflen;
1482
1483exit:
1484 mbedtls_mpi_free( &mpi );
1485 if( ret != 0 )
1486 mbedtls_free( buffer );
1487 return( mbedtls_to_psa_error( ret ) );
1488}
1489#endif /* MBEDTLS_RSA_C */
1490
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001491/** Retrieve all the publicly-accessible attributes of a key.
1492 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001493psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001494 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001495{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001496 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1497 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001498 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001499
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001500 psa_reset_key_attributes( attributes );
1501
Ronald Croncf56a0a2020-08-04 09:51:30 +02001502 status = psa_get_key_from_slot( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001503 if( status != PSA_SUCCESS )
1504 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001505
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001506 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001507 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1508 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1509
1510#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1511 if( psa_key_slot_is_external( slot ) )
1512 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1513#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001514
Gilles Peskine8e338702019-07-30 20:06:31 +02001515 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001516 {
1517#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001518 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001519 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001520#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001521 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001522 * is not yet implemented.
1523 * https://github.com/ARMmbed/mbed-crypto/issues/216
1524 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001525 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001526 break;
1527#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001528 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001529 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001530
Steven Cooreman4fed4552020-08-03 14:46:03 +02001531 status = psa_load_rsa_representation( slot->attr.type,
1532 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001533 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001534 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001535 if( status != PSA_SUCCESS )
1536 break;
1537
Steven Cooremana2371e52020-07-28 14:30:39 +02001538 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001539 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001540 mbedtls_rsa_free( rsa );
1541 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001542 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001543 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001544#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001545 default:
1546 /* Nothing else to do. */
1547 break;
1548 }
1549
1550 if( status != PSA_SUCCESS )
1551 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001552
1553 decrement_status = psa_decrement_key_slot_access_count( slot );
1554
1555 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001556}
1557
Gilles Peskinec8000c02019-08-02 20:15:51 +02001558#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1559psa_status_t psa_get_key_slot_number(
1560 const psa_key_attributes_t *attributes,
1561 psa_key_slot_number_t *slot_number )
1562{
1563 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1564 {
1565 *slot_number = attributes->slot_number;
1566 return( PSA_SUCCESS );
1567 }
1568 else
1569 return( PSA_ERROR_INVALID_ARGUMENT );
1570}
1571#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1572
Steven Cooremana01795d2020-07-24 22:48:15 +02001573static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1574 uint8_t *data,
1575 size_t data_size,
1576 size_t *data_length )
1577{
1578 if( slot->data.key.bytes > data_size )
1579 return( PSA_ERROR_BUFFER_TOO_SMALL );
1580 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1581 memset( data + slot->data.key.bytes, 0,
1582 data_size - slot->data.key.bytes );
1583 *data_length = slot->data.key.bytes;
1584 return( PSA_SUCCESS );
1585}
1586
Gilles Peskinef603c712019-01-19 13:40:11 +01001587static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1588 uint8_t *data,
1589 size_t data_size,
1590 size_t *data_length,
1591 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001592{
Gilles Peskine5d309672019-07-12 23:47:28 +02001593#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1594 const psa_drv_se_t *drv;
1595 psa_drv_se_context_t *drv_context;
1596#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1597
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001598 *data_length = 0;
1599
Gilles Peskine8e338702019-07-30 20:06:31 +02001600 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001601 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001602
Gilles Peskinef9168942019-09-12 19:20:29 +02001603 /* Reject a zero-length output buffer now, since this can never be a
1604 * valid key representation. This way we know that data must be a valid
1605 * pointer and we can do things like memset(data, ..., data_size). */
1606 if( data_size == 0 )
1607 return( PSA_ERROR_BUFFER_TOO_SMALL );
1608
Gilles Peskine5d309672019-07-12 23:47:28 +02001609#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001610 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001611 {
1612 psa_drv_se_export_key_t method;
1613 if( drv->key_management == NULL )
1614 return( PSA_ERROR_NOT_SUPPORTED );
1615 method = ( export_public_key ?
1616 drv->key_management->p_export_public :
1617 drv->key_management->p_export );
1618 if( method == NULL )
1619 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001620 return( method( drv_context,
1621 slot->data.se.slot_number,
1622 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001623 }
1624#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1625
Gilles Peskine8e338702019-07-30 20:06:31 +02001626 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001627 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001628 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001629 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001630 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1631 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001632 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001633 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001634 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001635 /* Exporting public -> public */
1636 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1637 }
1638 else if( !export_public_key )
1639 {
1640 /* Exporting private -> private */
1641 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1642 }
1643 /* Need to export the public part of a private key,
1644 * so conversion is needed */
1645 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1646 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001647#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001648 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001649 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001650 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001651 slot->data.key.data,
1652 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001653 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001654 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001655 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001656
Steven Cooreman560c28a2020-07-24 23:20:24 +02001657 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001658 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001659 data,
1660 data_size,
1661 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001662
Steven Cooremana2371e52020-07-28 14:30:39 +02001663 mbedtls_rsa_free( rsa );
1664 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001665
Steven Cooreman560c28a2020-07-24 23:20:24 +02001666 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001667#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001668 /* We don't know how to convert a private RSA key to public. */
1669 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001670#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001671 }
1672 else
Moran Pekera998bc62018-04-16 18:16:20 +03001673 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001674#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001675 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001676 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001677 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001678 slot->data.key.data,
1679 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001680 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001681 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001682 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001683
1684 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1685 PSA_KEY_TYPE_ECC_GET_FAMILY(
1686 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001687 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001688 data,
1689 data_size,
1690 data_length );
1691
Steven Cooremana2371e52020-07-28 14:30:39 +02001692 mbedtls_ecp_keypair_free( ecp );
1693 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001694 return( status );
1695#else
1696 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001697 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001698#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001699 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001700 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001701 else
1702 {
1703 /* This shouldn't happen in the reference implementation, but
1704 it is valid for a special-purpose implementation to omit
1705 support for exporting certain key types. */
1706 return( PSA_ERROR_NOT_SUPPORTED );
1707 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001708}
1709
Ronald Croncf56a0a2020-08-04 09:51:30 +02001710psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001711 uint8_t *data,
1712 size_t data_size,
1713 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001714{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001715 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1716 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001717 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001718
1719 /* Set the key to empty now, so that even when there are errors, we always
1720 * set data_length to a value between 0 and data_size. On error, setting
1721 * the key to empty is a good choice because an empty key representation is
1722 * unlikely to be accepted anywhere. */
1723 *data_length = 0;
1724
1725 /* Export requires the EXPORT flag. There is an exception for public keys,
1726 * which don't require any flag, but psa_get_key_from_slot takes
1727 * care of this. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001728 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001729 if( status != PSA_SUCCESS )
1730 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001731
1732 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
1733 decrement_status = psa_decrement_key_slot_access_count( slot );
1734
1735 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001736}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001737
Ronald Croncf56a0a2020-08-04 09:51:30 +02001738psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001739 uint8_t *data,
1740 size_t data_size,
1741 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001742{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001743 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1744 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001745 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001746
1747 /* Set the key to empty now, so that even when there are errors, we always
1748 * set data_length to a value between 0 and data_size. On error, setting
1749 * the key to empty is a good choice because an empty key representation is
1750 * unlikely to be accepted anywhere. */
1751 *data_length = 0;
1752
1753 /* Exporting a public key doesn't require a usage flag. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001754 status = psa_get_key_from_slot( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001755 if( status != PSA_SUCCESS )
1756 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001757
1758 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
1759 decrement_status = psa_decrement_key_slot_access_count( slot );
1760
1761 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001762}
1763
Gilles Peskine91e8c332019-08-02 19:19:39 +02001764#if defined(static_assert)
1765static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1766 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001767static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001768 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001769static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001770 "One or more key attribute flag is listed as both internal-only and external-only" );
1771#endif
1772
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001773/** Validate that a key policy is internally well-formed.
1774 *
1775 * This function only rejects invalid policies. It does not validate the
1776 * consistency of the policy with respect to other attributes of the key
1777 * such as the key type.
1778 */
1779static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001780{
1781 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001782 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001783 PSA_KEY_USAGE_ENCRYPT |
1784 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001785 PSA_KEY_USAGE_SIGN_HASH |
1786 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001787 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1788 return( PSA_ERROR_INVALID_ARGUMENT );
1789
Gilles Peskine4747d192019-04-17 15:05:45 +02001790 return( PSA_SUCCESS );
1791}
1792
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001793/** Validate the internal consistency of key attributes.
1794 *
1795 * This function only rejects invalid attribute values. If does not
1796 * validate the consistency of the attributes with any key data that may
1797 * be involved in the creation of the key.
1798 *
1799 * Call this function early in the key creation process.
1800 *
1801 * \param[in] attributes Key attributes for the new key.
1802 * \param[out] p_drv On any return, the driver for the key, if any.
1803 * NULL for a transparent key.
1804 *
1805 */
1806static psa_status_t psa_validate_key_attributes(
1807 const psa_key_attributes_t *attributes,
1808 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001809{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001810 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001811 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001812 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001813
Ronald Cron54b90082020-10-29 15:26:43 +01001814 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001815 if( status != PSA_SUCCESS )
1816 return( status );
1817
Ronald Crond2ed4812020-07-17 16:11:30 +02001818 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001819 if( status != PSA_SUCCESS )
1820 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001821
Ronald Cron65f38a32020-10-23 17:11:13 +02001822 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1823 {
1824 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1825 return( PSA_ERROR_INVALID_ARGUMENT );
1826 }
1827 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001828 {
Ronald Cron54b90082020-10-29 15:26:43 +01001829 status = psa_validate_key_id( psa_get_key_id( attributes ), 0, 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001830 if( status != PSA_SUCCESS )
1831 return( status );
1832 }
1833
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001834 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001835 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001836 return( status );
1837
1838 /* Refuse to create overly large keys.
1839 * Note that this doesn't trigger on import if the attributes don't
1840 * explicitly specify a size (so psa_get_key_bits returns 0), so
1841 * psa_import_key() needs its own checks. */
1842 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1843 return( PSA_ERROR_NOT_SUPPORTED );
1844
Gilles Peskine91e8c332019-08-02 19:19:39 +02001845 /* Reject invalid flags. These should not be reachable through the API. */
1846 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1847 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1848 return( PSA_ERROR_INVALID_ARGUMENT );
1849
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001850 return( PSA_SUCCESS );
1851}
1852
Gilles Peskine4747d192019-04-17 15:05:45 +02001853/** Prepare a key slot to receive key material.
1854 *
1855 * This function allocates a key slot and sets its metadata.
1856 *
1857 * If this function fails, call psa_fail_key_creation().
1858 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001859 * This function is intended to be used as follows:
1860 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001861 * it with the specified attributes, and in case of a volatile key assign it
1862 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001863 * -# Populate the slot with the key material.
1864 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1865 * In case of failure at any step, stop the sequence and call
1866 * psa_fail_key_creation().
1867 *
Ronald Cronf95a2b12020-10-22 15:24:49 +02001868 * On success, the access counter of the returned key slot is incremented by
1869 * one. It is the responsibility of the caller to call
1870 * psa_decrement_key_slot_access_count() when it does not access the key slot
1871 * anymore.
1872 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001873 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001874 * \param[in] attributes Key attributes for the new key.
Ronald Cron3c76a422020-10-16 12:17:04 +02001875 * \param[out] key On success, identifier of the key. Note that the
1876 * key identifier is also stored in the prepared
1877 * slot.
Gilles Peskine011e4282019-06-26 18:34:38 +02001878 * \param[out] p_slot On success, a pointer to the prepared slot.
1879 * \param[out] p_drv On any return, the driver for the key, if any.
1880 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001881 *
1882 * \retval #PSA_SUCCESS
1883 * The key slot is ready to receive key material.
1884 * \return If this function fails, the key slot is an invalid state.
1885 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001886 */
1887static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001888 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001889 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001890 mbedtls_svc_key_id_t *key,
Gilles Peskine011e4282019-06-26 18:34:38 +02001891 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001892 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001893{
1894 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001895 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001896 psa_key_slot_t *slot;
1897
Gilles Peskinedf179142019-07-15 22:02:14 +02001898 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001899 *p_drv = NULL;
1900
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001901 status = psa_validate_key_attributes( attributes, p_drv );
1902 if( status != PSA_SUCCESS )
1903 return( status );
1904
Ronald Cronc4d1b512020-07-31 11:26:37 +02001905 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001906 if( status != PSA_SUCCESS )
1907 return( status );
1908 slot = *p_slot;
1909
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001910 /* We're storing the declared bit-size of the key. It's up to each
1911 * creation mechanism to verify that this information is correct.
1912 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001913 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001914 * is optional (import, copy). In case of a volatile key, assign it the
1915 * volatile key identifier associated to the slot returned to contain its
1916 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001917
1918 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001919 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1920 {
1921#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1922 slot->attr.id = volatile_key_id;
1923#else
1924 slot->attr.id.key_id = volatile_key_id;
1925#endif
1926 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001927
Gilles Peskine91e8c332019-08-02 19:19:39 +02001928 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001929 * external-only flags, query `attributes`. Thanks to the check
1930 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001931 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001932 * may have set. */
1933 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001934
Gilles Peskinecbaff462019-07-12 23:46:04 +02001935#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001936 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001937 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001938 * create the key file in internal storage, create the
1939 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001940 * persistent data. This is done by starting a transaction that will
1941 * encompass these three actions.
1942 * For registering a volatile key, we just need to find an appropriate
1943 * slot number inside the SE. Since the key is designated volatile, creating
1944 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001945 /* The first thing to do is to find a slot number for the new key.
1946 * We save the slot number in persistent storage as part of the
1947 * transaction data. It will be needed to recover if the power
1948 * fails during the key creation process, to clean up on the secure
1949 * element side after restarting. Obtaining a slot number from the
1950 * secure element driver updates its persistent state, but we do not yet
1951 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001952 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001953 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001954 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001955 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001956 &slot->data.se.slot_number );
1957 if( status != PSA_SUCCESS )
1958 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001959
1960 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001961 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001962 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1963 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1964 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1965 psa_crypto_transaction.key.id = slot->attr.id;
1966 status = psa_crypto_save_transaction( );
1967 if( status != PSA_SUCCESS )
1968 {
1969 (void) psa_crypto_stop_transaction( );
1970 return( status );
1971 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001972 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001973 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001974
1975 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1976 {
1977 /* Key registration only makes sense with a secure element. */
1978 return( PSA_ERROR_INVALID_ARGUMENT );
1979 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001980#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1981
Ronald Croncf56a0a2020-08-04 09:51:30 +02001982 *key = slot->attr.id;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001983 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001984}
Gilles Peskine4747d192019-04-17 15:05:45 +02001985
1986/** Finalize the creation of a key once its key material has been set.
1987 *
1988 * This entails writing the key to persistent storage.
1989 *
1990 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001991 * See the documentation of psa_start_key_creation() for the intended use
1992 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001993 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001994 * \param[in,out] slot Pointer to the slot with key material.
1995 * \param[in] driver The secure element driver for the key,
1996 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001997 *
1998 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001999 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002000 * \return If this function fails, the key slot is an invalid state.
2001 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002002 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002003static psa_status_t psa_finish_key_creation(
2004 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002005 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002006{
2007 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002008 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002009 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002010
2011#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002012 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002013 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002014#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2015 if( driver != NULL )
2016 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002017 psa_se_key_data_storage_t data;
2018#if defined(static_assert)
2019 static_assert( sizeof( slot->data.se.slot_number ) ==
2020 sizeof( data.slot_number ),
2021 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002022#endif
2023 memcpy( &data.slot_number, &slot->data.se.slot_number,
2024 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002025 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002026 (uint8_t*) &data,
2027 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002028 }
2029 else
2030#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2031 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002032 /* Key material is saved in export representation in the slot, so
2033 * just pass the slot buffer for storage. */
2034 status = psa_save_persistent_key( &slot->attr,
2035 slot->data.key.data,
2036 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002037 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002038 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002039#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2040
Gilles Peskinecbaff462019-07-12 23:46:04 +02002041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002042 /* Finish the transaction for a key creation. This does not
2043 * happen when registering an existing key. Detect this case
2044 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002045 * creation of a persistent key in a secure element requires a transaction,
2046 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002047 if( driver != NULL &&
2048 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002049 {
2050 status = psa_save_se_persistent_data( driver );
2051 if( status != PSA_SUCCESS )
2052 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002053 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002054 return( status );
2055 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002056 status = psa_crypto_stop_transaction( );
2057 if( status != PSA_SUCCESS )
2058 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002059 }
2060#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2061
Gilles Peskine4747d192019-04-17 15:05:45 +02002062 return( status );
2063}
2064
2065/** Abort the creation of a key.
2066 *
2067 * You may call this function after calling psa_start_key_creation(),
2068 * or after psa_finish_key_creation() fails. In other circumstances, this
2069 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002070 * See the documentation of psa_start_key_creation() for the intended use
2071 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002072 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002073 * \param[in,out] slot Pointer to the slot with key material.
2074 * \param[in] driver The secure element driver for the key,
2075 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002076 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002077static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002078 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002079{
Gilles Peskine011e4282019-06-26 18:34:38 +02002080 (void) driver;
2081
Gilles Peskine4747d192019-04-17 15:05:45 +02002082 if( slot == NULL )
2083 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002084
2085#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002086 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002087 * element, and the failure happened later (when saving metadata
2088 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002089 * element.
2090 * https://github.com/ARMmbed/mbed-crypto/issues/217
2091 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002092
Gilles Peskined7729582019-08-05 15:55:54 +02002093 /* Abort the ongoing transaction if any (there may not be one if
2094 * the creation process failed before starting one, or if the
2095 * key creation is a registration of a key in a secure element).
2096 * Earlier functions must already have done what it takes to undo any
2097 * partial creation. All that's left is to update the transaction data
2098 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002099 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002100#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2101
Gilles Peskine4747d192019-04-17 15:05:45 +02002102 psa_wipe_key_slot( slot );
2103}
2104
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002105/** Validate optional attributes during key creation.
2106 *
2107 * Some key attributes are optional during key creation. If they are
2108 * specified in the attributes structure, check that they are consistent
2109 * with the data in the slot.
2110 *
2111 * This function should be called near the end of key creation, after
2112 * the slot in memory is fully populated but before saving persistent data.
2113 */
2114static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002115 const psa_key_slot_t *slot,
2116 const psa_key_attributes_t *attributes )
2117{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002118 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002119 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002120 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002121 return( PSA_ERROR_INVALID_ARGUMENT );
2122 }
2123
2124 if( attributes->domain_parameters_size != 0 )
2125 {
2126#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002127 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002128 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002129 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002130 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002132
Steven Cooreman7f391872020-07-30 14:57:44 +02002133 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002134 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002135 slot->data.key.data,
2136 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002137 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002138 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002139 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002140
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002141 mbedtls_mpi_init( &actual );
2142 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002143 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002144 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002145 mbedtls_rsa_free( rsa );
2146 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002147 if( ret != 0 )
2148 goto rsa_exit;
2149 ret = mbedtls_mpi_read_binary( &required,
2150 attributes->domain_parameters,
2151 attributes->domain_parameters_size );
2152 if( ret != 0 )
2153 goto rsa_exit;
2154 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2155 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2156 rsa_exit:
2157 mbedtls_mpi_free( &actual );
2158 mbedtls_mpi_free( &required );
2159 if( ret != 0)
2160 return( mbedtls_to_psa_error( ret ) );
2161 }
2162 else
2163#endif
2164 {
2165 return( PSA_ERROR_INVALID_ARGUMENT );
2166 }
2167 }
2168
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002169 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002170 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002171 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002172 return( PSA_ERROR_INVALID_ARGUMENT );
2173 }
2174
2175 return( PSA_SUCCESS );
2176}
2177
Gilles Peskine4747d192019-04-17 15:05:45 +02002178psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002179 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002180 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002181 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002182{
2183 psa_status_t status;
2184 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002185 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002186
Gilles Peskine0f84d622019-09-12 19:03:13 +02002187 /* Reject zero-length symmetric keys (including raw data key objects).
2188 * This also rejects any key which might be encoded as an empty string,
2189 * which is never valid. */
2190 if( data_length == 0 )
2191 return( PSA_ERROR_INVALID_ARGUMENT );
2192
Gilles Peskinedf179142019-07-15 22:02:14 +02002193 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002194 key, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002195 if( status != PSA_SUCCESS )
2196 goto exit;
2197
Gilles Peskine5d309672019-07-12 23:47:28 +02002198#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2199 if( driver != NULL )
2200 {
2201 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002202 /* The driver should set the number of key bits, however in
2203 * case it doesn't, we initialize bits to an invalid value. */
2204 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002205 if( drv->key_management == NULL ||
2206 drv->key_management->p_import == NULL )
2207 {
2208 status = PSA_ERROR_NOT_SUPPORTED;
2209 goto exit;
2210 }
2211 status = drv->key_management->p_import(
2212 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002213 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002214 &bits );
2215 if( status != PSA_SUCCESS )
2216 goto exit;
2217 if( bits > PSA_MAX_KEY_BITS )
2218 {
2219 status = PSA_ERROR_NOT_SUPPORTED;
2220 goto exit;
2221 }
2222 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002223 }
2224 else
2225#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2226 {
2227 status = psa_import_key_into_slot( slot, data, data_length );
2228 if( status != PSA_SUCCESS )
2229 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002230 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002231 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002232 if( status != PSA_SUCCESS )
2233 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002234
Gilles Peskine011e4282019-06-26 18:34:38 +02002235 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002236exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002237 if( status != PSA_SUCCESS )
2238 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002239 psa_fail_key_creation( slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002240 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4747d192019-04-17 15:05:45 +02002241 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002242 else
2243 status = psa_decrement_key_slot_access_count( slot );
2244
Gilles Peskine4747d192019-04-17 15:05:45 +02002245 return( status );
2246}
2247
Gilles Peskined7729582019-08-05 15:55:54 +02002248#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2249psa_status_t mbedtls_psa_register_se_key(
2250 const psa_key_attributes_t *attributes )
2251{
2252 psa_status_t status;
2253 psa_key_slot_t *slot = NULL;
2254 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002255 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002256
2257 /* Leaving attributes unspecified is not currently supported.
2258 * It could make sense to query the key type and size from the
2259 * secure element, but not all secure elements support this
2260 * and the driver HAL doesn't currently support it. */
2261 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2262 return( PSA_ERROR_NOT_SUPPORTED );
2263 if( psa_get_key_bits( attributes ) == 0 )
2264 return( PSA_ERROR_NOT_SUPPORTED );
2265
2266 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002267 &key, &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002268 if( status != PSA_SUCCESS )
2269 goto exit;
2270
Gilles Peskined7729582019-08-05 15:55:54 +02002271 status = psa_finish_key_creation( slot, driver );
2272
2273exit:
2274 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002275 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002276 else
2277 status = psa_decrement_key_slot_access_count( slot );
2278
Gilles Peskined7729582019-08-05 15:55:54 +02002279 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002280 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002281 return( status );
2282}
2283#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2284
Gilles Peskinef603c712019-01-19 13:40:11 +01002285static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002286 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002287{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002288 psa_status_t status = psa_copy_key_material_into_slot( target,
2289 source->data.key.data,
2290 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002291 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002292 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002293
Steven Cooreman398aee52020-10-13 14:35:45 +02002294 target->attr.type = source->attr.type;
2295 target->attr.bits = source->attr.bits;
2296
2297 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002298}
2299
Ronald Croncf56a0a2020-08-04 09:51:30 +02002300psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002301 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002302 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002303{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002304 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2305 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002306 psa_key_slot_t *source_slot = NULL;
2307 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002308 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002309 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002310
Ronald Croncf56a0a2020-08-04 09:51:30 +02002311 status = psa_get_transparent_key( source_key, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002312 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002313 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002314 goto exit;
2315
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002316 status = psa_validate_optional_attributes( source_slot,
2317 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002318 if( status != PSA_SUCCESS )
2319 goto exit;
2320
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002321 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002322 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002323 if( status != PSA_SUCCESS )
2324 goto exit;
2325
Gilles Peskinedf179142019-07-15 22:02:14 +02002326 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2327 &actual_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002328 target_key, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002329 if( status != PSA_SUCCESS )
2330 goto exit;
2331
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002332#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2333 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002334 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002335 /* Copying to a secure element is not implemented yet. */
2336 status = PSA_ERROR_NOT_SUPPORTED;
2337 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002338 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002339#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002340
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002341 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002342 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002343 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002344
Gilles Peskine011e4282019-06-26 18:34:38 +02002345 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002346exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002347 if( status != PSA_SUCCESS )
2348 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002349 psa_fail_key_creation( target_slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002350 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002351 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002352 else
2353 status = psa_decrement_key_slot_access_count( target_slot );
2354
2355 decrement_status = psa_decrement_key_slot_access_count( source_slot );
2356
2357 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002358}
2359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002360
2361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002362/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002363/* Message digests */
2364/****************************************************************/
2365
John Durkop0ea39e02020-10-13 19:58:20 -07002366#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002367static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002368{
2369 switch( alg )
2370 {
2371#if defined(MBEDTLS_MD2_C)
2372 case PSA_ALG_MD2:
2373 return( &mbedtls_md2_info );
2374#endif
2375#if defined(MBEDTLS_MD4_C)
2376 case PSA_ALG_MD4:
2377 return( &mbedtls_md4_info );
2378#endif
2379#if defined(MBEDTLS_MD5_C)
2380 case PSA_ALG_MD5:
2381 return( &mbedtls_md5_info );
2382#endif
2383#if defined(MBEDTLS_RIPEMD160_C)
2384 case PSA_ALG_RIPEMD160:
2385 return( &mbedtls_ripemd160_info );
2386#endif
2387#if defined(MBEDTLS_SHA1_C)
2388 case PSA_ALG_SHA_1:
2389 return( &mbedtls_sha1_info );
2390#endif
2391#if defined(MBEDTLS_SHA256_C)
2392 case PSA_ALG_SHA_224:
2393 return( &mbedtls_sha224_info );
2394 case PSA_ALG_SHA_256:
2395 return( &mbedtls_sha256_info );
2396#endif
2397#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002398#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002399 case PSA_ALG_SHA_384:
2400 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002401#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002402 case PSA_ALG_SHA_512:
2403 return( &mbedtls_sha512_info );
2404#endif
2405 default:
2406 return( NULL );
2407 }
2408}
John Durkop0ea39e02020-10-13 19:58:20 -07002409#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002410
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002411psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2412{
2413 switch( operation->alg )
2414 {
Gilles Peskine81736312018-06-26 15:04:31 +02002415 case 0:
2416 /* The object has (apparently) been initialized but it is not
2417 * in use. It's ok to call abort on such an object, and there's
2418 * nothing to do. */
2419 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002420#if defined(MBEDTLS_MD2_C)
2421 case PSA_ALG_MD2:
2422 mbedtls_md2_free( &operation->ctx.md2 );
2423 break;
2424#endif
2425#if defined(MBEDTLS_MD4_C)
2426 case PSA_ALG_MD4:
2427 mbedtls_md4_free( &operation->ctx.md4 );
2428 break;
2429#endif
2430#if defined(MBEDTLS_MD5_C)
2431 case PSA_ALG_MD5:
2432 mbedtls_md5_free( &operation->ctx.md5 );
2433 break;
2434#endif
2435#if defined(MBEDTLS_RIPEMD160_C)
2436 case PSA_ALG_RIPEMD160:
2437 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2438 break;
2439#endif
2440#if defined(MBEDTLS_SHA1_C)
2441 case PSA_ALG_SHA_1:
2442 mbedtls_sha1_free( &operation->ctx.sha1 );
2443 break;
2444#endif
2445#if defined(MBEDTLS_SHA256_C)
2446 case PSA_ALG_SHA_224:
2447 case PSA_ALG_SHA_256:
2448 mbedtls_sha256_free( &operation->ctx.sha256 );
2449 break;
2450#endif
2451#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002452#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002453 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002454#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002455 case PSA_ALG_SHA_512:
2456 mbedtls_sha512_free( &operation->ctx.sha512 );
2457 break;
2458#endif
2459 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002460 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002461 }
2462 operation->alg = 0;
2463 return( PSA_SUCCESS );
2464}
2465
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002466psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002467 psa_algorithm_t alg )
2468{
Janos Follath24eed8d2019-11-22 13:21:35 +00002469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002470
2471 /* A context must be freshly initialized before it can be set up. */
2472 if( operation->alg != 0 )
2473 {
2474 return( PSA_ERROR_BAD_STATE );
2475 }
2476
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002477 switch( alg )
2478 {
2479#if defined(MBEDTLS_MD2_C)
2480 case PSA_ALG_MD2:
2481 mbedtls_md2_init( &operation->ctx.md2 );
2482 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2483 break;
2484#endif
2485#if defined(MBEDTLS_MD4_C)
2486 case PSA_ALG_MD4:
2487 mbedtls_md4_init( &operation->ctx.md4 );
2488 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2489 break;
2490#endif
2491#if defined(MBEDTLS_MD5_C)
2492 case PSA_ALG_MD5:
2493 mbedtls_md5_init( &operation->ctx.md5 );
2494 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2495 break;
2496#endif
2497#if defined(MBEDTLS_RIPEMD160_C)
2498 case PSA_ALG_RIPEMD160:
2499 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2500 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2501 break;
2502#endif
2503#if defined(MBEDTLS_SHA1_C)
2504 case PSA_ALG_SHA_1:
2505 mbedtls_sha1_init( &operation->ctx.sha1 );
2506 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2507 break;
2508#endif
2509#if defined(MBEDTLS_SHA256_C)
2510 case PSA_ALG_SHA_224:
2511 mbedtls_sha256_init( &operation->ctx.sha256 );
2512 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2513 break;
2514 case PSA_ALG_SHA_256:
2515 mbedtls_sha256_init( &operation->ctx.sha256 );
2516 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2517 break;
2518#endif
2519#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002520#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002521 case PSA_ALG_SHA_384:
2522 mbedtls_sha512_init( &operation->ctx.sha512 );
2523 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2524 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002525#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002526 case PSA_ALG_SHA_512:
2527 mbedtls_sha512_init( &operation->ctx.sha512 );
2528 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2529 break;
2530#endif
2531 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002532 return( PSA_ALG_IS_HASH( alg ) ?
2533 PSA_ERROR_NOT_SUPPORTED :
2534 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002535 }
2536 if( ret == 0 )
2537 operation->alg = alg;
2538 else
2539 psa_hash_abort( operation );
2540 return( mbedtls_to_psa_error( ret ) );
2541}
2542
2543psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2544 const uint8_t *input,
2545 size_t input_length )
2546{
Janos Follath24eed8d2019-11-22 13:21:35 +00002547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002548
2549 /* Don't require hash implementations to behave correctly on a
2550 * zero-length input, which may have an invalid pointer. */
2551 if( input_length == 0 )
2552 return( PSA_SUCCESS );
2553
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002554 switch( operation->alg )
2555 {
2556#if defined(MBEDTLS_MD2_C)
2557 case PSA_ALG_MD2:
2558 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2559 input, input_length );
2560 break;
2561#endif
2562#if defined(MBEDTLS_MD4_C)
2563 case PSA_ALG_MD4:
2564 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2565 input, input_length );
2566 break;
2567#endif
2568#if defined(MBEDTLS_MD5_C)
2569 case PSA_ALG_MD5:
2570 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2571 input, input_length );
2572 break;
2573#endif
2574#if defined(MBEDTLS_RIPEMD160_C)
2575 case PSA_ALG_RIPEMD160:
2576 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2577 input, input_length );
2578 break;
2579#endif
2580#if defined(MBEDTLS_SHA1_C)
2581 case PSA_ALG_SHA_1:
2582 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2583 input, input_length );
2584 break;
2585#endif
2586#if defined(MBEDTLS_SHA256_C)
2587 case PSA_ALG_SHA_224:
2588 case PSA_ALG_SHA_256:
2589 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2590 input, input_length );
2591 break;
2592#endif
2593#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002594#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002595 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002596#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002597 case PSA_ALG_SHA_512:
2598 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2599 input, input_length );
2600 break;
2601#endif
2602 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002603 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002604 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002605
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002606 if( ret != 0 )
2607 psa_hash_abort( operation );
2608 return( mbedtls_to_psa_error( ret ) );
2609}
2610
2611psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2612 uint8_t *hash,
2613 size_t hash_size,
2614 size_t *hash_length )
2615{
itayzafrir40835d42018-08-02 13:14:17 +03002616 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002617 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002618 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002619
2620 /* Fill the output buffer with something that isn't a valid hash
2621 * (barring an attack on the hash and deliberately-crafted input),
2622 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002623 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002624 /* If hash_size is 0 then hash may be NULL and then the
2625 * call to memset would have undefined behavior. */
2626 if( hash_size != 0 )
2627 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002628
2629 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002630 {
2631 status = PSA_ERROR_BUFFER_TOO_SMALL;
2632 goto exit;
2633 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002634
2635 switch( operation->alg )
2636 {
2637#if defined(MBEDTLS_MD2_C)
2638 case PSA_ALG_MD2:
2639 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2640 break;
2641#endif
2642#if defined(MBEDTLS_MD4_C)
2643 case PSA_ALG_MD4:
2644 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2645 break;
2646#endif
2647#if defined(MBEDTLS_MD5_C)
2648 case PSA_ALG_MD5:
2649 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2650 break;
2651#endif
2652#if defined(MBEDTLS_RIPEMD160_C)
2653 case PSA_ALG_RIPEMD160:
2654 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2655 break;
2656#endif
2657#if defined(MBEDTLS_SHA1_C)
2658 case PSA_ALG_SHA_1:
2659 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2660 break;
2661#endif
2662#if defined(MBEDTLS_SHA256_C)
2663 case PSA_ALG_SHA_224:
2664 case PSA_ALG_SHA_256:
2665 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2666 break;
2667#endif
2668#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002669#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002670 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002671#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002672 case PSA_ALG_SHA_512:
2673 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2674 break;
2675#endif
2676 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002677 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002678 }
itayzafrir40835d42018-08-02 13:14:17 +03002679 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002680
itayzafrir40835d42018-08-02 13:14:17 +03002681exit:
2682 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002683 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002684 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002685 return( psa_hash_abort( operation ) );
2686 }
2687 else
2688 {
2689 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002690 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002691 }
2692}
2693
Gilles Peskine2d277862018-06-18 15:41:12 +02002694psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2695 const uint8_t *hash,
2696 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002697{
2698 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2699 size_t actual_hash_length;
2700 psa_status_t status = psa_hash_finish( operation,
2701 actual_hash, sizeof( actual_hash ),
2702 &actual_hash_length );
2703 if( status != PSA_SUCCESS )
2704 return( status );
2705 if( actual_hash_length != hash_length )
2706 return( PSA_ERROR_INVALID_SIGNATURE );
2707 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2708 return( PSA_ERROR_INVALID_SIGNATURE );
2709 return( PSA_SUCCESS );
2710}
2711
Gilles Peskine0a749c82019-11-28 19:33:58 +01002712psa_status_t psa_hash_compute( psa_algorithm_t alg,
2713 const uint8_t *input, size_t input_length,
2714 uint8_t *hash, size_t hash_size,
2715 size_t *hash_length )
2716{
2717 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2718 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2719
2720 *hash_length = hash_size;
2721 status = psa_hash_setup( &operation, alg );
2722 if( status != PSA_SUCCESS )
2723 goto exit;
2724 status = psa_hash_update( &operation, input, input_length );
2725 if( status != PSA_SUCCESS )
2726 goto exit;
2727 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2728 if( status != PSA_SUCCESS )
2729 goto exit;
2730
2731exit:
2732 if( status == PSA_SUCCESS )
2733 status = psa_hash_abort( &operation );
2734 else
2735 psa_hash_abort( &operation );
2736 return( status );
2737}
2738
2739psa_status_t psa_hash_compare( psa_algorithm_t alg,
2740 const uint8_t *input, size_t input_length,
2741 const uint8_t *hash, size_t hash_length )
2742{
2743 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2744 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2745
2746 status = psa_hash_setup( &operation, alg );
2747 if( status != PSA_SUCCESS )
2748 goto exit;
2749 status = psa_hash_update( &operation, input, input_length );
2750 if( status != PSA_SUCCESS )
2751 goto exit;
2752 status = psa_hash_verify( &operation, hash, hash_length );
2753 if( status != PSA_SUCCESS )
2754 goto exit;
2755
2756exit:
2757 if( status == PSA_SUCCESS )
2758 status = psa_hash_abort( &operation );
2759 else
2760 psa_hash_abort( &operation );
2761 return( status );
2762}
2763
Gilles Peskineeb35d782019-01-22 17:56:16 +01002764psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2765 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002766{
2767 if( target_operation->alg != 0 )
2768 return( PSA_ERROR_BAD_STATE );
2769
2770 switch( source_operation->alg )
2771 {
2772 case 0:
2773 return( PSA_ERROR_BAD_STATE );
2774#if defined(MBEDTLS_MD2_C)
2775 case PSA_ALG_MD2:
2776 mbedtls_md2_clone( &target_operation->ctx.md2,
2777 &source_operation->ctx.md2 );
2778 break;
2779#endif
2780#if defined(MBEDTLS_MD4_C)
2781 case PSA_ALG_MD4:
2782 mbedtls_md4_clone( &target_operation->ctx.md4,
2783 &source_operation->ctx.md4 );
2784 break;
2785#endif
2786#if defined(MBEDTLS_MD5_C)
2787 case PSA_ALG_MD5:
2788 mbedtls_md5_clone( &target_operation->ctx.md5,
2789 &source_operation->ctx.md5 );
2790 break;
2791#endif
2792#if defined(MBEDTLS_RIPEMD160_C)
2793 case PSA_ALG_RIPEMD160:
2794 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2795 &source_operation->ctx.ripemd160 );
2796 break;
2797#endif
2798#if defined(MBEDTLS_SHA1_C)
2799 case PSA_ALG_SHA_1:
2800 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2801 &source_operation->ctx.sha1 );
2802 break;
2803#endif
2804#if defined(MBEDTLS_SHA256_C)
2805 case PSA_ALG_SHA_224:
2806 case PSA_ALG_SHA_256:
2807 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2808 &source_operation->ctx.sha256 );
2809 break;
2810#endif
2811#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002812#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002813 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002814#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002815 case PSA_ALG_SHA_512:
2816 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2817 &source_operation->ctx.sha512 );
2818 break;
2819#endif
2820 default:
2821 return( PSA_ERROR_NOT_SUPPORTED );
2822 }
2823
2824 target_operation->alg = source_operation->alg;
2825 return( PSA_SUCCESS );
2826}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002827
2828
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002829/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002830/* MAC */
2831/****************************************************************/
2832
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002833static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002834 psa_algorithm_t alg,
2835 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002836 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002837 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002838{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002839 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002840 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002841
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002842 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002843 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002844
Gilles Peskine8c9def32018-02-08 10:02:12 +01002845 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2846 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002847 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002848 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002849 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002850 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002851 mode = MBEDTLS_MODE_STREAM;
2852 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002853 case PSA_ALG_CTR:
2854 mode = MBEDTLS_MODE_CTR;
2855 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002856 case PSA_ALG_CFB:
2857 mode = MBEDTLS_MODE_CFB;
2858 break;
2859 case PSA_ALG_OFB:
2860 mode = MBEDTLS_MODE_OFB;
2861 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002862 case PSA_ALG_ECB_NO_PADDING:
2863 mode = MBEDTLS_MODE_ECB;
2864 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002865 case PSA_ALG_CBC_NO_PADDING:
2866 mode = MBEDTLS_MODE_CBC;
2867 break;
2868 case PSA_ALG_CBC_PKCS7:
2869 mode = MBEDTLS_MODE_CBC;
2870 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002871 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002872 mode = MBEDTLS_MODE_CCM;
2873 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002874 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002875 mode = MBEDTLS_MODE_GCM;
2876 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002877 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2878 mode = MBEDTLS_MODE_CHACHAPOLY;
2879 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002880 default:
2881 return( NULL );
2882 }
2883 }
2884 else if( alg == PSA_ALG_CMAC )
2885 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002886 else
2887 return( NULL );
2888
2889 switch( key_type )
2890 {
2891 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002892 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002893 break;
2894 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002895 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2896 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002897 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002898 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002899 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002900 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002901 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2902 * but two-key Triple-DES is functionally three-key Triple-DES
2903 * with K1=K3, so that's how we present it to mbedtls. */
2904 if( key_bits == 128 )
2905 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002906 break;
2907 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002908 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002909 break;
2910 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002911 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002912 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002913 case PSA_KEY_TYPE_CHACHA20:
2914 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2915 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002916 default:
2917 return( NULL );
2918 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002919 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002920 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002921
Jaeden Amero23bbb752018-06-26 14:16:54 +01002922 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2923 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002924}
2925
Gilles Peskinea05219c2018-11-16 16:02:56 +01002926#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002927static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002928{
Gilles Peskine2d277862018-06-18 15:41:12 +02002929 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002930 {
2931 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002932 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002933 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002934 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002935 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002936 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002937 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002938 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002939 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002940 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002941 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002942 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002943 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002944 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002945 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002946 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002947 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002948 return( 128 );
2949 default:
2950 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002951 }
2952}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002953#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002954
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002955/* Initialize the MAC operation structure. Once this function has been
2956 * called, psa_mac_abort can run and will do the right thing. */
2957static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2958 psa_algorithm_t alg )
2959{
2960 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2961
2962 operation->alg = alg;
2963 operation->key_set = 0;
2964 operation->iv_set = 0;
2965 operation->iv_required = 0;
2966 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002967 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002968
2969#if defined(MBEDTLS_CMAC_C)
2970 if( alg == PSA_ALG_CMAC )
2971 {
2972 operation->iv_required = 0;
2973 mbedtls_cipher_init( &operation->ctx.cmac );
2974 status = PSA_SUCCESS;
2975 }
2976 else
2977#endif /* MBEDTLS_CMAC_C */
2978#if defined(MBEDTLS_MD_C)
2979 if( PSA_ALG_IS_HMAC( operation->alg ) )
2980 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002981 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2982 operation->ctx.hmac.hash_ctx.alg = 0;
2983 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002984 }
2985 else
2986#endif /* MBEDTLS_MD_C */
2987 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002988 if( ! PSA_ALG_IS_MAC( alg ) )
2989 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002990 }
2991
2992 if( status != PSA_SUCCESS )
2993 memset( operation, 0, sizeof( *operation ) );
2994 return( status );
2995}
2996
Gilles Peskine01126fa2018-07-12 17:04:55 +02002997#if defined(MBEDTLS_MD_C)
2998static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2999{
Gilles Peskine3f108122018-12-07 18:14:53 +01003000 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003001 return( psa_hash_abort( &hmac->hash_ctx ) );
3002}
3003#endif /* MBEDTLS_MD_C */
3004
Gilles Peskine8c9def32018-02-08 10:02:12 +01003005psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3006{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003007 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003009 /* The object has (apparently) been initialized but it is not
3010 * in use. It's ok to call abort on such an object, and there's
3011 * nothing to do. */
3012 return( PSA_SUCCESS );
3013 }
3014 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003015#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003016 if( operation->alg == PSA_ALG_CMAC )
3017 {
3018 mbedtls_cipher_free( &operation->ctx.cmac );
3019 }
3020 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003021#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003022#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003023 if( PSA_ALG_IS_HMAC( operation->alg ) )
3024 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003025 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003026 }
3027 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003028#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003029 {
3030 /* Sanity check (shouldn't happen: operation->alg should
3031 * always have been initialized to a valid value). */
3032 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003033 }
Moran Peker41deec42018-04-04 15:43:05 +03003034
Gilles Peskine8c9def32018-02-08 10:02:12 +01003035 operation->alg = 0;
3036 operation->key_set = 0;
3037 operation->iv_set = 0;
3038 operation->iv_required = 0;
3039 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003040 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003041
Gilles Peskine8c9def32018-02-08 10:02:12 +01003042 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003043
3044bad_state:
3045 /* If abort is called on an uninitialized object, we can't trust
3046 * anything. Wipe the object in case it contains confidential data.
3047 * This may result in a memory leak if a pointer gets overwritten,
3048 * but it's too late to do anything about this. */
3049 memset( operation, 0, sizeof( *operation ) );
3050 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003051}
3052
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003053#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003054static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003055 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003056 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003057 const mbedtls_cipher_info_t *cipher_info )
3058{
Janos Follath24eed8d2019-11-22 13:21:35 +00003059 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003060
3061 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003062
3063 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3064 if( ret != 0 )
3065 return( ret );
3066
3067 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003068 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003069 key_bits );
3070 return( ret );
3071}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003072#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003073
Gilles Peskine248051a2018-06-20 16:09:38 +02003074#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003075static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3076 const uint8_t *key,
3077 size_t key_length,
3078 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003079{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003080 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003081 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003082 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003083 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003084 psa_status_t status;
3085
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003086 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3087 * overflow below. This should never trigger if the hash algorithm
3088 * is implemented correctly. */
3089 /* The size checks against the ipad and opad buffers cannot be written
3090 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3091 * because that triggers -Wlogical-op on GCC 7.3. */
3092 if( block_size > sizeof( ipad ) )
3093 return( PSA_ERROR_NOT_SUPPORTED );
3094 if( block_size > sizeof( hmac->opad ) )
3095 return( PSA_ERROR_NOT_SUPPORTED );
3096 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003097 return( PSA_ERROR_NOT_SUPPORTED );
3098
Gilles Peskined223b522018-06-11 18:12:58 +02003099 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003100 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003101 status = psa_hash_compute( hash_alg, key, key_length,
3102 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003103 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003104 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003105 }
Gilles Peskine96889972018-07-12 17:07:03 +02003106 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3107 * but it is permitted. It is common when HMAC is used in HKDF, for
3108 * example. Don't call `memcpy` in the 0-length because `key` could be
3109 * an invalid pointer which would make the behavior undefined. */
3110 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003111 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003112
Gilles Peskined223b522018-06-11 18:12:58 +02003113 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3114 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003115 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003116 ipad[i] ^= 0x36;
3117 memset( ipad + key_length, 0x36, block_size - key_length );
3118
3119 /* Copy the key material from ipad to opad, flipping the requisite bits,
3120 * and filling the rest of opad with the requisite constant. */
3121 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003122 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3123 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003124
Gilles Peskine01126fa2018-07-12 17:04:55 +02003125 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003126 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003127 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003128
Gilles Peskine01126fa2018-07-12 17:04:55 +02003129 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003130
3131cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003132 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003133
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003134 return( status );
3135}
Gilles Peskine248051a2018-06-20 16:09:38 +02003136#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003137
Gilles Peskine89167cb2018-07-08 20:12:23 +02003138static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003139 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003140 psa_algorithm_t alg,
3141 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003142{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003143 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3144 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003145 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003146 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003147 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003148 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003149 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003150 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003151
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003152 /* A context must be freshly initialized before it can be set up. */
3153 if( operation->alg != 0 )
3154 {
3155 return( PSA_ERROR_BAD_STATE );
3156 }
3157
Gilles Peskined911eb72018-08-14 15:18:45 +02003158 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003159 if( status != PSA_SUCCESS )
3160 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003161 if( is_sign )
3162 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003163
Ronald Croncf56a0a2020-08-04 09:51:30 +02003164 status = psa_get_transparent_key( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003165 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003166 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003167 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003168
Gilles Peskine8c9def32018-02-08 10:02:12 +01003169#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003170 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003171 {
3172 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003173 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003174 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003175 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003176 if( cipher_info == NULL )
3177 {
3178 status = PSA_ERROR_NOT_SUPPORTED;
3179 goto exit;
3180 }
3181 operation->mac_size = cipher_info->block_size;
3182 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3183 status = mbedtls_to_psa_error( ret );
3184 }
3185 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003186#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003187#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003188 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003189 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003190 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003191 if( hash_alg == 0 )
3192 {
3193 status = PSA_ERROR_NOT_SUPPORTED;
3194 goto exit;
3195 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003196
3197 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3198 /* Sanity check. This shouldn't fail on a valid configuration. */
3199 if( operation->mac_size == 0 ||
3200 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3201 {
3202 status = PSA_ERROR_NOT_SUPPORTED;
3203 goto exit;
3204 }
3205
Gilles Peskine8e338702019-07-30 20:06:31 +02003206 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003207 {
3208 status = PSA_ERROR_INVALID_ARGUMENT;
3209 goto exit;
3210 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003211
Gilles Peskine01126fa2018-07-12 17:04:55 +02003212 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003213 slot->data.key.data,
3214 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003215 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003216 }
3217 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003218#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003219 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003220 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003221 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003222 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003223
Gilles Peskined911eb72018-08-14 15:18:45 +02003224 if( truncated == 0 )
3225 {
3226 /* The "normal" case: untruncated algorithm. Nothing to do. */
3227 }
3228 else if( truncated < 4 )
3229 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003230 /* A very short MAC is too short for security since it can be
3231 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3232 * so we make this our minimum, even though 32 bits is still
3233 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003234 status = PSA_ERROR_NOT_SUPPORTED;
3235 }
3236 else if( truncated > operation->mac_size )
3237 {
3238 /* It's impossible to "truncate" to a larger length. */
3239 status = PSA_ERROR_INVALID_ARGUMENT;
3240 }
3241 else
3242 operation->mac_size = truncated;
3243
Gilles Peskinefbfac682018-07-08 20:51:54 +02003244exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003245 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003246 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003247 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003248 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003249 else
3250 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003251 operation->key_set = 1;
3252 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003253
3254 decrement_status = psa_decrement_key_slot_access_count( slot );
3255
3256 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003257}
3258
Gilles Peskine89167cb2018-07-08 20:12:23 +02003259psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003260 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003261 psa_algorithm_t alg )
3262{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003263 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003264}
3265
3266psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003267 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003268 psa_algorithm_t alg )
3269{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003270 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003271}
3272
Gilles Peskine8c9def32018-02-08 10:02:12 +01003273psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3274 const uint8_t *input,
3275 size_t input_length )
3276{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003277 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003278 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003279 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003280 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003281 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003282 operation->has_input = 1;
3283
Gilles Peskine8c9def32018-02-08 10:02:12 +01003284#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003285 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003286 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003287 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3288 input, input_length );
3289 status = mbedtls_to_psa_error( ret );
3290 }
3291 else
3292#endif /* MBEDTLS_CMAC_C */
3293#if defined(MBEDTLS_MD_C)
3294 if( PSA_ALG_IS_HMAC( operation->alg ) )
3295 {
3296 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3297 input_length );
3298 }
3299 else
3300#endif /* MBEDTLS_MD_C */
3301 {
3302 /* This shouldn't happen if `operation` was initialized by
3303 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003304 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003305 }
3306
Gilles Peskinefbfac682018-07-08 20:51:54 +02003307 if( status != PSA_SUCCESS )
3308 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003309 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003310}
3311
Gilles Peskine01126fa2018-07-12 17:04:55 +02003312#if defined(MBEDTLS_MD_C)
3313static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3314 uint8_t *mac,
3315 size_t mac_size )
3316{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003317 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003318 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3319 size_t hash_size = 0;
3320 size_t block_size = psa_get_hash_block_size( hash_alg );
3321 psa_status_t status;
3322
Gilles Peskine01126fa2018-07-12 17:04:55 +02003323 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3324 if( status != PSA_SUCCESS )
3325 return( status );
3326 /* From here on, tmp needs to be wiped. */
3327
3328 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3329 if( status != PSA_SUCCESS )
3330 goto exit;
3331
3332 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3333 if( status != PSA_SUCCESS )
3334 goto exit;
3335
3336 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3337 if( status != PSA_SUCCESS )
3338 goto exit;
3339
Gilles Peskined911eb72018-08-14 15:18:45 +02003340 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3341 if( status != PSA_SUCCESS )
3342 goto exit;
3343
3344 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003345
3346exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003347 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003348 return( status );
3349}
3350#endif /* MBEDTLS_MD_C */
3351
mohammad16036df908f2018-04-02 08:34:15 -07003352static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003353 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003354 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003355{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003356 if( ! operation->key_set )
3357 return( PSA_ERROR_BAD_STATE );
3358 if( operation->iv_required && ! operation->iv_set )
3359 return( PSA_ERROR_BAD_STATE );
3360
Gilles Peskine8c9def32018-02-08 10:02:12 +01003361 if( mac_size < operation->mac_size )
3362 return( PSA_ERROR_BUFFER_TOO_SMALL );
3363
Gilles Peskine8c9def32018-02-08 10:02:12 +01003364#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003365 if( operation->alg == PSA_ALG_CMAC )
3366 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003367 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3368 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3369 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003370 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003371 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003372 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003373 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003374 else
3375#endif /* MBEDTLS_CMAC_C */
3376#if defined(MBEDTLS_MD_C)
3377 if( PSA_ALG_IS_HMAC( operation->alg ) )
3378 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003379 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003380 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003381 }
3382 else
3383#endif /* MBEDTLS_MD_C */
3384 {
3385 /* This shouldn't happen if `operation` was initialized by
3386 * a setup function. */
3387 return( PSA_ERROR_BAD_STATE );
3388 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003389}
3390
Gilles Peskineacd4be32018-07-08 19:56:25 +02003391psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3392 uint8_t *mac,
3393 size_t mac_size,
3394 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003395{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003396 psa_status_t status;
3397
Jaeden Amero252ef282019-02-15 14:05:35 +00003398 if( operation->alg == 0 )
3399 {
3400 return( PSA_ERROR_BAD_STATE );
3401 }
3402
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003403 /* Fill the output buffer with something that isn't a valid mac
3404 * (barring an attack on the mac and deliberately-crafted input),
3405 * in case the caller doesn't check the return status properly. */
3406 *mac_length = mac_size;
3407 /* If mac_size is 0 then mac may be NULL and then the
3408 * call to memset would have undefined behavior. */
3409 if( mac_size != 0 )
3410 memset( mac, '!', mac_size );
3411
Gilles Peskine89167cb2018-07-08 20:12:23 +02003412 if( ! operation->is_sign )
3413 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003414 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003415 }
mohammad16036df908f2018-04-02 08:34:15 -07003416
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003417 status = psa_mac_finish_internal( operation, mac, mac_size );
3418
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003419 if( status == PSA_SUCCESS )
3420 {
3421 status = psa_mac_abort( operation );
3422 if( status == PSA_SUCCESS )
3423 *mac_length = operation->mac_size;
3424 else
3425 memset( mac, '!', mac_size );
3426 }
3427 else
3428 psa_mac_abort( operation );
3429 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003430}
3431
Gilles Peskineacd4be32018-07-08 19:56:25 +02003432psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3433 const uint8_t *mac,
3434 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003435{
Gilles Peskine828ed142018-06-18 23:25:51 +02003436 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003437 psa_status_t status;
3438
Jaeden Amero252ef282019-02-15 14:05:35 +00003439 if( operation->alg == 0 )
3440 {
3441 return( PSA_ERROR_BAD_STATE );
3442 }
3443
Gilles Peskine89167cb2018-07-08 20:12:23 +02003444 if( operation->is_sign )
3445 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003446 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003447 }
3448 if( operation->mac_size != mac_length )
3449 {
3450 status = PSA_ERROR_INVALID_SIGNATURE;
3451 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003452 }
mohammad16036df908f2018-04-02 08:34:15 -07003453
3454 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003455 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003456 if( status != PSA_SUCCESS )
3457 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003458
3459 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3460 status = PSA_ERROR_INVALID_SIGNATURE;
3461
3462cleanup:
3463 if( status == PSA_SUCCESS )
3464 status = psa_mac_abort( operation );
3465 else
3466 psa_mac_abort( operation );
3467
Gilles Peskine3f108122018-12-07 18:14:53 +01003468 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003469
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003470 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003471}
3472
3473
Gilles Peskine20035e32018-02-03 22:44:14 +01003474
Gilles Peskine20035e32018-02-03 22:44:14 +01003475/****************************************************************/
3476/* Asymmetric cryptography */
3477/****************************************************************/
3478
Gilles Peskine2b450e32018-06-27 15:42:46 +02003479#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003480/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003481 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003482static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3483 size_t hash_length,
3484 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003485{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003486 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003487 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003488 *md_alg = mbedtls_md_get_type( md_info );
3489
3490 /* The Mbed TLS RSA module uses an unsigned int for hash length
3491 * parameters. Validate that it fits so that we don't risk an
3492 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003493#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003494 if( hash_length > UINT_MAX )
3495 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003496#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003497
3498#if defined(MBEDTLS_PKCS1_V15)
3499 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3500 * must be correct. */
3501 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3502 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003503 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003504 if( md_info == NULL )
3505 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003506 if( mbedtls_md_get_size( md_info ) != hash_length )
3507 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003508 }
3509#endif /* MBEDTLS_PKCS1_V15 */
3510
3511#if defined(MBEDTLS_PKCS1_V21)
3512 /* PSS requires a hash internally. */
3513 if( PSA_ALG_IS_RSA_PSS( alg ) )
3514 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003515 if( md_info == NULL )
3516 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003517 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003518#endif /* MBEDTLS_PKCS1_V21 */
3519
Gilles Peskine61b91d42018-06-08 16:09:36 +02003520 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003521}
3522
Gilles Peskine2b450e32018-06-27 15:42:46 +02003523static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3524 psa_algorithm_t alg,
3525 const uint8_t *hash,
3526 size_t hash_length,
3527 uint8_t *signature,
3528 size_t signature_size,
3529 size_t *signature_length )
3530{
3531 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003533 mbedtls_md_type_t md_alg;
3534
3535 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3536 if( status != PSA_SUCCESS )
3537 return( status );
3538
Gilles Peskine630a18a2018-06-29 17:49:35 +02003539 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003540 return( PSA_ERROR_BUFFER_TOO_SMALL );
3541
3542#if defined(MBEDTLS_PKCS1_V15)
3543 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3544 {
3545 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3546 MBEDTLS_MD_NONE );
3547 ret = mbedtls_rsa_pkcs1_sign( rsa,
3548 mbedtls_ctr_drbg_random,
3549 &global_data.ctr_drbg,
3550 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003551 md_alg,
3552 (unsigned int) hash_length,
3553 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003554 signature );
3555 }
3556 else
3557#endif /* MBEDTLS_PKCS1_V15 */
3558#if defined(MBEDTLS_PKCS1_V21)
3559 if( PSA_ALG_IS_RSA_PSS( alg ) )
3560 {
3561 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3562 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3563 mbedtls_ctr_drbg_random,
3564 &global_data.ctr_drbg,
3565 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003566 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003567 (unsigned int) hash_length,
3568 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003569 signature );
3570 }
3571 else
3572#endif /* MBEDTLS_PKCS1_V21 */
3573 {
3574 return( PSA_ERROR_INVALID_ARGUMENT );
3575 }
3576
3577 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003578 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003579 return( mbedtls_to_psa_error( ret ) );
3580}
3581
3582static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3583 psa_algorithm_t alg,
3584 const uint8_t *hash,
3585 size_t hash_length,
3586 const uint8_t *signature,
3587 size_t signature_length )
3588{
3589 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003591 mbedtls_md_type_t md_alg;
3592
3593 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3594 if( status != PSA_SUCCESS )
3595 return( status );
3596
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003597 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3598 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003599
3600#if defined(MBEDTLS_PKCS1_V15)
3601 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3602 {
3603 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3604 MBEDTLS_MD_NONE );
3605 ret = mbedtls_rsa_pkcs1_verify( rsa,
3606 mbedtls_ctr_drbg_random,
3607 &global_data.ctr_drbg,
3608 MBEDTLS_RSA_PUBLIC,
3609 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003610 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003611 hash,
3612 signature );
3613 }
3614 else
3615#endif /* MBEDTLS_PKCS1_V15 */
3616#if defined(MBEDTLS_PKCS1_V21)
3617 if( PSA_ALG_IS_RSA_PSS( alg ) )
3618 {
3619 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3620 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3621 mbedtls_ctr_drbg_random,
3622 &global_data.ctr_drbg,
3623 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003624 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003625 (unsigned int) hash_length,
3626 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003627 signature );
3628 }
3629 else
3630#endif /* MBEDTLS_PKCS1_V21 */
3631 {
3632 return( PSA_ERROR_INVALID_ARGUMENT );
3633 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003634
3635 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3636 * the rest of the signature is invalid". This has little use in
3637 * practice and PSA doesn't report this distinction. */
3638 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3639 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003640 return( mbedtls_to_psa_error( ret ) );
3641}
3642#endif /* MBEDTLS_RSA_C */
3643
John Durkopf87e3ae2020-10-26 15:25:23 -07003644#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003645/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3646 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3647 * (even though these functions don't modify it). */
3648static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3649 psa_algorithm_t alg,
3650 const uint8_t *hash,
3651 size_t hash_length,
3652 uint8_t *signature,
3653 size_t signature_size,
3654 size_t *signature_length )
3655{
Janos Follath24eed8d2019-11-22 13:21:35 +00003656 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003657 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003658 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003659 mbedtls_mpi_init( &r );
3660 mbedtls_mpi_init( &s );
3661
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003662 if( signature_size < 2 * curve_bytes )
3663 {
3664 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3665 goto cleanup;
3666 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003667
John Durkop0ea39e02020-10-13 19:58:20 -07003668#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003669 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3670 {
3671 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3672 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3673 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003674 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3675 &ecp->d, hash,
3676 hash_length, md_alg,
3677 mbedtls_ctr_drbg_random,
3678 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003679 }
3680 else
John Durkop0ea39e02020-10-13 19:58:20 -07003681#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003682 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003683 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003684 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3685 hash, hash_length,
3686 mbedtls_ctr_drbg_random,
3687 &global_data.ctr_drbg ) );
3688 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003689
3690 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3691 signature,
3692 curve_bytes ) );
3693 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3694 signature + curve_bytes,
3695 curve_bytes ) );
3696
3697cleanup:
3698 mbedtls_mpi_free( &r );
3699 mbedtls_mpi_free( &s );
3700 if( ret == 0 )
3701 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003702 return( mbedtls_to_psa_error( ret ) );
3703}
3704
3705static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3706 const uint8_t *hash,
3707 size_t hash_length,
3708 const uint8_t *signature,
3709 size_t signature_length )
3710{
Janos Follath24eed8d2019-11-22 13:21:35 +00003711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003712 mbedtls_mpi r, s;
3713 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3714 mbedtls_mpi_init( &r );
3715 mbedtls_mpi_init( &s );
3716
3717 if( signature_length != 2 * curve_bytes )
3718 return( PSA_ERROR_INVALID_SIGNATURE );
3719
3720 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3721 signature,
3722 curve_bytes ) );
3723 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3724 signature + curve_bytes,
3725 curve_bytes ) );
3726
Steven Cooremanacda8342020-07-24 23:09:52 +02003727 /* Check whether the public part is loaded. If not, load it. */
3728 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3729 {
3730 MBEDTLS_MPI_CHK(
3731 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3732 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3733 }
3734
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003735 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3736 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003737
3738cleanup:
3739 mbedtls_mpi_free( &r );
3740 mbedtls_mpi_free( &s );
3741 return( mbedtls_to_psa_error( ret ) );
3742}
John Durkopf87e3ae2020-10-26 15:25:23 -07003743#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003744
Ronald Croncf56a0a2020-08-04 09:51:30 +02003745psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003746 psa_algorithm_t alg,
3747 const uint8_t *hash,
3748 size_t hash_length,
3749 uint8_t *signature,
3750 size_t signature_size,
3751 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003752{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003753 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3754 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003755 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003756
3757 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003758 /* Immediately reject a zero-length signature buffer. This guarantees
3759 * that signature must be a valid pointer. (On the other hand, the hash
3760 * buffer can in principle be empty since it doesn't actually have
3761 * to be a hash.) */
3762 if( signature_size == 0 )
3763 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003764
Ronald Croncf56a0a2020-08-04 09:51:30 +02003765 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003766 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003767 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003768 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003769 {
3770 status = PSA_ERROR_INVALID_ARGUMENT;
3771 goto exit;
3772 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003773
Steven Cooremancd84cb42020-07-16 20:28:36 +02003774 /* Try any of the available accelerators first */
3775 status = psa_driver_wrapper_sign_hash( slot,
3776 alg,
3777 hash,
3778 hash_length,
3779 signature,
3780 signature_size,
3781 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003782 if( status != PSA_ERROR_NOT_SUPPORTED ||
3783 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003784 goto exit;
3785
Steven Cooreman7a250572020-07-17 16:43:05 +02003786 /* If the operation was not supported by any accelerator, try fallback. */
Gilles Peskine20035e32018-02-03 22:44:14 +01003787#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003788 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003789 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003790 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003791
Steven Cooreman4fed4552020-08-03 14:46:03 +02003792 status = psa_load_rsa_representation( slot->attr.type,
3793 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003794 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003795 &rsa );
3796 if( status != PSA_SUCCESS )
3797 goto exit;
3798
Steven Cooremana2371e52020-07-28 14:30:39 +02003799 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003800 alg,
3801 hash, hash_length,
3802 signature, signature_size,
3803 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003804
Steven Cooremana2371e52020-07-28 14:30:39 +02003805 mbedtls_rsa_free( rsa );
3806 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003807 }
3808 else
3809#endif /* defined(MBEDTLS_RSA_C) */
3810#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003811 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003812 {
John Durkop0ea39e02020-10-13 19:58:20 -07003813#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003814 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003815#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003816 PSA_ALG_IS_ECDSA( alg )
3817#else
3818 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3819#endif
3820 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003821 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003822 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003823 status = psa_load_ecp_representation( slot->attr.type,
3824 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003825 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003826 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003827 if( status != PSA_SUCCESS )
3828 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003829 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003830 alg,
3831 hash, hash_length,
3832 signature, signature_size,
3833 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003834 mbedtls_ecp_keypair_free( ecp );
3835 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003836 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003837 else
John Durkop0ea39e02020-10-13 19:58:20 -07003838#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003839 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003840 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003841 }
itayzafrir5c753392018-05-08 11:18:38 +03003842 }
3843 else
3844#endif /* defined(MBEDTLS_ECP_C) */
3845 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003846 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003847 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003848
3849exit:
3850 /* Fill the unused part of the output buffer (the whole buffer on error,
3851 * the trailing part on success) with something that isn't a valid mac
3852 * (barring an attack on the mac and deliberately-crafted input),
3853 * in case the caller doesn't check the return status properly. */
3854 if( status == PSA_SUCCESS )
3855 memset( signature + *signature_length, '!',
3856 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003857 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003858 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003859 /* If signature_size is 0 then we have nothing to do. We must not call
3860 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003861
3862 decrement_status = psa_decrement_key_slot_access_count( slot );
3863
3864 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003865}
3866
Ronald Croncf56a0a2020-08-04 09:51:30 +02003867psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003868 psa_algorithm_t alg,
3869 const uint8_t *hash,
3870 size_t hash_length,
3871 const uint8_t *signature,
3872 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003873{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003874 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3875 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003876 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003877
Ronald Croncf56a0a2020-08-04 09:51:30 +02003878 status = psa_get_key_from_slot( key, &slot,
3879 PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003880 if( status != PSA_SUCCESS )
3881 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003882
Steven Cooreman55ae2172020-07-17 19:46:15 +02003883 /* Try any of the available accelerators first */
3884 status = psa_driver_wrapper_verify_hash( slot,
3885 alg,
3886 hash,
3887 hash_length,
3888 signature,
3889 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003890 if( status != PSA_ERROR_NOT_SUPPORTED ||
3891 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003892 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003893
Gilles Peskine61b91d42018-06-08 16:09:36 +02003894#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003895 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003896 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003897 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003898
Steven Cooreman4fed4552020-08-03 14:46:03 +02003899 status = psa_load_rsa_representation( slot->attr.type,
3900 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003901 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003902 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003903 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003904 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003905
Steven Cooremana2371e52020-07-28 14:30:39 +02003906 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003907 alg,
3908 hash, hash_length,
3909 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003910 mbedtls_rsa_free( rsa );
3911 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003912 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003913 }
3914 else
3915#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003916#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003917 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003918 {
John Durkopf87e3ae2020-10-26 15:25:23 -07003919#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003920 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003921 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003922 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003923 status = psa_load_ecp_representation( slot->attr.type,
3924 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003925 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003926 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003927 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003928 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003929 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003930 hash, hash_length,
3931 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003932 mbedtls_ecp_keypair_free( ecp );
3933 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003934 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003935 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003936 else
John Durkopf87e3ae2020-10-26 15:25:23 -07003937#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003938 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003939 status = PSA_ERROR_INVALID_ARGUMENT;
3940 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003941 }
itayzafrir5c753392018-05-08 11:18:38 +03003942 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003943 else
3944#endif /* defined(MBEDTLS_ECP_C) */
3945 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003946 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003947 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003948
3949exit:
3950 decrement_status = psa_decrement_key_slot_access_count( slot );
3951
3952 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003953}
3954
Gilles Peskine072ac562018-06-30 00:21:29 +02003955#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3956static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3957 mbedtls_rsa_context *rsa )
3958{
3959 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3960 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3961 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3962 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3963}
3964#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3965
Ronald Croncf56a0a2020-08-04 09:51:30 +02003966psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003967 psa_algorithm_t alg,
3968 const uint8_t *input,
3969 size_t input_length,
3970 const uint8_t *salt,
3971 size_t salt_length,
3972 uint8_t *output,
3973 size_t output_size,
3974 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003975{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003976 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3977 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003978 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003979
Darryl Green5cc689a2018-07-24 15:34:10 +01003980 (void) input;
3981 (void) input_length;
3982 (void) salt;
3983 (void) output;
3984 (void) output_size;
3985
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003986 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003987
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003988 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3989 return( PSA_ERROR_INVALID_ARGUMENT );
3990
Ronald Croncf56a0a2020-08-04 09:51:30 +02003991 status = psa_get_transparent_key( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003992 if( status != PSA_SUCCESS )
3993 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003994 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3995 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003996 {
3997 status = PSA_ERROR_INVALID_ARGUMENT;
3998 goto exit;
3999 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004000
4001#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004002 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004003 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004004 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004005 status = psa_load_rsa_representation( slot->attr.type,
4006 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004007 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004008 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004009 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004010 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004011
4012 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004013 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004014 status = PSA_ERROR_BUFFER_TOO_SMALL;
4015 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004016 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004017#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004018 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004019 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004020 status = mbedtls_to_psa_error(
4021 mbedtls_rsa_pkcs1_encrypt( rsa,
4022 mbedtls_ctr_drbg_random,
4023 &global_data.ctr_drbg,
4024 MBEDTLS_RSA_PUBLIC,
4025 input_length,
4026 input,
4027 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004028 }
4029 else
4030#endif /* MBEDTLS_PKCS1_V15 */
4031#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004032 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004033 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004034 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004035 status = mbedtls_to_psa_error(
4036 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
4037 mbedtls_ctr_drbg_random,
4038 &global_data.ctr_drbg,
4039 MBEDTLS_RSA_PUBLIC,
4040 salt, salt_length,
4041 input_length,
4042 input,
4043 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004044 }
4045 else
4046#endif /* MBEDTLS_PKCS1_V21 */
4047 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004048 status = PSA_ERROR_INVALID_ARGUMENT;
4049 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004050 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004051rsa_exit:
4052 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004053 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004054
Steven Cooremana2371e52020-07-28 14:30:39 +02004055 mbedtls_rsa_free( rsa );
4056 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004057 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004058 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004059#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004060 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004061 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004062 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004063
4064exit:
4065 decrement_status = psa_decrement_key_slot_access_count( slot );
4066
4067 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004068}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004069
Ronald Croncf56a0a2020-08-04 09:51:30 +02004070psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004071 psa_algorithm_t alg,
4072 const uint8_t *input,
4073 size_t input_length,
4074 const uint8_t *salt,
4075 size_t salt_length,
4076 uint8_t *output,
4077 size_t output_size,
4078 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004079{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004080 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4081 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004082 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004083
Darryl Green5cc689a2018-07-24 15:34:10 +01004084 (void) input;
4085 (void) input_length;
4086 (void) salt;
4087 (void) output;
4088 (void) output_size;
4089
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004090 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004091
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004092 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4093 return( PSA_ERROR_INVALID_ARGUMENT );
4094
Ronald Croncf56a0a2020-08-04 09:51:30 +02004095 status = psa_get_transparent_key( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004096 if( status != PSA_SUCCESS )
4097 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004098 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004099 {
4100 status = PSA_ERROR_INVALID_ARGUMENT;
4101 goto exit;
4102 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004103
4104#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004105 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004106 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004107 mbedtls_rsa_context *rsa = NULL;
4108 status = psa_load_rsa_representation( slot->attr.type,
4109 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004110 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004111 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004112 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004113 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004114
Steven Cooremana2371e52020-07-28 14:30:39 +02004115 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004116 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004117 status = PSA_ERROR_INVALID_ARGUMENT;
4118 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004119 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004120
4121#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004122 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004123 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004124 status = mbedtls_to_psa_error(
4125 mbedtls_rsa_pkcs1_decrypt( rsa,
4126 mbedtls_ctr_drbg_random,
4127 &global_data.ctr_drbg,
4128 MBEDTLS_RSA_PRIVATE,
4129 output_length,
4130 input,
4131 output,
4132 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004133 }
4134 else
4135#endif /* MBEDTLS_PKCS1_V15 */
4136#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004137 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004138 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004139 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004140 status = mbedtls_to_psa_error(
4141 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4142 mbedtls_ctr_drbg_random,
4143 &global_data.ctr_drbg,
4144 MBEDTLS_RSA_PRIVATE,
4145 salt, salt_length,
4146 output_length,
4147 input,
4148 output,
4149 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004150 }
4151 else
4152#endif /* MBEDTLS_PKCS1_V21 */
4153 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004154 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004155 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004156
Steven Cooreman4fed4552020-08-03 14:46:03 +02004157rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004158 mbedtls_rsa_free( rsa );
4159 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004160 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004161 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004162#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004163 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004164 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004165 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004166
4167exit:
4168 decrement_status = psa_decrement_key_slot_access_count( slot );
4169
4170 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004171}
Gilles Peskine20035e32018-02-03 22:44:14 +01004172
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004173
4174
mohammad1603503973b2018-03-12 15:59:30 +02004175/****************************************************************/
4176/* Symmetric cryptography */
4177/****************************************************************/
4178
Gilles Peskinee553c652018-06-04 16:22:46 +02004179static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004180 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004181 psa_algorithm_t alg,
4182 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004183{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004184 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4185 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004186 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004187 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004188 size_t key_bits;
4189 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004190 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4191 PSA_KEY_USAGE_ENCRYPT :
4192 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004193
Steven Cooremand3feccd2020-09-01 15:56:14 +02004194 /* A context must be freshly initialized before it can be set up. */
4195 if( operation->alg != 0 )
4196 return( PSA_ERROR_BAD_STATE );
4197
Steven Cooremana07b9972020-09-10 14:54:14 +02004198 /* The requested algorithm must be one that can be processed by cipher. */
4199 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004200 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004201
Steven Cooremanef8575e2020-09-11 11:44:50 +02004202 /* Fetch key material from key storage. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02004203 status = psa_get_key_from_slot( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004204 if( status != PSA_SUCCESS )
4205 goto exit;
4206
4207 /* Initialize the operation struct members, except for alg. The alg member
4208 * is used to indicate to psa_cipher_abort that there are resources to free,
4209 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004210 operation->key_set = 0;
4211 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004212 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004213 operation->iv_size = 0;
4214 operation->block_size = 0;
4215 if( alg == PSA_ALG_ECB_NO_PADDING )
4216 operation->iv_required = 0;
4217 else
4218 operation->iv_required = 1;
4219
Steven Cooremana07b9972020-09-10 14:54:14 +02004220 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004221 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004222 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004223 slot,
4224 alg );
4225 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004226 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004227 slot,
4228 alg );
4229
Steven Cooremanef8575e2020-09-11 11:44:50 +02004230 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004231 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004232 /* Once the driver context is initialised, it needs to be freed using
4233 * psa_cipher_abort. Indicate this through setting alg. */
4234 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004235 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004236
Steven Cooremand3feccd2020-09-01 15:56:14 +02004237 if( status != PSA_ERROR_NOT_SUPPORTED ||
4238 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4239 goto exit;
4240
Steven Cooremana07b9972020-09-10 14:54:14 +02004241 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004242 * available for the given algorithm & key. */
4243 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004244
Steven Cooremana07b9972020-09-10 14:54:14 +02004245 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004246 * psa_cipher_abort. Indicate there is something to be freed through setting
4247 * alg, and indicate the operation is being done using mbedtls crypto through
4248 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004249 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004250 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004251
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004252 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004253 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004254 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004255 {
4256 status = PSA_ERROR_NOT_SUPPORTED;
4257 goto exit;
4258 }
mohammad1603503973b2018-03-12 15:59:30 +02004259
mohammad1603503973b2018-03-12 15:59:30 +02004260 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004261 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004262 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004263
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004264#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004265 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004266 {
4267 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004268 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004269 memcpy( keys, slot->data.key.data, 16 );
4270 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004271 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4272 keys,
4273 192, cipher_operation );
4274 }
4275 else
4276#endif
4277 {
4278 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004279 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004280 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004281 }
Moran Peker41deec42018-04-04 15:43:05 +03004282 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004283 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004284
mohammad16038481e742018-03-18 13:57:31 +02004285#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004286 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004287 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004288 case PSA_ALG_CBC_NO_PADDING:
4289 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4290 MBEDTLS_PADDING_NONE );
4291 break;
4292 case PSA_ALG_CBC_PKCS7:
4293 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4294 MBEDTLS_PADDING_PKCS7 );
4295 break;
4296 default:
4297 /* The algorithm doesn't involve padding. */
4298 ret = 0;
4299 break;
4300 }
4301 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004302 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004303#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4304
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004305 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004306 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004307 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4308 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004309 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004310 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004311 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004312#if defined(MBEDTLS_CHACHA20_C)
4313 else
4314 if( alg == PSA_ALG_CHACHA20 )
4315 operation->iv_size = 12;
4316#endif
mohammad1603503973b2018-03-12 15:59:30 +02004317
Steven Cooreman7df02922020-09-09 15:28:49 +02004318 status = PSA_SUCCESS;
4319
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004320exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004321 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004322 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004323 if( status == PSA_SUCCESS )
4324 {
4325 /* Update operation flags for both driver and software implementations */
4326 operation->key_set = 1;
4327 }
4328 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004329 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004330
4331 decrement_status = psa_decrement_key_slot_access_count( slot );
4332
4333 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004334}
4335
Gilles Peskinefe119512018-07-08 21:39:34 +02004336psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004337 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004338 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004339{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004340 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004341}
4342
Gilles Peskinefe119512018-07-08 21:39:34 +02004343psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004344 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004345 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004346{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004347 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004348}
4349
Gilles Peskinefe119512018-07-08 21:39:34 +02004350psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004351 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004352 size_t iv_size,
4353 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004354{
itayzafrir534bd7c2018-08-02 13:56:32 +03004355 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004356 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004357 if( operation->iv_set || ! operation->iv_required )
4358 {
4359 return( PSA_ERROR_BAD_STATE );
4360 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004361
Steven Cooremanef8575e2020-09-11 11:44:50 +02004362 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004363 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004364 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004365 iv,
4366 iv_size,
4367 iv_length );
4368 goto exit;
4369 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004370
Moran Peker41deec42018-04-04 15:43:05 +03004371 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004372 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004373 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004374 goto exit;
4375 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004376 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4377 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004378 if( ret != 0 )
4379 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004380 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004381 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004382 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004383
mohammad16038481e742018-03-18 13:57:31 +02004384 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004385 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004386
Moran Peker395db872018-05-31 14:07:14 +03004387exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004388 if( status == PSA_SUCCESS )
4389 operation->iv_set = 1;
4390 else
Moran Peker395db872018-05-31 14:07:14 +03004391 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004392 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004393}
4394
Gilles Peskinefe119512018-07-08 21:39:34 +02004395psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004396 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004397 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004398{
itayzafrir534bd7c2018-08-02 13:56:32 +03004399 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004401 if( operation->iv_set || ! operation->iv_required )
4402 {
4403 return( PSA_ERROR_BAD_STATE );
4404 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004405
Steven Cooremanef8575e2020-09-11 11:44:50 +02004406 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004407 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004408 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004409 iv,
4410 iv_length );
4411 goto exit;
4412 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004413
Moran Pekera28258c2018-05-29 16:25:04 +03004414 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004415 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004416 status = PSA_ERROR_INVALID_ARGUMENT;
4417 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004418 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004419 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4420 status = mbedtls_to_psa_error( ret );
4421exit:
4422 if( status == PSA_SUCCESS )
4423 operation->iv_set = 1;
4424 else
mohammad1603503973b2018-03-12 15:59:30 +02004425 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004426 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004427}
4428
Steven Cooreman177deba2020-09-07 17:14:14 +02004429/* Process input for which the algorithm is set to ECB mode. This requires
4430 * manual processing, since the PSA API is defined as being able to process
4431 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4432 * underlying mbedtls_cipher_update only takes full blocks. */
4433static psa_status_t psa_cipher_update_ecb_internal(
4434 mbedtls_cipher_context_t *ctx,
4435 const uint8_t *input,
4436 size_t input_length,
4437 uint8_t *output,
4438 size_t output_size,
4439 size_t *output_length )
4440{
4441 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4442 size_t block_size = ctx->cipher_info->block_size;
4443 size_t internal_output_length = 0;
4444 *output_length = 0;
4445
4446 if( input_length == 0 )
4447 {
4448 status = PSA_SUCCESS;
4449 goto exit;
4450 }
4451
4452 if( ctx->unprocessed_len > 0 )
4453 {
4454 /* Fill up to block size, and run the block if there's a full one. */
4455 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4456
4457 if( input_length < bytes_to_copy )
4458 bytes_to_copy = input_length;
4459
4460 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4461 input, bytes_to_copy );
4462 input_length -= bytes_to_copy;
4463 input += bytes_to_copy;
4464 ctx->unprocessed_len += bytes_to_copy;
4465
4466 if( ctx->unprocessed_len == block_size )
4467 {
4468 status = mbedtls_to_psa_error(
4469 mbedtls_cipher_update( ctx,
4470 ctx->unprocessed_data,
4471 block_size,
4472 output, &internal_output_length ) );
4473
4474 if( status != PSA_SUCCESS )
4475 goto exit;
4476
4477 output += internal_output_length;
4478 output_size -= internal_output_length;
4479 *output_length += internal_output_length;
4480 ctx->unprocessed_len = 0;
4481 }
4482 }
4483
4484 while( input_length >= block_size )
4485 {
4486 /* Run all full blocks we have, one by one */
4487 status = mbedtls_to_psa_error(
4488 mbedtls_cipher_update( ctx, input,
4489 block_size,
4490 output, &internal_output_length ) );
4491
4492 if( status != PSA_SUCCESS )
4493 goto exit;
4494
4495 input_length -= block_size;
4496 input += block_size;
4497
4498 output += internal_output_length;
4499 output_size -= internal_output_length;
4500 *output_length += internal_output_length;
4501 }
4502
4503 if( input_length > 0 )
4504 {
4505 /* Save unprocessed bytes for later processing */
4506 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4507 input, input_length );
4508 ctx->unprocessed_len += input_length;
4509 }
4510
4511 status = PSA_SUCCESS;
4512
4513exit:
4514 return( status );
4515}
4516
Gilles Peskinee553c652018-06-04 16:22:46 +02004517psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4518 const uint8_t *input,
4519 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004520 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004521 size_t output_size,
4522 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004523{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004524 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004525 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004526 if( operation->alg == 0 )
4527 {
4528 return( PSA_ERROR_BAD_STATE );
4529 }
4530 if( operation->iv_required && ! operation->iv_set )
4531 {
4532 return( PSA_ERROR_BAD_STATE );
4533 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004534
Steven Cooremanef8575e2020-09-11 11:44:50 +02004535 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004536 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004537 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004538 input,
4539 input_length,
4540 output,
4541 output_size,
4542 output_length );
4543 goto exit;
4544 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004545
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004546 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004547 {
4548 /* Take the unprocessed partial block left over from previous
4549 * update calls, if any, plus the input to this call. Remove
4550 * the last partial block, if any. You get the data that will be
4551 * output in this call. */
4552 expected_output_size =
4553 ( operation->ctx.cipher.unprocessed_len + input_length )
4554 / operation->block_size * operation->block_size;
4555 }
4556 else
4557 {
4558 expected_output_size = input_length;
4559 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004560
Gilles Peskine89d789c2018-06-04 17:17:16 +02004561 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004562 {
4563 status = PSA_ERROR_BUFFER_TOO_SMALL;
4564 goto exit;
4565 }
mohammad160382759612018-03-12 18:16:40 +02004566
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004567 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4568 {
4569 /* mbedtls_cipher_update has an API inconsistency: it will only
4570 * process a single block at a time in ECB mode. Abstract away that
4571 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004572 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4573 input,
4574 input_length,
4575 output,
4576 output_size,
4577 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004578 }
4579 else
4580 {
4581 status = mbedtls_to_psa_error(
4582 mbedtls_cipher_update( &operation->ctx.cipher, input,
4583 input_length, output, output_length ) );
4584 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004585exit:
4586 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004587 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004588 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004589}
4590
Gilles Peskinee553c652018-06-04 16:22:46 +02004591psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4592 uint8_t *output,
4593 size_t output_size,
4594 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004595{
David Saadab4ecc272019-02-14 13:48:10 +02004596 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004597 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004598 if( operation->alg == 0 )
4599 {
4600 return( PSA_ERROR_BAD_STATE );
4601 }
4602 if( operation->iv_required && ! operation->iv_set )
4603 {
4604 return( PSA_ERROR_BAD_STATE );
4605 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004606
Steven Cooremanef8575e2020-09-11 11:44:50 +02004607 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004608 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004609 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004610 output,
4611 output_size,
4612 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004613 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004614 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004615
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004616 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004617 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004618 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004619 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004620 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004621 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004622 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004623 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004624 }
4625
Steven Cooremanef8575e2020-09-11 11:44:50 +02004626 status = mbedtls_to_psa_error(
4627 mbedtls_cipher_finish( &operation->ctx.cipher,
4628 temp_output_buffer,
4629 output_length ) );
4630 if( status != PSA_SUCCESS )
4631 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004632
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004633 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004634 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004635 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004636 memcpy( output, temp_output_buffer, *output_length );
4637 else
Janos Follath315b51c2018-07-09 16:04:51 +01004638 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004639
Steven Cooremanef8575e2020-09-11 11:44:50 +02004640exit:
4641 if( operation->mbedtls_in_use == 1 )
4642 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004643
Steven Cooremanef8575e2020-09-11 11:44:50 +02004644 if( status == PSA_SUCCESS )
4645 return( psa_cipher_abort( operation ) );
4646 else
4647 {
4648 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004649 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004650
Steven Cooremanef8575e2020-09-11 11:44:50 +02004651 return( status );
4652 }
mohammad1603503973b2018-03-12 15:59:30 +02004653}
4654
Gilles Peskinee553c652018-06-04 16:22:46 +02004655psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4656{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004657 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004658 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004659 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004660 * in use. It's ok to call abort on such an object, and there's
4661 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004662 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004663 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004664
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004665 /* Sanity check (shouldn't happen: operation->alg should
4666 * always have been initialized to a valid value). */
4667 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4668 return( PSA_ERROR_BAD_STATE );
4669
Steven Cooremanef8575e2020-09-11 11:44:50 +02004670 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004671 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004672 else
4673 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004674
Moran Peker41deec42018-04-04 15:43:05 +03004675 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004676 operation->key_set = 0;
4677 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004678 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004679 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004680 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004681 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004682
Moran Peker395db872018-05-31 14:07:14 +03004683 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004684}
4685
Gilles Peskinea0655c32018-04-30 17:06:50 +02004686
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004687
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004688
mohammad16035955c982018-04-26 00:53:03 +03004689/****************************************************************/
4690/* AEAD */
4691/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004692
Gilles Peskineedf9a652018-08-17 18:11:56 +02004693typedef struct
4694{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004695 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004696 const mbedtls_cipher_info_t *cipher_info;
4697 union
4698 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004699 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004700#if defined(MBEDTLS_CCM_C)
4701 mbedtls_ccm_context ccm;
4702#endif /* MBEDTLS_CCM_C */
4703#if defined(MBEDTLS_GCM_C)
4704 mbedtls_gcm_context gcm;
4705#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004706#if defined(MBEDTLS_CHACHAPOLY_C)
4707 mbedtls_chachapoly_context chachapoly;
4708#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004709 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004710 psa_algorithm_t core_alg;
4711 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004712 uint8_t tag_length;
4713} aead_operation_t;
4714
Ronald Cronf95a2b12020-10-22 15:24:49 +02004715#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4716
Gilles Peskine30a9e412019-01-14 18:36:12 +01004717static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004718{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004719 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004720 {
4721#if defined(MBEDTLS_CCM_C)
4722 case PSA_ALG_CCM:
4723 mbedtls_ccm_free( &operation->ctx.ccm );
4724 break;
4725#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004726#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004727 case PSA_ALG_GCM:
4728 mbedtls_gcm_free( &operation->ctx.gcm );
4729 break;
4730#endif /* MBEDTLS_GCM_C */
4731 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004732
4733 psa_decrement_key_slot_access_count( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004734}
4735
4736static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004737 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004738 psa_key_usage_t usage,
4739 psa_algorithm_t alg )
4740{
4741 psa_status_t status;
4742 size_t key_bits;
4743 mbedtls_cipher_id_t cipher_id;
4744
Ronald Croncf56a0a2020-08-04 09:51:30 +02004745 status = psa_get_transparent_key( key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004746 if( status != PSA_SUCCESS )
4747 return( status );
4748
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004749 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004750
4751 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004752 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004753 &cipher_id );
4754 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004755 {
4756 status = PSA_ERROR_NOT_SUPPORTED;
4757 goto cleanup;
4758 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004759
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004760 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004761 {
4762#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004763 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4764 operation->core_alg = PSA_ALG_CCM;
4765 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004766 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4767 * The call to mbedtls_ccm_encrypt_and_tag or
4768 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004769 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004770 {
4771 status = PSA_ERROR_INVALID_ARGUMENT;
4772 goto cleanup;
4773 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004774 mbedtls_ccm_init( &operation->ctx.ccm );
4775 status = mbedtls_to_psa_error(
4776 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004777 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004778 (unsigned int) key_bits ) );
4779 if( status != 0 )
4780 goto cleanup;
4781 break;
4782#endif /* MBEDTLS_CCM_C */
4783
4784#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004785 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4786 operation->core_alg = PSA_ALG_GCM;
4787 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004788 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4789 * The call to mbedtls_gcm_crypt_and_tag or
4790 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004791 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004792 {
4793 status = PSA_ERROR_INVALID_ARGUMENT;
4794 goto cleanup;
4795 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004796 mbedtls_gcm_init( &operation->ctx.gcm );
4797 status = mbedtls_to_psa_error(
4798 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004799 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004800 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004801 if( status != 0 )
4802 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004803 break;
4804#endif /* MBEDTLS_GCM_C */
4805
Gilles Peskine26869f22019-05-06 15:25:00 +02004806#if defined(MBEDTLS_CHACHAPOLY_C)
4807 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4808 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4809 operation->full_tag_length = 16;
4810 /* We only support the default tag length. */
4811 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004812 {
4813 status = PSA_ERROR_NOT_SUPPORTED;
4814 goto cleanup;
4815 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004816 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4817 status = mbedtls_to_psa_error(
4818 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004819 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004820 if( status != 0 )
4821 goto cleanup;
4822 break;
4823#endif /* MBEDTLS_CHACHAPOLY_C */
4824
Gilles Peskineedf9a652018-08-17 18:11:56 +02004825 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004826 status = PSA_ERROR_NOT_SUPPORTED;
4827 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004828 }
4829
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004830 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4831 {
4832 status = PSA_ERROR_INVALID_ARGUMENT;
4833 goto cleanup;
4834 }
4835 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004836
Gilles Peskineedf9a652018-08-17 18:11:56 +02004837 return( PSA_SUCCESS );
4838
4839cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004840 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004841 return( status );
4842}
4843
Ronald Croncf56a0a2020-08-04 09:51:30 +02004844psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004845 psa_algorithm_t alg,
4846 const uint8_t *nonce,
4847 size_t nonce_length,
4848 const uint8_t *additional_data,
4849 size_t additional_data_length,
4850 const uint8_t *plaintext,
4851 size_t plaintext_length,
4852 uint8_t *ciphertext,
4853 size_t ciphertext_size,
4854 size_t *ciphertext_length )
4855{
mohammad16035955c982018-04-26 00:53:03 +03004856 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004857 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004858 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004859
mohammad1603f08a5502018-06-03 15:05:47 +03004860 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004861
Ronald Croncf56a0a2020-08-04 09:51:30 +02004862 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004863 if( status != PSA_SUCCESS )
4864 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004865
Gilles Peskineedf9a652018-08-17 18:11:56 +02004866 /* For all currently supported modes, the tag is at the end of the
4867 * ciphertext. */
4868 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4869 {
4870 status = PSA_ERROR_BUFFER_TOO_SMALL;
4871 goto exit;
4872 }
4873 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004874
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004875#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004876 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004877 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004878 status = mbedtls_to_psa_error(
4879 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4880 MBEDTLS_GCM_ENCRYPT,
4881 plaintext_length,
4882 nonce, nonce_length,
4883 additional_data, additional_data_length,
4884 plaintext, ciphertext,
4885 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004886 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004887 else
4888#endif /* MBEDTLS_GCM_C */
4889#if defined(MBEDTLS_CCM_C)
4890 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004891 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004892 status = mbedtls_to_psa_error(
4893 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4894 plaintext_length,
4895 nonce, nonce_length,
4896 additional_data,
4897 additional_data_length,
4898 plaintext, ciphertext,
4899 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004900 }
mohammad16035c8845f2018-05-09 05:40:09 -07004901 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004902#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004903#if defined(MBEDTLS_CHACHAPOLY_C)
4904 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4905 {
4906 if( nonce_length != 12 || operation.tag_length != 16 )
4907 {
4908 status = PSA_ERROR_NOT_SUPPORTED;
4909 goto exit;
4910 }
4911 status = mbedtls_to_psa_error(
4912 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4913 plaintext_length,
4914 nonce,
4915 additional_data,
4916 additional_data_length,
4917 plaintext,
4918 ciphertext,
4919 tag ) );
4920 }
4921 else
4922#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004923 {
mohammad1603554faad2018-06-03 15:07:38 +03004924 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004925 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004926
Gilles Peskineedf9a652018-08-17 18:11:56 +02004927 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4928 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004929
Gilles Peskineedf9a652018-08-17 18:11:56 +02004930exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004931 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004932 if( status == PSA_SUCCESS )
4933 *ciphertext_length = plaintext_length + operation.tag_length;
4934 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004935}
4936
Gilles Peskineee652a32018-06-01 19:23:52 +02004937/* Locate the tag in a ciphertext buffer containing the encrypted data
4938 * followed by the tag. Return the length of the part preceding the tag in
4939 * *plaintext_length. This is the size of the plaintext in modes where
4940 * the encrypted data has the same size as the plaintext, such as
4941 * CCM and GCM. */
4942static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4943 const uint8_t *ciphertext,
4944 size_t ciphertext_length,
4945 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004946 const uint8_t **p_tag )
4947{
4948 size_t payload_length;
4949 if( tag_length > ciphertext_length )
4950 return( PSA_ERROR_INVALID_ARGUMENT );
4951 payload_length = ciphertext_length - tag_length;
4952 if( payload_length > plaintext_size )
4953 return( PSA_ERROR_BUFFER_TOO_SMALL );
4954 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004955 return( PSA_SUCCESS );
4956}
4957
Ronald Croncf56a0a2020-08-04 09:51:30 +02004958psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004959 psa_algorithm_t alg,
4960 const uint8_t *nonce,
4961 size_t nonce_length,
4962 const uint8_t *additional_data,
4963 size_t additional_data_length,
4964 const uint8_t *ciphertext,
4965 size_t ciphertext_length,
4966 uint8_t *plaintext,
4967 size_t plaintext_size,
4968 size_t *plaintext_length )
4969{
mohammad16035955c982018-04-26 00:53:03 +03004970 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004971 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004972 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004973
Gilles Peskineee652a32018-06-01 19:23:52 +02004974 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004975
Ronald Croncf56a0a2020-08-04 09:51:30 +02004976 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004977 if( status != PSA_SUCCESS )
4978 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004979
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004980 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4981 ciphertext, ciphertext_length,
4982 plaintext_size, &tag );
4983 if( status != PSA_SUCCESS )
4984 goto exit;
4985
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004986#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004987 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004988 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004989 status = mbedtls_to_psa_error(
4990 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4991 ciphertext_length - operation.tag_length,
4992 nonce, nonce_length,
4993 additional_data,
4994 additional_data_length,
4995 tag, operation.tag_length,
4996 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004997 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004998 else
4999#endif /* MBEDTLS_GCM_C */
5000#if defined(MBEDTLS_CCM_C)
5001 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005002 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005003 status = mbedtls_to_psa_error(
5004 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5005 ciphertext_length - operation.tag_length,
5006 nonce, nonce_length,
5007 additional_data,
5008 additional_data_length,
5009 ciphertext, plaintext,
5010 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005011 }
mohammad160339574652018-06-01 04:39:53 -07005012 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005013#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005014#if defined(MBEDTLS_CHACHAPOLY_C)
5015 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5016 {
5017 if( nonce_length != 12 || operation.tag_length != 16 )
5018 {
5019 status = PSA_ERROR_NOT_SUPPORTED;
5020 goto exit;
5021 }
5022 status = mbedtls_to_psa_error(
5023 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5024 ciphertext_length - operation.tag_length,
5025 nonce,
5026 additional_data,
5027 additional_data_length,
5028 tag,
5029 ciphertext,
5030 plaintext ) );
5031 }
5032 else
5033#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005034 {
mohammad1603554faad2018-06-03 15:07:38 +03005035 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005036 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005037
Gilles Peskineedf9a652018-08-17 18:11:56 +02005038 if( status != PSA_SUCCESS && plaintext_size != 0 )
5039 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005040
Gilles Peskineedf9a652018-08-17 18:11:56 +02005041exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005042 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005043 if( status == PSA_SUCCESS )
5044 *plaintext_length = ciphertext_length - operation.tag_length;
5045 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005046}
5047
Gilles Peskinea0655c32018-04-30 17:06:50 +02005048
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005049
Gilles Peskine20035e32018-02-03 22:44:14 +01005050/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005051/* Generators */
5052/****************************************************************/
5053
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005054#define HKDF_STATE_INIT 0 /* no input yet */
5055#define HKDF_STATE_STARTED 1 /* got salt */
5056#define HKDF_STATE_KEYED 2 /* got key */
5057#define HKDF_STATE_OUTPUT 3 /* output started */
5058
Gilles Peskinecbe66502019-05-16 16:59:18 +02005059static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005060 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005061{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005062 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5063 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005064 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005065 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005066}
5067
5068
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005069psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005070{
5071 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005072 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005073 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005074 {
5075 /* The object has (apparently) been initialized but it is not
5076 * in use. It's ok to call abort on such an object, and there's
5077 * nothing to do. */
5078 }
5079 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02005080#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005081 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005082 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005083 mbedtls_free( operation->ctx.hkdf.info );
5084 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005085 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005086 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005087 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005088 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005089 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005090 if( operation->ctx.tls12_prf.seed != NULL )
5091 {
5092 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5093 operation->ctx.tls12_prf.seed_length );
5094 mbedtls_free( operation->ctx.tls12_prf.seed );
5095 }
5096
5097 if( operation->ctx.tls12_prf.label != NULL )
5098 {
5099 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5100 operation->ctx.tls12_prf.label_length );
5101 mbedtls_free( operation->ctx.tls12_prf.label );
5102 }
5103
5104 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5105
5106 /* We leave the fields Ai and output_block to be erased safely by the
5107 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005108 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005109 else
5110#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005111 {
5112 status = PSA_ERROR_BAD_STATE;
5113 }
Janos Follath083036a2019-06-11 10:22:26 +01005114 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005115 return( status );
5116}
5117
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005118psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005119 size_t *capacity)
5120{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005122 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005123 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005124 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005125 }
5126
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005127 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005128 return( PSA_SUCCESS );
5129}
5130
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005131psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005132 size_t capacity )
5133{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005134 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005135 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005136 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005137 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005138 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005139 return( PSA_SUCCESS );
5140}
5141
Gilles Peskinebef7f142018-07-12 17:22:21 +02005142#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005143/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005144 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005145static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005146 psa_algorithm_t hash_alg,
5147 uint8_t *output,
5148 size_t output_length )
5149{
5150 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5151 psa_status_t status;
5152
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005153 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5154 return( PSA_ERROR_BAD_STATE );
5155 hkdf->state = HKDF_STATE_OUTPUT;
5156
Gilles Peskinebef7f142018-07-12 17:22:21 +02005157 while( output_length != 0 )
5158 {
5159 /* Copy what remains of the current block */
5160 uint8_t n = hash_length - hkdf->offset_in_block;
5161 if( n > output_length )
5162 n = (uint8_t) output_length;
5163 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5164 output += n;
5165 output_length -= n;
5166 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005167 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005168 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005169 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005170 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005171 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005172 * object was corrupted or if this function is called directly
5173 * inside the library. */
5174 if( hkdf->block_number == 0xff )
5175 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005176
5177 /* We need a new block */
5178 ++hkdf->block_number;
5179 hkdf->offset_in_block = 0;
5180 status = psa_hmac_setup_internal( &hkdf->hmac,
5181 hkdf->prk, hash_length,
5182 hash_alg );
5183 if( status != PSA_SUCCESS )
5184 return( status );
5185 if( hkdf->block_number != 1 )
5186 {
5187 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5188 hkdf->output_block,
5189 hash_length );
5190 if( status != PSA_SUCCESS )
5191 return( status );
5192 }
5193 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5194 hkdf->info,
5195 hkdf->info_length );
5196 if( status != PSA_SUCCESS )
5197 return( status );
5198 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5199 &hkdf->block_number, 1 );
5200 if( status != PSA_SUCCESS )
5201 return( status );
5202 status = psa_hmac_finish_internal( &hkdf->hmac,
5203 hkdf->output_block,
5204 sizeof( hkdf->output_block ) );
5205 if( status != PSA_SUCCESS )
5206 return( status );
5207 }
5208
5209 return( PSA_SUCCESS );
5210}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005211
Janos Follath7742fee2019-06-17 12:58:10 +01005212static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5213 psa_tls12_prf_key_derivation_t *tls12_prf,
5214 psa_algorithm_t alg )
5215{
5216 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5217 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005218 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5219 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005220
Janos Follath7742fee2019-06-17 12:58:10 +01005221 /* We can't be wanting more output after block 0xff, otherwise
5222 * the capacity check in psa_key_derivation_output_bytes() would have
5223 * prevented this call. It could happen only if the operation
5224 * object was corrupted or if this function is called directly
5225 * inside the library. */
5226 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005227 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005228
5229 /* We need a new block */
5230 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005231 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005232
5233 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5234 *
5235 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5236 *
5237 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5238 * HMAC_hash(secret, A(2) + seed) +
5239 * HMAC_hash(secret, A(3) + seed) + ...
5240 *
5241 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005242 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005243 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005244 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005245 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005246 * is currently extracted as `output_block` and where i is
5247 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005248 */
5249
Janos Follathea29bfb2019-06-19 12:21:20 +01005250 /* Save the hash context before using it, to preserve the hash state with
5251 * only the inner padding in it. We need this, because inner padding depends
5252 * on the key (secret in the RFC's terminology). */
5253 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5254 if( status != PSA_SUCCESS )
5255 goto cleanup;
5256
5257 /* Calculate A(i) where i = tls12_prf->block_number. */
5258 if( tls12_prf->block_number == 1 )
5259 {
5260 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5261 * the variable seed and in this instance means it in the context of the
5262 * P_hash function, where seed = label + seed.) */
5263 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5264 tls12_prf->label, tls12_prf->label_length );
5265 if( status != PSA_SUCCESS )
5266 goto cleanup;
5267 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5268 tls12_prf->seed, tls12_prf->seed_length );
5269 if( status != PSA_SUCCESS )
5270 goto cleanup;
5271 }
5272 else
5273 {
5274 /* A(i) = HMAC_hash(secret, A(i-1)) */
5275 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5276 tls12_prf->Ai, hash_length );
5277 if( status != PSA_SUCCESS )
5278 goto cleanup;
5279 }
5280
5281 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5282 tls12_prf->Ai, hash_length );
5283 if( status != PSA_SUCCESS )
5284 goto cleanup;
5285 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5286 if( status != PSA_SUCCESS )
5287 goto cleanup;
5288
5289 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5290 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5291 tls12_prf->Ai, hash_length );
5292 if( status != PSA_SUCCESS )
5293 goto cleanup;
5294 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5295 tls12_prf->label, tls12_prf->label_length );
5296 if( status != PSA_SUCCESS )
5297 goto cleanup;
5298 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5299 tls12_prf->seed, tls12_prf->seed_length );
5300 if( status != PSA_SUCCESS )
5301 goto cleanup;
5302 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5303 tls12_prf->output_block, hash_length );
5304 if( status != PSA_SUCCESS )
5305 goto cleanup;
5306 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5307 if( status != PSA_SUCCESS )
5308 goto cleanup;
5309
Janos Follath7742fee2019-06-17 12:58:10 +01005310
5311cleanup:
5312
Janos Follathea29bfb2019-06-19 12:21:20 +01005313 cleanup_status = psa_hash_abort( &backup );
5314 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5315 status = cleanup_status;
5316
5317 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005318}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005319
Janos Follath844eb0e2019-06-19 12:10:49 +01005320static psa_status_t psa_key_derivation_tls12_prf_read(
5321 psa_tls12_prf_key_derivation_t *tls12_prf,
5322 psa_algorithm_t alg,
5323 uint8_t *output,
5324 size_t output_length )
5325{
5326 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5327 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5328 psa_status_t status;
5329 uint8_t offset, length;
5330
5331 while( output_length != 0 )
5332 {
5333 /* Check if we have fully processed the current block. */
5334 if( tls12_prf->left_in_block == 0 )
5335 {
5336 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5337 alg );
5338 if( status != PSA_SUCCESS )
5339 return( status );
5340
5341 continue;
5342 }
5343
5344 if( tls12_prf->left_in_block > output_length )
5345 length = (uint8_t) output_length;
5346 else
5347 length = tls12_prf->left_in_block;
5348
5349 offset = hash_length - tls12_prf->left_in_block;
5350 memcpy( output, tls12_prf->output_block + offset, length );
5351 output += length;
5352 output_length -= length;
5353 tls12_prf->left_in_block -= length;
5354 }
5355
5356 return( PSA_SUCCESS );
5357}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005358#endif /* MBEDTLS_MD_C */
5359
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005360psa_status_t psa_key_derivation_output_bytes(
5361 psa_key_derivation_operation_t *operation,
5362 uint8_t *output,
5363 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005364{
5365 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005366 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005367
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005368 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005369 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005370 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005371 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005372 }
5373
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005374 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005375 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005376 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005377 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005378 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005379 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005380 goto exit;
5381 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005382 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005383 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005384 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005385 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005386 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5387 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005388 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005389 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005390 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005391 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005392 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005393
Gilles Peskinebef7f142018-07-12 17:22:21 +02005394#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005395 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005396 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005397 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005398 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005399 output, output_length );
5400 }
Janos Follathadbec812019-06-14 11:05:39 +01005401 else
Janos Follathadbec812019-06-14 11:05:39 +01005402 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005403 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005404 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005405 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005406 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005407 output_length );
5408 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005409 else
5410#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005411 {
5412 return( PSA_ERROR_BAD_STATE );
5413 }
5414
5415exit:
5416 if( status != PSA_SUCCESS )
5417 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005418 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005419 * This allows us to differentiate between exhausted operations and
5420 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5421 * operations. */
5422 psa_algorithm_t alg = operation->alg;
5423 psa_key_derivation_abort( operation );
5424 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005425 memset( output, '!', output_length );
5426 }
5427 return( status );
5428}
5429
Gilles Peskine08542d82018-07-19 17:05:42 +02005430#if defined(MBEDTLS_DES_C)
5431static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5432{
5433 if( data_size >= 8 )
5434 mbedtls_des_key_set_parity( data );
5435 if( data_size >= 16 )
5436 mbedtls_des_key_set_parity( data + 8 );
5437 if( data_size >= 24 )
5438 mbedtls_des_key_set_parity( data + 16 );
5439}
5440#endif /* MBEDTLS_DES_C */
5441
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005442static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005443 psa_key_slot_t *slot,
5444 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005445 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005446{
5447 uint8_t *data = NULL;
5448 size_t bytes = PSA_BITS_TO_BYTES( bits );
5449 psa_status_t status;
5450
Gilles Peskine8e338702019-07-30 20:06:31 +02005451 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005452 return( PSA_ERROR_INVALID_ARGUMENT );
5453 if( bits % 8 != 0 )
5454 return( PSA_ERROR_INVALID_ARGUMENT );
5455 data = mbedtls_calloc( 1, bytes );
5456 if( data == NULL )
5457 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5458
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005459 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005460 if( status != PSA_SUCCESS )
5461 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005462#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005463 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005464 psa_des_set_key_parity( data, bytes );
5465#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005466 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005467
5468exit:
5469 mbedtls_free( data );
5470 return( status );
5471}
5472
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005473psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005474 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005475 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005476{
5477 psa_status_t status;
5478 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005479 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005480
5481 /* Reject any attempt to create a zero-length key so that we don't
5482 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5483 if( psa_get_key_bits( attributes ) == 0 )
5484 return( PSA_ERROR_INVALID_ARGUMENT );
5485
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005486 if( ! operation->can_output_key )
5487 return( PSA_ERROR_NOT_PERMITTED );
5488
Gilles Peskinedf179142019-07-15 22:02:14 +02005489 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005490 attributes, key, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005491#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5492 if( driver != NULL )
5493 {
5494 /* Deriving a key in a secure element is not implemented yet. */
5495 status = PSA_ERROR_NOT_SUPPORTED;
5496 }
5497#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005498 if( status == PSA_SUCCESS )
5499 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005500 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005501 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005502 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005503 }
5504 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005505 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005506 if( status != PSA_SUCCESS )
5507 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005508 psa_fail_key_creation( slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02005509 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005510 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005511 else
5512 status = psa_decrement_key_slot_access_count( slot );
5513
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005514 return( status );
5515}
5516
Gilles Peskineeab56e42018-07-12 17:12:33 +02005517
5518
5519/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005520/* Key derivation */
5521/****************************************************************/
5522
Gilles Peskine969c5d62019-01-16 15:53:06 +01005523static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005524 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005525 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005526{
Janos Follath5fe19732019-06-20 15:09:30 +01005527 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5528 * initialisers for this union leave some bytes unspecified.) */
5529 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5530
Gilles Peskine969c5d62019-01-16 15:53:06 +01005531 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005532#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005533 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5534 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5535 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005536 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005537 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005538 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5539 if( hash_size == 0 )
5540 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005541 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5542 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005543 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005544 {
5545 return( PSA_ERROR_NOT_SUPPORTED );
5546 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005547 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005548 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005549 }
5550#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005551 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005552 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005553}
5554
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005555psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005556 psa_algorithm_t alg )
5557{
5558 psa_status_t status;
5559
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005560 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005561 return( PSA_ERROR_BAD_STATE );
5562
Gilles Peskine6843c292019-01-18 16:44:49 +01005563 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5564 return( PSA_ERROR_INVALID_ARGUMENT );
5565 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005566 {
5567 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005568 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005569 }
5570 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5571 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005572 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005573 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005574 else
5575 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005576
5577 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005578 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005579 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005580}
5581
5582#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005583static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005584 psa_algorithm_t hash_alg,
5585 psa_key_derivation_step_t step,
5586 const uint8_t *data,
5587 size_t data_length )
5588{
5589 psa_status_t status;
5590 switch( step )
5591 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005592 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005593 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005594 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005595 status = psa_hmac_setup_internal( &hkdf->hmac,
5596 data, data_length,
5597 hash_alg );
5598 if( status != PSA_SUCCESS )
5599 return( status );
5600 hkdf->state = HKDF_STATE_STARTED;
5601 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005602 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005603 /* If no salt was provided, use an empty salt. */
5604 if( hkdf->state == HKDF_STATE_INIT )
5605 {
5606 status = psa_hmac_setup_internal( &hkdf->hmac,
5607 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005608 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005609 if( status != PSA_SUCCESS )
5610 return( status );
5611 hkdf->state = HKDF_STATE_STARTED;
5612 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005613 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005614 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005615 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5616 data, data_length );
5617 if( status != PSA_SUCCESS )
5618 return( status );
5619 status = psa_hmac_finish_internal( &hkdf->hmac,
5620 hkdf->prk,
5621 sizeof( hkdf->prk ) );
5622 if( status != PSA_SUCCESS )
5623 return( status );
5624 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5625 hkdf->block_number = 0;
5626 hkdf->state = HKDF_STATE_KEYED;
5627 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005628 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005629 if( hkdf->state == HKDF_STATE_OUTPUT )
5630 return( PSA_ERROR_BAD_STATE );
5631 if( hkdf->info_set )
5632 return( PSA_ERROR_BAD_STATE );
5633 hkdf->info_length = data_length;
5634 if( data_length != 0 )
5635 {
5636 hkdf->info = mbedtls_calloc( 1, data_length );
5637 if( hkdf->info == NULL )
5638 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5639 memcpy( hkdf->info, data, data_length );
5640 }
5641 hkdf->info_set = 1;
5642 return( PSA_SUCCESS );
5643 default:
5644 return( PSA_ERROR_INVALID_ARGUMENT );
5645 }
5646}
Janos Follathb03233e2019-06-11 15:30:30 +01005647
Janos Follathf08e2652019-06-13 09:05:41 +01005648static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5649 const uint8_t *data,
5650 size_t data_length )
5651{
5652 if( prf->state != TLS12_PRF_STATE_INIT )
5653 return( PSA_ERROR_BAD_STATE );
5654
Janos Follathd6dce9f2019-07-04 09:11:38 +01005655 if( data_length != 0 )
5656 {
5657 prf->seed = mbedtls_calloc( 1, data_length );
5658 if( prf->seed == NULL )
5659 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005660
Janos Follathd6dce9f2019-07-04 09:11:38 +01005661 memcpy( prf->seed, data, data_length );
5662 prf->seed_length = data_length;
5663 }
Janos Follathf08e2652019-06-13 09:05:41 +01005664
5665 prf->state = TLS12_PRF_STATE_SEED_SET;
5666
5667 return( PSA_SUCCESS );
5668}
5669
Janos Follath81550542019-06-13 14:26:34 +01005670static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5671 psa_algorithm_t hash_alg,
5672 const uint8_t *data,
5673 size_t data_length )
5674{
5675 psa_status_t status;
5676 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5677 return( PSA_ERROR_BAD_STATE );
5678
5679 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5680 if( status != PSA_SUCCESS )
5681 return( status );
5682
5683 prf->state = TLS12_PRF_STATE_KEY_SET;
5684
5685 return( PSA_SUCCESS );
5686}
5687
Janos Follath6660f0e2019-06-17 08:44:03 +01005688static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5689 psa_tls12_prf_key_derivation_t *prf,
5690 psa_algorithm_t hash_alg,
5691 const uint8_t *data,
5692 size_t data_length )
5693{
5694 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005695 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5696 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005697
5698 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5699 return( PSA_ERROR_INVALID_ARGUMENT );
5700
5701 /* Quoting RFC 4279, Section 2:
5702 *
5703 * The premaster secret is formed as follows: if the PSK is N octets
5704 * long, concatenate a uint16 with the value N, N zero octets, a second
5705 * uint16 with the value N, and the PSK itself.
5706 */
5707
Janos Follath40e13932019-06-26 13:22:29 +01005708 *cur++ = ( data_length >> 8 ) & 0xff;
5709 *cur++ = ( data_length >> 0 ) & 0xff;
5710 memset( cur, 0, data_length );
5711 cur += data_length;
5712 *cur++ = pms[0];
5713 *cur++ = pms[1];
5714 memcpy( cur, data, data_length );
5715 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005716
Janos Follath40e13932019-06-26 13:22:29 +01005717 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005718
5719 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5720 return( status );
5721}
5722
Janos Follath63028dd2019-06-13 09:15:47 +01005723static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5724 const uint8_t *data,
5725 size_t data_length )
5726{
5727 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5728 return( PSA_ERROR_BAD_STATE );
5729
Janos Follathd6dce9f2019-07-04 09:11:38 +01005730 if( data_length != 0 )
5731 {
5732 prf->label = mbedtls_calloc( 1, data_length );
5733 if( prf->label == NULL )
5734 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005735
Janos Follathd6dce9f2019-07-04 09:11:38 +01005736 memcpy( prf->label, data, data_length );
5737 prf->label_length = data_length;
5738 }
Janos Follath63028dd2019-06-13 09:15:47 +01005739
5740 prf->state = TLS12_PRF_STATE_LABEL_SET;
5741
5742 return( PSA_SUCCESS );
5743}
5744
Janos Follathb03233e2019-06-11 15:30:30 +01005745static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5746 psa_algorithm_t hash_alg,
5747 psa_key_derivation_step_t step,
5748 const uint8_t *data,
5749 size_t data_length )
5750{
Janos Follathb03233e2019-06-11 15:30:30 +01005751 switch( step )
5752 {
Janos Follathf08e2652019-06-13 09:05:41 +01005753 case PSA_KEY_DERIVATION_INPUT_SEED:
5754 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005755 case PSA_KEY_DERIVATION_INPUT_SECRET:
5756 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005757 case PSA_KEY_DERIVATION_INPUT_LABEL:
5758 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005759 default:
5760 return( PSA_ERROR_INVALID_ARGUMENT );
5761 }
5762}
Janos Follath6660f0e2019-06-17 08:44:03 +01005763
5764static psa_status_t psa_tls12_prf_psk_to_ms_input(
5765 psa_tls12_prf_key_derivation_t *prf,
5766 psa_algorithm_t hash_alg,
5767 psa_key_derivation_step_t step,
5768 const uint8_t *data,
5769 size_t data_length )
5770{
5771 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005772 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005773 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5774 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005775 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005776
5777 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5778}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005779#endif /* MBEDTLS_MD_C */
5780
Gilles Peskineb8965192019-09-24 16:21:10 +02005781/** Check whether the given key type is acceptable for the given
5782 * input step of a key derivation.
5783 *
5784 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5785 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5786 * Both secret and non-secret inputs can alternatively have the type
5787 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5788 * that the input was passed as a buffer rather than via a key object.
5789 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005790static int psa_key_derivation_check_input_type(
5791 psa_key_derivation_step_t step,
5792 psa_key_type_t key_type )
5793{
5794 switch( step )
5795 {
5796 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005797 if( key_type == PSA_KEY_TYPE_DERIVE )
5798 return( PSA_SUCCESS );
5799 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005800 return( PSA_SUCCESS );
5801 break;
5802 case PSA_KEY_DERIVATION_INPUT_LABEL:
5803 case PSA_KEY_DERIVATION_INPUT_SALT:
5804 case PSA_KEY_DERIVATION_INPUT_INFO:
5805 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005806 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5807 return( PSA_SUCCESS );
5808 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005809 return( PSA_SUCCESS );
5810 break;
5811 }
5812 return( PSA_ERROR_INVALID_ARGUMENT );
5813}
5814
Janos Follathb80a94e2019-06-12 15:54:46 +01005815static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005816 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005817 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005818 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005819 const uint8_t *data,
5820 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005821{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005822 psa_status_t status;
5823 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5824
5825 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005826 if( status != PSA_SUCCESS )
5827 goto exit;
5828
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005829#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005830 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005831 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005832 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005833 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005834 step, data, data_length );
5835 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005836 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005837 {
Janos Follathb03233e2019-06-11 15:30:30 +01005838 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5839 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5840 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005841 }
5842 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5843 {
5844 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5845 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5846 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005847 }
5848 else
5849#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005850 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005851 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005852 return( PSA_ERROR_BAD_STATE );
5853 }
5854
Gilles Peskine224b0d62019-09-23 18:13:17 +02005855exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005856 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005857 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005858 return( status );
5859}
5860
Janos Follath51f4a0f2019-06-14 11:35:55 +01005861psa_status_t psa_key_derivation_input_bytes(
5862 psa_key_derivation_operation_t *operation,
5863 psa_key_derivation_step_t step,
5864 const uint8_t *data,
5865 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005866{
Gilles Peskineb8965192019-09-24 16:21:10 +02005867 return( psa_key_derivation_input_internal( operation, step,
5868 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005869 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005870}
5871
Janos Follath51f4a0f2019-06-14 11:35:55 +01005872psa_status_t psa_key_derivation_input_key(
5873 psa_key_derivation_operation_t *operation,
5874 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005875 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005876{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005877 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5878 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005879 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005880
Ronald Croncf56a0a2020-08-04 09:51:30 +02005881 status = psa_get_transparent_key( key, &slot,
5882 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005883 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005884 {
5885 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005886 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005887 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005888
5889 /* Passing a key object as a SECRET input unlocks the permission
5890 * to output to a key object. */
5891 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5892 operation->can_output_key = 1;
5893
Ronald Cronf95a2b12020-10-22 15:24:49 +02005894 status = psa_key_derivation_input_internal( operation,
5895 step, slot->attr.type,
5896 slot->data.key.data,
5897 slot->data.key.bytes );
5898
5899 decrement_status = psa_decrement_key_slot_access_count( slot );
5900
5901 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005902}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005903
5904
5905
5906/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005907/* Key agreement */
5908/****************************************************************/
5909
Gilles Peskinea05219c2018-11-16 16:02:56 +01005910#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005911static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5912 size_t peer_key_length,
5913 const mbedtls_ecp_keypair *our_key,
5914 uint8_t *shared_secret,
5915 size_t shared_secret_size,
5916 size_t *shared_secret_length )
5917{
Steven Cooremand4867872020-08-05 16:31:39 +02005918 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005919 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005920 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005921 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005922 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005923 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005924
Steven Cooreman4fed4552020-08-03 14:46:03 +02005925 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5926 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005927 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005928 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005929 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005930 goto exit;
5931
Jaeden Amero97271b32019-01-10 19:38:51 +00005932 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005933 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005934 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005935 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005936 status = mbedtls_to_psa_error(
5937 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5938 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005939 goto exit;
5940
Jaeden Amero97271b32019-01-10 19:38:51 +00005941 status = mbedtls_to_psa_error(
5942 mbedtls_ecdh_calc_secret( &ecdh,
5943 shared_secret_length,
5944 shared_secret, shared_secret_size,
5945 mbedtls_ctr_drbg_random,
5946 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005947 if( status != PSA_SUCCESS )
5948 goto exit;
5949 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5950 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005951
5952exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005953 if( status != PSA_SUCCESS )
5954 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005955 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005956 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005957 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005958
Jaeden Amero97271b32019-01-10 19:38:51 +00005959 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005960}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005961#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005962
Gilles Peskine01d718c2018-09-18 12:01:02 +02005963#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5964
Gilles Peskine0216fe12019-04-11 21:23:21 +02005965static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5966 psa_key_slot_t *private_key,
5967 const uint8_t *peer_key,
5968 size_t peer_key_length,
5969 uint8_t *shared_secret,
5970 size_t shared_secret_size,
5971 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005972{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005973 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005974 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005975#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005976 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005977 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005978 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005979 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005980 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005981 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005982 private_key->data.key.data,
5983 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005984 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005985 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005986 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005987 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005988 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005989 shared_secret, shared_secret_size,
5990 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005991 mbedtls_ecp_keypair_free( ecp );
5992 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005993 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005994#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005995 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005996 (void) private_key;
5997 (void) peer_key;
5998 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005999 (void) shared_secret;
6000 (void) shared_secret_size;
6001 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006002 return( PSA_ERROR_NOT_SUPPORTED );
6003 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006004}
6005
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006006/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006007 * to potentially free embedded data structures and wipe confidential data.
6008 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006009static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006010 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006011 psa_key_slot_t *private_key,
6012 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006013 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006014{
6015 psa_status_t status;
6016 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6017 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006018 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006019
6020 /* Step 1: run the secret agreement algorithm to generate the shared
6021 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006022 status = psa_key_agreement_raw_internal( ka_alg,
6023 private_key,
6024 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006025 shared_secret,
6026 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006027 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006028 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006029 goto exit;
6030
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006031 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006032 * the shared secret. A shared secret is permitted wherever a key
6033 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006034 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006035 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006036 shared_secret,
6037 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006038exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006039 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006040 return( status );
6041}
6042
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006043psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006044 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006045 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006046 const uint8_t *peer_key,
6047 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006048{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006049 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6050 psa_status_t decrement_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006051 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006052
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006053 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006054 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02006055 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006056 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006057 if( status != PSA_SUCCESS )
6058 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006059 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006060 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006061 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006062 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006063 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006064 else
6065 {
6066 /* If a private key has been added as SECRET, we allow the derived
6067 * key material to be used as a key in PSA Crypto. */
6068 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6069 operation->can_output_key = 1;
6070 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006071
6072 decrement_status = psa_decrement_key_slot_access_count( slot );
6073
6074 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006075}
6076
Gilles Peskinebe697d82019-05-16 18:00:41 +02006077psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006078 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006079 const uint8_t *peer_key,
6080 size_t peer_key_length,
6081 uint8_t *output,
6082 size_t output_size,
6083 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +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;
6087 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006088
6089 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6090 {
6091 status = PSA_ERROR_INVALID_ARGUMENT;
6092 goto exit;
6093 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02006094 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006095 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006096 if( status != PSA_SUCCESS )
6097 goto exit;
6098
6099 status = psa_key_agreement_raw_internal( alg, slot,
6100 peer_key, peer_key_length,
6101 output, output_size,
6102 output_length );
6103
6104exit:
6105 if( status != PSA_SUCCESS )
6106 {
6107 /* If an error happens and is not handled properly, the output
6108 * may be used as a key to protect sensitive data. Arrange for such
6109 * a key to be random, which is likely to result in decryption or
6110 * verification errors. This is better than filling the buffer with
6111 * some constant data such as zeros, which would result in the data
6112 * being protected with a reproducible, easily knowable key.
6113 */
6114 psa_generate_random( output, output_size );
6115 *output_length = output_size;
6116 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006117
6118 decrement_status = psa_decrement_key_slot_access_count( slot );
6119
6120 return( ( status == PSA_SUCCESS ) ? decrement_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006121}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006122
6123
6124/****************************************************************/
6125/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006126/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006127
Gilles Peskine4c317f42018-07-12 01:24:09 +02006128psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006129 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006130{
Janos Follath24eed8d2019-11-22 13:21:35 +00006131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006132 GUARD_MODULE_INITIALIZED;
6133
Gilles Peskinef181eca2019-08-07 13:49:00 +02006134 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6135 {
6136 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6137 output,
6138 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6139 if( ret != 0 )
6140 return( mbedtls_to_psa_error( ret ) );
6141 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6142 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6143 }
6144
Gilles Peskinee59236f2018-01-27 23:32:46 +01006145 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6146 return( mbedtls_to_psa_error( ret ) );
6147}
6148
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006149#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6150#include "mbedtls/entropy_poll.h"
6151
Gilles Peskine7228da22019-07-15 11:06:15 +02006152psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006153 size_t seed_size )
6154{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006155 if( global_data.initialized )
6156 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006157
6158 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6159 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6160 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6161 return( PSA_ERROR_INVALID_ARGUMENT );
6162
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006163 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006164}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006165#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006166
Gilles Peskinee56e8782019-04-26 17:34:02 +02006167#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
6168static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6169 size_t domain_parameters_size,
6170 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006171{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006172 size_t i;
6173 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006174
Gilles Peskinee56e8782019-04-26 17:34:02 +02006175 if( domain_parameters_size == 0 )
6176 {
6177 *exponent = 65537;
6178 return( PSA_SUCCESS );
6179 }
6180
6181 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6182 * support values that fit in a 32-bit integer, which is larger than
6183 * int on just about every platform anyway. */
6184 if( domain_parameters_size > sizeof( acc ) )
6185 return( PSA_ERROR_NOT_SUPPORTED );
6186 for( i = 0; i < domain_parameters_size; i++ )
6187 acc = ( acc << 8 ) | domain_parameters[i];
6188 if( acc > INT_MAX )
6189 return( PSA_ERROR_NOT_SUPPORTED );
6190 *exponent = acc;
6191 return( PSA_SUCCESS );
6192}
6193#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6194
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006195static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006196 psa_key_slot_t *slot, size_t bits,
6197 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006198{
Gilles Peskine8e338702019-07-30 20:06:31 +02006199 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006200
Gilles Peskinee56e8782019-04-26 17:34:02 +02006201 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006202 return( PSA_ERROR_INVALID_ARGUMENT );
6203
Gilles Peskinee59236f2018-01-27 23:32:46 +01006204 if( key_type_is_raw_bytes( type ) )
6205 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006206 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006207
6208 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006209 if( status != PSA_SUCCESS )
6210 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006211
6212 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006213 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6214 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006215 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006216
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006217 status = psa_generate_random( slot->data.key.data,
6218 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006219 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006220 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006221
6222 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006223#if defined(MBEDTLS_DES_C)
6224 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006225 psa_des_set_key_parity( slot->data.key.data,
6226 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006227#endif /* MBEDTLS_DES_C */
6228 }
6229 else
6230
6231#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006232 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006233 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006234 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006235 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006236 int exponent;
6237 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006238 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6239 return( PSA_ERROR_NOT_SUPPORTED );
6240 /* Accept only byte-aligned keys, for the same reasons as
6241 * in psa_import_rsa_key(). */
6242 if( bits % 8 != 0 )
6243 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006244 status = psa_read_rsa_exponent( domain_parameters,
6245 domain_parameters_size,
6246 &exponent );
6247 if( status != PSA_SUCCESS )
6248 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006249 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6250 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006251 mbedtls_ctr_drbg_random,
6252 &global_data.ctr_drbg,
6253 (unsigned int) bits,
6254 exponent );
6255 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006256 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006257
6258 /* Make sure to always have an export representation available */
6259 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6260
Steven Cooreman75b74362020-07-28 14:30:13 +02006261 status = psa_allocate_buffer_to_slot( slot, bytes );
6262 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006263 {
6264 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006265 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006266 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006267
6268 status = psa_export_rsa_key( type,
6269 &rsa,
6270 slot->data.key.data,
6271 bytes,
6272 &slot->data.key.bytes );
6273 mbedtls_rsa_free( &rsa );
6274 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006275 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006276 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006277 }
6278 else
6279#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6280
6281#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006282 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006283 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006284 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006285 mbedtls_ecp_group_id grp_id =
6286 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006287 const mbedtls_ecp_curve_info *curve_info =
6288 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006289 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006291 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006292 return( PSA_ERROR_NOT_SUPPORTED );
6293 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6294 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006295 mbedtls_ecp_keypair_init( &ecp );
6296 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006297 mbedtls_ctr_drbg_random,
6298 &global_data.ctr_drbg );
6299 if( ret != 0 )
6300 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006301 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006302 return( mbedtls_to_psa_error( ret ) );
6303 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006304
6305
6306 /* Make sure to always have an export representation available */
6307 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006308 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6309 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006310 {
6311 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006312 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006313 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006314
6315 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006316 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6317
6318 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006319 if( status != PSA_SUCCESS ) {
6320 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006321 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006322 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006323 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006324 }
6325 else
6326#endif /* MBEDTLS_ECP_C */
Steven Cooreman29149862020-08-05 15:43:42 +02006327 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006328 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006329 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006330
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006331 return( PSA_SUCCESS );
6332}
Darryl Green0c6575a2018-11-07 16:05:30 +00006333
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006334psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006335 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006336{
6337 psa_status_t status;
6338 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006339 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006340
Gilles Peskine0f84d622019-09-12 19:03:13 +02006341 /* Reject any attempt to create a zero-length key so that we don't
6342 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6343 if( psa_get_key_bits( attributes ) == 0 )
6344 return( PSA_ERROR_INVALID_ARGUMENT );
6345
Gilles Peskinedf179142019-07-15 22:02:14 +02006346 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006347 attributes, key, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006348 if( status != PSA_SUCCESS )
6349 goto exit;
6350
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006351 status = psa_driver_wrapper_generate_key( attributes,
6352 slot );
6353 if( status != PSA_ERROR_NOT_SUPPORTED ||
6354 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6355 goto exit;
6356
6357 status = psa_generate_key_internal(
6358 slot, attributes->core.bits,
6359 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006360
6361exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006362 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006363 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006364 if( status != PSA_SUCCESS )
6365 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006366 psa_fail_key_creation( slot, driver );
Ronald Croncf56a0a2020-08-04 09:51:30 +02006367 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006368 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006369 else
6370 status = psa_decrement_key_slot_access_count( slot );
6371
Darryl Green0c6575a2018-11-07 16:05:30 +00006372 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006373}
6374
6375
Gilles Peskinee59236f2018-01-27 23:32:46 +01006376
6377/****************************************************************/
6378/* Module setup */
6379/****************************************************************/
6380
Gilles Peskine5e769522018-11-20 21:59:56 +01006381psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6382 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6383 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6384{
6385 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6386 return( PSA_ERROR_BAD_STATE );
6387 global_data.entropy_init = entropy_init;
6388 global_data.entropy_free = entropy_free;
6389 return( PSA_SUCCESS );
6390}
6391
Gilles Peskinee59236f2018-01-27 23:32:46 +01006392void mbedtls_psa_crypto_free( void )
6393{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006394 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006395 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6396 {
6397 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006398 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006399 }
6400 /* Wipe all remaining data, including configuration.
6401 * In particular, this sets all state indicator to the value
6402 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006403 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006404#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006405 /* Unregister all secure element drivers, so that we restart from
6406 * a pristine state. */
6407 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006408#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006409}
6410
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006411#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6412/** Recover a transaction that was interrupted by a power failure.
6413 *
6414 * This function is called during initialization, before psa_crypto_init()
6415 * returns. If this function returns a failure status, the initialization
6416 * fails.
6417 */
6418static psa_status_t psa_crypto_recover_transaction(
6419 const psa_crypto_transaction_t *transaction )
6420{
6421 switch( transaction->unknown.type )
6422 {
6423 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6424 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006425 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006426 * is implemented.
6427 * https://github.com/ARMmbed/mbed-crypto/issues/218
6428 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006429 default:
6430 /* We found an unsupported transaction in the storage.
6431 * We don't know what state the storage is in. Give up. */
6432 return( PSA_ERROR_STORAGE_FAILURE );
6433 }
6434}
6435#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6436
Gilles Peskinee59236f2018-01-27 23:32:46 +01006437psa_status_t psa_crypto_init( void )
6438{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006439 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006440 const unsigned char drbg_seed[] = "PSA";
6441
Gilles Peskinec6b69072018-11-20 21:42:52 +01006442 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006443 if( global_data.initialized != 0 )
6444 return( PSA_SUCCESS );
6445
Gilles Peskine5e769522018-11-20 21:59:56 +01006446 /* Set default configuration if
6447 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6448 if( global_data.entropy_init == NULL )
6449 global_data.entropy_init = mbedtls_entropy_init;
6450 if( global_data.entropy_free == NULL )
6451 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006452
Gilles Peskinec6b69072018-11-20 21:42:52 +01006453 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006454 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006455#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6456 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6457 /* The PSA entropy injection feature depends on using NV seed as an entropy
6458 * source. Add NV seed as an entropy source for PSA entropy injection. */
6459 mbedtls_entropy_add_source( &global_data.entropy,
6460 mbedtls_nv_seed_poll, NULL,
6461 MBEDTLS_ENTROPY_BLOCK_SIZE,
6462 MBEDTLS_ENTROPY_SOURCE_STRONG );
6463#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006464 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006465 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006466 status = mbedtls_to_psa_error(
6467 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6468 mbedtls_entropy_func,
6469 &global_data.entropy,
6470 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6471 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006472 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006473 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006474
Gilles Peskine66fb1262018-12-10 16:29:04 +01006475 status = psa_initialize_key_slots( );
6476 if( status != PSA_SUCCESS )
6477 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006478
Gilles Peskined9348f22019-10-01 15:22:29 +02006479#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6480 status = psa_init_all_se_drivers( );
6481 if( status != PSA_SUCCESS )
6482 goto exit;
6483#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6484
Gilles Peskine4b734222019-07-24 15:56:31 +02006485#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006486 status = psa_crypto_load_transaction( );
6487 if( status == PSA_SUCCESS )
6488 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006489 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6490 if( status != PSA_SUCCESS )
6491 goto exit;
6492 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006493 }
6494 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6495 {
6496 /* There's no transaction to complete. It's all good. */
6497 status = PSA_SUCCESS;
6498 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006499#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006500
Gilles Peskinec6b69072018-11-20 21:42:52 +01006501 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006502 global_data.initialized = 1;
6503
6504exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006505 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006506 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006507 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006508}
6509
6510#endif /* MBEDTLS_PSA_CRYPTO_C */