blob: e527ab72a9a7f548a25c4554a0867ac90ba36a4e [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
John Durkopf4c4cb02020-10-28 20:09:55 -0700373#if defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY)
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}
John Durkopf4c4cb02020-10-28 20:09:55 -0700441#endif /* defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY) */
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
John Durkop0e005192020-10-31 22:06:54 -0700494#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
495 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
496 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
497 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Steven Cooremana01795d2020-07-24 22:48:15 +0200498
499#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100500/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
501 * that are not a multiple of 8) well. For example, there is only
502 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
503 * way to return the exact bit size of a key.
504 * To keep things simple, reject non-byte-aligned key sizes. */
505static psa_status_t psa_check_rsa_key_byte_aligned(
506 const mbedtls_rsa_context *rsa )
507{
508 mbedtls_mpi n;
509 psa_status_t status;
510 mbedtls_mpi_init( &n );
511 status = mbedtls_to_psa_error(
512 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
513 if( status == PSA_SUCCESS )
514 {
515 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
516 status = PSA_ERROR_NOT_SUPPORTED;
517 }
518 mbedtls_mpi_free( &n );
519 return( status );
520}
Steven Cooremana01795d2020-07-24 22:48:15 +0200521#endif /* MBEDTLS_PK_PARSE_C */
Gilles Peskine86a440b2018-11-12 18:39:40 +0100522
Steven Cooreman7f391872020-07-30 14:57:44 +0200523/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200524 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200525 * \param[in] type The type of key contained in \p data.
526 * \param[in] data The buffer from which to load the representation.
527 * \param[in] data_length The size in bytes of \p data.
528 * \param[out] p_rsa Returns a pointer to an RSA context on success.
529 * The caller is responsible for freeing both the
530 * contents of the context and the context itself
531 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200532 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200533static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
534 const uint8_t *data,
535 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200536 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200537{
Steven Cooremana01795d2020-07-24 22:48:15 +0200538#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000539 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200540 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000541 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200542 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543
544 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200545 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200547 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200548 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000549 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200550 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000551 if( status != PSA_SUCCESS )
552 goto exit;
553
554 /* We have something that the pkparse module recognizes. If it is a
555 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200556 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558 status = PSA_ERROR_INVALID_ARGUMENT;
559 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000561
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000562 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
563 * supports non-byte-aligned key sizes, but not well. For example,
564 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200565 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000566 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
567 {
568 status = PSA_ERROR_NOT_SUPPORTED;
569 goto exit;
570 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200571 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200572 if( status != PSA_SUCCESS )
573 goto exit;
574
Steven Cooremana2371e52020-07-28 14:30:39 +0200575 /* Copy out the pointer to the RSA context, and reset the PK context
576 * such that pk_free doesn't free the RSA context we just grabbed. */
577 *p_rsa = mbedtls_pk_rsa( ctx );
578 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000579
580exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200581 mbedtls_pk_free( &ctx );
582 return( status );
583#else
Steven Cooreman4fed4552020-08-03 14:46:03 +0200584 (void) data;
585 (void) data_length;
Steven Cooreman7f391872020-07-30 14:57:44 +0200586 (void) type;
Steven Cooremana01795d2020-07-24 22:48:15 +0200587 (void) rsa;
588 return( PSA_ERROR_NOT_SUPPORTED );
589#endif /* MBEDTLS_PK_PARSE_C */
590}
591
592/** Export an RSA key to export representation
593 *
594 * \param[in] type The type of key (public/private) to export
595 * \param[in] rsa The internal RSA representation from which to export
596 * \param[out] data The buffer to export to
597 * \param[in] data_size The length of the buffer to export to
598 * \param[out] data_length The amount of bytes written to \p data
599 */
600static psa_status_t psa_export_rsa_key( psa_key_type_t type,
601 mbedtls_rsa_context *rsa,
602 uint8_t *data,
603 size_t data_size,
604 size_t *data_length )
605{
606#if defined(MBEDTLS_PK_WRITE_C)
607 int ret;
608 mbedtls_pk_context pk;
609 uint8_t *pos = data + data_size;
610
611 mbedtls_pk_init( &pk );
612 pk.pk_info = &mbedtls_rsa_info;
613 pk.pk_ctx = rsa;
614
615 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200616 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
617 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200618 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
619 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
620 else
621 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
622
623 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200624 {
625 /* Clean up in case pk_write failed halfway through. */
626 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200627 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200628 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200629
630 /* The mbedtls_pk_xxx functions write to the end of the buffer.
631 * Move the data to the beginning and erase remaining data
632 * at the original location. */
633 if( 2 * (size_t) ret <= data_size )
634 {
635 memcpy( data, data + data_size - ret, ret );
636 memset( data + data_size - ret, 0, ret );
637 }
638 else if( (size_t) ret < data_size )
639 {
640 memmove( data, data + data_size - ret, ret );
641 memset( data + ret, 0, data_size - ret );
642 }
643
644 *data_length = ret;
645 return( PSA_SUCCESS );
646#else
647 (void) type;
648 (void) rsa;
649 (void) data;
650 (void) data_size;
651 (void) data_length;
652 return( PSA_ERROR_NOT_SUPPORTED );
653#endif /* MBEDTLS_PK_WRITE_C */
654}
655
656/** Import an RSA key from import representation to a slot
657 *
658 * \param[in,out] slot The slot where to store the export representation to
659 * \param[in] data The buffer containing the import representation
660 * \param[in] data_length The amount of bytes in \p data
661 */
662static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
663 const uint8_t *data,
664 size_t data_length )
665{
666 psa_status_t status;
667 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200668 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200669
Steven Cooremana01795d2020-07-24 22:48:15 +0200670 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200671 status = psa_load_rsa_representation( slot->attr.type,
672 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200673 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200674 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200675 if( status != PSA_SUCCESS )
676 goto exit;
677
678 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200679 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200680
Steven Cooreman75b74362020-07-28 14:30:13 +0200681 /* Re-export the data to PSA export format, such that we can store export
682 * representation in the key slot. Export representation in case of RSA is
683 * the smallest representation that's allowed as input, so a straight-up
684 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200685 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200686 if( output == NULL )
687 {
688 status = PSA_ERROR_INSUFFICIENT_MEMORY;
689 goto exit;
690 }
691
Steven Cooremana01795d2020-07-24 22:48:15 +0200692 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200693 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200694 output,
695 data_length,
696 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200697exit:
698 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200699 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200700 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200701
702 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000703 if( status != PSA_SUCCESS )
704 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200705 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000706 return( status );
707 }
708
Steven Cooremana01795d2020-07-24 22:48:15 +0200709 /* On success, store the allocated export-formatted key. */
710 slot->data.key.data = output;
711 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000712
713 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200714}
John Durkop0e005192020-10-31 22:06:54 -0700715#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200716
John Durkopf4c4cb02020-10-28 20:09:55 -0700717#if defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY)
Steven Cooreman7f391872020-07-30 14:57:44 +0200718/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200719 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200720 * \param[in] type The type of key contained in \p data.
721 * \param[in] data The buffer from which to load the representation.
722 * \param[in] data_length The size in bytes of \p data.
723 * \param[out] p_ecp Returns a pointer to an ECP context on success.
724 * The caller is responsible for freeing both the
725 * contents of the context and the context itself
726 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200727 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200728static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
729 const uint8_t *data,
730 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200731 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100732{
733 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200734 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200735 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200736 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100737
Steven Cooreman4fed4552020-08-03 14:46:03 +0200738 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
739 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100740 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200741 /* A Weierstrass public key is represented as:
742 * - The byte 0x04;
743 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
744 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200745 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200746 */
747 if( ( data_length & 1 ) == 0 )
748 return( PSA_ERROR_INVALID_ARGUMENT );
749 curve_size = data_length / 2;
750
751 /* Montgomery public keys are represented in compressed format, meaning
752 * their curve_size is equal to the amount of input. */
753
754 /* Private keys are represented in uncompressed private random integer
755 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100756 }
757
Steven Cooreman6d839f02020-07-30 11:36:45 +0200758 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200759 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200760 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200761 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200762 mbedtls_ecp_keypair_init( ecp );
763
Gilles Peskine4cd32772019-12-02 20:49:42 +0100764 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200765 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
766 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100767 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200768 {
769 status = PSA_ERROR_INVALID_ARGUMENT;
770 goto exit;
771 }
772
Steven Cooremanacda8342020-07-24 23:09:52 +0200773 status = mbedtls_to_psa_error(
774 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
775 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200776 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200777
Steven Cooreman6d839f02020-07-30 11:36:45 +0200778 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200779 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200780 {
781 /* Load the public value. */
782 status = mbedtls_to_psa_error(
783 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200784 data,
785 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200786 if( status != PSA_SUCCESS )
787 goto exit;
788
789 /* Check that the point is on the curve. */
790 status = mbedtls_to_psa_error(
791 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
792 if( status != PSA_SUCCESS )
793 goto exit;
794 }
795 else
796 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200797 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200798 status = mbedtls_to_psa_error(
799 mbedtls_ecp_read_key( ecp->grp.id,
800 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200801 data,
802 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200803 if( status != PSA_SUCCESS )
804 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200805 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200806
807 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200808exit:
809 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200810 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200811 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200812 mbedtls_free( ecp );
813 }
814
Steven Cooreman29149862020-08-05 15:43:42 +0200815 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100816}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000817
Steven Cooreman4fed4552020-08-03 14:46:03 +0200818/** Export an ECP key to export representation
819 *
820 * \param[in] type The type of key (public/private) to export
821 * \param[in] ecp The internal ECP representation from which to export
822 * \param[out] data The buffer to export to
823 * \param[in] data_size The length of the buffer to export to
824 * \param[out] data_length The amount of bytes written to \p data
825 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200826static psa_status_t psa_export_ecp_key( psa_key_type_t type,
827 mbedtls_ecp_keypair *ecp,
828 uint8_t *data,
829 size_t data_size,
830 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200831{
Steven Cooremanacda8342020-07-24 23:09:52 +0200832 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000833
Steven Cooremanacda8342020-07-24 23:09:52 +0200834 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200835 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200836 /* Check whether the public part is loaded */
837 if( mbedtls_ecp_is_zero( &ecp->Q ) )
838 {
839 /* Calculate the public key */
840 status = mbedtls_to_psa_error(
841 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
842 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
843 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200844 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200845 }
846
Steven Cooreman4fed4552020-08-03 14:46:03 +0200847 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200848 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
849 MBEDTLS_ECP_PF_UNCOMPRESSED,
850 data_length,
851 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200852 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200853 if( status != PSA_SUCCESS )
854 memset( data, 0, data_size );
855
Steven Cooreman29149862020-08-05 15:43:42 +0200856 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200857 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200858 else
859 {
Steven Cooreman29149862020-08-05 15:43:42 +0200860 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200861 return( PSA_ERROR_BUFFER_TOO_SMALL );
862
863 status = mbedtls_to_psa_error(
864 mbedtls_ecp_write_key( ecp,
865 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200866 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200867 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200868 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200869 else
870 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200871
872 return( status );
873 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200874}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200875
Steven Cooreman4fed4552020-08-03 14:46:03 +0200876/** Import an ECP key from import representation to a slot
877 *
878 * \param[in,out] slot The slot where to store the export representation to
879 * \param[in] data The buffer containing the import representation
880 * \param[in] data_length The amount of bytes in \p data
881 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200882static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
883 const uint8_t *data,
884 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100885{
Steven Cooremanacda8342020-07-24 23:09:52 +0200886 psa_status_t status;
887 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200888 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100889
Steven Cooremanacda8342020-07-24 23:09:52 +0200890 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200891 status = psa_load_ecp_representation( slot->attr.type,
892 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200893 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200894 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100895 if( status != PSA_SUCCESS )
896 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100897
Steven Cooremanacda8342020-07-24 23:09:52 +0200898 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200899 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200900 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200901 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200902
Steven Cooremanacda8342020-07-24 23:09:52 +0200903 /* Re-export the data to PSA export format. There is currently no support
904 * for other input formats then the export format, so this is a 1-1
905 * copy operation. */
906 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200907 if( output == NULL )
908 {
909 status = PSA_ERROR_INSUFFICIENT_MEMORY;
910 goto exit;
911 }
912
913 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200914 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200915 output,
916 data_length,
917 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100918exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200919 /* Always free the PK object (will also free contained ECP context) */
920 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200921 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200922
923 /* Free the allocated buffer only on error. */
924 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100925 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200926 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200927 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100928 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200929
930 /* On success, store the allocated export-formatted key. */
931 slot->data.key.data = output;
932 slot->data.key.bytes = data_length;
933
934 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100935}
John Durkopf4c4cb02020-10-28 20:09:55 -0700936#endif /* defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100937
Gilles Peskineb46bef22019-07-30 21:32:04 +0200938/** Return the size of the key in the given slot, in bits.
939 *
940 * \param[in] slot A key slot.
941 *
942 * \return The key size in bits, read from the metadata in the slot.
943 */
944static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
945{
946 return( slot->attr.bits );
947}
948
Steven Cooreman75b74362020-07-28 14:30:13 +0200949/** Try to allocate a buffer to an empty key slot.
950 *
951 * \param[in,out] slot Key slot to attach buffer to.
952 * \param[in] buffer_length Requested size of the buffer.
953 *
954 * \retval #PSA_SUCCESS
955 * The buffer has been successfully allocated.
956 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
957 * Not enough memory was available for allocation.
958 * \retval #PSA_ERROR_ALREADY_EXISTS
959 * Trying to allocate a buffer to a non-empty key slot.
960 */
961static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
962 size_t buffer_length )
963{
964 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200965 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200966
967 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
968 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200969 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200970
971 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200972 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200973}
974
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200975psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
976 const uint8_t* data,
977 size_t data_length )
978{
979 psa_status_t status = psa_allocate_buffer_to_slot( slot,
980 data_length );
981 if( status != PSA_SUCCESS )
982 return( status );
983
984 memcpy( slot->data.key.data, data, data_length );
985 return( PSA_SUCCESS );
986}
987
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200988/** Import key data into a slot.
989 *
990 * `slot->type` must have been set previously.
991 * This function assumes that the slot does not contain any key material yet.
992 * On failure, the slot content is unchanged.
993 *
994 * Persistent storage is not affected.
995 *
996 * \param[in,out] slot The key slot to import data into.
997 * Its `type` field must have previously been set to
998 * the desired key type.
999 * It must not contain any key material yet.
1000 * \param[in] data Buffer containing the key material to parse and import.
1001 * \param data_length Size of \p data in bytes.
1002 *
1003 * \retval #PSA_SUCCESS
1004 * \retval #PSA_ERROR_INVALID_ARGUMENT
1005 * \retval #PSA_ERROR_NOT_SUPPORTED
1006 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1007 */
1008static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1009 const uint8_t *data,
1010 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001012 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001013 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001015 /* zero-length keys are never supported. */
1016 if( data_length == 0 )
1017 return( PSA_ERROR_NOT_SUPPORTED );
1018
Gilles Peskine8e338702019-07-30 20:06:31 +02001019 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001020 {
Steven Cooreman04524762020-10-13 17:43:44 +02001021 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001022
Steven Cooreman75b74362020-07-28 14:30:13 +02001023 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1024 if( data_length > SIZE_MAX / 8 )
1025 return( PSA_ERROR_NOT_SUPPORTED );
1026
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001027 /* Enforce a size limit, and in particular ensure that the bit
1028 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001029 if( bit_size > PSA_MAX_KEY_BITS )
1030 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001031
1032 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001033 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001034 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001035
1036 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001037 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001038 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001039 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001040
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001041 /* Write the actual key size to the slot.
1042 * psa_start_key_creation() wrote the size declared by the
1043 * caller, which may be 0 (meaning unspecified) or wrong. */
1044 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001045
1046 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001047 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001048 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001049 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001050 /* Try validation through accelerators first. */
1051 bit_size = slot->attr.bits;
1052 psa_key_attributes_t attributes = {
1053 .core = slot->attr
1054 };
1055 status = psa_driver_wrapper_validate_key( &attributes,
1056 data,
1057 data_length,
1058 &bit_size );
1059 if( status == PSA_SUCCESS )
1060 {
1061 /* Key has been validated successfully by an accelerator.
1062 * Copy key material into slot. */
1063 status = psa_copy_key_material_into_slot( slot, data, data_length );
1064 if( status != PSA_SUCCESS )
1065 return( status );
1066
1067 slot->attr.bits = (psa_key_bits_t) bit_size;
1068 return( PSA_SUCCESS );
1069 }
1070 else if( status != PSA_ERROR_NOT_SUPPORTED )
1071 return( status );
1072
1073 /* Key format is not supported by any accelerator, try software fallback
1074 * if present. */
John Durkopf4c4cb02020-10-28 20:09:55 -07001075#if defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001076 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1077 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001078 return( psa_import_ecp_key( slot, data, data_length ) );
1079 }
John Durkopf4c4cb02020-10-28 20:09:55 -07001080#endif /* defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -07001081#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1082 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +01001083 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001084 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001085 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001086 }
John Durkop0e005192020-10-31 22:06:54 -07001087#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001088
1089 /* Fell through the fallback as well, so have nothing else to try. */
1090 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091 }
1092 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001093 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001094 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001095 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001096 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001097}
1098
Gilles Peskinef603c712019-01-19 13:40:11 +01001099/** Calculate the intersection of two algorithm usage policies.
1100 *
1101 * Return 0 (which allows no operation) on incompatibility.
1102 */
1103static psa_algorithm_t psa_key_policy_algorithm_intersection(
1104 psa_algorithm_t alg1,
1105 psa_algorithm_t alg2 )
1106{
Gilles Peskine549ea862019-05-22 11:45:59 +02001107 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001108 if( alg1 == alg2 )
1109 return( alg1 );
1110 /* If the policies are from the same hash-and-sign family, check
1111 * if one is a wildcard. If so the other has the specific algorithm. */
1112 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1113 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1114 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1115 {
1116 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1117 return( alg2 );
1118 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1119 return( alg1 );
1120 }
1121 /* If the policies are incompatible, allow nothing. */
1122 return( 0 );
1123}
1124
Gilles Peskined6f371b2019-05-10 19:33:38 +02001125static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1126 psa_algorithm_t requested_alg )
1127{
Gilles Peskine549ea862019-05-22 11:45:59 +02001128 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001129 if( requested_alg == policy_alg )
1130 return( 1 );
1131 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001132 * and requested_alg is the same hash-and-sign family with any hash,
1133 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001134 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1135 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1136 {
1137 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1138 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1139 }
Steven Cooremance48e852020-10-05 16:02:45 +02001140 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001141 * a key derivation with that key agreement should also be allowed. This
1142 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001143 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1144 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1145 {
1146 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1147 policy_alg );
1148 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001149 /* If it isn't permitted, it's forbidden. */
1150 return( 0 );
1151}
1152
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001153/** Test whether a policy permits an algorithm.
1154 *
1155 * The caller must test usage flags separately.
1156 */
1157static int psa_key_policy_permits( const psa_key_policy_t *policy,
1158 psa_algorithm_t alg )
1159{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001160 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1161 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001162}
1163
Gilles Peskinef603c712019-01-19 13:40:11 +01001164/** Restrict a key policy based on a constraint.
1165 *
1166 * \param[in,out] policy The policy to restrict.
1167 * \param[in] constraint The policy constraint to apply.
1168 *
1169 * \retval #PSA_SUCCESS
1170 * \c *policy contains the intersection of the original value of
1171 * \c *policy and \c *constraint.
1172 * \retval #PSA_ERROR_INVALID_ARGUMENT
1173 * \c *policy and \c *constraint are incompatible.
1174 * \c *policy is unchanged.
1175 */
1176static psa_status_t psa_restrict_key_policy(
1177 psa_key_policy_t *policy,
1178 const psa_key_policy_t *constraint )
1179{
1180 psa_algorithm_t intersection_alg =
1181 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001182 psa_algorithm_t intersection_alg2 =
1183 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001184 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1185 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001186 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1187 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001188 policy->usage &= constraint->usage;
1189 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001190 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001191 return( PSA_SUCCESS );
1192}
1193
Darryl Green06fd18d2018-07-16 11:21:11 +01001194/** Retrieve a slot which must contain a key. The key must have allow all the
1195 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1196 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001197static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001198 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001199 psa_key_usage_t usage,
1200 psa_algorithm_t alg )
1201{
1202 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001203 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001204
1205 *p_slot = NULL;
1206
Gilles Peskinec5487a82018-12-03 18:08:14 +01001207 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001208 if( status != PSA_SUCCESS )
1209 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001210
1211 /* Enforce that usage policy for the key slot contains all the flags
1212 * required by the usage parameter. There is one exception: public
1213 * keys can always be exported, so we treat public key objects as
1214 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001215 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001216 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001217 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001218 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001219
1220 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001221 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001222 return( PSA_ERROR_NOT_PERMITTED );
1223
1224 *p_slot = slot;
1225 return( PSA_SUCCESS );
1226}
Darryl Green940d72c2018-07-13 13:18:51 +01001227
Gilles Peskine28f8f302019-07-24 13:30:31 +02001228/** Retrieve a slot which must contain a transparent key.
1229 *
1230 * A transparent key is a key for which the key material is directly
1231 * available, as opposed to a key in a secure element.
1232 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001233 * This is a temporary function to use instead of psa_get_key_from_slot()
1234 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001235 */
1236#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1237static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1238 psa_key_slot_t **p_slot,
1239 psa_key_usage_t usage,
1240 psa_algorithm_t alg )
1241{
1242 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1243 if( status != PSA_SUCCESS )
1244 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001245 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001246 {
1247 *p_slot = NULL;
1248 return( PSA_ERROR_NOT_SUPPORTED );
1249 }
1250 return( PSA_SUCCESS );
1251}
1252#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1253/* With no secure element support, all keys are transparent. */
1254#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1255 psa_get_key_from_slot( handle, p_slot, usage, alg )
1256#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1257
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001258/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001259static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001260{
Gilles Peskine73167e12019-07-12 23:44:37 +02001261#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1262 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001263 {
1264 /* No key material to clean. */
1265 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001266 else
1267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001268 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001269 /* Data pointer will always be either a valid pointer or NULL in an
1270 * initialized slot, so we can just free it. */
1271 if( slot->data.key.data != NULL )
1272 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001273 mbedtls_free( slot->data.key.data );
1274 slot->data.key.data = NULL;
1275 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001276 }
Darryl Green40225ba2018-11-15 14:48:15 +00001277
1278 return( PSA_SUCCESS );
1279}
1280
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001281/** Completely wipe a slot in memory, including its policy.
1282 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001283psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001284{
1285 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001286 /* Multipart operations may still be using the key. This is safe
1287 * because all multipart operation objects are independent from
1288 * the key slot: if they need to access the key after the setup
1289 * phase, they have a copy of the key. Note that this means that
1290 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001291 /* At this point, key material and other type-specific content has
1292 * been wiped. Clear remaining metadata. We can call memset and not
1293 * zeroize because the metadata is not particularly sensitive. */
1294 memset( slot, 0, sizeof( *slot ) );
1295 return( status );
1296}
1297
Gilles Peskinec5487a82018-12-03 18:08:14 +01001298psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001299{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001300 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001301 psa_status_t status; /* status of the last operation */
1302 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001303#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1304 psa_se_drv_table_entry_t *driver;
1305#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001306
Gilles Peskine1841cf42019-10-08 15:48:25 +02001307 if( handle == 0 )
1308 return( PSA_SUCCESS );
1309
Gilles Peskinec5487a82018-12-03 18:08:14 +01001310 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001311 if( status != PSA_SUCCESS )
1312 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001313
1314#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001315 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001316 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001317 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001318 /* For a key in a secure element, we need to do three things:
1319 * remove the key file in internal storage, destroy the
1320 * key inside the secure element, and update the driver's
1321 * persistent data. Start a transaction that will encompass these
1322 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001323 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001324 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001325 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001326 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001327 status = psa_crypto_save_transaction( );
1328 if( status != PSA_SUCCESS )
1329 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001330 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001331 /* We should still try to destroy the key in the secure
1332 * element and the key metadata in storage. This is especially
1333 * important if the error is that the storage is full.
1334 * But how to do it exactly without risking an inconsistent
1335 * state after a reset?
1336 * https://github.com/ARMmbed/mbed-crypto/issues/215
1337 */
1338 overall_status = status;
1339 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001340 }
1341
Gilles Peskine354f7672019-07-12 23:46:38 +02001342 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001343 if( overall_status == PSA_SUCCESS )
1344 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001345 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001346#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1347
Darryl Greend49a4992018-06-18 17:27:26 +01001348#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001349 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001350 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001351 status = psa_destroy_persistent_key( slot->attr.id );
1352 if( overall_status == PSA_SUCCESS )
1353 overall_status = status;
1354
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001355 /* TODO: other slots may have a copy of the same key. We should
1356 * invalidate them.
1357 * https://github.com/ARMmbed/mbed-crypto/issues/214
1358 */
Darryl Greend49a4992018-06-18 17:27:26 +01001359 }
1360#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001361
Gilles Peskinefc762652019-07-22 19:30:34 +02001362#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1363 if( driver != NULL )
1364 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001365 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001366 if( overall_status == PSA_SUCCESS )
1367 overall_status = status;
1368 status = psa_crypto_stop_transaction( );
1369 if( overall_status == PSA_SUCCESS )
1370 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001371 }
1372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1373
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001374#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1375exit:
1376#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001377 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001378 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1379 if( overall_status == PSA_SUCCESS )
1380 overall_status = status;
1381 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001382}
1383
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001384void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001385{
Gilles Peskineb699f072019-04-26 16:06:02 +02001386 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001387 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001388}
1389
Gilles Peskineb699f072019-04-26 16:06:02 +02001390psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1391 psa_key_type_t type,
1392 const uint8_t *data,
1393 size_t data_length )
1394{
1395 uint8_t *copy = NULL;
1396
1397 if( data_length != 0 )
1398 {
1399 copy = mbedtls_calloc( 1, data_length );
1400 if( copy == NULL )
1401 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1402 memcpy( copy, data, data_length );
1403 }
1404 /* After this point, this function is guaranteed to succeed, so it
1405 * can start modifying `*attributes`. */
1406
1407 if( attributes->domain_parameters != NULL )
1408 {
1409 mbedtls_free( attributes->domain_parameters );
1410 attributes->domain_parameters = NULL;
1411 attributes->domain_parameters_size = 0;
1412 }
1413
1414 attributes->domain_parameters = copy;
1415 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001416 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001417 return( PSA_SUCCESS );
1418}
1419
1420psa_status_t psa_get_key_domain_parameters(
1421 const psa_key_attributes_t *attributes,
1422 uint8_t *data, size_t data_size, size_t *data_length )
1423{
1424 if( attributes->domain_parameters_size > data_size )
1425 return( PSA_ERROR_BUFFER_TOO_SMALL );
1426 *data_length = attributes->domain_parameters_size;
1427 if( attributes->domain_parameters_size != 0 )
1428 memcpy( data, attributes->domain_parameters,
1429 attributes->domain_parameters_size );
1430 return( PSA_SUCCESS );
1431}
1432
John Durkop0e005192020-10-31 22:06:54 -07001433#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1434 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001435static psa_status_t psa_get_rsa_public_exponent(
1436 const mbedtls_rsa_context *rsa,
1437 psa_key_attributes_t *attributes )
1438{
1439 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001441 uint8_t *buffer = NULL;
1442 size_t buflen;
1443 mbedtls_mpi_init( &mpi );
1444
1445 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1446 if( ret != 0 )
1447 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001448 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1449 {
1450 /* It's the default value, which is reported as an empty string,
1451 * so there's nothing to do. */
1452 goto exit;
1453 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001454
1455 buflen = mbedtls_mpi_size( &mpi );
1456 buffer = mbedtls_calloc( 1, buflen );
1457 if( buffer == NULL )
1458 {
1459 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1460 goto exit;
1461 }
1462 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1463 if( ret != 0 )
1464 goto exit;
1465 attributes->domain_parameters = buffer;
1466 attributes->domain_parameters_size = buflen;
1467
1468exit:
1469 mbedtls_mpi_free( &mpi );
1470 if( ret != 0 )
1471 mbedtls_free( buffer );
1472 return( mbedtls_to_psa_error( ret ) );
1473}
John Durkop0e005192020-10-31 22:06:54 -07001474#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001475
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001476/** Retrieve all the publicly-accessible attributes of a key.
1477 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001478psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1479 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001480{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001481 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001482 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001483
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001484 psa_reset_key_attributes( attributes );
1485
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001486 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001487 if( status != PSA_SUCCESS )
1488 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001489
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001490 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001491 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1492 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1493
1494#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1495 if( psa_key_slot_is_external( slot ) )
1496 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1497#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001498
Gilles Peskine8e338702019-07-30 20:06:31 +02001499 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001500 {
John Durkop0e005192020-10-31 22:06:54 -07001501#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1502 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001503 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001504 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001505#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001506 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001507 * is not yet implemented.
1508 * https://github.com/ARMmbed/mbed-crypto/issues/216
1509 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001510 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001511 break;
1512#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001513 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001514 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001515
Steven Cooreman4fed4552020-08-03 14:46:03 +02001516 status = psa_load_rsa_representation( slot->attr.type,
1517 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001518 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001519 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001520 if( status != PSA_SUCCESS )
1521 break;
1522
Steven Cooremana2371e52020-07-28 14:30:39 +02001523 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001524 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001525 mbedtls_rsa_free( rsa );
1526 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001527 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001528 break;
John Durkop0e005192020-10-31 22:06:54 -07001529#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001530 default:
1531 /* Nothing else to do. */
1532 break;
1533 }
1534
1535 if( status != PSA_SUCCESS )
1536 psa_reset_key_attributes( attributes );
1537 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001538}
1539
Gilles Peskinec8000c02019-08-02 20:15:51 +02001540#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1541psa_status_t psa_get_key_slot_number(
1542 const psa_key_attributes_t *attributes,
1543 psa_key_slot_number_t *slot_number )
1544{
1545 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1546 {
1547 *slot_number = attributes->slot_number;
1548 return( PSA_SUCCESS );
1549 }
1550 else
1551 return( PSA_ERROR_INVALID_ARGUMENT );
1552}
1553#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1554
Steven Cooremana01795d2020-07-24 22:48:15 +02001555static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1556 uint8_t *data,
1557 size_t data_size,
1558 size_t *data_length )
1559{
1560 if( slot->data.key.bytes > data_size )
1561 return( PSA_ERROR_BUFFER_TOO_SMALL );
1562 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1563 memset( data + slot->data.key.bytes, 0,
1564 data_size - slot->data.key.bytes );
1565 *data_length = slot->data.key.bytes;
1566 return( PSA_SUCCESS );
1567}
1568
Gilles Peskinef603c712019-01-19 13:40:11 +01001569static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1570 uint8_t *data,
1571 size_t data_size,
1572 size_t *data_length,
1573 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001574{
Gilles Peskine5d309672019-07-12 23:47:28 +02001575#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1576 const psa_drv_se_t *drv;
1577 psa_drv_se_context_t *drv_context;
1578#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1579
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001580 *data_length = 0;
1581
Gilles Peskine8e338702019-07-30 20:06:31 +02001582 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001583 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001584
Gilles Peskinef9168942019-09-12 19:20:29 +02001585 /* Reject a zero-length output buffer now, since this can never be a
1586 * valid key representation. This way we know that data must be a valid
1587 * pointer and we can do things like memset(data, ..., data_size). */
1588 if( data_size == 0 )
1589 return( PSA_ERROR_BUFFER_TOO_SMALL );
1590
Gilles Peskine5d309672019-07-12 23:47:28 +02001591#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001592 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001593 {
1594 psa_drv_se_export_key_t method;
1595 if( drv->key_management == NULL )
1596 return( PSA_ERROR_NOT_SUPPORTED );
1597 method = ( export_public_key ?
1598 drv->key_management->p_export_public :
1599 drv->key_management->p_export );
1600 if( method == NULL )
1601 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001602 return( method( drv_context,
1603 slot->data.se.slot_number,
1604 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001605 }
1606#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1607
Gilles Peskine8e338702019-07-30 20:06:31 +02001608 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001609 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001610 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001611 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001612 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1613 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001614 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001615 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001616 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001617 /* Exporting public -> public */
1618 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1619 }
1620 else if( !export_public_key )
1621 {
1622 /* Exporting private -> private */
1623 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1624 }
1625 /* Need to export the public part of a private key,
1626 * so conversion is needed */
1627 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1628 {
John Durkop0e005192020-10-31 22:06:54 -07001629#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1630 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001631 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001632 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001633 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001634 slot->data.key.data,
1635 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001636 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001637 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001638 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001639
Steven Cooreman560c28a2020-07-24 23:20:24 +02001640 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001641 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001642 data,
1643 data_size,
1644 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001645
Steven Cooremana2371e52020-07-28 14:30:39 +02001646 mbedtls_rsa_free( rsa );
1647 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001648
Steven Cooreman560c28a2020-07-24 23:20:24 +02001649 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001650#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001651 /* We don't know how to convert a private RSA key to public. */
1652 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop0e005192020-10-31 22:06:54 -07001653#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001654 }
1655 else
Moran Pekera998bc62018-04-16 18:16:20 +03001656 {
John Durkopf4c4cb02020-10-28 20:09:55 -07001657#if defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001658 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001659 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001660 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001661 slot->data.key.data,
1662 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001663 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001664 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001665 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001666
1667 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1668 PSA_KEY_TYPE_ECC_GET_FAMILY(
1669 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001670 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001671 data,
1672 data_size,
1673 data_length );
1674
Steven Cooremana2371e52020-07-28 14:30:39 +02001675 mbedtls_ecp_keypair_free( ecp );
1676 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001677 return( status );
1678#else
1679 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001680 return( PSA_ERROR_NOT_SUPPORTED );
John Durkopf4c4cb02020-10-28 20:09:55 -07001681#endif /* defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001682 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001683 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001684 else
1685 {
1686 /* This shouldn't happen in the reference implementation, but
1687 it is valid for a special-purpose implementation to omit
1688 support for exporting certain key types. */
1689 return( PSA_ERROR_NOT_SUPPORTED );
1690 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001691}
1692
Gilles Peskinec5487a82018-12-03 18:08:14 +01001693psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001694 uint8_t *data,
1695 size_t data_size,
1696 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001697{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001698 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001699 psa_status_t status;
1700
1701 /* Set the key to empty now, so that even when there are errors, we always
1702 * set data_length to a value between 0 and data_size. On error, setting
1703 * the key to empty is a good choice because an empty key representation is
1704 * unlikely to be accepted anywhere. */
1705 *data_length = 0;
1706
1707 /* Export requires the EXPORT flag. There is an exception for public keys,
1708 * which don't require any flag, but psa_get_key_from_slot takes
1709 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001710 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001711 if( status != PSA_SUCCESS )
1712 return( status );
1713 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001714 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001715}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001716
Gilles Peskinec5487a82018-12-03 18:08:14 +01001717psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001718 uint8_t *data,
1719 size_t data_size,
1720 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001721{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001722 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001723 psa_status_t status;
1724
1725 /* Set the key to empty now, so that even when there are errors, we always
1726 * set data_length to a value between 0 and data_size. On error, setting
1727 * the key to empty is a good choice because an empty key representation is
1728 * unlikely to be accepted anywhere. */
1729 *data_length = 0;
1730
1731 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001732 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001733 if( status != PSA_SUCCESS )
1734 return( status );
1735 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001736 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001737}
1738
Gilles Peskine91e8c332019-08-02 19:19:39 +02001739#if defined(static_assert)
1740static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1741 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001742static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001743 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001744static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001745 "One or more key attribute flag is listed as both internal-only and external-only" );
1746#endif
1747
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001748/** Validate that a key policy is internally well-formed.
1749 *
1750 * This function only rejects invalid policies. It does not validate the
1751 * consistency of the policy with respect to other attributes of the key
1752 * such as the key type.
1753 */
1754static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001755{
1756 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001757 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001758 PSA_KEY_USAGE_ENCRYPT |
1759 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001760 PSA_KEY_USAGE_SIGN_HASH |
1761 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001762 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1763 return( PSA_ERROR_INVALID_ARGUMENT );
1764
Gilles Peskine4747d192019-04-17 15:05:45 +02001765 return( PSA_SUCCESS );
1766}
1767
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001768/** Validate the internal consistency of key attributes.
1769 *
1770 * This function only rejects invalid attribute values. If does not
1771 * validate the consistency of the attributes with any key data that may
1772 * be involved in the creation of the key.
1773 *
1774 * Call this function early in the key creation process.
1775 *
1776 * \param[in] attributes Key attributes for the new key.
1777 * \param[out] p_drv On any return, the driver for the key, if any.
1778 * NULL for a transparent key.
1779 *
1780 */
1781static psa_status_t psa_validate_key_attributes(
1782 const psa_key_attributes_t *attributes,
1783 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001784{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001785 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001786
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001787 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1788 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001789 if( status != PSA_SUCCESS )
1790 return( status );
1791
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001792 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1793 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001794 if( status != PSA_SUCCESS )
1795 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001796
1797 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001798 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001799 return( status );
1800
1801 /* Refuse to create overly large keys.
1802 * Note that this doesn't trigger on import if the attributes don't
1803 * explicitly specify a size (so psa_get_key_bits returns 0), so
1804 * psa_import_key() needs its own checks. */
1805 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1806 return( PSA_ERROR_NOT_SUPPORTED );
1807
Gilles Peskine91e8c332019-08-02 19:19:39 +02001808 /* Reject invalid flags. These should not be reachable through the API. */
1809 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1810 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1811 return( PSA_ERROR_INVALID_ARGUMENT );
1812
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001813 return( PSA_SUCCESS );
1814}
1815
Gilles Peskine4747d192019-04-17 15:05:45 +02001816/** Prepare a key slot to receive key material.
1817 *
1818 * This function allocates a key slot and sets its metadata.
1819 *
1820 * If this function fails, call psa_fail_key_creation().
1821 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001822 * This function is intended to be used as follows:
1823 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1824 * it with the specified attributes, and assign it a handle.
1825 * -# Populate the slot with the key material.
1826 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1827 * In case of failure at any step, stop the sequence and call
1828 * psa_fail_key_creation().
1829 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001830 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001831 * \param[in] attributes Key attributes for the new key.
1832 * \param[out] handle On success, a handle for the allocated slot.
1833 * \param[out] p_slot On success, a pointer to the prepared slot.
1834 * \param[out] p_drv On any return, the driver for the key, if any.
1835 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001836 *
1837 * \retval #PSA_SUCCESS
1838 * The key slot is ready to receive key material.
1839 * \return If this function fails, the key slot is an invalid state.
1840 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001841 */
1842static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001843 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001844 const psa_key_attributes_t *attributes,
1845 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001846 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001847 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001848{
1849 psa_status_t status;
1850 psa_key_slot_t *slot;
1851
Gilles Peskinedf179142019-07-15 22:02:14 +02001852 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001853 *p_drv = NULL;
1854
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001855 status = psa_validate_key_attributes( attributes, p_drv );
1856 if( status != PSA_SUCCESS )
1857 return( status );
1858
Gilles Peskineedbed562019-08-07 18:19:59 +02001859 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001860 if( status != PSA_SUCCESS )
1861 return( status );
1862 slot = *p_slot;
1863
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001864 /* We're storing the declared bit-size of the key. It's up to each
1865 * creation mechanism to verify that this information is correct.
1866 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001867 * an input (generate, device) but not for those where the bit-size
1868 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001869
1870 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001871
Gilles Peskine91e8c332019-08-02 19:19:39 +02001872 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001873 * external-only flags, query `attributes`. Thanks to the check
1874 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001875 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001876 * may have set. */
1877 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001878
Gilles Peskinecbaff462019-07-12 23:46:04 +02001879#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001880 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001881 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001882 * create the key file in internal storage, create the
1883 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001884 * persistent data. This is done by starting a transaction that will
1885 * encompass these three actions.
1886 * For registering a volatile key, we just need to find an appropriate
1887 * slot number inside the SE. Since the key is designated volatile, creating
1888 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001889 /* The first thing to do is to find a slot number for the new key.
1890 * We save the slot number in persistent storage as part of the
1891 * transaction data. It will be needed to recover if the power
1892 * fails during the key creation process, to clean up on the secure
1893 * element side after restarting. Obtaining a slot number from the
1894 * secure element driver updates its persistent state, but we do not yet
1895 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001896 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001897 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001898 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001899 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001900 &slot->data.se.slot_number );
1901 if( status != PSA_SUCCESS )
1902 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001903
1904 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001905 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001906 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1907 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1908 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1909 psa_crypto_transaction.key.id = slot->attr.id;
1910 status = psa_crypto_save_transaction( );
1911 if( status != PSA_SUCCESS )
1912 {
1913 (void) psa_crypto_stop_transaction( );
1914 return( status );
1915 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001916 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001917 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001918
1919 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1920 {
1921 /* Key registration only makes sense with a secure element. */
1922 return( PSA_ERROR_INVALID_ARGUMENT );
1923 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001924#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1925
Darryl Green0c6575a2018-11-07 16:05:30 +00001926 return( status );
1927}
Gilles Peskine4747d192019-04-17 15:05:45 +02001928
1929/** Finalize the creation of a key once its key material has been set.
1930 *
1931 * This entails writing the key to persistent storage.
1932 *
1933 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001934 * See the documentation of psa_start_key_creation() for the intended use
1935 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001936 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001937 * \param[in,out] slot Pointer to the slot with key material.
1938 * \param[in] driver The secure element driver for the key,
1939 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001940 *
1941 * \retval #PSA_SUCCESS
1942 * The key was successfully created. The handle is now valid.
1943 * \return If this function fails, the key slot is an invalid state.
1944 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001945 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001946static psa_status_t psa_finish_key_creation(
1947 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001948 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001949{
1950 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001951 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001952 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001953
1954#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001955 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001956 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001957#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1958 if( driver != NULL )
1959 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001960 psa_se_key_data_storage_t data;
1961#if defined(static_assert)
1962 static_assert( sizeof( slot->data.se.slot_number ) ==
1963 sizeof( data.slot_number ),
1964 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001965#endif
1966 memcpy( &data.slot_number, &slot->data.se.slot_number,
1967 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001968 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001969 (uint8_t*) &data,
1970 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001971 }
1972 else
1973#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1974 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001975 /* Key material is saved in export representation in the slot, so
1976 * just pass the slot buffer for storage. */
1977 status = psa_save_persistent_key( &slot->attr,
1978 slot->data.key.data,
1979 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001980 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001981 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001982#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1983
Gilles Peskinecbaff462019-07-12 23:46:04 +02001984#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001985 /* Finish the transaction for a key creation. This does not
1986 * happen when registering an existing key. Detect this case
1987 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001988 * creation of a persistent key in a secure element requires a transaction,
1989 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001990 if( driver != NULL &&
1991 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001992 {
1993 status = psa_save_se_persistent_data( driver );
1994 if( status != PSA_SUCCESS )
1995 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001996 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001997 return( status );
1998 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001999 status = psa_crypto_stop_transaction( );
2000 if( status != PSA_SUCCESS )
2001 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002002 }
2003#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2004
Gilles Peskine4747d192019-04-17 15:05:45 +02002005 return( status );
2006}
2007
2008/** Abort the creation of a key.
2009 *
2010 * You may call this function after calling psa_start_key_creation(),
2011 * or after psa_finish_key_creation() fails. In other circumstances, this
2012 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002013 * See the documentation of psa_start_key_creation() for the intended use
2014 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002015 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002016 * \param[in,out] slot Pointer to the slot with key material.
2017 * \param[in] driver The secure element driver for the key,
2018 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002019 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002020static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002021 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002022{
Gilles Peskine011e4282019-06-26 18:34:38 +02002023 (void) driver;
2024
Gilles Peskine4747d192019-04-17 15:05:45 +02002025 if( slot == NULL )
2026 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002027
2028#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002029 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002030 * element, and the failure happened later (when saving metadata
2031 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002032 * element.
2033 * https://github.com/ARMmbed/mbed-crypto/issues/217
2034 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002035
Gilles Peskined7729582019-08-05 15:55:54 +02002036 /* Abort the ongoing transaction if any (there may not be one if
2037 * the creation process failed before starting one, or if the
2038 * key creation is a registration of a key in a secure element).
2039 * Earlier functions must already have done what it takes to undo any
2040 * partial creation. All that's left is to update the transaction data
2041 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002042 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002043#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2044
Gilles Peskine4747d192019-04-17 15:05:45 +02002045 psa_wipe_key_slot( slot );
2046}
2047
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002048/** Validate optional attributes during key creation.
2049 *
2050 * Some key attributes are optional during key creation. If they are
2051 * specified in the attributes structure, check that they are consistent
2052 * with the data in the slot.
2053 *
2054 * This function should be called near the end of key creation, after
2055 * the slot in memory is fully populated but before saving persistent data.
2056 */
2057static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002058 const psa_key_slot_t *slot,
2059 const psa_key_attributes_t *attributes )
2060{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002061 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002062 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002063 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002064 return( PSA_ERROR_INVALID_ARGUMENT );
2065 }
2066
2067 if( attributes->domain_parameters_size != 0 )
2068 {
John Durkop0e005192020-10-31 22:06:54 -07002069#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2070 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002071 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002072 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002073 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002074 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002076
Steven Cooreman7f391872020-07-30 14:57:44 +02002077 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002078 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002079 slot->data.key.data,
2080 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002081 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002082 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002083 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002084
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002085 mbedtls_mpi_init( &actual );
2086 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002087 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002088 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002089 mbedtls_rsa_free( rsa );
2090 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002091 if( ret != 0 )
2092 goto rsa_exit;
2093 ret = mbedtls_mpi_read_binary( &required,
2094 attributes->domain_parameters,
2095 attributes->domain_parameters_size );
2096 if( ret != 0 )
2097 goto rsa_exit;
2098 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2099 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2100 rsa_exit:
2101 mbedtls_mpi_free( &actual );
2102 mbedtls_mpi_free( &required );
2103 if( ret != 0)
2104 return( mbedtls_to_psa_error( ret ) );
2105 }
2106 else
John Durkop0e005192020-10-31 22:06:54 -07002107#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002108 {
2109 return( PSA_ERROR_INVALID_ARGUMENT );
2110 }
2111 }
2112
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002113 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002114 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002115 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002116 return( PSA_ERROR_INVALID_ARGUMENT );
2117 }
2118
2119 return( PSA_SUCCESS );
2120}
2121
Gilles Peskine4747d192019-04-17 15:05:45 +02002122psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002123 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002124 size_t data_length,
2125 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002126{
2127 psa_status_t status;
2128 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002129 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002130
Gilles Peskine0f84d622019-09-12 19:03:13 +02002131 /* Reject zero-length symmetric keys (including raw data key objects).
2132 * This also rejects any key which might be encoded as an empty string,
2133 * which is never valid. */
2134 if( data_length == 0 )
2135 return( PSA_ERROR_INVALID_ARGUMENT );
2136
Gilles Peskinedf179142019-07-15 22:02:14 +02002137 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2138 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002139 if( status != PSA_SUCCESS )
2140 goto exit;
2141
Gilles Peskine5d309672019-07-12 23:47:28 +02002142#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2143 if( driver != NULL )
2144 {
2145 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002146 /* The driver should set the number of key bits, however in
2147 * case it doesn't, we initialize bits to an invalid value. */
2148 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002149 if( drv->key_management == NULL ||
2150 drv->key_management->p_import == NULL )
2151 {
2152 status = PSA_ERROR_NOT_SUPPORTED;
2153 goto exit;
2154 }
2155 status = drv->key_management->p_import(
2156 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002157 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002158 &bits );
2159 if( status != PSA_SUCCESS )
2160 goto exit;
2161 if( bits > PSA_MAX_KEY_BITS )
2162 {
2163 status = PSA_ERROR_NOT_SUPPORTED;
2164 goto exit;
2165 }
2166 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002167 }
2168 else
2169#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2170 {
2171 status = psa_import_key_into_slot( slot, data, data_length );
2172 if( status != PSA_SUCCESS )
2173 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002174 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002175 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002176 if( status != PSA_SUCCESS )
2177 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002178
Gilles Peskine011e4282019-06-26 18:34:38 +02002179 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002180exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002181 if( status != PSA_SUCCESS )
2182 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002183 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002184 *handle = 0;
2185 }
2186 return( status );
2187}
2188
Gilles Peskined7729582019-08-05 15:55:54 +02002189#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2190psa_status_t mbedtls_psa_register_se_key(
2191 const psa_key_attributes_t *attributes )
2192{
2193 psa_status_t status;
2194 psa_key_slot_t *slot = NULL;
2195 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002196 psa_key_handle_t handle = 0;
2197
2198 /* Leaving attributes unspecified is not currently supported.
2199 * It could make sense to query the key type and size from the
2200 * secure element, but not all secure elements support this
2201 * and the driver HAL doesn't currently support it. */
2202 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2203 return( PSA_ERROR_NOT_SUPPORTED );
2204 if( psa_get_key_bits( attributes ) == 0 )
2205 return( PSA_ERROR_NOT_SUPPORTED );
2206
2207 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2208 &handle, &slot, &driver );
2209 if( status != PSA_SUCCESS )
2210 goto exit;
2211
Gilles Peskined7729582019-08-05 15:55:54 +02002212 status = psa_finish_key_creation( slot, driver );
2213
2214exit:
2215 if( status != PSA_SUCCESS )
2216 {
2217 psa_fail_key_creation( slot, driver );
2218 }
2219 /* Registration doesn't keep the key in RAM. */
2220 psa_close_key( handle );
2221 return( status );
2222}
2223#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2224
Gilles Peskinef603c712019-01-19 13:40:11 +01002225static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002226 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002227{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002228 psa_status_t status = psa_copy_key_material_into_slot( target,
2229 source->data.key.data,
2230 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002231 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002232 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002233
Steven Cooreman398aee52020-10-13 14:35:45 +02002234 target->attr.type = source->attr.type;
2235 target->attr.bits = source->attr.bits;
2236
2237 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002238}
2239
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002240psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2241 const psa_key_attributes_t *specified_attributes,
2242 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002243{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002244 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002245 psa_key_slot_t *source_slot = NULL;
2246 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002247 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002248 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002249
Gilles Peskine28f8f302019-07-24 13:30:31 +02002250 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002251 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002252 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002253 goto exit;
2254
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002255 status = psa_validate_optional_attributes( source_slot,
2256 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002257 if( status != PSA_SUCCESS )
2258 goto exit;
2259
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002260 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002261 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002262 if( status != PSA_SUCCESS )
2263 goto exit;
2264
Gilles Peskinedf179142019-07-15 22:02:14 +02002265 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2266 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002267 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002268 if( status != PSA_SUCCESS )
2269 goto exit;
2270
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002271#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2272 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002273 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002274 /* Copying to a secure element is not implemented yet. */
2275 status = PSA_ERROR_NOT_SUPPORTED;
2276 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002277 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002278#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002279
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002280 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002281 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002282 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002283
Gilles Peskine011e4282019-06-26 18:34:38 +02002284 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002285exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002286 if( status != PSA_SUCCESS )
2287 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002288 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002289 *target_handle = 0;
2290 }
2291 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002292}
2293
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002294
2295
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002296/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002297/* Message digests */
2298/****************************************************************/
2299
John Durkop0e005192020-10-31 22:06:54 -07002300#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
2301 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2302 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2303 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
2304 (defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && \
2305 (defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY)))
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002306static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002307{
2308 switch( alg )
2309 {
2310#if defined(MBEDTLS_MD2_C)
2311 case PSA_ALG_MD2:
2312 return( &mbedtls_md2_info );
2313#endif
2314#if defined(MBEDTLS_MD4_C)
2315 case PSA_ALG_MD4:
2316 return( &mbedtls_md4_info );
2317#endif
2318#if defined(MBEDTLS_MD5_C)
2319 case PSA_ALG_MD5:
2320 return( &mbedtls_md5_info );
2321#endif
2322#if defined(MBEDTLS_RIPEMD160_C)
2323 case PSA_ALG_RIPEMD160:
2324 return( &mbedtls_ripemd160_info );
2325#endif
2326#if defined(MBEDTLS_SHA1_C)
2327 case PSA_ALG_SHA_1:
2328 return( &mbedtls_sha1_info );
2329#endif
2330#if defined(MBEDTLS_SHA256_C)
2331 case PSA_ALG_SHA_224:
2332 return( &mbedtls_sha224_info );
2333 case PSA_ALG_SHA_256:
2334 return( &mbedtls_sha256_info );
2335#endif
2336#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002337#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002338 case PSA_ALG_SHA_384:
2339 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002340#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002341 case PSA_ALG_SHA_512:
2342 return( &mbedtls_sha512_info );
2343#endif
2344 default:
2345 return( NULL );
2346 }
2347}
John Durkop0e005192020-10-31 22:06:54 -07002348#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || (defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && (defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY))) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002349
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002350psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2351{
2352 switch( operation->alg )
2353 {
Gilles Peskine81736312018-06-26 15:04:31 +02002354 case 0:
2355 /* The object has (apparently) been initialized but it is not
2356 * in use. It's ok to call abort on such an object, and there's
2357 * nothing to do. */
2358 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002359#if defined(MBEDTLS_MD2_C)
2360 case PSA_ALG_MD2:
2361 mbedtls_md2_free( &operation->ctx.md2 );
2362 break;
2363#endif
2364#if defined(MBEDTLS_MD4_C)
2365 case PSA_ALG_MD4:
2366 mbedtls_md4_free( &operation->ctx.md4 );
2367 break;
2368#endif
2369#if defined(MBEDTLS_MD5_C)
2370 case PSA_ALG_MD5:
2371 mbedtls_md5_free( &operation->ctx.md5 );
2372 break;
2373#endif
2374#if defined(MBEDTLS_RIPEMD160_C)
2375 case PSA_ALG_RIPEMD160:
2376 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2377 break;
2378#endif
2379#if defined(MBEDTLS_SHA1_C)
2380 case PSA_ALG_SHA_1:
2381 mbedtls_sha1_free( &operation->ctx.sha1 );
2382 break;
2383#endif
2384#if defined(MBEDTLS_SHA256_C)
2385 case PSA_ALG_SHA_224:
2386 case PSA_ALG_SHA_256:
2387 mbedtls_sha256_free( &operation->ctx.sha256 );
2388 break;
2389#endif
2390#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002391#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002392 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002393#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002394 case PSA_ALG_SHA_512:
2395 mbedtls_sha512_free( &operation->ctx.sha512 );
2396 break;
2397#endif
2398 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002399 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002400 }
2401 operation->alg = 0;
2402 return( PSA_SUCCESS );
2403}
2404
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002405psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002406 psa_algorithm_t alg )
2407{
Janos Follath24eed8d2019-11-22 13:21:35 +00002408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002409
2410 /* A context must be freshly initialized before it can be set up. */
2411 if( operation->alg != 0 )
2412 {
2413 return( PSA_ERROR_BAD_STATE );
2414 }
2415
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002416 switch( alg )
2417 {
2418#if defined(MBEDTLS_MD2_C)
2419 case PSA_ALG_MD2:
2420 mbedtls_md2_init( &operation->ctx.md2 );
2421 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2422 break;
2423#endif
2424#if defined(MBEDTLS_MD4_C)
2425 case PSA_ALG_MD4:
2426 mbedtls_md4_init( &operation->ctx.md4 );
2427 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2428 break;
2429#endif
2430#if defined(MBEDTLS_MD5_C)
2431 case PSA_ALG_MD5:
2432 mbedtls_md5_init( &operation->ctx.md5 );
2433 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2434 break;
2435#endif
2436#if defined(MBEDTLS_RIPEMD160_C)
2437 case PSA_ALG_RIPEMD160:
2438 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2439 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2440 break;
2441#endif
2442#if defined(MBEDTLS_SHA1_C)
2443 case PSA_ALG_SHA_1:
2444 mbedtls_sha1_init( &operation->ctx.sha1 );
2445 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2446 break;
2447#endif
2448#if defined(MBEDTLS_SHA256_C)
2449 case PSA_ALG_SHA_224:
2450 mbedtls_sha256_init( &operation->ctx.sha256 );
2451 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2452 break;
2453 case PSA_ALG_SHA_256:
2454 mbedtls_sha256_init( &operation->ctx.sha256 );
2455 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2456 break;
2457#endif
2458#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002459#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002460 case PSA_ALG_SHA_384:
2461 mbedtls_sha512_init( &operation->ctx.sha512 );
2462 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2463 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002464#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002465 case PSA_ALG_SHA_512:
2466 mbedtls_sha512_init( &operation->ctx.sha512 );
2467 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2468 break;
2469#endif
2470 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002471 return( PSA_ALG_IS_HASH( alg ) ?
2472 PSA_ERROR_NOT_SUPPORTED :
2473 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002474 }
2475 if( ret == 0 )
2476 operation->alg = alg;
2477 else
2478 psa_hash_abort( operation );
2479 return( mbedtls_to_psa_error( ret ) );
2480}
2481
2482psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2483 const uint8_t *input,
2484 size_t input_length )
2485{
Janos Follath24eed8d2019-11-22 13:21:35 +00002486 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002487
2488 /* Don't require hash implementations to behave correctly on a
2489 * zero-length input, which may have an invalid pointer. */
2490 if( input_length == 0 )
2491 return( PSA_SUCCESS );
2492
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002493 switch( operation->alg )
2494 {
2495#if defined(MBEDTLS_MD2_C)
2496 case PSA_ALG_MD2:
2497 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2498 input, input_length );
2499 break;
2500#endif
2501#if defined(MBEDTLS_MD4_C)
2502 case PSA_ALG_MD4:
2503 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2504 input, input_length );
2505 break;
2506#endif
2507#if defined(MBEDTLS_MD5_C)
2508 case PSA_ALG_MD5:
2509 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2510 input, input_length );
2511 break;
2512#endif
2513#if defined(MBEDTLS_RIPEMD160_C)
2514 case PSA_ALG_RIPEMD160:
2515 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2516 input, input_length );
2517 break;
2518#endif
2519#if defined(MBEDTLS_SHA1_C)
2520 case PSA_ALG_SHA_1:
2521 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2522 input, input_length );
2523 break;
2524#endif
2525#if defined(MBEDTLS_SHA256_C)
2526 case PSA_ALG_SHA_224:
2527 case PSA_ALG_SHA_256:
2528 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2529 input, input_length );
2530 break;
2531#endif
2532#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002533#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002534 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002535#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002536 case PSA_ALG_SHA_512:
2537 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2538 input, input_length );
2539 break;
2540#endif
2541 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002542 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002543 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002544
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002545 if( ret != 0 )
2546 psa_hash_abort( operation );
2547 return( mbedtls_to_psa_error( ret ) );
2548}
2549
2550psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2551 uint8_t *hash,
2552 size_t hash_size,
2553 size_t *hash_length )
2554{
itayzafrir40835d42018-08-02 13:14:17 +03002555 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002557 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002558
2559 /* Fill the output buffer with something that isn't a valid hash
2560 * (barring an attack on the hash and deliberately-crafted input),
2561 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002562 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002563 /* If hash_size is 0 then hash may be NULL and then the
2564 * call to memset would have undefined behavior. */
2565 if( hash_size != 0 )
2566 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002567
2568 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002569 {
2570 status = PSA_ERROR_BUFFER_TOO_SMALL;
2571 goto exit;
2572 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002573
2574 switch( operation->alg )
2575 {
2576#if defined(MBEDTLS_MD2_C)
2577 case PSA_ALG_MD2:
2578 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2579 break;
2580#endif
2581#if defined(MBEDTLS_MD4_C)
2582 case PSA_ALG_MD4:
2583 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2584 break;
2585#endif
2586#if defined(MBEDTLS_MD5_C)
2587 case PSA_ALG_MD5:
2588 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2589 break;
2590#endif
2591#if defined(MBEDTLS_RIPEMD160_C)
2592 case PSA_ALG_RIPEMD160:
2593 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2594 break;
2595#endif
2596#if defined(MBEDTLS_SHA1_C)
2597 case PSA_ALG_SHA_1:
2598 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2599 break;
2600#endif
2601#if defined(MBEDTLS_SHA256_C)
2602 case PSA_ALG_SHA_224:
2603 case PSA_ALG_SHA_256:
2604 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2605 break;
2606#endif
2607#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002608#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002609 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002610#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002611 case PSA_ALG_SHA_512:
2612 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2613 break;
2614#endif
2615 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002616 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002617 }
itayzafrir40835d42018-08-02 13:14:17 +03002618 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002619
itayzafrir40835d42018-08-02 13:14:17 +03002620exit:
2621 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002622 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002623 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002624 return( psa_hash_abort( operation ) );
2625 }
2626 else
2627 {
2628 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002629 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002630 }
2631}
2632
Gilles Peskine2d277862018-06-18 15:41:12 +02002633psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2634 const uint8_t *hash,
2635 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002636{
2637 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2638 size_t actual_hash_length;
2639 psa_status_t status = psa_hash_finish( operation,
2640 actual_hash, sizeof( actual_hash ),
2641 &actual_hash_length );
2642 if( status != PSA_SUCCESS )
2643 return( status );
2644 if( actual_hash_length != hash_length )
2645 return( PSA_ERROR_INVALID_SIGNATURE );
2646 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2647 return( PSA_ERROR_INVALID_SIGNATURE );
2648 return( PSA_SUCCESS );
2649}
2650
Gilles Peskine0a749c82019-11-28 19:33:58 +01002651psa_status_t psa_hash_compute( psa_algorithm_t alg,
2652 const uint8_t *input, size_t input_length,
2653 uint8_t *hash, size_t hash_size,
2654 size_t *hash_length )
2655{
2656 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2657 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2658
2659 *hash_length = hash_size;
2660 status = psa_hash_setup( &operation, alg );
2661 if( status != PSA_SUCCESS )
2662 goto exit;
2663 status = psa_hash_update( &operation, input, input_length );
2664 if( status != PSA_SUCCESS )
2665 goto exit;
2666 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2667 if( status != PSA_SUCCESS )
2668 goto exit;
2669
2670exit:
2671 if( status == PSA_SUCCESS )
2672 status = psa_hash_abort( &operation );
2673 else
2674 psa_hash_abort( &operation );
2675 return( status );
2676}
2677
2678psa_status_t psa_hash_compare( psa_algorithm_t alg,
2679 const uint8_t *input, size_t input_length,
2680 const uint8_t *hash, size_t hash_length )
2681{
2682 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2683 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2684
2685 status = psa_hash_setup( &operation, alg );
2686 if( status != PSA_SUCCESS )
2687 goto exit;
2688 status = psa_hash_update( &operation, input, input_length );
2689 if( status != PSA_SUCCESS )
2690 goto exit;
2691 status = psa_hash_verify( &operation, hash, hash_length );
2692 if( status != PSA_SUCCESS )
2693 goto exit;
2694
2695exit:
2696 if( status == PSA_SUCCESS )
2697 status = psa_hash_abort( &operation );
2698 else
2699 psa_hash_abort( &operation );
2700 return( status );
2701}
2702
Gilles Peskineeb35d782019-01-22 17:56:16 +01002703psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2704 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002705{
2706 if( target_operation->alg != 0 )
2707 return( PSA_ERROR_BAD_STATE );
2708
2709 switch( source_operation->alg )
2710 {
2711 case 0:
2712 return( PSA_ERROR_BAD_STATE );
2713#if defined(MBEDTLS_MD2_C)
2714 case PSA_ALG_MD2:
2715 mbedtls_md2_clone( &target_operation->ctx.md2,
2716 &source_operation->ctx.md2 );
2717 break;
2718#endif
2719#if defined(MBEDTLS_MD4_C)
2720 case PSA_ALG_MD4:
2721 mbedtls_md4_clone( &target_operation->ctx.md4,
2722 &source_operation->ctx.md4 );
2723 break;
2724#endif
2725#if defined(MBEDTLS_MD5_C)
2726 case PSA_ALG_MD5:
2727 mbedtls_md5_clone( &target_operation->ctx.md5,
2728 &source_operation->ctx.md5 );
2729 break;
2730#endif
2731#if defined(MBEDTLS_RIPEMD160_C)
2732 case PSA_ALG_RIPEMD160:
2733 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2734 &source_operation->ctx.ripemd160 );
2735 break;
2736#endif
2737#if defined(MBEDTLS_SHA1_C)
2738 case PSA_ALG_SHA_1:
2739 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2740 &source_operation->ctx.sha1 );
2741 break;
2742#endif
2743#if defined(MBEDTLS_SHA256_C)
2744 case PSA_ALG_SHA_224:
2745 case PSA_ALG_SHA_256:
2746 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2747 &source_operation->ctx.sha256 );
2748 break;
2749#endif
2750#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002751#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002752 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002753#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002754 case PSA_ALG_SHA_512:
2755 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2756 &source_operation->ctx.sha512 );
2757 break;
2758#endif
2759 default:
2760 return( PSA_ERROR_NOT_SUPPORTED );
2761 }
2762
2763 target_operation->alg = source_operation->alg;
2764 return( PSA_SUCCESS );
2765}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002766
2767
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002768/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002769/* MAC */
2770/****************************************************************/
2771
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002772static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002773 psa_algorithm_t alg,
2774 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002775 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002776 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002777{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002778 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002779 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002780
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002781 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002782 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002783
Gilles Peskine8c9def32018-02-08 10:02:12 +01002784 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2785 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002786 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002787 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002788 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002789 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002790 mode = MBEDTLS_MODE_STREAM;
2791 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002792 case PSA_ALG_CTR:
2793 mode = MBEDTLS_MODE_CTR;
2794 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002795 case PSA_ALG_CFB:
2796 mode = MBEDTLS_MODE_CFB;
2797 break;
2798 case PSA_ALG_OFB:
2799 mode = MBEDTLS_MODE_OFB;
2800 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002801 case PSA_ALG_ECB_NO_PADDING:
2802 mode = MBEDTLS_MODE_ECB;
2803 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002804 case PSA_ALG_CBC_NO_PADDING:
2805 mode = MBEDTLS_MODE_CBC;
2806 break;
2807 case PSA_ALG_CBC_PKCS7:
2808 mode = MBEDTLS_MODE_CBC;
2809 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002810 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002811 mode = MBEDTLS_MODE_CCM;
2812 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002813 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002814 mode = MBEDTLS_MODE_GCM;
2815 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002816 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2817 mode = MBEDTLS_MODE_CHACHAPOLY;
2818 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002819 default:
2820 return( NULL );
2821 }
2822 }
2823 else if( alg == PSA_ALG_CMAC )
2824 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002825 else
2826 return( NULL );
2827
2828 switch( key_type )
2829 {
2830 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002831 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002832 break;
2833 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002834 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2835 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002836 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002837 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002838 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002839 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002840 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2841 * but two-key Triple-DES is functionally three-key Triple-DES
2842 * with K1=K3, so that's how we present it to mbedtls. */
2843 if( key_bits == 128 )
2844 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002845 break;
2846 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002847 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002848 break;
2849 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002850 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002851 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002852 case PSA_KEY_TYPE_CHACHA20:
2853 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2854 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002855 default:
2856 return( NULL );
2857 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002858 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002859 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002860
Jaeden Amero23bbb752018-06-26 14:16:54 +01002861 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2862 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002863}
2864
John Durkopd0321952020-10-29 21:37:36 -07002865#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002866static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002867{
Gilles Peskine2d277862018-06-18 15:41:12 +02002868 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002869 {
2870 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002871 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002872 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002873 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002874 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002875 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002876 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002877 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002878 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002879 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002880 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002881 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002882 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002883 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002884 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002885 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002886 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002887 return( 128 );
2888 default:
2889 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002890 }
2891}
John Durkopd0321952020-10-29 21:37:36 -07002892#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002893
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002894/* Initialize the MAC operation structure. Once this function has been
2895 * called, psa_mac_abort can run and will do the right thing. */
2896static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2897 psa_algorithm_t alg )
2898{
2899 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2900
2901 operation->alg = alg;
2902 operation->key_set = 0;
2903 operation->iv_set = 0;
2904 operation->iv_required = 0;
2905 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002906 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002907
2908#if defined(MBEDTLS_CMAC_C)
2909 if( alg == PSA_ALG_CMAC )
2910 {
2911 operation->iv_required = 0;
2912 mbedtls_cipher_init( &operation->ctx.cmac );
2913 status = PSA_SUCCESS;
2914 }
2915 else
2916#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002917#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002918 if( PSA_ALG_IS_HMAC( operation->alg ) )
2919 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002920 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2921 operation->ctx.hmac.hash_ctx.alg = 0;
2922 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002923 }
2924 else
John Durkopd0321952020-10-29 21:37:36 -07002925#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002926 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002927 if( ! PSA_ALG_IS_MAC( alg ) )
2928 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002929 }
2930
2931 if( status != PSA_SUCCESS )
2932 memset( operation, 0, sizeof( *operation ) );
2933 return( status );
2934}
2935
John Durkopd0321952020-10-29 21:37:36 -07002936#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002937static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2938{
Gilles Peskine3f108122018-12-07 18:14:53 +01002939 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002940 return( psa_hash_abort( &hmac->hash_ctx ) );
2941}
John Durkopd0321952020-10-29 21:37:36 -07002942#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002943
Gilles Peskine8c9def32018-02-08 10:02:12 +01002944psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2945{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002946 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002947 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002948 /* The object has (apparently) been initialized but it is not
2949 * in use. It's ok to call abort on such an object, and there's
2950 * nothing to do. */
2951 return( PSA_SUCCESS );
2952 }
2953 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002954#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002955 if( operation->alg == PSA_ALG_CMAC )
2956 {
2957 mbedtls_cipher_free( &operation->ctx.cmac );
2958 }
2959 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002960#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002961#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002962 if( PSA_ALG_IS_HMAC( operation->alg ) )
2963 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002964 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002965 }
2966 else
John Durkopd0321952020-10-29 21:37:36 -07002967#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002968 {
2969 /* Sanity check (shouldn't happen: operation->alg should
2970 * always have been initialized to a valid value). */
2971 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002972 }
Moran Peker41deec42018-04-04 15:43:05 +03002973
Gilles Peskine8c9def32018-02-08 10:02:12 +01002974 operation->alg = 0;
2975 operation->key_set = 0;
2976 operation->iv_set = 0;
2977 operation->iv_required = 0;
2978 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002979 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002980
Gilles Peskine8c9def32018-02-08 10:02:12 +01002981 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002982
2983bad_state:
2984 /* If abort is called on an uninitialized object, we can't trust
2985 * anything. Wipe the object in case it contains confidential data.
2986 * This may result in a memory leak if a pointer gets overwritten,
2987 * but it's too late to do anything about this. */
2988 memset( operation, 0, sizeof( *operation ) );
2989 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002990}
2991
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002992#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002993static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002994 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002995 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002996 const mbedtls_cipher_info_t *cipher_info )
2997{
Janos Follath24eed8d2019-11-22 13:21:35 +00002998 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002999
3000 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003001
3002 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3003 if( ret != 0 )
3004 return( ret );
3005
3006 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003007 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003008 key_bits );
3009 return( ret );
3010}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003011#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003012
John Durkopd0321952020-10-29 21:37:36 -07003013#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003014static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3015 const uint8_t *key,
3016 size_t key_length,
3017 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003018{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003019 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003020 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003021 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003022 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003023 psa_status_t status;
3024
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003025 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3026 * overflow below. This should never trigger if the hash algorithm
3027 * is implemented correctly. */
3028 /* The size checks against the ipad and opad buffers cannot be written
3029 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3030 * because that triggers -Wlogical-op on GCC 7.3. */
3031 if( block_size > sizeof( ipad ) )
3032 return( PSA_ERROR_NOT_SUPPORTED );
3033 if( block_size > sizeof( hmac->opad ) )
3034 return( PSA_ERROR_NOT_SUPPORTED );
3035 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003036 return( PSA_ERROR_NOT_SUPPORTED );
3037
Gilles Peskined223b522018-06-11 18:12:58 +02003038 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003039 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003040 status = psa_hash_compute( hash_alg, key, key_length,
3041 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003042 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003043 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003044 }
Gilles Peskine96889972018-07-12 17:07:03 +02003045 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3046 * but it is permitted. It is common when HMAC is used in HKDF, for
3047 * example. Don't call `memcpy` in the 0-length because `key` could be
3048 * an invalid pointer which would make the behavior undefined. */
3049 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003050 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003051
Gilles Peskined223b522018-06-11 18:12:58 +02003052 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3053 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003054 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003055 ipad[i] ^= 0x36;
3056 memset( ipad + key_length, 0x36, block_size - key_length );
3057
3058 /* Copy the key material from ipad to opad, flipping the requisite bits,
3059 * and filling the rest of opad with the requisite constant. */
3060 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003061 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3062 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003063
Gilles Peskine01126fa2018-07-12 17:04:55 +02003064 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003065 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003066 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003067
Gilles Peskine01126fa2018-07-12 17:04:55 +02003068 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003069
3070cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003071 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003072
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003073 return( status );
3074}
John Durkopd0321952020-10-29 21:37:36 -07003075#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003076
Gilles Peskine89167cb2018-07-08 20:12:23 +02003077static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003078 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003079 psa_algorithm_t alg,
3080 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003081{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003082 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003083 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003084 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003085 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003086 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003087 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003088 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003089
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003090 /* A context must be freshly initialized before it can be set up. */
3091 if( operation->alg != 0 )
3092 {
3093 return( PSA_ERROR_BAD_STATE );
3094 }
3095
Gilles Peskined911eb72018-08-14 15:18:45 +02003096 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003097 if( status != PSA_SUCCESS )
3098 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003099 if( is_sign )
3100 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003101
Gilles Peskine28f8f302019-07-24 13:30:31 +02003102 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003103 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003104 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003105 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003106
Gilles Peskine8c9def32018-02-08 10:02:12 +01003107#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003108 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003109 {
3110 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003111 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003112 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003113 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003114 if( cipher_info == NULL )
3115 {
3116 status = PSA_ERROR_NOT_SUPPORTED;
3117 goto exit;
3118 }
3119 operation->mac_size = cipher_info->block_size;
3120 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3121 status = mbedtls_to_psa_error( ret );
3122 }
3123 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003124#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003125#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003126 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003127 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003128 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003129 if( hash_alg == 0 )
3130 {
3131 status = PSA_ERROR_NOT_SUPPORTED;
3132 goto exit;
3133 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003134
3135 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3136 /* Sanity check. This shouldn't fail on a valid configuration. */
3137 if( operation->mac_size == 0 ||
3138 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3139 {
3140 status = PSA_ERROR_NOT_SUPPORTED;
3141 goto exit;
3142 }
3143
Gilles Peskine8e338702019-07-30 20:06:31 +02003144 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003145 {
3146 status = PSA_ERROR_INVALID_ARGUMENT;
3147 goto exit;
3148 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003149
Gilles Peskine01126fa2018-07-12 17:04:55 +02003150 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003151 slot->data.key.data,
3152 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003153 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003154 }
3155 else
John Durkopd0321952020-10-29 21:37:36 -07003156#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003157 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003158 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003159 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003160 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003161
Gilles Peskined911eb72018-08-14 15:18:45 +02003162 if( truncated == 0 )
3163 {
3164 /* The "normal" case: untruncated algorithm. Nothing to do. */
3165 }
3166 else if( truncated < 4 )
3167 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003168 /* A very short MAC is too short for security since it can be
3169 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3170 * so we make this our minimum, even though 32 bits is still
3171 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003172 status = PSA_ERROR_NOT_SUPPORTED;
3173 }
3174 else if( truncated > operation->mac_size )
3175 {
3176 /* It's impossible to "truncate" to a larger length. */
3177 status = PSA_ERROR_INVALID_ARGUMENT;
3178 }
3179 else
3180 operation->mac_size = truncated;
3181
Gilles Peskinefbfac682018-07-08 20:51:54 +02003182exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003183 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003184 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003185 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003186 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003187 else
3188 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003189 operation->key_set = 1;
3190 }
3191 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003192}
3193
Gilles Peskine89167cb2018-07-08 20:12:23 +02003194psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003195 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003196 psa_algorithm_t alg )
3197{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003198 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003199}
3200
3201psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003202 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003203 psa_algorithm_t alg )
3204{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003205 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003206}
3207
Gilles Peskine8c9def32018-02-08 10:02:12 +01003208psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3209 const uint8_t *input,
3210 size_t input_length )
3211{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003212 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003213 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003214 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003215 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003216 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003217 operation->has_input = 1;
3218
Gilles Peskine8c9def32018-02-08 10:02:12 +01003219#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003220 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003221 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003222 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3223 input, input_length );
3224 status = mbedtls_to_psa_error( ret );
3225 }
3226 else
3227#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003228#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003229 if( PSA_ALG_IS_HMAC( operation->alg ) )
3230 {
3231 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3232 input_length );
3233 }
3234 else
John Durkopd0321952020-10-29 21:37:36 -07003235#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003236 {
3237 /* This shouldn't happen if `operation` was initialized by
3238 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003239 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003240 }
3241
Gilles Peskinefbfac682018-07-08 20:51:54 +02003242 if( status != PSA_SUCCESS )
3243 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003244 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003245}
3246
John Durkopd0321952020-10-29 21:37:36 -07003247#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003248static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3249 uint8_t *mac,
3250 size_t mac_size )
3251{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003252 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003253 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3254 size_t hash_size = 0;
3255 size_t block_size = psa_get_hash_block_size( hash_alg );
3256 psa_status_t status;
3257
Gilles Peskine01126fa2018-07-12 17:04:55 +02003258 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3259 if( status != PSA_SUCCESS )
3260 return( status );
3261 /* From here on, tmp needs to be wiped. */
3262
3263 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3264 if( status != PSA_SUCCESS )
3265 goto exit;
3266
3267 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3268 if( status != PSA_SUCCESS )
3269 goto exit;
3270
3271 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3272 if( status != PSA_SUCCESS )
3273 goto exit;
3274
Gilles Peskined911eb72018-08-14 15:18:45 +02003275 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3276 if( status != PSA_SUCCESS )
3277 goto exit;
3278
3279 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003280
3281exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003282 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003283 return( status );
3284}
John Durkopd0321952020-10-29 21:37:36 -07003285#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC || MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003286
mohammad16036df908f2018-04-02 08:34:15 -07003287static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003288 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003289 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003290{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003291 if( ! operation->key_set )
3292 return( PSA_ERROR_BAD_STATE );
3293 if( operation->iv_required && ! operation->iv_set )
3294 return( PSA_ERROR_BAD_STATE );
3295
Gilles Peskine8c9def32018-02-08 10:02:12 +01003296 if( mac_size < operation->mac_size )
3297 return( PSA_ERROR_BUFFER_TOO_SMALL );
3298
Gilles Peskine8c9def32018-02-08 10:02:12 +01003299#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003300 if( operation->alg == PSA_ALG_CMAC )
3301 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003302 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3303 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3304 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003305 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003306 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003307 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003308 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003309 else
3310#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003311#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003312 if( PSA_ALG_IS_HMAC( operation->alg ) )
3313 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003314 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003315 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003316 }
3317 else
John Durkopd0321952020-10-29 21:37:36 -07003318#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003319 {
3320 /* This shouldn't happen if `operation` was initialized by
3321 * a setup function. */
3322 return( PSA_ERROR_BAD_STATE );
3323 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003324}
3325
Gilles Peskineacd4be32018-07-08 19:56:25 +02003326psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3327 uint8_t *mac,
3328 size_t mac_size,
3329 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003330{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003331 psa_status_t status;
3332
Jaeden Amero252ef282019-02-15 14:05:35 +00003333 if( operation->alg == 0 )
3334 {
3335 return( PSA_ERROR_BAD_STATE );
3336 }
3337
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003338 /* Fill the output buffer with something that isn't a valid mac
3339 * (barring an attack on the mac and deliberately-crafted input),
3340 * in case the caller doesn't check the return status properly. */
3341 *mac_length = mac_size;
3342 /* If mac_size is 0 then mac may be NULL and then the
3343 * call to memset would have undefined behavior. */
3344 if( mac_size != 0 )
3345 memset( mac, '!', mac_size );
3346
Gilles Peskine89167cb2018-07-08 20:12:23 +02003347 if( ! operation->is_sign )
3348 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003349 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003350 }
mohammad16036df908f2018-04-02 08:34:15 -07003351
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003352 status = psa_mac_finish_internal( operation, mac, mac_size );
3353
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003354 if( status == PSA_SUCCESS )
3355 {
3356 status = psa_mac_abort( operation );
3357 if( status == PSA_SUCCESS )
3358 *mac_length = operation->mac_size;
3359 else
3360 memset( mac, '!', mac_size );
3361 }
3362 else
3363 psa_mac_abort( operation );
3364 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003365}
3366
Gilles Peskineacd4be32018-07-08 19:56:25 +02003367psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3368 const uint8_t *mac,
3369 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003370{
Gilles Peskine828ed142018-06-18 23:25:51 +02003371 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003372 psa_status_t status;
3373
Jaeden Amero252ef282019-02-15 14:05:35 +00003374 if( operation->alg == 0 )
3375 {
3376 return( PSA_ERROR_BAD_STATE );
3377 }
3378
Gilles Peskine89167cb2018-07-08 20:12:23 +02003379 if( operation->is_sign )
3380 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003381 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003382 }
3383 if( operation->mac_size != mac_length )
3384 {
3385 status = PSA_ERROR_INVALID_SIGNATURE;
3386 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003387 }
mohammad16036df908f2018-04-02 08:34:15 -07003388
3389 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003390 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003391 if( status != PSA_SUCCESS )
3392 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003393
3394 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3395 status = PSA_ERROR_INVALID_SIGNATURE;
3396
3397cleanup:
3398 if( status == PSA_SUCCESS )
3399 status = psa_mac_abort( operation );
3400 else
3401 psa_mac_abort( operation );
3402
Gilles Peskine3f108122018-12-07 18:14:53 +01003403 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003404
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003405 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003406}
3407
3408
Gilles Peskine20035e32018-02-03 22:44:14 +01003409
Gilles Peskine20035e32018-02-03 22:44:14 +01003410/****************************************************************/
3411/* Asymmetric cryptography */
3412/****************************************************************/
3413
John Durkop0e005192020-10-31 22:06:54 -07003414#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3415 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3416 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
3417 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003418/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003419 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003420static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3421 size_t hash_length,
3422 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003423{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003424 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003425 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003426 *md_alg = mbedtls_md_get_type( md_info );
3427
3428 /* The Mbed TLS RSA module uses an unsigned int for hash length
3429 * parameters. Validate that it fits so that we don't risk an
3430 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003431#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003432 if( hash_length > UINT_MAX )
3433 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003434#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003435
John Durkop0e005192020-10-31 22:06:54 -07003436#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003437 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3438 * must be correct. */
3439 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3440 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003441 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003442 if( md_info == NULL )
3443 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003444 if( mbedtls_md_get_size( md_info ) != hash_length )
3445 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003446 }
John Durkop0e005192020-10-31 22:06:54 -07003447#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003448
John Durkop0e005192020-10-31 22:06:54 -07003449#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003450 /* PSS requires a hash internally. */
3451 if( PSA_ALG_IS_RSA_PSS( alg ) )
3452 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003453 if( md_info == NULL )
3454 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003455 }
John Durkop0e005192020-10-31 22:06:54 -07003456#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003457
Gilles Peskine61b91d42018-06-08 16:09:36 +02003458 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003459}
3460
John Durkop0e005192020-10-31 22:06:54 -07003461#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003462static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3463 psa_algorithm_t alg,
3464 const uint8_t *hash,
3465 size_t hash_length,
3466 uint8_t *signature,
3467 size_t signature_size,
3468 size_t *signature_length )
3469{
3470 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003472 mbedtls_md_type_t md_alg;
John Durkop0e005192020-10-31 22:06:54 -07003473#if !defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
3474 (void)hash;
3475 (void)signature;
3476#endif /* !MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003477
3478 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3479 if( status != PSA_SUCCESS )
3480 return( status );
3481
Gilles Peskine630a18a2018-06-29 17:49:35 +02003482 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003483 return( PSA_ERROR_BUFFER_TOO_SMALL );
3484
John Durkop0e005192020-10-31 22:06:54 -07003485#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003486 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3487 {
3488 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3489 MBEDTLS_MD_NONE );
3490 ret = mbedtls_rsa_pkcs1_sign( rsa,
3491 mbedtls_ctr_drbg_random,
3492 &global_data.ctr_drbg,
3493 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003494 md_alg,
3495 (unsigned int) hash_length,
3496 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003497 signature );
3498 }
3499 else
John Durkop0e005192020-10-31 22:06:54 -07003500#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3501#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003502 if( PSA_ALG_IS_RSA_PSS( alg ) )
3503 {
3504 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3505 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3506 mbedtls_ctr_drbg_random,
3507 &global_data.ctr_drbg,
3508 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003509 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003510 (unsigned int) hash_length,
3511 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003512 signature );
3513 }
3514 else
John Durkop0e005192020-10-31 22:06:54 -07003515#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003516 {
3517 return( PSA_ERROR_INVALID_ARGUMENT );
3518 }
3519
3520 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003521 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003522 return( mbedtls_to_psa_error( ret ) );
3523}
John Durkop0e005192020-10-31 22:06:54 -07003524#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003525
3526static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3527 psa_algorithm_t alg,
3528 const uint8_t *hash,
3529 size_t hash_length,
3530 const uint8_t *signature,
3531 size_t signature_length )
3532{
3533 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003535 mbedtls_md_type_t md_alg;
John Durkop0e005192020-10-31 22:06:54 -07003536#if !defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
3537 (void)hash;
3538 (void)signature;
3539#endif /* !MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003540
3541 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3542 if( status != PSA_SUCCESS )
3543 return( status );
3544
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003545 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3546 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003547
John Durkop0e005192020-10-31 22:06:54 -07003548#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003549 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3550 {
3551 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3552 MBEDTLS_MD_NONE );
3553 ret = mbedtls_rsa_pkcs1_verify( rsa,
3554 mbedtls_ctr_drbg_random,
3555 &global_data.ctr_drbg,
3556 MBEDTLS_RSA_PUBLIC,
3557 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003558 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003559 hash,
3560 signature );
3561 }
3562 else
John Durkop0e005192020-10-31 22:06:54 -07003563#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3564#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003565 if( PSA_ALG_IS_RSA_PSS( alg ) )
3566 {
3567 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3568 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3569 mbedtls_ctr_drbg_random,
3570 &global_data.ctr_drbg,
3571 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003572 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003573 (unsigned int) hash_length,
3574 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003575 signature );
3576 }
3577 else
John Durkop0e005192020-10-31 22:06:54 -07003578#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003579 {
3580 return( PSA_ERROR_INVALID_ARGUMENT );
3581 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003582
3583 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3584 * the rest of the signature is invalid". This has little use in
3585 * practice and PSA doesn't report this distinction. */
3586 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3587 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003588 return( mbedtls_to_psa_error( ret ) );
3589}
John Durkop0e005192020-10-31 22:06:54 -07003590#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003591
John Durkop0e005192020-10-31 22:06:54 -07003592#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3593 (defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY))
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003594/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3595 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3596 * (even though these functions don't modify it). */
3597static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3598 psa_algorithm_t alg,
3599 const uint8_t *hash,
3600 size_t hash_length,
3601 uint8_t *signature,
3602 size_t signature_size,
3603 size_t *signature_length )
3604{
Janos Follath24eed8d2019-11-22 13:21:35 +00003605 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003606 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003607 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003608 mbedtls_mpi_init( &r );
3609 mbedtls_mpi_init( &s );
3610
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003611 if( signature_size < 2 * curve_bytes )
3612 {
3613 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3614 goto cleanup;
3615 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003616
John Durkop0ea39e02020-10-13 19:58:20 -07003617#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003618 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3619 {
3620 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3621 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3622 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003623 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3624 &ecp->d, hash,
3625 hash_length, md_alg,
3626 mbedtls_ctr_drbg_random,
3627 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003628 }
3629 else
John Durkop0ea39e02020-10-13 19:58:20 -07003630#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003631 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003632 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003633 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3634 hash, hash_length,
3635 mbedtls_ctr_drbg_random,
3636 &global_data.ctr_drbg ) );
3637 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003638
3639 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3640 signature,
3641 curve_bytes ) );
3642 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3643 signature + curve_bytes,
3644 curve_bytes ) );
3645
3646cleanup:
3647 mbedtls_mpi_free( &r );
3648 mbedtls_mpi_free( &s );
3649 if( ret == 0 )
3650 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003651 return( mbedtls_to_psa_error( ret ) );
3652}
3653
3654static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3655 const uint8_t *hash,
3656 size_t hash_length,
3657 const uint8_t *signature,
3658 size_t signature_length )
3659{
Janos Follath24eed8d2019-11-22 13:21:35 +00003660 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003661 mbedtls_mpi r, s;
3662 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3663 mbedtls_mpi_init( &r );
3664 mbedtls_mpi_init( &s );
3665
3666 if( signature_length != 2 * curve_bytes )
3667 return( PSA_ERROR_INVALID_SIGNATURE );
3668
3669 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3670 signature,
3671 curve_bytes ) );
3672 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3673 signature + curve_bytes,
3674 curve_bytes ) );
3675
Steven Cooremanacda8342020-07-24 23:09:52 +02003676 /* Check whether the public part is loaded. If not, load it. */
3677 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3678 {
3679 MBEDTLS_MPI_CHK(
3680 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3681 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3682 }
3683
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003684 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3685 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003686
3687cleanup:
3688 mbedtls_mpi_free( &r );
3689 mbedtls_mpi_free( &s );
3690 return( mbedtls_to_psa_error( ret ) );
3691}
John Durkop0e005192020-10-31 22:06:54 -07003692#endif /* (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && (defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY)) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003693
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003694psa_status_t psa_sign_hash( psa_key_handle_t handle,
3695 psa_algorithm_t alg,
3696 const uint8_t *hash,
3697 size_t hash_length,
3698 uint8_t *signature,
3699 size_t signature_size,
3700 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003701{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003702 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003703 psa_status_t status;
3704
3705 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003706 /* Immediately reject a zero-length signature buffer. This guarantees
3707 * that signature must be a valid pointer. (On the other hand, the hash
3708 * buffer can in principle be empty since it doesn't actually have
3709 * to be a hash.) */
3710 if( signature_size == 0 )
3711 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003712
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003713 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003714 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003715 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003716 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003717 {
3718 status = PSA_ERROR_INVALID_ARGUMENT;
3719 goto exit;
3720 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003721
Steven Cooremancd84cb42020-07-16 20:28:36 +02003722 /* Try any of the available accelerators first */
3723 status = psa_driver_wrapper_sign_hash( slot,
3724 alg,
3725 hash,
3726 hash_length,
3727 signature,
3728 signature_size,
3729 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003730 if( status != PSA_ERROR_NOT_SUPPORTED ||
3731 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003732 goto exit;
3733
Steven Cooreman7a250572020-07-17 16:43:05 +02003734 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop0e005192020-10-31 22:06:54 -07003735#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine8e338702019-07-30 20:06:31 +02003736 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003737 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003738 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003739
Steven Cooreman4fed4552020-08-03 14:46:03 +02003740 status = psa_load_rsa_representation( slot->attr.type,
3741 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003742 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003743 &rsa );
3744 if( status != PSA_SUCCESS )
3745 goto exit;
3746
Steven Cooremana2371e52020-07-28 14:30:39 +02003747 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003748 alg,
3749 hash, hash_length,
3750 signature, signature_size,
3751 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003752
Steven Cooremana2371e52020-07-28 14:30:39 +02003753 mbedtls_rsa_free( rsa );
3754 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003755 }
3756 else
John Durkop0e005192020-10-31 22:06:54 -07003757#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
John Durkopf4c4cb02020-10-28 20:09:55 -07003758#if defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02003759 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003760 {
John Durkop0e005192020-10-31 22:06:54 -07003761#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003762 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003763#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003764 PSA_ALG_IS_ECDSA( alg )
3765#else
3766 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3767#endif
3768 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003769 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003770 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003771 status = psa_load_ecp_representation( slot->attr.type,
3772 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003773 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003774 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003775 if( status != PSA_SUCCESS )
3776 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003777 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003778 alg,
3779 hash, hash_length,
3780 signature, signature_size,
3781 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003782 mbedtls_ecp_keypair_free( ecp );
3783 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003784 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003785 else
John Durkop0e005192020-10-31 22:06:54 -07003786#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003787 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003788 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003789 }
itayzafrir5c753392018-05-08 11:18:38 +03003790 }
3791 else
John Durkopf4c4cb02020-10-28 20:09:55 -07003792#endif /* defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY) */
itayzafrir5c753392018-05-08 11:18:38 +03003793 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003794 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003795 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003796
3797exit:
3798 /* Fill the unused part of the output buffer (the whole buffer on error,
3799 * the trailing part on success) with something that isn't a valid mac
3800 * (barring an attack on the mac and deliberately-crafted input),
3801 * in case the caller doesn't check the return status properly. */
3802 if( status == PSA_SUCCESS )
3803 memset( signature + *signature_length, '!',
3804 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003805 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003806 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003807 /* If signature_size is 0 then we have nothing to do. We must not call
3808 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003809 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003810}
3811
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003812psa_status_t psa_verify_hash( psa_key_handle_t handle,
3813 psa_algorithm_t alg,
3814 const uint8_t *hash,
3815 size_t hash_length,
3816 const uint8_t *signature,
3817 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003818{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003819 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003820 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003821
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003822 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003823 if( status != PSA_SUCCESS )
3824 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003825
Steven Cooreman55ae2172020-07-17 19:46:15 +02003826 /* Try any of the available accelerators first */
3827 status = psa_driver_wrapper_verify_hash( slot,
3828 alg,
3829 hash,
3830 hash_length,
3831 signature,
3832 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003833 if( status != PSA_ERROR_NOT_SUPPORTED ||
3834 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +02003835 return status;
3836
John Durkop0e005192020-10-31 22:06:54 -07003837#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
3838 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02003839 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003840 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003841 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003842
Steven Cooreman4fed4552020-08-03 14:46:03 +02003843 status = psa_load_rsa_representation( slot->attr.type,
3844 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003845 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003846 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003847 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003848 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02003849
Steven Cooremana2371e52020-07-28 14:30:39 +02003850 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003851 alg,
3852 hash, hash_length,
3853 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003854 mbedtls_rsa_free( rsa );
3855 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003856 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003857 }
3858 else
John Durkop0e005192020-10-31 22:06:54 -07003859#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
John Durkopf4c4cb02020-10-28 20:09:55 -07003860#if defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02003861 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003862 {
John Durkopf87e3ae2020-10-26 15:25:23 -07003863#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003864 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003865 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003866 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003867 status = psa_load_ecp_representation( slot->attr.type,
3868 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003869 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003870 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003871 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003872 return( status );
Steven Cooremana2371e52020-07-28 14:30:39 +02003873 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003874 hash, hash_length,
3875 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003876 mbedtls_ecp_keypair_free( ecp );
3877 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02003878 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02003879 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003880 else
John Durkopf87e3ae2020-10-26 15:25:23 -07003881#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003882 {
3883 return( PSA_ERROR_INVALID_ARGUMENT );
3884 }
itayzafrir5c753392018-05-08 11:18:38 +03003885 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003886 else
John Durkopf4c4cb02020-10-28 20:09:55 -07003887#endif /* defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) || defined(MBEDTLS_PSA_BUILTIN_ECC_PUBLIC_KEY) */
Gilles Peskine20035e32018-02-03 22:44:14 +01003888 {
3889 return( PSA_ERROR_NOT_SUPPORTED );
3890 }
3891}
3892
John Durkop0e005192020-10-31 22:06:54 -07003893#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003894static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3895 mbedtls_rsa_context *rsa )
3896{
3897 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3898 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3899 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3900 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3901}
John Durkop0e005192020-10-31 22:06:54 -07003902#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003903
Gilles Peskinec5487a82018-12-03 18:08:14 +01003904psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003905 psa_algorithm_t alg,
3906 const uint8_t *input,
3907 size_t input_length,
3908 const uint8_t *salt,
3909 size_t salt_length,
3910 uint8_t *output,
3911 size_t output_size,
3912 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003913{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003914 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003915 psa_status_t status;
3916
Darryl Green5cc689a2018-07-24 15:34:10 +01003917 (void) input;
3918 (void) input_length;
3919 (void) salt;
3920 (void) output;
3921 (void) output_size;
3922
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003923 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003924
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003925 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3926 return( PSA_ERROR_INVALID_ARGUMENT );
3927
Gilles Peskine28f8f302019-07-24 13:30:31 +02003928 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003929 if( status != PSA_SUCCESS )
3930 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003931 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3932 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003933 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003934
John Durkop0e005192020-10-31 22:06:54 -07003935#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
3936 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02003937 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003938 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003939 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003940 status = psa_load_rsa_representation( slot->attr.type,
3941 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003942 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003943 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003944 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003945 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003946
3947 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003948 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003949 status = PSA_ERROR_BUFFER_TOO_SMALL;
3950 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003951 }
John Durkop0e005192020-10-31 22:06:54 -07003952#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003953 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003954 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003955 status = mbedtls_to_psa_error(
3956 mbedtls_rsa_pkcs1_encrypt( rsa,
3957 mbedtls_ctr_drbg_random,
3958 &global_data.ctr_drbg,
3959 MBEDTLS_RSA_PUBLIC,
3960 input_length,
3961 input,
3962 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003963 }
3964 else
John Durkop0e005192020-10-31 22:06:54 -07003965#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3966#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003967 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003969 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003970 status = mbedtls_to_psa_error(
3971 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3972 mbedtls_ctr_drbg_random,
3973 &global_data.ctr_drbg,
3974 MBEDTLS_RSA_PUBLIC,
3975 salt, salt_length,
3976 input_length,
3977 input,
3978 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003979 }
3980 else
John Durkop0e005192020-10-31 22:06:54 -07003981#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003982 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003983 status = PSA_ERROR_INVALID_ARGUMENT;
3984 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003985 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003986rsa_exit:
3987 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003988 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003989
Steven Cooremana2371e52020-07-28 14:30:39 +02003990 mbedtls_rsa_free( rsa );
3991 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003992 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003993 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003994 else
John Durkop0e005192020-10-31 22:06:54 -07003995#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003996 {
3997 return( PSA_ERROR_NOT_SUPPORTED );
3998 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003999}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004000
Gilles Peskinec5487a82018-12-03 18:08:14 +01004001psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004002 psa_algorithm_t alg,
4003 const uint8_t *input,
4004 size_t input_length,
4005 const uint8_t *salt,
4006 size_t salt_length,
4007 uint8_t *output,
4008 size_t output_size,
4009 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004010{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004011 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004012 psa_status_t status;
4013
Darryl Green5cc689a2018-07-24 15:34:10 +01004014 (void) input;
4015 (void) input_length;
4016 (void) salt;
4017 (void) output;
4018 (void) output_size;
4019
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004020 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004021
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004022 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4023 return( PSA_ERROR_INVALID_ARGUMENT );
4024
Gilles Peskine28f8f302019-07-24 13:30:31 +02004025 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004026 if( status != PSA_SUCCESS )
4027 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004028 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004029 return( PSA_ERROR_INVALID_ARGUMENT );
4030
John Durkop0e005192020-10-31 22:06:54 -07004031#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine8e338702019-07-30 20:06:31 +02004032 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004033 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004034 mbedtls_rsa_context *rsa = NULL;
4035 status = psa_load_rsa_representation( slot->attr.type,
4036 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004037 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004038 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004039 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004040 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004041
Steven Cooremana2371e52020-07-28 14:30:39 +02004042 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004043 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004044 status = PSA_ERROR_INVALID_ARGUMENT;
4045 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004046 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004047
John Durkop0e005192020-10-31 22:06:54 -07004048#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004049 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004050 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004051 status = mbedtls_to_psa_error(
4052 mbedtls_rsa_pkcs1_decrypt( rsa,
4053 mbedtls_ctr_drbg_random,
4054 &global_data.ctr_drbg,
4055 MBEDTLS_RSA_PRIVATE,
4056 output_length,
4057 input,
4058 output,
4059 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004060 }
4061 else
John Durkop0e005192020-10-31 22:06:54 -07004062#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4063#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004064 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004065 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004066 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004067 status = mbedtls_to_psa_error(
4068 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4069 mbedtls_ctr_drbg_random,
4070 &global_data.ctr_drbg,
4071 MBEDTLS_RSA_PRIVATE,
4072 salt, salt_length,
4073 output_length,
4074 input,
4075 output,
4076 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004077 }
4078 else
John Durkop0e005192020-10-31 22:06:54 -07004079#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004080 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004081 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004082 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004083
Steven Cooreman4fed4552020-08-03 14:46:03 +02004084rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004085 mbedtls_rsa_free( rsa );
4086 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004087 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004088 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004089 else
John Durkop0e005192020-10-31 22:06:54 -07004090#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004091 {
4092 return( PSA_ERROR_NOT_SUPPORTED );
4093 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004094}
Gilles Peskine20035e32018-02-03 22:44:14 +01004095
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004096
4097
mohammad1603503973b2018-03-12 15:59:30 +02004098/****************************************************************/
4099/* Symmetric cryptography */
4100/****************************************************************/
4101
Gilles Peskinee553c652018-06-04 16:22:46 +02004102static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004103 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004104 psa_algorithm_t alg,
4105 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004106{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004107 int ret = 0;
4108 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004109 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004110 size_t key_bits;
4111 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004112 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4113 PSA_KEY_USAGE_ENCRYPT :
4114 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004115
Steven Cooremand3feccd2020-09-01 15:56:14 +02004116 /* A context must be freshly initialized before it can be set up. */
4117 if( operation->alg != 0 )
4118 return( PSA_ERROR_BAD_STATE );
4119
Steven Cooremana07b9972020-09-10 14:54:14 +02004120 /* The requested algorithm must be one that can be processed by cipher. */
4121 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004122 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004123
Steven Cooremanef8575e2020-09-11 11:44:50 +02004124 /* Fetch key material from key storage. */
4125 status = psa_get_key_from_slot( handle, &slot, usage, alg );
4126 if( status != PSA_SUCCESS )
4127 goto exit;
4128
4129 /* Initialize the operation struct members, except for alg. The alg member
4130 * is used to indicate to psa_cipher_abort that there are resources to free,
4131 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004132 operation->key_set = 0;
4133 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004134 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004135 operation->iv_size = 0;
4136 operation->block_size = 0;
4137 if( alg == PSA_ALG_ECB_NO_PADDING )
4138 operation->iv_required = 0;
4139 else
4140 operation->iv_required = 1;
4141
Steven Cooremana07b9972020-09-10 14:54:14 +02004142 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004143 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004144 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004145 slot,
4146 alg );
4147 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004148 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004149 slot,
4150 alg );
4151
Steven Cooremanef8575e2020-09-11 11:44:50 +02004152 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004153 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004154 /* Once the driver context is initialised, it needs to be freed using
4155 * psa_cipher_abort. Indicate this through setting alg. */
4156 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004157 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004158
Steven Cooremand3feccd2020-09-01 15:56:14 +02004159 if( status != PSA_ERROR_NOT_SUPPORTED ||
4160 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4161 goto exit;
4162
Steven Cooremana07b9972020-09-10 14:54:14 +02004163 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004164 * available for the given algorithm & key. */
4165 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004166
Steven Cooremana07b9972020-09-10 14:54:14 +02004167 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004168 * psa_cipher_abort. Indicate there is something to be freed through setting
4169 * alg, and indicate the operation is being done using mbedtls crypto through
4170 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004171 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004172 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004173
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004174 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004175 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004176 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004177 {
4178 status = PSA_ERROR_NOT_SUPPORTED;
4179 goto exit;
4180 }
mohammad1603503973b2018-03-12 15:59:30 +02004181
mohammad1603503973b2018-03-12 15:59:30 +02004182 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004183 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004184 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004185
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004186#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004187 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004188 {
4189 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004190 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004191 memcpy( keys, slot->data.key.data, 16 );
4192 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004193 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4194 keys,
4195 192, cipher_operation );
4196 }
4197 else
4198#endif
4199 {
4200 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004201 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004202 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004203 }
Moran Peker41deec42018-04-04 15:43:05 +03004204 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004205 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004206
mohammad16038481e742018-03-18 13:57:31 +02004207#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004208 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004209 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004210 case PSA_ALG_CBC_NO_PADDING:
4211 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4212 MBEDTLS_PADDING_NONE );
4213 break;
4214 case PSA_ALG_CBC_PKCS7:
4215 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4216 MBEDTLS_PADDING_PKCS7 );
4217 break;
4218 default:
4219 /* The algorithm doesn't involve padding. */
4220 ret = 0;
4221 break;
4222 }
4223 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004224 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004225#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4226
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004227 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004228 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004229 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4230 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004231 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004232 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004233 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004234#if defined(MBEDTLS_CHACHA20_C)
4235 else
4236 if( alg == PSA_ALG_CHACHA20 )
4237 operation->iv_size = 12;
4238#endif
mohammad1603503973b2018-03-12 15:59:30 +02004239
Steven Cooreman7df02922020-09-09 15:28:49 +02004240 status = PSA_SUCCESS;
4241
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004242exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004243 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004244 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004245 if( status == PSA_SUCCESS )
4246 {
4247 /* Update operation flags for both driver and software implementations */
4248 operation->key_set = 1;
4249 }
4250 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004251 psa_cipher_abort( operation );
4252 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004253}
4254
Gilles Peskinefe119512018-07-08 21:39:34 +02004255psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004256 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004257 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004258{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004259 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004260}
4261
Gilles Peskinefe119512018-07-08 21:39:34 +02004262psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004263 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004264 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004265{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004266 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004267}
4268
Gilles Peskinefe119512018-07-08 21:39:34 +02004269psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004270 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004271 size_t iv_size,
4272 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004273{
itayzafrir534bd7c2018-08-02 13:56:32 +03004274 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004275 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004276 if( operation->iv_set || ! operation->iv_required )
4277 {
4278 return( PSA_ERROR_BAD_STATE );
4279 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004280
Steven Cooremanef8575e2020-09-11 11:44:50 +02004281 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004282 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004283 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004284 iv,
4285 iv_size,
4286 iv_length );
4287 goto exit;
4288 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004289
Moran Peker41deec42018-04-04 15:43:05 +03004290 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004291 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004292 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004293 goto exit;
4294 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004295 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4296 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004297 if( ret != 0 )
4298 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004299 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004300 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004301 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004302
mohammad16038481e742018-03-18 13:57:31 +02004303 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004304 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004305
Moran Peker395db872018-05-31 14:07:14 +03004306exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004307 if( status == PSA_SUCCESS )
4308 operation->iv_set = 1;
4309 else
Moran Peker395db872018-05-31 14:07:14 +03004310 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004311 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004312}
4313
Gilles Peskinefe119512018-07-08 21:39:34 +02004314psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004315 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004316 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004317{
itayzafrir534bd7c2018-08-02 13:56:32 +03004318 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004319 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004320 if( operation->iv_set || ! operation->iv_required )
4321 {
4322 return( PSA_ERROR_BAD_STATE );
4323 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004324
Steven Cooremanef8575e2020-09-11 11:44:50 +02004325 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004326 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004327 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004328 iv,
4329 iv_length );
4330 goto exit;
4331 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004332
Moran Pekera28258c2018-05-29 16:25:04 +03004333 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004334 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004335 status = PSA_ERROR_INVALID_ARGUMENT;
4336 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004337 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004338 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4339 status = mbedtls_to_psa_error( ret );
4340exit:
4341 if( status == PSA_SUCCESS )
4342 operation->iv_set = 1;
4343 else
mohammad1603503973b2018-03-12 15:59:30 +02004344 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004345 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004346}
4347
Steven Cooreman177deba2020-09-07 17:14:14 +02004348/* Process input for which the algorithm is set to ECB mode. This requires
4349 * manual processing, since the PSA API is defined as being able to process
4350 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4351 * underlying mbedtls_cipher_update only takes full blocks. */
4352static psa_status_t psa_cipher_update_ecb_internal(
4353 mbedtls_cipher_context_t *ctx,
4354 const uint8_t *input,
4355 size_t input_length,
4356 uint8_t *output,
4357 size_t output_size,
4358 size_t *output_length )
4359{
4360 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4361 size_t block_size = ctx->cipher_info->block_size;
4362 size_t internal_output_length = 0;
4363 *output_length = 0;
4364
4365 if( input_length == 0 )
4366 {
4367 status = PSA_SUCCESS;
4368 goto exit;
4369 }
4370
4371 if( ctx->unprocessed_len > 0 )
4372 {
4373 /* Fill up to block size, and run the block if there's a full one. */
4374 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4375
4376 if( input_length < bytes_to_copy )
4377 bytes_to_copy = input_length;
4378
4379 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4380 input, bytes_to_copy );
4381 input_length -= bytes_to_copy;
4382 input += bytes_to_copy;
4383 ctx->unprocessed_len += bytes_to_copy;
4384
4385 if( ctx->unprocessed_len == block_size )
4386 {
4387 status = mbedtls_to_psa_error(
4388 mbedtls_cipher_update( ctx,
4389 ctx->unprocessed_data,
4390 block_size,
4391 output, &internal_output_length ) );
4392
4393 if( status != PSA_SUCCESS )
4394 goto exit;
4395
4396 output += internal_output_length;
4397 output_size -= internal_output_length;
4398 *output_length += internal_output_length;
4399 ctx->unprocessed_len = 0;
4400 }
4401 }
4402
4403 while( input_length >= block_size )
4404 {
4405 /* Run all full blocks we have, one by one */
4406 status = mbedtls_to_psa_error(
4407 mbedtls_cipher_update( ctx, input,
4408 block_size,
4409 output, &internal_output_length ) );
4410
4411 if( status != PSA_SUCCESS )
4412 goto exit;
4413
4414 input_length -= block_size;
4415 input += block_size;
4416
4417 output += internal_output_length;
4418 output_size -= internal_output_length;
4419 *output_length += internal_output_length;
4420 }
4421
4422 if( input_length > 0 )
4423 {
4424 /* Save unprocessed bytes for later processing */
4425 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4426 input, input_length );
4427 ctx->unprocessed_len += input_length;
4428 }
4429
4430 status = PSA_SUCCESS;
4431
4432exit:
4433 return( status );
4434}
4435
Gilles Peskinee553c652018-06-04 16:22:46 +02004436psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4437 const uint8_t *input,
4438 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004439 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004440 size_t output_size,
4441 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004442{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004443 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004444 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004445 if( operation->alg == 0 )
4446 {
4447 return( PSA_ERROR_BAD_STATE );
4448 }
4449 if( operation->iv_required && ! operation->iv_set )
4450 {
4451 return( PSA_ERROR_BAD_STATE );
4452 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004453
Steven Cooremanef8575e2020-09-11 11:44:50 +02004454 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004455 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004456 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004457 input,
4458 input_length,
4459 output,
4460 output_size,
4461 output_length );
4462 goto exit;
4463 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004464
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004465 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004466 {
4467 /* Take the unprocessed partial block left over from previous
4468 * update calls, if any, plus the input to this call. Remove
4469 * the last partial block, if any. You get the data that will be
4470 * output in this call. */
4471 expected_output_size =
4472 ( operation->ctx.cipher.unprocessed_len + input_length )
4473 / operation->block_size * operation->block_size;
4474 }
4475 else
4476 {
4477 expected_output_size = input_length;
4478 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004479
Gilles Peskine89d789c2018-06-04 17:17:16 +02004480 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004481 {
4482 status = PSA_ERROR_BUFFER_TOO_SMALL;
4483 goto exit;
4484 }
mohammad160382759612018-03-12 18:16:40 +02004485
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004486 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4487 {
4488 /* mbedtls_cipher_update has an API inconsistency: it will only
4489 * process a single block at a time in ECB mode. Abstract away that
4490 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004491 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4492 input,
4493 input_length,
4494 output,
4495 output_size,
4496 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004497 }
4498 else
4499 {
4500 status = mbedtls_to_psa_error(
4501 mbedtls_cipher_update( &operation->ctx.cipher, input,
4502 input_length, output, output_length ) );
4503 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004504exit:
4505 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004506 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004507 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004508}
4509
Gilles Peskinee553c652018-06-04 16:22:46 +02004510psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4511 uint8_t *output,
4512 size_t output_size,
4513 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004514{
David Saadab4ecc272019-02-14 13:48:10 +02004515 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004516 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004517 if( operation->alg == 0 )
4518 {
4519 return( PSA_ERROR_BAD_STATE );
4520 }
4521 if( operation->iv_required && ! operation->iv_set )
4522 {
4523 return( PSA_ERROR_BAD_STATE );
4524 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004525
Steven Cooremanef8575e2020-09-11 11:44:50 +02004526 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004527 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004528 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004529 output,
4530 output_size,
4531 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004532 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004533 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004534
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004535 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004536 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004537 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004538 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004539 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004540 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004541 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004542 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004543 }
4544
Steven Cooremanef8575e2020-09-11 11:44:50 +02004545 status = mbedtls_to_psa_error(
4546 mbedtls_cipher_finish( &operation->ctx.cipher,
4547 temp_output_buffer,
4548 output_length ) );
4549 if( status != PSA_SUCCESS )
4550 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004551
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004552 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004553 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004554 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004555 memcpy( output, temp_output_buffer, *output_length );
4556 else
Janos Follath315b51c2018-07-09 16:04:51 +01004557 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004558
Steven Cooremanef8575e2020-09-11 11:44:50 +02004559exit:
4560 if( operation->mbedtls_in_use == 1 )
4561 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004562
Steven Cooremanef8575e2020-09-11 11:44:50 +02004563 if( status == PSA_SUCCESS )
4564 return( psa_cipher_abort( operation ) );
4565 else
4566 {
4567 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004568 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004569
Steven Cooremanef8575e2020-09-11 11:44:50 +02004570 return( status );
4571 }
mohammad1603503973b2018-03-12 15:59:30 +02004572}
4573
Gilles Peskinee553c652018-06-04 16:22:46 +02004574psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4575{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004576 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004577 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004578 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004579 * in use. It's ok to call abort on such an object, and there's
4580 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004581 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004582 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004583
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004584 /* Sanity check (shouldn't happen: operation->alg should
4585 * always have been initialized to a valid value). */
4586 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4587 return( PSA_ERROR_BAD_STATE );
4588
Steven Cooremanef8575e2020-09-11 11:44:50 +02004589 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004590 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004591 else
4592 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004593
Moran Peker41deec42018-04-04 15:43:05 +03004594 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004595 operation->key_set = 0;
4596 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004597 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004598 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004599 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004600 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004601
Moran Peker395db872018-05-31 14:07:14 +03004602 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004603}
4604
Gilles Peskinea0655c32018-04-30 17:06:50 +02004605
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004606
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004607
mohammad16035955c982018-04-26 00:53:03 +03004608/****************************************************************/
4609/* AEAD */
4610/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004611
Gilles Peskineedf9a652018-08-17 18:11:56 +02004612typedef struct
4613{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004614 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004615 const mbedtls_cipher_info_t *cipher_info;
4616 union
4617 {
4618#if defined(MBEDTLS_CCM_C)
4619 mbedtls_ccm_context ccm;
4620#endif /* MBEDTLS_CCM_C */
4621#if defined(MBEDTLS_GCM_C)
4622 mbedtls_gcm_context gcm;
4623#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004624#if defined(MBEDTLS_CHACHAPOLY_C)
4625 mbedtls_chachapoly_context chachapoly;
4626#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004627 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004628 psa_algorithm_t core_alg;
4629 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004630 uint8_t tag_length;
4631} aead_operation_t;
4632
Gilles Peskine30a9e412019-01-14 18:36:12 +01004633static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004634{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004635 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004636 {
4637#if defined(MBEDTLS_CCM_C)
4638 case PSA_ALG_CCM:
4639 mbedtls_ccm_free( &operation->ctx.ccm );
4640 break;
4641#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004642#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004643 case PSA_ALG_GCM:
4644 mbedtls_gcm_free( &operation->ctx.gcm );
4645 break;
4646#endif /* MBEDTLS_GCM_C */
4647 }
4648}
4649
4650static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004651 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004652 psa_key_usage_t usage,
4653 psa_algorithm_t alg )
4654{
4655 psa_status_t status;
4656 size_t key_bits;
4657 mbedtls_cipher_id_t cipher_id;
4658
Gilles Peskine28f8f302019-07-24 13:30:31 +02004659 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004660 if( status != PSA_SUCCESS )
4661 return( status );
4662
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004663 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004664
4665 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004666 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004667 &cipher_id );
4668 if( operation->cipher_info == NULL )
4669 return( PSA_ERROR_NOT_SUPPORTED );
4670
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004671 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004672 {
4673#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004674 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4675 operation->core_alg = PSA_ALG_CCM;
4676 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004677 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4678 * The call to mbedtls_ccm_encrypt_and_tag or
4679 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004680 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004681 return( PSA_ERROR_INVALID_ARGUMENT );
4682 mbedtls_ccm_init( &operation->ctx.ccm );
4683 status = mbedtls_to_psa_error(
4684 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004685 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004686 (unsigned int) key_bits ) );
4687 if( status != 0 )
4688 goto cleanup;
4689 break;
4690#endif /* MBEDTLS_CCM_C */
4691
4692#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004693 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4694 operation->core_alg = PSA_ALG_GCM;
4695 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004696 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4697 * The call to mbedtls_gcm_crypt_and_tag or
4698 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004699 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004700 return( PSA_ERROR_INVALID_ARGUMENT );
4701 mbedtls_gcm_init( &operation->ctx.gcm );
4702 status = mbedtls_to_psa_error(
4703 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004704 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004705 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004706 if( status != 0 )
4707 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004708 break;
4709#endif /* MBEDTLS_GCM_C */
4710
Gilles Peskine26869f22019-05-06 15:25:00 +02004711#if defined(MBEDTLS_CHACHAPOLY_C)
4712 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4713 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4714 operation->full_tag_length = 16;
4715 /* We only support the default tag length. */
4716 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4717 return( PSA_ERROR_NOT_SUPPORTED );
4718 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4719 status = mbedtls_to_psa_error(
4720 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004721 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004722 if( status != 0 )
4723 goto cleanup;
4724 break;
4725#endif /* MBEDTLS_CHACHAPOLY_C */
4726
Gilles Peskineedf9a652018-08-17 18:11:56 +02004727 default:
4728 return( PSA_ERROR_NOT_SUPPORTED );
4729 }
4730
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004731 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4732 {
4733 status = PSA_ERROR_INVALID_ARGUMENT;
4734 goto cleanup;
4735 }
4736 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004737
Gilles Peskineedf9a652018-08-17 18:11:56 +02004738 return( PSA_SUCCESS );
4739
4740cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004741 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004742 return( status );
4743}
4744
Gilles Peskinec5487a82018-12-03 18:08:14 +01004745psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004746 psa_algorithm_t alg,
4747 const uint8_t *nonce,
4748 size_t nonce_length,
4749 const uint8_t *additional_data,
4750 size_t additional_data_length,
4751 const uint8_t *plaintext,
4752 size_t plaintext_length,
4753 uint8_t *ciphertext,
4754 size_t ciphertext_size,
4755 size_t *ciphertext_length )
4756{
mohammad16035955c982018-04-26 00:53:03 +03004757 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004758 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004759 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004760
mohammad1603f08a5502018-06-03 15:05:47 +03004761 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004762
Gilles Peskinec5487a82018-12-03 18:08:14 +01004763 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004764 if( status != PSA_SUCCESS )
4765 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004766
Gilles Peskineedf9a652018-08-17 18:11:56 +02004767 /* For all currently supported modes, the tag is at the end of the
4768 * ciphertext. */
4769 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4770 {
4771 status = PSA_ERROR_BUFFER_TOO_SMALL;
4772 goto exit;
4773 }
4774 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004775
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004776#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004777 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004778 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004779 status = mbedtls_to_psa_error(
4780 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4781 MBEDTLS_GCM_ENCRYPT,
4782 plaintext_length,
4783 nonce, nonce_length,
4784 additional_data, additional_data_length,
4785 plaintext, ciphertext,
4786 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004787 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004788 else
4789#endif /* MBEDTLS_GCM_C */
4790#if defined(MBEDTLS_CCM_C)
4791 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004792 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004793 status = mbedtls_to_psa_error(
4794 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4795 plaintext_length,
4796 nonce, nonce_length,
4797 additional_data,
4798 additional_data_length,
4799 plaintext, ciphertext,
4800 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004801 }
mohammad16035c8845f2018-05-09 05:40:09 -07004802 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004803#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004804#if defined(MBEDTLS_CHACHAPOLY_C)
4805 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4806 {
4807 if( nonce_length != 12 || operation.tag_length != 16 )
4808 {
4809 status = PSA_ERROR_NOT_SUPPORTED;
4810 goto exit;
4811 }
4812 status = mbedtls_to_psa_error(
4813 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4814 plaintext_length,
4815 nonce,
4816 additional_data,
4817 additional_data_length,
4818 plaintext,
4819 ciphertext,
4820 tag ) );
4821 }
4822 else
4823#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004824 {
mohammad1603554faad2018-06-03 15:07:38 +03004825 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004826 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004827
Gilles Peskineedf9a652018-08-17 18:11:56 +02004828 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4829 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004830
Gilles Peskineedf9a652018-08-17 18:11:56 +02004831exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004832 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004833 if( status == PSA_SUCCESS )
4834 *ciphertext_length = plaintext_length + operation.tag_length;
4835 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004836}
4837
Gilles Peskineee652a32018-06-01 19:23:52 +02004838/* Locate the tag in a ciphertext buffer containing the encrypted data
4839 * followed by the tag. Return the length of the part preceding the tag in
4840 * *plaintext_length. This is the size of the plaintext in modes where
4841 * the encrypted data has the same size as the plaintext, such as
4842 * CCM and GCM. */
4843static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4844 const uint8_t *ciphertext,
4845 size_t ciphertext_length,
4846 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004847 const uint8_t **p_tag )
4848{
4849 size_t payload_length;
4850 if( tag_length > ciphertext_length )
4851 return( PSA_ERROR_INVALID_ARGUMENT );
4852 payload_length = ciphertext_length - tag_length;
4853 if( payload_length > plaintext_size )
4854 return( PSA_ERROR_BUFFER_TOO_SMALL );
4855 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004856 return( PSA_SUCCESS );
4857}
4858
Gilles Peskinec5487a82018-12-03 18:08:14 +01004859psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004860 psa_algorithm_t alg,
4861 const uint8_t *nonce,
4862 size_t nonce_length,
4863 const uint8_t *additional_data,
4864 size_t additional_data_length,
4865 const uint8_t *ciphertext,
4866 size_t ciphertext_length,
4867 uint8_t *plaintext,
4868 size_t plaintext_size,
4869 size_t *plaintext_length )
4870{
mohammad16035955c982018-04-26 00:53:03 +03004871 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004872 aead_operation_t operation;
4873 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004874
Gilles Peskineee652a32018-06-01 19:23:52 +02004875 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004876
Gilles Peskinec5487a82018-12-03 18:08:14 +01004877 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004878 if( status != PSA_SUCCESS )
4879 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004880
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004881 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4882 ciphertext, ciphertext_length,
4883 plaintext_size, &tag );
4884 if( status != PSA_SUCCESS )
4885 goto exit;
4886
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004887#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004888 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004889 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004890 status = mbedtls_to_psa_error(
4891 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4892 ciphertext_length - operation.tag_length,
4893 nonce, nonce_length,
4894 additional_data,
4895 additional_data_length,
4896 tag, operation.tag_length,
4897 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004898 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004899 else
4900#endif /* MBEDTLS_GCM_C */
4901#if defined(MBEDTLS_CCM_C)
4902 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004903 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004904 status = mbedtls_to_psa_error(
4905 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4906 ciphertext_length - operation.tag_length,
4907 nonce, nonce_length,
4908 additional_data,
4909 additional_data_length,
4910 ciphertext, plaintext,
4911 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004912 }
mohammad160339574652018-06-01 04:39:53 -07004913 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004914#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004915#if defined(MBEDTLS_CHACHAPOLY_C)
4916 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4917 {
4918 if( nonce_length != 12 || operation.tag_length != 16 )
4919 {
4920 status = PSA_ERROR_NOT_SUPPORTED;
4921 goto exit;
4922 }
4923 status = mbedtls_to_psa_error(
4924 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4925 ciphertext_length - operation.tag_length,
4926 nonce,
4927 additional_data,
4928 additional_data_length,
4929 tag,
4930 ciphertext,
4931 plaintext ) );
4932 }
4933 else
4934#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004935 {
mohammad1603554faad2018-06-03 15:07:38 +03004936 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004937 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004938
Gilles Peskineedf9a652018-08-17 18:11:56 +02004939 if( status != PSA_SUCCESS && plaintext_size != 0 )
4940 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004941
Gilles Peskineedf9a652018-08-17 18:11:56 +02004942exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004943 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004944 if( status == PSA_SUCCESS )
4945 *plaintext_length = ciphertext_length - operation.tag_length;
4946 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004947}
4948
Gilles Peskinea0655c32018-04-30 17:06:50 +02004949
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004950
Gilles Peskine20035e32018-02-03 22:44:14 +01004951/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004952/* Generators */
4953/****************************************************************/
4954
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004955#define HKDF_STATE_INIT 0 /* no input yet */
4956#define HKDF_STATE_STARTED 1 /* got salt */
4957#define HKDF_STATE_KEYED 2 /* got key */
4958#define HKDF_STATE_OUTPUT 3 /* output started */
4959
Gilles Peskinecbe66502019-05-16 16:59:18 +02004960static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004961 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004962{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4964 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004965 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004967}
4968
4969
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004971{
4972 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004973 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004974 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004975 {
4976 /* The object has (apparently) been initialized but it is not
4977 * in use. It's ok to call abort on such an object, and there's
4978 * nothing to do. */
4979 }
4980 else
John Durkopd0321952020-10-29 21:37:36 -07004981#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004982 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004983 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004984 mbedtls_free( operation->ctx.hkdf.info );
4985 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004986 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004987 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004988 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004989 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004990 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004991 if( operation->ctx.tls12_prf.seed != NULL )
4992 {
4993 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4994 operation->ctx.tls12_prf.seed_length );
4995 mbedtls_free( operation->ctx.tls12_prf.seed );
4996 }
4997
4998 if( operation->ctx.tls12_prf.label != NULL )
4999 {
5000 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5001 operation->ctx.tls12_prf.label_length );
5002 mbedtls_free( operation->ctx.tls12_prf.label );
5003 }
5004
5005 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5006
5007 /* We leave the fields Ai and output_block to be erased safely by the
5008 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005009 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005010 else
John Durkopd0321952020-10-29 21:37:36 -07005011#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005012 {
5013 status = PSA_ERROR_BAD_STATE;
5014 }
Janos Follath083036a2019-06-11 10:22:26 +01005015 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005016 return( status );
5017}
5018
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005019psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005020 size_t *capacity)
5021{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005022 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005023 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005024 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005025 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005026 }
5027
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005028 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005029 return( PSA_SUCCESS );
5030}
5031
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005033 size_t capacity )
5034{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005035 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005036 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005037 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005038 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005039 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005040 return( PSA_SUCCESS );
5041}
5042
John Durkopd0321952020-10-29 21:37:36 -07005043#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005044/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005045 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005046static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005047 psa_algorithm_t hash_alg,
5048 uint8_t *output,
5049 size_t output_length )
5050{
5051 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5052 psa_status_t status;
5053
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005054 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5055 return( PSA_ERROR_BAD_STATE );
5056 hkdf->state = HKDF_STATE_OUTPUT;
5057
Gilles Peskinebef7f142018-07-12 17:22:21 +02005058 while( output_length != 0 )
5059 {
5060 /* Copy what remains of the current block */
5061 uint8_t n = hash_length - hkdf->offset_in_block;
5062 if( n > output_length )
5063 n = (uint8_t) output_length;
5064 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5065 output += n;
5066 output_length -= n;
5067 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005068 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005069 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005070 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005071 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005072 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005073 * object was corrupted or if this function is called directly
5074 * inside the library. */
5075 if( hkdf->block_number == 0xff )
5076 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005077
5078 /* We need a new block */
5079 ++hkdf->block_number;
5080 hkdf->offset_in_block = 0;
5081 status = psa_hmac_setup_internal( &hkdf->hmac,
5082 hkdf->prk, hash_length,
5083 hash_alg );
5084 if( status != PSA_SUCCESS )
5085 return( status );
5086 if( hkdf->block_number != 1 )
5087 {
5088 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5089 hkdf->output_block,
5090 hash_length );
5091 if( status != PSA_SUCCESS )
5092 return( status );
5093 }
5094 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5095 hkdf->info,
5096 hkdf->info_length );
5097 if( status != PSA_SUCCESS )
5098 return( status );
5099 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5100 &hkdf->block_number, 1 );
5101 if( status != PSA_SUCCESS )
5102 return( status );
5103 status = psa_hmac_finish_internal( &hkdf->hmac,
5104 hkdf->output_block,
5105 sizeof( hkdf->output_block ) );
5106 if( status != PSA_SUCCESS )
5107 return( status );
5108 }
5109
5110 return( PSA_SUCCESS );
5111}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005112
Janos Follath7742fee2019-06-17 12:58:10 +01005113static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5114 psa_tls12_prf_key_derivation_t *tls12_prf,
5115 psa_algorithm_t alg )
5116{
5117 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5118 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005119 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5120 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005121
Janos Follath7742fee2019-06-17 12:58:10 +01005122 /* We can't be wanting more output after block 0xff, otherwise
5123 * the capacity check in psa_key_derivation_output_bytes() would have
5124 * prevented this call. It could happen only if the operation
5125 * object was corrupted or if this function is called directly
5126 * inside the library. */
5127 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005128 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005129
5130 /* We need a new block */
5131 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005132 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005133
5134 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5135 *
5136 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5137 *
5138 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5139 * HMAC_hash(secret, A(2) + seed) +
5140 * HMAC_hash(secret, A(3) + seed) + ...
5141 *
5142 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005143 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005144 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005145 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005146 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005147 * is currently extracted as `output_block` and where i is
5148 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005149 */
5150
Janos Follathea29bfb2019-06-19 12:21:20 +01005151 /* Save the hash context before using it, to preserve the hash state with
5152 * only the inner padding in it. We need this, because inner padding depends
5153 * on the key (secret in the RFC's terminology). */
5154 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5155 if( status != PSA_SUCCESS )
5156 goto cleanup;
5157
5158 /* Calculate A(i) where i = tls12_prf->block_number. */
5159 if( tls12_prf->block_number == 1 )
5160 {
5161 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5162 * the variable seed and in this instance means it in the context of the
5163 * P_hash function, where seed = label + seed.) */
5164 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5165 tls12_prf->label, tls12_prf->label_length );
5166 if( status != PSA_SUCCESS )
5167 goto cleanup;
5168 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5169 tls12_prf->seed, tls12_prf->seed_length );
5170 if( status != PSA_SUCCESS )
5171 goto cleanup;
5172 }
5173 else
5174 {
5175 /* A(i) = HMAC_hash(secret, A(i-1)) */
5176 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5177 tls12_prf->Ai, hash_length );
5178 if( status != PSA_SUCCESS )
5179 goto cleanup;
5180 }
5181
5182 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5183 tls12_prf->Ai, hash_length );
5184 if( status != PSA_SUCCESS )
5185 goto cleanup;
5186 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5187 if( status != PSA_SUCCESS )
5188 goto cleanup;
5189
5190 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5191 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5192 tls12_prf->Ai, hash_length );
5193 if( status != PSA_SUCCESS )
5194 goto cleanup;
5195 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5196 tls12_prf->label, tls12_prf->label_length );
5197 if( status != PSA_SUCCESS )
5198 goto cleanup;
5199 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5200 tls12_prf->seed, tls12_prf->seed_length );
5201 if( status != PSA_SUCCESS )
5202 goto cleanup;
5203 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5204 tls12_prf->output_block, hash_length );
5205 if( status != PSA_SUCCESS )
5206 goto cleanup;
5207 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5208 if( status != PSA_SUCCESS )
5209 goto cleanup;
5210
Janos Follath7742fee2019-06-17 12:58:10 +01005211
5212cleanup:
5213
Janos Follathea29bfb2019-06-19 12:21:20 +01005214 cleanup_status = psa_hash_abort( &backup );
5215 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5216 status = cleanup_status;
5217
5218 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005219}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005220
Janos Follath844eb0e2019-06-19 12:10:49 +01005221static psa_status_t psa_key_derivation_tls12_prf_read(
5222 psa_tls12_prf_key_derivation_t *tls12_prf,
5223 psa_algorithm_t alg,
5224 uint8_t *output,
5225 size_t output_length )
5226{
5227 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5228 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5229 psa_status_t status;
5230 uint8_t offset, length;
5231
5232 while( output_length != 0 )
5233 {
5234 /* Check if we have fully processed the current block. */
5235 if( tls12_prf->left_in_block == 0 )
5236 {
5237 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5238 alg );
5239 if( status != PSA_SUCCESS )
5240 return( status );
5241
5242 continue;
5243 }
5244
5245 if( tls12_prf->left_in_block > output_length )
5246 length = (uint8_t) output_length;
5247 else
5248 length = tls12_prf->left_in_block;
5249
5250 offset = hash_length - tls12_prf->left_in_block;
5251 memcpy( output, tls12_prf->output_block + offset, length );
5252 output += length;
5253 output_length -= length;
5254 tls12_prf->left_in_block -= length;
5255 }
5256
5257 return( PSA_SUCCESS );
5258}
John Durkopd0321952020-10-29 21:37:36 -07005259#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005260
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005261psa_status_t psa_key_derivation_output_bytes(
5262 psa_key_derivation_operation_t *operation,
5263 uint8_t *output,
5264 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005265{
5266 psa_status_t status;
John Durkopd0321952020-10-29 21:37:36 -07005267#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005268 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
John Durkopd0321952020-10-29 21:37:36 -07005269#endif
Gilles Peskineeab56e42018-07-12 17:12:33 +02005270
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005272 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005273 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005274 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005275 }
5276
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005277 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005278 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005279 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005280 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005281 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005282 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005283 goto exit;
5284 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005285 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005286 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005288 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005289 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5290 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005291 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005292 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005293 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005294 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005295 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005296
John Durkopd0321952020-10-29 21:37:36 -07005297#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005298 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005299 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005300 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005301 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005302 output, output_length );
5303 }
Janos Follathadbec812019-06-14 11:05:39 +01005304 else
Janos Follathadbec812019-06-14 11:05:39 +01005305 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005306 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005307 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005308 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005309 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005310 output_length );
5311 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005312 else
John Durkopd0321952020-10-29 21:37:36 -07005313#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005314 {
5315 return( PSA_ERROR_BAD_STATE );
5316 }
5317
5318exit:
5319 if( status != PSA_SUCCESS )
5320 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005321 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005322 * This allows us to differentiate between exhausted operations and
5323 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5324 * operations. */
5325 psa_algorithm_t alg = operation->alg;
5326 psa_key_derivation_abort( operation );
5327 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005328 memset( output, '!', output_length );
5329 }
5330 return( status );
5331}
5332
Gilles Peskine08542d82018-07-19 17:05:42 +02005333#if defined(MBEDTLS_DES_C)
5334static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5335{
5336 if( data_size >= 8 )
5337 mbedtls_des_key_set_parity( data );
5338 if( data_size >= 16 )
5339 mbedtls_des_key_set_parity( data + 8 );
5340 if( data_size >= 24 )
5341 mbedtls_des_key_set_parity( data + 16 );
5342}
5343#endif /* MBEDTLS_DES_C */
5344
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005345static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005346 psa_key_slot_t *slot,
5347 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005348 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005349{
5350 uint8_t *data = NULL;
5351 size_t bytes = PSA_BITS_TO_BYTES( bits );
5352 psa_status_t status;
5353
Gilles Peskine8e338702019-07-30 20:06:31 +02005354 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005355 return( PSA_ERROR_INVALID_ARGUMENT );
5356 if( bits % 8 != 0 )
5357 return( PSA_ERROR_INVALID_ARGUMENT );
5358 data = mbedtls_calloc( 1, bytes );
5359 if( data == NULL )
5360 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5361
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005362 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005363 if( status != PSA_SUCCESS )
5364 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005365#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005366 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005367 psa_des_set_key_parity( data, bytes );
5368#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005369 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005370
5371exit:
5372 mbedtls_free( data );
5373 return( status );
5374}
5375
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005376psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005377 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005378 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005379{
5380 psa_status_t status;
5381 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005382 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005383
5384 /* Reject any attempt to create a zero-length key so that we don't
5385 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5386 if( psa_get_key_bits( attributes ) == 0 )
5387 return( PSA_ERROR_INVALID_ARGUMENT );
5388
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005389 if( ! operation->can_output_key )
5390 return( PSA_ERROR_NOT_PERMITTED );
5391
Gilles Peskinedf179142019-07-15 22:02:14 +02005392 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5393 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005394#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5395 if( driver != NULL )
5396 {
5397 /* Deriving a key in a secure element is not implemented yet. */
5398 status = PSA_ERROR_NOT_SUPPORTED;
5399 }
5400#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005401 if( status == PSA_SUCCESS )
5402 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005403 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005404 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005405 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005406 }
5407 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005408 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005409 if( status != PSA_SUCCESS )
5410 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005411 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005412 *handle = 0;
5413 }
5414 return( status );
5415}
5416
Gilles Peskineeab56e42018-07-12 17:12:33 +02005417
5418
5419/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005420/* Key derivation */
5421/****************************************************************/
5422
Gilles Peskine969c5d62019-01-16 15:53:06 +01005423static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005424 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005425 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005426{
John Durkopd0321952020-10-29 21:37:36 -07005427#if !defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
5428 (void)kdf_alg;
5429#endif
Janos Follath5fe19732019-06-20 15:09:30 +01005430 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5431 * initialisers for this union leave some bytes unspecified.) */
5432 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5433
Gilles Peskine969c5d62019-01-16 15:53:06 +01005434 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005435#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005436 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5437 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5438 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005439 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005440 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005441 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5442 if( hash_size == 0 )
5443 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005444 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5445 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005446 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005447 {
5448 return( PSA_ERROR_NOT_SUPPORTED );
5449 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005450 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005451 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005452 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005453 else
John Durkopd0321952020-10-29 21:37:36 -07005454#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005455 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005456}
5457
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005458psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005459 psa_algorithm_t alg )
5460{
5461 psa_status_t status;
5462
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005463 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005464 return( PSA_ERROR_BAD_STATE );
5465
Gilles Peskine6843c292019-01-18 16:44:49 +01005466 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5467 return( PSA_ERROR_INVALID_ARGUMENT );
5468 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005469 {
5470 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005471 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005472 }
5473 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5474 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005475 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005476 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005477 else
5478 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005479
5480 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005481 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005482 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005483}
5484
John Durkopd0321952020-10-29 21:37:36 -07005485#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005486static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005487 psa_algorithm_t hash_alg,
5488 psa_key_derivation_step_t step,
5489 const uint8_t *data,
5490 size_t data_length )
5491{
5492 psa_status_t status;
5493 switch( step )
5494 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005495 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005496 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005497 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005498 status = psa_hmac_setup_internal( &hkdf->hmac,
5499 data, data_length,
5500 hash_alg );
5501 if( status != PSA_SUCCESS )
5502 return( status );
5503 hkdf->state = HKDF_STATE_STARTED;
5504 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005505 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005506 /* If no salt was provided, use an empty salt. */
5507 if( hkdf->state == HKDF_STATE_INIT )
5508 {
5509 status = psa_hmac_setup_internal( &hkdf->hmac,
5510 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005511 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005512 if( status != PSA_SUCCESS )
5513 return( status );
5514 hkdf->state = HKDF_STATE_STARTED;
5515 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005516 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005517 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005518 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5519 data, data_length );
5520 if( status != PSA_SUCCESS )
5521 return( status );
5522 status = psa_hmac_finish_internal( &hkdf->hmac,
5523 hkdf->prk,
5524 sizeof( hkdf->prk ) );
5525 if( status != PSA_SUCCESS )
5526 return( status );
5527 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5528 hkdf->block_number = 0;
5529 hkdf->state = HKDF_STATE_KEYED;
5530 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005531 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005532 if( hkdf->state == HKDF_STATE_OUTPUT )
5533 return( PSA_ERROR_BAD_STATE );
5534 if( hkdf->info_set )
5535 return( PSA_ERROR_BAD_STATE );
5536 hkdf->info_length = data_length;
5537 if( data_length != 0 )
5538 {
5539 hkdf->info = mbedtls_calloc( 1, data_length );
5540 if( hkdf->info == NULL )
5541 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5542 memcpy( hkdf->info, data, data_length );
5543 }
5544 hkdf->info_set = 1;
5545 return( PSA_SUCCESS );
5546 default:
5547 return( PSA_ERROR_INVALID_ARGUMENT );
5548 }
5549}
Janos Follathb03233e2019-06-11 15:30:30 +01005550
Janos Follathf08e2652019-06-13 09:05:41 +01005551static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5552 const uint8_t *data,
5553 size_t data_length )
5554{
5555 if( prf->state != TLS12_PRF_STATE_INIT )
5556 return( PSA_ERROR_BAD_STATE );
5557
Janos Follathd6dce9f2019-07-04 09:11:38 +01005558 if( data_length != 0 )
5559 {
5560 prf->seed = mbedtls_calloc( 1, data_length );
5561 if( prf->seed == NULL )
5562 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005563
Janos Follathd6dce9f2019-07-04 09:11:38 +01005564 memcpy( prf->seed, data, data_length );
5565 prf->seed_length = data_length;
5566 }
Janos Follathf08e2652019-06-13 09:05:41 +01005567
5568 prf->state = TLS12_PRF_STATE_SEED_SET;
5569
5570 return( PSA_SUCCESS );
5571}
5572
Janos Follath81550542019-06-13 14:26:34 +01005573static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5574 psa_algorithm_t hash_alg,
5575 const uint8_t *data,
5576 size_t data_length )
5577{
5578 psa_status_t status;
5579 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5580 return( PSA_ERROR_BAD_STATE );
5581
5582 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5583 if( status != PSA_SUCCESS )
5584 return( status );
5585
5586 prf->state = TLS12_PRF_STATE_KEY_SET;
5587
5588 return( PSA_SUCCESS );
5589}
5590
Janos Follath6660f0e2019-06-17 08:44:03 +01005591static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5592 psa_tls12_prf_key_derivation_t *prf,
5593 psa_algorithm_t hash_alg,
5594 const uint8_t *data,
5595 size_t data_length )
5596{
5597 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005598 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5599 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005600
5601 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5602 return( PSA_ERROR_INVALID_ARGUMENT );
5603
5604 /* Quoting RFC 4279, Section 2:
5605 *
5606 * The premaster secret is formed as follows: if the PSK is N octets
5607 * long, concatenate a uint16 with the value N, N zero octets, a second
5608 * uint16 with the value N, and the PSK itself.
5609 */
5610
Janos Follath40e13932019-06-26 13:22:29 +01005611 *cur++ = ( data_length >> 8 ) & 0xff;
5612 *cur++ = ( data_length >> 0 ) & 0xff;
5613 memset( cur, 0, data_length );
5614 cur += data_length;
5615 *cur++ = pms[0];
5616 *cur++ = pms[1];
5617 memcpy( cur, data, data_length );
5618 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005619
Janos Follath40e13932019-06-26 13:22:29 +01005620 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005621
5622 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5623 return( status );
5624}
5625
Janos Follath63028dd2019-06-13 09:15:47 +01005626static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5627 const uint8_t *data,
5628 size_t data_length )
5629{
5630 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5631 return( PSA_ERROR_BAD_STATE );
5632
Janos Follathd6dce9f2019-07-04 09:11:38 +01005633 if( data_length != 0 )
5634 {
5635 prf->label = mbedtls_calloc( 1, data_length );
5636 if( prf->label == NULL )
5637 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005638
Janos Follathd6dce9f2019-07-04 09:11:38 +01005639 memcpy( prf->label, data, data_length );
5640 prf->label_length = data_length;
5641 }
Janos Follath63028dd2019-06-13 09:15:47 +01005642
5643 prf->state = TLS12_PRF_STATE_LABEL_SET;
5644
5645 return( PSA_SUCCESS );
5646}
5647
Janos Follathb03233e2019-06-11 15:30:30 +01005648static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5649 psa_algorithm_t hash_alg,
5650 psa_key_derivation_step_t step,
5651 const uint8_t *data,
5652 size_t data_length )
5653{
Janos Follathb03233e2019-06-11 15:30:30 +01005654 switch( step )
5655 {
Janos Follathf08e2652019-06-13 09:05:41 +01005656 case PSA_KEY_DERIVATION_INPUT_SEED:
5657 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005658 case PSA_KEY_DERIVATION_INPUT_SECRET:
5659 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005660 case PSA_KEY_DERIVATION_INPUT_LABEL:
5661 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005662 default:
5663 return( PSA_ERROR_INVALID_ARGUMENT );
5664 }
5665}
Janos Follath6660f0e2019-06-17 08:44:03 +01005666
5667static psa_status_t psa_tls12_prf_psk_to_ms_input(
5668 psa_tls12_prf_key_derivation_t *prf,
5669 psa_algorithm_t hash_alg,
5670 psa_key_derivation_step_t step,
5671 const uint8_t *data,
5672 size_t data_length )
5673{
5674 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005675 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005676 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5677 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005678 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005679
5680 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5681}
John Durkopd0321952020-10-29 21:37:36 -07005682#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005683
Gilles Peskineb8965192019-09-24 16:21:10 +02005684/** Check whether the given key type is acceptable for the given
5685 * input step of a key derivation.
5686 *
5687 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5688 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5689 * Both secret and non-secret inputs can alternatively have the type
5690 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5691 * that the input was passed as a buffer rather than via a key object.
5692 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005693static int psa_key_derivation_check_input_type(
5694 psa_key_derivation_step_t step,
5695 psa_key_type_t key_type )
5696{
5697 switch( step )
5698 {
5699 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005700 if( key_type == PSA_KEY_TYPE_DERIVE )
5701 return( PSA_SUCCESS );
5702 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005703 return( PSA_SUCCESS );
5704 break;
5705 case PSA_KEY_DERIVATION_INPUT_LABEL:
5706 case PSA_KEY_DERIVATION_INPUT_SALT:
5707 case PSA_KEY_DERIVATION_INPUT_INFO:
5708 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005709 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5710 return( PSA_SUCCESS );
5711 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005712 return( PSA_SUCCESS );
5713 break;
5714 }
5715 return( PSA_ERROR_INVALID_ARGUMENT );
5716}
5717
Janos Follathb80a94e2019-06-12 15:54:46 +01005718static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005719 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005720 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005721 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005722 const uint8_t *data,
5723 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005724{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005725 psa_status_t status;
John Durkopd0321952020-10-29 21:37:36 -07005726#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005727 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
John Durkopd0321952020-10-29 21:37:36 -07005728#else
5729 (void)data;
5730 (void)data_length;
5731#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005732
5733 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005734 if( status != PSA_SUCCESS )
5735 goto exit;
5736
John Durkopd0321952020-10-29 21:37:36 -07005737#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005738 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005739 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005740 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005741 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005742 step, data, data_length );
5743 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005744 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005745 {
Janos Follathb03233e2019-06-11 15:30:30 +01005746 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5747 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5748 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005749 }
5750 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5751 {
5752 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5753 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5754 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005755 }
5756 else
John Durkopd0321952020-10-29 21:37:36 -07005757#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005758 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005759 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005760 return( PSA_ERROR_BAD_STATE );
5761 }
5762
Gilles Peskine224b0d62019-09-23 18:13:17 +02005763exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005764 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005765 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005766 return( status );
5767}
5768
Janos Follath51f4a0f2019-06-14 11:35:55 +01005769psa_status_t psa_key_derivation_input_bytes(
5770 psa_key_derivation_operation_t *operation,
5771 psa_key_derivation_step_t step,
5772 const uint8_t *data,
5773 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005774{
Gilles Peskineb8965192019-09-24 16:21:10 +02005775 return( psa_key_derivation_input_internal( operation, step,
5776 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005777 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005778}
5779
Janos Follath51f4a0f2019-06-14 11:35:55 +01005780psa_status_t psa_key_derivation_input_key(
5781 psa_key_derivation_operation_t *operation,
5782 psa_key_derivation_step_t step,
5783 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005784{
5785 psa_key_slot_t *slot;
5786 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005787
Gilles Peskine28f8f302019-07-24 13:30:31 +02005788 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005789 PSA_KEY_USAGE_DERIVE,
5790 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005791 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005792 {
5793 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005794 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005795 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005796
5797 /* Passing a key object as a SECRET input unlocks the permission
5798 * to output to a key object. */
5799 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5800 operation->can_output_key = 1;
5801
Janos Follathb80a94e2019-06-12 15:54:46 +01005802 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005803 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005804 slot->data.key.data,
5805 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005806}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005807
5808
5809
5810/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005811/* Key agreement */
5812/****************************************************************/
5813
John Durkopf4c4cb02020-10-28 20:09:55 -07005814#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) && defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005815static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5816 size_t peer_key_length,
5817 const mbedtls_ecp_keypair *our_key,
5818 uint8_t *shared_secret,
5819 size_t shared_secret_size,
5820 size_t *shared_secret_length )
5821{
Steven Cooremand4867872020-08-05 16:31:39 +02005822 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005823 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005824 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005825 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005826 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005827 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005828
Steven Cooreman4fed4552020-08-03 14:46:03 +02005829 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5830 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005831 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005832 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005833 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005834 goto exit;
5835
Jaeden Amero97271b32019-01-10 19:38:51 +00005836 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005837 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005838 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005839 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005840 status = mbedtls_to_psa_error(
5841 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5842 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005843 goto exit;
5844
Jaeden Amero97271b32019-01-10 19:38:51 +00005845 status = mbedtls_to_psa_error(
5846 mbedtls_ecdh_calc_secret( &ecdh,
5847 shared_secret_length,
5848 shared_secret, shared_secret_size,
5849 mbedtls_ctr_drbg_random,
5850 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005851 if( status != PSA_SUCCESS )
5852 goto exit;
5853 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5854 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005855
5856exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005857 if( status != PSA_SUCCESS )
5858 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005859 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005860 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005861 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005862
Jaeden Amero97271b32019-01-10 19:38:51 +00005863 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005864}
John Durkopf4c4cb02020-10-28 20:09:55 -07005865#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH && MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005866
Gilles Peskine01d718c2018-09-18 12:01:02 +02005867#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5868
Gilles Peskine0216fe12019-04-11 21:23:21 +02005869static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5870 psa_key_slot_t *private_key,
5871 const uint8_t *peer_key,
5872 size_t peer_key_length,
5873 uint8_t *shared_secret,
5874 size_t shared_secret_size,
5875 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005876{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005877 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005878 {
John Durkopf4c4cb02020-10-28 20:09:55 -07005879#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) && defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005880 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005881 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005882 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005883 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005884 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005885 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005886 private_key->data.key.data,
5887 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005888 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005889 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005890 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005891 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005892 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005893 shared_secret, shared_secret_size,
5894 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005895 mbedtls_ecp_keypair_free( ecp );
5896 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005897 return( status );
John Durkopf4c4cb02020-10-28 20:09:55 -07005898#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH && MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005899 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005900 (void) private_key;
5901 (void) peer_key;
5902 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005903 (void) shared_secret;
5904 (void) shared_secret_size;
5905 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005906 return( PSA_ERROR_NOT_SUPPORTED );
5907 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005908}
5909
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005910/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005911 * to potentially free embedded data structures and wipe confidential data.
5912 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005913static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005914 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005915 psa_key_slot_t *private_key,
5916 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005917 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005918{
5919 psa_status_t status;
5920 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5921 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005922 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005923
5924 /* Step 1: run the secret agreement algorithm to generate the shared
5925 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005926 status = psa_key_agreement_raw_internal( ka_alg,
5927 private_key,
5928 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005929 shared_secret,
5930 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005931 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005932 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005933 goto exit;
5934
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005935 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005936 * the shared secret. A shared secret is permitted wherever a key
5937 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005938 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005939 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005940 shared_secret,
5941 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005942exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005943 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005944 return( status );
5945}
5946
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005947psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005948 psa_key_derivation_step_t step,
5949 psa_key_handle_t private_key,
5950 const uint8_t *peer_key,
5951 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005952{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005953 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005954 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005955 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005956 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005957 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005958 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005959 if( status != PSA_SUCCESS )
5960 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005961 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005962 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005963 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005964 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005965 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005966 else
5967 {
5968 /* If a private key has been added as SECRET, we allow the derived
5969 * key material to be used as a key in PSA Crypto. */
5970 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5971 operation->can_output_key = 1;
5972 }
Gilles Peskine346797d2018-11-16 16:05:06 +01005973 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005974}
5975
Gilles Peskinebe697d82019-05-16 18:00:41 +02005976psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5977 psa_key_handle_t private_key,
5978 const uint8_t *peer_key,
5979 size_t peer_key_length,
5980 uint8_t *output,
5981 size_t output_size,
5982 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005983{
5984 psa_key_slot_t *slot;
5985 psa_status_t status;
5986
5987 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5988 {
5989 status = PSA_ERROR_INVALID_ARGUMENT;
5990 goto exit;
5991 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005992 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005993 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005994 if( status != PSA_SUCCESS )
5995 goto exit;
5996
5997 status = psa_key_agreement_raw_internal( alg, slot,
5998 peer_key, peer_key_length,
5999 output, output_size,
6000 output_length );
6001
6002exit:
6003 if( status != PSA_SUCCESS )
6004 {
6005 /* If an error happens and is not handled properly, the output
6006 * may be used as a key to protect sensitive data. Arrange for such
6007 * a key to be random, which is likely to result in decryption or
6008 * verification errors. This is better than filling the buffer with
6009 * some constant data such as zeros, which would result in the data
6010 * being protected with a reproducible, easily knowable key.
6011 */
6012 psa_generate_random( output, output_size );
6013 *output_length = output_size;
6014 }
6015 return( status );
6016}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006017
6018
6019/****************************************************************/
6020/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006021/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006022
Gilles Peskine4c317f42018-07-12 01:24:09 +02006023psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006024 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006025{
Janos Follath24eed8d2019-11-22 13:21:35 +00006026 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006027 GUARD_MODULE_INITIALIZED;
6028
Gilles Peskinef181eca2019-08-07 13:49:00 +02006029 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6030 {
6031 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6032 output,
6033 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6034 if( ret != 0 )
6035 return( mbedtls_to_psa_error( ret ) );
6036 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6037 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6038 }
6039
Gilles Peskinee59236f2018-01-27 23:32:46 +01006040 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6041 return( mbedtls_to_psa_error( ret ) );
6042}
6043
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006044#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6045#include "mbedtls/entropy_poll.h"
6046
Gilles Peskine7228da22019-07-15 11:06:15 +02006047psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006048 size_t seed_size )
6049{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006050 if( global_data.initialized )
6051 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006052
6053 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6054 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6055 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6056 return( PSA_ERROR_INVALID_ARGUMENT );
6057
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006058 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006059}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006060#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006061
John Durkop0e005192020-10-31 22:06:54 -07006062#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && defined(MBEDTLS_GENPRIME)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006063static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6064 size_t domain_parameters_size,
6065 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006066{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006067 size_t i;
6068 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006069
Gilles Peskinee56e8782019-04-26 17:34:02 +02006070 if( domain_parameters_size == 0 )
6071 {
6072 *exponent = 65537;
6073 return( PSA_SUCCESS );
6074 }
6075
6076 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6077 * support values that fit in a 32-bit integer, which is larger than
6078 * int on just about every platform anyway. */
6079 if( domain_parameters_size > sizeof( acc ) )
6080 return( PSA_ERROR_NOT_SUPPORTED );
6081 for( i = 0; i < domain_parameters_size; i++ )
6082 acc = ( acc << 8 ) | domain_parameters[i];
6083 if( acc > INT_MAX )
6084 return( PSA_ERROR_NOT_SUPPORTED );
6085 *exponent = acc;
6086 return( PSA_SUCCESS );
6087}
John Durkop0e005192020-10-31 22:06:54 -07006088#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && defined(MBEDTLS_GENPRIME) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006089
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006090static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006091 psa_key_slot_t *slot, size_t bits,
6092 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006093{
Gilles Peskine8e338702019-07-30 20:06:31 +02006094 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006095
Gilles Peskinee56e8782019-04-26 17:34:02 +02006096 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006097 return( PSA_ERROR_INVALID_ARGUMENT );
6098
Gilles Peskinee59236f2018-01-27 23:32:46 +01006099 if( key_type_is_raw_bytes( type ) )
6100 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006101 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006102
6103 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006104 if( status != PSA_SUCCESS )
6105 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006106
6107 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006108 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6109 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006110 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006111
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006112 status = psa_generate_random( slot->data.key.data,
6113 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006114 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006115 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006116
6117 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006118#if defined(MBEDTLS_DES_C)
6119 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006120 psa_des_set_key_parity( slot->data.key.data,
6121 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006122#endif /* MBEDTLS_DES_C */
6123 }
6124 else
6125
John Durkop0e005192020-10-31 22:06:54 -07006126#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006127 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006128 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006129 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006130 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006131 int exponent;
6132 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006133 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6134 return( PSA_ERROR_NOT_SUPPORTED );
6135 /* Accept only byte-aligned keys, for the same reasons as
6136 * in psa_import_rsa_key(). */
6137 if( bits % 8 != 0 )
6138 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006139 status = psa_read_rsa_exponent( domain_parameters,
6140 domain_parameters_size,
6141 &exponent );
6142 if( status != PSA_SUCCESS )
6143 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006144 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6145 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006146 mbedtls_ctr_drbg_random,
6147 &global_data.ctr_drbg,
6148 (unsigned int) bits,
6149 exponent );
6150 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006151 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006152
6153 /* Make sure to always have an export representation available */
6154 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6155
Steven Cooreman75b74362020-07-28 14:30:13 +02006156 status = psa_allocate_buffer_to_slot( slot, bytes );
6157 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006158 {
6159 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006160 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006161 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006162
6163 status = psa_export_rsa_key( type,
6164 &rsa,
6165 slot->data.key.data,
6166 bytes,
6167 &slot->data.key.bytes );
6168 mbedtls_rsa_free( &rsa );
6169 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006170 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006171 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006172 }
6173 else
John Durkop0e005192020-10-31 22:06:54 -07006174#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006175
John Durkopf4c4cb02020-10-28 20:09:55 -07006176#if defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006177 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006178 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006179 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006180 mbedtls_ecp_group_id grp_id =
6181 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006182 const mbedtls_ecp_curve_info *curve_info =
6183 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006184 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006186 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006187 return( PSA_ERROR_NOT_SUPPORTED );
6188 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6189 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006190 mbedtls_ecp_keypair_init( &ecp );
6191 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006192 mbedtls_ctr_drbg_random,
6193 &global_data.ctr_drbg );
6194 if( ret != 0 )
6195 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006196 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006197 return( mbedtls_to_psa_error( ret ) );
6198 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006199
6200
6201 /* Make sure to always have an export representation available */
6202 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006203 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6204 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006205 {
6206 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006207 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006208 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006209
6210 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006211 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6212
6213 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006214 if( status != PSA_SUCCESS ) {
6215 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006216 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006217 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006218 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006219 }
6220 else
John Durkopf4c4cb02020-10-28 20:09:55 -07006221#endif /* defined(MBEDTLS_PSA_BUILTIN_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006222 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006223 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006224 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006225
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006226 return( PSA_SUCCESS );
6227}
Darryl Green0c6575a2018-11-07 16:05:30 +00006228
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006229psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006230 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006231{
6232 psa_status_t status;
6233 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006234 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006235
Gilles Peskine0f84d622019-09-12 19:03:13 +02006236 /* Reject any attempt to create a zero-length key so that we don't
6237 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6238 if( psa_get_key_bits( attributes ) == 0 )
6239 return( PSA_ERROR_INVALID_ARGUMENT );
6240
Gilles Peskinedf179142019-07-15 22:02:14 +02006241 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6242 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006243 if( status != PSA_SUCCESS )
6244 goto exit;
6245
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006246 status = psa_driver_wrapper_generate_key( attributes,
6247 slot );
6248 if( status != PSA_ERROR_NOT_SUPPORTED ||
6249 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6250 goto exit;
6251
6252 status = psa_generate_key_internal(
6253 slot, attributes->core.bits,
6254 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006255
6256exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006257 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006258 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006259 if( status != PSA_SUCCESS )
6260 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006261 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006262 *handle = 0;
6263 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006264 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006265}
6266
6267
Gilles Peskinee59236f2018-01-27 23:32:46 +01006268
6269/****************************************************************/
6270/* Module setup */
6271/****************************************************************/
6272
Gilles Peskine5e769522018-11-20 21:59:56 +01006273psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6274 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6275 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6276{
6277 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6278 return( PSA_ERROR_BAD_STATE );
6279 global_data.entropy_init = entropy_init;
6280 global_data.entropy_free = entropy_free;
6281 return( PSA_SUCCESS );
6282}
6283
Gilles Peskinee59236f2018-01-27 23:32:46 +01006284void mbedtls_psa_crypto_free( void )
6285{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006286 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006287 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6288 {
6289 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006290 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006291 }
6292 /* Wipe all remaining data, including configuration.
6293 * In particular, this sets all state indicator to the value
6294 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006295 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006296#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006297 /* Unregister all secure element drivers, so that we restart from
6298 * a pristine state. */
6299 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006300#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006301}
6302
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006303#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6304/** Recover a transaction that was interrupted by a power failure.
6305 *
6306 * This function is called during initialization, before psa_crypto_init()
6307 * returns. If this function returns a failure status, the initialization
6308 * fails.
6309 */
6310static psa_status_t psa_crypto_recover_transaction(
6311 const psa_crypto_transaction_t *transaction )
6312{
6313 switch( transaction->unknown.type )
6314 {
6315 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6316 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006317 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006318 * is implemented.
6319 * https://github.com/ARMmbed/mbed-crypto/issues/218
6320 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006321 default:
6322 /* We found an unsupported transaction in the storage.
6323 * We don't know what state the storage is in. Give up. */
6324 return( PSA_ERROR_STORAGE_FAILURE );
6325 }
6326}
6327#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6328
Gilles Peskinee59236f2018-01-27 23:32:46 +01006329psa_status_t psa_crypto_init( void )
6330{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006331 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006332 const unsigned char drbg_seed[] = "PSA";
6333
Gilles Peskinec6b69072018-11-20 21:42:52 +01006334 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006335 if( global_data.initialized != 0 )
6336 return( PSA_SUCCESS );
6337
Gilles Peskine5e769522018-11-20 21:59:56 +01006338 /* Set default configuration if
6339 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6340 if( global_data.entropy_init == NULL )
6341 global_data.entropy_init = mbedtls_entropy_init;
6342 if( global_data.entropy_free == NULL )
6343 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006344
Gilles Peskinec6b69072018-11-20 21:42:52 +01006345 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006346 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006347#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6348 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6349 /* The PSA entropy injection feature depends on using NV seed as an entropy
6350 * source. Add NV seed as an entropy source for PSA entropy injection. */
6351 mbedtls_entropy_add_source( &global_data.entropy,
6352 mbedtls_nv_seed_poll, NULL,
6353 MBEDTLS_ENTROPY_BLOCK_SIZE,
6354 MBEDTLS_ENTROPY_SOURCE_STRONG );
6355#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006356 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006357 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006358 status = mbedtls_to_psa_error(
6359 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6360 mbedtls_entropy_func,
6361 &global_data.entropy,
6362 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6363 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006364 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006365 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006366
Gilles Peskine66fb1262018-12-10 16:29:04 +01006367 status = psa_initialize_key_slots( );
6368 if( status != PSA_SUCCESS )
6369 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006370
Gilles Peskined9348f22019-10-01 15:22:29 +02006371#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6372 status = psa_init_all_se_drivers( );
6373 if( status != PSA_SUCCESS )
6374 goto exit;
6375#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6376
Gilles Peskine4b734222019-07-24 15:56:31 +02006377#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006378 status = psa_crypto_load_transaction( );
6379 if( status == PSA_SUCCESS )
6380 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006381 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6382 if( status != PSA_SUCCESS )
6383 goto exit;
6384 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006385 }
6386 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6387 {
6388 /* There's no transaction to complete. It's all good. */
6389 status = PSA_SUCCESS;
6390 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006391#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006392
Gilles Peskinec6b69072018-11-20 21:42:52 +01006393 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006394 global_data.initialized = 1;
6395
6396exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006397 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006398 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006399 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006400}
6401
6402#endif /* MBEDTLS_PSA_CRYPTO_C */