blob: af8a7a97399f8da9185ba9b8de9ff730caa87427 [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
Gilles Peskinee59236f2018-01-27 23:32:46 +0100128static psa_status_t mbedtls_to_psa_error( int ret )
129{
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:
202 return( PSA_ERROR_BAD_STATE );
203 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
205 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200206 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
208 return( PSA_ERROR_HARDWARE_FAILURE );
209
210 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
214 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
215 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
216 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
217 return( PSA_ERROR_NOT_SUPPORTED );
218 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
220
221 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
Gilles Peskinee59236f2018-01-27 23:32:46 +0100226 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
227 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
228 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
229 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100230
231 case MBEDTLS_ERR_GCM_AUTH_FAILED:
232 return( PSA_ERROR_INVALID_SIGNATURE );
233 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200234 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
243 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
244 return( PSA_ERROR_NOT_SUPPORTED );
245 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
246 return( PSA_ERROR_INVALID_ARGUMENT );
247 case MBEDTLS_ERR_MD_ALLOC_FAILED:
248 return( PSA_ERROR_INSUFFICIENT_MEMORY );
249 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
250 return( PSA_ERROR_STORAGE_FAILURE );
251 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
255 return( PSA_ERROR_STORAGE_FAILURE );
256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
261 return( PSA_ERROR_BUFFER_TOO_SMALL );
262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
269 return( PSA_ERROR_INSUFFICIENT_MEMORY );
270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
282 return( PSA_ERROR_NOT_SUPPORTED );
283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
285 return( PSA_ERROR_NOT_PERMITTED );
286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
291 return( PSA_ERROR_NOT_SUPPORTED );
292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
293 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
300 return( PSA_ERROR_NOT_SUPPORTED );
301
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
303 return( PSA_ERROR_HARDWARE_FAILURE );
304
305 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_RSA_INVALID_PADDING:
308 return( PSA_ERROR_INVALID_PADDING );
309 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
312 return( PSA_ERROR_INVALID_ARGUMENT );
313 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
314 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200315 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100316 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
317 return( PSA_ERROR_INVALID_SIGNATURE );
318 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
319 return( PSA_ERROR_BUFFER_TOO_SMALL );
320 case MBEDTLS_ERR_RSA_RNG_FAILED:
321 return( PSA_ERROR_INSUFFICIENT_MEMORY );
322 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
323 return( PSA_ERROR_NOT_SUPPORTED );
324 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
327 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
328 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
329 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
330 return( PSA_ERROR_HARDWARE_FAILURE );
331
332 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
333 return( PSA_ERROR_INVALID_ARGUMENT );
334 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
336
itayzafrir5c753392018-05-08 11:18:38 +0300337 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300338 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300339 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
341 return( PSA_ERROR_BUFFER_TOO_SMALL );
342 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
343 return( PSA_ERROR_NOT_SUPPORTED );
344 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
345 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
346 return( PSA_ERROR_INVALID_SIGNATURE );
347 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
348 return( PSA_ERROR_INSUFFICIENT_MEMORY );
349 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
350 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000351 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
352 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
Gilles Peskine8e338702019-07-30 20:06:31 +0200369 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100374mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100375 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200376{
377 switch( curve )
378 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100379 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100380 switch( byte_length )
381 {
382 case PSA_BITS_TO_BYTES( 192 ):
383 return( MBEDTLS_ECP_DP_SECP192R1 );
384 case PSA_BITS_TO_BYTES( 224 ):
385 return( MBEDTLS_ECP_DP_SECP224R1 );
386 case PSA_BITS_TO_BYTES( 256 ):
387 return( MBEDTLS_ECP_DP_SECP256R1 );
388 case PSA_BITS_TO_BYTES( 384 ):
389 return( MBEDTLS_ECP_DP_SECP384R1 );
390 case PSA_BITS_TO_BYTES( 521 ):
391 return( MBEDTLS_ECP_DP_SECP521R1 );
392 default:
393 return( MBEDTLS_ECP_DP_NONE );
394 }
395 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100396
Paul Elliott8ff510a2020-06-02 17:19:28 +0100397 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100398 switch( byte_length )
399 {
400 case PSA_BITS_TO_BYTES( 256 ):
401 return( MBEDTLS_ECP_DP_BP256R1 );
402 case PSA_BITS_TO_BYTES( 384 ):
403 return( MBEDTLS_ECP_DP_BP384R1 );
404 case PSA_BITS_TO_BYTES( 512 ):
405 return( MBEDTLS_ECP_DP_BP512R1 );
406 default:
407 return( MBEDTLS_ECP_DP_NONE );
408 }
409 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100410
Paul Elliott8ff510a2020-06-02 17:19:28 +0100411 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 switch( byte_length )
413 {
414 case PSA_BITS_TO_BYTES( 255 ):
415 return( MBEDTLS_ECP_DP_CURVE25519 );
416 case PSA_BITS_TO_BYTES( 448 ):
417 return( MBEDTLS_ECP_DP_CURVE448 );
418 default:
419 return( MBEDTLS_ECP_DP_NONE );
420 }
421 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100422
Paul Elliott8ff510a2020-06-02 17:19:28 +0100423 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 switch( byte_length )
425 {
426 case PSA_BITS_TO_BYTES( 192 ):
427 return( MBEDTLS_ECP_DP_SECP192K1 );
428 case PSA_BITS_TO_BYTES( 224 ):
429 return( MBEDTLS_ECP_DP_SECP224K1 );
430 case PSA_BITS_TO_BYTES( 256 ):
431 return( MBEDTLS_ECP_DP_SECP256K1 );
432 default:
433 return( MBEDTLS_ECP_DP_NONE );
434 }
435 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100436
Gilles Peskine12313cd2018-06-20 00:20:32 +0200437 default:
438 return( MBEDTLS_ECP_DP_NONE );
439 }
440}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100441#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200442
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200443static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
444 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445{
446 /* Check that the bit size is acceptable for the key type */
447 switch( type )
448 {
449 case PSA_KEY_TYPE_RAW_DATA:
450#if defined(MBEDTLS_MD_C)
451 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200452#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200453 case PSA_KEY_TYPE_DERIVE:
454 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200455#if defined(MBEDTLS_AES_C)
456 case PSA_KEY_TYPE_AES:
457 if( bits != 128 && bits != 192 && bits != 256 )
458 return( PSA_ERROR_INVALID_ARGUMENT );
459 break;
460#endif
461#if defined(MBEDTLS_CAMELLIA_C)
462 case PSA_KEY_TYPE_CAMELLIA:
463 if( bits != 128 && bits != 192 && bits != 256 )
464 return( PSA_ERROR_INVALID_ARGUMENT );
465 break;
466#endif
467#if defined(MBEDTLS_DES_C)
468 case PSA_KEY_TYPE_DES:
469 if( bits != 64 && bits != 128 && bits != 192 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
471 break;
472#endif
473#if defined(MBEDTLS_ARC4_C)
474 case PSA_KEY_TYPE_ARC4:
475 if( bits < 8 || bits > 2048 )
476 return( PSA_ERROR_INVALID_ARGUMENT );
477 break;
478#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200479#if defined(MBEDTLS_CHACHA20_C)
480 case PSA_KEY_TYPE_CHACHA20:
481 if( bits != 256 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200485 default:
486 return( PSA_ERROR_NOT_SUPPORTED );
487 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200488 if( bits % 8 != 0 )
489 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491 return( PSA_SUCCESS );
492}
493
Steven Cooremana01795d2020-07-24 22:48:15 +0200494#if defined(MBEDTLS_RSA_C)
495
496#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100497/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
498 * that are not a multiple of 8) well. For example, there is only
499 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
500 * way to return the exact bit size of a key.
501 * To keep things simple, reject non-byte-aligned key sizes. */
502static psa_status_t psa_check_rsa_key_byte_aligned(
503 const mbedtls_rsa_context *rsa )
504{
505 mbedtls_mpi n;
506 psa_status_t status;
507 mbedtls_mpi_init( &n );
508 status = mbedtls_to_psa_error(
509 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
510 if( status == PSA_SUCCESS )
511 {
512 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
513 status = PSA_ERROR_NOT_SUPPORTED;
514 }
515 mbedtls_mpi_free( &n );
516 return( status );
517}
Steven Cooremana01795d2020-07-24 22:48:15 +0200518#endif /* MBEDTLS_PK_PARSE_C */
Gilles Peskine86a440b2018-11-12 18:39:40 +0100519
Steven Cooreman7f391872020-07-30 14:57:44 +0200520/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200521 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200522 * \param[in] type The type of key contained in \p data.
523 * \param[in] data The buffer from which to load the representation.
524 * \param[in] data_length The size in bytes of \p data.
525 * \param[out] p_rsa Returns a pointer to an RSA context on success.
526 * The caller is responsible for freeing both the
527 * contents of the context and the context itself
528 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200529 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200530static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
531 const uint8_t *data,
532 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200533 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200534{
Steven Cooremana01795d2020-07-24 22:48:15 +0200535#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000536 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200537 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000538 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200539 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000540
541 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200542 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200544 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200547 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000548 if( status != PSA_SUCCESS )
549 goto exit;
550
551 /* We have something that the pkparse module recognizes. If it is a
552 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200553 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555 status = PSA_ERROR_INVALID_ARGUMENT;
556 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
560 * supports non-byte-aligned key sizes, but not well. For example,
561 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200562 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000563 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
564 {
565 status = PSA_ERROR_NOT_SUPPORTED;
566 goto exit;
567 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200568 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200569 if( status != PSA_SUCCESS )
570 goto exit;
571
Steven Cooremana2371e52020-07-28 14:30:39 +0200572 /* Copy out the pointer to the RSA context, and reset the PK context
573 * such that pk_free doesn't free the RSA context we just grabbed. */
574 *p_rsa = mbedtls_pk_rsa( ctx );
575 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000576
577exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200578 mbedtls_pk_free( &ctx );
579 return( status );
580#else
Steven Cooreman4fed4552020-08-03 14:46:03 +0200581 (void) data;
582 (void) data_length;
Steven Cooreman7f391872020-07-30 14:57:44 +0200583 (void) type;
Steven Cooremana01795d2020-07-24 22:48:15 +0200584 (void) rsa;
585 return( PSA_ERROR_NOT_SUPPORTED );
586#endif /* MBEDTLS_PK_PARSE_C */
587}
588
589/** Export an RSA key to export representation
590 *
591 * \param[in] type The type of key (public/private) to export
592 * \param[in] rsa The internal RSA representation from which to export
593 * \param[out] data The buffer to export to
594 * \param[in] data_size The length of the buffer to export to
595 * \param[out] data_length The amount of bytes written to \p data
596 */
597static psa_status_t psa_export_rsa_key( psa_key_type_t type,
598 mbedtls_rsa_context *rsa,
599 uint8_t *data,
600 size_t data_size,
601 size_t *data_length )
602{
603#if defined(MBEDTLS_PK_WRITE_C)
604 int ret;
605 mbedtls_pk_context pk;
606 uint8_t *pos = data + data_size;
607
608 mbedtls_pk_init( &pk );
609 pk.pk_info = &mbedtls_rsa_info;
610 pk.pk_ctx = rsa;
611
612 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200613 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
614 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200615 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
616 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
617 else
618 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
619
620 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200621 {
622 /* Clean up in case pk_write failed halfway through. */
623 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200624 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200625 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200626
627 /* The mbedtls_pk_xxx functions write to the end of the buffer.
628 * Move the data to the beginning and erase remaining data
629 * at the original location. */
630 if( 2 * (size_t) ret <= data_size )
631 {
632 memcpy( data, data + data_size - ret, ret );
633 memset( data + data_size - ret, 0, ret );
634 }
635 else if( (size_t) ret < data_size )
636 {
637 memmove( data, data + data_size - ret, ret );
638 memset( data + ret, 0, data_size - ret );
639 }
640
641 *data_length = ret;
642 return( PSA_SUCCESS );
643#else
644 (void) type;
645 (void) rsa;
646 (void) data;
647 (void) data_size;
648 (void) data_length;
649 return( PSA_ERROR_NOT_SUPPORTED );
650#endif /* MBEDTLS_PK_WRITE_C */
651}
652
653/** Import an RSA key from import representation to a slot
654 *
655 * \param[in,out] slot The slot where to store the export representation to
656 * \param[in] data The buffer containing the import representation
657 * \param[in] data_length The amount of bytes in \p data
658 */
659static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
660 const uint8_t *data,
661 size_t data_length )
662{
663 psa_status_t status;
664 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200665 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200666
Steven Cooremana01795d2020-07-24 22:48:15 +0200667 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200668 status = psa_load_rsa_representation( slot->attr.type,
669 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200670 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200671 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200672 if( status != PSA_SUCCESS )
673 goto exit;
674
675 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200676 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200677
Steven Cooreman75b74362020-07-28 14:30:13 +0200678 /* Re-export the data to PSA export format, such that we can store export
679 * representation in the key slot. Export representation in case of RSA is
680 * the smallest representation that's allowed as input, so a straight-up
681 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200682 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200683 if( output == NULL )
684 {
685 status = PSA_ERROR_INSUFFICIENT_MEMORY;
686 goto exit;
687 }
688
Steven Cooremana01795d2020-07-24 22:48:15 +0200689 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200690 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200691 output,
692 data_length,
693 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200694exit:
695 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200696 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200697 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200698
699 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000700 if( status != PSA_SUCCESS )
701 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200702 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000703 return( status );
704 }
705
Steven Cooremana01795d2020-07-24 22:48:15 +0200706 /* On success, store the allocated export-formatted key. */
707 slot->data.key.data = output;
708 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000709
710 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200711}
Steven Cooremana01795d2020-07-24 22:48:15 +0200712#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200713
Jaeden Ameroccdce902019-01-10 11:42:27 +0000714#if defined(MBEDTLS_ECP_C)
Steven Cooreman7f391872020-07-30 14:57:44 +0200715/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200716 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200717 * \param[in] type The type of key contained in \p data.
718 * \param[in] data The buffer from which to load the representation.
719 * \param[in] data_length The size in bytes of \p data.
720 * \param[out] p_ecp Returns a pointer to an ECP context on success.
721 * The caller is responsible for freeing both the
722 * contents of the context and the context itself
723 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200724 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200725static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
726 const uint8_t *data,
727 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200728 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100729{
730 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200731 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200732 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200733 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100734
Steven Cooreman4fed4552020-08-03 14:46:03 +0200735 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
736 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100737 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200738 /* A Weierstrass public key is represented as:
739 * - The byte 0x04;
740 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
741 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
742 * So its data length is 2m+1 where n is the key size in bits.
743 */
744 if( ( data_length & 1 ) == 0 )
745 return( PSA_ERROR_INVALID_ARGUMENT );
746 curve_size = data_length / 2;
747
748 /* Montgomery public keys are represented in compressed format, meaning
749 * their curve_size is equal to the amount of input. */
750
751 /* Private keys are represented in uncompressed private random integer
752 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100753 }
754
Steven Cooreman6d839f02020-07-30 11:36:45 +0200755 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200756 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200757 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200758 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200759 mbedtls_ecp_keypair_init( ecp );
760
Gilles Peskine4cd32772019-12-02 20:49:42 +0100761 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200762 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
763 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100764 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200765 {
766 status = PSA_ERROR_INVALID_ARGUMENT;
767 goto exit;
768 }
769
Steven Cooremanacda8342020-07-24 23:09:52 +0200770 status = mbedtls_to_psa_error(
771 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
772 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200773 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200774
Steven Cooreman6d839f02020-07-30 11:36:45 +0200775 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200776 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200777 {
778 /* Load the public value. */
779 status = mbedtls_to_psa_error(
780 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200781 data,
782 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200783 if( status != PSA_SUCCESS )
784 goto exit;
785
786 /* Check that the point is on the curve. */
787 status = mbedtls_to_psa_error(
788 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
789 if( status != PSA_SUCCESS )
790 goto exit;
791 }
792 else
793 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200794 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200795 status = mbedtls_to_psa_error(
796 mbedtls_ecp_read_key( ecp->grp.id,
797 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200798 data,
799 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200800 if( status != PSA_SUCCESS )
801 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200802 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200803
804 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200805exit:
806 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200807 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200808 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200809 mbedtls_free( ecp );
810 }
811
Steven Cooreman29149862020-08-05 15:43:42 +0200812 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100813}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000814
Steven Cooreman4fed4552020-08-03 14:46:03 +0200815/** Export an ECP key to export representation
816 *
817 * \param[in] type The type of key (public/private) to export
818 * \param[in] ecp The internal ECP representation from which to export
819 * \param[out] data The buffer to export to
820 * \param[in] data_size The length of the buffer to export to
821 * \param[out] data_length The amount of bytes written to \p data
822 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200823static psa_status_t psa_export_ecp_key( psa_key_type_t type,
824 mbedtls_ecp_keypair *ecp,
825 uint8_t *data,
826 size_t data_size,
827 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200828{
Steven Cooremanacda8342020-07-24 23:09:52 +0200829 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000830
Steven Cooremanacda8342020-07-24 23:09:52 +0200831 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200832 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200833 /* Check whether the public part is loaded */
834 if( mbedtls_ecp_is_zero( &ecp->Q ) )
835 {
836 /* Calculate the public key */
837 status = mbedtls_to_psa_error(
838 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
839 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
840 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200841 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200842 }
843
Steven Cooreman4fed4552020-08-03 14:46:03 +0200844 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200845 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
846 MBEDTLS_ECP_PF_UNCOMPRESSED,
847 data_length,
848 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200849 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200850 if( status != PSA_SUCCESS )
851 memset( data, 0, data_size );
852
Steven Cooreman29149862020-08-05 15:43:42 +0200853 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200854 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200855 else
856 {
Steven Cooreman29149862020-08-05 15:43:42 +0200857 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200858 return( PSA_ERROR_BUFFER_TOO_SMALL );
859
860 status = mbedtls_to_psa_error(
861 mbedtls_ecp_write_key( ecp,
862 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200863 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200864 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200865 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200866 else
867 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200868
869 return( status );
870 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200871}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200872
Steven Cooreman4fed4552020-08-03 14:46:03 +0200873/** Import an ECP key from import representation to a slot
874 *
875 * \param[in,out] slot The slot where to store the export representation to
876 * \param[in] data The buffer containing the import representation
877 * \param[in] data_length The amount of bytes in \p data
878 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200879static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
880 const uint8_t *data,
881 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100882{
Steven Cooremanacda8342020-07-24 23:09:52 +0200883 psa_status_t status;
884 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200885 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100886
Steven Cooremanacda8342020-07-24 23:09:52 +0200887 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200888 status = psa_load_ecp_representation( slot->attr.type,
889 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200890 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200891 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100892 if( status != PSA_SUCCESS )
893 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100894
Steven Cooremanacda8342020-07-24 23:09:52 +0200895 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200896 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200897 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200898 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200899
Steven Cooremanacda8342020-07-24 23:09:52 +0200900 /* Re-export the data to PSA export format. There is currently no support
901 * for other input formats then the export format, so this is a 1-1
902 * copy operation. */
903 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200904 if( output == NULL )
905 {
906 status = PSA_ERROR_INSUFFICIENT_MEMORY;
907 goto exit;
908 }
909
910 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200911 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200912 output,
913 data_length,
914 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100915exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200916 /* Always free the PK object (will also free contained ECP context) */
917 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200918 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200919
920 /* Free the allocated buffer only on error. */
921 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100922 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200923 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200924 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100925 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200926
927 /* On success, store the allocated export-formatted key. */
928 slot->data.key.data = output;
929 slot->data.key.bytes = data_length;
930
931 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100932}
933#endif /* defined(MBEDTLS_ECP_C) */
934
Gilles Peskineb46bef22019-07-30 21:32:04 +0200935/** Return the size of the key in the given slot, in bits.
936 *
937 * \param[in] slot A key slot.
938 *
939 * \return The key size in bits, read from the metadata in the slot.
940 */
941static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
942{
943 return( slot->attr.bits );
944}
945
Steven Cooreman75b74362020-07-28 14:30:13 +0200946/** Try to allocate a buffer to an empty key slot.
947 *
948 * \param[in,out] slot Key slot to attach buffer to.
949 * \param[in] buffer_length Requested size of the buffer.
950 *
951 * \retval #PSA_SUCCESS
952 * The buffer has been successfully allocated.
953 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
954 * Not enough memory was available for allocation.
955 * \retval #PSA_ERROR_ALREADY_EXISTS
956 * Trying to allocate a buffer to a non-empty key slot.
957 */
958static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
959 size_t buffer_length )
960{
961 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200962 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200963
964 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
965 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200966 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200967
968 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200969 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200970}
971
Gilles Peskine8e338702019-07-30 20:06:31 +0200972/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100973 * previously. This function assumes that the slot does not contain
974 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100975psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
976 const uint8_t *data,
977 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100978{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200979 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200981 /* zero-length keys are never supported. */
982 if( data_length == 0 )
983 return( PSA_ERROR_NOT_SUPPORTED );
984
Gilles Peskine8e338702019-07-30 20:06:31 +0200985 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200987 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200988
Steven Cooreman75b74362020-07-28 14:30:13 +0200989 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
990 if( data_length > SIZE_MAX / 8 )
991 return( PSA_ERROR_NOT_SUPPORTED );
992
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200993 /* Enforce a size limit, and in particular ensure that the bit
994 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200995 if( bit_size > PSA_MAX_KEY_BITS )
996 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200997
998 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200999 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001000 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001001
1002 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02001003 status = psa_allocate_buffer_to_slot( slot, data_length );
1004 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001005 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001006
1007 /* copy key into allocated buffer */
Steven Cooreman29149862020-08-05 15:43:42 +02001008 memcpy( slot->data.key.data, data, data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001009
1010 /* Write the actual key size to the slot.
1011 * psa_start_key_creation() wrote the size declared by the
1012 * caller, which may be 0 (meaning unspecified) or wrong. */
1013 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001015 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1016 {
Gilles Peskinef76aa772018-10-29 19:24:33 +01001017#if defined(MBEDTLS_ECP_C)
Steven Cooreman19fd5742020-07-24 23:31:01 +02001018 status = psa_import_ecp_key( slot,
1019 data, data_length );
1020#else
1021 /* No drivers have been implemented yet, so without mbed TLS backing
1022 * there's no way to do ECP with the current library. */
1023 return( PSA_ERROR_NOT_SUPPORTED );
1024#endif /* defined(MBEDTLS_ECP_C) */
1025 }
1026 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +01001027 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001028#if defined(MBEDTLS_RSA_C)
1029 status = psa_import_rsa_key( slot,
1030 data, data_length );
1031#else
1032 /* No drivers have been implemented yet, so without mbed TLS backing
1033 * there's no way to do RSA with the current library. */
1034 status = PSA_ERROR_NOT_SUPPORTED;
1035#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036 }
1037 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001038 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001039 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001041 }
Darryl Green940d72c2018-07-13 13:18:51 +01001042
Darryl Green06fd18d2018-07-16 11:21:11 +01001043 return( status );
1044}
1045
Gilles Peskinef603c712019-01-19 13:40:11 +01001046/** Calculate the intersection of two algorithm usage policies.
1047 *
1048 * Return 0 (which allows no operation) on incompatibility.
1049 */
1050static psa_algorithm_t psa_key_policy_algorithm_intersection(
1051 psa_algorithm_t alg1,
1052 psa_algorithm_t alg2 )
1053{
Gilles Peskine549ea862019-05-22 11:45:59 +02001054 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001055 if( alg1 == alg2 )
1056 return( alg1 );
1057 /* If the policies are from the same hash-and-sign family, check
1058 * if one is a wildcard. If so the other has the specific algorithm. */
1059 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1060 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1061 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1062 {
1063 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1064 return( alg2 );
1065 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1066 return( alg1 );
1067 }
1068 /* If the policies are incompatible, allow nothing. */
1069 return( 0 );
1070}
1071
Gilles Peskined6f371b2019-05-10 19:33:38 +02001072static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1073 psa_algorithm_t requested_alg )
1074{
Gilles Peskine549ea862019-05-22 11:45:59 +02001075 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001076 if( requested_alg == policy_alg )
1077 return( 1 );
1078 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001079 * and requested_alg is the same hash-and-sign family with any hash,
1080 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001081 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1082 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1083 {
1084 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1085 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1086 }
1087 /* If it isn't permitted, it's forbidden. */
1088 return( 0 );
1089}
1090
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001091/** Test whether a policy permits an algorithm.
1092 *
1093 * The caller must test usage flags separately.
1094 */
1095static int psa_key_policy_permits( const psa_key_policy_t *policy,
1096 psa_algorithm_t alg )
1097{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001098 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1099 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001100}
1101
Gilles Peskinef603c712019-01-19 13:40:11 +01001102/** Restrict a key policy based on a constraint.
1103 *
1104 * \param[in,out] policy The policy to restrict.
1105 * \param[in] constraint The policy constraint to apply.
1106 *
1107 * \retval #PSA_SUCCESS
1108 * \c *policy contains the intersection of the original value of
1109 * \c *policy and \c *constraint.
1110 * \retval #PSA_ERROR_INVALID_ARGUMENT
1111 * \c *policy and \c *constraint are incompatible.
1112 * \c *policy is unchanged.
1113 */
1114static psa_status_t psa_restrict_key_policy(
1115 psa_key_policy_t *policy,
1116 const psa_key_policy_t *constraint )
1117{
1118 psa_algorithm_t intersection_alg =
1119 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001120 psa_algorithm_t intersection_alg2 =
1121 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001122 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1123 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001124 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1125 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001126 policy->usage &= constraint->usage;
1127 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001128 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001129 return( PSA_SUCCESS );
1130}
1131
Darryl Green06fd18d2018-07-16 11:21:11 +01001132/** Retrieve a slot which must contain a key. The key must have allow all the
1133 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1134 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001135static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001136 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001137 psa_key_usage_t usage,
1138 psa_algorithm_t alg )
1139{
1140 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001141 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001142
1143 *p_slot = NULL;
1144
Gilles Peskinec5487a82018-12-03 18:08:14 +01001145 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001146 if( status != PSA_SUCCESS )
1147 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001148
1149 /* Enforce that usage policy for the key slot contains all the flags
1150 * required by the usage parameter. There is one exception: public
1151 * keys can always be exported, so we treat public key objects as
1152 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001153 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001154 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001155 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001156 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001157
1158 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001159 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001160 return( PSA_ERROR_NOT_PERMITTED );
1161
1162 *p_slot = slot;
1163 return( PSA_SUCCESS );
1164}
Darryl Green940d72c2018-07-13 13:18:51 +01001165
Gilles Peskine28f8f302019-07-24 13:30:31 +02001166/** Retrieve a slot which must contain a transparent key.
1167 *
1168 * A transparent key is a key for which the key material is directly
1169 * available, as opposed to a key in a secure element.
1170 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001171 * This is a temporary function to use instead of psa_get_key_from_slot()
1172 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001173 */
1174#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1175static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1176 psa_key_slot_t **p_slot,
1177 psa_key_usage_t usage,
1178 psa_algorithm_t alg )
1179{
1180 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1181 if( status != PSA_SUCCESS )
1182 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001183 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001184 {
1185 *p_slot = NULL;
1186 return( PSA_ERROR_NOT_SUPPORTED );
1187 }
1188 return( PSA_SUCCESS );
1189}
1190#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1191/* With no secure element support, all keys are transparent. */
1192#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1193 psa_get_key_from_slot( handle, p_slot, usage, alg )
1194#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1195
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001196/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001197static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001198{
Gilles Peskine73167e12019-07-12 23:44:37 +02001199#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1200 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001201 {
1202 /* No key material to clean. */
1203 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001204 else
1205#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001206 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001207 /* Data pointer will always be either a valid pointer or NULL in an
1208 * initialized slot, so we can just free it. */
1209 if( slot->data.key.data != NULL )
1210 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001211 mbedtls_free( slot->data.key.data );
1212 slot->data.key.data = NULL;
1213 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001214 }
Darryl Green40225ba2018-11-15 14:48:15 +00001215
1216 return( PSA_SUCCESS );
1217}
1218
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001219/** Completely wipe a slot in memory, including its policy.
1220 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001221psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001222{
1223 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001224 /* Multipart operations may still be using the key. This is safe
1225 * because all multipart operation objects are independent from
1226 * the key slot: if they need to access the key after the setup
1227 * phase, they have a copy of the key. Note that this means that
1228 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001229 /* At this point, key material and other type-specific content has
1230 * been wiped. Clear remaining metadata. We can call memset and not
1231 * zeroize because the metadata is not particularly sensitive. */
1232 memset( slot, 0, sizeof( *slot ) );
1233 return( status );
1234}
1235
Gilles Peskinec5487a82018-12-03 18:08:14 +01001236psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001237{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001238 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001239 psa_status_t status; /* status of the last operation */
1240 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001241#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1242 psa_se_drv_table_entry_t *driver;
1243#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001244
Gilles Peskine1841cf42019-10-08 15:48:25 +02001245 if( handle == 0 )
1246 return( PSA_SUCCESS );
1247
Gilles Peskinec5487a82018-12-03 18:08:14 +01001248 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001249 if( status != PSA_SUCCESS )
1250 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001251
1252#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001253 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001254 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001255 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001256 /* For a key in a secure element, we need to do three things:
1257 * remove the key file in internal storage, destroy the
1258 * key inside the secure element, and update the driver's
1259 * persistent data. Start a transaction that will encompass these
1260 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001261 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001262 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001263 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001264 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001265 status = psa_crypto_save_transaction( );
1266 if( status != PSA_SUCCESS )
1267 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001268 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001269 /* We should still try to destroy the key in the secure
1270 * element and the key metadata in storage. This is especially
1271 * important if the error is that the storage is full.
1272 * But how to do it exactly without risking an inconsistent
1273 * state after a reset?
1274 * https://github.com/ARMmbed/mbed-crypto/issues/215
1275 */
1276 overall_status = status;
1277 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001278 }
1279
Gilles Peskine354f7672019-07-12 23:46:38 +02001280 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001281 if( overall_status == PSA_SUCCESS )
1282 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001283 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001284#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1285
Darryl Greend49a4992018-06-18 17:27:26 +01001286#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001287 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001288 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001289 status = psa_destroy_persistent_key( slot->attr.id );
1290 if( overall_status == PSA_SUCCESS )
1291 overall_status = status;
1292
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001293 /* TODO: other slots may have a copy of the same key. We should
1294 * invalidate them.
1295 * https://github.com/ARMmbed/mbed-crypto/issues/214
1296 */
Darryl Greend49a4992018-06-18 17:27:26 +01001297 }
1298#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001299
Gilles Peskinefc762652019-07-22 19:30:34 +02001300#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1301 if( driver != NULL )
1302 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001303 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001304 if( overall_status == PSA_SUCCESS )
1305 overall_status = status;
1306 status = psa_crypto_stop_transaction( );
1307 if( overall_status == PSA_SUCCESS )
1308 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001309 }
1310#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1311
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001312#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1313exit:
1314#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001315 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001316 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1317 if( overall_status == PSA_SUCCESS )
1318 overall_status = status;
1319 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001320}
1321
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001322void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001323{
Gilles Peskineb699f072019-04-26 16:06:02 +02001324 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001325 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001326}
1327
Gilles Peskineb699f072019-04-26 16:06:02 +02001328psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1329 psa_key_type_t type,
1330 const uint8_t *data,
1331 size_t data_length )
1332{
1333 uint8_t *copy = NULL;
1334
1335 if( data_length != 0 )
1336 {
1337 copy = mbedtls_calloc( 1, data_length );
1338 if( copy == NULL )
1339 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1340 memcpy( copy, data, data_length );
1341 }
1342 /* After this point, this function is guaranteed to succeed, so it
1343 * can start modifying `*attributes`. */
1344
1345 if( attributes->domain_parameters != NULL )
1346 {
1347 mbedtls_free( attributes->domain_parameters );
1348 attributes->domain_parameters = NULL;
1349 attributes->domain_parameters_size = 0;
1350 }
1351
1352 attributes->domain_parameters = copy;
1353 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001354 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001355 return( PSA_SUCCESS );
1356}
1357
1358psa_status_t psa_get_key_domain_parameters(
1359 const psa_key_attributes_t *attributes,
1360 uint8_t *data, size_t data_size, size_t *data_length )
1361{
1362 if( attributes->domain_parameters_size > data_size )
1363 return( PSA_ERROR_BUFFER_TOO_SMALL );
1364 *data_length = attributes->domain_parameters_size;
1365 if( attributes->domain_parameters_size != 0 )
1366 memcpy( data, attributes->domain_parameters,
1367 attributes->domain_parameters_size );
1368 return( PSA_SUCCESS );
1369}
1370
1371#if defined(MBEDTLS_RSA_C)
1372static psa_status_t psa_get_rsa_public_exponent(
1373 const mbedtls_rsa_context *rsa,
1374 psa_key_attributes_t *attributes )
1375{
1376 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001378 uint8_t *buffer = NULL;
1379 size_t buflen;
1380 mbedtls_mpi_init( &mpi );
1381
1382 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1383 if( ret != 0 )
1384 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001385 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1386 {
1387 /* It's the default value, which is reported as an empty string,
1388 * so there's nothing to do. */
1389 goto exit;
1390 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001391
1392 buflen = mbedtls_mpi_size( &mpi );
1393 buffer = mbedtls_calloc( 1, buflen );
1394 if( buffer == NULL )
1395 {
1396 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1397 goto exit;
1398 }
1399 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1400 if( ret != 0 )
1401 goto exit;
1402 attributes->domain_parameters = buffer;
1403 attributes->domain_parameters_size = buflen;
1404
1405exit:
1406 mbedtls_mpi_free( &mpi );
1407 if( ret != 0 )
1408 mbedtls_free( buffer );
1409 return( mbedtls_to_psa_error( ret ) );
1410}
1411#endif /* MBEDTLS_RSA_C */
1412
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001413/** Retrieve all the publicly-accessible attributes of a key.
1414 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001415psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1416 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001417{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001418 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001419 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001420
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001421 psa_reset_key_attributes( attributes );
1422
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001423 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001424 if( status != PSA_SUCCESS )
1425 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001426
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001427 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001428 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1429 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1430
1431#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1432 if( psa_key_slot_is_external( slot ) )
1433 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1434#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001435
Gilles Peskine8e338702019-07-30 20:06:31 +02001436 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001437 {
1438#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001439 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001440 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001441#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001442 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001443 * is not yet implemented.
1444 * https://github.com/ARMmbed/mbed-crypto/issues/216
1445 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001446 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001447 break;
1448#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001449 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001450 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001451
Steven Cooreman4fed4552020-08-03 14:46:03 +02001452 status = psa_load_rsa_representation( slot->attr.type,
1453 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001454 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001455 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001456 if( status != PSA_SUCCESS )
1457 break;
1458
Steven Cooremana2371e52020-07-28 14:30:39 +02001459 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001460 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001461 mbedtls_rsa_free( rsa );
1462 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001463 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001464 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001465#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001466 default:
1467 /* Nothing else to do. */
1468 break;
1469 }
1470
1471 if( status != PSA_SUCCESS )
1472 psa_reset_key_attributes( attributes );
1473 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001474}
1475
Gilles Peskinec8000c02019-08-02 20:15:51 +02001476#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1477psa_status_t psa_get_key_slot_number(
1478 const psa_key_attributes_t *attributes,
1479 psa_key_slot_number_t *slot_number )
1480{
1481 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1482 {
1483 *slot_number = attributes->slot_number;
1484 return( PSA_SUCCESS );
1485 }
1486 else
1487 return( PSA_ERROR_INVALID_ARGUMENT );
1488}
1489#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1490
Steven Cooremana01795d2020-07-24 22:48:15 +02001491static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1492 uint8_t *data,
1493 size_t data_size,
1494 size_t *data_length )
1495{
1496 if( slot->data.key.bytes > data_size )
1497 return( PSA_ERROR_BUFFER_TOO_SMALL );
1498 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1499 memset( data + slot->data.key.bytes, 0,
1500 data_size - slot->data.key.bytes );
1501 *data_length = slot->data.key.bytes;
1502 return( PSA_SUCCESS );
1503}
1504
Gilles Peskinef603c712019-01-19 13:40:11 +01001505static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1506 uint8_t *data,
1507 size_t data_size,
1508 size_t *data_length,
1509 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001510{
Gilles Peskine5d309672019-07-12 23:47:28 +02001511#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1512 const psa_drv_se_t *drv;
1513 psa_drv_se_context_t *drv_context;
1514#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1515
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001516 *data_length = 0;
1517
Gilles Peskine8e338702019-07-30 20:06:31 +02001518 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001519 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001520
Gilles Peskinef9168942019-09-12 19:20:29 +02001521 /* Reject a zero-length output buffer now, since this can never be a
1522 * valid key representation. This way we know that data must be a valid
1523 * pointer and we can do things like memset(data, ..., data_size). */
1524 if( data_size == 0 )
1525 return( PSA_ERROR_BUFFER_TOO_SMALL );
1526
Gilles Peskine5d309672019-07-12 23:47:28 +02001527#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001528 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001529 {
1530 psa_drv_se_export_key_t method;
1531 if( drv->key_management == NULL )
1532 return( PSA_ERROR_NOT_SUPPORTED );
1533 method = ( export_public_key ?
1534 drv->key_management->p_export_public :
1535 drv->key_management->p_export );
1536 if( method == NULL )
1537 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001538 return( method( drv_context,
1539 slot->data.se.slot_number,
1540 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001541 }
1542#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1543
Gilles Peskine8e338702019-07-30 20:06:31 +02001544 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001545 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001546 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001547 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001548 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1549 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001550 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001551 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001552 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001553 /* Exporting public -> public */
1554 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1555 }
1556 else if( !export_public_key )
1557 {
1558 /* Exporting private -> private */
1559 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1560 }
1561 /* Need to export the public part of a private key,
1562 * so conversion is needed */
1563 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1564 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001565#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001566 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001567 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001568 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001569 slot->data.key.data,
1570 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001571 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001572 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001573 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001574
Steven Cooreman560c28a2020-07-24 23:20:24 +02001575 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001576 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001577 data,
1578 data_size,
1579 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001580
Steven Cooremana2371e52020-07-28 14:30:39 +02001581 mbedtls_rsa_free( rsa );
1582 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001583
Steven Cooreman560c28a2020-07-24 23:20:24 +02001584 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001585#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001586 /* We don't know how to convert a private RSA key to public. */
1587 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001588#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001589 }
1590 else
Moran Pekera998bc62018-04-16 18:16:20 +03001591 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001592#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001593 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001594 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001595 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001596 slot->data.key.data,
1597 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001598 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001599 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001600 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001601
1602 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1603 PSA_KEY_TYPE_ECC_GET_FAMILY(
1604 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001605 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001606 data,
1607 data_size,
1608 data_length );
1609
Steven Cooremana2371e52020-07-28 14:30:39 +02001610 mbedtls_ecp_keypair_free( ecp );
1611 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001612 return( status );
1613#else
1614 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001615 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001616#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001617 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001618 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001619 else
1620 {
1621 /* This shouldn't happen in the reference implementation, but
1622 it is valid for a special-purpose implementation to omit
1623 support for exporting certain key types. */
1624 return( PSA_ERROR_NOT_SUPPORTED );
1625 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001626}
1627
Gilles Peskinec5487a82018-12-03 18:08:14 +01001628psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001629 uint8_t *data,
1630 size_t data_size,
1631 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001632{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001633 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001634 psa_status_t status;
1635
1636 /* Set the key to empty now, so that even when there are errors, we always
1637 * set data_length to a value between 0 and data_size. On error, setting
1638 * the key to empty is a good choice because an empty key representation is
1639 * unlikely to be accepted anywhere. */
1640 *data_length = 0;
1641
1642 /* Export requires the EXPORT flag. There is an exception for public keys,
1643 * which don't require any flag, but psa_get_key_from_slot takes
1644 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001645 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001646 if( status != PSA_SUCCESS )
1647 return( status );
1648 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001649 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001650}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001651
Gilles Peskinec5487a82018-12-03 18:08:14 +01001652psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001653 uint8_t *data,
1654 size_t data_size,
1655 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001656{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001657 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001658 psa_status_t status;
1659
1660 /* Set the key to empty now, so that even when there are errors, we always
1661 * set data_length to a value between 0 and data_size. On error, setting
1662 * the key to empty is a good choice because an empty key representation is
1663 * unlikely to be accepted anywhere. */
1664 *data_length = 0;
1665
1666 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001667 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001668 if( status != PSA_SUCCESS )
1669 return( status );
1670 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001671 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001672}
1673
Gilles Peskine91e8c332019-08-02 19:19:39 +02001674#if defined(static_assert)
1675static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1676 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001677static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001678 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001679static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001680 "One or more key attribute flag is listed as both internal-only and external-only" );
1681#endif
1682
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001683/** Validate that a key policy is internally well-formed.
1684 *
1685 * This function only rejects invalid policies. It does not validate the
1686 * consistency of the policy with respect to other attributes of the key
1687 * such as the key type.
1688 */
1689static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001690{
1691 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001692 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001693 PSA_KEY_USAGE_ENCRYPT |
1694 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001695 PSA_KEY_USAGE_SIGN_HASH |
1696 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001697 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1698 return( PSA_ERROR_INVALID_ARGUMENT );
1699
Gilles Peskine4747d192019-04-17 15:05:45 +02001700 return( PSA_SUCCESS );
1701}
1702
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001703/** Validate the internal consistency of key attributes.
1704 *
1705 * This function only rejects invalid attribute values. If does not
1706 * validate the consistency of the attributes with any key data that may
1707 * be involved in the creation of the key.
1708 *
1709 * Call this function early in the key creation process.
1710 *
1711 * \param[in] attributes Key attributes for the new key.
1712 * \param[out] p_drv On any return, the driver for the key, if any.
1713 * NULL for a transparent key.
1714 *
1715 */
1716static psa_status_t psa_validate_key_attributes(
1717 const psa_key_attributes_t *attributes,
1718 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001719{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001720 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001721
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001722 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1723 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001724 if( status != PSA_SUCCESS )
1725 return( status );
1726
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001727 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1728 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001729 if( status != PSA_SUCCESS )
1730 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001731
1732 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001733 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001734 return( status );
1735
1736 /* Refuse to create overly large keys.
1737 * Note that this doesn't trigger on import if the attributes don't
1738 * explicitly specify a size (so psa_get_key_bits returns 0), so
1739 * psa_import_key() needs its own checks. */
1740 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1741 return( PSA_ERROR_NOT_SUPPORTED );
1742
Gilles Peskine91e8c332019-08-02 19:19:39 +02001743 /* Reject invalid flags. These should not be reachable through the API. */
1744 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1745 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1746 return( PSA_ERROR_INVALID_ARGUMENT );
1747
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001748 return( PSA_SUCCESS );
1749}
1750
Gilles Peskine4747d192019-04-17 15:05:45 +02001751/** Prepare a key slot to receive key material.
1752 *
1753 * This function allocates a key slot and sets its metadata.
1754 *
1755 * If this function fails, call psa_fail_key_creation().
1756 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001757 * This function is intended to be used as follows:
1758 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1759 * it with the specified attributes, and assign it a handle.
1760 * -# Populate the slot with the key material.
1761 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1762 * In case of failure at any step, stop the sequence and call
1763 * psa_fail_key_creation().
1764 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001765 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001766 * \param[in] attributes Key attributes for the new key.
1767 * \param[out] handle On success, a handle for the allocated slot.
1768 * \param[out] p_slot On success, a pointer to the prepared slot.
1769 * \param[out] p_drv On any return, the driver for the key, if any.
1770 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001771 *
1772 * \retval #PSA_SUCCESS
1773 * The key slot is ready to receive key material.
1774 * \return If this function fails, the key slot is an invalid state.
1775 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001776 */
1777static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001778 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001779 const psa_key_attributes_t *attributes,
1780 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001781 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001782 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001783{
1784 psa_status_t status;
1785 psa_key_slot_t *slot;
1786
Gilles Peskinedf179142019-07-15 22:02:14 +02001787 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001788 *p_drv = NULL;
1789
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001790 status = psa_validate_key_attributes( attributes, p_drv );
1791 if( status != PSA_SUCCESS )
1792 return( status );
1793
Gilles Peskineedbed562019-08-07 18:19:59 +02001794 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001795 if( status != PSA_SUCCESS )
1796 return( status );
1797 slot = *p_slot;
1798
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001799 /* We're storing the declared bit-size of the key. It's up to each
1800 * creation mechanism to verify that this information is correct.
1801 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001802 * an input (generate, device) but not for those where the bit-size
1803 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001804
1805 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001806
Gilles Peskine91e8c332019-08-02 19:19:39 +02001807 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001808 * external-only flags, query `attributes`. Thanks to the check
1809 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001810 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001811 * may have set. */
1812 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001813
Gilles Peskinecbaff462019-07-12 23:46:04 +02001814#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001815 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001816 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001817 * create the key file in internal storage, create the
1818 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001819 * persistent data. This is done by starting a transaction that will
1820 * encompass these three actions.
1821 * For registering a volatile key, we just need to find an appropriate
1822 * slot number inside the SE. Since the key is designated volatile, creating
1823 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001824 /* The first thing to do is to find a slot number for the new key.
1825 * We save the slot number in persistent storage as part of the
1826 * transaction data. It will be needed to recover if the power
1827 * fails during the key creation process, to clean up on the secure
1828 * element side after restarting. Obtaining a slot number from the
1829 * secure element driver updates its persistent state, but we do not yet
1830 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001831 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001832 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001833 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001834 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001835 &slot->data.se.slot_number );
1836 if( status != PSA_SUCCESS )
1837 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001838
1839 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001840 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001841 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1842 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1843 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1844 psa_crypto_transaction.key.id = slot->attr.id;
1845 status = psa_crypto_save_transaction( );
1846 if( status != PSA_SUCCESS )
1847 {
1848 (void) psa_crypto_stop_transaction( );
1849 return( status );
1850 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001851 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001852 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001853
1854 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1855 {
1856 /* Key registration only makes sense with a secure element. */
1857 return( PSA_ERROR_INVALID_ARGUMENT );
1858 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001859#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1860
Darryl Green0c6575a2018-11-07 16:05:30 +00001861 return( status );
1862}
Gilles Peskine4747d192019-04-17 15:05:45 +02001863
1864/** Finalize the creation of a key once its key material has been set.
1865 *
1866 * This entails writing the key to persistent storage.
1867 *
1868 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001869 * See the documentation of psa_start_key_creation() for the intended use
1870 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001871 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001872 * \param[in,out] slot Pointer to the slot with key material.
1873 * \param[in] driver The secure element driver for the key,
1874 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001875 *
1876 * \retval #PSA_SUCCESS
1877 * The key was successfully created. The handle is now valid.
1878 * \return If this function fails, the key slot is an invalid state.
1879 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001880 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001881static psa_status_t psa_finish_key_creation(
1882 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001883 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001884{
1885 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001886 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001887 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001888
1889#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001890 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001891 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001892#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1893 if( driver != NULL )
1894 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001895 psa_se_key_data_storage_t data;
1896#if defined(static_assert)
1897 static_assert( sizeof( slot->data.se.slot_number ) ==
1898 sizeof( data.slot_number ),
1899 "Slot number size does not match psa_se_key_data_storage_t" );
1900 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1901 "Bit-size size does not match psa_se_key_data_storage_t" );
1902#endif
1903 memcpy( &data.slot_number, &slot->data.se.slot_number,
1904 sizeof( slot->data.se.slot_number ) );
1905 memcpy( &data.bits, &slot->attr.bits,
1906 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001907 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001908 (uint8_t*) &data,
1909 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001910 }
1911 else
1912#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1913 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001914 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001915 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001916 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001917 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1918 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001919 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001920 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1921 status = psa_internal_export_key( slot,
1922 buffer, buffer_size, &length,
1923 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001924 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001925 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001926 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001927
Gilles Peskinef9168942019-09-12 19:20:29 +02001928 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001929 mbedtls_free( buffer );
1930 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001931 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001932#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1933
Gilles Peskinecbaff462019-07-12 23:46:04 +02001934#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001935 /* Finish the transaction for a key creation. This does not
1936 * happen when registering an existing key. Detect this case
1937 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001938 * creation of a persistent key in a secure element requires a transaction,
1939 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001940 if( driver != NULL &&
1941 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001942 {
1943 status = psa_save_se_persistent_data( driver );
1944 if( status != PSA_SUCCESS )
1945 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001946 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001947 return( status );
1948 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001949 status = psa_crypto_stop_transaction( );
1950 if( status != PSA_SUCCESS )
1951 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001952 }
1953#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1954
Gilles Peskine4747d192019-04-17 15:05:45 +02001955 return( status );
1956}
1957
1958/** Abort the creation of a key.
1959 *
1960 * You may call this function after calling psa_start_key_creation(),
1961 * or after psa_finish_key_creation() fails. In other circumstances, this
1962 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001963 * See the documentation of psa_start_key_creation() for the intended use
1964 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001965 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001966 * \param[in,out] slot Pointer to the slot with key material.
1967 * \param[in] driver The secure element driver for the key,
1968 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001969 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001970static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001971 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001972{
Gilles Peskine011e4282019-06-26 18:34:38 +02001973 (void) driver;
1974
Gilles Peskine4747d192019-04-17 15:05:45 +02001975 if( slot == NULL )
1976 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001977
1978#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001979 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001980 * element, and the failure happened later (when saving metadata
1981 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001982 * element.
1983 * https://github.com/ARMmbed/mbed-crypto/issues/217
1984 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001985
Gilles Peskined7729582019-08-05 15:55:54 +02001986 /* Abort the ongoing transaction if any (there may not be one if
1987 * the creation process failed before starting one, or if the
1988 * key creation is a registration of a key in a secure element).
1989 * Earlier functions must already have done what it takes to undo any
1990 * partial creation. All that's left is to update the transaction data
1991 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001992 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001993#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1994
Gilles Peskine4747d192019-04-17 15:05:45 +02001995 psa_wipe_key_slot( slot );
1996}
1997
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001998/** Validate optional attributes during key creation.
1999 *
2000 * Some key attributes are optional during key creation. If they are
2001 * specified in the attributes structure, check that they are consistent
2002 * with the data in the slot.
2003 *
2004 * This function should be called near the end of key creation, after
2005 * the slot in memory is fully populated but before saving persistent data.
2006 */
2007static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002008 const psa_key_slot_t *slot,
2009 const psa_key_attributes_t *attributes )
2010{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002011 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002012 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002013 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002014 return( PSA_ERROR_INVALID_ARGUMENT );
2015 }
2016
2017 if( attributes->domain_parameters_size != 0 )
2018 {
2019#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002020 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002021 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002022 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002023 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002024 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002025
Steven Cooreman7f391872020-07-30 14:57:44 +02002026 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002027 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002028 slot->data.key.data,
2029 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002030 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002031 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002032 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002033
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002034 mbedtls_mpi_init( &actual );
2035 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002036 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002037 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002038 mbedtls_rsa_free( rsa );
2039 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002040 if( ret != 0 )
2041 goto rsa_exit;
2042 ret = mbedtls_mpi_read_binary( &required,
2043 attributes->domain_parameters,
2044 attributes->domain_parameters_size );
2045 if( ret != 0 )
2046 goto rsa_exit;
2047 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2048 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2049 rsa_exit:
2050 mbedtls_mpi_free( &actual );
2051 mbedtls_mpi_free( &required );
2052 if( ret != 0)
2053 return( mbedtls_to_psa_error( ret ) );
2054 }
2055 else
2056#endif
2057 {
2058 return( PSA_ERROR_INVALID_ARGUMENT );
2059 }
2060 }
2061
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002062 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002063 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002064 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002065 return( PSA_ERROR_INVALID_ARGUMENT );
2066 }
2067
2068 return( PSA_SUCCESS );
2069}
2070
Gilles Peskine4747d192019-04-17 15:05:45 +02002071psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002072 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002073 size_t data_length,
2074 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002075{
2076 psa_status_t status;
2077 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002078 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002079
Gilles Peskine0f84d622019-09-12 19:03:13 +02002080 /* Reject zero-length symmetric keys (including raw data key objects).
2081 * This also rejects any key which might be encoded as an empty string,
2082 * which is never valid. */
2083 if( data_length == 0 )
2084 return( PSA_ERROR_INVALID_ARGUMENT );
2085
Gilles Peskinedf179142019-07-15 22:02:14 +02002086 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2087 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002088 if( status != PSA_SUCCESS )
2089 goto exit;
2090
Gilles Peskine5d309672019-07-12 23:47:28 +02002091#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2092 if( driver != NULL )
2093 {
2094 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002095 /* The driver should set the number of key bits, however in
2096 * case it doesn't, we initialize bits to an invalid value. */
2097 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002098 if( drv->key_management == NULL ||
2099 drv->key_management->p_import == NULL )
2100 {
2101 status = PSA_ERROR_NOT_SUPPORTED;
2102 goto exit;
2103 }
2104 status = drv->key_management->p_import(
2105 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002106 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002107 &bits );
2108 if( status != PSA_SUCCESS )
2109 goto exit;
2110 if( bits > PSA_MAX_KEY_BITS )
2111 {
2112 status = PSA_ERROR_NOT_SUPPORTED;
2113 goto exit;
2114 }
2115 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002116 }
2117 else
2118#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2119 {
2120 status = psa_import_key_into_slot( slot, data, data_length );
2121 if( status != PSA_SUCCESS )
2122 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002123 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002124 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002125 if( status != PSA_SUCCESS )
2126 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002127
Gilles Peskine011e4282019-06-26 18:34:38 +02002128 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002129exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002130 if( status != PSA_SUCCESS )
2131 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002132 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002133 *handle = 0;
2134 }
2135 return( status );
2136}
2137
Gilles Peskined7729582019-08-05 15:55:54 +02002138#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2139psa_status_t mbedtls_psa_register_se_key(
2140 const psa_key_attributes_t *attributes )
2141{
2142 psa_status_t status;
2143 psa_key_slot_t *slot = NULL;
2144 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002145 psa_key_handle_t handle = 0;
2146
2147 /* Leaving attributes unspecified is not currently supported.
2148 * It could make sense to query the key type and size from the
2149 * secure element, but not all secure elements support this
2150 * and the driver HAL doesn't currently support it. */
2151 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2152 return( PSA_ERROR_NOT_SUPPORTED );
2153 if( psa_get_key_bits( attributes ) == 0 )
2154 return( PSA_ERROR_NOT_SUPPORTED );
2155
2156 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2157 &handle, &slot, &driver );
2158 if( status != PSA_SUCCESS )
2159 goto exit;
2160
Gilles Peskined7729582019-08-05 15:55:54 +02002161 status = psa_finish_key_creation( slot, driver );
2162
2163exit:
2164 if( status != PSA_SUCCESS )
2165 {
2166 psa_fail_key_creation( slot, driver );
2167 }
2168 /* Registration doesn't keep the key in RAM. */
2169 psa_close_key( handle );
2170 return( status );
2171}
2172#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2173
Gilles Peskinef603c712019-01-19 13:40:11 +01002174static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002175 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002176{
2177 psa_status_t status;
2178 uint8_t *buffer = NULL;
2179 size_t buffer_size = 0;
2180 size_t length;
2181
Gilles Peskine8e338702019-07-30 20:06:31 +02002182 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002183 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002184 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002185 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002186 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002187 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2188 if( status != PSA_SUCCESS )
2189 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002190 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002191 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002192
2193exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002194 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002195 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002196 return( status );
2197}
2198
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002199psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2200 const psa_key_attributes_t *specified_attributes,
2201 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002202{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002203 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002204 psa_key_slot_t *source_slot = NULL;
2205 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002206 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002207 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002208
Gilles Peskine28f8f302019-07-24 13:30:31 +02002209 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002210 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002211 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002212 goto exit;
2213
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002214 status = psa_validate_optional_attributes( source_slot,
2215 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002216 if( status != PSA_SUCCESS )
2217 goto exit;
2218
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002219 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002220 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002221 if( status != PSA_SUCCESS )
2222 goto exit;
2223
Gilles Peskinedf179142019-07-15 22:02:14 +02002224 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2225 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002226 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002227 if( status != PSA_SUCCESS )
2228 goto exit;
2229
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002230#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2231 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002232 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002233 /* Copying to a secure element is not implemented yet. */
2234 status = PSA_ERROR_NOT_SUPPORTED;
2235 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002236 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002237#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002238
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002239 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002240 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002241 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002242
Gilles Peskine011e4282019-06-26 18:34:38 +02002243 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002244exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002245 if( status != PSA_SUCCESS )
2246 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002247 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002248 *target_handle = 0;
2249 }
2250 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002251}
2252
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002253
2254
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002255/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002256/* Message digests */
2257/****************************************************************/
2258
Gilles Peskineb16841e2019-10-10 20:36:12 +02002259#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002260static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002261{
2262 switch( alg )
2263 {
2264#if defined(MBEDTLS_MD2_C)
2265 case PSA_ALG_MD2:
2266 return( &mbedtls_md2_info );
2267#endif
2268#if defined(MBEDTLS_MD4_C)
2269 case PSA_ALG_MD4:
2270 return( &mbedtls_md4_info );
2271#endif
2272#if defined(MBEDTLS_MD5_C)
2273 case PSA_ALG_MD5:
2274 return( &mbedtls_md5_info );
2275#endif
2276#if defined(MBEDTLS_RIPEMD160_C)
2277 case PSA_ALG_RIPEMD160:
2278 return( &mbedtls_ripemd160_info );
2279#endif
2280#if defined(MBEDTLS_SHA1_C)
2281 case PSA_ALG_SHA_1:
2282 return( &mbedtls_sha1_info );
2283#endif
2284#if defined(MBEDTLS_SHA256_C)
2285 case PSA_ALG_SHA_224:
2286 return( &mbedtls_sha224_info );
2287 case PSA_ALG_SHA_256:
2288 return( &mbedtls_sha256_info );
2289#endif
2290#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002291#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002292 case PSA_ALG_SHA_384:
2293 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002294#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002295 case PSA_ALG_SHA_512:
2296 return( &mbedtls_sha512_info );
2297#endif
2298 default:
2299 return( NULL );
2300 }
2301}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002302#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002303
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002304psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2305{
2306 switch( operation->alg )
2307 {
Gilles Peskine81736312018-06-26 15:04:31 +02002308 case 0:
2309 /* The object has (apparently) been initialized but it is not
2310 * in use. It's ok to call abort on such an object, and there's
2311 * nothing to do. */
2312 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002313#if defined(MBEDTLS_MD2_C)
2314 case PSA_ALG_MD2:
2315 mbedtls_md2_free( &operation->ctx.md2 );
2316 break;
2317#endif
2318#if defined(MBEDTLS_MD4_C)
2319 case PSA_ALG_MD4:
2320 mbedtls_md4_free( &operation->ctx.md4 );
2321 break;
2322#endif
2323#if defined(MBEDTLS_MD5_C)
2324 case PSA_ALG_MD5:
2325 mbedtls_md5_free( &operation->ctx.md5 );
2326 break;
2327#endif
2328#if defined(MBEDTLS_RIPEMD160_C)
2329 case PSA_ALG_RIPEMD160:
2330 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2331 break;
2332#endif
2333#if defined(MBEDTLS_SHA1_C)
2334 case PSA_ALG_SHA_1:
2335 mbedtls_sha1_free( &operation->ctx.sha1 );
2336 break;
2337#endif
2338#if defined(MBEDTLS_SHA256_C)
2339 case PSA_ALG_SHA_224:
2340 case PSA_ALG_SHA_256:
2341 mbedtls_sha256_free( &operation->ctx.sha256 );
2342 break;
2343#endif
2344#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002345#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002347#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002348 case PSA_ALG_SHA_512:
2349 mbedtls_sha512_free( &operation->ctx.sha512 );
2350 break;
2351#endif
2352 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002353 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002354 }
2355 operation->alg = 0;
2356 return( PSA_SUCCESS );
2357}
2358
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002359psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002360 psa_algorithm_t alg )
2361{
Janos Follath24eed8d2019-11-22 13:21:35 +00002362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002363
2364 /* A context must be freshly initialized before it can be set up. */
2365 if( operation->alg != 0 )
2366 {
2367 return( PSA_ERROR_BAD_STATE );
2368 }
2369
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002370 switch( alg )
2371 {
2372#if defined(MBEDTLS_MD2_C)
2373 case PSA_ALG_MD2:
2374 mbedtls_md2_init( &operation->ctx.md2 );
2375 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2376 break;
2377#endif
2378#if defined(MBEDTLS_MD4_C)
2379 case PSA_ALG_MD4:
2380 mbedtls_md4_init( &operation->ctx.md4 );
2381 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2382 break;
2383#endif
2384#if defined(MBEDTLS_MD5_C)
2385 case PSA_ALG_MD5:
2386 mbedtls_md5_init( &operation->ctx.md5 );
2387 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2388 break;
2389#endif
2390#if defined(MBEDTLS_RIPEMD160_C)
2391 case PSA_ALG_RIPEMD160:
2392 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2393 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2394 break;
2395#endif
2396#if defined(MBEDTLS_SHA1_C)
2397 case PSA_ALG_SHA_1:
2398 mbedtls_sha1_init( &operation->ctx.sha1 );
2399 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2400 break;
2401#endif
2402#if defined(MBEDTLS_SHA256_C)
2403 case PSA_ALG_SHA_224:
2404 mbedtls_sha256_init( &operation->ctx.sha256 );
2405 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2406 break;
2407 case PSA_ALG_SHA_256:
2408 mbedtls_sha256_init( &operation->ctx.sha256 );
2409 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2410 break;
2411#endif
2412#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002413#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002414 case PSA_ALG_SHA_384:
2415 mbedtls_sha512_init( &operation->ctx.sha512 );
2416 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2417 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002418#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002419 case PSA_ALG_SHA_512:
2420 mbedtls_sha512_init( &operation->ctx.sha512 );
2421 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2422 break;
2423#endif
2424 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002425 return( PSA_ALG_IS_HASH( alg ) ?
2426 PSA_ERROR_NOT_SUPPORTED :
2427 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002428 }
2429 if( ret == 0 )
2430 operation->alg = alg;
2431 else
2432 psa_hash_abort( operation );
2433 return( mbedtls_to_psa_error( ret ) );
2434}
2435
2436psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2437 const uint8_t *input,
2438 size_t input_length )
2439{
Janos Follath24eed8d2019-11-22 13:21:35 +00002440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002441
2442 /* Don't require hash implementations to behave correctly on a
2443 * zero-length input, which may have an invalid pointer. */
2444 if( input_length == 0 )
2445 return( PSA_SUCCESS );
2446
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002447 switch( operation->alg )
2448 {
2449#if defined(MBEDTLS_MD2_C)
2450 case PSA_ALG_MD2:
2451 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2452 input, input_length );
2453 break;
2454#endif
2455#if defined(MBEDTLS_MD4_C)
2456 case PSA_ALG_MD4:
2457 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2458 input, input_length );
2459 break;
2460#endif
2461#if defined(MBEDTLS_MD5_C)
2462 case PSA_ALG_MD5:
2463 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2464 input, input_length );
2465 break;
2466#endif
2467#if defined(MBEDTLS_RIPEMD160_C)
2468 case PSA_ALG_RIPEMD160:
2469 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2470 input, input_length );
2471 break;
2472#endif
2473#if defined(MBEDTLS_SHA1_C)
2474 case PSA_ALG_SHA_1:
2475 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2476 input, input_length );
2477 break;
2478#endif
2479#if defined(MBEDTLS_SHA256_C)
2480 case PSA_ALG_SHA_224:
2481 case PSA_ALG_SHA_256:
2482 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2483 input, input_length );
2484 break;
2485#endif
2486#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002487#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002489#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002490 case PSA_ALG_SHA_512:
2491 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2492 input, input_length );
2493 break;
2494#endif
2495 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002496 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002497 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002498
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002499 if( ret != 0 )
2500 psa_hash_abort( operation );
2501 return( mbedtls_to_psa_error( ret ) );
2502}
2503
2504psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2505 uint8_t *hash,
2506 size_t hash_size,
2507 size_t *hash_length )
2508{
itayzafrir40835d42018-08-02 13:14:17 +03002509 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002511 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002512
2513 /* Fill the output buffer with something that isn't a valid hash
2514 * (barring an attack on the hash and deliberately-crafted input),
2515 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002516 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002517 /* If hash_size is 0 then hash may be NULL and then the
2518 * call to memset would have undefined behavior. */
2519 if( hash_size != 0 )
2520 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002521
2522 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002523 {
2524 status = PSA_ERROR_BUFFER_TOO_SMALL;
2525 goto exit;
2526 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002527
2528 switch( operation->alg )
2529 {
2530#if defined(MBEDTLS_MD2_C)
2531 case PSA_ALG_MD2:
2532 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2533 break;
2534#endif
2535#if defined(MBEDTLS_MD4_C)
2536 case PSA_ALG_MD4:
2537 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2538 break;
2539#endif
2540#if defined(MBEDTLS_MD5_C)
2541 case PSA_ALG_MD5:
2542 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2543 break;
2544#endif
2545#if defined(MBEDTLS_RIPEMD160_C)
2546 case PSA_ALG_RIPEMD160:
2547 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2548 break;
2549#endif
2550#if defined(MBEDTLS_SHA1_C)
2551 case PSA_ALG_SHA_1:
2552 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2553 break;
2554#endif
2555#if defined(MBEDTLS_SHA256_C)
2556 case PSA_ALG_SHA_224:
2557 case PSA_ALG_SHA_256:
2558 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2559 break;
2560#endif
2561#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002562#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002563 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002564#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002565 case PSA_ALG_SHA_512:
2566 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2567 break;
2568#endif
2569 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002570 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002571 }
itayzafrir40835d42018-08-02 13:14:17 +03002572 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002573
itayzafrir40835d42018-08-02 13:14:17 +03002574exit:
2575 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002576 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002577 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002578 return( psa_hash_abort( operation ) );
2579 }
2580 else
2581 {
2582 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002583 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002584 }
2585}
2586
Gilles Peskine2d277862018-06-18 15:41:12 +02002587psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2588 const uint8_t *hash,
2589 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002590{
2591 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2592 size_t actual_hash_length;
2593 psa_status_t status = psa_hash_finish( operation,
2594 actual_hash, sizeof( actual_hash ),
2595 &actual_hash_length );
2596 if( status != PSA_SUCCESS )
2597 return( status );
2598 if( actual_hash_length != hash_length )
2599 return( PSA_ERROR_INVALID_SIGNATURE );
2600 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2601 return( PSA_ERROR_INVALID_SIGNATURE );
2602 return( PSA_SUCCESS );
2603}
2604
Gilles Peskine0a749c82019-11-28 19:33:58 +01002605psa_status_t psa_hash_compute( psa_algorithm_t alg,
2606 const uint8_t *input, size_t input_length,
2607 uint8_t *hash, size_t hash_size,
2608 size_t *hash_length )
2609{
2610 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2611 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2612
2613 *hash_length = hash_size;
2614 status = psa_hash_setup( &operation, alg );
2615 if( status != PSA_SUCCESS )
2616 goto exit;
2617 status = psa_hash_update( &operation, input, input_length );
2618 if( status != PSA_SUCCESS )
2619 goto exit;
2620 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2621 if( status != PSA_SUCCESS )
2622 goto exit;
2623
2624exit:
2625 if( status == PSA_SUCCESS )
2626 status = psa_hash_abort( &operation );
2627 else
2628 psa_hash_abort( &operation );
2629 return( status );
2630}
2631
2632psa_status_t psa_hash_compare( psa_algorithm_t alg,
2633 const uint8_t *input, size_t input_length,
2634 const uint8_t *hash, size_t hash_length )
2635{
2636 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2637 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2638
2639 status = psa_hash_setup( &operation, alg );
2640 if( status != PSA_SUCCESS )
2641 goto exit;
2642 status = psa_hash_update( &operation, input, input_length );
2643 if( status != PSA_SUCCESS )
2644 goto exit;
2645 status = psa_hash_verify( &operation, hash, hash_length );
2646 if( status != PSA_SUCCESS )
2647 goto exit;
2648
2649exit:
2650 if( status == PSA_SUCCESS )
2651 status = psa_hash_abort( &operation );
2652 else
2653 psa_hash_abort( &operation );
2654 return( status );
2655}
2656
Gilles Peskineeb35d782019-01-22 17:56:16 +01002657psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2658 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002659{
2660 if( target_operation->alg != 0 )
2661 return( PSA_ERROR_BAD_STATE );
2662
2663 switch( source_operation->alg )
2664 {
2665 case 0:
2666 return( PSA_ERROR_BAD_STATE );
2667#if defined(MBEDTLS_MD2_C)
2668 case PSA_ALG_MD2:
2669 mbedtls_md2_clone( &target_operation->ctx.md2,
2670 &source_operation->ctx.md2 );
2671 break;
2672#endif
2673#if defined(MBEDTLS_MD4_C)
2674 case PSA_ALG_MD4:
2675 mbedtls_md4_clone( &target_operation->ctx.md4,
2676 &source_operation->ctx.md4 );
2677 break;
2678#endif
2679#if defined(MBEDTLS_MD5_C)
2680 case PSA_ALG_MD5:
2681 mbedtls_md5_clone( &target_operation->ctx.md5,
2682 &source_operation->ctx.md5 );
2683 break;
2684#endif
2685#if defined(MBEDTLS_RIPEMD160_C)
2686 case PSA_ALG_RIPEMD160:
2687 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2688 &source_operation->ctx.ripemd160 );
2689 break;
2690#endif
2691#if defined(MBEDTLS_SHA1_C)
2692 case PSA_ALG_SHA_1:
2693 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2694 &source_operation->ctx.sha1 );
2695 break;
2696#endif
2697#if defined(MBEDTLS_SHA256_C)
2698 case PSA_ALG_SHA_224:
2699 case PSA_ALG_SHA_256:
2700 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2701 &source_operation->ctx.sha256 );
2702 break;
2703#endif
2704#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002705#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002706 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002707#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002708 case PSA_ALG_SHA_512:
2709 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2710 &source_operation->ctx.sha512 );
2711 break;
2712#endif
2713 default:
2714 return( PSA_ERROR_NOT_SUPPORTED );
2715 }
2716
2717 target_operation->alg = source_operation->alg;
2718 return( PSA_SUCCESS );
2719}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002720
2721
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002722/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002723/* MAC */
2724/****************************************************************/
2725
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002726static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727 psa_algorithm_t alg,
2728 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002729 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002730 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002731{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002732 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002733 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002734
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002735 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002736 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002737
Gilles Peskine8c9def32018-02-08 10:02:12 +01002738 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2739 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002740 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002742 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002743 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002744 mode = MBEDTLS_MODE_STREAM;
2745 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746 case PSA_ALG_CTR:
2747 mode = MBEDTLS_MODE_CTR;
2748 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002749 case PSA_ALG_CFB:
2750 mode = MBEDTLS_MODE_CFB;
2751 break;
2752 case PSA_ALG_OFB:
2753 mode = MBEDTLS_MODE_OFB;
2754 break;
2755 case PSA_ALG_CBC_NO_PADDING:
2756 mode = MBEDTLS_MODE_CBC;
2757 break;
2758 case PSA_ALG_CBC_PKCS7:
2759 mode = MBEDTLS_MODE_CBC;
2760 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002761 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002762 mode = MBEDTLS_MODE_CCM;
2763 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002764 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002765 mode = MBEDTLS_MODE_GCM;
2766 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002767 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2768 mode = MBEDTLS_MODE_CHACHAPOLY;
2769 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002770 default:
2771 return( NULL );
2772 }
2773 }
2774 else if( alg == PSA_ALG_CMAC )
2775 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002776 else
2777 return( NULL );
2778
2779 switch( key_type )
2780 {
2781 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002782 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002783 break;
2784 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002785 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2786 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002787 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002788 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002789 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002790 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002791 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2792 * but two-key Triple-DES is functionally three-key Triple-DES
2793 * with K1=K3, so that's how we present it to mbedtls. */
2794 if( key_bits == 128 )
2795 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002796 break;
2797 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002798 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002799 break;
2800 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002801 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002802 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002803 case PSA_KEY_TYPE_CHACHA20:
2804 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2805 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002806 default:
2807 return( NULL );
2808 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002809 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002810 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002811
Jaeden Amero23bbb752018-06-26 14:16:54 +01002812 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2813 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002814}
2815
Gilles Peskinea05219c2018-11-16 16:02:56 +01002816#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002817static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002818{
Gilles Peskine2d277862018-06-18 15:41:12 +02002819 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002820 {
2821 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002822 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002823 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002824 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002825 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002826 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002827 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002828 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002829 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002830 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002831 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002832 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002833 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002834 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002835 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002836 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002837 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002838 return( 128 );
2839 default:
2840 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002841 }
2842}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002843#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002844
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002845/* Initialize the MAC operation structure. Once this function has been
2846 * called, psa_mac_abort can run and will do the right thing. */
2847static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2848 psa_algorithm_t alg )
2849{
2850 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2851
2852 operation->alg = alg;
2853 operation->key_set = 0;
2854 operation->iv_set = 0;
2855 operation->iv_required = 0;
2856 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002857 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002858
2859#if defined(MBEDTLS_CMAC_C)
2860 if( alg == PSA_ALG_CMAC )
2861 {
2862 operation->iv_required = 0;
2863 mbedtls_cipher_init( &operation->ctx.cmac );
2864 status = PSA_SUCCESS;
2865 }
2866 else
2867#endif /* MBEDTLS_CMAC_C */
2868#if defined(MBEDTLS_MD_C)
2869 if( PSA_ALG_IS_HMAC( operation->alg ) )
2870 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002871 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2872 operation->ctx.hmac.hash_ctx.alg = 0;
2873 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002874 }
2875 else
2876#endif /* MBEDTLS_MD_C */
2877 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002878 if( ! PSA_ALG_IS_MAC( alg ) )
2879 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002880 }
2881
2882 if( status != PSA_SUCCESS )
2883 memset( operation, 0, sizeof( *operation ) );
2884 return( status );
2885}
2886
Gilles Peskine01126fa2018-07-12 17:04:55 +02002887#if defined(MBEDTLS_MD_C)
2888static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2889{
Gilles Peskine3f108122018-12-07 18:14:53 +01002890 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002891 return( psa_hash_abort( &hmac->hash_ctx ) );
2892}
2893#endif /* MBEDTLS_MD_C */
2894
Gilles Peskine8c9def32018-02-08 10:02:12 +01002895psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2896{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002897 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002898 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002899 /* The object has (apparently) been initialized but it is not
2900 * in use. It's ok to call abort on such an object, and there's
2901 * nothing to do. */
2902 return( PSA_SUCCESS );
2903 }
2904 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002905#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002906 if( operation->alg == PSA_ALG_CMAC )
2907 {
2908 mbedtls_cipher_free( &operation->ctx.cmac );
2909 }
2910 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002911#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002912#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002913 if( PSA_ALG_IS_HMAC( operation->alg ) )
2914 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002915 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002916 }
2917 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002918#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002919 {
2920 /* Sanity check (shouldn't happen: operation->alg should
2921 * always have been initialized to a valid value). */
2922 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002923 }
Moran Peker41deec42018-04-04 15:43:05 +03002924
Gilles Peskine8c9def32018-02-08 10:02:12 +01002925 operation->alg = 0;
2926 operation->key_set = 0;
2927 operation->iv_set = 0;
2928 operation->iv_required = 0;
2929 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002930 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002931
Gilles Peskine8c9def32018-02-08 10:02:12 +01002932 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002933
2934bad_state:
2935 /* If abort is called on an uninitialized object, we can't trust
2936 * anything. Wipe the object in case it contains confidential data.
2937 * This may result in a memory leak if a pointer gets overwritten,
2938 * but it's too late to do anything about this. */
2939 memset( operation, 0, sizeof( *operation ) );
2940 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002941}
2942
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002943#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002944static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002945 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002946 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002947 const mbedtls_cipher_info_t *cipher_info )
2948{
Janos Follath24eed8d2019-11-22 13:21:35 +00002949 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002950
2951 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002952
2953 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2954 if( ret != 0 )
2955 return( ret );
2956
2957 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002958 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002959 key_bits );
2960 return( ret );
2961}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002962#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002963
Gilles Peskine248051a2018-06-20 16:09:38 +02002964#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002965static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2966 const uint8_t *key,
2967 size_t key_length,
2968 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002969{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002970 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002971 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002972 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002973 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002974 psa_status_t status;
2975
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002976 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2977 * overflow below. This should never trigger if the hash algorithm
2978 * is implemented correctly. */
2979 /* The size checks against the ipad and opad buffers cannot be written
2980 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2981 * because that triggers -Wlogical-op on GCC 7.3. */
2982 if( block_size > sizeof( ipad ) )
2983 return( PSA_ERROR_NOT_SUPPORTED );
2984 if( block_size > sizeof( hmac->opad ) )
2985 return( PSA_ERROR_NOT_SUPPORTED );
2986 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002987 return( PSA_ERROR_NOT_SUPPORTED );
2988
Gilles Peskined223b522018-06-11 18:12:58 +02002989 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002990 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002991 status = psa_hash_compute( hash_alg, key, key_length,
2992 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002993 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002994 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002995 }
Gilles Peskine96889972018-07-12 17:07:03 +02002996 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2997 * but it is permitted. It is common when HMAC is used in HKDF, for
2998 * example. Don't call `memcpy` in the 0-length because `key` could be
2999 * an invalid pointer which would make the behavior undefined. */
3000 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003001 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003002
Gilles Peskined223b522018-06-11 18:12:58 +02003003 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3004 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003005 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003006 ipad[i] ^= 0x36;
3007 memset( ipad + key_length, 0x36, block_size - key_length );
3008
3009 /* Copy the key material from ipad to opad, flipping the requisite bits,
3010 * and filling the rest of opad with the requisite constant. */
3011 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003012 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3013 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003014
Gilles Peskine01126fa2018-07-12 17:04:55 +02003015 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003016 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003017 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003018
Gilles Peskine01126fa2018-07-12 17:04:55 +02003019 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003020
3021cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003022 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003023
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003024 return( status );
3025}
Gilles Peskine248051a2018-06-20 16:09:38 +02003026#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003027
Gilles Peskine89167cb2018-07-08 20:12:23 +02003028static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003029 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003030 psa_algorithm_t alg,
3031 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003032{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003033 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003034 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003035 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003036 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003037 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003038 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003039 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003040
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003041 /* A context must be freshly initialized before it can be set up. */
3042 if( operation->alg != 0 )
3043 {
3044 return( PSA_ERROR_BAD_STATE );
3045 }
3046
Gilles Peskined911eb72018-08-14 15:18:45 +02003047 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003048 if( status != PSA_SUCCESS )
3049 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003050 if( is_sign )
3051 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003052
Gilles Peskine28f8f302019-07-24 13:30:31 +02003053 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003054 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003055 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003056 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003057
Gilles Peskine8c9def32018-02-08 10:02:12 +01003058#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003059 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003060 {
3061 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003062 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003063 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003064 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003065 if( cipher_info == NULL )
3066 {
3067 status = PSA_ERROR_NOT_SUPPORTED;
3068 goto exit;
3069 }
3070 operation->mac_size = cipher_info->block_size;
3071 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3072 status = mbedtls_to_psa_error( ret );
3073 }
3074 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003075#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003076#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003077 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003078 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003079 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003080 if( hash_alg == 0 )
3081 {
3082 status = PSA_ERROR_NOT_SUPPORTED;
3083 goto exit;
3084 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003085
3086 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3087 /* Sanity check. This shouldn't fail on a valid configuration. */
3088 if( operation->mac_size == 0 ||
3089 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3090 {
3091 status = PSA_ERROR_NOT_SUPPORTED;
3092 goto exit;
3093 }
3094
Gilles Peskine8e338702019-07-30 20:06:31 +02003095 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003096 {
3097 status = PSA_ERROR_INVALID_ARGUMENT;
3098 goto exit;
3099 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003100
Gilles Peskine01126fa2018-07-12 17:04:55 +02003101 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003102 slot->data.key.data,
3103 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003104 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003105 }
3106 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003107#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003108 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003109 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003110 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003111 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003112
Gilles Peskined911eb72018-08-14 15:18:45 +02003113 if( truncated == 0 )
3114 {
3115 /* The "normal" case: untruncated algorithm. Nothing to do. */
3116 }
3117 else if( truncated < 4 )
3118 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003119 /* A very short MAC is too short for security since it can be
3120 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3121 * so we make this our minimum, even though 32 bits is still
3122 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003123 status = PSA_ERROR_NOT_SUPPORTED;
3124 }
3125 else if( truncated > operation->mac_size )
3126 {
3127 /* It's impossible to "truncate" to a larger length. */
3128 status = PSA_ERROR_INVALID_ARGUMENT;
3129 }
3130 else
3131 operation->mac_size = truncated;
3132
Gilles Peskinefbfac682018-07-08 20:51:54 +02003133exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003134 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003135 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003136 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003137 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003138 else
3139 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003140 operation->key_set = 1;
3141 }
3142 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003143}
3144
Gilles Peskine89167cb2018-07-08 20:12:23 +02003145psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003146 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003147 psa_algorithm_t alg )
3148{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003149 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003150}
3151
3152psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003153 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003154 psa_algorithm_t alg )
3155{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003156 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003157}
3158
Gilles Peskine8c9def32018-02-08 10:02:12 +01003159psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3160 const uint8_t *input,
3161 size_t input_length )
3162{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003163 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003164 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003165 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003166 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003167 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003168 operation->has_input = 1;
3169
Gilles Peskine8c9def32018-02-08 10:02:12 +01003170#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003171 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003172 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003173 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3174 input, input_length );
3175 status = mbedtls_to_psa_error( ret );
3176 }
3177 else
3178#endif /* MBEDTLS_CMAC_C */
3179#if defined(MBEDTLS_MD_C)
3180 if( PSA_ALG_IS_HMAC( operation->alg ) )
3181 {
3182 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3183 input_length );
3184 }
3185 else
3186#endif /* MBEDTLS_MD_C */
3187 {
3188 /* This shouldn't happen if `operation` was initialized by
3189 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003190 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003191 }
3192
Gilles Peskinefbfac682018-07-08 20:51:54 +02003193 if( status != PSA_SUCCESS )
3194 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003195 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003196}
3197
Gilles Peskine01126fa2018-07-12 17:04:55 +02003198#if defined(MBEDTLS_MD_C)
3199static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3200 uint8_t *mac,
3201 size_t mac_size )
3202{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003203 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003204 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3205 size_t hash_size = 0;
3206 size_t block_size = psa_get_hash_block_size( hash_alg );
3207 psa_status_t status;
3208
Gilles Peskine01126fa2018-07-12 17:04:55 +02003209 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3210 if( status != PSA_SUCCESS )
3211 return( status );
3212 /* From here on, tmp needs to be wiped. */
3213
3214 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3215 if( status != PSA_SUCCESS )
3216 goto exit;
3217
3218 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3219 if( status != PSA_SUCCESS )
3220 goto exit;
3221
3222 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3223 if( status != PSA_SUCCESS )
3224 goto exit;
3225
Gilles Peskined911eb72018-08-14 15:18:45 +02003226 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3227 if( status != PSA_SUCCESS )
3228 goto exit;
3229
3230 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003231
3232exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003233 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003234 return( status );
3235}
3236#endif /* MBEDTLS_MD_C */
3237
mohammad16036df908f2018-04-02 08:34:15 -07003238static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003239 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003240 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003241{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003242 if( ! operation->key_set )
3243 return( PSA_ERROR_BAD_STATE );
3244 if( operation->iv_required && ! operation->iv_set )
3245 return( PSA_ERROR_BAD_STATE );
3246
Gilles Peskine8c9def32018-02-08 10:02:12 +01003247 if( mac_size < operation->mac_size )
3248 return( PSA_ERROR_BUFFER_TOO_SMALL );
3249
Gilles Peskine8c9def32018-02-08 10:02:12 +01003250#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003251 if( operation->alg == PSA_ALG_CMAC )
3252 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003253 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3254 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3255 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003256 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003257 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003258 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003259 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003260 else
3261#endif /* MBEDTLS_CMAC_C */
3262#if defined(MBEDTLS_MD_C)
3263 if( PSA_ALG_IS_HMAC( operation->alg ) )
3264 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003265 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003266 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003267 }
3268 else
3269#endif /* MBEDTLS_MD_C */
3270 {
3271 /* This shouldn't happen if `operation` was initialized by
3272 * a setup function. */
3273 return( PSA_ERROR_BAD_STATE );
3274 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003275}
3276
Gilles Peskineacd4be32018-07-08 19:56:25 +02003277psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3278 uint8_t *mac,
3279 size_t mac_size,
3280 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003281{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003282 psa_status_t status;
3283
Jaeden Amero252ef282019-02-15 14:05:35 +00003284 if( operation->alg == 0 )
3285 {
3286 return( PSA_ERROR_BAD_STATE );
3287 }
3288
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003289 /* Fill the output buffer with something that isn't a valid mac
3290 * (barring an attack on the mac and deliberately-crafted input),
3291 * in case the caller doesn't check the return status properly. */
3292 *mac_length = mac_size;
3293 /* If mac_size is 0 then mac may be NULL and then the
3294 * call to memset would have undefined behavior. */
3295 if( mac_size != 0 )
3296 memset( mac, '!', mac_size );
3297
Gilles Peskine89167cb2018-07-08 20:12:23 +02003298 if( ! operation->is_sign )
3299 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003300 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003301 }
mohammad16036df908f2018-04-02 08:34:15 -07003302
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003303 status = psa_mac_finish_internal( operation, mac, mac_size );
3304
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003305 if( status == PSA_SUCCESS )
3306 {
3307 status = psa_mac_abort( operation );
3308 if( status == PSA_SUCCESS )
3309 *mac_length = operation->mac_size;
3310 else
3311 memset( mac, '!', mac_size );
3312 }
3313 else
3314 psa_mac_abort( operation );
3315 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003316}
3317
Gilles Peskineacd4be32018-07-08 19:56:25 +02003318psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3319 const uint8_t *mac,
3320 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003321{
Gilles Peskine828ed142018-06-18 23:25:51 +02003322 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003323 psa_status_t status;
3324
Jaeden Amero252ef282019-02-15 14:05:35 +00003325 if( operation->alg == 0 )
3326 {
3327 return( PSA_ERROR_BAD_STATE );
3328 }
3329
Gilles Peskine89167cb2018-07-08 20:12:23 +02003330 if( operation->is_sign )
3331 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003332 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003333 }
3334 if( operation->mac_size != mac_length )
3335 {
3336 status = PSA_ERROR_INVALID_SIGNATURE;
3337 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003338 }
mohammad16036df908f2018-04-02 08:34:15 -07003339
3340 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003341 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003342 if( status != PSA_SUCCESS )
3343 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003344
3345 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3346 status = PSA_ERROR_INVALID_SIGNATURE;
3347
3348cleanup:
3349 if( status == PSA_SUCCESS )
3350 status = psa_mac_abort( operation );
3351 else
3352 psa_mac_abort( operation );
3353
Gilles Peskine3f108122018-12-07 18:14:53 +01003354 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003355
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003356 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003357}
3358
3359
Gilles Peskine20035e32018-02-03 22:44:14 +01003360
Gilles Peskine20035e32018-02-03 22:44:14 +01003361/****************************************************************/
3362/* Asymmetric cryptography */
3363/****************************************************************/
3364
Gilles Peskine2b450e32018-06-27 15:42:46 +02003365#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003366/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003367 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003368static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3369 size_t hash_length,
3370 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003371{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003372 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003373 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003374 *md_alg = mbedtls_md_get_type( md_info );
3375
3376 /* The Mbed TLS RSA module uses an unsigned int for hash length
3377 * parameters. Validate that it fits so that we don't risk an
3378 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003379#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003380 if( hash_length > UINT_MAX )
3381 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003382#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003383
3384#if defined(MBEDTLS_PKCS1_V15)
3385 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3386 * must be correct. */
3387 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3388 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003389 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003390 if( md_info == NULL )
3391 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003392 if( mbedtls_md_get_size( md_info ) != hash_length )
3393 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003394 }
3395#endif /* MBEDTLS_PKCS1_V15 */
3396
3397#if defined(MBEDTLS_PKCS1_V21)
3398 /* PSS requires a hash internally. */
3399 if( PSA_ALG_IS_RSA_PSS( alg ) )
3400 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003401 if( md_info == NULL )
3402 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003403 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003404#endif /* MBEDTLS_PKCS1_V21 */
3405
Gilles Peskine61b91d42018-06-08 16:09:36 +02003406 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003407}
3408
Gilles Peskine2b450e32018-06-27 15:42:46 +02003409static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3410 psa_algorithm_t alg,
3411 const uint8_t *hash,
3412 size_t hash_length,
3413 uint8_t *signature,
3414 size_t signature_size,
3415 size_t *signature_length )
3416{
3417 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003418 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003419 mbedtls_md_type_t md_alg;
3420
3421 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3422 if( status != PSA_SUCCESS )
3423 return( status );
3424
Gilles Peskine630a18a2018-06-29 17:49:35 +02003425 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003426 return( PSA_ERROR_BUFFER_TOO_SMALL );
3427
3428#if defined(MBEDTLS_PKCS1_V15)
3429 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3430 {
3431 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3432 MBEDTLS_MD_NONE );
3433 ret = mbedtls_rsa_pkcs1_sign( rsa,
3434 mbedtls_ctr_drbg_random,
3435 &global_data.ctr_drbg,
3436 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003437 md_alg,
3438 (unsigned int) hash_length,
3439 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003440 signature );
3441 }
3442 else
3443#endif /* MBEDTLS_PKCS1_V15 */
3444#if defined(MBEDTLS_PKCS1_V21)
3445 if( PSA_ALG_IS_RSA_PSS( alg ) )
3446 {
3447 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3448 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3449 mbedtls_ctr_drbg_random,
3450 &global_data.ctr_drbg,
3451 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003452 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003453 (unsigned int) hash_length,
3454 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003455 signature );
3456 }
3457 else
3458#endif /* MBEDTLS_PKCS1_V21 */
3459 {
3460 return( PSA_ERROR_INVALID_ARGUMENT );
3461 }
3462
3463 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003464 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003465 return( mbedtls_to_psa_error( ret ) );
3466}
3467
3468static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3469 psa_algorithm_t alg,
3470 const uint8_t *hash,
3471 size_t hash_length,
3472 const uint8_t *signature,
3473 size_t signature_length )
3474{
3475 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003476 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003477 mbedtls_md_type_t md_alg;
3478
3479 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3480 if( status != PSA_SUCCESS )
3481 return( status );
3482
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003483 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3484 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003485
3486#if defined(MBEDTLS_PKCS1_V15)
3487 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3488 {
3489 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3490 MBEDTLS_MD_NONE );
3491 ret = mbedtls_rsa_pkcs1_verify( rsa,
3492 mbedtls_ctr_drbg_random,
3493 &global_data.ctr_drbg,
3494 MBEDTLS_RSA_PUBLIC,
3495 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003496 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003497 hash,
3498 signature );
3499 }
3500 else
3501#endif /* MBEDTLS_PKCS1_V15 */
3502#if defined(MBEDTLS_PKCS1_V21)
3503 if( PSA_ALG_IS_RSA_PSS( alg ) )
3504 {
3505 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3506 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3507 mbedtls_ctr_drbg_random,
3508 &global_data.ctr_drbg,
3509 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003510 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003511 (unsigned int) hash_length,
3512 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003513 signature );
3514 }
3515 else
3516#endif /* MBEDTLS_PKCS1_V21 */
3517 {
3518 return( PSA_ERROR_INVALID_ARGUMENT );
3519 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003520
3521 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3522 * the rest of the signature is invalid". This has little use in
3523 * practice and PSA doesn't report this distinction. */
3524 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3525 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003526 return( mbedtls_to_psa_error( ret ) );
3527}
3528#endif /* MBEDTLS_RSA_C */
3529
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003530#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003531/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3532 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3533 * (even though these functions don't modify it). */
3534static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3535 psa_algorithm_t alg,
3536 const uint8_t *hash,
3537 size_t hash_length,
3538 uint8_t *signature,
3539 size_t signature_size,
3540 size_t *signature_length )
3541{
Janos Follath24eed8d2019-11-22 13:21:35 +00003542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003543 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003544 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003545 mbedtls_mpi_init( &r );
3546 mbedtls_mpi_init( &s );
3547
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003548 if( signature_size < 2 * curve_bytes )
3549 {
3550 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3551 goto cleanup;
3552 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003553
Gilles Peskinea05219c2018-11-16 16:02:56 +01003554#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003555 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3556 {
3557 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3558 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3559 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003560 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3561 &ecp->d, hash,
3562 hash_length, md_alg,
3563 mbedtls_ctr_drbg_random,
3564 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003565 }
3566 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003567#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003568 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003569 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003570 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3571 hash, hash_length,
3572 mbedtls_ctr_drbg_random,
3573 &global_data.ctr_drbg ) );
3574 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003575
3576 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3577 signature,
3578 curve_bytes ) );
3579 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3580 signature + curve_bytes,
3581 curve_bytes ) );
3582
3583cleanup:
3584 mbedtls_mpi_free( &r );
3585 mbedtls_mpi_free( &s );
3586 if( ret == 0 )
3587 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003588 return( mbedtls_to_psa_error( ret ) );
3589}
3590
3591static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3592 const uint8_t *hash,
3593 size_t hash_length,
3594 const uint8_t *signature,
3595 size_t signature_length )
3596{
Janos Follath24eed8d2019-11-22 13:21:35 +00003597 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003598 mbedtls_mpi r, s;
3599 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3600 mbedtls_mpi_init( &r );
3601 mbedtls_mpi_init( &s );
3602
3603 if( signature_length != 2 * curve_bytes )
3604 return( PSA_ERROR_INVALID_SIGNATURE );
3605
3606 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3607 signature,
3608 curve_bytes ) );
3609 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3610 signature + curve_bytes,
3611 curve_bytes ) );
3612
Steven Cooremanacda8342020-07-24 23:09:52 +02003613 /* Check whether the public part is loaded. If not, load it. */
3614 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3615 {
3616 MBEDTLS_MPI_CHK(
3617 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3618 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3619 }
3620
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003621 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3622 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003623
3624cleanup:
3625 mbedtls_mpi_free( &r );
3626 mbedtls_mpi_free( &s );
3627 return( mbedtls_to_psa_error( ret ) );
3628}
3629#endif /* MBEDTLS_ECDSA_C */
3630
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003631psa_status_t psa_sign_hash( psa_key_handle_t handle,
3632 psa_algorithm_t alg,
3633 const uint8_t *hash,
3634 size_t hash_length,
3635 uint8_t *signature,
3636 size_t signature_size,
3637 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003638{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003639 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003640 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003641#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3642 const psa_drv_se_t *drv;
3643 psa_drv_se_context_t *drv_context;
3644#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003645
3646 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003647 /* Immediately reject a zero-length signature buffer. This guarantees
3648 * that signature must be a valid pointer. (On the other hand, the hash
3649 * buffer can in principle be empty since it doesn't actually have
3650 * to be a hash.) */
3651 if( signature_size == 0 )
3652 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003653
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003654 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003655 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003656 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003657 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003658 {
3659 status = PSA_ERROR_INVALID_ARGUMENT;
3660 goto exit;
3661 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003662
Steven Cooremancd84cb42020-07-16 20:28:36 +02003663 /* Try any of the available accelerators first */
3664 status = psa_driver_wrapper_sign_hash( slot,
3665 alg,
3666 hash,
3667 hash_length,
3668 signature,
3669 signature_size,
3670 signature_length );
3671 if( status != PSA_ERROR_NOT_SUPPORTED )
3672 goto exit;
3673
Gilles Peskineedc64242019-08-07 21:05:07 +02003674#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3675 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3676 {
3677 if( drv->asymmetric == NULL ||
3678 drv->asymmetric->p_sign == NULL )
3679 {
3680 status = PSA_ERROR_NOT_SUPPORTED;
3681 goto exit;
3682 }
3683 status = drv->asymmetric->p_sign( drv_context,
3684 slot->data.se.slot_number,
3685 alg,
3686 hash, hash_length,
3687 signature, signature_size,
3688 signature_length );
3689 }
3690 else
3691#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003692#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003693 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003694 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003695 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003696
Steven Cooreman4fed4552020-08-03 14:46:03 +02003697 status = psa_load_rsa_representation( slot->attr.type,
3698 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003699 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003700 &rsa );
3701 if( status != PSA_SUCCESS )
3702 goto exit;
3703
Steven Cooremana2371e52020-07-28 14:30:39 +02003704 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003705 alg,
3706 hash, hash_length,
3707 signature, signature_size,
3708 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003709
Steven Cooremana2371e52020-07-28 14:30:39 +02003710 mbedtls_rsa_free( rsa );
3711 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003712 }
3713 else
3714#endif /* defined(MBEDTLS_RSA_C) */
3715#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003716 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003717 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003718#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003719 if(
3720#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3721 PSA_ALG_IS_ECDSA( alg )
3722#else
3723 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3724#endif
3725 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003726 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003727 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003728 status = psa_load_ecp_representation( slot->attr.type,
3729 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003730 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003731 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003732 if( status != PSA_SUCCESS )
3733 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003734 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003735 alg,
3736 hash, hash_length,
3737 signature, signature_size,
3738 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003739 mbedtls_ecp_keypair_free( ecp );
3740 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003741 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003742 else
3743#endif /* defined(MBEDTLS_ECDSA_C) */
3744 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003745 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003746 }
itayzafrir5c753392018-05-08 11:18:38 +03003747 }
3748 else
3749#endif /* defined(MBEDTLS_ECP_C) */
3750 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003751 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003752 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003753
3754exit:
3755 /* Fill the unused part of the output buffer (the whole buffer on error,
3756 * the trailing part on success) with something that isn't a valid mac
3757 * (barring an attack on the mac and deliberately-crafted input),
3758 * in case the caller doesn't check the return status properly. */
3759 if( status == PSA_SUCCESS )
3760 memset( signature + *signature_length, '!',
3761 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003762 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003763 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003764 /* If signature_size is 0 then we have nothing to do. We must not call
3765 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003766 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003767}
3768
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003769psa_status_t psa_verify_hash( psa_key_handle_t handle,
3770 psa_algorithm_t alg,
3771 const uint8_t *hash,
3772 size_t hash_length,
3773 const uint8_t *signature,
3774 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003775{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003776 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003777 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003778#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3779 const psa_drv_se_t *drv;
3780 psa_drv_se_context_t *drv_context;
3781#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003782
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003783 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003784 if( status != PSA_SUCCESS )
3785 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003786
Gilles Peskineedc64242019-08-07 21:05:07 +02003787#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3788 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3789 {
3790 if( drv->asymmetric == NULL ||
3791 drv->asymmetric->p_verify == NULL )
3792 return( PSA_ERROR_NOT_SUPPORTED );
3793 return( drv->asymmetric->p_verify( drv_context,
3794 slot->data.se.slot_number,
3795 alg,
3796 hash, hash_length,
3797 signature, signature_length ) );
3798 }
3799 else
3800#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003801#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003802 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003803 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003804 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003805
Steven Cooreman4fed4552020-08-03 14:46:03 +02003806 status = psa_load_rsa_representation( slot->attr.type,
3807 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003808 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003809 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003810 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003811 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02003812
Steven Cooremana2371e52020-07-28 14:30:39 +02003813 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003814 alg,
3815 hash, hash_length,
3816 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003817 mbedtls_rsa_free( rsa );
3818 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003819 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003820 }
3821 else
3822#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003823#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003824 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003825 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003826#if defined(MBEDTLS_ECDSA_C)
3827 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003828 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003829 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003830 status = psa_load_ecp_representation( slot->attr.type,
3831 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003832 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003833 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003834 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003835 return( status );
Steven Cooremana2371e52020-07-28 14:30:39 +02003836 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003837 hash, hash_length,
3838 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003839 mbedtls_ecp_keypair_free( ecp );
3840 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02003841 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02003842 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003843 else
3844#endif /* defined(MBEDTLS_ECDSA_C) */
3845 {
3846 return( PSA_ERROR_INVALID_ARGUMENT );
3847 }
itayzafrir5c753392018-05-08 11:18:38 +03003848 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003849 else
3850#endif /* defined(MBEDTLS_ECP_C) */
3851 {
3852 return( PSA_ERROR_NOT_SUPPORTED );
3853 }
3854}
3855
Gilles Peskine072ac562018-06-30 00:21:29 +02003856#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3857static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3858 mbedtls_rsa_context *rsa )
3859{
3860 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3861 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3862 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3863 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3864}
3865#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3866
Gilles Peskinec5487a82018-12-03 18:08:14 +01003867psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003868 psa_algorithm_t alg,
3869 const uint8_t *input,
3870 size_t input_length,
3871 const uint8_t *salt,
3872 size_t salt_length,
3873 uint8_t *output,
3874 size_t output_size,
3875 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +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;
3879
Darryl Green5cc689a2018-07-24 15:34:10 +01003880 (void) input;
3881 (void) input_length;
3882 (void) salt;
3883 (void) output;
3884 (void) output_size;
3885
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003886 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003887
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003888 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3889 return( PSA_ERROR_INVALID_ARGUMENT );
3890
Gilles Peskine28f8f302019-07-24 13:30:31 +02003891 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003892 if( status != PSA_SUCCESS )
3893 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003894 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3895 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003896 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003897
3898#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003899 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003900 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003901 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003902 status = psa_load_rsa_representation( slot->attr.type,
3903 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003904 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003905 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003906 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003907 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003908
3909 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003910 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003911 status = PSA_ERROR_BUFFER_TOO_SMALL;
3912 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003913 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003914#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003915 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003916 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003917 status = mbedtls_to_psa_error(
3918 mbedtls_rsa_pkcs1_encrypt( rsa,
3919 mbedtls_ctr_drbg_random,
3920 &global_data.ctr_drbg,
3921 MBEDTLS_RSA_PUBLIC,
3922 input_length,
3923 input,
3924 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003925 }
3926 else
3927#endif /* MBEDTLS_PKCS1_V15 */
3928#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003929 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003930 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003931 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003932 status = mbedtls_to_psa_error(
3933 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3934 mbedtls_ctr_drbg_random,
3935 &global_data.ctr_drbg,
3936 MBEDTLS_RSA_PUBLIC,
3937 salt, salt_length,
3938 input_length,
3939 input,
3940 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003941 }
3942 else
3943#endif /* MBEDTLS_PKCS1_V21 */
3944 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003945 status = PSA_ERROR_INVALID_ARGUMENT;
3946 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003947 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003948rsa_exit:
3949 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003950 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003951
Steven Cooremana2371e52020-07-28 14:30:39 +02003952 mbedtls_rsa_free( rsa );
3953 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003954 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003955 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003956 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003957#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003958 {
3959 return( PSA_ERROR_NOT_SUPPORTED );
3960 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003961}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003962
Gilles Peskinec5487a82018-12-03 18:08:14 +01003963psa_status_t psa_asymmetric_decrypt( 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_DECRYPT, 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_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003991 return( PSA_ERROR_INVALID_ARGUMENT );
3992
3993#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003994 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003995 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003996 mbedtls_rsa_context *rsa = NULL;
3997 status = psa_load_rsa_representation( slot->attr.type,
3998 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003999 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004000 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004001 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004002 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004003
Steven Cooremana2371e52020-07-28 14:30:39 +02004004 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004005 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004006 status = PSA_ERROR_INVALID_ARGUMENT;
4007 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004008 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004009
4010#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004011 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004012 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004013 status = mbedtls_to_psa_error(
4014 mbedtls_rsa_pkcs1_decrypt( rsa,
4015 mbedtls_ctr_drbg_random,
4016 &global_data.ctr_drbg,
4017 MBEDTLS_RSA_PRIVATE,
4018 output_length,
4019 input,
4020 output,
4021 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004022 }
4023 else
4024#endif /* MBEDTLS_PKCS1_V15 */
4025#if defined(MBEDTLS_PKCS1_V21)
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_decrypt( rsa,
4031 mbedtls_ctr_drbg_random,
4032 &global_data.ctr_drbg,
4033 MBEDTLS_RSA_PRIVATE,
4034 salt, salt_length,
4035 output_length,
4036 input,
4037 output,
4038 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004039 }
4040 else
4041#endif /* MBEDTLS_PKCS1_V21 */
4042 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004043 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004044 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004045
Steven Cooreman4fed4552020-08-03 14:46:03 +02004046rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004047 mbedtls_rsa_free( rsa );
4048 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004049 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004050 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004051 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004052#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004053 {
4054 return( PSA_ERROR_NOT_SUPPORTED );
4055 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004056}
Gilles Peskine20035e32018-02-03 22:44:14 +01004057
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004058
4059
mohammad1603503973b2018-03-12 15:59:30 +02004060/****************************************************************/
4061/* Symmetric cryptography */
4062/****************************************************************/
4063
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004064/* Initialize the cipher operation structure. Once this function has been
4065 * called, psa_cipher_abort can run and will do the right thing. */
4066static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
4067 psa_algorithm_t alg )
4068{
Gilles Peskinec06e0712018-06-20 16:21:04 +02004069 if( ! PSA_ALG_IS_CIPHER( alg ) )
4070 {
4071 memset( operation, 0, sizeof( *operation ) );
4072 return( PSA_ERROR_INVALID_ARGUMENT );
4073 }
4074
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004075 operation->alg = alg;
4076 operation->key_set = 0;
4077 operation->iv_set = 0;
4078 operation->iv_required = 1;
4079 operation->iv_size = 0;
4080 operation->block_size = 0;
4081 mbedtls_cipher_init( &operation->ctx.cipher );
4082 return( PSA_SUCCESS );
4083}
4084
Gilles Peskinee553c652018-06-04 16:22:46 +02004085static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004086 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004087 psa_algorithm_t alg,
4088 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004089{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004090 int ret = 0;
4091 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004092 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004093 size_t key_bits;
4094 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004095 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4096 PSA_KEY_USAGE_ENCRYPT :
4097 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004098
Jaeden Amero36ee5d02019-02-19 09:25:10 +00004099 /* A context must be freshly initialized before it can be set up. */
4100 if( operation->alg != 0 )
4101 {
4102 return( PSA_ERROR_BAD_STATE );
4103 }
4104
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004105 status = psa_cipher_init( operation, alg );
4106 if( status != PSA_SUCCESS )
4107 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004108
Gilles Peskine28f8f302019-07-24 13:30:31 +02004109 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004110 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004111 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004112 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02004113
Gilles Peskine8e338702019-07-30 20:06:31 +02004114 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004115 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004116 {
4117 status = PSA_ERROR_NOT_SUPPORTED;
4118 goto exit;
4119 }
mohammad1603503973b2018-03-12 15:59:30 +02004120
mohammad1603503973b2018-03-12 15:59:30 +02004121 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004122 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004123 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004124
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004125#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004126 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004127 {
4128 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004129 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004130 memcpy( keys, slot->data.key.data, 16 );
4131 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004132 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4133 keys,
4134 192, cipher_operation );
4135 }
4136 else
4137#endif
4138 {
4139 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004140 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004141 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004142 }
Moran Peker41deec42018-04-04 15:43:05 +03004143 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004144 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004145
mohammad16038481e742018-03-18 13:57:31 +02004146#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004147 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004148 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004149 case PSA_ALG_CBC_NO_PADDING:
4150 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4151 MBEDTLS_PADDING_NONE );
4152 break;
4153 case PSA_ALG_CBC_PKCS7:
4154 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4155 MBEDTLS_PADDING_PKCS7 );
4156 break;
4157 default:
4158 /* The algorithm doesn't involve padding. */
4159 ret = 0;
4160 break;
4161 }
4162 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004163 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004164#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4165
mohammad1603503973b2018-03-12 15:59:30 +02004166 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004167 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004168 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004169 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02004170 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004171 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004172 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004173#if defined(MBEDTLS_CHACHA20_C)
4174 else
4175 if( alg == PSA_ALG_CHACHA20 )
4176 operation->iv_size = 12;
4177#endif
mohammad1603503973b2018-03-12 15:59:30 +02004178
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004179exit:
4180 if( status == 0 )
4181 status = mbedtls_to_psa_error( ret );
4182 if( status != 0 )
4183 psa_cipher_abort( operation );
4184 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004185}
4186
Gilles Peskinefe119512018-07-08 21:39:34 +02004187psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004188 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004189 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004190{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004191 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004192}
4193
Gilles Peskinefe119512018-07-08 21:39:34 +02004194psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004195 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004196 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004197{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004198 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004199}
4200
Gilles Peskinefe119512018-07-08 21:39:34 +02004201psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004202 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004203 size_t iv_size,
4204 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004205{
itayzafrir534bd7c2018-08-02 13:56:32 +03004206 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004207 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004208 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004209 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004210 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004211 }
Moran Peker41deec42018-04-04 15:43:05 +03004212 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004213 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004214 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004215 goto exit;
4216 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004217 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4218 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004219 if( ret != 0 )
4220 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004221 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004222 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004223 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004224
mohammad16038481e742018-03-18 13:57:31 +02004225 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004226 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004227
Moran Peker395db872018-05-31 14:07:14 +03004228exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03004229 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03004230 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004231 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004232}
4233
Gilles Peskinefe119512018-07-08 21:39:34 +02004234psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004235 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004236 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004237{
itayzafrir534bd7c2018-08-02 13:56:32 +03004238 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004240 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004241 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004242 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004243 }
Moran Pekera28258c2018-05-29 16:25:04 +03004244 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004245 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004246 status = PSA_ERROR_INVALID_ARGUMENT;
4247 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004248 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004249 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4250 status = mbedtls_to_psa_error( ret );
4251exit:
4252 if( status == PSA_SUCCESS )
4253 operation->iv_set = 1;
4254 else
mohammad1603503973b2018-03-12 15:59:30 +02004255 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004256 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004257}
4258
Gilles Peskinee553c652018-06-04 16:22:46 +02004259psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4260 const uint8_t *input,
4261 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004262 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004263 size_t output_size,
4264 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004265{
itayzafrir534bd7c2018-08-02 13:56:32 +03004266 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004267 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004268 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00004269
4270 if( operation->alg == 0 )
4271 {
4272 return( PSA_ERROR_BAD_STATE );
4273 }
4274
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004275 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004276 {
4277 /* Take the unprocessed partial block left over from previous
4278 * update calls, if any, plus the input to this call. Remove
4279 * the last partial block, if any. You get the data that will be
4280 * output in this call. */
4281 expected_output_size =
4282 ( operation->ctx.cipher.unprocessed_len + input_length )
4283 / operation->block_size * operation->block_size;
4284 }
4285 else
4286 {
4287 expected_output_size = input_length;
4288 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004289
Gilles Peskine89d789c2018-06-04 17:17:16 +02004290 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004291 {
4292 status = PSA_ERROR_BUFFER_TOO_SMALL;
4293 goto exit;
4294 }
mohammad160382759612018-03-12 18:16:40 +02004295
mohammad1603503973b2018-03-12 15:59:30 +02004296 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004297 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004298 status = mbedtls_to_psa_error( ret );
4299exit:
4300 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004301 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004302 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004303}
4304
Gilles Peskinee553c652018-06-04 16:22:46 +02004305psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4306 uint8_t *output,
4307 size_t output_size,
4308 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004309{
David Saadab4ecc272019-02-14 13:48:10 +02004310 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004311 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004312 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004313
mohammad1603503973b2018-03-12 15:59:30 +02004314 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004315 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004316 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004317 }
4318 if( operation->iv_required && ! operation->iv_set )
4319 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004320 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004321 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004322
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004323 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004324 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4325 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004326 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004327 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004328 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004329 }
4330
Janos Follath315b51c2018-07-09 16:04:51 +01004331 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4332 temp_output_buffer,
4333 output_length );
4334 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004335 {
Janos Follath315b51c2018-07-09 16:04:51 +01004336 status = mbedtls_to_psa_error( cipher_ret );
4337 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004338 }
Janos Follath315b51c2018-07-09 16:04:51 +01004339
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004340 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004341 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004342 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004343 memcpy( output, temp_output_buffer, *output_length );
4344 else
4345 {
Janos Follath315b51c2018-07-09 16:04:51 +01004346 status = PSA_ERROR_BUFFER_TOO_SMALL;
4347 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004348 }
mohammad1603503973b2018-03-12 15:59:30 +02004349
Gilles Peskine3f108122018-12-07 18:14:53 +01004350 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004351 status = psa_cipher_abort( operation );
4352
4353 return( status );
4354
4355error:
4356
4357 *output_length = 0;
4358
Gilles Peskine3f108122018-12-07 18:14:53 +01004359 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004360 (void) psa_cipher_abort( operation );
4361
4362 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004363}
4364
Gilles Peskinee553c652018-06-04 16:22:46 +02004365psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4366{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004367 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004368 {
4369 /* The object has (apparently) been initialized but it is not
4370 * in use. It's ok to call abort on such an object, and there's
4371 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004372 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004373 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004374
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004375 /* Sanity check (shouldn't happen: operation->alg should
4376 * always have been initialized to a valid value). */
4377 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4378 return( PSA_ERROR_BAD_STATE );
4379
mohammad1603503973b2018-03-12 15:59:30 +02004380 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004381
Moran Peker41deec42018-04-04 15:43:05 +03004382 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004383 operation->key_set = 0;
4384 operation->iv_set = 0;
4385 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004386 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004387 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004388
Moran Peker395db872018-05-31 14:07:14 +03004389 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004390}
4391
Gilles Peskinea0655c32018-04-30 17:06:50 +02004392
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004393
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004394
mohammad16035955c982018-04-26 00:53:03 +03004395/****************************************************************/
4396/* AEAD */
4397/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004398
Gilles Peskineedf9a652018-08-17 18:11:56 +02004399typedef struct
4400{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004401 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004402 const mbedtls_cipher_info_t *cipher_info;
4403 union
4404 {
4405#if defined(MBEDTLS_CCM_C)
4406 mbedtls_ccm_context ccm;
4407#endif /* MBEDTLS_CCM_C */
4408#if defined(MBEDTLS_GCM_C)
4409 mbedtls_gcm_context gcm;
4410#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004411#if defined(MBEDTLS_CHACHAPOLY_C)
4412 mbedtls_chachapoly_context chachapoly;
4413#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004414 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004415 psa_algorithm_t core_alg;
4416 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004417 uint8_t tag_length;
4418} aead_operation_t;
4419
Gilles Peskine30a9e412019-01-14 18:36:12 +01004420static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004421{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004422 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004423 {
4424#if defined(MBEDTLS_CCM_C)
4425 case PSA_ALG_CCM:
4426 mbedtls_ccm_free( &operation->ctx.ccm );
4427 break;
4428#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004429#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004430 case PSA_ALG_GCM:
4431 mbedtls_gcm_free( &operation->ctx.gcm );
4432 break;
4433#endif /* MBEDTLS_GCM_C */
4434 }
4435}
4436
4437static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004438 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004439 psa_key_usage_t usage,
4440 psa_algorithm_t alg )
4441{
4442 psa_status_t status;
4443 size_t key_bits;
4444 mbedtls_cipher_id_t cipher_id;
4445
Gilles Peskine28f8f302019-07-24 13:30:31 +02004446 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004447 if( status != PSA_SUCCESS )
4448 return( status );
4449
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004450 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004451
4452 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004453 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004454 &cipher_id );
4455 if( operation->cipher_info == NULL )
4456 return( PSA_ERROR_NOT_SUPPORTED );
4457
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004458 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004459 {
4460#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004461 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4462 operation->core_alg = PSA_ALG_CCM;
4463 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004464 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4465 * The call to mbedtls_ccm_encrypt_and_tag or
4466 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004467 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004468 return( PSA_ERROR_INVALID_ARGUMENT );
4469 mbedtls_ccm_init( &operation->ctx.ccm );
4470 status = mbedtls_to_psa_error(
4471 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004472 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004473 (unsigned int) key_bits ) );
4474 if( status != 0 )
4475 goto cleanup;
4476 break;
4477#endif /* MBEDTLS_CCM_C */
4478
4479#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004480 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4481 operation->core_alg = PSA_ALG_GCM;
4482 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004483 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4484 * The call to mbedtls_gcm_crypt_and_tag or
4485 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004486 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004487 return( PSA_ERROR_INVALID_ARGUMENT );
4488 mbedtls_gcm_init( &operation->ctx.gcm );
4489 status = mbedtls_to_psa_error(
4490 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004491 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004492 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004493 if( status != 0 )
4494 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004495 break;
4496#endif /* MBEDTLS_GCM_C */
4497
Gilles Peskine26869f22019-05-06 15:25:00 +02004498#if defined(MBEDTLS_CHACHAPOLY_C)
4499 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4500 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4501 operation->full_tag_length = 16;
4502 /* We only support the default tag length. */
4503 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4504 return( PSA_ERROR_NOT_SUPPORTED );
4505 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4506 status = mbedtls_to_psa_error(
4507 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004508 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004509 if( status != 0 )
4510 goto cleanup;
4511 break;
4512#endif /* MBEDTLS_CHACHAPOLY_C */
4513
Gilles Peskineedf9a652018-08-17 18:11:56 +02004514 default:
4515 return( PSA_ERROR_NOT_SUPPORTED );
4516 }
4517
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004518 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4519 {
4520 status = PSA_ERROR_INVALID_ARGUMENT;
4521 goto cleanup;
4522 }
4523 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004524
Gilles Peskineedf9a652018-08-17 18:11:56 +02004525 return( PSA_SUCCESS );
4526
4527cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004528 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004529 return( status );
4530}
4531
Gilles Peskinec5487a82018-12-03 18:08:14 +01004532psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004533 psa_algorithm_t alg,
4534 const uint8_t *nonce,
4535 size_t nonce_length,
4536 const uint8_t *additional_data,
4537 size_t additional_data_length,
4538 const uint8_t *plaintext,
4539 size_t plaintext_length,
4540 uint8_t *ciphertext,
4541 size_t ciphertext_size,
4542 size_t *ciphertext_length )
4543{
mohammad16035955c982018-04-26 00:53:03 +03004544 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004545 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004546 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004547
mohammad1603f08a5502018-06-03 15:05:47 +03004548 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004549
Gilles Peskinec5487a82018-12-03 18:08:14 +01004550 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004551 if( status != PSA_SUCCESS )
4552 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004553
Gilles Peskineedf9a652018-08-17 18:11:56 +02004554 /* For all currently supported modes, the tag is at the end of the
4555 * ciphertext. */
4556 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4557 {
4558 status = PSA_ERROR_BUFFER_TOO_SMALL;
4559 goto exit;
4560 }
4561 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004562
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004563#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004564 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004565 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004566 status = mbedtls_to_psa_error(
4567 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4568 MBEDTLS_GCM_ENCRYPT,
4569 plaintext_length,
4570 nonce, nonce_length,
4571 additional_data, additional_data_length,
4572 plaintext, ciphertext,
4573 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004574 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004575 else
4576#endif /* MBEDTLS_GCM_C */
4577#if defined(MBEDTLS_CCM_C)
4578 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004579 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004580 status = mbedtls_to_psa_error(
4581 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4582 plaintext_length,
4583 nonce, nonce_length,
4584 additional_data,
4585 additional_data_length,
4586 plaintext, ciphertext,
4587 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004588 }
mohammad16035c8845f2018-05-09 05:40:09 -07004589 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004590#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004591#if defined(MBEDTLS_CHACHAPOLY_C)
4592 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4593 {
4594 if( nonce_length != 12 || operation.tag_length != 16 )
4595 {
4596 status = PSA_ERROR_NOT_SUPPORTED;
4597 goto exit;
4598 }
4599 status = mbedtls_to_psa_error(
4600 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4601 plaintext_length,
4602 nonce,
4603 additional_data,
4604 additional_data_length,
4605 plaintext,
4606 ciphertext,
4607 tag ) );
4608 }
4609 else
4610#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004611 {
mohammad1603554faad2018-06-03 15:07:38 +03004612 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004613 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004614
Gilles Peskineedf9a652018-08-17 18:11:56 +02004615 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4616 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004617
Gilles Peskineedf9a652018-08-17 18:11:56 +02004618exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004619 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004620 if( status == PSA_SUCCESS )
4621 *ciphertext_length = plaintext_length + operation.tag_length;
4622 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004623}
4624
Gilles Peskineee652a32018-06-01 19:23:52 +02004625/* Locate the tag in a ciphertext buffer containing the encrypted data
4626 * followed by the tag. Return the length of the part preceding the tag in
4627 * *plaintext_length. This is the size of the plaintext in modes where
4628 * the encrypted data has the same size as the plaintext, such as
4629 * CCM and GCM. */
4630static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4631 const uint8_t *ciphertext,
4632 size_t ciphertext_length,
4633 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004634 const uint8_t **p_tag )
4635{
4636 size_t payload_length;
4637 if( tag_length > ciphertext_length )
4638 return( PSA_ERROR_INVALID_ARGUMENT );
4639 payload_length = ciphertext_length - tag_length;
4640 if( payload_length > plaintext_size )
4641 return( PSA_ERROR_BUFFER_TOO_SMALL );
4642 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004643 return( PSA_SUCCESS );
4644}
4645
Gilles Peskinec5487a82018-12-03 18:08:14 +01004646psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004647 psa_algorithm_t alg,
4648 const uint8_t *nonce,
4649 size_t nonce_length,
4650 const uint8_t *additional_data,
4651 size_t additional_data_length,
4652 const uint8_t *ciphertext,
4653 size_t ciphertext_length,
4654 uint8_t *plaintext,
4655 size_t plaintext_size,
4656 size_t *plaintext_length )
4657{
mohammad16035955c982018-04-26 00:53:03 +03004658 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004659 aead_operation_t operation;
4660 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004661
Gilles Peskineee652a32018-06-01 19:23:52 +02004662 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004663
Gilles Peskinec5487a82018-12-03 18:08:14 +01004664 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004665 if( status != PSA_SUCCESS )
4666 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004667
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004668 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4669 ciphertext, ciphertext_length,
4670 plaintext_size, &tag );
4671 if( status != PSA_SUCCESS )
4672 goto exit;
4673
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004674#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004675 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004676 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004677 status = mbedtls_to_psa_error(
4678 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4679 ciphertext_length - operation.tag_length,
4680 nonce, nonce_length,
4681 additional_data,
4682 additional_data_length,
4683 tag, operation.tag_length,
4684 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004685 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004686 else
4687#endif /* MBEDTLS_GCM_C */
4688#if defined(MBEDTLS_CCM_C)
4689 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004690 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004691 status = mbedtls_to_psa_error(
4692 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4693 ciphertext_length - operation.tag_length,
4694 nonce, nonce_length,
4695 additional_data,
4696 additional_data_length,
4697 ciphertext, plaintext,
4698 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004699 }
mohammad160339574652018-06-01 04:39:53 -07004700 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004701#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004702#if defined(MBEDTLS_CHACHAPOLY_C)
4703 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4704 {
4705 if( nonce_length != 12 || operation.tag_length != 16 )
4706 {
4707 status = PSA_ERROR_NOT_SUPPORTED;
4708 goto exit;
4709 }
4710 status = mbedtls_to_psa_error(
4711 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4712 ciphertext_length - operation.tag_length,
4713 nonce,
4714 additional_data,
4715 additional_data_length,
4716 tag,
4717 ciphertext,
4718 plaintext ) );
4719 }
4720 else
4721#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004722 {
mohammad1603554faad2018-06-03 15:07:38 +03004723 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004724 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004725
Gilles Peskineedf9a652018-08-17 18:11:56 +02004726 if( status != PSA_SUCCESS && plaintext_size != 0 )
4727 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004728
Gilles Peskineedf9a652018-08-17 18:11:56 +02004729exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004730 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004731 if( status == PSA_SUCCESS )
4732 *plaintext_length = ciphertext_length - operation.tag_length;
4733 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004734}
4735
Gilles Peskinea0655c32018-04-30 17:06:50 +02004736
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004737
Gilles Peskine20035e32018-02-03 22:44:14 +01004738/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004739/* Generators */
4740/****************************************************************/
4741
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004742#define HKDF_STATE_INIT 0 /* no input yet */
4743#define HKDF_STATE_STARTED 1 /* got salt */
4744#define HKDF_STATE_KEYED 2 /* got key */
4745#define HKDF_STATE_OUTPUT 3 /* output started */
4746
Gilles Peskinecbe66502019-05-16 16:59:18 +02004747static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004748 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004749{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004750 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4751 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004752 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004753 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004754}
4755
4756
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004757psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004758{
4759 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004760 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004761 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004762 {
4763 /* The object has (apparently) been initialized but it is not
4764 * in use. It's ok to call abort on such an object, and there's
4765 * nothing to do. */
4766 }
4767 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004768#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004769 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004770 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004771 mbedtls_free( operation->ctx.hkdf.info );
4772 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004773 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004774 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004775 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004776 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004777 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004778 if( operation->ctx.tls12_prf.seed != NULL )
4779 {
4780 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4781 operation->ctx.tls12_prf.seed_length );
4782 mbedtls_free( operation->ctx.tls12_prf.seed );
4783 }
4784
4785 if( operation->ctx.tls12_prf.label != NULL )
4786 {
4787 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4788 operation->ctx.tls12_prf.label_length );
4789 mbedtls_free( operation->ctx.tls12_prf.label );
4790 }
4791
4792 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4793
4794 /* We leave the fields Ai and output_block to be erased safely by the
4795 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004796 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004797 else
4798#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004799 {
4800 status = PSA_ERROR_BAD_STATE;
4801 }
Janos Follath083036a2019-06-11 10:22:26 +01004802 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004803 return( status );
4804}
4805
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004806psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004807 size_t *capacity)
4808{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004809 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004810 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004811 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004812 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004813 }
4814
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004815 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004816 return( PSA_SUCCESS );
4817}
4818
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004819psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004820 size_t capacity )
4821{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004822 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004823 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004825 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004826 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004827 return( PSA_SUCCESS );
4828}
4829
Gilles Peskinebef7f142018-07-12 17:22:21 +02004830#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004831/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004832 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004833static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004834 psa_algorithm_t hash_alg,
4835 uint8_t *output,
4836 size_t output_length )
4837{
4838 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4839 psa_status_t status;
4840
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004841 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4842 return( PSA_ERROR_BAD_STATE );
4843 hkdf->state = HKDF_STATE_OUTPUT;
4844
Gilles Peskinebef7f142018-07-12 17:22:21 +02004845 while( output_length != 0 )
4846 {
4847 /* Copy what remains of the current block */
4848 uint8_t n = hash_length - hkdf->offset_in_block;
4849 if( n > output_length )
4850 n = (uint8_t) output_length;
4851 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4852 output += n;
4853 output_length -= n;
4854 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004855 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004856 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004857 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004858 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004859 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004860 * object was corrupted or if this function is called directly
4861 * inside the library. */
4862 if( hkdf->block_number == 0xff )
4863 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004864
4865 /* We need a new block */
4866 ++hkdf->block_number;
4867 hkdf->offset_in_block = 0;
4868 status = psa_hmac_setup_internal( &hkdf->hmac,
4869 hkdf->prk, hash_length,
4870 hash_alg );
4871 if( status != PSA_SUCCESS )
4872 return( status );
4873 if( hkdf->block_number != 1 )
4874 {
4875 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4876 hkdf->output_block,
4877 hash_length );
4878 if( status != PSA_SUCCESS )
4879 return( status );
4880 }
4881 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4882 hkdf->info,
4883 hkdf->info_length );
4884 if( status != PSA_SUCCESS )
4885 return( status );
4886 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4887 &hkdf->block_number, 1 );
4888 if( status != PSA_SUCCESS )
4889 return( status );
4890 status = psa_hmac_finish_internal( &hkdf->hmac,
4891 hkdf->output_block,
4892 sizeof( hkdf->output_block ) );
4893 if( status != PSA_SUCCESS )
4894 return( status );
4895 }
4896
4897 return( PSA_SUCCESS );
4898}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004899
Janos Follath7742fee2019-06-17 12:58:10 +01004900static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4901 psa_tls12_prf_key_derivation_t *tls12_prf,
4902 psa_algorithm_t alg )
4903{
4904 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4905 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004906 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4907 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004908
Janos Follath7742fee2019-06-17 12:58:10 +01004909 /* We can't be wanting more output after block 0xff, otherwise
4910 * the capacity check in psa_key_derivation_output_bytes() would have
4911 * prevented this call. It could happen only if the operation
4912 * object was corrupted or if this function is called directly
4913 * inside the library. */
4914 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004915 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004916
4917 /* We need a new block */
4918 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004919 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004920
4921 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4922 *
4923 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4924 *
4925 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4926 * HMAC_hash(secret, A(2) + seed) +
4927 * HMAC_hash(secret, A(3) + seed) + ...
4928 *
4929 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004930 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004931 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004932 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004933 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004934 * is currently extracted as `output_block` and where i is
4935 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004936 */
4937
Janos Follathea29bfb2019-06-19 12:21:20 +01004938 /* Save the hash context before using it, to preserve the hash state with
4939 * only the inner padding in it. We need this, because inner padding depends
4940 * on the key (secret in the RFC's terminology). */
4941 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4942 if( status != PSA_SUCCESS )
4943 goto cleanup;
4944
4945 /* Calculate A(i) where i = tls12_prf->block_number. */
4946 if( tls12_prf->block_number == 1 )
4947 {
4948 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4949 * the variable seed and in this instance means it in the context of the
4950 * P_hash function, where seed = label + seed.) */
4951 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4952 tls12_prf->label, tls12_prf->label_length );
4953 if( status != PSA_SUCCESS )
4954 goto cleanup;
4955 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4956 tls12_prf->seed, tls12_prf->seed_length );
4957 if( status != PSA_SUCCESS )
4958 goto cleanup;
4959 }
4960 else
4961 {
4962 /* A(i) = HMAC_hash(secret, A(i-1)) */
4963 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4964 tls12_prf->Ai, hash_length );
4965 if( status != PSA_SUCCESS )
4966 goto cleanup;
4967 }
4968
4969 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4970 tls12_prf->Ai, hash_length );
4971 if( status != PSA_SUCCESS )
4972 goto cleanup;
4973 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4974 if( status != PSA_SUCCESS )
4975 goto cleanup;
4976
4977 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4978 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4979 tls12_prf->Ai, hash_length );
4980 if( status != PSA_SUCCESS )
4981 goto cleanup;
4982 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4983 tls12_prf->label, tls12_prf->label_length );
4984 if( status != PSA_SUCCESS )
4985 goto cleanup;
4986 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4987 tls12_prf->seed, tls12_prf->seed_length );
4988 if( status != PSA_SUCCESS )
4989 goto cleanup;
4990 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4991 tls12_prf->output_block, hash_length );
4992 if( status != PSA_SUCCESS )
4993 goto cleanup;
4994 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4995 if( status != PSA_SUCCESS )
4996 goto cleanup;
4997
Janos Follath7742fee2019-06-17 12:58:10 +01004998
4999cleanup:
5000
Janos Follathea29bfb2019-06-19 12:21:20 +01005001 cleanup_status = psa_hash_abort( &backup );
5002 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5003 status = cleanup_status;
5004
5005 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005006}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005007
Janos Follath844eb0e2019-06-19 12:10:49 +01005008static psa_status_t psa_key_derivation_tls12_prf_read(
5009 psa_tls12_prf_key_derivation_t *tls12_prf,
5010 psa_algorithm_t alg,
5011 uint8_t *output,
5012 size_t output_length )
5013{
5014 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5015 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5016 psa_status_t status;
5017 uint8_t offset, length;
5018
5019 while( output_length != 0 )
5020 {
5021 /* Check if we have fully processed the current block. */
5022 if( tls12_prf->left_in_block == 0 )
5023 {
5024 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5025 alg );
5026 if( status != PSA_SUCCESS )
5027 return( status );
5028
5029 continue;
5030 }
5031
5032 if( tls12_prf->left_in_block > output_length )
5033 length = (uint8_t) output_length;
5034 else
5035 length = tls12_prf->left_in_block;
5036
5037 offset = hash_length - tls12_prf->left_in_block;
5038 memcpy( output, tls12_prf->output_block + offset, length );
5039 output += length;
5040 output_length -= length;
5041 tls12_prf->left_in_block -= length;
5042 }
5043
5044 return( PSA_SUCCESS );
5045}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005046#endif /* MBEDTLS_MD_C */
5047
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005048psa_status_t psa_key_derivation_output_bytes(
5049 psa_key_derivation_operation_t *operation,
5050 uint8_t *output,
5051 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005052{
5053 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005054 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005055
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005056 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005057 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005058 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005059 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005060 }
5061
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005062 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005063 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005064 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005065 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005066 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005067 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005068 goto exit;
5069 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005070 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005071 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005072 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005073 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005074 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5075 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005076 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005077 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005078 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005079 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005080 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005081
Gilles Peskinebef7f142018-07-12 17:22:21 +02005082#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005083 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005084 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005085 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005086 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005087 output, output_length );
5088 }
Janos Follathadbec812019-06-14 11:05:39 +01005089 else
Janos Follathadbec812019-06-14 11:05:39 +01005090 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005091 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005092 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005093 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005094 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005095 output_length );
5096 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005097 else
5098#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005099 {
5100 return( PSA_ERROR_BAD_STATE );
5101 }
5102
5103exit:
5104 if( status != PSA_SUCCESS )
5105 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005106 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005107 * This allows us to differentiate between exhausted operations and
5108 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5109 * operations. */
5110 psa_algorithm_t alg = operation->alg;
5111 psa_key_derivation_abort( operation );
5112 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005113 memset( output, '!', output_length );
5114 }
5115 return( status );
5116}
5117
Gilles Peskine08542d82018-07-19 17:05:42 +02005118#if defined(MBEDTLS_DES_C)
5119static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5120{
5121 if( data_size >= 8 )
5122 mbedtls_des_key_set_parity( data );
5123 if( data_size >= 16 )
5124 mbedtls_des_key_set_parity( data + 8 );
5125 if( data_size >= 24 )
5126 mbedtls_des_key_set_parity( data + 16 );
5127}
5128#endif /* MBEDTLS_DES_C */
5129
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005130static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005131 psa_key_slot_t *slot,
5132 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005133 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005134{
5135 uint8_t *data = NULL;
5136 size_t bytes = PSA_BITS_TO_BYTES( bits );
5137 psa_status_t status;
5138
Gilles Peskine8e338702019-07-30 20:06:31 +02005139 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005140 return( PSA_ERROR_INVALID_ARGUMENT );
5141 if( bits % 8 != 0 )
5142 return( PSA_ERROR_INVALID_ARGUMENT );
5143 data = mbedtls_calloc( 1, bytes );
5144 if( data == NULL )
5145 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5146
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005147 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005148 if( status != PSA_SUCCESS )
5149 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005150#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005151 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005152 psa_des_set_key_parity( data, bytes );
5153#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005154 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005155
5156exit:
5157 mbedtls_free( data );
5158 return( status );
5159}
5160
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005161psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005162 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005163 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005164{
5165 psa_status_t status;
5166 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005167 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005168
5169 /* Reject any attempt to create a zero-length key so that we don't
5170 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5171 if( psa_get_key_bits( attributes ) == 0 )
5172 return( PSA_ERROR_INVALID_ARGUMENT );
5173
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005174 if( ! operation->can_output_key )
5175 return( PSA_ERROR_NOT_PERMITTED );
5176
Gilles Peskinedf179142019-07-15 22:02:14 +02005177 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5178 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005179#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5180 if( driver != NULL )
5181 {
5182 /* Deriving a key in a secure element is not implemented yet. */
5183 status = PSA_ERROR_NOT_SUPPORTED;
5184 }
5185#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005186 if( status == PSA_SUCCESS )
5187 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005188 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005189 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005190 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005191 }
5192 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005193 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005194 if( status != PSA_SUCCESS )
5195 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005196 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005197 *handle = 0;
5198 }
5199 return( status );
5200}
5201
Gilles Peskineeab56e42018-07-12 17:12:33 +02005202
5203
5204/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005205/* Key derivation */
5206/****************************************************************/
5207
Gilles Peskine969c5d62019-01-16 15:53:06 +01005208static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005210 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005211{
Janos Follath5fe19732019-06-20 15:09:30 +01005212 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5213 * initialisers for this union leave some bytes unspecified.) */
5214 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5215
Gilles Peskine969c5d62019-01-16 15:53:06 +01005216 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005217#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005218 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5219 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5220 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005221 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005222 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005223 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5224 if( hash_size == 0 )
5225 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005226 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5227 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005228 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005229 {
5230 return( PSA_ERROR_NOT_SUPPORTED );
5231 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005232 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005233 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005234 }
5235#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005236 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005237 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005238}
5239
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005240psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005241 psa_algorithm_t alg )
5242{
5243 psa_status_t status;
5244
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005245 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005246 return( PSA_ERROR_BAD_STATE );
5247
Gilles Peskine6843c292019-01-18 16:44:49 +01005248 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5249 return( PSA_ERROR_INVALID_ARGUMENT );
5250 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005251 {
5252 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005253 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005254 }
5255 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5256 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005257 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005258 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005259 else
5260 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005261
5262 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005263 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005264 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005265}
5266
5267#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005268static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005269 psa_algorithm_t hash_alg,
5270 psa_key_derivation_step_t step,
5271 const uint8_t *data,
5272 size_t data_length )
5273{
5274 psa_status_t status;
5275 switch( step )
5276 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005277 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005278 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005279 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005280 status = psa_hmac_setup_internal( &hkdf->hmac,
5281 data, data_length,
5282 hash_alg );
5283 if( status != PSA_SUCCESS )
5284 return( status );
5285 hkdf->state = HKDF_STATE_STARTED;
5286 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005287 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005288 /* If no salt was provided, use an empty salt. */
5289 if( hkdf->state == HKDF_STATE_INIT )
5290 {
5291 status = psa_hmac_setup_internal( &hkdf->hmac,
5292 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005293 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005294 if( status != PSA_SUCCESS )
5295 return( status );
5296 hkdf->state = HKDF_STATE_STARTED;
5297 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005298 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005299 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005300 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5301 data, data_length );
5302 if( status != PSA_SUCCESS )
5303 return( status );
5304 status = psa_hmac_finish_internal( &hkdf->hmac,
5305 hkdf->prk,
5306 sizeof( hkdf->prk ) );
5307 if( status != PSA_SUCCESS )
5308 return( status );
5309 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5310 hkdf->block_number = 0;
5311 hkdf->state = HKDF_STATE_KEYED;
5312 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005313 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005314 if( hkdf->state == HKDF_STATE_OUTPUT )
5315 return( PSA_ERROR_BAD_STATE );
5316 if( hkdf->info_set )
5317 return( PSA_ERROR_BAD_STATE );
5318 hkdf->info_length = data_length;
5319 if( data_length != 0 )
5320 {
5321 hkdf->info = mbedtls_calloc( 1, data_length );
5322 if( hkdf->info == NULL )
5323 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5324 memcpy( hkdf->info, data, data_length );
5325 }
5326 hkdf->info_set = 1;
5327 return( PSA_SUCCESS );
5328 default:
5329 return( PSA_ERROR_INVALID_ARGUMENT );
5330 }
5331}
Janos Follathb03233e2019-06-11 15:30:30 +01005332
Janos Follathf08e2652019-06-13 09:05:41 +01005333static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5334 const uint8_t *data,
5335 size_t data_length )
5336{
5337 if( prf->state != TLS12_PRF_STATE_INIT )
5338 return( PSA_ERROR_BAD_STATE );
5339
Janos Follathd6dce9f2019-07-04 09:11:38 +01005340 if( data_length != 0 )
5341 {
5342 prf->seed = mbedtls_calloc( 1, data_length );
5343 if( prf->seed == NULL )
5344 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005345
Janos Follathd6dce9f2019-07-04 09:11:38 +01005346 memcpy( prf->seed, data, data_length );
5347 prf->seed_length = data_length;
5348 }
Janos Follathf08e2652019-06-13 09:05:41 +01005349
5350 prf->state = TLS12_PRF_STATE_SEED_SET;
5351
5352 return( PSA_SUCCESS );
5353}
5354
Janos Follath81550542019-06-13 14:26:34 +01005355static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5356 psa_algorithm_t hash_alg,
5357 const uint8_t *data,
5358 size_t data_length )
5359{
5360 psa_status_t status;
5361 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5362 return( PSA_ERROR_BAD_STATE );
5363
5364 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5365 if( status != PSA_SUCCESS )
5366 return( status );
5367
5368 prf->state = TLS12_PRF_STATE_KEY_SET;
5369
5370 return( PSA_SUCCESS );
5371}
5372
Janos Follath6660f0e2019-06-17 08:44:03 +01005373static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5374 psa_tls12_prf_key_derivation_t *prf,
5375 psa_algorithm_t hash_alg,
5376 const uint8_t *data,
5377 size_t data_length )
5378{
5379 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005380 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5381 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005382
5383 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5384 return( PSA_ERROR_INVALID_ARGUMENT );
5385
5386 /* Quoting RFC 4279, Section 2:
5387 *
5388 * The premaster secret is formed as follows: if the PSK is N octets
5389 * long, concatenate a uint16 with the value N, N zero octets, a second
5390 * uint16 with the value N, and the PSK itself.
5391 */
5392
Janos Follath40e13932019-06-26 13:22:29 +01005393 *cur++ = ( data_length >> 8 ) & 0xff;
5394 *cur++ = ( data_length >> 0 ) & 0xff;
5395 memset( cur, 0, data_length );
5396 cur += data_length;
5397 *cur++ = pms[0];
5398 *cur++ = pms[1];
5399 memcpy( cur, data, data_length );
5400 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005401
Janos Follath40e13932019-06-26 13:22:29 +01005402 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005403
5404 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5405 return( status );
5406}
5407
Janos Follath63028dd2019-06-13 09:15:47 +01005408static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5409 const uint8_t *data,
5410 size_t data_length )
5411{
5412 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5413 return( PSA_ERROR_BAD_STATE );
5414
Janos Follathd6dce9f2019-07-04 09:11:38 +01005415 if( data_length != 0 )
5416 {
5417 prf->label = mbedtls_calloc( 1, data_length );
5418 if( prf->label == NULL )
5419 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005420
Janos Follathd6dce9f2019-07-04 09:11:38 +01005421 memcpy( prf->label, data, data_length );
5422 prf->label_length = data_length;
5423 }
Janos Follath63028dd2019-06-13 09:15:47 +01005424
5425 prf->state = TLS12_PRF_STATE_LABEL_SET;
5426
5427 return( PSA_SUCCESS );
5428}
5429
Janos Follathb03233e2019-06-11 15:30:30 +01005430static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5431 psa_algorithm_t hash_alg,
5432 psa_key_derivation_step_t step,
5433 const uint8_t *data,
5434 size_t data_length )
5435{
Janos Follathb03233e2019-06-11 15:30:30 +01005436 switch( step )
5437 {
Janos Follathf08e2652019-06-13 09:05:41 +01005438 case PSA_KEY_DERIVATION_INPUT_SEED:
5439 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005440 case PSA_KEY_DERIVATION_INPUT_SECRET:
5441 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005442 case PSA_KEY_DERIVATION_INPUT_LABEL:
5443 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005444 default:
5445 return( PSA_ERROR_INVALID_ARGUMENT );
5446 }
5447}
Janos Follath6660f0e2019-06-17 08:44:03 +01005448
5449static psa_status_t psa_tls12_prf_psk_to_ms_input(
5450 psa_tls12_prf_key_derivation_t *prf,
5451 psa_algorithm_t hash_alg,
5452 psa_key_derivation_step_t step,
5453 const uint8_t *data,
5454 size_t data_length )
5455{
5456 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005457 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005458 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5459 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005460 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005461
5462 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5463}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005464#endif /* MBEDTLS_MD_C */
5465
Gilles Peskineb8965192019-09-24 16:21:10 +02005466/** Check whether the given key type is acceptable for the given
5467 * input step of a key derivation.
5468 *
5469 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5470 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5471 * Both secret and non-secret inputs can alternatively have the type
5472 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5473 * that the input was passed as a buffer rather than via a key object.
5474 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005475static int psa_key_derivation_check_input_type(
5476 psa_key_derivation_step_t step,
5477 psa_key_type_t key_type )
5478{
5479 switch( step )
5480 {
5481 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005482 if( key_type == PSA_KEY_TYPE_DERIVE )
5483 return( PSA_SUCCESS );
5484 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005485 return( PSA_SUCCESS );
5486 break;
5487 case PSA_KEY_DERIVATION_INPUT_LABEL:
5488 case PSA_KEY_DERIVATION_INPUT_SALT:
5489 case PSA_KEY_DERIVATION_INPUT_INFO:
5490 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005491 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5492 return( PSA_SUCCESS );
5493 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005494 return( PSA_SUCCESS );
5495 break;
5496 }
5497 return( PSA_ERROR_INVALID_ARGUMENT );
5498}
5499
Janos Follathb80a94e2019-06-12 15:54:46 +01005500static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005501 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005502 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005503 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005504 const uint8_t *data,
5505 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005506{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005507 psa_status_t status;
5508 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5509
5510 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005511 if( status != PSA_SUCCESS )
5512 goto exit;
5513
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005514#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005515 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005516 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005517 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005518 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005519 step, data, data_length );
5520 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005521 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005522 {
Janos Follathb03233e2019-06-11 15:30:30 +01005523 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5524 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5525 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005526 }
5527 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5528 {
5529 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5530 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5531 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005532 }
5533 else
5534#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005535 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005536 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005537 return( PSA_ERROR_BAD_STATE );
5538 }
5539
Gilles Peskine224b0d62019-09-23 18:13:17 +02005540exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005541 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005542 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005543 return( status );
5544}
5545
Janos Follath51f4a0f2019-06-14 11:35:55 +01005546psa_status_t psa_key_derivation_input_bytes(
5547 psa_key_derivation_operation_t *operation,
5548 psa_key_derivation_step_t step,
5549 const uint8_t *data,
5550 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005551{
Gilles Peskineb8965192019-09-24 16:21:10 +02005552 return( psa_key_derivation_input_internal( operation, step,
5553 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005554 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005555}
5556
Janos Follath51f4a0f2019-06-14 11:35:55 +01005557psa_status_t psa_key_derivation_input_key(
5558 psa_key_derivation_operation_t *operation,
5559 psa_key_derivation_step_t step,
5560 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005561{
5562 psa_key_slot_t *slot;
5563 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005564
Gilles Peskine28f8f302019-07-24 13:30:31 +02005565 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005566 PSA_KEY_USAGE_DERIVE,
5567 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005568 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005569 {
5570 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005571 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005572 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005573
5574 /* Passing a key object as a SECRET input unlocks the permission
5575 * to output to a key object. */
5576 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5577 operation->can_output_key = 1;
5578
Janos Follathb80a94e2019-06-12 15:54:46 +01005579 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005580 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005581 slot->data.key.data,
5582 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005583}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005584
5585
5586
5587/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005588/* Key agreement */
5589/****************************************************************/
5590
Gilles Peskinea05219c2018-11-16 16:02:56 +01005591#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005592static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5593 size_t peer_key_length,
5594 const mbedtls_ecp_keypair *our_key,
5595 uint8_t *shared_secret,
5596 size_t shared_secret_size,
5597 size_t *shared_secret_length )
5598{
Steven Cooremand4867872020-08-05 16:31:39 +02005599 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005600 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005601 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005602 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005603 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005604 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005605
Steven Cooreman4fed4552020-08-03 14:46:03 +02005606 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5607 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005608 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005609 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005610 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005611 goto exit;
5612
Jaeden Amero97271b32019-01-10 19:38:51 +00005613 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005614 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005615 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005616 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005617 status = mbedtls_to_psa_error(
5618 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5619 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005620 goto exit;
5621
Jaeden Amero97271b32019-01-10 19:38:51 +00005622 status = mbedtls_to_psa_error(
5623 mbedtls_ecdh_calc_secret( &ecdh,
5624 shared_secret_length,
5625 shared_secret, shared_secret_size,
5626 mbedtls_ctr_drbg_random,
5627 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005628 if( status != PSA_SUCCESS )
5629 goto exit;
5630 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5631 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005632
5633exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005634 if( status != PSA_SUCCESS )
5635 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005636 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005637 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005638 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005639
Jaeden Amero97271b32019-01-10 19:38:51 +00005640 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005641}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005642#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005643
Gilles Peskine01d718c2018-09-18 12:01:02 +02005644#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5645
Gilles Peskine0216fe12019-04-11 21:23:21 +02005646static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5647 psa_key_slot_t *private_key,
5648 const uint8_t *peer_key,
5649 size_t peer_key_length,
5650 uint8_t *shared_secret,
5651 size_t shared_secret_size,
5652 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005653{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005654 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005655 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005656#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005657 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005658 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005659 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005660 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005661 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005662 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005663 private_key->data.key.data,
5664 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005665 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005666 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005667 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005668 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005669 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005670 shared_secret, shared_secret_size,
5671 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005672 mbedtls_ecp_keypair_free( ecp );
5673 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005674 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005675#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005676 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005677 (void) private_key;
5678 (void) peer_key;
5679 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005680 (void) shared_secret;
5681 (void) shared_secret_size;
5682 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005683 return( PSA_ERROR_NOT_SUPPORTED );
5684 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005685}
5686
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005687/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005688 * to potentially free embedded data structures and wipe confidential data.
5689 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005690static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005691 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005692 psa_key_slot_t *private_key,
5693 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005694 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005695{
5696 psa_status_t status;
5697 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5698 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005699 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005700
5701 /* Step 1: run the secret agreement algorithm to generate the shared
5702 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005703 status = psa_key_agreement_raw_internal( ka_alg,
5704 private_key,
5705 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005706 shared_secret,
5707 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005708 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005709 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005710 goto exit;
5711
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005712 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005713 * the shared secret. A shared secret is permitted wherever a key
5714 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005715 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005716 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005717 shared_secret,
5718 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005719
Gilles Peskine12313cd2018-06-20 00:20:32 +02005720exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005721 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005722 return( status );
5723}
5724
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005725psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005726 psa_key_derivation_step_t step,
5727 psa_key_handle_t private_key,
5728 const uint8_t *peer_key,
5729 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005730{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005731 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005732 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005733 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005734 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005735 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005736 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005737 if( status != PSA_SUCCESS )
5738 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005739 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005740 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005741 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005742 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005743 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005744 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005745}
5746
Gilles Peskinebe697d82019-05-16 18:00:41 +02005747psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5748 psa_key_handle_t private_key,
5749 const uint8_t *peer_key,
5750 size_t peer_key_length,
5751 uint8_t *output,
5752 size_t output_size,
5753 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005754{
5755 psa_key_slot_t *slot;
5756 psa_status_t status;
5757
5758 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5759 {
5760 status = PSA_ERROR_INVALID_ARGUMENT;
5761 goto exit;
5762 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005763 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005764 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005765 if( status != PSA_SUCCESS )
5766 goto exit;
5767
5768 status = psa_key_agreement_raw_internal( alg, slot,
5769 peer_key, peer_key_length,
5770 output, output_size,
5771 output_length );
5772
5773exit:
5774 if( status != PSA_SUCCESS )
5775 {
5776 /* If an error happens and is not handled properly, the output
5777 * may be used as a key to protect sensitive data. Arrange for such
5778 * a key to be random, which is likely to result in decryption or
5779 * verification errors. This is better than filling the buffer with
5780 * some constant data such as zeros, which would result in the data
5781 * being protected with a reproducible, easily knowable key.
5782 */
5783 psa_generate_random( output, output_size );
5784 *output_length = output_size;
5785 }
5786 return( status );
5787}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005788
5789
5790/****************************************************************/
5791/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005792/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005793
Gilles Peskine4c317f42018-07-12 01:24:09 +02005794psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005795 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005796{
Janos Follath24eed8d2019-11-22 13:21:35 +00005797 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005798 GUARD_MODULE_INITIALIZED;
5799
Gilles Peskinef181eca2019-08-07 13:49:00 +02005800 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5801 {
5802 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5803 output,
5804 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5805 if( ret != 0 )
5806 return( mbedtls_to_psa_error( ret ) );
5807 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5808 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5809 }
5810
Gilles Peskinee59236f2018-01-27 23:32:46 +01005811 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5812 return( mbedtls_to_psa_error( ret ) );
5813}
5814
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005815#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5816#include "mbedtls/entropy_poll.h"
5817
Gilles Peskine7228da22019-07-15 11:06:15 +02005818psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005819 size_t seed_size )
5820{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005821 if( global_data.initialized )
5822 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005823
5824 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5825 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5826 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5827 return( PSA_ERROR_INVALID_ARGUMENT );
5828
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005829 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005830}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005831#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005832
Gilles Peskinee56e8782019-04-26 17:34:02 +02005833#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5834static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5835 size_t domain_parameters_size,
5836 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005837{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005838 size_t i;
5839 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005840
Gilles Peskinee56e8782019-04-26 17:34:02 +02005841 if( domain_parameters_size == 0 )
5842 {
5843 *exponent = 65537;
5844 return( PSA_SUCCESS );
5845 }
5846
5847 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5848 * support values that fit in a 32-bit integer, which is larger than
5849 * int on just about every platform anyway. */
5850 if( domain_parameters_size > sizeof( acc ) )
5851 return( PSA_ERROR_NOT_SUPPORTED );
5852 for( i = 0; i < domain_parameters_size; i++ )
5853 acc = ( acc << 8 ) | domain_parameters[i];
5854 if( acc > INT_MAX )
5855 return( PSA_ERROR_NOT_SUPPORTED );
5856 *exponent = acc;
5857 return( PSA_SUCCESS );
5858}
5859#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5860
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005861static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005862 psa_key_slot_t *slot, size_t bits,
5863 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005864{
Gilles Peskine8e338702019-07-30 20:06:31 +02005865 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005866
Gilles Peskinee56e8782019-04-26 17:34:02 +02005867 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005868 return( PSA_ERROR_INVALID_ARGUMENT );
5869
Gilles Peskinee59236f2018-01-27 23:32:46 +01005870 if( key_type_is_raw_bytes( type ) )
5871 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005872 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005873
5874 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005875 if( status != PSA_SUCCESS )
5876 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005877
5878 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02005879 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
5880 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005881 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005882
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005883 status = psa_generate_random( slot->data.key.data,
5884 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005885 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005886 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005887
5888 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005889#if defined(MBEDTLS_DES_C)
5890 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005891 psa_des_set_key_parity( slot->data.key.data,
5892 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005893#endif /* MBEDTLS_DES_C */
5894 }
5895 else
5896
5897#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005898 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005899 {
Steven Cooremana01795d2020-07-24 22:48:15 +02005900 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005901 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005902 int exponent;
5903 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005904 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5905 return( PSA_ERROR_NOT_SUPPORTED );
5906 /* Accept only byte-aligned keys, for the same reasons as
5907 * in psa_import_rsa_key(). */
5908 if( bits % 8 != 0 )
5909 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005910 status = psa_read_rsa_exponent( domain_parameters,
5911 domain_parameters_size,
5912 &exponent );
5913 if( status != PSA_SUCCESS )
5914 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02005915 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5916 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005917 mbedtls_ctr_drbg_random,
5918 &global_data.ctr_drbg,
5919 (unsigned int) bits,
5920 exponent );
5921 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005922 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02005923
5924 /* Make sure to always have an export representation available */
5925 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
5926
Steven Cooreman75b74362020-07-28 14:30:13 +02005927 status = psa_allocate_buffer_to_slot( slot, bytes );
5928 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005929 {
5930 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02005931 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005932 }
Steven Cooremana01795d2020-07-24 22:48:15 +02005933
5934 status = psa_export_rsa_key( type,
5935 &rsa,
5936 slot->data.key.data,
5937 bytes,
5938 &slot->data.key.bytes );
5939 mbedtls_rsa_free( &rsa );
5940 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005941 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005942 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005943 }
5944 else
5945#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5946
5947#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005948 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005949 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005950 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005951 mbedtls_ecp_group_id grp_id =
5952 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005953 const mbedtls_ecp_curve_info *curve_info =
5954 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02005955 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005956 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005957 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005958 return( PSA_ERROR_NOT_SUPPORTED );
5959 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5960 return( PSA_ERROR_NOT_SUPPORTED );
5961 if( curve_info->bit_size != bits )
5962 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005963 mbedtls_ecp_keypair_init( &ecp );
5964 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005965 mbedtls_ctr_drbg_random,
5966 &global_data.ctr_drbg );
5967 if( ret != 0 )
5968 {
Steven Cooremanacda8342020-07-24 23:09:52 +02005969 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005970 return( mbedtls_to_psa_error( ret ) );
5971 }
Steven Cooremanacda8342020-07-24 23:09:52 +02005972
5973
5974 /* Make sure to always have an export representation available */
5975 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02005976 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
5977 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005978 {
5979 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005980 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005981 }
Steven Cooreman75b74362020-07-28 14:30:13 +02005982
5983 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02005984 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
5985
5986 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02005987 if( status != PSA_SUCCESS ) {
5988 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02005989 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02005990 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02005991 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005992 }
5993 else
5994#endif /* MBEDTLS_ECP_C */
Steven Cooreman29149862020-08-05 15:43:42 +02005995 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005996 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02005997 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005998
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005999 return( PSA_SUCCESS );
6000}
Darryl Green0c6575a2018-11-07 16:05:30 +00006001
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006002psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006003 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006004{
6005 psa_status_t status;
6006 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006007 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006008
Gilles Peskine0f84d622019-09-12 19:03:13 +02006009 /* Reject any attempt to create a zero-length key so that we don't
6010 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6011 if( psa_get_key_bits( attributes ) == 0 )
6012 return( PSA_ERROR_INVALID_ARGUMENT );
6013
Gilles Peskinedf179142019-07-15 22:02:14 +02006014 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6015 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006016 if( status != PSA_SUCCESS )
6017 goto exit;
6018
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006019#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6020 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00006021 {
Gilles Peskine11792082019-08-06 18:36:36 +02006022 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
6023 size_t pubkey_length = 0; /* We don't support this feature yet */
6024 if( drv->key_management == NULL ||
6025 drv->key_management->p_generate == NULL )
6026 {
6027 status = PSA_ERROR_NOT_SUPPORTED;
6028 goto exit;
6029 }
6030 status = drv->key_management->p_generate(
6031 psa_get_se_driver_context( driver ),
6032 slot->data.se.slot_number, attributes,
6033 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006034 }
Gilles Peskine11792082019-08-06 18:36:36 +02006035 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006036#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006037 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006038 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02006039 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006040 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006041 }
Gilles Peskine11792082019-08-06 18:36:36 +02006042
6043exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006044 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006045 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006046 if( status != PSA_SUCCESS )
6047 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006048 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006049 *handle = 0;
6050 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006051 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006052}
6053
6054
Gilles Peskinee59236f2018-01-27 23:32:46 +01006055
6056/****************************************************************/
6057/* Module setup */
6058/****************************************************************/
6059
Gilles Peskine5e769522018-11-20 21:59:56 +01006060psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6061 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6062 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6063{
6064 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6065 return( PSA_ERROR_BAD_STATE );
6066 global_data.entropy_init = entropy_init;
6067 global_data.entropy_free = entropy_free;
6068 return( PSA_SUCCESS );
6069}
6070
Gilles Peskinee59236f2018-01-27 23:32:46 +01006071void mbedtls_psa_crypto_free( void )
6072{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006073 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006074 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6075 {
6076 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006077 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006078 }
6079 /* Wipe all remaining data, including configuration.
6080 * In particular, this sets all state indicator to the value
6081 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006082 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006083#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006084 /* Unregister all secure element drivers, so that we restart from
6085 * a pristine state. */
6086 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006087#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006088}
6089
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006090#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6091/** Recover a transaction that was interrupted by a power failure.
6092 *
6093 * This function is called during initialization, before psa_crypto_init()
6094 * returns. If this function returns a failure status, the initialization
6095 * fails.
6096 */
6097static psa_status_t psa_crypto_recover_transaction(
6098 const psa_crypto_transaction_t *transaction )
6099{
6100 switch( transaction->unknown.type )
6101 {
6102 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6103 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006104 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006105 * is implemented.
6106 * https://github.com/ARMmbed/mbed-crypto/issues/218
6107 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006108 default:
6109 /* We found an unsupported transaction in the storage.
6110 * We don't know what state the storage is in. Give up. */
6111 return( PSA_ERROR_STORAGE_FAILURE );
6112 }
6113}
6114#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6115
Gilles Peskinee59236f2018-01-27 23:32:46 +01006116psa_status_t psa_crypto_init( void )
6117{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006118 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006119 const unsigned char drbg_seed[] = "PSA";
6120
Gilles Peskinec6b69072018-11-20 21:42:52 +01006121 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006122 if( global_data.initialized != 0 )
6123 return( PSA_SUCCESS );
6124
Gilles Peskine5e769522018-11-20 21:59:56 +01006125 /* Set default configuration if
6126 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6127 if( global_data.entropy_init == NULL )
6128 global_data.entropy_init = mbedtls_entropy_init;
6129 if( global_data.entropy_free == NULL )
6130 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006131
Gilles Peskinec6b69072018-11-20 21:42:52 +01006132 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006133 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006134#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6135 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6136 /* The PSA entropy injection feature depends on using NV seed as an entropy
6137 * source. Add NV seed as an entropy source for PSA entropy injection. */
6138 mbedtls_entropy_add_source( &global_data.entropy,
6139 mbedtls_nv_seed_poll, NULL,
6140 MBEDTLS_ENTROPY_BLOCK_SIZE,
6141 MBEDTLS_ENTROPY_SOURCE_STRONG );
6142#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006143 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006144 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006145 status = mbedtls_to_psa_error(
6146 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6147 mbedtls_entropy_func,
6148 &global_data.entropy,
6149 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6150 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006151 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006152 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006153
Gilles Peskine66fb1262018-12-10 16:29:04 +01006154 status = psa_initialize_key_slots( );
6155 if( status != PSA_SUCCESS )
6156 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006157
Gilles Peskined9348f22019-10-01 15:22:29 +02006158#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6159 status = psa_init_all_se_drivers( );
6160 if( status != PSA_SUCCESS )
6161 goto exit;
6162#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6163
Gilles Peskine4b734222019-07-24 15:56:31 +02006164#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006165 status = psa_crypto_load_transaction( );
6166 if( status == PSA_SUCCESS )
6167 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006168 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6169 if( status != PSA_SUCCESS )
6170 goto exit;
6171 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006172 }
6173 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6174 {
6175 /* There's no transaction to complete. It's all good. */
6176 status = PSA_SUCCESS;
6177 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006178#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006179
Gilles Peskinec6b69072018-11-20 21:42:52 +01006180 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006181 global_data.initialized = 1;
6182
6183exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006184 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006185 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006186 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006187}
6188
6189#endif /* MBEDTLS_PSA_CRYPTO_C */