blob: 42d141c19a783e827f94b20fca8cc37b2e28b533 [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 Durkop9814fa22020-11-04 12:28:15 -0800373#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800374 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
375 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
376 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100377mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100378 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200379{
380 switch( curve )
381 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100382 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100383 switch( byte_length )
384 {
385 case PSA_BITS_TO_BYTES( 192 ):
386 return( MBEDTLS_ECP_DP_SECP192R1 );
387 case PSA_BITS_TO_BYTES( 224 ):
388 return( MBEDTLS_ECP_DP_SECP224R1 );
389 case PSA_BITS_TO_BYTES( 256 ):
390 return( MBEDTLS_ECP_DP_SECP256R1 );
391 case PSA_BITS_TO_BYTES( 384 ):
392 return( MBEDTLS_ECP_DP_SECP384R1 );
393 case PSA_BITS_TO_BYTES( 521 ):
394 return( MBEDTLS_ECP_DP_SECP521R1 );
395 default:
396 return( MBEDTLS_ECP_DP_NONE );
397 }
398 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100399
Paul Elliott8ff510a2020-06-02 17:19:28 +0100400 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100401 switch( byte_length )
402 {
403 case PSA_BITS_TO_BYTES( 256 ):
404 return( MBEDTLS_ECP_DP_BP256R1 );
405 case PSA_BITS_TO_BYTES( 384 ):
406 return( MBEDTLS_ECP_DP_BP384R1 );
407 case PSA_BITS_TO_BYTES( 512 ):
408 return( MBEDTLS_ECP_DP_BP512R1 );
409 default:
410 return( MBEDTLS_ECP_DP_NONE );
411 }
412 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100413
Paul Elliott8ff510a2020-06-02 17:19:28 +0100414 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100415 switch( byte_length )
416 {
417 case PSA_BITS_TO_BYTES( 255 ):
418 return( MBEDTLS_ECP_DP_CURVE25519 );
419 case PSA_BITS_TO_BYTES( 448 ):
420 return( MBEDTLS_ECP_DP_CURVE448 );
421 default:
422 return( MBEDTLS_ECP_DP_NONE );
423 }
424 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100425
Paul Elliott8ff510a2020-06-02 17:19:28 +0100426 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100427 switch( byte_length )
428 {
429 case PSA_BITS_TO_BYTES( 192 ):
430 return( MBEDTLS_ECP_DP_SECP192K1 );
431 case PSA_BITS_TO_BYTES( 224 ):
432 return( MBEDTLS_ECP_DP_SECP224K1 );
433 case PSA_BITS_TO_BYTES( 256 ):
434 return( MBEDTLS_ECP_DP_SECP256K1 );
435 default:
436 return( MBEDTLS_ECP_DP_NONE );
437 }
438 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100439
Gilles Peskine12313cd2018-06-20 00:20:32 +0200440 default:
441 return( MBEDTLS_ECP_DP_NONE );
442 }
443}
John Durkop9814fa22020-11-04 12:28:15 -0800444#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800445 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
446 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
447 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200448
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200449static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
450 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200451{
452 /* Check that the bit size is acceptable for the key type */
453 switch( type )
454 {
455 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200456 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200457 case PSA_KEY_TYPE_DERIVE:
458 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200459#if defined(MBEDTLS_AES_C)
460 case PSA_KEY_TYPE_AES:
461 if( bits != 128 && bits != 192 && bits != 256 )
462 return( PSA_ERROR_INVALID_ARGUMENT );
463 break;
464#endif
465#if defined(MBEDTLS_CAMELLIA_C)
466 case PSA_KEY_TYPE_CAMELLIA:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_DES_C)
472 case PSA_KEY_TYPE_DES:
473 if( bits != 64 && bits != 128 && bits != 192 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_ARC4_C)
478 case PSA_KEY_TYPE_ARC4:
479 if( bits < 8 || bits > 2048 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200483#if defined(MBEDTLS_CHACHA20_C)
484 case PSA_KEY_TYPE_CHACHA20:
485 if( bits != 256 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489 default:
490 return( PSA_ERROR_NOT_SUPPORTED );
491 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200492 if( bits % 8 != 0 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200494
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 return( PSA_SUCCESS );
496}
497
John Durkop0e005192020-10-31 22:06:54 -0700498#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
499 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
500 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
John Durkop9814fa22020-11-04 12:28:15 -0800501 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
502 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
503 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana01795d2020-07-24 22:48:15 +0200504
505#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100506/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
507 * that are not a multiple of 8) well. For example, there is only
508 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
509 * way to return the exact bit size of a key.
510 * To keep things simple, reject non-byte-aligned key sizes. */
511static psa_status_t psa_check_rsa_key_byte_aligned(
512 const mbedtls_rsa_context *rsa )
513{
514 mbedtls_mpi n;
515 psa_status_t status;
516 mbedtls_mpi_init( &n );
517 status = mbedtls_to_psa_error(
518 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
519 if( status == PSA_SUCCESS )
520 {
521 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
522 status = PSA_ERROR_NOT_SUPPORTED;
523 }
524 mbedtls_mpi_free( &n );
525 return( status );
526}
Steven Cooremana01795d2020-07-24 22:48:15 +0200527#endif /* MBEDTLS_PK_PARSE_C */
Gilles Peskine86a440b2018-11-12 18:39:40 +0100528
Steven Cooreman7f391872020-07-30 14:57:44 +0200529/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200530 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200531 * \param[in] type The type of key contained in \p data.
532 * \param[in] data The buffer from which to load the representation.
533 * \param[in] data_length The size in bytes of \p data.
534 * \param[out] p_rsa Returns a pointer to an RSA context on success.
535 * The caller is responsible for freeing both the
536 * contents of the context and the context itself
537 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200538 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200539static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
540 const uint8_t *data,
541 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200542 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200543{
Steven Cooremana01795d2020-07-24 22:48:15 +0200544#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000545 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200546 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000547 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200548 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000549
550 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200551 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000552 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200553 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200556 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000557 if( status != PSA_SUCCESS )
558 goto exit;
559
560 /* We have something that the pkparse module recognizes. If it is a
561 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200562 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200563 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000564 status = PSA_ERROR_INVALID_ARGUMENT;
565 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200566 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000567
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000568 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
569 * supports non-byte-aligned key sizes, but not well. For example,
570 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200571 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000572 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
573 {
574 status = PSA_ERROR_NOT_SUPPORTED;
575 goto exit;
576 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200577 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200578 if( status != PSA_SUCCESS )
579 goto exit;
580
Steven Cooremana2371e52020-07-28 14:30:39 +0200581 /* Copy out the pointer to the RSA context, and reset the PK context
582 * such that pk_free doesn't free the RSA context we just grabbed. */
583 *p_rsa = mbedtls_pk_rsa( ctx );
584 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000585
586exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200587 mbedtls_pk_free( &ctx );
588 return( status );
589#else
Steven Cooreman4fed4552020-08-03 14:46:03 +0200590 (void) data;
591 (void) data_length;
Steven Cooreman7f391872020-07-30 14:57:44 +0200592 (void) type;
Steven Cooremana01795d2020-07-24 22:48:15 +0200593 (void) rsa;
594 return( PSA_ERROR_NOT_SUPPORTED );
595#endif /* MBEDTLS_PK_PARSE_C */
596}
597
John Durkop6ba40d12020-11-10 08:50:04 -0800598#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
599 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
600 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
601 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
602 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
603 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
604
605#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
606 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
607
Steven Cooremana01795d2020-07-24 22:48:15 +0200608/** Export an RSA key to export representation
609 *
610 * \param[in] type The type of key (public/private) to export
611 * \param[in] rsa The internal RSA representation from which to export
612 * \param[out] data The buffer to export to
613 * \param[in] data_size The length of the buffer to export to
614 * \param[out] data_length The amount of bytes written to \p data
615 */
616static psa_status_t psa_export_rsa_key( psa_key_type_t type,
617 mbedtls_rsa_context *rsa,
618 uint8_t *data,
619 size_t data_size,
620 size_t *data_length )
621{
622#if defined(MBEDTLS_PK_WRITE_C)
623 int ret;
624 mbedtls_pk_context pk;
625 uint8_t *pos = data + data_size;
626
627 mbedtls_pk_init( &pk );
628 pk.pk_info = &mbedtls_rsa_info;
629 pk.pk_ctx = rsa;
630
631 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200632 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
633 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200634 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
635 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
636 else
637 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
638
639 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200640 {
641 /* Clean up in case pk_write failed halfway through. */
642 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200643 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200644 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200645
646 /* The mbedtls_pk_xxx functions write to the end of the buffer.
647 * Move the data to the beginning and erase remaining data
648 * at the original location. */
649 if( 2 * (size_t) ret <= data_size )
650 {
651 memcpy( data, data + data_size - ret, ret );
652 memset( data + data_size - ret, 0, ret );
653 }
654 else if( (size_t) ret < data_size )
655 {
656 memmove( data, data + data_size - ret, ret );
657 memset( data + ret, 0, data_size - ret );
658 }
659
660 *data_length = ret;
661 return( PSA_SUCCESS );
662#else
663 (void) type;
664 (void) rsa;
665 (void) data;
666 (void) data_size;
667 (void) data_length;
668 return( PSA_ERROR_NOT_SUPPORTED );
669#endif /* MBEDTLS_PK_WRITE_C */
670}
671
672/** Import an RSA key from import representation to a slot
673 *
674 * \param[in,out] slot The slot where to store the export representation to
675 * \param[in] data The buffer containing the import representation
676 * \param[in] data_length The amount of bytes in \p data
677 */
678static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
679 const uint8_t *data,
680 size_t data_length )
681{
682 psa_status_t status;
683 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200684 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200685
Steven Cooremana01795d2020-07-24 22:48:15 +0200686 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200687 status = psa_load_rsa_representation( slot->attr.type,
688 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200689 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200690 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200691 if( status != PSA_SUCCESS )
692 goto exit;
693
694 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200695 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200696
Steven Cooreman75b74362020-07-28 14:30:13 +0200697 /* Re-export the data to PSA export format, such that we can store export
698 * representation in the key slot. Export representation in case of RSA is
699 * the smallest representation that's allowed as input, so a straight-up
700 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200701 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200702 if( output == NULL )
703 {
704 status = PSA_ERROR_INSUFFICIENT_MEMORY;
705 goto exit;
706 }
707
Steven Cooremana01795d2020-07-24 22:48:15 +0200708 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200709 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200710 output,
711 data_length,
712 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200713exit:
714 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200715 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200716 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200717
718 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000719 if( status != PSA_SUCCESS )
720 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200721 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000722 return( status );
723 }
724
Steven Cooremana01795d2020-07-24 22:48:15 +0200725 /* On success, store the allocated export-formatted key. */
726 slot->data.key.data = output;
727 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000728
729 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200730}
John Durkop6ba40d12020-11-10 08:50:04 -0800731
732#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800733 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200734
John Durkop9814fa22020-11-04 12:28:15 -0800735#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800736 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
737 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
738 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
739 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Steven Cooreman7f391872020-07-30 14:57:44 +0200740/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200741 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200742 * \param[in] type The type of key contained in \p data.
743 * \param[in] data The buffer from which to load the representation.
744 * \param[in] data_length The size in bytes of \p data.
745 * \param[out] p_ecp Returns a pointer to an ECP context on success.
746 * The caller is responsible for freeing both the
747 * contents of the context and the context itself
748 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200749 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200750static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
751 const uint8_t *data,
752 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200753 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100754{
755 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200756 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200757 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200758 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100759
Steven Cooreman4fed4552020-08-03 14:46:03 +0200760 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
761 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100762 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200763 /* A Weierstrass public key is represented as:
764 * - The byte 0x04;
765 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
766 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200767 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200768 */
769 if( ( data_length & 1 ) == 0 )
770 return( PSA_ERROR_INVALID_ARGUMENT );
771 curve_size = data_length / 2;
772
773 /* Montgomery public keys are represented in compressed format, meaning
774 * their curve_size is equal to the amount of input. */
775
776 /* Private keys are represented in uncompressed private random integer
777 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100778 }
779
Steven Cooreman6d839f02020-07-30 11:36:45 +0200780 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200781 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200782 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200783 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200784 mbedtls_ecp_keypair_init( ecp );
785
Gilles Peskine4cd32772019-12-02 20:49:42 +0100786 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200787 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
788 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100789 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200790 {
791 status = PSA_ERROR_INVALID_ARGUMENT;
792 goto exit;
793 }
794
Steven Cooremanacda8342020-07-24 23:09:52 +0200795 status = mbedtls_to_psa_error(
796 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
797 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200798 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200799
Steven Cooreman6d839f02020-07-30 11:36:45 +0200800 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200801 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200802 {
803 /* Load the public value. */
804 status = mbedtls_to_psa_error(
805 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200806 data,
807 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200808 if( status != PSA_SUCCESS )
809 goto exit;
810
811 /* Check that the point is on the curve. */
812 status = mbedtls_to_psa_error(
813 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
814 if( status != PSA_SUCCESS )
815 goto exit;
816 }
817 else
818 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200819 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200820 status = mbedtls_to_psa_error(
821 mbedtls_ecp_read_key( ecp->grp.id,
822 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200823 data,
824 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200825 if( status != PSA_SUCCESS )
826 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200827 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200828
829 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200830exit:
831 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200832 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200833 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200834 mbedtls_free( ecp );
835 }
836
Steven Cooreman29149862020-08-05 15:43:42 +0200837 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100838}
John Durkop6ba40d12020-11-10 08:50:04 -0800839#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
840 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
841 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
842 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
843 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000844
John Durkop6ba40d12020-11-10 08:50:04 -0800845#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
846 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200847/** Export an ECP key to export representation
848 *
849 * \param[in] type The type of key (public/private) to export
850 * \param[in] ecp The internal ECP representation from which to export
851 * \param[out] data The buffer to export to
852 * \param[in] data_size The length of the buffer to export to
853 * \param[out] data_length The amount of bytes written to \p data
854 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200855static psa_status_t psa_export_ecp_key( psa_key_type_t type,
856 mbedtls_ecp_keypair *ecp,
857 uint8_t *data,
858 size_t data_size,
859 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200860{
Steven Cooremanacda8342020-07-24 23:09:52 +0200861 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000862
Steven Cooremanacda8342020-07-24 23:09:52 +0200863 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200864 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200865 /* Check whether the public part is loaded */
866 if( mbedtls_ecp_is_zero( &ecp->Q ) )
867 {
868 /* Calculate the public key */
869 status = mbedtls_to_psa_error(
870 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
871 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
872 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200873 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200874 }
875
Steven Cooreman4fed4552020-08-03 14:46:03 +0200876 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200877 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
878 MBEDTLS_ECP_PF_UNCOMPRESSED,
879 data_length,
880 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200881 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200882 if( status != PSA_SUCCESS )
883 memset( data, 0, data_size );
884
Steven Cooreman29149862020-08-05 15:43:42 +0200885 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200886 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200887 else
888 {
Steven Cooreman29149862020-08-05 15:43:42 +0200889 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200890 return( PSA_ERROR_BUFFER_TOO_SMALL );
891
892 status = mbedtls_to_psa_error(
893 mbedtls_ecp_write_key( ecp,
894 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200895 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200896 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200897 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200898 else
899 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200900
901 return( status );
902 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200903}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200904
Steven Cooreman4fed4552020-08-03 14:46:03 +0200905/** Import an ECP key from import representation to a slot
906 *
907 * \param[in,out] slot The slot where to store the export representation to
908 * \param[in] data The buffer containing the import representation
909 * \param[in] data_length The amount of bytes in \p data
910 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200911static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
912 const uint8_t *data,
913 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100914{
Steven Cooremanacda8342020-07-24 23:09:52 +0200915 psa_status_t status;
916 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200917 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100918
Steven Cooremanacda8342020-07-24 23:09:52 +0200919 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200920 status = psa_load_ecp_representation( slot->attr.type,
921 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200922 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200923 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100924 if( status != PSA_SUCCESS )
925 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100926
Steven Cooremanacda8342020-07-24 23:09:52 +0200927 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200928 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200929 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200930 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200931
Steven Cooremanacda8342020-07-24 23:09:52 +0200932 /* Re-export the data to PSA export format. There is currently no support
933 * for other input formats then the export format, so this is a 1-1
934 * copy operation. */
935 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200936 if( output == NULL )
937 {
938 status = PSA_ERROR_INSUFFICIENT_MEMORY;
939 goto exit;
940 }
941
942 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200943 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200944 output,
945 data_length,
946 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100947exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200948 /* Always free the PK object (will also free contained ECP context) */
949 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200950 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200951
952 /* Free the allocated buffer only on error. */
953 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100954 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200955 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200956 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100957 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200958
959 /* On success, store the allocated export-formatted key. */
960 slot->data.key.data = output;
961 slot->data.key.bytes = data_length;
962
963 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100964}
John Durkop9814fa22020-11-04 12:28:15 -0800965#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
966 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100967
Gilles Peskineb46bef22019-07-30 21:32:04 +0200968/** Return the size of the key in the given slot, in bits.
969 *
970 * \param[in] slot A key slot.
971 *
972 * \return The key size in bits, read from the metadata in the slot.
973 */
974static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
975{
976 return( slot->attr.bits );
977}
978
Steven Cooreman75b74362020-07-28 14:30:13 +0200979/** Try to allocate a buffer to an empty key slot.
980 *
981 * \param[in,out] slot Key slot to attach buffer to.
982 * \param[in] buffer_length Requested size of the buffer.
983 *
984 * \retval #PSA_SUCCESS
985 * The buffer has been successfully allocated.
986 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
987 * Not enough memory was available for allocation.
988 * \retval #PSA_ERROR_ALREADY_EXISTS
989 * Trying to allocate a buffer to a non-empty key slot.
990 */
991static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
992 size_t buffer_length )
993{
994 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200995 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200996
997 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
998 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200999 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +02001000
1001 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +02001002 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001003}
1004
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001005psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
1006 const uint8_t* data,
1007 size_t data_length )
1008{
1009 psa_status_t status = psa_allocate_buffer_to_slot( slot,
1010 data_length );
1011 if( status != PSA_SUCCESS )
1012 return( status );
1013
1014 memcpy( slot->data.key.data, data, data_length );
1015 return( PSA_SUCCESS );
1016}
1017
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001018/** Import key data into a slot.
1019 *
1020 * `slot->type` must have been set previously.
1021 * This function assumes that the slot does not contain any key material yet.
1022 * On failure, the slot content is unchanged.
1023 *
1024 * Persistent storage is not affected.
1025 *
1026 * \param[in,out] slot The key slot to import data into.
1027 * Its `type` field must have previously been set to
1028 * the desired key type.
1029 * It must not contain any key material yet.
1030 * \param[in] data Buffer containing the key material to parse and import.
1031 * \param data_length Size of \p data in bytes.
1032 *
1033 * \retval #PSA_SUCCESS
1034 * \retval #PSA_ERROR_INVALID_ARGUMENT
1035 * \retval #PSA_ERROR_NOT_SUPPORTED
1036 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1037 */
1038static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1039 const uint8_t *data,
1040 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001041{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001042 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001043 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001044
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001045 /* zero-length keys are never supported. */
1046 if( data_length == 0 )
1047 return( PSA_ERROR_NOT_SUPPORTED );
1048
Gilles Peskine8e338702019-07-30 20:06:31 +02001049 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001050 {
Steven Cooreman04524762020-10-13 17:43:44 +02001051 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001052
Steven Cooreman75b74362020-07-28 14:30:13 +02001053 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1054 if( data_length > SIZE_MAX / 8 )
1055 return( PSA_ERROR_NOT_SUPPORTED );
1056
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001057 /* Enforce a size limit, and in particular ensure that the bit
1058 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001059 if( bit_size > PSA_MAX_KEY_BITS )
1060 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001061
1062 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001063 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001064 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001065
1066 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001067 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001068 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001069 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001070
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001071 /* Write the actual key size to the slot.
1072 * psa_start_key_creation() wrote the size declared by the
1073 * caller, which may be 0 (meaning unspecified) or wrong. */
1074 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001075
1076 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001077 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001078 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001079 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001080 /* Try validation through accelerators first. */
1081 bit_size = slot->attr.bits;
1082 psa_key_attributes_t attributes = {
1083 .core = slot->attr
1084 };
1085 status = psa_driver_wrapper_validate_key( &attributes,
1086 data,
1087 data_length,
1088 &bit_size );
1089 if( status == PSA_SUCCESS )
1090 {
1091 /* Key has been validated successfully by an accelerator.
1092 * Copy key material into slot. */
1093 status = psa_copy_key_material_into_slot( slot, data, data_length );
1094 if( status != PSA_SUCCESS )
1095 return( status );
1096
1097 slot->attr.bits = (psa_key_bits_t) bit_size;
1098 return( PSA_SUCCESS );
1099 }
1100 else if( status != PSA_ERROR_NOT_SUPPORTED )
1101 return( status );
1102
1103 /* Key format is not supported by any accelerator, try software fallback
1104 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -08001105#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1106 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001107 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1108 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001109 return( psa_import_ecp_key( slot, data, data_length ) );
1110 }
John Durkop9814fa22020-11-04 12:28:15 -08001111#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1112 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -07001113#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1114 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +01001115 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001116 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001117 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001118 }
John Durkop9814fa22020-11-04 12:28:15 -08001119#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1120 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001121
1122 /* Fell through the fallback as well, so have nothing else to try. */
1123 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001124 }
1125 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001126 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001127 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001128 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001129 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001130}
1131
Gilles Peskinef603c712019-01-19 13:40:11 +01001132/** Calculate the intersection of two algorithm usage policies.
1133 *
1134 * Return 0 (which allows no operation) on incompatibility.
1135 */
1136static psa_algorithm_t psa_key_policy_algorithm_intersection(
1137 psa_algorithm_t alg1,
1138 psa_algorithm_t alg2 )
1139{
Gilles Peskine549ea862019-05-22 11:45:59 +02001140 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001141 if( alg1 == alg2 )
1142 return( alg1 );
1143 /* If the policies are from the same hash-and-sign family, check
1144 * if one is a wildcard. If so the other has the specific algorithm. */
1145 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1146 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1147 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1148 {
1149 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1150 return( alg2 );
1151 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1152 return( alg1 );
1153 }
1154 /* If the policies are incompatible, allow nothing. */
1155 return( 0 );
1156}
1157
Gilles Peskined6f371b2019-05-10 19:33:38 +02001158static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1159 psa_algorithm_t requested_alg )
1160{
Gilles Peskine549ea862019-05-22 11:45:59 +02001161 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001162 if( requested_alg == policy_alg )
1163 return( 1 );
1164 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001165 * and requested_alg is the same hash-and-sign family with any hash,
1166 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001167 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1168 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1169 {
1170 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1171 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1172 }
Steven Cooremance48e852020-10-05 16:02:45 +02001173 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001174 * a key derivation with that key agreement should also be allowed. This
1175 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001176 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1177 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1178 {
1179 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1180 policy_alg );
1181 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001182 /* If it isn't permitted, it's forbidden. */
1183 return( 0 );
1184}
1185
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001186/** Test whether a policy permits an algorithm.
1187 *
1188 * The caller must test usage flags separately.
1189 */
1190static int psa_key_policy_permits( const psa_key_policy_t *policy,
1191 psa_algorithm_t alg )
1192{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001193 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1194 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001195}
1196
Gilles Peskinef603c712019-01-19 13:40:11 +01001197/** Restrict a key policy based on a constraint.
1198 *
1199 * \param[in,out] policy The policy to restrict.
1200 * \param[in] constraint The policy constraint to apply.
1201 *
1202 * \retval #PSA_SUCCESS
1203 * \c *policy contains the intersection of the original value of
1204 * \c *policy and \c *constraint.
1205 * \retval #PSA_ERROR_INVALID_ARGUMENT
1206 * \c *policy and \c *constraint are incompatible.
1207 * \c *policy is unchanged.
1208 */
1209static psa_status_t psa_restrict_key_policy(
1210 psa_key_policy_t *policy,
1211 const psa_key_policy_t *constraint )
1212{
1213 psa_algorithm_t intersection_alg =
1214 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001215 psa_algorithm_t intersection_alg2 =
1216 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001217 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1218 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001219 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1220 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001221 policy->usage &= constraint->usage;
1222 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001223 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001224 return( PSA_SUCCESS );
1225}
1226
Darryl Green06fd18d2018-07-16 11:21:11 +01001227/** Retrieve a slot which must contain a key. The key must have allow all the
1228 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1229 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001230static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001231 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001232 psa_key_usage_t usage,
1233 psa_algorithm_t alg )
1234{
1235 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001236 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001237
1238 *p_slot = NULL;
1239
Gilles Peskinec5487a82018-12-03 18:08:14 +01001240 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001241 if( status != PSA_SUCCESS )
1242 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001243
1244 /* Enforce that usage policy for the key slot contains all the flags
1245 * required by the usage parameter. There is one exception: public
1246 * keys can always be exported, so we treat public key objects as
1247 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001248 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001249 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001250 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001251 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001252
1253 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001254 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001255 return( PSA_ERROR_NOT_PERMITTED );
1256
1257 *p_slot = slot;
1258 return( PSA_SUCCESS );
1259}
Darryl Green940d72c2018-07-13 13:18:51 +01001260
Gilles Peskine28f8f302019-07-24 13:30:31 +02001261/** Retrieve a slot which must contain a transparent key.
1262 *
1263 * A transparent key is a key for which the key material is directly
1264 * available, as opposed to a key in a secure element.
1265 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001266 * This is a temporary function to use instead of psa_get_key_from_slot()
1267 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001268 */
1269#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1270static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1271 psa_key_slot_t **p_slot,
1272 psa_key_usage_t usage,
1273 psa_algorithm_t alg )
1274{
1275 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1276 if( status != PSA_SUCCESS )
1277 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001278 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001279 {
1280 *p_slot = NULL;
1281 return( PSA_ERROR_NOT_SUPPORTED );
1282 }
1283 return( PSA_SUCCESS );
1284}
1285#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1286/* With no secure element support, all keys are transparent. */
1287#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1288 psa_get_key_from_slot( handle, p_slot, usage, alg )
1289#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1290
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001291/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001292static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001293{
Gilles Peskine73167e12019-07-12 23:44:37 +02001294#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1295 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001296 {
1297 /* No key material to clean. */
1298 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001299 else
1300#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001301 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001302 /* Data pointer will always be either a valid pointer or NULL in an
1303 * initialized slot, so we can just free it. */
1304 if( slot->data.key.data != NULL )
1305 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001306 mbedtls_free( slot->data.key.data );
1307 slot->data.key.data = NULL;
1308 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001309 }
Darryl Green40225ba2018-11-15 14:48:15 +00001310
1311 return( PSA_SUCCESS );
1312}
1313
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001314/** Completely wipe a slot in memory, including its policy.
1315 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001316psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001317{
1318 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001319 /* Multipart operations may still be using the key. This is safe
1320 * because all multipart operation objects are independent from
1321 * the key slot: if they need to access the key after the setup
1322 * phase, they have a copy of the key. Note that this means that
1323 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001324 /* At this point, key material and other type-specific content has
1325 * been wiped. Clear remaining metadata. We can call memset and not
1326 * zeroize because the metadata is not particularly sensitive. */
1327 memset( slot, 0, sizeof( *slot ) );
1328 return( status );
1329}
1330
Gilles Peskinec5487a82018-12-03 18:08:14 +01001331psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001332{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001333 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001334 psa_status_t status; /* status of the last operation */
1335 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001336#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1337 psa_se_drv_table_entry_t *driver;
1338#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001339
Gilles Peskine1841cf42019-10-08 15:48:25 +02001340 if( handle == 0 )
1341 return( PSA_SUCCESS );
1342
Gilles Peskinec5487a82018-12-03 18:08:14 +01001343 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001344 if( status != PSA_SUCCESS )
1345 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001346
1347#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001348 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001349 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001350 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001351 /* For a key in a secure element, we need to do three things:
1352 * remove the key file in internal storage, destroy the
1353 * key inside the secure element, and update the driver's
1354 * persistent data. Start a transaction that will encompass these
1355 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001356 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001357 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001358 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001359 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001360 status = psa_crypto_save_transaction( );
1361 if( status != PSA_SUCCESS )
1362 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001363 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001364 /* We should still try to destroy the key in the secure
1365 * element and the key metadata in storage. This is especially
1366 * important if the error is that the storage is full.
1367 * But how to do it exactly without risking an inconsistent
1368 * state after a reset?
1369 * https://github.com/ARMmbed/mbed-crypto/issues/215
1370 */
1371 overall_status = status;
1372 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001373 }
1374
Gilles Peskine354f7672019-07-12 23:46:38 +02001375 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001376 if( overall_status == PSA_SUCCESS )
1377 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001378 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001379#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1380
Darryl Greend49a4992018-06-18 17:27:26 +01001381#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001382 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001383 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001384 status = psa_destroy_persistent_key( slot->attr.id );
1385 if( overall_status == PSA_SUCCESS )
1386 overall_status = status;
1387
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001388 /* TODO: other slots may have a copy of the same key. We should
1389 * invalidate them.
1390 * https://github.com/ARMmbed/mbed-crypto/issues/214
1391 */
Darryl Greend49a4992018-06-18 17:27:26 +01001392 }
1393#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001394
Gilles Peskinefc762652019-07-22 19:30:34 +02001395#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1396 if( driver != NULL )
1397 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001398 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001399 if( overall_status == PSA_SUCCESS )
1400 overall_status = status;
1401 status = psa_crypto_stop_transaction( );
1402 if( overall_status == PSA_SUCCESS )
1403 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001404 }
1405#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1406
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001407#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1408exit:
1409#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001410 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001411 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1412 if( overall_status == PSA_SUCCESS )
1413 overall_status = status;
1414 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001415}
1416
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001417void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001418{
Gilles Peskineb699f072019-04-26 16:06:02 +02001419 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001420 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001421}
1422
Gilles Peskineb699f072019-04-26 16:06:02 +02001423psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1424 psa_key_type_t type,
1425 const uint8_t *data,
1426 size_t data_length )
1427{
1428 uint8_t *copy = NULL;
1429
1430 if( data_length != 0 )
1431 {
1432 copy = mbedtls_calloc( 1, data_length );
1433 if( copy == NULL )
1434 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1435 memcpy( copy, data, data_length );
1436 }
1437 /* After this point, this function is guaranteed to succeed, so it
1438 * can start modifying `*attributes`. */
1439
1440 if( attributes->domain_parameters != NULL )
1441 {
1442 mbedtls_free( attributes->domain_parameters );
1443 attributes->domain_parameters = NULL;
1444 attributes->domain_parameters_size = 0;
1445 }
1446
1447 attributes->domain_parameters = copy;
1448 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001449 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001450 return( PSA_SUCCESS );
1451}
1452
1453psa_status_t psa_get_key_domain_parameters(
1454 const psa_key_attributes_t *attributes,
1455 uint8_t *data, size_t data_size, size_t *data_length )
1456{
1457 if( attributes->domain_parameters_size > data_size )
1458 return( PSA_ERROR_BUFFER_TOO_SMALL );
1459 *data_length = attributes->domain_parameters_size;
1460 if( attributes->domain_parameters_size != 0 )
1461 memcpy( data, attributes->domain_parameters,
1462 attributes->domain_parameters_size );
1463 return( PSA_SUCCESS );
1464}
1465
John Durkop6ba40d12020-11-10 08:50:04 -08001466#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
1467 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
1468 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
1469 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
1470 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001471 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001472static psa_status_t psa_get_rsa_public_exponent(
1473 const mbedtls_rsa_context *rsa,
1474 psa_key_attributes_t *attributes )
1475{
1476 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001478 uint8_t *buffer = NULL;
1479 size_t buflen;
1480 mbedtls_mpi_init( &mpi );
1481
1482 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1483 if( ret != 0 )
1484 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001485 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1486 {
1487 /* It's the default value, which is reported as an empty string,
1488 * so there's nothing to do. */
1489 goto exit;
1490 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001491
1492 buflen = mbedtls_mpi_size( &mpi );
1493 buffer = mbedtls_calloc( 1, buflen );
1494 if( buffer == NULL )
1495 {
1496 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1497 goto exit;
1498 }
1499 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1500 if( ret != 0 )
1501 goto exit;
1502 attributes->domain_parameters = buffer;
1503 attributes->domain_parameters_size = buflen;
1504
1505exit:
1506 mbedtls_mpi_free( &mpi );
1507 if( ret != 0 )
1508 mbedtls_free( buffer );
1509 return( mbedtls_to_psa_error( ret ) );
1510}
John Durkop6ba40d12020-11-10 08:50:04 -08001511#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
1512 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
1513 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
1514 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
1515 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001516 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001517
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001518/** Retrieve all the publicly-accessible attributes of a key.
1519 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001520psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1521 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001522{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001523 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001524 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001525
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001526 psa_reset_key_attributes( attributes );
1527
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001528 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001529 if( status != PSA_SUCCESS )
1530 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001531
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001532 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001533 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1534 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1535
1536#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1537 if( psa_key_slot_is_external( slot ) )
1538 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1539#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001540
Gilles Peskine8e338702019-07-30 20:06:31 +02001541 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001542 {
John Durkop6ba40d12020-11-10 08:50:04 -08001543#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
1544 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
1545 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
1546 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
1547 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001548 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001549 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001550 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001551#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001552 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001553 * is not yet implemented.
1554 * https://github.com/ARMmbed/mbed-crypto/issues/216
1555 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001556 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001557 break;
1558#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001559 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001560 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001561
Steven Cooreman4fed4552020-08-03 14:46:03 +02001562 status = psa_load_rsa_representation( slot->attr.type,
1563 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001564 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001565 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001566 if( status != PSA_SUCCESS )
1567 break;
1568
Steven Cooremana2371e52020-07-28 14:30:39 +02001569 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001570 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001571 mbedtls_rsa_free( rsa );
1572 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001573 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001574 break;
John Durkop6ba40d12020-11-10 08:50:04 -08001575#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
1576 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
1577 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
1578 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
1579 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001580 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001581 default:
1582 /* Nothing else to do. */
1583 break;
1584 }
1585
1586 if( status != PSA_SUCCESS )
1587 psa_reset_key_attributes( attributes );
1588 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001589}
1590
Gilles Peskinec8000c02019-08-02 20:15:51 +02001591#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1592psa_status_t psa_get_key_slot_number(
1593 const psa_key_attributes_t *attributes,
1594 psa_key_slot_number_t *slot_number )
1595{
1596 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1597 {
1598 *slot_number = attributes->slot_number;
1599 return( PSA_SUCCESS );
1600 }
1601 else
1602 return( PSA_ERROR_INVALID_ARGUMENT );
1603}
1604#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1605
Steven Cooremana01795d2020-07-24 22:48:15 +02001606static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1607 uint8_t *data,
1608 size_t data_size,
1609 size_t *data_length )
1610{
1611 if( slot->data.key.bytes > data_size )
1612 return( PSA_ERROR_BUFFER_TOO_SMALL );
1613 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1614 memset( data + slot->data.key.bytes, 0,
1615 data_size - slot->data.key.bytes );
1616 *data_length = slot->data.key.bytes;
1617 return( PSA_SUCCESS );
1618}
1619
Gilles Peskinef603c712019-01-19 13:40:11 +01001620static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1621 uint8_t *data,
1622 size_t data_size,
1623 size_t *data_length,
1624 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001625{
Gilles Peskine5d309672019-07-12 23:47:28 +02001626#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1627 const psa_drv_se_t *drv;
1628 psa_drv_se_context_t *drv_context;
1629#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1630
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001631 *data_length = 0;
1632
Gilles Peskine8e338702019-07-30 20:06:31 +02001633 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001634 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001635
Gilles Peskinef9168942019-09-12 19:20:29 +02001636 /* Reject a zero-length output buffer now, since this can never be a
1637 * valid key representation. This way we know that data must be a valid
1638 * pointer and we can do things like memset(data, ..., data_size). */
1639 if( data_size == 0 )
1640 return( PSA_ERROR_BUFFER_TOO_SMALL );
1641
Gilles Peskine5d309672019-07-12 23:47:28 +02001642#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001643 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001644 {
1645 psa_drv_se_export_key_t method;
1646 if( drv->key_management == NULL )
1647 return( PSA_ERROR_NOT_SUPPORTED );
1648 method = ( export_public_key ?
1649 drv->key_management->p_export_public :
1650 drv->key_management->p_export );
1651 if( method == NULL )
1652 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001653 return( method( drv_context,
1654 slot->data.se.slot_number,
1655 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001656 }
1657#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1658
Gilles Peskine8e338702019-07-30 20:06:31 +02001659 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001660 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001661 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001662 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001663 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1664 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001665 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001666 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001667 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001668 /* Exporting public -> public */
1669 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1670 }
1671 else if( !export_public_key )
1672 {
1673 /* Exporting private -> private */
1674 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1675 }
1676 /* Need to export the public part of a private key,
1677 * so conversion is needed */
1678 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1679 {
John Durkop0e005192020-10-31 22:06:54 -07001680#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1681 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001682 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001683 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001684 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001685 slot->data.key.data,
1686 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001687 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001688 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001689 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001690
Steven Cooreman560c28a2020-07-24 23:20:24 +02001691 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001692 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001693 data,
1694 data_size,
1695 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001696
Steven Cooremana2371e52020-07-28 14:30:39 +02001697 mbedtls_rsa_free( rsa );
1698 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001699
Steven Cooreman560c28a2020-07-24 23:20:24 +02001700 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001701#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001702 /* We don't know how to convert a private RSA key to public. */
1703 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001704#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1705 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001706 }
1707 else
Moran Pekera998bc62018-04-16 18:16:20 +03001708 {
John Durkop6ba40d12020-11-10 08:50:04 -08001709#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1710 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001711 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001712 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001713 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001714 slot->data.key.data,
1715 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001716 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001717 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001718 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001719
1720 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1721 PSA_KEY_TYPE_ECC_GET_FAMILY(
1722 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001723 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001724 data,
1725 data_size,
1726 data_length );
1727
Steven Cooremana2371e52020-07-28 14:30:39 +02001728 mbedtls_ecp_keypair_free( ecp );
1729 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001730 return( status );
1731#else
1732 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001733 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001734#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1735 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001736 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001737 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001738 else
1739 {
1740 /* This shouldn't happen in the reference implementation, but
1741 it is valid for a special-purpose implementation to omit
1742 support for exporting certain key types. */
1743 return( PSA_ERROR_NOT_SUPPORTED );
1744 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001745}
1746
Gilles Peskinec5487a82018-12-03 18:08:14 +01001747psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001748 uint8_t *data,
1749 size_t data_size,
1750 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001751{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001752 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001753 psa_status_t status;
1754
1755 /* Set the key to empty now, so that even when there are errors, we always
1756 * set data_length to a value between 0 and data_size. On error, setting
1757 * the key to empty is a good choice because an empty key representation is
1758 * unlikely to be accepted anywhere. */
1759 *data_length = 0;
1760
1761 /* Export requires the EXPORT flag. There is an exception for public keys,
1762 * which don't require any flag, but psa_get_key_from_slot takes
1763 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001764 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001765 if( status != PSA_SUCCESS )
1766 return( status );
1767 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001768 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001769}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001770
Gilles Peskinec5487a82018-12-03 18:08:14 +01001771psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001772 uint8_t *data,
1773 size_t data_size,
1774 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001775{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001776 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001777 psa_status_t status;
1778
1779 /* Set the key to empty now, so that even when there are errors, we always
1780 * set data_length to a value between 0 and data_size. On error, setting
1781 * the key to empty is a good choice because an empty key representation is
1782 * unlikely to be accepted anywhere. */
1783 *data_length = 0;
1784
1785 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001786 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001787 if( status != PSA_SUCCESS )
1788 return( status );
1789 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001790 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001791}
1792
Gilles Peskine91e8c332019-08-02 19:19:39 +02001793#if defined(static_assert)
1794static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1795 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001796static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001797 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001798static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001799 "One or more key attribute flag is listed as both internal-only and external-only" );
1800#endif
1801
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001802/** Validate that a key policy is internally well-formed.
1803 *
1804 * This function only rejects invalid policies. It does not validate the
1805 * consistency of the policy with respect to other attributes of the key
1806 * such as the key type.
1807 */
1808static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001809{
1810 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001811 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001812 PSA_KEY_USAGE_ENCRYPT |
1813 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001814 PSA_KEY_USAGE_SIGN_HASH |
1815 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001816 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1817 return( PSA_ERROR_INVALID_ARGUMENT );
1818
Gilles Peskine4747d192019-04-17 15:05:45 +02001819 return( PSA_SUCCESS );
1820}
1821
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001822/** Validate the internal consistency of key attributes.
1823 *
1824 * This function only rejects invalid attribute values. If does not
1825 * validate the consistency of the attributes with any key data that may
1826 * be involved in the creation of the key.
1827 *
1828 * Call this function early in the key creation process.
1829 *
1830 * \param[in] attributes Key attributes for the new key.
1831 * \param[out] p_drv On any return, the driver for the key, if any.
1832 * NULL for a transparent key.
1833 *
1834 */
1835static psa_status_t psa_validate_key_attributes(
1836 const psa_key_attributes_t *attributes,
1837 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001838{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001839 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001840
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001841 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1842 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001843 if( status != PSA_SUCCESS )
1844 return( status );
1845
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001846 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1847 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001848 if( status != PSA_SUCCESS )
1849 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001850
1851 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001852 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001853 return( status );
1854
1855 /* Refuse to create overly large keys.
1856 * Note that this doesn't trigger on import if the attributes don't
1857 * explicitly specify a size (so psa_get_key_bits returns 0), so
1858 * psa_import_key() needs its own checks. */
1859 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1860 return( PSA_ERROR_NOT_SUPPORTED );
1861
Gilles Peskine91e8c332019-08-02 19:19:39 +02001862 /* Reject invalid flags. These should not be reachable through the API. */
1863 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1864 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1865 return( PSA_ERROR_INVALID_ARGUMENT );
1866
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001867 return( PSA_SUCCESS );
1868}
1869
Gilles Peskine4747d192019-04-17 15:05:45 +02001870/** Prepare a key slot to receive key material.
1871 *
1872 * This function allocates a key slot and sets its metadata.
1873 *
1874 * If this function fails, call psa_fail_key_creation().
1875 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001876 * This function is intended to be used as follows:
1877 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1878 * it with the specified attributes, and assign it a handle.
1879 * -# Populate the slot with the key material.
1880 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1881 * In case of failure at any step, stop the sequence and call
1882 * psa_fail_key_creation().
1883 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001884 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001885 * \param[in] attributes Key attributes for the new key.
1886 * \param[out] handle On success, a handle for the allocated slot.
1887 * \param[out] p_slot On success, a pointer to the prepared slot.
1888 * \param[out] p_drv On any return, the driver for the key, if any.
1889 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001890 *
1891 * \retval #PSA_SUCCESS
1892 * The key slot is ready to receive key material.
1893 * \return If this function fails, the key slot is an invalid state.
1894 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001895 */
1896static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001897 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001898 const psa_key_attributes_t *attributes,
1899 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001900 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001901 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001902{
1903 psa_status_t status;
1904 psa_key_slot_t *slot;
1905
Gilles Peskinedf179142019-07-15 22:02:14 +02001906 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001907 *p_drv = NULL;
1908
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001909 status = psa_validate_key_attributes( attributes, p_drv );
1910 if( status != PSA_SUCCESS )
1911 return( status );
1912
Gilles Peskineedbed562019-08-07 18:19:59 +02001913 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001914 if( status != PSA_SUCCESS )
1915 return( status );
1916 slot = *p_slot;
1917
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001918 /* We're storing the declared bit-size of the key. It's up to each
1919 * creation mechanism to verify that this information is correct.
1920 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001921 * an input (generate, device) but not for those where the bit-size
1922 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001923
1924 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001925
Gilles Peskine91e8c332019-08-02 19:19:39 +02001926 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001927 * external-only flags, query `attributes`. Thanks to the check
1928 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001929 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001930 * may have set. */
1931 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001932
Gilles Peskinecbaff462019-07-12 23:46:04 +02001933#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001934 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001935 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001936 * create the key file in internal storage, create the
1937 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001938 * persistent data. This is done by starting a transaction that will
1939 * encompass these three actions.
1940 * For registering a volatile key, we just need to find an appropriate
1941 * slot number inside the SE. Since the key is designated volatile, creating
1942 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001943 /* The first thing to do is to find a slot number for the new key.
1944 * We save the slot number in persistent storage as part of the
1945 * transaction data. It will be needed to recover if the power
1946 * fails during the key creation process, to clean up on the secure
1947 * element side after restarting. Obtaining a slot number from the
1948 * secure element driver updates its persistent state, but we do not yet
1949 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001950 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001951 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001952 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001953 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001954 &slot->data.se.slot_number );
1955 if( status != PSA_SUCCESS )
1956 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001957
1958 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001959 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001960 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1961 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1962 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1963 psa_crypto_transaction.key.id = slot->attr.id;
1964 status = psa_crypto_save_transaction( );
1965 if( status != PSA_SUCCESS )
1966 {
1967 (void) psa_crypto_stop_transaction( );
1968 return( status );
1969 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001970 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001971 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001972
1973 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1974 {
1975 /* Key registration only makes sense with a secure element. */
1976 return( PSA_ERROR_INVALID_ARGUMENT );
1977 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001978#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1979
Darryl Green0c6575a2018-11-07 16:05:30 +00001980 return( status );
1981}
Gilles Peskine4747d192019-04-17 15:05:45 +02001982
1983/** Finalize the creation of a key once its key material has been set.
1984 *
1985 * This entails writing the key to persistent storage.
1986 *
1987 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001988 * See the documentation of psa_start_key_creation() for the intended use
1989 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001990 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001991 * \param[in,out] slot Pointer to the slot with key material.
1992 * \param[in] driver The secure element driver for the key,
1993 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001994 *
1995 * \retval #PSA_SUCCESS
1996 * The key was successfully created. The handle is now valid.
1997 * \return If this function fails, the key slot is an invalid state.
1998 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001999 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002000static psa_status_t psa_finish_key_creation(
2001 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002002 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002003{
2004 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002005 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002006 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002007
2008#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002009 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002010 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002011#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2012 if( driver != NULL )
2013 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002014 psa_se_key_data_storage_t data;
2015#if defined(static_assert)
2016 static_assert( sizeof( slot->data.se.slot_number ) ==
2017 sizeof( data.slot_number ),
2018 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002019#endif
2020 memcpy( &data.slot_number, &slot->data.se.slot_number,
2021 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002022 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002023 (uint8_t*) &data,
2024 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002025 }
2026 else
2027#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2028 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002029 /* Key material is saved in export representation in the slot, so
2030 * just pass the slot buffer for storage. */
2031 status = psa_save_persistent_key( &slot->attr,
2032 slot->data.key.data,
2033 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002034 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002035 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002036#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2037
Gilles Peskinecbaff462019-07-12 23:46:04 +02002038#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002039 /* Finish the transaction for a key creation. This does not
2040 * happen when registering an existing key. Detect this case
2041 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002042 * creation of a persistent key in a secure element requires a transaction,
2043 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002044 if( driver != NULL &&
2045 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002046 {
2047 status = psa_save_se_persistent_data( driver );
2048 if( status != PSA_SUCCESS )
2049 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002050 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002051 return( status );
2052 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002053 status = psa_crypto_stop_transaction( );
2054 if( status != PSA_SUCCESS )
2055 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002056 }
2057#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2058
Gilles Peskine4747d192019-04-17 15:05:45 +02002059 return( status );
2060}
2061
2062/** Abort the creation of a key.
2063 *
2064 * You may call this function after calling psa_start_key_creation(),
2065 * or after psa_finish_key_creation() fails. In other circumstances, this
2066 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002067 * See the documentation of psa_start_key_creation() for the intended use
2068 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002069 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002070 * \param[in,out] slot Pointer to the slot with key material.
2071 * \param[in] driver The secure element driver for the key,
2072 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002073 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002074static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002075 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002076{
Gilles Peskine011e4282019-06-26 18:34:38 +02002077 (void) driver;
2078
Gilles Peskine4747d192019-04-17 15:05:45 +02002079 if( slot == NULL )
2080 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002081
2082#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002083 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002084 * element, and the failure happened later (when saving metadata
2085 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002086 * element.
2087 * https://github.com/ARMmbed/mbed-crypto/issues/217
2088 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002089
Gilles Peskined7729582019-08-05 15:55:54 +02002090 /* Abort the ongoing transaction if any (there may not be one if
2091 * the creation process failed before starting one, or if the
2092 * key creation is a registration of a key in a secure element).
2093 * Earlier functions must already have done what it takes to undo any
2094 * partial creation. All that's left is to update the transaction data
2095 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002096 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002097#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2098
Gilles Peskine4747d192019-04-17 15:05:45 +02002099 psa_wipe_key_slot( slot );
2100}
2101
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002102/** Validate optional attributes during key creation.
2103 *
2104 * Some key attributes are optional during key creation. If they are
2105 * specified in the attributes structure, check that they are consistent
2106 * with the data in the slot.
2107 *
2108 * This function should be called near the end of key creation, after
2109 * the slot in memory is fully populated but before saving persistent data.
2110 */
2111static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002112 const psa_key_slot_t *slot,
2113 const psa_key_attributes_t *attributes )
2114{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002115 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002116 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002117 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002118 return( PSA_ERROR_INVALID_ARGUMENT );
2119 }
2120
2121 if( attributes->domain_parameters_size != 0 )
2122 {
John Durkop0e005192020-10-31 22:06:54 -07002123#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2124 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002125 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002126 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002127 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002128 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002130
Steven Cooreman7f391872020-07-30 14:57:44 +02002131 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002132 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002133 slot->data.key.data,
2134 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002135 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002136 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002137 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002138
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002139 mbedtls_mpi_init( &actual );
2140 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002141 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002142 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002143 mbedtls_rsa_free( rsa );
2144 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002145 if( ret != 0 )
2146 goto rsa_exit;
2147 ret = mbedtls_mpi_read_binary( &required,
2148 attributes->domain_parameters,
2149 attributes->domain_parameters_size );
2150 if( ret != 0 )
2151 goto rsa_exit;
2152 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2153 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2154 rsa_exit:
2155 mbedtls_mpi_free( &actual );
2156 mbedtls_mpi_free( &required );
2157 if( ret != 0)
2158 return( mbedtls_to_psa_error( ret ) );
2159 }
2160 else
John Durkop9814fa22020-11-04 12:28:15 -08002161#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2162 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002163 {
2164 return( PSA_ERROR_INVALID_ARGUMENT );
2165 }
2166 }
2167
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002168 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002169 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002170 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002171 return( PSA_ERROR_INVALID_ARGUMENT );
2172 }
2173
2174 return( PSA_SUCCESS );
2175}
2176
Gilles Peskine4747d192019-04-17 15:05:45 +02002177psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002178 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002179 size_t data_length,
2180 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002181{
2182 psa_status_t status;
2183 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002184 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002185
Gilles Peskine0f84d622019-09-12 19:03:13 +02002186 /* Reject zero-length symmetric keys (including raw data key objects).
2187 * This also rejects any key which might be encoded as an empty string,
2188 * which is never valid. */
2189 if( data_length == 0 )
2190 return( PSA_ERROR_INVALID_ARGUMENT );
2191
Gilles Peskinedf179142019-07-15 22:02:14 +02002192 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2193 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002194 if( status != PSA_SUCCESS )
2195 goto exit;
2196
Gilles Peskine5d309672019-07-12 23:47:28 +02002197#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2198 if( driver != NULL )
2199 {
2200 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002201 /* The driver should set the number of key bits, however in
2202 * case it doesn't, we initialize bits to an invalid value. */
2203 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002204 if( drv->key_management == NULL ||
2205 drv->key_management->p_import == NULL )
2206 {
2207 status = PSA_ERROR_NOT_SUPPORTED;
2208 goto exit;
2209 }
2210 status = drv->key_management->p_import(
2211 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002212 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002213 &bits );
2214 if( status != PSA_SUCCESS )
2215 goto exit;
2216 if( bits > PSA_MAX_KEY_BITS )
2217 {
2218 status = PSA_ERROR_NOT_SUPPORTED;
2219 goto exit;
2220 }
2221 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002222 }
2223 else
2224#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2225 {
2226 status = psa_import_key_into_slot( slot, data, data_length );
2227 if( status != PSA_SUCCESS )
2228 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002229 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002230 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002231 if( status != PSA_SUCCESS )
2232 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002233
Gilles Peskine011e4282019-06-26 18:34:38 +02002234 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002235exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002236 if( status != PSA_SUCCESS )
2237 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002238 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002239 *handle = 0;
2240 }
2241 return( status );
2242}
2243
Gilles Peskined7729582019-08-05 15:55:54 +02002244#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2245psa_status_t mbedtls_psa_register_se_key(
2246 const psa_key_attributes_t *attributes )
2247{
2248 psa_status_t status;
2249 psa_key_slot_t *slot = NULL;
2250 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002251 psa_key_handle_t handle = 0;
2252
2253 /* Leaving attributes unspecified is not currently supported.
2254 * It could make sense to query the key type and size from the
2255 * secure element, but not all secure elements support this
2256 * and the driver HAL doesn't currently support it. */
2257 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2258 return( PSA_ERROR_NOT_SUPPORTED );
2259 if( psa_get_key_bits( attributes ) == 0 )
2260 return( PSA_ERROR_NOT_SUPPORTED );
2261
2262 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2263 &handle, &slot, &driver );
2264 if( status != PSA_SUCCESS )
2265 goto exit;
2266
Gilles Peskined7729582019-08-05 15:55:54 +02002267 status = psa_finish_key_creation( slot, driver );
2268
2269exit:
2270 if( status != PSA_SUCCESS )
2271 {
2272 psa_fail_key_creation( slot, driver );
2273 }
2274 /* Registration doesn't keep the key in RAM. */
2275 psa_close_key( handle );
2276 return( status );
2277}
2278#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2279
Gilles Peskinef603c712019-01-19 13:40:11 +01002280static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002281 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002282{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002283 psa_status_t status = psa_copy_key_material_into_slot( target,
2284 source->data.key.data,
2285 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002286 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002287 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002288
Steven Cooreman398aee52020-10-13 14:35:45 +02002289 target->attr.type = source->attr.type;
2290 target->attr.bits = source->attr.bits;
2291
2292 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002293}
2294
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002295psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2296 const psa_key_attributes_t *specified_attributes,
2297 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002298{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002299 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002300 psa_key_slot_t *source_slot = NULL;
2301 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002302 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002303 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002304
Gilles Peskine28f8f302019-07-24 13:30:31 +02002305 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002306 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002307 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002308 goto exit;
2309
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002310 status = psa_validate_optional_attributes( source_slot,
2311 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002312 if( status != PSA_SUCCESS )
2313 goto exit;
2314
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002315 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002316 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002317 if( status != PSA_SUCCESS )
2318 goto exit;
2319
Gilles Peskinedf179142019-07-15 22:02:14 +02002320 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2321 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002322 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002323 if( status != PSA_SUCCESS )
2324 goto exit;
2325
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002326#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2327 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002328 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002329 /* Copying to a secure element is not implemented yet. */
2330 status = PSA_ERROR_NOT_SUPPORTED;
2331 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002332 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002333#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002334
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002335 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002336 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002337 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002338
Gilles Peskine011e4282019-06-26 18:34:38 +02002339 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002340exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002341 if( status != PSA_SUCCESS )
2342 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002343 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002344 *target_handle = 0;
2345 }
2346 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002347}
2348
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002349
2350
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002351/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002352/* Message digests */
2353/****************************************************************/
2354
John Durkop0e005192020-10-31 22:06:54 -07002355#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
2356 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2357 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2358 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002359 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002360static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002361{
2362 switch( alg )
2363 {
2364#if defined(MBEDTLS_MD2_C)
2365 case PSA_ALG_MD2:
2366 return( &mbedtls_md2_info );
2367#endif
2368#if defined(MBEDTLS_MD4_C)
2369 case PSA_ALG_MD4:
2370 return( &mbedtls_md4_info );
2371#endif
2372#if defined(MBEDTLS_MD5_C)
2373 case PSA_ALG_MD5:
2374 return( &mbedtls_md5_info );
2375#endif
2376#if defined(MBEDTLS_RIPEMD160_C)
2377 case PSA_ALG_RIPEMD160:
2378 return( &mbedtls_ripemd160_info );
2379#endif
2380#if defined(MBEDTLS_SHA1_C)
2381 case PSA_ALG_SHA_1:
2382 return( &mbedtls_sha1_info );
2383#endif
2384#if defined(MBEDTLS_SHA256_C)
2385 case PSA_ALG_SHA_224:
2386 return( &mbedtls_sha224_info );
2387 case PSA_ALG_SHA_256:
2388 return( &mbedtls_sha256_info );
2389#endif
2390#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002391#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002392 case PSA_ALG_SHA_384:
2393 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002394#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002395 case PSA_ALG_SHA_512:
2396 return( &mbedtls_sha512_info );
2397#endif
2398 default:
2399 return( NULL );
2400 }
2401}
John Durkop9814fa22020-11-04 12:28:15 -08002402#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
2403 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2404 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2405 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2406 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002407
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002408psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2409{
2410 switch( operation->alg )
2411 {
Gilles Peskine81736312018-06-26 15:04:31 +02002412 case 0:
2413 /* The object has (apparently) been initialized but it is not
2414 * in use. It's ok to call abort on such an object, and there's
2415 * nothing to do. */
2416 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002417#if defined(MBEDTLS_MD2_C)
2418 case PSA_ALG_MD2:
2419 mbedtls_md2_free( &operation->ctx.md2 );
2420 break;
2421#endif
2422#if defined(MBEDTLS_MD4_C)
2423 case PSA_ALG_MD4:
2424 mbedtls_md4_free( &operation->ctx.md4 );
2425 break;
2426#endif
2427#if defined(MBEDTLS_MD5_C)
2428 case PSA_ALG_MD5:
2429 mbedtls_md5_free( &operation->ctx.md5 );
2430 break;
2431#endif
2432#if defined(MBEDTLS_RIPEMD160_C)
2433 case PSA_ALG_RIPEMD160:
2434 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2435 break;
2436#endif
2437#if defined(MBEDTLS_SHA1_C)
2438 case PSA_ALG_SHA_1:
2439 mbedtls_sha1_free( &operation->ctx.sha1 );
2440 break;
2441#endif
2442#if defined(MBEDTLS_SHA256_C)
2443 case PSA_ALG_SHA_224:
2444 case PSA_ALG_SHA_256:
2445 mbedtls_sha256_free( &operation->ctx.sha256 );
2446 break;
2447#endif
2448#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002449#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002450 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002451#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002452 case PSA_ALG_SHA_512:
2453 mbedtls_sha512_free( &operation->ctx.sha512 );
2454 break;
2455#endif
2456 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002457 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002458 }
2459 operation->alg = 0;
2460 return( PSA_SUCCESS );
2461}
2462
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002463psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002464 psa_algorithm_t alg )
2465{
Janos Follath24eed8d2019-11-22 13:21:35 +00002466 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002467
2468 /* A context must be freshly initialized before it can be set up. */
2469 if( operation->alg != 0 )
2470 {
2471 return( PSA_ERROR_BAD_STATE );
2472 }
2473
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002474 switch( alg )
2475 {
2476#if defined(MBEDTLS_MD2_C)
2477 case PSA_ALG_MD2:
2478 mbedtls_md2_init( &operation->ctx.md2 );
2479 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2480 break;
2481#endif
2482#if defined(MBEDTLS_MD4_C)
2483 case PSA_ALG_MD4:
2484 mbedtls_md4_init( &operation->ctx.md4 );
2485 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2486 break;
2487#endif
2488#if defined(MBEDTLS_MD5_C)
2489 case PSA_ALG_MD5:
2490 mbedtls_md5_init( &operation->ctx.md5 );
2491 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2492 break;
2493#endif
2494#if defined(MBEDTLS_RIPEMD160_C)
2495 case PSA_ALG_RIPEMD160:
2496 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2497 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2498 break;
2499#endif
2500#if defined(MBEDTLS_SHA1_C)
2501 case PSA_ALG_SHA_1:
2502 mbedtls_sha1_init( &operation->ctx.sha1 );
2503 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2504 break;
2505#endif
2506#if defined(MBEDTLS_SHA256_C)
2507 case PSA_ALG_SHA_224:
2508 mbedtls_sha256_init( &operation->ctx.sha256 );
2509 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2510 break;
2511 case PSA_ALG_SHA_256:
2512 mbedtls_sha256_init( &operation->ctx.sha256 );
2513 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2514 break;
2515#endif
2516#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002517#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002518 case PSA_ALG_SHA_384:
2519 mbedtls_sha512_init( &operation->ctx.sha512 );
2520 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2521 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002522#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002523 case PSA_ALG_SHA_512:
2524 mbedtls_sha512_init( &operation->ctx.sha512 );
2525 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2526 break;
2527#endif
2528 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002529 return( PSA_ALG_IS_HASH( alg ) ?
2530 PSA_ERROR_NOT_SUPPORTED :
2531 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002532 }
2533 if( ret == 0 )
2534 operation->alg = alg;
2535 else
2536 psa_hash_abort( operation );
2537 return( mbedtls_to_psa_error( ret ) );
2538}
2539
2540psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2541 const uint8_t *input,
2542 size_t input_length )
2543{
Janos Follath24eed8d2019-11-22 13:21:35 +00002544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002545
2546 /* Don't require hash implementations to behave correctly on a
2547 * zero-length input, which may have an invalid pointer. */
2548 if( input_length == 0 )
2549 return( PSA_SUCCESS );
2550
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002551 switch( operation->alg )
2552 {
2553#if defined(MBEDTLS_MD2_C)
2554 case PSA_ALG_MD2:
2555 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2556 input, input_length );
2557 break;
2558#endif
2559#if defined(MBEDTLS_MD4_C)
2560 case PSA_ALG_MD4:
2561 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2562 input, input_length );
2563 break;
2564#endif
2565#if defined(MBEDTLS_MD5_C)
2566 case PSA_ALG_MD5:
2567 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2568 input, input_length );
2569 break;
2570#endif
2571#if defined(MBEDTLS_RIPEMD160_C)
2572 case PSA_ALG_RIPEMD160:
2573 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2574 input, input_length );
2575 break;
2576#endif
2577#if defined(MBEDTLS_SHA1_C)
2578 case PSA_ALG_SHA_1:
2579 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2580 input, input_length );
2581 break;
2582#endif
2583#if defined(MBEDTLS_SHA256_C)
2584 case PSA_ALG_SHA_224:
2585 case PSA_ALG_SHA_256:
2586 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2587 input, input_length );
2588 break;
2589#endif
2590#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002591#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002592 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002593#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002594 case PSA_ALG_SHA_512:
2595 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2596 input, input_length );
2597 break;
2598#endif
2599 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002600 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002601 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002602
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002603 if( ret != 0 )
2604 psa_hash_abort( operation );
2605 return( mbedtls_to_psa_error( ret ) );
2606}
2607
2608psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2609 uint8_t *hash,
2610 size_t hash_size,
2611 size_t *hash_length )
2612{
itayzafrir40835d42018-08-02 13:14:17 +03002613 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002614 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002615 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002616
2617 /* Fill the output buffer with something that isn't a valid hash
2618 * (barring an attack on the hash and deliberately-crafted input),
2619 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002620 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002621 /* If hash_size is 0 then hash may be NULL and then the
2622 * call to memset would have undefined behavior. */
2623 if( hash_size != 0 )
2624 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002625
2626 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002627 {
2628 status = PSA_ERROR_BUFFER_TOO_SMALL;
2629 goto exit;
2630 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002631
2632 switch( operation->alg )
2633 {
2634#if defined(MBEDTLS_MD2_C)
2635 case PSA_ALG_MD2:
2636 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2637 break;
2638#endif
2639#if defined(MBEDTLS_MD4_C)
2640 case PSA_ALG_MD4:
2641 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2642 break;
2643#endif
2644#if defined(MBEDTLS_MD5_C)
2645 case PSA_ALG_MD5:
2646 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2647 break;
2648#endif
2649#if defined(MBEDTLS_RIPEMD160_C)
2650 case PSA_ALG_RIPEMD160:
2651 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2652 break;
2653#endif
2654#if defined(MBEDTLS_SHA1_C)
2655 case PSA_ALG_SHA_1:
2656 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2657 break;
2658#endif
2659#if defined(MBEDTLS_SHA256_C)
2660 case PSA_ALG_SHA_224:
2661 case PSA_ALG_SHA_256:
2662 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2663 break;
2664#endif
2665#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002666#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002667 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002668#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002669 case PSA_ALG_SHA_512:
2670 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2671 break;
2672#endif
2673 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002674 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002675 }
itayzafrir40835d42018-08-02 13:14:17 +03002676 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002677
itayzafrir40835d42018-08-02 13:14:17 +03002678exit:
2679 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002680 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002681 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002682 return( psa_hash_abort( operation ) );
2683 }
2684 else
2685 {
2686 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002687 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002688 }
2689}
2690
Gilles Peskine2d277862018-06-18 15:41:12 +02002691psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2692 const uint8_t *hash,
2693 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002694{
2695 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2696 size_t actual_hash_length;
2697 psa_status_t status = psa_hash_finish( operation,
2698 actual_hash, sizeof( actual_hash ),
2699 &actual_hash_length );
2700 if( status != PSA_SUCCESS )
2701 return( status );
2702 if( actual_hash_length != hash_length )
2703 return( PSA_ERROR_INVALID_SIGNATURE );
2704 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2705 return( PSA_ERROR_INVALID_SIGNATURE );
2706 return( PSA_SUCCESS );
2707}
2708
Gilles Peskine0a749c82019-11-28 19:33:58 +01002709psa_status_t psa_hash_compute( psa_algorithm_t alg,
2710 const uint8_t *input, size_t input_length,
2711 uint8_t *hash, size_t hash_size,
2712 size_t *hash_length )
2713{
2714 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2715 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2716
2717 *hash_length = hash_size;
2718 status = psa_hash_setup( &operation, alg );
2719 if( status != PSA_SUCCESS )
2720 goto exit;
2721 status = psa_hash_update( &operation, input, input_length );
2722 if( status != PSA_SUCCESS )
2723 goto exit;
2724 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2725 if( status != PSA_SUCCESS )
2726 goto exit;
2727
2728exit:
2729 if( status == PSA_SUCCESS )
2730 status = psa_hash_abort( &operation );
2731 else
2732 psa_hash_abort( &operation );
2733 return( status );
2734}
2735
2736psa_status_t psa_hash_compare( psa_algorithm_t alg,
2737 const uint8_t *input, size_t input_length,
2738 const uint8_t *hash, size_t hash_length )
2739{
2740 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2741 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2742
2743 status = psa_hash_setup( &operation, alg );
2744 if( status != PSA_SUCCESS )
2745 goto exit;
2746 status = psa_hash_update( &operation, input, input_length );
2747 if( status != PSA_SUCCESS )
2748 goto exit;
2749 status = psa_hash_verify( &operation, hash, hash_length );
2750 if( status != PSA_SUCCESS )
2751 goto exit;
2752
2753exit:
2754 if( status == PSA_SUCCESS )
2755 status = psa_hash_abort( &operation );
2756 else
2757 psa_hash_abort( &operation );
2758 return( status );
2759}
2760
Gilles Peskineeb35d782019-01-22 17:56:16 +01002761psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2762 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002763{
2764 if( target_operation->alg != 0 )
2765 return( PSA_ERROR_BAD_STATE );
2766
2767 switch( source_operation->alg )
2768 {
2769 case 0:
2770 return( PSA_ERROR_BAD_STATE );
2771#if defined(MBEDTLS_MD2_C)
2772 case PSA_ALG_MD2:
2773 mbedtls_md2_clone( &target_operation->ctx.md2,
2774 &source_operation->ctx.md2 );
2775 break;
2776#endif
2777#if defined(MBEDTLS_MD4_C)
2778 case PSA_ALG_MD4:
2779 mbedtls_md4_clone( &target_operation->ctx.md4,
2780 &source_operation->ctx.md4 );
2781 break;
2782#endif
2783#if defined(MBEDTLS_MD5_C)
2784 case PSA_ALG_MD5:
2785 mbedtls_md5_clone( &target_operation->ctx.md5,
2786 &source_operation->ctx.md5 );
2787 break;
2788#endif
2789#if defined(MBEDTLS_RIPEMD160_C)
2790 case PSA_ALG_RIPEMD160:
2791 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2792 &source_operation->ctx.ripemd160 );
2793 break;
2794#endif
2795#if defined(MBEDTLS_SHA1_C)
2796 case PSA_ALG_SHA_1:
2797 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2798 &source_operation->ctx.sha1 );
2799 break;
2800#endif
2801#if defined(MBEDTLS_SHA256_C)
2802 case PSA_ALG_SHA_224:
2803 case PSA_ALG_SHA_256:
2804 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2805 &source_operation->ctx.sha256 );
2806 break;
2807#endif
2808#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002809#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002810 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002811#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002812 case PSA_ALG_SHA_512:
2813 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2814 &source_operation->ctx.sha512 );
2815 break;
2816#endif
2817 default:
2818 return( PSA_ERROR_NOT_SUPPORTED );
2819 }
2820
2821 target_operation->alg = source_operation->alg;
2822 return( PSA_SUCCESS );
2823}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002824
2825
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002826/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002827/* MAC */
2828/****************************************************************/
2829
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002830static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002831 psa_algorithm_t alg,
2832 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002833 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002834 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002835{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002836 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002837 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002838
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002839 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002840 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002841
Gilles Peskine8c9def32018-02-08 10:02:12 +01002842 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2843 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002844 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002845 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002846 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002847 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002848 mode = MBEDTLS_MODE_STREAM;
2849 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002850 case PSA_ALG_CTR:
2851 mode = MBEDTLS_MODE_CTR;
2852 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002853 case PSA_ALG_CFB:
2854 mode = MBEDTLS_MODE_CFB;
2855 break;
2856 case PSA_ALG_OFB:
2857 mode = MBEDTLS_MODE_OFB;
2858 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002859 case PSA_ALG_ECB_NO_PADDING:
2860 mode = MBEDTLS_MODE_ECB;
2861 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002862 case PSA_ALG_CBC_NO_PADDING:
2863 mode = MBEDTLS_MODE_CBC;
2864 break;
2865 case PSA_ALG_CBC_PKCS7:
2866 mode = MBEDTLS_MODE_CBC;
2867 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002868 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002869 mode = MBEDTLS_MODE_CCM;
2870 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002871 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002872 mode = MBEDTLS_MODE_GCM;
2873 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002874 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2875 mode = MBEDTLS_MODE_CHACHAPOLY;
2876 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002877 default:
2878 return( NULL );
2879 }
2880 }
2881 else if( alg == PSA_ALG_CMAC )
2882 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002883 else
2884 return( NULL );
2885
2886 switch( key_type )
2887 {
2888 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002889 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002890 break;
2891 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002892 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2893 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002894 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002895 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002896 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002897 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002898 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2899 * but two-key Triple-DES is functionally three-key Triple-DES
2900 * with K1=K3, so that's how we present it to mbedtls. */
2901 if( key_bits == 128 )
2902 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002903 break;
2904 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002905 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002906 break;
2907 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002908 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002909 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002910 case PSA_KEY_TYPE_CHACHA20:
2911 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2912 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002913 default:
2914 return( NULL );
2915 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002916 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002917 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002918
Jaeden Amero23bbb752018-06-26 14:16:54 +01002919 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2920 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002921}
2922
John Durkop6ba40d12020-11-10 08:50:04 -08002923#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002924static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002925{
Gilles Peskine2d277862018-06-18 15:41:12 +02002926 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002927 {
2928 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002929 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002930 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002931 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002932 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002933 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002934 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002935 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002936 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002937 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002938 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002939 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002940 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002941 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002942 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002943 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002944 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002945 return( 128 );
2946 default:
2947 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002948 }
2949}
John Durkop6ba40d12020-11-10 08:50:04 -08002950#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002951
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002952/* Initialize the MAC operation structure. Once this function has been
2953 * called, psa_mac_abort can run and will do the right thing. */
2954static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2955 psa_algorithm_t alg )
2956{
2957 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2958
2959 operation->alg = alg;
2960 operation->key_set = 0;
2961 operation->iv_set = 0;
2962 operation->iv_required = 0;
2963 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002964 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002965
2966#if defined(MBEDTLS_CMAC_C)
2967 if( alg == PSA_ALG_CMAC )
2968 {
2969 operation->iv_required = 0;
2970 mbedtls_cipher_init( &operation->ctx.cmac );
2971 status = PSA_SUCCESS;
2972 }
2973 else
2974#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002975#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002976 if( PSA_ALG_IS_HMAC( operation->alg ) )
2977 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002978 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2979 operation->ctx.hmac.hash_ctx.alg = 0;
2980 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002981 }
2982 else
John Durkopd0321952020-10-29 21:37:36 -07002983#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002984 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002985 if( ! PSA_ALG_IS_MAC( alg ) )
2986 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002987 }
2988
2989 if( status != PSA_SUCCESS )
2990 memset( operation, 0, sizeof( *operation ) );
2991 return( status );
2992}
2993
John Durkop6ba40d12020-11-10 08:50:04 -08002994#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002995static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2996{
Gilles Peskine3f108122018-12-07 18:14:53 +01002997 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002998 return( psa_hash_abort( &hmac->hash_ctx ) );
2999}
John Durkop6ba40d12020-11-10 08:50:04 -08003000#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003001
Gilles Peskine8c9def32018-02-08 10:02:12 +01003002psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3003{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003004 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003005 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003006 /* The object has (apparently) been initialized but it is not
3007 * in use. It's ok to call abort on such an object, and there's
3008 * nothing to do. */
3009 return( PSA_SUCCESS );
3010 }
3011 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003012#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003013 if( operation->alg == PSA_ALG_CMAC )
3014 {
3015 mbedtls_cipher_free( &operation->ctx.cmac );
3016 }
3017 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003018#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003019#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003020 if( PSA_ALG_IS_HMAC( operation->alg ) )
3021 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003022 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003023 }
3024 else
John Durkopd0321952020-10-29 21:37:36 -07003025#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003026 {
3027 /* Sanity check (shouldn't happen: operation->alg should
3028 * always have been initialized to a valid value). */
3029 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003030 }
Moran Peker41deec42018-04-04 15:43:05 +03003031
Gilles Peskine8c9def32018-02-08 10:02:12 +01003032 operation->alg = 0;
3033 operation->key_set = 0;
3034 operation->iv_set = 0;
3035 operation->iv_required = 0;
3036 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003037 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003038
Gilles Peskine8c9def32018-02-08 10:02:12 +01003039 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003040
3041bad_state:
3042 /* If abort is called on an uninitialized object, we can't trust
3043 * anything. Wipe the object in case it contains confidential data.
3044 * This may result in a memory leak if a pointer gets overwritten,
3045 * but it's too late to do anything about this. */
3046 memset( operation, 0, sizeof( *operation ) );
3047 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003048}
3049
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003050#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003051static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003052 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003053 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003054 const mbedtls_cipher_info_t *cipher_info )
3055{
Janos Follath24eed8d2019-11-22 13:21:35 +00003056 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003057
3058 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003059
3060 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3061 if( ret != 0 )
3062 return( ret );
3063
3064 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003065 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003066 key_bits );
3067 return( ret );
3068}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003069#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003070
John Durkop6ba40d12020-11-10 08:50:04 -08003071#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003072static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3073 const uint8_t *key,
3074 size_t key_length,
3075 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003076{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003077 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003078 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003079 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003080 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003081 psa_status_t status;
3082
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003083 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3084 * overflow below. This should never trigger if the hash algorithm
3085 * is implemented correctly. */
3086 /* The size checks against the ipad and opad buffers cannot be written
3087 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3088 * because that triggers -Wlogical-op on GCC 7.3. */
3089 if( block_size > sizeof( ipad ) )
3090 return( PSA_ERROR_NOT_SUPPORTED );
3091 if( block_size > sizeof( hmac->opad ) )
3092 return( PSA_ERROR_NOT_SUPPORTED );
3093 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003094 return( PSA_ERROR_NOT_SUPPORTED );
3095
Gilles Peskined223b522018-06-11 18:12:58 +02003096 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003097 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003098 status = psa_hash_compute( hash_alg, key, key_length,
3099 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003100 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003101 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003102 }
Gilles Peskine96889972018-07-12 17:07:03 +02003103 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3104 * but it is permitted. It is common when HMAC is used in HKDF, for
3105 * example. Don't call `memcpy` in the 0-length because `key` could be
3106 * an invalid pointer which would make the behavior undefined. */
3107 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003108 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003109
Gilles Peskined223b522018-06-11 18:12:58 +02003110 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3111 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003112 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003113 ipad[i] ^= 0x36;
3114 memset( ipad + key_length, 0x36, block_size - key_length );
3115
3116 /* Copy the key material from ipad to opad, flipping the requisite bits,
3117 * and filling the rest of opad with the requisite constant. */
3118 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003119 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3120 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003121
Gilles Peskine01126fa2018-07-12 17:04:55 +02003122 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003123 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003124 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003125
Gilles Peskine01126fa2018-07-12 17:04:55 +02003126 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003127
3128cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003129 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003130
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003131 return( status );
3132}
John Durkop6ba40d12020-11-10 08:50:04 -08003133#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003134
Gilles Peskine89167cb2018-07-08 20:12:23 +02003135static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003136 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003137 psa_algorithm_t alg,
3138 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003139{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003140 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003141 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003142 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003143 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003144 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003145 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003146 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003147
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003148 /* A context must be freshly initialized before it can be set up. */
3149 if( operation->alg != 0 )
3150 {
3151 return( PSA_ERROR_BAD_STATE );
3152 }
3153
Gilles Peskined911eb72018-08-14 15:18:45 +02003154 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003155 if( status != PSA_SUCCESS )
3156 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003157 if( is_sign )
3158 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003159
Gilles Peskine28f8f302019-07-24 13:30:31 +02003160 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003161 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003162 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003163 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003164
Gilles Peskine8c9def32018-02-08 10:02:12 +01003165#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003166 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003167 {
3168 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003169 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003170 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003172 if( cipher_info == NULL )
3173 {
3174 status = PSA_ERROR_NOT_SUPPORTED;
3175 goto exit;
3176 }
3177 operation->mac_size = cipher_info->block_size;
3178 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3179 status = mbedtls_to_psa_error( ret );
3180 }
3181 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003182#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003183#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003184 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003185 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003186 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003187 if( hash_alg == 0 )
3188 {
3189 status = PSA_ERROR_NOT_SUPPORTED;
3190 goto exit;
3191 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003192
3193 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3194 /* Sanity check. This shouldn't fail on a valid configuration. */
3195 if( operation->mac_size == 0 ||
3196 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3197 {
3198 status = PSA_ERROR_NOT_SUPPORTED;
3199 goto exit;
3200 }
3201
Gilles Peskine8e338702019-07-30 20:06:31 +02003202 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003203 {
3204 status = PSA_ERROR_INVALID_ARGUMENT;
3205 goto exit;
3206 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003207
Gilles Peskine01126fa2018-07-12 17:04:55 +02003208 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003209 slot->data.key.data,
3210 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003211 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003212 }
3213 else
John Durkopd0321952020-10-29 21:37:36 -07003214#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003215 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003216 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003217 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003218 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003219
Gilles Peskined911eb72018-08-14 15:18:45 +02003220 if( truncated == 0 )
3221 {
3222 /* The "normal" case: untruncated algorithm. Nothing to do. */
3223 }
3224 else if( truncated < 4 )
3225 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003226 /* A very short MAC is too short for security since it can be
3227 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3228 * so we make this our minimum, even though 32 bits is still
3229 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003230 status = PSA_ERROR_NOT_SUPPORTED;
3231 }
3232 else if( truncated > operation->mac_size )
3233 {
3234 /* It's impossible to "truncate" to a larger length. */
3235 status = PSA_ERROR_INVALID_ARGUMENT;
3236 }
3237 else
3238 operation->mac_size = truncated;
3239
Gilles Peskinefbfac682018-07-08 20:51:54 +02003240exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003241 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003242 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003243 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003244 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003245 else
3246 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003247 operation->key_set = 1;
3248 }
3249 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003250}
3251
Gilles Peskine89167cb2018-07-08 20:12:23 +02003252psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003253 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003254 psa_algorithm_t alg )
3255{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003256 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003257}
3258
3259psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003260 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003261 psa_algorithm_t alg )
3262{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003263 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003264}
3265
Gilles Peskine8c9def32018-02-08 10:02:12 +01003266psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3267 const uint8_t *input,
3268 size_t input_length )
3269{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003270 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003271 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003272 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003273 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003274 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003275 operation->has_input = 1;
3276
Gilles Peskine8c9def32018-02-08 10:02:12 +01003277#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003278 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003279 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003280 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3281 input, input_length );
3282 status = mbedtls_to_psa_error( ret );
3283 }
3284 else
3285#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003286#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003287 if( PSA_ALG_IS_HMAC( operation->alg ) )
3288 {
3289 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3290 input_length );
3291 }
3292 else
John Durkopd0321952020-10-29 21:37:36 -07003293#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003294 {
3295 /* This shouldn't happen if `operation` was initialized by
3296 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003297 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003298 }
3299
Gilles Peskinefbfac682018-07-08 20:51:54 +02003300 if( status != PSA_SUCCESS )
3301 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003302 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003303}
3304
John Durkop6ba40d12020-11-10 08:50:04 -08003305#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003306static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3307 uint8_t *mac,
3308 size_t mac_size )
3309{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003310 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003311 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3312 size_t hash_size = 0;
3313 size_t block_size = psa_get_hash_block_size( hash_alg );
3314 psa_status_t status;
3315
Gilles Peskine01126fa2018-07-12 17:04:55 +02003316 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3317 if( status != PSA_SUCCESS )
3318 return( status );
3319 /* From here on, tmp needs to be wiped. */
3320
3321 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3322 if( status != PSA_SUCCESS )
3323 goto exit;
3324
3325 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3326 if( status != PSA_SUCCESS )
3327 goto exit;
3328
3329 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3330 if( status != PSA_SUCCESS )
3331 goto exit;
3332
Gilles Peskined911eb72018-08-14 15:18:45 +02003333 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3334 if( status != PSA_SUCCESS )
3335 goto exit;
3336
3337 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003338
3339exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003340 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003341 return( status );
3342}
John Durkop6ba40d12020-11-10 08:50:04 -08003343#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003344
mohammad16036df908f2018-04-02 08:34:15 -07003345static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003346 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003347 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003348{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003349 if( ! operation->key_set )
3350 return( PSA_ERROR_BAD_STATE );
3351 if( operation->iv_required && ! operation->iv_set )
3352 return( PSA_ERROR_BAD_STATE );
3353
Gilles Peskine8c9def32018-02-08 10:02:12 +01003354 if( mac_size < operation->mac_size )
3355 return( PSA_ERROR_BUFFER_TOO_SMALL );
3356
Gilles Peskine8c9def32018-02-08 10:02:12 +01003357#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003358 if( operation->alg == PSA_ALG_CMAC )
3359 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003360 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3361 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3362 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003363 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003364 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003365 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003366 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003367 else
3368#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003369#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003370 if( PSA_ALG_IS_HMAC( operation->alg ) )
3371 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003372 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003373 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003374 }
3375 else
John Durkopd0321952020-10-29 21:37:36 -07003376#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003377 {
3378 /* This shouldn't happen if `operation` was initialized by
3379 * a setup function. */
3380 return( PSA_ERROR_BAD_STATE );
3381 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003382}
3383
Gilles Peskineacd4be32018-07-08 19:56:25 +02003384psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3385 uint8_t *mac,
3386 size_t mac_size,
3387 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003388{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003389 psa_status_t status;
3390
Jaeden Amero252ef282019-02-15 14:05:35 +00003391 if( operation->alg == 0 )
3392 {
3393 return( PSA_ERROR_BAD_STATE );
3394 }
3395
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003396 /* Fill the output buffer with something that isn't a valid mac
3397 * (barring an attack on the mac and deliberately-crafted input),
3398 * in case the caller doesn't check the return status properly. */
3399 *mac_length = mac_size;
3400 /* If mac_size is 0 then mac may be NULL and then the
3401 * call to memset would have undefined behavior. */
3402 if( mac_size != 0 )
3403 memset( mac, '!', mac_size );
3404
Gilles Peskine89167cb2018-07-08 20:12:23 +02003405 if( ! operation->is_sign )
3406 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003407 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003408 }
mohammad16036df908f2018-04-02 08:34:15 -07003409
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003410 status = psa_mac_finish_internal( operation, mac, mac_size );
3411
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003412 if( status == PSA_SUCCESS )
3413 {
3414 status = psa_mac_abort( operation );
3415 if( status == PSA_SUCCESS )
3416 *mac_length = operation->mac_size;
3417 else
3418 memset( mac, '!', mac_size );
3419 }
3420 else
3421 psa_mac_abort( operation );
3422 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003423}
3424
Gilles Peskineacd4be32018-07-08 19:56:25 +02003425psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3426 const uint8_t *mac,
3427 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003428{
Gilles Peskine828ed142018-06-18 23:25:51 +02003429 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003430 psa_status_t status;
3431
Jaeden Amero252ef282019-02-15 14:05:35 +00003432 if( operation->alg == 0 )
3433 {
3434 return( PSA_ERROR_BAD_STATE );
3435 }
3436
Gilles Peskine89167cb2018-07-08 20:12:23 +02003437 if( operation->is_sign )
3438 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003439 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003440 }
3441 if( operation->mac_size != mac_length )
3442 {
3443 status = PSA_ERROR_INVALID_SIGNATURE;
3444 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003445 }
mohammad16036df908f2018-04-02 08:34:15 -07003446
3447 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003448 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003449 if( status != PSA_SUCCESS )
3450 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003451
3452 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3453 status = PSA_ERROR_INVALID_SIGNATURE;
3454
3455cleanup:
3456 if( status == PSA_SUCCESS )
3457 status = psa_mac_abort( operation );
3458 else
3459 psa_mac_abort( operation );
3460
Gilles Peskine3f108122018-12-07 18:14:53 +01003461 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003462
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003463 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003464}
3465
3466
Gilles Peskine20035e32018-02-03 22:44:14 +01003467
Gilles Peskine20035e32018-02-03 22:44:14 +01003468/****************************************************************/
3469/* Asymmetric cryptography */
3470/****************************************************************/
3471
John Durkop6ba40d12020-11-10 08:50:04 -08003472#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3473 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003474/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003475 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003476static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3477 size_t hash_length,
3478 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003479{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003480 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003481 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003482 *md_alg = mbedtls_md_get_type( md_info );
3483
3484 /* The Mbed TLS RSA module uses an unsigned int for hash length
3485 * parameters. Validate that it fits so that we don't risk an
3486 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003487#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003488 if( hash_length > UINT_MAX )
3489 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003490#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003491
John Durkop0e005192020-10-31 22:06:54 -07003492#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003493 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3494 * must be correct. */
3495 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3496 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003497 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003498 if( md_info == NULL )
3499 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003500 if( mbedtls_md_get_size( md_info ) != hash_length )
3501 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003502 }
John Durkop0e005192020-10-31 22:06:54 -07003503#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003504
John Durkop0e005192020-10-31 22:06:54 -07003505#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003506 /* PSS requires a hash internally. */
3507 if( PSA_ALG_IS_RSA_PSS( alg ) )
3508 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003509 if( md_info == NULL )
3510 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003511 }
John Durkop0e005192020-10-31 22:06:54 -07003512#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003513
Gilles Peskine61b91d42018-06-08 16:09:36 +02003514 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003515}
3516
Gilles Peskine2b450e32018-06-27 15:42:46 +02003517static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3518 psa_algorithm_t alg,
3519 const uint8_t *hash,
3520 size_t hash_length,
3521 uint8_t *signature,
3522 size_t signature_size,
3523 size_t *signature_length )
3524{
3525 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003527 mbedtls_md_type_t md_alg;
John Durkop0e005192020-10-31 22:06:54 -07003528#if !defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
3529 (void)hash;
3530 (void)signature;
3531#endif /* !MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003532
3533 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3534 if( status != PSA_SUCCESS )
3535 return( status );
3536
Gilles Peskine630a18a2018-06-29 17:49:35 +02003537 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003538 return( PSA_ERROR_BUFFER_TOO_SMALL );
3539
John Durkop0e005192020-10-31 22:06:54 -07003540#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003541 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3542 {
3543 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3544 MBEDTLS_MD_NONE );
3545 ret = mbedtls_rsa_pkcs1_sign( rsa,
3546 mbedtls_ctr_drbg_random,
3547 &global_data.ctr_drbg,
3548 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003549 md_alg,
3550 (unsigned int) hash_length,
3551 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003552 signature );
3553 }
3554 else
John Durkop0e005192020-10-31 22:06:54 -07003555#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3556#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003557 if( PSA_ALG_IS_RSA_PSS( alg ) )
3558 {
3559 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3560 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3561 mbedtls_ctr_drbg_random,
3562 &global_data.ctr_drbg,
3563 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003564 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003565 (unsigned int) hash_length,
3566 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003567 signature );
3568 }
3569 else
John Durkop0e005192020-10-31 22:06:54 -07003570#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003571 {
3572 return( PSA_ERROR_INVALID_ARGUMENT );
3573 }
3574
3575 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003576 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003577 return( mbedtls_to_psa_error( ret ) );
3578}
3579
3580static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3581 psa_algorithm_t alg,
3582 const uint8_t *hash,
3583 size_t hash_length,
3584 const uint8_t *signature,
3585 size_t signature_length )
3586{
3587 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003589 mbedtls_md_type_t md_alg;
John Durkop0e005192020-10-31 22:06:54 -07003590#if !defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
3591 (void)hash;
3592 (void)signature;
3593#endif /* !MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003594
3595 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3596 if( status != PSA_SUCCESS )
3597 return( status );
3598
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003599 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3600 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003601
John Durkop0e005192020-10-31 22:06:54 -07003602#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003603 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3604 {
3605 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3606 MBEDTLS_MD_NONE );
3607 ret = mbedtls_rsa_pkcs1_verify( rsa,
3608 mbedtls_ctr_drbg_random,
3609 &global_data.ctr_drbg,
3610 MBEDTLS_RSA_PUBLIC,
3611 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003612 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003613 hash,
3614 signature );
3615 }
3616 else
John Durkop0e005192020-10-31 22:06:54 -07003617#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3618#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003619 if( PSA_ALG_IS_RSA_PSS( alg ) )
3620 {
3621 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3622 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3623 mbedtls_ctr_drbg_random,
3624 &global_data.ctr_drbg,
3625 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003626 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003627 (unsigned int) hash_length,
3628 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003629 signature );
3630 }
3631 else
John Durkop0e005192020-10-31 22:06:54 -07003632#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003633 {
3634 return( PSA_ERROR_INVALID_ARGUMENT );
3635 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003636
3637 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3638 * the rest of the signature is invalid". This has little use in
3639 * practice and PSA doesn't report this distinction. */
3640 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3641 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003642 return( mbedtls_to_psa_error( ret ) );
3643}
John Durkop6ba40d12020-11-10 08:50:04 -08003644#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3645 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003646
John Durkop6ba40d12020-11-10 08:50:04 -08003647#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3648 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003649/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3650 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3651 * (even though these functions don't modify it). */
3652static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3653 psa_algorithm_t alg,
3654 const uint8_t *hash,
3655 size_t hash_length,
3656 uint8_t *signature,
3657 size_t signature_size,
3658 size_t *signature_length )
3659{
Janos Follath24eed8d2019-11-22 13:21:35 +00003660 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003661 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003662 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003663 mbedtls_mpi_init( &r );
3664 mbedtls_mpi_init( &s );
3665
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003666 if( signature_size < 2 * curve_bytes )
3667 {
3668 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3669 goto cleanup;
3670 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003671
John Durkop0ea39e02020-10-13 19:58:20 -07003672#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003673 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3674 {
3675 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3676 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3677 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003678 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3679 &ecp->d, hash,
3680 hash_length, md_alg,
3681 mbedtls_ctr_drbg_random,
3682 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003683 }
3684 else
John Durkop0ea39e02020-10-13 19:58:20 -07003685#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003686 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003687 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003688 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3689 hash, hash_length,
3690 mbedtls_ctr_drbg_random,
3691 &global_data.ctr_drbg ) );
3692 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003693
3694 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3695 signature,
3696 curve_bytes ) );
3697 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3698 signature + curve_bytes,
3699 curve_bytes ) );
3700
3701cleanup:
3702 mbedtls_mpi_free( &r );
3703 mbedtls_mpi_free( &s );
3704 if( ret == 0 )
3705 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003706 return( mbedtls_to_psa_error( ret ) );
3707}
3708
3709static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3710 const uint8_t *hash,
3711 size_t hash_length,
3712 const uint8_t *signature,
3713 size_t signature_length )
3714{
Janos Follath24eed8d2019-11-22 13:21:35 +00003715 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003716 mbedtls_mpi r, s;
3717 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3718 mbedtls_mpi_init( &r );
3719 mbedtls_mpi_init( &s );
3720
3721 if( signature_length != 2 * curve_bytes )
3722 return( PSA_ERROR_INVALID_SIGNATURE );
3723
3724 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3725 signature,
3726 curve_bytes ) );
3727 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3728 signature + curve_bytes,
3729 curve_bytes ) );
3730
Steven Cooremanacda8342020-07-24 23:09:52 +02003731 /* Check whether the public part is loaded. If not, load it. */
3732 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3733 {
3734 MBEDTLS_MPI_CHK(
3735 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3736 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3737 }
3738
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003739 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3740 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003741
3742cleanup:
3743 mbedtls_mpi_free( &r );
3744 mbedtls_mpi_free( &s );
3745 return( mbedtls_to_psa_error( ret ) );
3746}
John Durkop6ba40d12020-11-10 08:50:04 -08003747#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3748 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003749
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003750psa_status_t psa_sign_hash( psa_key_handle_t handle,
3751 psa_algorithm_t alg,
3752 const uint8_t *hash,
3753 size_t hash_length,
3754 uint8_t *signature,
3755 size_t signature_size,
3756 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003757{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003758 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003759 psa_status_t status;
3760
3761 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003762 /* Immediately reject a zero-length signature buffer. This guarantees
3763 * that signature must be a valid pointer. (On the other hand, the hash
3764 * buffer can in principle be empty since it doesn't actually have
3765 * to be a hash.) */
3766 if( signature_size == 0 )
3767 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003768
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003769 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003770 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003771 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003772 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003773 {
3774 status = PSA_ERROR_INVALID_ARGUMENT;
3775 goto exit;
3776 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003777
Steven Cooremancd84cb42020-07-16 20:28:36 +02003778 /* Try any of the available accelerators first */
3779 status = psa_driver_wrapper_sign_hash( slot,
3780 alg,
3781 hash,
3782 hash_length,
3783 signature,
3784 signature_size,
3785 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003786 if( status != PSA_ERROR_NOT_SUPPORTED ||
3787 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003788 goto exit;
3789
Steven Cooreman7a250572020-07-17 16:43:05 +02003790 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003791#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3792 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003793 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003794 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003795 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003796
Steven Cooreman4fed4552020-08-03 14:46:03 +02003797 status = psa_load_rsa_representation( slot->attr.type,
3798 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003799 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003800 &rsa );
3801 if( status != PSA_SUCCESS )
3802 goto exit;
3803
Steven Cooremana2371e52020-07-28 14:30:39 +02003804 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003805 alg,
3806 hash, hash_length,
3807 signature, signature_size,
3808 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003809
Steven Cooremana2371e52020-07-28 14:30:39 +02003810 mbedtls_rsa_free( rsa );
3811 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003812 }
3813 else
John Durkop6ba40d12020-11-10 08:50:04 -08003814#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3815 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003816 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003817 {
John Durkop9814fa22020-11-04 12:28:15 -08003818#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3819 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003820 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003821#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003822 PSA_ALG_IS_ECDSA( alg )
3823#else
3824 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3825#endif
3826 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003827 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003828 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003829 status = psa_load_ecp_representation( slot->attr.type,
3830 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003831 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003832 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003833 if( status != PSA_SUCCESS )
3834 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003835 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003836 alg,
3837 hash, hash_length,
3838 signature, signature_size,
3839 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003840 mbedtls_ecp_keypair_free( ecp );
3841 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003842 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003843 else
John Durkop9814fa22020-11-04 12:28:15 -08003844#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3845 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003846 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003847 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003848 }
itayzafrir5c753392018-05-08 11:18:38 +03003849 }
3850 else
itayzafrir5c753392018-05-08 11:18:38 +03003851 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003852 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003853 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003854
3855exit:
3856 /* Fill the unused part of the output buffer (the whole buffer on error,
3857 * the trailing part on success) with something that isn't a valid mac
3858 * (barring an attack on the mac and deliberately-crafted input),
3859 * in case the caller doesn't check the return status properly. */
3860 if( status == PSA_SUCCESS )
3861 memset( signature + *signature_length, '!',
3862 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003863 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003864 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003865 /* If signature_size is 0 then we have nothing to do. We must not call
3866 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003867 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003868}
3869
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003870psa_status_t psa_verify_hash( psa_key_handle_t handle,
3871 psa_algorithm_t alg,
3872 const uint8_t *hash,
3873 size_t hash_length,
3874 const uint8_t *signature,
3875 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003876{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003877 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003878 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003879
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003880 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003881 if( status != PSA_SUCCESS )
3882 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003883
Steven Cooreman55ae2172020-07-17 19:46:15 +02003884 /* Try any of the available accelerators first */
3885 status = psa_driver_wrapper_verify_hash( slot,
3886 alg,
3887 hash,
3888 hash_length,
3889 signature,
3890 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003891 if( status != PSA_ERROR_NOT_SUPPORTED ||
3892 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +02003893 return status;
3894
John Durkop6ba40d12020-11-10 08:50:04 -08003895#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3896 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003897 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003898 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003899 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003900
Steven Cooreman4fed4552020-08-03 14:46:03 +02003901 status = psa_load_rsa_representation( slot->attr.type,
3902 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003903 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003904 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003905 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003906 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02003907
Steven Cooremana2371e52020-07-28 14:30:39 +02003908 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003909 alg,
3910 hash, hash_length,
3911 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003912 mbedtls_rsa_free( rsa );
3913 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003914 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003915 }
3916 else
John Durkop6ba40d12020-11-10 08:50:04 -08003917#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3918 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003919 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003920 {
John Durkop9814fa22020-11-04 12:28:15 -08003921#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3922 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003923 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003924 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003925 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003926 status = psa_load_ecp_representation( slot->attr.type,
3927 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003928 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003929 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003930 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003931 return( status );
Steven Cooremana2371e52020-07-28 14:30:39 +02003932 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003933 hash, hash_length,
3934 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003935 mbedtls_ecp_keypair_free( ecp );
3936 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02003937 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02003938 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003939 else
John Durkop9814fa22020-11-04 12:28:15 -08003940#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3941 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003942 {
3943 return( PSA_ERROR_INVALID_ARGUMENT );
3944 }
itayzafrir5c753392018-05-08 11:18:38 +03003945 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003946 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003947 {
3948 return( PSA_ERROR_NOT_SUPPORTED );
3949 }
3950}
3951
John Durkop0e005192020-10-31 22:06:54 -07003952#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003953static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3954 mbedtls_rsa_context *rsa )
3955{
3956 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3957 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3958 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3959 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3960}
John Durkop0e005192020-10-31 22:06:54 -07003961#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003962
Gilles Peskinec5487a82018-12-03 18:08:14 +01003963psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003964 psa_algorithm_t alg,
3965 const uint8_t *input,
3966 size_t input_length,
3967 const uint8_t *salt,
3968 size_t salt_length,
3969 uint8_t *output,
3970 size_t output_size,
3971 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003972{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003973 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003974 psa_status_t status;
3975
Darryl Green5cc689a2018-07-24 15:34:10 +01003976 (void) input;
3977 (void) input_length;
3978 (void) salt;
3979 (void) output;
3980 (void) output_size;
3981
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003982 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003983
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003984 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3985 return( PSA_ERROR_INVALID_ARGUMENT );
3986
Gilles Peskine28f8f302019-07-24 13:30:31 +02003987 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003988 if( status != PSA_SUCCESS )
3989 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003990 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3991 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003992 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003993
John Durkop6ba40d12020-11-10 08:50:04 -08003994#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3995 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003996 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003997 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003998 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003999 status = psa_load_rsa_representation( slot->attr.type,
4000 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004001 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004002 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004003 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004004 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004005
4006 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004007 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004008 status = PSA_ERROR_BUFFER_TOO_SMALL;
4009 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004010 }
John Durkop0e005192020-10-31 22:06:54 -07004011#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004012 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004013 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004014 status = mbedtls_to_psa_error(
4015 mbedtls_rsa_pkcs1_encrypt( rsa,
4016 mbedtls_ctr_drbg_random,
4017 &global_data.ctr_drbg,
4018 MBEDTLS_RSA_PUBLIC,
4019 input_length,
4020 input,
4021 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004022 }
4023 else
John Durkop0e005192020-10-31 22:06:54 -07004024#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4025#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004026 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004027 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004028 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004029 status = mbedtls_to_psa_error(
4030 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
4031 mbedtls_ctr_drbg_random,
4032 &global_data.ctr_drbg,
4033 MBEDTLS_RSA_PUBLIC,
4034 salt, salt_length,
4035 input_length,
4036 input,
4037 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004038 }
4039 else
John Durkop0e005192020-10-31 22:06:54 -07004040#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004041 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004042 status = PSA_ERROR_INVALID_ARGUMENT;
4043 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004044 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004045rsa_exit:
4046 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004047 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004048
Steven Cooremana2371e52020-07-28 14:30:39 +02004049 mbedtls_rsa_free( rsa );
4050 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004051 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004052 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004053 else
John Durkop9814fa22020-11-04 12:28:15 -08004054#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004055 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004056 {
4057 return( PSA_ERROR_NOT_SUPPORTED );
4058 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004059}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004060
Gilles Peskinec5487a82018-12-03 18:08:14 +01004061psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004062 psa_algorithm_t alg,
4063 const uint8_t *input,
4064 size_t input_length,
4065 const uint8_t *salt,
4066 size_t salt_length,
4067 uint8_t *output,
4068 size_t output_size,
4069 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004070{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004071 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004072 psa_status_t status;
4073
Darryl Green5cc689a2018-07-24 15:34:10 +01004074 (void) input;
4075 (void) input_length;
4076 (void) salt;
4077 (void) output;
4078 (void) output_size;
4079
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004080 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004081
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004082 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4083 return( PSA_ERROR_INVALID_ARGUMENT );
4084
Gilles Peskine28f8f302019-07-24 13:30:31 +02004085 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004086 if( status != PSA_SUCCESS )
4087 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004088 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004089 return( PSA_ERROR_INVALID_ARGUMENT );
4090
John Durkop6ba40d12020-11-10 08:50:04 -08004091#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4092 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004093 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004094 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004095 mbedtls_rsa_context *rsa = NULL;
4096 status = psa_load_rsa_representation( slot->attr.type,
4097 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004098 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004099 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004100 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004101 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004102
Steven Cooremana2371e52020-07-28 14:30:39 +02004103 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004104 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004105 status = PSA_ERROR_INVALID_ARGUMENT;
4106 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004107 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004108
John Durkop0e005192020-10-31 22:06:54 -07004109#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004110 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004111 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004112 status = mbedtls_to_psa_error(
4113 mbedtls_rsa_pkcs1_decrypt( rsa,
4114 mbedtls_ctr_drbg_random,
4115 &global_data.ctr_drbg,
4116 MBEDTLS_RSA_PRIVATE,
4117 output_length,
4118 input,
4119 output,
4120 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004121 }
4122 else
John Durkop0e005192020-10-31 22:06:54 -07004123#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4124#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004125 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004126 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004127 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004128 status = mbedtls_to_psa_error(
4129 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4130 mbedtls_ctr_drbg_random,
4131 &global_data.ctr_drbg,
4132 MBEDTLS_RSA_PRIVATE,
4133 salt, salt_length,
4134 output_length,
4135 input,
4136 output,
4137 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004138 }
4139 else
John Durkop0e005192020-10-31 22:06:54 -07004140#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004141 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004142 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004143 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004144
Steven Cooreman4fed4552020-08-03 14:46:03 +02004145rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004146 mbedtls_rsa_free( rsa );
4147 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004148 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004149 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004150 else
John Durkop6ba40d12020-11-10 08:50:04 -08004151#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4152 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004153 {
4154 return( PSA_ERROR_NOT_SUPPORTED );
4155 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004156}
Gilles Peskine20035e32018-02-03 22:44:14 +01004157
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004158
4159
mohammad1603503973b2018-03-12 15:59:30 +02004160/****************************************************************/
4161/* Symmetric cryptography */
4162/****************************************************************/
4163
Gilles Peskinee553c652018-06-04 16:22:46 +02004164static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004165 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004166 psa_algorithm_t alg,
4167 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004168{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004169 int ret = 0;
4170 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004171 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004172 size_t key_bits;
4173 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004174 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4175 PSA_KEY_USAGE_ENCRYPT :
4176 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004177
Steven Cooremand3feccd2020-09-01 15:56:14 +02004178 /* A context must be freshly initialized before it can be set up. */
4179 if( operation->alg != 0 )
4180 return( PSA_ERROR_BAD_STATE );
4181
Steven Cooremana07b9972020-09-10 14:54:14 +02004182 /* The requested algorithm must be one that can be processed by cipher. */
4183 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004184 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004185
Steven Cooremanef8575e2020-09-11 11:44:50 +02004186 /* Fetch key material from key storage. */
4187 status = psa_get_key_from_slot( handle, &slot, usage, alg );
4188 if( status != PSA_SUCCESS )
4189 goto exit;
4190
4191 /* Initialize the operation struct members, except for alg. The alg member
4192 * is used to indicate to psa_cipher_abort that there are resources to free,
4193 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004194 operation->key_set = 0;
4195 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004196 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004197 operation->iv_size = 0;
4198 operation->block_size = 0;
4199 if( alg == PSA_ALG_ECB_NO_PADDING )
4200 operation->iv_required = 0;
4201 else
4202 operation->iv_required = 1;
4203
Steven Cooremana07b9972020-09-10 14:54:14 +02004204 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004205 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004206 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004207 slot,
4208 alg );
4209 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004210 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004211 slot,
4212 alg );
4213
Steven Cooremanef8575e2020-09-11 11:44:50 +02004214 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004215 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004216 /* Once the driver context is initialised, it needs to be freed using
4217 * psa_cipher_abort. Indicate this through setting alg. */
4218 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004219 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004220
Steven Cooremand3feccd2020-09-01 15:56:14 +02004221 if( status != PSA_ERROR_NOT_SUPPORTED ||
4222 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4223 goto exit;
4224
Steven Cooremana07b9972020-09-10 14:54:14 +02004225 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004226 * available for the given algorithm & key. */
4227 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004228
Steven Cooremana07b9972020-09-10 14:54:14 +02004229 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004230 * psa_cipher_abort. Indicate there is something to be freed through setting
4231 * alg, and indicate the operation is being done using mbedtls crypto through
4232 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004233 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004234 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004235
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004236 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004237 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004238 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004239 {
4240 status = PSA_ERROR_NOT_SUPPORTED;
4241 goto exit;
4242 }
mohammad1603503973b2018-03-12 15:59:30 +02004243
mohammad1603503973b2018-03-12 15:59:30 +02004244 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004245 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004246 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004247
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004248#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004249 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004250 {
4251 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004252 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004253 memcpy( keys, slot->data.key.data, 16 );
4254 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004255 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4256 keys,
4257 192, cipher_operation );
4258 }
4259 else
4260#endif
4261 {
4262 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004263 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004264 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004265 }
Moran Peker41deec42018-04-04 15:43:05 +03004266 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004267 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004268
mohammad16038481e742018-03-18 13:57:31 +02004269#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004270 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004271 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004272 case PSA_ALG_CBC_NO_PADDING:
4273 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4274 MBEDTLS_PADDING_NONE );
4275 break;
4276 case PSA_ALG_CBC_PKCS7:
4277 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4278 MBEDTLS_PADDING_PKCS7 );
4279 break;
4280 default:
4281 /* The algorithm doesn't involve padding. */
4282 ret = 0;
4283 break;
4284 }
4285 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004286 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004287#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4288
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004289 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004290 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004291 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4292 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004293 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004294 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004295 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004296#if defined(MBEDTLS_CHACHA20_C)
4297 else
4298 if( alg == PSA_ALG_CHACHA20 )
4299 operation->iv_size = 12;
4300#endif
mohammad1603503973b2018-03-12 15:59:30 +02004301
Steven Cooreman7df02922020-09-09 15:28:49 +02004302 status = PSA_SUCCESS;
4303
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004304exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004305 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004306 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004307 if( status == PSA_SUCCESS )
4308 {
4309 /* Update operation flags for both driver and software implementations */
4310 operation->key_set = 1;
4311 }
4312 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004313 psa_cipher_abort( operation );
4314 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004315}
4316
Gilles Peskinefe119512018-07-08 21:39:34 +02004317psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004318 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004319 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004320{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004321 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004322}
4323
Gilles Peskinefe119512018-07-08 21:39:34 +02004324psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004325 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004326 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004327{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004328 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004329}
4330
Gilles Peskinefe119512018-07-08 21:39:34 +02004331psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004332 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004333 size_t iv_size,
4334 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004335{
itayzafrir534bd7c2018-08-02 13:56:32 +03004336 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004338 if( operation->iv_set || ! operation->iv_required )
4339 {
4340 return( PSA_ERROR_BAD_STATE );
4341 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004342
Steven Cooremanef8575e2020-09-11 11:44:50 +02004343 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004344 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004345 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004346 iv,
4347 iv_size,
4348 iv_length );
4349 goto exit;
4350 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004351
Moran Peker41deec42018-04-04 15:43:05 +03004352 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004353 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004354 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004355 goto exit;
4356 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004357 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4358 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004359 if( ret != 0 )
4360 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004361 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004362 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004363 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004364
mohammad16038481e742018-03-18 13:57:31 +02004365 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004366 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004367
Moran Peker395db872018-05-31 14:07:14 +03004368exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004369 if( status == PSA_SUCCESS )
4370 operation->iv_set = 1;
4371 else
Moran Peker395db872018-05-31 14:07:14 +03004372 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004373 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004374}
4375
Gilles Peskinefe119512018-07-08 21:39:34 +02004376psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004377 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004378 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004379{
itayzafrir534bd7c2018-08-02 13:56:32 +03004380 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004382 if( operation->iv_set || ! operation->iv_required )
4383 {
4384 return( PSA_ERROR_BAD_STATE );
4385 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004386
Steven Cooremanef8575e2020-09-11 11:44:50 +02004387 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004388 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004389 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004390 iv,
4391 iv_length );
4392 goto exit;
4393 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004394
Moran Pekera28258c2018-05-29 16:25:04 +03004395 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004396 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004397 status = PSA_ERROR_INVALID_ARGUMENT;
4398 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004399 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004400 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4401 status = mbedtls_to_psa_error( ret );
4402exit:
4403 if( status == PSA_SUCCESS )
4404 operation->iv_set = 1;
4405 else
mohammad1603503973b2018-03-12 15:59:30 +02004406 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004407 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004408}
4409
Steven Cooreman177deba2020-09-07 17:14:14 +02004410/* Process input for which the algorithm is set to ECB mode. This requires
4411 * manual processing, since the PSA API is defined as being able to process
4412 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4413 * underlying mbedtls_cipher_update only takes full blocks. */
4414static psa_status_t psa_cipher_update_ecb_internal(
4415 mbedtls_cipher_context_t *ctx,
4416 const uint8_t *input,
4417 size_t input_length,
4418 uint8_t *output,
4419 size_t output_size,
4420 size_t *output_length )
4421{
4422 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4423 size_t block_size = ctx->cipher_info->block_size;
4424 size_t internal_output_length = 0;
4425 *output_length = 0;
4426
4427 if( input_length == 0 )
4428 {
4429 status = PSA_SUCCESS;
4430 goto exit;
4431 }
4432
4433 if( ctx->unprocessed_len > 0 )
4434 {
4435 /* Fill up to block size, and run the block if there's a full one. */
4436 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4437
4438 if( input_length < bytes_to_copy )
4439 bytes_to_copy = input_length;
4440
4441 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4442 input, bytes_to_copy );
4443 input_length -= bytes_to_copy;
4444 input += bytes_to_copy;
4445 ctx->unprocessed_len += bytes_to_copy;
4446
4447 if( ctx->unprocessed_len == block_size )
4448 {
4449 status = mbedtls_to_psa_error(
4450 mbedtls_cipher_update( ctx,
4451 ctx->unprocessed_data,
4452 block_size,
4453 output, &internal_output_length ) );
4454
4455 if( status != PSA_SUCCESS )
4456 goto exit;
4457
4458 output += internal_output_length;
4459 output_size -= internal_output_length;
4460 *output_length += internal_output_length;
4461 ctx->unprocessed_len = 0;
4462 }
4463 }
4464
4465 while( input_length >= block_size )
4466 {
4467 /* Run all full blocks we have, one by one */
4468 status = mbedtls_to_psa_error(
4469 mbedtls_cipher_update( ctx, input,
4470 block_size,
4471 output, &internal_output_length ) );
4472
4473 if( status != PSA_SUCCESS )
4474 goto exit;
4475
4476 input_length -= block_size;
4477 input += block_size;
4478
4479 output += internal_output_length;
4480 output_size -= internal_output_length;
4481 *output_length += internal_output_length;
4482 }
4483
4484 if( input_length > 0 )
4485 {
4486 /* Save unprocessed bytes for later processing */
4487 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4488 input, input_length );
4489 ctx->unprocessed_len += input_length;
4490 }
4491
4492 status = PSA_SUCCESS;
4493
4494exit:
4495 return( status );
4496}
4497
Gilles Peskinee553c652018-06-04 16:22:46 +02004498psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4499 const uint8_t *input,
4500 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004501 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004502 size_t output_size,
4503 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004504{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004505 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004506 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004507 if( operation->alg == 0 )
4508 {
4509 return( PSA_ERROR_BAD_STATE );
4510 }
4511 if( operation->iv_required && ! operation->iv_set )
4512 {
4513 return( PSA_ERROR_BAD_STATE );
4514 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004515
Steven Cooremanef8575e2020-09-11 11:44:50 +02004516 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004517 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004518 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004519 input,
4520 input_length,
4521 output,
4522 output_size,
4523 output_length );
4524 goto exit;
4525 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004526
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004527 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004528 {
4529 /* Take the unprocessed partial block left over from previous
4530 * update calls, if any, plus the input to this call. Remove
4531 * the last partial block, if any. You get the data that will be
4532 * output in this call. */
4533 expected_output_size =
4534 ( operation->ctx.cipher.unprocessed_len + input_length )
4535 / operation->block_size * operation->block_size;
4536 }
4537 else
4538 {
4539 expected_output_size = input_length;
4540 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004541
Gilles Peskine89d789c2018-06-04 17:17:16 +02004542 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004543 {
4544 status = PSA_ERROR_BUFFER_TOO_SMALL;
4545 goto exit;
4546 }
mohammad160382759612018-03-12 18:16:40 +02004547
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004548 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4549 {
4550 /* mbedtls_cipher_update has an API inconsistency: it will only
4551 * process a single block at a time in ECB mode. Abstract away that
4552 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004553 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4554 input,
4555 input_length,
4556 output,
4557 output_size,
4558 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004559 }
4560 else
4561 {
4562 status = mbedtls_to_psa_error(
4563 mbedtls_cipher_update( &operation->ctx.cipher, input,
4564 input_length, output, output_length ) );
4565 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004566exit:
4567 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004568 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004569 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004570}
4571
Gilles Peskinee553c652018-06-04 16:22:46 +02004572psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4573 uint8_t *output,
4574 size_t output_size,
4575 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004576{
David Saadab4ecc272019-02-14 13:48:10 +02004577 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004578 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004579 if( operation->alg == 0 )
4580 {
4581 return( PSA_ERROR_BAD_STATE );
4582 }
4583 if( operation->iv_required && ! operation->iv_set )
4584 {
4585 return( PSA_ERROR_BAD_STATE );
4586 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004587
Steven Cooremanef8575e2020-09-11 11:44:50 +02004588 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004589 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004590 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004591 output,
4592 output_size,
4593 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004594 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004595 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004596
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004597 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004598 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004599 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004600 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004601 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004602 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004603 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004604 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004605 }
4606
Steven Cooremanef8575e2020-09-11 11:44:50 +02004607 status = mbedtls_to_psa_error(
4608 mbedtls_cipher_finish( &operation->ctx.cipher,
4609 temp_output_buffer,
4610 output_length ) );
4611 if( status != PSA_SUCCESS )
4612 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004613
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004614 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004615 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004616 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004617 memcpy( output, temp_output_buffer, *output_length );
4618 else
Janos Follath315b51c2018-07-09 16:04:51 +01004619 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004620
Steven Cooremanef8575e2020-09-11 11:44:50 +02004621exit:
4622 if( operation->mbedtls_in_use == 1 )
4623 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004624
Steven Cooremanef8575e2020-09-11 11:44:50 +02004625 if( status == PSA_SUCCESS )
4626 return( psa_cipher_abort( operation ) );
4627 else
4628 {
4629 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004630 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004631
Steven Cooremanef8575e2020-09-11 11:44:50 +02004632 return( status );
4633 }
mohammad1603503973b2018-03-12 15:59:30 +02004634}
4635
Gilles Peskinee553c652018-06-04 16:22:46 +02004636psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4637{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004638 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004639 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004640 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004641 * in use. It's ok to call abort on such an object, and there's
4642 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004643 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004644 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004645
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004646 /* Sanity check (shouldn't happen: operation->alg should
4647 * always have been initialized to a valid value). */
4648 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4649 return( PSA_ERROR_BAD_STATE );
4650
Steven Cooremanef8575e2020-09-11 11:44:50 +02004651 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004652 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004653 else
4654 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004655
Moran Peker41deec42018-04-04 15:43:05 +03004656 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004657 operation->key_set = 0;
4658 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004659 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004660 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004661 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004662 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004663
Moran Peker395db872018-05-31 14:07:14 +03004664 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004665}
4666
Gilles Peskinea0655c32018-04-30 17:06:50 +02004667
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004668
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004669
mohammad16035955c982018-04-26 00:53:03 +03004670/****************************************************************/
4671/* AEAD */
4672/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004673
Gilles Peskineedf9a652018-08-17 18:11:56 +02004674typedef struct
4675{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004676 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004677 const mbedtls_cipher_info_t *cipher_info;
4678 union
4679 {
4680#if defined(MBEDTLS_CCM_C)
4681 mbedtls_ccm_context ccm;
4682#endif /* MBEDTLS_CCM_C */
4683#if defined(MBEDTLS_GCM_C)
4684 mbedtls_gcm_context gcm;
4685#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004686#if defined(MBEDTLS_CHACHAPOLY_C)
4687 mbedtls_chachapoly_context chachapoly;
4688#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004689 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004690 psa_algorithm_t core_alg;
4691 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004692 uint8_t tag_length;
4693} aead_operation_t;
4694
Gilles Peskine30a9e412019-01-14 18:36:12 +01004695static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004696{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004697 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004698 {
4699#if defined(MBEDTLS_CCM_C)
4700 case PSA_ALG_CCM:
4701 mbedtls_ccm_free( &operation->ctx.ccm );
4702 break;
4703#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004704#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004705 case PSA_ALG_GCM:
4706 mbedtls_gcm_free( &operation->ctx.gcm );
4707 break;
4708#endif /* MBEDTLS_GCM_C */
4709 }
4710}
4711
4712static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004713 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004714 psa_key_usage_t usage,
4715 psa_algorithm_t alg )
4716{
4717 psa_status_t status;
4718 size_t key_bits;
4719 mbedtls_cipher_id_t cipher_id;
4720
Gilles Peskine28f8f302019-07-24 13:30:31 +02004721 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004722 if( status != PSA_SUCCESS )
4723 return( status );
4724
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004725 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004726
4727 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004728 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004729 &cipher_id );
4730 if( operation->cipher_info == NULL )
4731 return( PSA_ERROR_NOT_SUPPORTED );
4732
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004733 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004734 {
4735#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004736 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4737 operation->core_alg = PSA_ALG_CCM;
4738 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004739 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4740 * The call to mbedtls_ccm_encrypt_and_tag or
4741 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004742 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004743 return( PSA_ERROR_INVALID_ARGUMENT );
4744 mbedtls_ccm_init( &operation->ctx.ccm );
4745 status = mbedtls_to_psa_error(
4746 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004747 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004748 (unsigned int) key_bits ) );
4749 if( status != 0 )
4750 goto cleanup;
4751 break;
4752#endif /* MBEDTLS_CCM_C */
4753
4754#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004755 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4756 operation->core_alg = PSA_ALG_GCM;
4757 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004758 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4759 * The call to mbedtls_gcm_crypt_and_tag or
4760 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004761 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004762 return( PSA_ERROR_INVALID_ARGUMENT );
4763 mbedtls_gcm_init( &operation->ctx.gcm );
4764 status = mbedtls_to_psa_error(
4765 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004766 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004767 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004768 if( status != 0 )
4769 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004770 break;
4771#endif /* MBEDTLS_GCM_C */
4772
Gilles Peskine26869f22019-05-06 15:25:00 +02004773#if defined(MBEDTLS_CHACHAPOLY_C)
4774 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4775 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4776 operation->full_tag_length = 16;
4777 /* We only support the default tag length. */
4778 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4779 return( PSA_ERROR_NOT_SUPPORTED );
4780 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4781 status = mbedtls_to_psa_error(
4782 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004783 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004784 if( status != 0 )
4785 goto cleanup;
4786 break;
4787#endif /* MBEDTLS_CHACHAPOLY_C */
4788
Gilles Peskineedf9a652018-08-17 18:11:56 +02004789 default:
4790 return( PSA_ERROR_NOT_SUPPORTED );
4791 }
4792
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004793 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4794 {
4795 status = PSA_ERROR_INVALID_ARGUMENT;
4796 goto cleanup;
4797 }
4798 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004799
Gilles Peskineedf9a652018-08-17 18:11:56 +02004800 return( PSA_SUCCESS );
4801
4802cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004803 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004804 return( status );
4805}
4806
Gilles Peskinec5487a82018-12-03 18:08:14 +01004807psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004808 psa_algorithm_t alg,
4809 const uint8_t *nonce,
4810 size_t nonce_length,
4811 const uint8_t *additional_data,
4812 size_t additional_data_length,
4813 const uint8_t *plaintext,
4814 size_t plaintext_length,
4815 uint8_t *ciphertext,
4816 size_t ciphertext_size,
4817 size_t *ciphertext_length )
4818{
mohammad16035955c982018-04-26 00:53:03 +03004819 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004820 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004821 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004822
mohammad1603f08a5502018-06-03 15:05:47 +03004823 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004824
Gilles Peskinec5487a82018-12-03 18:08:14 +01004825 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004826 if( status != PSA_SUCCESS )
4827 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004828
Gilles Peskineedf9a652018-08-17 18:11:56 +02004829 /* For all currently supported modes, the tag is at the end of the
4830 * ciphertext. */
4831 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4832 {
4833 status = PSA_ERROR_BUFFER_TOO_SMALL;
4834 goto exit;
4835 }
4836 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004837
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004838#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004839 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004840 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004841 status = mbedtls_to_psa_error(
4842 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4843 MBEDTLS_GCM_ENCRYPT,
4844 plaintext_length,
4845 nonce, nonce_length,
4846 additional_data, additional_data_length,
4847 plaintext, ciphertext,
4848 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004849 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004850 else
4851#endif /* MBEDTLS_GCM_C */
4852#if defined(MBEDTLS_CCM_C)
4853 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004854 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004855 status = mbedtls_to_psa_error(
4856 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4857 plaintext_length,
4858 nonce, nonce_length,
4859 additional_data,
4860 additional_data_length,
4861 plaintext, ciphertext,
4862 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004863 }
mohammad16035c8845f2018-05-09 05:40:09 -07004864 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004865#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004866#if defined(MBEDTLS_CHACHAPOLY_C)
4867 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4868 {
4869 if( nonce_length != 12 || operation.tag_length != 16 )
4870 {
4871 status = PSA_ERROR_NOT_SUPPORTED;
4872 goto exit;
4873 }
4874 status = mbedtls_to_psa_error(
4875 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4876 plaintext_length,
4877 nonce,
4878 additional_data,
4879 additional_data_length,
4880 plaintext,
4881 ciphertext,
4882 tag ) );
4883 }
4884 else
4885#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004886 {
mohammad1603554faad2018-06-03 15:07:38 +03004887 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004888 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004889
Gilles Peskineedf9a652018-08-17 18:11:56 +02004890 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4891 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004892
Gilles Peskineedf9a652018-08-17 18:11:56 +02004893exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004894 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004895 if( status == PSA_SUCCESS )
4896 *ciphertext_length = plaintext_length + operation.tag_length;
4897 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004898}
4899
Gilles Peskineee652a32018-06-01 19:23:52 +02004900/* Locate the tag in a ciphertext buffer containing the encrypted data
4901 * followed by the tag. Return the length of the part preceding the tag in
4902 * *plaintext_length. This is the size of the plaintext in modes where
4903 * the encrypted data has the same size as the plaintext, such as
4904 * CCM and GCM. */
4905static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4906 const uint8_t *ciphertext,
4907 size_t ciphertext_length,
4908 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004909 const uint8_t **p_tag )
4910{
4911 size_t payload_length;
4912 if( tag_length > ciphertext_length )
4913 return( PSA_ERROR_INVALID_ARGUMENT );
4914 payload_length = ciphertext_length - tag_length;
4915 if( payload_length > plaintext_size )
4916 return( PSA_ERROR_BUFFER_TOO_SMALL );
4917 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004918 return( PSA_SUCCESS );
4919}
4920
Gilles Peskinec5487a82018-12-03 18:08:14 +01004921psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004922 psa_algorithm_t alg,
4923 const uint8_t *nonce,
4924 size_t nonce_length,
4925 const uint8_t *additional_data,
4926 size_t additional_data_length,
4927 const uint8_t *ciphertext,
4928 size_t ciphertext_length,
4929 uint8_t *plaintext,
4930 size_t plaintext_size,
4931 size_t *plaintext_length )
4932{
mohammad16035955c982018-04-26 00:53:03 +03004933 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004934 aead_operation_t operation;
4935 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004936
Gilles Peskineee652a32018-06-01 19:23:52 +02004937 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004938
Gilles Peskinec5487a82018-12-03 18:08:14 +01004939 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004940 if( status != PSA_SUCCESS )
4941 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004942
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004943 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4944 ciphertext, ciphertext_length,
4945 plaintext_size, &tag );
4946 if( status != PSA_SUCCESS )
4947 goto exit;
4948
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004949#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004950 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004951 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004952 status = mbedtls_to_psa_error(
4953 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4954 ciphertext_length - operation.tag_length,
4955 nonce, nonce_length,
4956 additional_data,
4957 additional_data_length,
4958 tag, operation.tag_length,
4959 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004960 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004961 else
4962#endif /* MBEDTLS_GCM_C */
4963#if defined(MBEDTLS_CCM_C)
4964 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004965 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004966 status = mbedtls_to_psa_error(
4967 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4968 ciphertext_length - operation.tag_length,
4969 nonce, nonce_length,
4970 additional_data,
4971 additional_data_length,
4972 ciphertext, plaintext,
4973 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004974 }
mohammad160339574652018-06-01 04:39:53 -07004975 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004976#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004977#if defined(MBEDTLS_CHACHAPOLY_C)
4978 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4979 {
4980 if( nonce_length != 12 || operation.tag_length != 16 )
4981 {
4982 status = PSA_ERROR_NOT_SUPPORTED;
4983 goto exit;
4984 }
4985 status = mbedtls_to_psa_error(
4986 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4987 ciphertext_length - operation.tag_length,
4988 nonce,
4989 additional_data,
4990 additional_data_length,
4991 tag,
4992 ciphertext,
4993 plaintext ) );
4994 }
4995 else
4996#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004997 {
mohammad1603554faad2018-06-03 15:07:38 +03004998 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004999 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005000
Gilles Peskineedf9a652018-08-17 18:11:56 +02005001 if( status != PSA_SUCCESS && plaintext_size != 0 )
5002 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005003
Gilles Peskineedf9a652018-08-17 18:11:56 +02005004exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005005 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005006 if( status == PSA_SUCCESS )
5007 *plaintext_length = ciphertext_length - operation.tag_length;
5008 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005009}
5010
Gilles Peskinea0655c32018-04-30 17:06:50 +02005011
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005012
Gilles Peskine20035e32018-02-03 22:44:14 +01005013/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005014/* Generators */
5015/****************************************************************/
5016
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005017#define HKDF_STATE_INIT 0 /* no input yet */
5018#define HKDF_STATE_STARTED 1 /* got salt */
5019#define HKDF_STATE_KEYED 2 /* got key */
5020#define HKDF_STATE_OUTPUT 3 /* output started */
5021
Gilles Peskinecbe66502019-05-16 16:59:18 +02005022static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005023 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005024{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005025 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5026 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005027 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005028 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005029}
5030
5031
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005033{
5034 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005035 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005036 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005037 {
5038 /* The object has (apparently) been initialized but it is not
5039 * in use. It's ok to call abort on such an object, and there's
5040 * nothing to do. */
5041 }
5042 else
John Durkopd0321952020-10-29 21:37:36 -07005043#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005044 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005045 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005046 mbedtls_free( operation->ctx.hkdf.info );
5047 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005048 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005049 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005050 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005051 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005052 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005053 if( operation->ctx.tls12_prf.seed != NULL )
5054 {
5055 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5056 operation->ctx.tls12_prf.seed_length );
5057 mbedtls_free( operation->ctx.tls12_prf.seed );
5058 }
5059
5060 if( operation->ctx.tls12_prf.label != NULL )
5061 {
5062 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5063 operation->ctx.tls12_prf.label_length );
5064 mbedtls_free( operation->ctx.tls12_prf.label );
5065 }
5066
5067 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5068
5069 /* We leave the fields Ai and output_block to be erased safely by the
5070 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005071 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005072 else
John Durkopd0321952020-10-29 21:37:36 -07005073#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005074 {
5075 status = PSA_ERROR_BAD_STATE;
5076 }
Janos Follath083036a2019-06-11 10:22:26 +01005077 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005078 return( status );
5079}
5080
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005081psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005082 size_t *capacity)
5083{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005084 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005085 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005086 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005087 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005088 }
5089
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005090 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005091 return( PSA_SUCCESS );
5092}
5093
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005094psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005095 size_t capacity )
5096{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005097 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005098 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005099 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005100 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005101 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005102 return( PSA_SUCCESS );
5103}
5104
John Durkopd0321952020-10-29 21:37:36 -07005105#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005106/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005107 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005108static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005109 psa_algorithm_t hash_alg,
5110 uint8_t *output,
5111 size_t output_length )
5112{
5113 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5114 psa_status_t status;
5115
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005116 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5117 return( PSA_ERROR_BAD_STATE );
5118 hkdf->state = HKDF_STATE_OUTPUT;
5119
Gilles Peskinebef7f142018-07-12 17:22:21 +02005120 while( output_length != 0 )
5121 {
5122 /* Copy what remains of the current block */
5123 uint8_t n = hash_length - hkdf->offset_in_block;
5124 if( n > output_length )
5125 n = (uint8_t) output_length;
5126 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5127 output += n;
5128 output_length -= n;
5129 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005130 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005131 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005132 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005133 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005134 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005135 * object was corrupted or if this function is called directly
5136 * inside the library. */
5137 if( hkdf->block_number == 0xff )
5138 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005139
5140 /* We need a new block */
5141 ++hkdf->block_number;
5142 hkdf->offset_in_block = 0;
5143 status = psa_hmac_setup_internal( &hkdf->hmac,
5144 hkdf->prk, hash_length,
5145 hash_alg );
5146 if( status != PSA_SUCCESS )
5147 return( status );
5148 if( hkdf->block_number != 1 )
5149 {
5150 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5151 hkdf->output_block,
5152 hash_length );
5153 if( status != PSA_SUCCESS )
5154 return( status );
5155 }
5156 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5157 hkdf->info,
5158 hkdf->info_length );
5159 if( status != PSA_SUCCESS )
5160 return( status );
5161 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5162 &hkdf->block_number, 1 );
5163 if( status != PSA_SUCCESS )
5164 return( status );
5165 status = psa_hmac_finish_internal( &hkdf->hmac,
5166 hkdf->output_block,
5167 sizeof( hkdf->output_block ) );
5168 if( status != PSA_SUCCESS )
5169 return( status );
5170 }
5171
5172 return( PSA_SUCCESS );
5173}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005174
Janos Follath7742fee2019-06-17 12:58:10 +01005175static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5176 psa_tls12_prf_key_derivation_t *tls12_prf,
5177 psa_algorithm_t alg )
5178{
5179 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5180 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005181 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5182 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005183
Janos Follath7742fee2019-06-17 12:58:10 +01005184 /* We can't be wanting more output after block 0xff, otherwise
5185 * the capacity check in psa_key_derivation_output_bytes() would have
5186 * prevented this call. It could happen only if the operation
5187 * object was corrupted or if this function is called directly
5188 * inside the library. */
5189 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005190 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005191
5192 /* We need a new block */
5193 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005194 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005195
5196 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5197 *
5198 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5199 *
5200 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5201 * HMAC_hash(secret, A(2) + seed) +
5202 * HMAC_hash(secret, A(3) + seed) + ...
5203 *
5204 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005205 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005206 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005207 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005208 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005209 * is currently extracted as `output_block` and where i is
5210 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005211 */
5212
Janos Follathea29bfb2019-06-19 12:21:20 +01005213 /* Save the hash context before using it, to preserve the hash state with
5214 * only the inner padding in it. We need this, because inner padding depends
5215 * on the key (secret in the RFC's terminology). */
5216 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5217 if( status != PSA_SUCCESS )
5218 goto cleanup;
5219
5220 /* Calculate A(i) where i = tls12_prf->block_number. */
5221 if( tls12_prf->block_number == 1 )
5222 {
5223 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5224 * the variable seed and in this instance means it in the context of the
5225 * P_hash function, where seed = label + seed.) */
5226 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5227 tls12_prf->label, tls12_prf->label_length );
5228 if( status != PSA_SUCCESS )
5229 goto cleanup;
5230 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5231 tls12_prf->seed, tls12_prf->seed_length );
5232 if( status != PSA_SUCCESS )
5233 goto cleanup;
5234 }
5235 else
5236 {
5237 /* A(i) = HMAC_hash(secret, A(i-1)) */
5238 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5239 tls12_prf->Ai, hash_length );
5240 if( status != PSA_SUCCESS )
5241 goto cleanup;
5242 }
5243
5244 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5245 tls12_prf->Ai, hash_length );
5246 if( status != PSA_SUCCESS )
5247 goto cleanup;
5248 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5249 if( status != PSA_SUCCESS )
5250 goto cleanup;
5251
5252 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5253 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5254 tls12_prf->Ai, hash_length );
5255 if( status != PSA_SUCCESS )
5256 goto cleanup;
5257 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5258 tls12_prf->label, tls12_prf->label_length );
5259 if( status != PSA_SUCCESS )
5260 goto cleanup;
5261 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5262 tls12_prf->seed, tls12_prf->seed_length );
5263 if( status != PSA_SUCCESS )
5264 goto cleanup;
5265 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5266 tls12_prf->output_block, hash_length );
5267 if( status != PSA_SUCCESS )
5268 goto cleanup;
5269 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5270 if( status != PSA_SUCCESS )
5271 goto cleanup;
5272
Janos Follath7742fee2019-06-17 12:58:10 +01005273
5274cleanup:
5275
Janos Follathea29bfb2019-06-19 12:21:20 +01005276 cleanup_status = psa_hash_abort( &backup );
5277 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5278 status = cleanup_status;
5279
5280 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005281}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005282
Janos Follath844eb0e2019-06-19 12:10:49 +01005283static psa_status_t psa_key_derivation_tls12_prf_read(
5284 psa_tls12_prf_key_derivation_t *tls12_prf,
5285 psa_algorithm_t alg,
5286 uint8_t *output,
5287 size_t output_length )
5288{
5289 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5290 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5291 psa_status_t status;
5292 uint8_t offset, length;
5293
5294 while( output_length != 0 )
5295 {
5296 /* Check if we have fully processed the current block. */
5297 if( tls12_prf->left_in_block == 0 )
5298 {
5299 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5300 alg );
5301 if( status != PSA_SUCCESS )
5302 return( status );
5303
5304 continue;
5305 }
5306
5307 if( tls12_prf->left_in_block > output_length )
5308 length = (uint8_t) output_length;
5309 else
5310 length = tls12_prf->left_in_block;
5311
5312 offset = hash_length - tls12_prf->left_in_block;
5313 memcpy( output, tls12_prf->output_block + offset, length );
5314 output += length;
5315 output_length -= length;
5316 tls12_prf->left_in_block -= length;
5317 }
5318
5319 return( PSA_SUCCESS );
5320}
John Durkopd0321952020-10-29 21:37:36 -07005321#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005322
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005323psa_status_t psa_key_derivation_output_bytes(
5324 psa_key_derivation_operation_t *operation,
5325 uint8_t *output,
5326 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005327{
5328 psa_status_t status;
John Durkopd0321952020-10-29 21:37:36 -07005329#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005330 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
John Durkopd0321952020-10-29 21:37:36 -07005331#endif
Gilles Peskineeab56e42018-07-12 17:12:33 +02005332
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005333 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005334 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005335 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005336 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005337 }
5338
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005339 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005340 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005341 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005342 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005343 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005344 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005345 goto exit;
5346 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005347 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005348 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005349 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005350 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005351 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5352 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005353 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005354 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005355 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005356 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005357 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005358
John Durkopd0321952020-10-29 21:37:36 -07005359#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005360 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005361 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005362 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005363 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005364 output, output_length );
5365 }
Janos Follathadbec812019-06-14 11:05:39 +01005366 else
Janos Follathadbec812019-06-14 11:05:39 +01005367 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005368 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005369 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005370 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005371 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005372 output_length );
5373 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005374 else
John Durkopd0321952020-10-29 21:37:36 -07005375#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005376 {
5377 return( PSA_ERROR_BAD_STATE );
5378 }
5379
5380exit:
5381 if( status != PSA_SUCCESS )
5382 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005383 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005384 * This allows us to differentiate between exhausted operations and
5385 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5386 * operations. */
5387 psa_algorithm_t alg = operation->alg;
5388 psa_key_derivation_abort( operation );
5389 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005390 memset( output, '!', output_length );
5391 }
5392 return( status );
5393}
5394
Gilles Peskine08542d82018-07-19 17:05:42 +02005395#if defined(MBEDTLS_DES_C)
5396static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5397{
5398 if( data_size >= 8 )
5399 mbedtls_des_key_set_parity( data );
5400 if( data_size >= 16 )
5401 mbedtls_des_key_set_parity( data + 8 );
5402 if( data_size >= 24 )
5403 mbedtls_des_key_set_parity( data + 16 );
5404}
5405#endif /* MBEDTLS_DES_C */
5406
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005407static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005408 psa_key_slot_t *slot,
5409 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005410 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005411{
5412 uint8_t *data = NULL;
5413 size_t bytes = PSA_BITS_TO_BYTES( bits );
5414 psa_status_t status;
5415
Gilles Peskine8e338702019-07-30 20:06:31 +02005416 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005417 return( PSA_ERROR_INVALID_ARGUMENT );
5418 if( bits % 8 != 0 )
5419 return( PSA_ERROR_INVALID_ARGUMENT );
5420 data = mbedtls_calloc( 1, bytes );
5421 if( data == NULL )
5422 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5423
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005424 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005425 if( status != PSA_SUCCESS )
5426 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005427#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005428 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005429 psa_des_set_key_parity( data, bytes );
5430#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005431 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005432
5433exit:
5434 mbedtls_free( data );
5435 return( status );
5436}
5437
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005438psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005439 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005440 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005441{
5442 psa_status_t status;
5443 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005444 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005445
5446 /* Reject any attempt to create a zero-length key so that we don't
5447 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5448 if( psa_get_key_bits( attributes ) == 0 )
5449 return( PSA_ERROR_INVALID_ARGUMENT );
5450
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005451 if( ! operation->can_output_key )
5452 return( PSA_ERROR_NOT_PERMITTED );
5453
Gilles Peskinedf179142019-07-15 22:02:14 +02005454 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5455 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005456#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5457 if( driver != NULL )
5458 {
5459 /* Deriving a key in a secure element is not implemented yet. */
5460 status = PSA_ERROR_NOT_SUPPORTED;
5461 }
5462#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005463 if( status == PSA_SUCCESS )
5464 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005465 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005466 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005467 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005468 }
5469 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005470 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005471 if( status != PSA_SUCCESS )
5472 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005473 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005474 *handle = 0;
5475 }
5476 return( status );
5477}
5478
Gilles Peskineeab56e42018-07-12 17:12:33 +02005479
5480
5481/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005482/* Key derivation */
5483/****************************************************************/
5484
Gilles Peskine969c5d62019-01-16 15:53:06 +01005485static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005486 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005487 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005488{
John Durkopd0321952020-10-29 21:37:36 -07005489#if !defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
5490 (void)kdf_alg;
5491#endif
Janos Follath5fe19732019-06-20 15:09:30 +01005492 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5493 * initialisers for this union leave some bytes unspecified.) */
5494 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5495
Gilles Peskine969c5d62019-01-16 15:53:06 +01005496 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005497#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005498 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5499 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5500 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005501 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005502 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005503 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5504 if( hash_size == 0 )
5505 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005506 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5507 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005508 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005509 {
5510 return( PSA_ERROR_NOT_SUPPORTED );
5511 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005512 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005513 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005514 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005515 else
John Durkopd0321952020-10-29 21:37:36 -07005516#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005517 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005518}
5519
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005520psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005521 psa_algorithm_t alg )
5522{
5523 psa_status_t status;
5524
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005526 return( PSA_ERROR_BAD_STATE );
5527
Gilles Peskine6843c292019-01-18 16:44:49 +01005528 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5529 return( PSA_ERROR_INVALID_ARGUMENT );
5530 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005531 {
5532 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005533 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005534 }
5535 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5536 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005537 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005538 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005539 else
5540 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005541
5542 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005543 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005544 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005545}
5546
John Durkopd0321952020-10-29 21:37:36 -07005547#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005548static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005549 psa_algorithm_t hash_alg,
5550 psa_key_derivation_step_t step,
5551 const uint8_t *data,
5552 size_t data_length )
5553{
5554 psa_status_t status;
5555 switch( step )
5556 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005557 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005558 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005559 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005560 status = psa_hmac_setup_internal( &hkdf->hmac,
5561 data, data_length,
5562 hash_alg );
5563 if( status != PSA_SUCCESS )
5564 return( status );
5565 hkdf->state = HKDF_STATE_STARTED;
5566 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005567 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005568 /* If no salt was provided, use an empty salt. */
5569 if( hkdf->state == HKDF_STATE_INIT )
5570 {
5571 status = psa_hmac_setup_internal( &hkdf->hmac,
5572 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005573 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005574 if( status != PSA_SUCCESS )
5575 return( status );
5576 hkdf->state = HKDF_STATE_STARTED;
5577 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005578 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005579 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005580 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5581 data, data_length );
5582 if( status != PSA_SUCCESS )
5583 return( status );
5584 status = psa_hmac_finish_internal( &hkdf->hmac,
5585 hkdf->prk,
5586 sizeof( hkdf->prk ) );
5587 if( status != PSA_SUCCESS )
5588 return( status );
5589 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5590 hkdf->block_number = 0;
5591 hkdf->state = HKDF_STATE_KEYED;
5592 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005593 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005594 if( hkdf->state == HKDF_STATE_OUTPUT )
5595 return( PSA_ERROR_BAD_STATE );
5596 if( hkdf->info_set )
5597 return( PSA_ERROR_BAD_STATE );
5598 hkdf->info_length = data_length;
5599 if( data_length != 0 )
5600 {
5601 hkdf->info = mbedtls_calloc( 1, data_length );
5602 if( hkdf->info == NULL )
5603 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5604 memcpy( hkdf->info, data, data_length );
5605 }
5606 hkdf->info_set = 1;
5607 return( PSA_SUCCESS );
5608 default:
5609 return( PSA_ERROR_INVALID_ARGUMENT );
5610 }
5611}
Janos Follathb03233e2019-06-11 15:30:30 +01005612
Janos Follathf08e2652019-06-13 09:05:41 +01005613static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5614 const uint8_t *data,
5615 size_t data_length )
5616{
5617 if( prf->state != TLS12_PRF_STATE_INIT )
5618 return( PSA_ERROR_BAD_STATE );
5619
Janos Follathd6dce9f2019-07-04 09:11:38 +01005620 if( data_length != 0 )
5621 {
5622 prf->seed = mbedtls_calloc( 1, data_length );
5623 if( prf->seed == NULL )
5624 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005625
Janos Follathd6dce9f2019-07-04 09:11:38 +01005626 memcpy( prf->seed, data, data_length );
5627 prf->seed_length = data_length;
5628 }
Janos Follathf08e2652019-06-13 09:05:41 +01005629
5630 prf->state = TLS12_PRF_STATE_SEED_SET;
5631
5632 return( PSA_SUCCESS );
5633}
5634
Janos Follath81550542019-06-13 14:26:34 +01005635static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5636 psa_algorithm_t hash_alg,
5637 const uint8_t *data,
5638 size_t data_length )
5639{
5640 psa_status_t status;
5641 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5642 return( PSA_ERROR_BAD_STATE );
5643
5644 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5645 if( status != PSA_SUCCESS )
5646 return( status );
5647
5648 prf->state = TLS12_PRF_STATE_KEY_SET;
5649
5650 return( PSA_SUCCESS );
5651}
5652
Janos Follath6660f0e2019-06-17 08:44:03 +01005653static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5654 psa_tls12_prf_key_derivation_t *prf,
5655 psa_algorithm_t hash_alg,
5656 const uint8_t *data,
5657 size_t data_length )
5658{
5659 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005660 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5661 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005662
5663 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5664 return( PSA_ERROR_INVALID_ARGUMENT );
5665
5666 /* Quoting RFC 4279, Section 2:
5667 *
5668 * The premaster secret is formed as follows: if the PSK is N octets
5669 * long, concatenate a uint16 with the value N, N zero octets, a second
5670 * uint16 with the value N, and the PSK itself.
5671 */
5672
Janos Follath40e13932019-06-26 13:22:29 +01005673 *cur++ = ( data_length >> 8 ) & 0xff;
5674 *cur++ = ( data_length >> 0 ) & 0xff;
5675 memset( cur, 0, data_length );
5676 cur += data_length;
5677 *cur++ = pms[0];
5678 *cur++ = pms[1];
5679 memcpy( cur, data, data_length );
5680 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005681
Janos Follath40e13932019-06-26 13:22:29 +01005682 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005683
5684 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5685 return( status );
5686}
5687
Janos Follath63028dd2019-06-13 09:15:47 +01005688static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5689 const uint8_t *data,
5690 size_t data_length )
5691{
5692 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5693 return( PSA_ERROR_BAD_STATE );
5694
Janos Follathd6dce9f2019-07-04 09:11:38 +01005695 if( data_length != 0 )
5696 {
5697 prf->label = mbedtls_calloc( 1, data_length );
5698 if( prf->label == NULL )
5699 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005700
Janos Follathd6dce9f2019-07-04 09:11:38 +01005701 memcpy( prf->label, data, data_length );
5702 prf->label_length = data_length;
5703 }
Janos Follath63028dd2019-06-13 09:15:47 +01005704
5705 prf->state = TLS12_PRF_STATE_LABEL_SET;
5706
5707 return( PSA_SUCCESS );
5708}
5709
Janos Follathb03233e2019-06-11 15:30:30 +01005710static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5711 psa_algorithm_t hash_alg,
5712 psa_key_derivation_step_t step,
5713 const uint8_t *data,
5714 size_t data_length )
5715{
Janos Follathb03233e2019-06-11 15:30:30 +01005716 switch( step )
5717 {
Janos Follathf08e2652019-06-13 09:05:41 +01005718 case PSA_KEY_DERIVATION_INPUT_SEED:
5719 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005720 case PSA_KEY_DERIVATION_INPUT_SECRET:
5721 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005722 case PSA_KEY_DERIVATION_INPUT_LABEL:
5723 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005724 default:
5725 return( PSA_ERROR_INVALID_ARGUMENT );
5726 }
5727}
Janos Follath6660f0e2019-06-17 08:44:03 +01005728
5729static psa_status_t psa_tls12_prf_psk_to_ms_input(
5730 psa_tls12_prf_key_derivation_t *prf,
5731 psa_algorithm_t hash_alg,
5732 psa_key_derivation_step_t step,
5733 const uint8_t *data,
5734 size_t data_length )
5735{
5736 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005737 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005738 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5739 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005740 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005741
5742 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5743}
John Durkopd0321952020-10-29 21:37:36 -07005744#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005745
Gilles Peskineb8965192019-09-24 16:21:10 +02005746/** Check whether the given key type is acceptable for the given
5747 * input step of a key derivation.
5748 *
5749 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5750 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5751 * Both secret and non-secret inputs can alternatively have the type
5752 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5753 * that the input was passed as a buffer rather than via a key object.
5754 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005755static int psa_key_derivation_check_input_type(
5756 psa_key_derivation_step_t step,
5757 psa_key_type_t key_type )
5758{
5759 switch( step )
5760 {
5761 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005762 if( key_type == PSA_KEY_TYPE_DERIVE )
5763 return( PSA_SUCCESS );
5764 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005765 return( PSA_SUCCESS );
5766 break;
5767 case PSA_KEY_DERIVATION_INPUT_LABEL:
5768 case PSA_KEY_DERIVATION_INPUT_SALT:
5769 case PSA_KEY_DERIVATION_INPUT_INFO:
5770 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005771 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5772 return( PSA_SUCCESS );
5773 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005774 return( PSA_SUCCESS );
5775 break;
5776 }
5777 return( PSA_ERROR_INVALID_ARGUMENT );
5778}
5779
Janos Follathb80a94e2019-06-12 15:54:46 +01005780static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005781 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005782 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005783 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005784 const uint8_t *data,
5785 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005786{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005787 psa_status_t status;
John Durkopd0321952020-10-29 21:37:36 -07005788#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005789 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
John Durkopd0321952020-10-29 21:37:36 -07005790#else
5791 (void)data;
5792 (void)data_length;
5793#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005794
5795 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005796 if( status != PSA_SUCCESS )
5797 goto exit;
5798
John Durkopd0321952020-10-29 21:37:36 -07005799#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005800 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005801 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005802 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005803 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005804 step, data, data_length );
5805 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005806 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005807 {
Janos Follathb03233e2019-06-11 15:30:30 +01005808 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5809 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5810 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005811 }
5812 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5813 {
5814 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5815 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5816 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005817 }
5818 else
John Durkopd0321952020-10-29 21:37:36 -07005819#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005820 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005821 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005822 return( PSA_ERROR_BAD_STATE );
5823 }
5824
Gilles Peskine224b0d62019-09-23 18:13:17 +02005825exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005826 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005827 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005828 return( status );
5829}
5830
Janos Follath51f4a0f2019-06-14 11:35:55 +01005831psa_status_t psa_key_derivation_input_bytes(
5832 psa_key_derivation_operation_t *operation,
5833 psa_key_derivation_step_t step,
5834 const uint8_t *data,
5835 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005836{
Gilles Peskineb8965192019-09-24 16:21:10 +02005837 return( psa_key_derivation_input_internal( operation, step,
5838 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005839 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005840}
5841
Janos Follath51f4a0f2019-06-14 11:35:55 +01005842psa_status_t psa_key_derivation_input_key(
5843 psa_key_derivation_operation_t *operation,
5844 psa_key_derivation_step_t step,
5845 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005846{
5847 psa_key_slot_t *slot;
5848 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005849
Gilles Peskine28f8f302019-07-24 13:30:31 +02005850 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005851 PSA_KEY_USAGE_DERIVE,
5852 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005853 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005854 {
5855 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005856 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005857 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005858
5859 /* Passing a key object as a SECRET input unlocks the permission
5860 * to output to a key object. */
5861 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5862 operation->can_output_key = 1;
5863
Janos Follathb80a94e2019-06-12 15:54:46 +01005864 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005865 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005866 slot->data.key.data,
5867 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005868}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005869
5870
5871
5872/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005873/* Key agreement */
5874/****************************************************************/
5875
John Durkop6ba40d12020-11-10 08:50:04 -08005876#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005877static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5878 size_t peer_key_length,
5879 const mbedtls_ecp_keypair *our_key,
5880 uint8_t *shared_secret,
5881 size_t shared_secret_size,
5882 size_t *shared_secret_length )
5883{
Steven Cooremand4867872020-08-05 16:31:39 +02005884 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005885 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005886 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005887 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005888 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005889 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005890
Steven Cooreman4fed4552020-08-03 14:46:03 +02005891 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5892 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005893 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005894 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005895 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005896 goto exit;
5897
Jaeden Amero97271b32019-01-10 19:38:51 +00005898 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005899 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005900 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005901 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005902 status = mbedtls_to_psa_error(
5903 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5904 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005905 goto exit;
5906
Jaeden Amero97271b32019-01-10 19:38:51 +00005907 status = mbedtls_to_psa_error(
5908 mbedtls_ecdh_calc_secret( &ecdh,
5909 shared_secret_length,
5910 shared_secret, shared_secret_size,
5911 mbedtls_ctr_drbg_random,
5912 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005913 if( status != PSA_SUCCESS )
5914 goto exit;
5915 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5916 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005917
5918exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005919 if( status != PSA_SUCCESS )
5920 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005921 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005922 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005923 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005924
Jaeden Amero97271b32019-01-10 19:38:51 +00005925 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005926}
John Durkop6ba40d12020-11-10 08:50:04 -08005927#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005928
Gilles Peskine01d718c2018-09-18 12:01:02 +02005929#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5930
Gilles Peskine0216fe12019-04-11 21:23:21 +02005931static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5932 psa_key_slot_t *private_key,
5933 const uint8_t *peer_key,
5934 size_t peer_key_length,
5935 uint8_t *shared_secret,
5936 size_t shared_secret_size,
5937 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005938{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005939 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005940 {
John Durkop6ba40d12020-11-10 08:50:04 -08005941#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005942 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005943 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005944 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005945 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005946 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005947 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005948 private_key->data.key.data,
5949 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005950 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005951 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005952 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005953 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005954 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005955 shared_secret, shared_secret_size,
5956 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005957 mbedtls_ecp_keypair_free( ecp );
5958 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005959 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005960#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005961 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005962 (void) private_key;
5963 (void) peer_key;
5964 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005965 (void) shared_secret;
5966 (void) shared_secret_size;
5967 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005968 return( PSA_ERROR_NOT_SUPPORTED );
5969 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005970}
5971
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005972/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005973 * to potentially free embedded data structures and wipe confidential data.
5974 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005975static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005976 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005977 psa_key_slot_t *private_key,
5978 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005979 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005980{
5981 psa_status_t status;
5982 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5983 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005984 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005985
5986 /* Step 1: run the secret agreement algorithm to generate the shared
5987 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005988 status = psa_key_agreement_raw_internal( ka_alg,
5989 private_key,
5990 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005991 shared_secret,
5992 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005993 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005994 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005995 goto exit;
5996
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005997 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005998 * the shared secret. A shared secret is permitted wherever a key
5999 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006000 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006001 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006002 shared_secret,
6003 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006004exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006005 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006006 return( status );
6007}
6008
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006009psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006010 psa_key_derivation_step_t step,
6011 psa_key_handle_t private_key,
6012 const uint8_t *peer_key,
6013 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006014{
Gilles Peskine2f060a82018-12-04 17:12:32 +01006015 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02006016 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006017 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006018 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02006019 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006020 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006021 if( status != PSA_SUCCESS )
6022 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006023 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006024 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006025 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006026 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006027 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006028 else
6029 {
6030 /* If a private key has been added as SECRET, we allow the derived
6031 * key material to be used as a key in PSA Crypto. */
6032 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6033 operation->can_output_key = 1;
6034 }
Gilles Peskine346797d2018-11-16 16:05:06 +01006035 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006036}
6037
Gilles Peskinebe697d82019-05-16 18:00:41 +02006038psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
6039 psa_key_handle_t private_key,
6040 const uint8_t *peer_key,
6041 size_t peer_key_length,
6042 uint8_t *output,
6043 size_t output_size,
6044 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006045{
6046 psa_key_slot_t *slot;
6047 psa_status_t status;
6048
6049 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6050 {
6051 status = PSA_ERROR_INVALID_ARGUMENT;
6052 goto exit;
6053 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02006054 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006055 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006056 if( status != PSA_SUCCESS )
6057 goto exit;
6058
6059 status = psa_key_agreement_raw_internal( alg, slot,
6060 peer_key, peer_key_length,
6061 output, output_size,
6062 output_length );
6063
6064exit:
6065 if( status != PSA_SUCCESS )
6066 {
6067 /* If an error happens and is not handled properly, the output
6068 * may be used as a key to protect sensitive data. Arrange for such
6069 * a key to be random, which is likely to result in decryption or
6070 * verification errors. This is better than filling the buffer with
6071 * some constant data such as zeros, which would result in the data
6072 * being protected with a reproducible, easily knowable key.
6073 */
6074 psa_generate_random( output, output_size );
6075 *output_length = output_size;
6076 }
6077 return( status );
6078}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006079
6080
6081/****************************************************************/
6082/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006083/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006084
Gilles Peskine4c317f42018-07-12 01:24:09 +02006085psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006086 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006087{
Janos Follath24eed8d2019-11-22 13:21:35 +00006088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006089 GUARD_MODULE_INITIALIZED;
6090
Gilles Peskinef181eca2019-08-07 13:49:00 +02006091 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6092 {
6093 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6094 output,
6095 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6096 if( ret != 0 )
6097 return( mbedtls_to_psa_error( ret ) );
6098 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6099 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6100 }
6101
Gilles Peskinee59236f2018-01-27 23:32:46 +01006102 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6103 return( mbedtls_to_psa_error( ret ) );
6104}
6105
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006106#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6107#include "mbedtls/entropy_poll.h"
6108
Gilles Peskine7228da22019-07-15 11:06:15 +02006109psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006110 size_t seed_size )
6111{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006112 if( global_data.initialized )
6113 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006114
6115 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6116 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6117 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6118 return( PSA_ERROR_INVALID_ARGUMENT );
6119
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006120 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006121}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006122#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006123
John Durkop6ba40d12020-11-10 08:50:04 -08006124#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6125 defined(MBEDTLS_GENPRIME)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006126static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6127 size_t domain_parameters_size,
6128 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006129{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006130 size_t i;
6131 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006132
Gilles Peskinee56e8782019-04-26 17:34:02 +02006133 if( domain_parameters_size == 0 )
6134 {
6135 *exponent = 65537;
6136 return( PSA_SUCCESS );
6137 }
6138
6139 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6140 * support values that fit in a 32-bit integer, which is larger than
6141 * int on just about every platform anyway. */
6142 if( domain_parameters_size > sizeof( acc ) )
6143 return( PSA_ERROR_NOT_SUPPORTED );
6144 for( i = 0; i < domain_parameters_size; i++ )
6145 acc = ( acc << 8 ) | domain_parameters[i];
6146 if( acc > INT_MAX )
6147 return( PSA_ERROR_NOT_SUPPORTED );
6148 *exponent = acc;
6149 return( PSA_SUCCESS );
6150}
John Durkop6ba40d12020-11-10 08:50:04 -08006151#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) &&
6152 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006153
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006154static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006155 psa_key_slot_t *slot, size_t bits,
6156 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006157{
Gilles Peskine8e338702019-07-30 20:06:31 +02006158 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006159
Gilles Peskinee56e8782019-04-26 17:34:02 +02006160 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006161 return( PSA_ERROR_INVALID_ARGUMENT );
6162
Gilles Peskinee59236f2018-01-27 23:32:46 +01006163 if( key_type_is_raw_bytes( type ) )
6164 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006165 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006166
6167 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006168 if( status != PSA_SUCCESS )
6169 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006170
6171 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006172 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6173 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006174 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006175
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006176 status = psa_generate_random( slot->data.key.data,
6177 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006178 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006179 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006180
6181 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006182#if defined(MBEDTLS_DES_C)
6183 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006184 psa_des_set_key_parity( slot->data.key.data,
6185 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006186#endif /* MBEDTLS_DES_C */
6187 }
6188 else
6189
John Durkop6ba40d12020-11-10 08:50:04 -08006190#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6191 defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006192 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006193 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006194 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006195 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006196 int exponent;
6197 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006198 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6199 return( PSA_ERROR_NOT_SUPPORTED );
6200 /* Accept only byte-aligned keys, for the same reasons as
6201 * in psa_import_rsa_key(). */
6202 if( bits % 8 != 0 )
6203 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006204 status = psa_read_rsa_exponent( domain_parameters,
6205 domain_parameters_size,
6206 &exponent );
6207 if( status != PSA_SUCCESS )
6208 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006209 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6210 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006211 mbedtls_ctr_drbg_random,
6212 &global_data.ctr_drbg,
6213 (unsigned int) bits,
6214 exponent );
6215 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006216 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006217
6218 /* Make sure to always have an export representation available */
6219 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6220
Steven Cooreman75b74362020-07-28 14:30:13 +02006221 status = psa_allocate_buffer_to_slot( slot, bytes );
6222 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006223 {
6224 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006225 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006226 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006227
6228 status = psa_export_rsa_key( type,
6229 &rsa,
6230 slot->data.key.data,
6231 bytes,
6232 &slot->data.key.bytes );
6233 mbedtls_rsa_free( &rsa );
6234 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006235 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006236 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006237 }
6238 else
John Durkop6ba40d12020-11-10 08:50:04 -08006239#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) &&
6240 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006241
John Durkop9814fa22020-11-04 12:28:15 -08006242#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006243 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006244 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006245 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006246 mbedtls_ecp_group_id grp_id =
6247 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006248 const mbedtls_ecp_curve_info *curve_info =
6249 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006250 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006252 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006253 return( PSA_ERROR_NOT_SUPPORTED );
6254 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6255 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006256 mbedtls_ecp_keypair_init( &ecp );
6257 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006258 mbedtls_ctr_drbg_random,
6259 &global_data.ctr_drbg );
6260 if( ret != 0 )
6261 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006262 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006263 return( mbedtls_to_psa_error( ret ) );
6264 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006265
6266
6267 /* Make sure to always have an export representation available */
6268 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006269 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6270 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006271 {
6272 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006273 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006274 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006275
6276 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006277 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6278
6279 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006280 if( status != PSA_SUCCESS ) {
6281 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006282 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006283 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006284 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006285 }
6286 else
John Durkop9814fa22020-11-04 12:28:15 -08006287#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006288 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006289 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006290 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006291
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006292 return( PSA_SUCCESS );
6293}
Darryl Green0c6575a2018-11-07 16:05:30 +00006294
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006295psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006296 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006297{
6298 psa_status_t status;
6299 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006300 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006301
Gilles Peskine0f84d622019-09-12 19:03:13 +02006302 /* Reject any attempt to create a zero-length key so that we don't
6303 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6304 if( psa_get_key_bits( attributes ) == 0 )
6305 return( PSA_ERROR_INVALID_ARGUMENT );
6306
Gilles Peskinedf179142019-07-15 22:02:14 +02006307 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6308 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006309 if( status != PSA_SUCCESS )
6310 goto exit;
6311
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006312 status = psa_driver_wrapper_generate_key( attributes,
6313 slot );
6314 if( status != PSA_ERROR_NOT_SUPPORTED ||
6315 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6316 goto exit;
6317
6318 status = psa_generate_key_internal(
6319 slot, attributes->core.bits,
6320 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006321
6322exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006323 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006324 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006325 if( status != PSA_SUCCESS )
6326 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006327 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006328 *handle = 0;
6329 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006330 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006331}
6332
6333
Gilles Peskinee59236f2018-01-27 23:32:46 +01006334
6335/****************************************************************/
6336/* Module setup */
6337/****************************************************************/
6338
Gilles Peskine5e769522018-11-20 21:59:56 +01006339psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6340 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6341 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6342{
6343 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6344 return( PSA_ERROR_BAD_STATE );
6345 global_data.entropy_init = entropy_init;
6346 global_data.entropy_free = entropy_free;
6347 return( PSA_SUCCESS );
6348}
6349
Gilles Peskinee59236f2018-01-27 23:32:46 +01006350void mbedtls_psa_crypto_free( void )
6351{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006352 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006353 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6354 {
6355 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006356 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006357 }
6358 /* Wipe all remaining data, including configuration.
6359 * In particular, this sets all state indicator to the value
6360 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006361 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006362#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006363 /* Unregister all secure element drivers, so that we restart from
6364 * a pristine state. */
6365 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006366#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006367}
6368
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006369#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6370/** Recover a transaction that was interrupted by a power failure.
6371 *
6372 * This function is called during initialization, before psa_crypto_init()
6373 * returns. If this function returns a failure status, the initialization
6374 * fails.
6375 */
6376static psa_status_t psa_crypto_recover_transaction(
6377 const psa_crypto_transaction_t *transaction )
6378{
6379 switch( transaction->unknown.type )
6380 {
6381 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6382 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006383 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006384 * is implemented.
6385 * https://github.com/ARMmbed/mbed-crypto/issues/218
6386 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006387 default:
6388 /* We found an unsupported transaction in the storage.
6389 * We don't know what state the storage is in. Give up. */
6390 return( PSA_ERROR_STORAGE_FAILURE );
6391 }
6392}
6393#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6394
Gilles Peskinee59236f2018-01-27 23:32:46 +01006395psa_status_t psa_crypto_init( void )
6396{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006397 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006398 const unsigned char drbg_seed[] = "PSA";
6399
Gilles Peskinec6b69072018-11-20 21:42:52 +01006400 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006401 if( global_data.initialized != 0 )
6402 return( PSA_SUCCESS );
6403
Gilles Peskine5e769522018-11-20 21:59:56 +01006404 /* Set default configuration if
6405 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6406 if( global_data.entropy_init == NULL )
6407 global_data.entropy_init = mbedtls_entropy_init;
6408 if( global_data.entropy_free == NULL )
6409 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006410
Gilles Peskinec6b69072018-11-20 21:42:52 +01006411 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006412 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006413#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6414 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6415 /* The PSA entropy injection feature depends on using NV seed as an entropy
6416 * source. Add NV seed as an entropy source for PSA entropy injection. */
6417 mbedtls_entropy_add_source( &global_data.entropy,
6418 mbedtls_nv_seed_poll, NULL,
6419 MBEDTLS_ENTROPY_BLOCK_SIZE,
6420 MBEDTLS_ENTROPY_SOURCE_STRONG );
6421#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006422 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006423 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006424 status = mbedtls_to_psa_error(
6425 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6426 mbedtls_entropy_func,
6427 &global_data.entropy,
6428 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6429 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006430 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006431 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006432
Gilles Peskine66fb1262018-12-10 16:29:04 +01006433 status = psa_initialize_key_slots( );
6434 if( status != PSA_SUCCESS )
6435 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006436
Gilles Peskined9348f22019-10-01 15:22:29 +02006437#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6438 status = psa_init_all_se_drivers( );
6439 if( status != PSA_SUCCESS )
6440 goto exit;
6441#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6442
Gilles Peskine4b734222019-07-24 15:56:31 +02006443#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006444 status = psa_crypto_load_transaction( );
6445 if( status == PSA_SUCCESS )
6446 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006447 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6448 if( status != PSA_SUCCESS )
6449 goto exit;
6450 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006451 }
6452 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6453 {
6454 /* There's no transaction to complete. It's all good. */
6455 status = PSA_SUCCESS;
6456 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006457#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006458
Gilles Peskinec6b69072018-11-20 21:42:52 +01006459 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006460 global_data.initialized = 1;
6461
6462exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006463 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006464 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006465 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006466}
6467
6468#endif /* MBEDTLS_PSA_CRYPTO_C */