blob: c813ca38223bbf456e518ab06dd7e56768a86975 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
itayzafrir7723ab12019-02-14 10:28:02 +020025#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010026#include "psa/crypto.h"
27
Gilles Peskine039b90c2018-12-07 18:24:41 +010028#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010029#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020030#include "psa_crypto_driver_wrappers.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020031#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020032#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020033#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010034#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010035/* Include internal declarations that are useful for implementing persistently
36 * stored keys. */
37#include "psa_crypto_storage.h"
38
Gilles Peskineb46bef22019-07-30 21:32:04 +020039#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include <stdlib.h>
41#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020043#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#define mbedtls_calloc calloc
45#define mbedtls_free free
46#endif
47
Gilles Peskinea5905292018-02-07 20:59:33 +010048#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020049#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000050#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020051#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/blowfish.h"
53#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020054#include "mbedtls/chacha20.h"
55#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/cipher.h"
57#include "mbedtls/ccm.h"
58#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010059#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020061#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010062#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/error.h"
65#include "mbedtls/gcm.h"
66#include "mbedtls/md2.h"
67#include "mbedtls/md4.h"
68#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010069#include "mbedtls/md.h"
70#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010071#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010073#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000074#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/sha1.h"
78#include "mbedtls/sha256.h"
79#include "mbedtls/sha512.h"
80#include "mbedtls/xtea.h"
81
Gilles Peskine996deb12018-08-01 15:45:45 +020082#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
83
Gilles Peskine9ef733f2018-02-07 21:05:37 +010084/* constant-time buffer comparison */
85static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
86{
87 size_t i;
88 unsigned char diff = 0;
89
90 for( i = 0; i < n; i++ )
91 diff |= a[i] ^ b[i];
92
93 return( diff );
94}
95
96
97
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010098/****************************************************************/
99/* Global data, support functions and library management */
100/****************************************************************/
101
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102static int key_type_is_raw_bytes( psa_key_type_t type )
103{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200104 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105}
106
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100107/* Values for psa_global_data_t::rng_state */
108#define RNG_NOT_INITIALIZED 0
109#define RNG_INITIALIZED 1
110#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100111
Gilles Peskine2d277862018-06-18 15:41:12 +0200112typedef struct
113{
Gilles Peskine5e769522018-11-20 21:59:56 +0100114 void (* entropy_init )( mbedtls_entropy_context *ctx );
115 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100116 mbedtls_entropy_context entropy;
117 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100118 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100119 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120} psa_global_data_t;
121
122static psa_global_data_t global_data;
123
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124#define GUARD_MODULE_INITIALIZED \
125 if( global_data.initialized == 0 ) \
126 return( PSA_ERROR_BAD_STATE );
127
Steven Cooreman01164162020-07-20 15:31:37 +0200128psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100129{
Gilles Peskinea5905292018-02-07 20:59:33 +0100130 /* If there's both a high-level code and low-level code, dispatch on
131 * the high-level code. */
132 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133 {
134 case 0:
135 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100136
137 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
138 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
139 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
140 return( PSA_ERROR_NOT_SUPPORTED );
141 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
142 return( PSA_ERROR_HARDWARE_FAILURE );
143
144 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
Gilles Peskine9a944802018-06-21 09:35:35 +0200147 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
148 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
149 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
150 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
151 case MBEDTLS_ERR_ASN1_INVALID_DATA:
152 return( PSA_ERROR_INVALID_ARGUMENT );
153 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
154 return( PSA_ERROR_INSUFFICIENT_MEMORY );
155 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
156 return( PSA_ERROR_BUFFER_TOO_SMALL );
157
Jaeden Amero93e21112019-02-20 13:57:28 +0000158#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000159 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
161 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
162#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
164 return( PSA_ERROR_NOT_SUPPORTED );
165 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
166 return( PSA_ERROR_HARDWARE_FAILURE );
167
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000169 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
171 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
174 return( PSA_ERROR_NOT_SUPPORTED );
175 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
176 return( PSA_ERROR_HARDWARE_FAILURE );
177
178 case MBEDTLS_ERR_CCM_BAD_INPUT:
179 return( PSA_ERROR_INVALID_ARGUMENT );
180 case MBEDTLS_ERR_CCM_AUTH_FAILED:
181 return( PSA_ERROR_INVALID_SIGNATURE );
182 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
183 return( PSA_ERROR_HARDWARE_FAILURE );
184
Gilles Peskine26869f22019-05-06 15:25:00 +0200185 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
186 return( PSA_ERROR_INVALID_ARGUMENT );
187
188 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
189 return( PSA_ERROR_BAD_STATE );
190 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
191 return( PSA_ERROR_INVALID_SIGNATURE );
192
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
194 return( PSA_ERROR_NOT_SUPPORTED );
195 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
198 return( PSA_ERROR_INSUFFICIENT_MEMORY );
199 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
200 return( PSA_ERROR_INVALID_PADDING );
201 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200202 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
205 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200206 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
208 return( PSA_ERROR_HARDWARE_FAILURE );
209
210 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
214 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
215 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
216 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
217 return( PSA_ERROR_NOT_SUPPORTED );
218 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
220
221 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
Gilles Peskinee59236f2018-01-27 23:32:46 +0100226 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
227 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
228 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
229 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100230
231 case MBEDTLS_ERR_GCM_AUTH_FAILED:
232 return( PSA_ERROR_INVALID_SIGNATURE );
233 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200234 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
243 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
244 return( PSA_ERROR_NOT_SUPPORTED );
245 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
246 return( PSA_ERROR_INVALID_ARGUMENT );
247 case MBEDTLS_ERR_MD_ALLOC_FAILED:
248 return( PSA_ERROR_INSUFFICIENT_MEMORY );
249 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
250 return( PSA_ERROR_STORAGE_FAILURE );
251 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
255 return( PSA_ERROR_STORAGE_FAILURE );
256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
261 return( PSA_ERROR_BUFFER_TOO_SMALL );
262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
269 return( PSA_ERROR_INSUFFICIENT_MEMORY );
270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
282 return( PSA_ERROR_NOT_SUPPORTED );
283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
285 return( PSA_ERROR_NOT_PERMITTED );
286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
291 return( PSA_ERROR_NOT_SUPPORTED );
292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
293 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
300 return( PSA_ERROR_NOT_SUPPORTED );
301
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
303 return( PSA_ERROR_HARDWARE_FAILURE );
304
305 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_RSA_INVALID_PADDING:
308 return( PSA_ERROR_INVALID_PADDING );
309 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
312 return( PSA_ERROR_INVALID_ARGUMENT );
313 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
314 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200315 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100316 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
317 return( PSA_ERROR_INVALID_SIGNATURE );
318 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
319 return( PSA_ERROR_BUFFER_TOO_SMALL );
320 case MBEDTLS_ERR_RSA_RNG_FAILED:
321 return( PSA_ERROR_INSUFFICIENT_MEMORY );
322 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
323 return( PSA_ERROR_NOT_SUPPORTED );
324 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
327 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
328 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
329 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
330 return( PSA_ERROR_HARDWARE_FAILURE );
331
332 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
333 return( PSA_ERROR_INVALID_ARGUMENT );
334 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
336
itayzafrir5c753392018-05-08 11:18:38 +0300337 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300338 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300339 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
341 return( PSA_ERROR_BUFFER_TOO_SMALL );
342 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
343 return( PSA_ERROR_NOT_SUPPORTED );
344 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
345 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
346 return( PSA_ERROR_INVALID_SIGNATURE );
347 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
348 return( PSA_ERROR_INSUFFICIENT_MEMORY );
349 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
350 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000351 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
352 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
Gilles Peskine8e338702019-07-30 20:06:31 +0200369 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100374mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100375 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200376{
377 switch( curve )
378 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100379 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100380 switch( byte_length )
381 {
382 case PSA_BITS_TO_BYTES( 192 ):
383 return( MBEDTLS_ECP_DP_SECP192R1 );
384 case PSA_BITS_TO_BYTES( 224 ):
385 return( MBEDTLS_ECP_DP_SECP224R1 );
386 case PSA_BITS_TO_BYTES( 256 ):
387 return( MBEDTLS_ECP_DP_SECP256R1 );
388 case PSA_BITS_TO_BYTES( 384 ):
389 return( MBEDTLS_ECP_DP_SECP384R1 );
390 case PSA_BITS_TO_BYTES( 521 ):
391 return( MBEDTLS_ECP_DP_SECP521R1 );
392 default:
393 return( MBEDTLS_ECP_DP_NONE );
394 }
395 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100396
Paul Elliott8ff510a2020-06-02 17:19:28 +0100397 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100398 switch( byte_length )
399 {
400 case PSA_BITS_TO_BYTES( 256 ):
401 return( MBEDTLS_ECP_DP_BP256R1 );
402 case PSA_BITS_TO_BYTES( 384 ):
403 return( MBEDTLS_ECP_DP_BP384R1 );
404 case PSA_BITS_TO_BYTES( 512 ):
405 return( MBEDTLS_ECP_DP_BP512R1 );
406 default:
407 return( MBEDTLS_ECP_DP_NONE );
408 }
409 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100410
Paul Elliott8ff510a2020-06-02 17:19:28 +0100411 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 switch( byte_length )
413 {
414 case PSA_BITS_TO_BYTES( 255 ):
415 return( MBEDTLS_ECP_DP_CURVE25519 );
416 case PSA_BITS_TO_BYTES( 448 ):
417 return( MBEDTLS_ECP_DP_CURVE448 );
418 default:
419 return( MBEDTLS_ECP_DP_NONE );
420 }
421 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100422
Paul Elliott8ff510a2020-06-02 17:19:28 +0100423 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 switch( byte_length )
425 {
426 case PSA_BITS_TO_BYTES( 192 ):
427 return( MBEDTLS_ECP_DP_SECP192K1 );
428 case PSA_BITS_TO_BYTES( 224 ):
429 return( MBEDTLS_ECP_DP_SECP224K1 );
430 case PSA_BITS_TO_BYTES( 256 ):
431 return( MBEDTLS_ECP_DP_SECP256K1 );
432 default:
433 return( MBEDTLS_ECP_DP_NONE );
434 }
435 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100436
Gilles Peskine12313cd2018-06-20 00:20:32 +0200437 default:
438 return( MBEDTLS_ECP_DP_NONE );
439 }
440}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100441#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200442
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200443static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
444 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445{
446 /* Check that the bit size is acceptable for the key type */
447 switch( type )
448 {
449 case PSA_KEY_TYPE_RAW_DATA:
450#if defined(MBEDTLS_MD_C)
451 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200452#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200453 case PSA_KEY_TYPE_DERIVE:
454 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200455#if defined(MBEDTLS_AES_C)
456 case PSA_KEY_TYPE_AES:
457 if( bits != 128 && bits != 192 && bits != 256 )
458 return( PSA_ERROR_INVALID_ARGUMENT );
459 break;
460#endif
461#if defined(MBEDTLS_CAMELLIA_C)
462 case PSA_KEY_TYPE_CAMELLIA:
463 if( bits != 128 && bits != 192 && bits != 256 )
464 return( PSA_ERROR_INVALID_ARGUMENT );
465 break;
466#endif
467#if defined(MBEDTLS_DES_C)
468 case PSA_KEY_TYPE_DES:
469 if( bits != 64 && bits != 128 && bits != 192 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
471 break;
472#endif
473#if defined(MBEDTLS_ARC4_C)
474 case PSA_KEY_TYPE_ARC4:
475 if( bits < 8 || bits > 2048 )
476 return( PSA_ERROR_INVALID_ARGUMENT );
477 break;
478#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200479#if defined(MBEDTLS_CHACHA20_C)
480 case PSA_KEY_TYPE_CHACHA20:
481 if( bits != 256 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200485 default:
486 return( PSA_ERROR_NOT_SUPPORTED );
487 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200488 if( bits % 8 != 0 )
489 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491 return( PSA_SUCCESS );
492}
493
Steven Cooremana01795d2020-07-24 22:48:15 +0200494#if defined(MBEDTLS_RSA_C)
495
496#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100497/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
498 * that are not a multiple of 8) well. For example, there is only
499 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
500 * way to return the exact bit size of a key.
501 * To keep things simple, reject non-byte-aligned key sizes. */
502static psa_status_t psa_check_rsa_key_byte_aligned(
503 const mbedtls_rsa_context *rsa )
504{
505 mbedtls_mpi n;
506 psa_status_t status;
507 mbedtls_mpi_init( &n );
508 status = mbedtls_to_psa_error(
509 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
510 if( status == PSA_SUCCESS )
511 {
512 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
513 status = PSA_ERROR_NOT_SUPPORTED;
514 }
515 mbedtls_mpi_free( &n );
516 return( status );
517}
Steven Cooremana01795d2020-07-24 22:48:15 +0200518#endif /* MBEDTLS_PK_PARSE_C */
Gilles Peskine86a440b2018-11-12 18:39:40 +0100519
Steven Cooreman7f391872020-07-30 14:57:44 +0200520/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200521 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200522 * \param[in] type The type of key contained in \p data.
523 * \param[in] data The buffer from which to load the representation.
524 * \param[in] data_length The size in bytes of \p data.
525 * \param[out] p_rsa Returns a pointer to an RSA context on success.
526 * The caller is responsible for freeing both the
527 * contents of the context and the context itself
528 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200529 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200530static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
531 const uint8_t *data,
532 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200533 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200534{
Steven Cooremana01795d2020-07-24 22:48:15 +0200535#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000536 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200537 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000538 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200539 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000540
541 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200542 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200544 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200547 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000548 if( status != PSA_SUCCESS )
549 goto exit;
550
551 /* We have something that the pkparse module recognizes. If it is a
552 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200553 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555 status = PSA_ERROR_INVALID_ARGUMENT;
556 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
560 * supports non-byte-aligned key sizes, but not well. For example,
561 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200562 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000563 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
564 {
565 status = PSA_ERROR_NOT_SUPPORTED;
566 goto exit;
567 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200568 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200569 if( status != PSA_SUCCESS )
570 goto exit;
571
Steven Cooremana2371e52020-07-28 14:30:39 +0200572 /* Copy out the pointer to the RSA context, and reset the PK context
573 * such that pk_free doesn't free the RSA context we just grabbed. */
574 *p_rsa = mbedtls_pk_rsa( ctx );
575 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000576
577exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200578 mbedtls_pk_free( &ctx );
579 return( status );
580#else
Steven Cooreman4fed4552020-08-03 14:46:03 +0200581 (void) data;
582 (void) data_length;
Steven Cooreman7f391872020-07-30 14:57:44 +0200583 (void) type;
Steven Cooremana01795d2020-07-24 22:48:15 +0200584 (void) rsa;
585 return( PSA_ERROR_NOT_SUPPORTED );
586#endif /* MBEDTLS_PK_PARSE_C */
587}
588
589/** Export an RSA key to export representation
590 *
591 * \param[in] type The type of key (public/private) to export
592 * \param[in] rsa The internal RSA representation from which to export
593 * \param[out] data The buffer to export to
594 * \param[in] data_size The length of the buffer to export to
595 * \param[out] data_length The amount of bytes written to \p data
596 */
597static psa_status_t psa_export_rsa_key( psa_key_type_t type,
598 mbedtls_rsa_context *rsa,
599 uint8_t *data,
600 size_t data_size,
601 size_t *data_length )
602{
603#if defined(MBEDTLS_PK_WRITE_C)
604 int ret;
605 mbedtls_pk_context pk;
606 uint8_t *pos = data + data_size;
607
608 mbedtls_pk_init( &pk );
609 pk.pk_info = &mbedtls_rsa_info;
610 pk.pk_ctx = rsa;
611
612 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200613 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
614 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200615 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
616 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
617 else
618 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
619
620 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200621 {
622 /* Clean up in case pk_write failed halfway through. */
623 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200624 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200625 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200626
627 /* The mbedtls_pk_xxx functions write to the end of the buffer.
628 * Move the data to the beginning and erase remaining data
629 * at the original location. */
630 if( 2 * (size_t) ret <= data_size )
631 {
632 memcpy( data, data + data_size - ret, ret );
633 memset( data + data_size - ret, 0, ret );
634 }
635 else if( (size_t) ret < data_size )
636 {
637 memmove( data, data + data_size - ret, ret );
638 memset( data + ret, 0, data_size - ret );
639 }
640
641 *data_length = ret;
642 return( PSA_SUCCESS );
643#else
644 (void) type;
645 (void) rsa;
646 (void) data;
647 (void) data_size;
648 (void) data_length;
649 return( PSA_ERROR_NOT_SUPPORTED );
650#endif /* MBEDTLS_PK_WRITE_C */
651}
652
653/** Import an RSA key from import representation to a slot
654 *
655 * \param[in,out] slot The slot where to store the export representation to
656 * \param[in] data The buffer containing the import representation
657 * \param[in] data_length The amount of bytes in \p data
658 */
659static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
660 const uint8_t *data,
661 size_t data_length )
662{
663 psa_status_t status;
664 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200665 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200666
Steven Cooremana01795d2020-07-24 22:48:15 +0200667 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200668 status = psa_load_rsa_representation( slot->attr.type,
669 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200670 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200671 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200672 if( status != PSA_SUCCESS )
673 goto exit;
674
675 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200676 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200677
Steven Cooreman75b74362020-07-28 14:30:13 +0200678 /* Re-export the data to PSA export format, such that we can store export
679 * representation in the key slot. Export representation in case of RSA is
680 * the smallest representation that's allowed as input, so a straight-up
681 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200682 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200683 if( output == NULL )
684 {
685 status = PSA_ERROR_INSUFFICIENT_MEMORY;
686 goto exit;
687 }
688
Steven Cooremana01795d2020-07-24 22:48:15 +0200689 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200690 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200691 output,
692 data_length,
693 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200694exit:
695 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200696 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200697 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200698
699 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000700 if( status != PSA_SUCCESS )
701 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200702 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000703 return( status );
704 }
705
Steven Cooremana01795d2020-07-24 22:48:15 +0200706 /* On success, store the allocated export-formatted key. */
707 slot->data.key.data = output;
708 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000709
710 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200711}
Steven Cooremana01795d2020-07-24 22:48:15 +0200712#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200713
Jaeden Ameroccdce902019-01-10 11:42:27 +0000714#if defined(MBEDTLS_ECP_C)
Steven Cooreman7f391872020-07-30 14:57:44 +0200715/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200716 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200717 * \param[in] type The type of key contained in \p data.
718 * \param[in] data The buffer from which to load the representation.
719 * \param[in] data_length The size in bytes of \p data.
720 * \param[out] p_ecp Returns a pointer to an ECP context on success.
721 * The caller is responsible for freeing both the
722 * contents of the context and the context itself
723 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200724 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200725static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
726 const uint8_t *data,
727 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200728 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100729{
730 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200731 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200732 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200733 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100734
Steven Cooreman4fed4552020-08-03 14:46:03 +0200735 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
736 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100737 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200738 /* A Weierstrass public key is represented as:
739 * - The byte 0x04;
740 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
741 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200742 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200743 */
744 if( ( data_length & 1 ) == 0 )
745 return( PSA_ERROR_INVALID_ARGUMENT );
746 curve_size = data_length / 2;
747
748 /* Montgomery public keys are represented in compressed format, meaning
749 * their curve_size is equal to the amount of input. */
750
751 /* Private keys are represented in uncompressed private random integer
752 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100753 }
754
Steven Cooreman6d839f02020-07-30 11:36:45 +0200755 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200756 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200757 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200758 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200759 mbedtls_ecp_keypair_init( ecp );
760
Gilles Peskine4cd32772019-12-02 20:49:42 +0100761 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200762 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
763 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100764 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200765 {
766 status = PSA_ERROR_INVALID_ARGUMENT;
767 goto exit;
768 }
769
Steven Cooremanacda8342020-07-24 23:09:52 +0200770 status = mbedtls_to_psa_error(
771 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
772 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200773 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200774
Steven Cooreman6d839f02020-07-30 11:36:45 +0200775 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200776 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200777 {
778 /* Load the public value. */
779 status = mbedtls_to_psa_error(
780 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200781 data,
782 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200783 if( status != PSA_SUCCESS )
784 goto exit;
785
786 /* Check that the point is on the curve. */
787 status = mbedtls_to_psa_error(
788 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
789 if( status != PSA_SUCCESS )
790 goto exit;
791 }
792 else
793 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200794 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200795 status = mbedtls_to_psa_error(
796 mbedtls_ecp_read_key( ecp->grp.id,
797 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200798 data,
799 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200800 if( status != PSA_SUCCESS )
801 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200802 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200803
804 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200805exit:
806 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200807 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200808 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200809 mbedtls_free( ecp );
810 }
811
Steven Cooreman29149862020-08-05 15:43:42 +0200812 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100813}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000814
Steven Cooreman4fed4552020-08-03 14:46:03 +0200815/** Export an ECP key to export representation
816 *
817 * \param[in] type The type of key (public/private) to export
818 * \param[in] ecp The internal ECP representation from which to export
819 * \param[out] data The buffer to export to
820 * \param[in] data_size The length of the buffer to export to
821 * \param[out] data_length The amount of bytes written to \p data
822 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200823static psa_status_t psa_export_ecp_key( psa_key_type_t type,
824 mbedtls_ecp_keypair *ecp,
825 uint8_t *data,
826 size_t data_size,
827 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200828{
Steven Cooremanacda8342020-07-24 23:09:52 +0200829 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000830
Steven Cooremanacda8342020-07-24 23:09:52 +0200831 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200832 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200833 /* Check whether the public part is loaded */
834 if( mbedtls_ecp_is_zero( &ecp->Q ) )
835 {
836 /* Calculate the public key */
837 status = mbedtls_to_psa_error(
838 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
839 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
840 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200841 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200842 }
843
Steven Cooreman4fed4552020-08-03 14:46:03 +0200844 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200845 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
846 MBEDTLS_ECP_PF_UNCOMPRESSED,
847 data_length,
848 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200849 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200850 if( status != PSA_SUCCESS )
851 memset( data, 0, data_size );
852
Steven Cooreman29149862020-08-05 15:43:42 +0200853 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200854 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200855 else
856 {
Steven Cooreman29149862020-08-05 15:43:42 +0200857 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200858 return( PSA_ERROR_BUFFER_TOO_SMALL );
859
860 status = mbedtls_to_psa_error(
861 mbedtls_ecp_write_key( ecp,
862 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200863 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200864 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200865 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200866 else
867 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200868
869 return( status );
870 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200871}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200872
Steven Cooreman4fed4552020-08-03 14:46:03 +0200873/** Import an ECP key from import representation to a slot
874 *
875 * \param[in,out] slot The slot where to store the export representation to
876 * \param[in] data The buffer containing the import representation
877 * \param[in] data_length The amount of bytes in \p data
878 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200879static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
880 const uint8_t *data,
881 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100882{
Steven Cooremanacda8342020-07-24 23:09:52 +0200883 psa_status_t status;
884 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200885 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100886
Steven Cooremanacda8342020-07-24 23:09:52 +0200887 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200888 status = psa_load_ecp_representation( slot->attr.type,
889 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200890 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200891 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100892 if( status != PSA_SUCCESS )
893 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100894
Steven Cooremanacda8342020-07-24 23:09:52 +0200895 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200896 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200897 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200898 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200899
Steven Cooremanacda8342020-07-24 23:09:52 +0200900 /* Re-export the data to PSA export format. There is currently no support
901 * for other input formats then the export format, so this is a 1-1
902 * copy operation. */
903 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200904 if( output == NULL )
905 {
906 status = PSA_ERROR_INSUFFICIENT_MEMORY;
907 goto exit;
908 }
909
910 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200911 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200912 output,
913 data_length,
914 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100915exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200916 /* Always free the PK object (will also free contained ECP context) */
917 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200918 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200919
920 /* Free the allocated buffer only on error. */
921 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100922 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200923 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200924 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100925 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200926
927 /* On success, store the allocated export-formatted key. */
928 slot->data.key.data = output;
929 slot->data.key.bytes = data_length;
930
931 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100932}
933#endif /* defined(MBEDTLS_ECP_C) */
934
Gilles Peskineb46bef22019-07-30 21:32:04 +0200935/** Return the size of the key in the given slot, in bits.
936 *
937 * \param[in] slot A key slot.
938 *
939 * \return The key size in bits, read from the metadata in the slot.
940 */
941static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
942{
943 return( slot->attr.bits );
944}
945
Steven Cooreman75b74362020-07-28 14:30:13 +0200946/** Try to allocate a buffer to an empty key slot.
947 *
948 * \param[in,out] slot Key slot to attach buffer to.
949 * \param[in] buffer_length Requested size of the buffer.
950 *
951 * \retval #PSA_SUCCESS
952 * The buffer has been successfully allocated.
953 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
954 * Not enough memory was available for allocation.
955 * \retval #PSA_ERROR_ALREADY_EXISTS
956 * Trying to allocate a buffer to a non-empty key slot.
957 */
958static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
959 size_t buffer_length )
960{
961 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200962 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200963
964 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
965 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200966 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200967
968 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200969 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200970}
971
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200972psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
973 const uint8_t* data,
974 size_t data_length )
975{
976 psa_status_t status = psa_allocate_buffer_to_slot( slot,
977 data_length );
978 if( status != PSA_SUCCESS )
979 return( status );
980
981 memcpy( slot->data.key.data, data, data_length );
982 return( PSA_SUCCESS );
983}
984
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200985/** Import key data into a slot.
986 *
987 * `slot->type` must have been set previously.
988 * This function assumes that the slot does not contain any key material yet.
989 * On failure, the slot content is unchanged.
990 *
991 * Persistent storage is not affected.
992 *
993 * \param[in,out] slot The key slot to import data into.
994 * Its `type` field must have previously been set to
995 * the desired key type.
996 * It must not contain any key material yet.
997 * \param[in] data Buffer containing the key material to parse and import.
998 * \param data_length Size of \p data in bytes.
999 *
1000 * \retval #PSA_SUCCESS
1001 * \retval #PSA_ERROR_INVALID_ARGUMENT
1002 * \retval #PSA_ERROR_NOT_SUPPORTED
1003 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1004 */
1005static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1006 const uint8_t *data,
1007 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001009 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001010 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001012 /* zero-length keys are never supported. */
1013 if( data_length == 0 )
1014 return( PSA_ERROR_NOT_SUPPORTED );
1015
Gilles Peskine8e338702019-07-30 20:06:31 +02001016 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017 {
Steven Cooreman04524762020-10-13 17:43:44 +02001018 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001019
Steven Cooreman75b74362020-07-28 14:30:13 +02001020 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1021 if( data_length > SIZE_MAX / 8 )
1022 return( PSA_ERROR_NOT_SUPPORTED );
1023
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001024 /* Enforce a size limit, and in particular ensure that the bit
1025 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001026 if( bit_size > PSA_MAX_KEY_BITS )
1027 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001028
1029 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001030 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001031 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001032
1033 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001034 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001035 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001036 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001037
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001038 /* Write the actual key size to the slot.
1039 * psa_start_key_creation() wrote the size declared by the
1040 * caller, which may be 0 (meaning unspecified) or wrong. */
1041 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001042
1043 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001044 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001045 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001046 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001047 /* Try validation through accelerators first. */
1048 bit_size = slot->attr.bits;
1049 psa_key_attributes_t attributes = {
1050 .core = slot->attr
1051 };
1052 status = psa_driver_wrapper_validate_key( &attributes,
1053 data,
1054 data_length,
1055 &bit_size );
1056 if( status == PSA_SUCCESS )
1057 {
1058 /* Key has been validated successfully by an accelerator.
1059 * Copy key material into slot. */
1060 status = psa_copy_key_material_into_slot( slot, data, data_length );
1061 if( status != PSA_SUCCESS )
1062 return( status );
1063
1064 slot->attr.bits = (psa_key_bits_t) bit_size;
1065 return( PSA_SUCCESS );
1066 }
1067 else if( status != PSA_ERROR_NOT_SUPPORTED )
1068 return( status );
1069
1070 /* Key format is not supported by any accelerator, try software fallback
1071 * if present. */
Gilles Peskinef76aa772018-10-29 19:24:33 +01001072#if defined(MBEDTLS_ECP_C)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001073 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1074 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001075 return( psa_import_ecp_key( slot, data, data_length ) );
1076 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001077#endif /* defined(MBEDTLS_ECP_C) */
Steven Cooreman19fd5742020-07-24 23:31:01 +02001078#if defined(MBEDTLS_RSA_C)
Steven Cooreman40120f62020-10-29 11:42:22 +01001079 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001080 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001081 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001082 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001083#endif /* defined(MBEDTLS_RSA_C) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001084
1085 /* Fell through the fallback as well, so have nothing else to try. */
1086 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001087 }
1088 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001089 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001090 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001092 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001093}
1094
Gilles Peskinef603c712019-01-19 13:40:11 +01001095/** Calculate the intersection of two algorithm usage policies.
1096 *
1097 * Return 0 (which allows no operation) on incompatibility.
1098 */
1099static psa_algorithm_t psa_key_policy_algorithm_intersection(
1100 psa_algorithm_t alg1,
1101 psa_algorithm_t alg2 )
1102{
Gilles Peskine549ea862019-05-22 11:45:59 +02001103 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001104 if( alg1 == alg2 )
1105 return( alg1 );
1106 /* If the policies are from the same hash-and-sign family, check
1107 * if one is a wildcard. If so the other has the specific algorithm. */
1108 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1109 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1110 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1111 {
1112 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1113 return( alg2 );
1114 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1115 return( alg1 );
1116 }
1117 /* If the policies are incompatible, allow nothing. */
1118 return( 0 );
1119}
1120
Gilles Peskined6f371b2019-05-10 19:33:38 +02001121static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1122 psa_algorithm_t requested_alg )
1123{
Gilles Peskine549ea862019-05-22 11:45:59 +02001124 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001125 if( requested_alg == policy_alg )
1126 return( 1 );
1127 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001128 * and requested_alg is the same hash-and-sign family with any hash,
1129 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001130 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1131 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1132 {
1133 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1134 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1135 }
Steven Cooremance48e852020-10-05 16:02:45 +02001136 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001137 * a key derivation with that key agreement should also be allowed. This
1138 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001139 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1140 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1141 {
1142 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1143 policy_alg );
1144 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001145 /* If it isn't permitted, it's forbidden. */
1146 return( 0 );
1147}
1148
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001149/** Test whether a policy permits an algorithm.
1150 *
1151 * The caller must test usage flags separately.
1152 */
1153static int psa_key_policy_permits( const psa_key_policy_t *policy,
1154 psa_algorithm_t alg )
1155{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001156 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1157 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001158}
1159
Gilles Peskinef603c712019-01-19 13:40:11 +01001160/** Restrict a key policy based on a constraint.
1161 *
1162 * \param[in,out] policy The policy to restrict.
1163 * \param[in] constraint The policy constraint to apply.
1164 *
1165 * \retval #PSA_SUCCESS
1166 * \c *policy contains the intersection of the original value of
1167 * \c *policy and \c *constraint.
1168 * \retval #PSA_ERROR_INVALID_ARGUMENT
1169 * \c *policy and \c *constraint are incompatible.
1170 * \c *policy is unchanged.
1171 */
1172static psa_status_t psa_restrict_key_policy(
1173 psa_key_policy_t *policy,
1174 const psa_key_policy_t *constraint )
1175{
1176 psa_algorithm_t intersection_alg =
1177 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001178 psa_algorithm_t intersection_alg2 =
1179 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001180 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1181 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001182 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1183 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001184 policy->usage &= constraint->usage;
1185 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001186 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001187 return( PSA_SUCCESS );
1188}
1189
Darryl Green06fd18d2018-07-16 11:21:11 +01001190/** Retrieve a slot which must contain a key. The key must have allow all the
1191 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1192 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001193static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001194 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001195 psa_key_usage_t usage,
1196 psa_algorithm_t alg )
1197{
1198 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001199 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001200
1201 *p_slot = NULL;
1202
Gilles Peskinec5487a82018-12-03 18:08:14 +01001203 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001204 if( status != PSA_SUCCESS )
1205 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001206
1207 /* Enforce that usage policy for the key slot contains all the flags
1208 * required by the usage parameter. There is one exception: public
1209 * keys can always be exported, so we treat public key objects as
1210 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001211 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001212 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001213 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001214 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001215
1216 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001217 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001218 return( PSA_ERROR_NOT_PERMITTED );
1219
1220 *p_slot = slot;
1221 return( PSA_SUCCESS );
1222}
Darryl Green940d72c2018-07-13 13:18:51 +01001223
Gilles Peskine28f8f302019-07-24 13:30:31 +02001224/** Retrieve a slot which must contain a transparent key.
1225 *
1226 * A transparent key is a key for which the key material is directly
1227 * available, as opposed to a key in a secure element.
1228 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001229 * This is a temporary function to use instead of psa_get_key_from_slot()
1230 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001231 */
1232#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1233static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1234 psa_key_slot_t **p_slot,
1235 psa_key_usage_t usage,
1236 psa_algorithm_t alg )
1237{
1238 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1239 if( status != PSA_SUCCESS )
1240 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001241 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001242 {
1243 *p_slot = NULL;
1244 return( PSA_ERROR_NOT_SUPPORTED );
1245 }
1246 return( PSA_SUCCESS );
1247}
1248#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1249/* With no secure element support, all keys are transparent. */
1250#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1251 psa_get_key_from_slot( handle, p_slot, usage, alg )
1252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1253
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001254/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001255static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001256{
Gilles Peskine73167e12019-07-12 23:44:37 +02001257#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1258 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001259 {
1260 /* No key material to clean. */
1261 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001262 else
1263#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001264 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001265 /* Data pointer will always be either a valid pointer or NULL in an
1266 * initialized slot, so we can just free it. */
1267 if( slot->data.key.data != NULL )
1268 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001269 mbedtls_free( slot->data.key.data );
1270 slot->data.key.data = NULL;
1271 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001272 }
Darryl Green40225ba2018-11-15 14:48:15 +00001273
1274 return( PSA_SUCCESS );
1275}
1276
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001277/** Completely wipe a slot in memory, including its policy.
1278 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001279psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001280{
1281 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001282 /* Multipart operations may still be using the key. This is safe
1283 * because all multipart operation objects are independent from
1284 * the key slot: if they need to access the key after the setup
1285 * phase, they have a copy of the key. Note that this means that
1286 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001287 /* At this point, key material and other type-specific content has
1288 * been wiped. Clear remaining metadata. We can call memset and not
1289 * zeroize because the metadata is not particularly sensitive. */
1290 memset( slot, 0, sizeof( *slot ) );
1291 return( status );
1292}
1293
Gilles Peskinec5487a82018-12-03 18:08:14 +01001294psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001295{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001296 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001297 psa_status_t status; /* status of the last operation */
1298 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001299#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1300 psa_se_drv_table_entry_t *driver;
1301#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001302
Gilles Peskine1841cf42019-10-08 15:48:25 +02001303 if( handle == 0 )
1304 return( PSA_SUCCESS );
1305
Gilles Peskinec5487a82018-12-03 18:08:14 +01001306 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001307 if( status != PSA_SUCCESS )
1308 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001309
1310#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001311 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001312 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001313 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001314 /* For a key in a secure element, we need to do three things:
1315 * remove the key file in internal storage, destroy the
1316 * key inside the secure element, and update the driver's
1317 * persistent data. Start a transaction that will encompass these
1318 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001319 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001320 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001321 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001322 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001323 status = psa_crypto_save_transaction( );
1324 if( status != PSA_SUCCESS )
1325 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001326 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001327 /* We should still try to destroy the key in the secure
1328 * element and the key metadata in storage. This is especially
1329 * important if the error is that the storage is full.
1330 * But how to do it exactly without risking an inconsistent
1331 * state after a reset?
1332 * https://github.com/ARMmbed/mbed-crypto/issues/215
1333 */
1334 overall_status = status;
1335 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001336 }
1337
Gilles Peskine354f7672019-07-12 23:46:38 +02001338 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001339 if( overall_status == PSA_SUCCESS )
1340 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001341 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001342#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1343
Darryl Greend49a4992018-06-18 17:27:26 +01001344#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001345 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001346 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001347 status = psa_destroy_persistent_key( slot->attr.id );
1348 if( overall_status == PSA_SUCCESS )
1349 overall_status = status;
1350
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001351 /* TODO: other slots may have a copy of the same key. We should
1352 * invalidate them.
1353 * https://github.com/ARMmbed/mbed-crypto/issues/214
1354 */
Darryl Greend49a4992018-06-18 17:27:26 +01001355 }
1356#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001357
Gilles Peskinefc762652019-07-22 19:30:34 +02001358#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1359 if( driver != NULL )
1360 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001361 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001362 if( overall_status == PSA_SUCCESS )
1363 overall_status = status;
1364 status = psa_crypto_stop_transaction( );
1365 if( overall_status == PSA_SUCCESS )
1366 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001367 }
1368#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1369
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1371exit:
1372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001373 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001374 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1375 if( overall_status == PSA_SUCCESS )
1376 overall_status = status;
1377 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001378}
1379
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001380void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001381{
Gilles Peskineb699f072019-04-26 16:06:02 +02001382 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001383 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001384}
1385
Gilles Peskineb699f072019-04-26 16:06:02 +02001386psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1387 psa_key_type_t type,
1388 const uint8_t *data,
1389 size_t data_length )
1390{
1391 uint8_t *copy = NULL;
1392
1393 if( data_length != 0 )
1394 {
1395 copy = mbedtls_calloc( 1, data_length );
1396 if( copy == NULL )
1397 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1398 memcpy( copy, data, data_length );
1399 }
1400 /* After this point, this function is guaranteed to succeed, so it
1401 * can start modifying `*attributes`. */
1402
1403 if( attributes->domain_parameters != NULL )
1404 {
1405 mbedtls_free( attributes->domain_parameters );
1406 attributes->domain_parameters = NULL;
1407 attributes->domain_parameters_size = 0;
1408 }
1409
1410 attributes->domain_parameters = copy;
1411 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001412 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001413 return( PSA_SUCCESS );
1414}
1415
1416psa_status_t psa_get_key_domain_parameters(
1417 const psa_key_attributes_t *attributes,
1418 uint8_t *data, size_t data_size, size_t *data_length )
1419{
1420 if( attributes->domain_parameters_size > data_size )
1421 return( PSA_ERROR_BUFFER_TOO_SMALL );
1422 *data_length = attributes->domain_parameters_size;
1423 if( attributes->domain_parameters_size != 0 )
1424 memcpy( data, attributes->domain_parameters,
1425 attributes->domain_parameters_size );
1426 return( PSA_SUCCESS );
1427}
1428
1429#if defined(MBEDTLS_RSA_C)
1430static psa_status_t psa_get_rsa_public_exponent(
1431 const mbedtls_rsa_context *rsa,
1432 psa_key_attributes_t *attributes )
1433{
1434 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001436 uint8_t *buffer = NULL;
1437 size_t buflen;
1438 mbedtls_mpi_init( &mpi );
1439
1440 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1441 if( ret != 0 )
1442 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001443 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1444 {
1445 /* It's the default value, which is reported as an empty string,
1446 * so there's nothing to do. */
1447 goto exit;
1448 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001449
1450 buflen = mbedtls_mpi_size( &mpi );
1451 buffer = mbedtls_calloc( 1, buflen );
1452 if( buffer == NULL )
1453 {
1454 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1455 goto exit;
1456 }
1457 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1458 if( ret != 0 )
1459 goto exit;
1460 attributes->domain_parameters = buffer;
1461 attributes->domain_parameters_size = buflen;
1462
1463exit:
1464 mbedtls_mpi_free( &mpi );
1465 if( ret != 0 )
1466 mbedtls_free( buffer );
1467 return( mbedtls_to_psa_error( ret ) );
1468}
1469#endif /* MBEDTLS_RSA_C */
1470
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001471/** Retrieve all the publicly-accessible attributes of a key.
1472 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001473psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1474 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001475{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001476 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001477 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001478
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001479 psa_reset_key_attributes( attributes );
1480
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001481 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001482 if( status != PSA_SUCCESS )
1483 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001484
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001485 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001486 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1487 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1488
1489#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1490 if( psa_key_slot_is_external( slot ) )
1491 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1492#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001493
Gilles Peskine8e338702019-07-30 20:06:31 +02001494 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001495 {
1496#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001497 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001498 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001499#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001500 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001501 * is not yet implemented.
1502 * https://github.com/ARMmbed/mbed-crypto/issues/216
1503 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001504 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001505 break;
1506#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001507 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001508 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001509
Steven Cooreman4fed4552020-08-03 14:46:03 +02001510 status = psa_load_rsa_representation( slot->attr.type,
1511 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001512 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001513 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001514 if( status != PSA_SUCCESS )
1515 break;
1516
Steven Cooremana2371e52020-07-28 14:30:39 +02001517 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001518 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001519 mbedtls_rsa_free( rsa );
1520 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001521 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001522 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001523#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001524 default:
1525 /* Nothing else to do. */
1526 break;
1527 }
1528
1529 if( status != PSA_SUCCESS )
1530 psa_reset_key_attributes( attributes );
1531 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001532}
1533
Gilles Peskinec8000c02019-08-02 20:15:51 +02001534#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1535psa_status_t psa_get_key_slot_number(
1536 const psa_key_attributes_t *attributes,
1537 psa_key_slot_number_t *slot_number )
1538{
1539 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1540 {
1541 *slot_number = attributes->slot_number;
1542 return( PSA_SUCCESS );
1543 }
1544 else
1545 return( PSA_ERROR_INVALID_ARGUMENT );
1546}
1547#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1548
Steven Cooremana01795d2020-07-24 22:48:15 +02001549static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1550 uint8_t *data,
1551 size_t data_size,
1552 size_t *data_length )
1553{
1554 if( slot->data.key.bytes > data_size )
1555 return( PSA_ERROR_BUFFER_TOO_SMALL );
1556 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1557 memset( data + slot->data.key.bytes, 0,
1558 data_size - slot->data.key.bytes );
1559 *data_length = slot->data.key.bytes;
1560 return( PSA_SUCCESS );
1561}
1562
Gilles Peskinef603c712019-01-19 13:40:11 +01001563static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1564 uint8_t *data,
1565 size_t data_size,
1566 size_t *data_length,
1567 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001568{
Gilles Peskine5d309672019-07-12 23:47:28 +02001569#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1570 const psa_drv_se_t *drv;
1571 psa_drv_se_context_t *drv_context;
1572#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1573
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001574 *data_length = 0;
1575
Gilles Peskine8e338702019-07-30 20:06:31 +02001576 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001577 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001578
Gilles Peskinef9168942019-09-12 19:20:29 +02001579 /* Reject a zero-length output buffer now, since this can never be a
1580 * valid key representation. This way we know that data must be a valid
1581 * pointer and we can do things like memset(data, ..., data_size). */
1582 if( data_size == 0 )
1583 return( PSA_ERROR_BUFFER_TOO_SMALL );
1584
Gilles Peskine5d309672019-07-12 23:47:28 +02001585#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001586 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001587 {
1588 psa_drv_se_export_key_t method;
1589 if( drv->key_management == NULL )
1590 return( PSA_ERROR_NOT_SUPPORTED );
1591 method = ( export_public_key ?
1592 drv->key_management->p_export_public :
1593 drv->key_management->p_export );
1594 if( method == NULL )
1595 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001596 return( method( drv_context,
1597 slot->data.se.slot_number,
1598 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001599 }
1600#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1601
Gilles Peskine8e338702019-07-30 20:06:31 +02001602 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001604 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001605 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001606 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1607 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001608 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001609 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001610 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001611 /* Exporting public -> public */
1612 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1613 }
1614 else if( !export_public_key )
1615 {
1616 /* Exporting private -> private */
1617 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1618 }
1619 /* Need to export the public part of a private key,
1620 * so conversion is needed */
1621 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1622 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001623#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001624 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001625 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001626 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001627 slot->data.key.data,
1628 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001629 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001630 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001631 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001632
Steven Cooreman560c28a2020-07-24 23:20:24 +02001633 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001634 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001635 data,
1636 data_size,
1637 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001638
Steven Cooremana2371e52020-07-28 14:30:39 +02001639 mbedtls_rsa_free( rsa );
1640 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001641
Steven Cooreman560c28a2020-07-24 23:20:24 +02001642 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001643#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001644 /* We don't know how to convert a private RSA key to public. */
1645 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001646#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001647 }
1648 else
Moran Pekera998bc62018-04-16 18:16:20 +03001649 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001650#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001651 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001652 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001653 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001654 slot->data.key.data,
1655 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001656 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001657 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001658 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001659
1660 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1661 PSA_KEY_TYPE_ECC_GET_FAMILY(
1662 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001663 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001664 data,
1665 data_size,
1666 data_length );
1667
Steven Cooremana2371e52020-07-28 14:30:39 +02001668 mbedtls_ecp_keypair_free( ecp );
1669 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001670 return( status );
1671#else
1672 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001673 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001674#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001675 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001676 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001677 else
1678 {
1679 /* This shouldn't happen in the reference implementation, but
1680 it is valid for a special-purpose implementation to omit
1681 support for exporting certain key types. */
1682 return( PSA_ERROR_NOT_SUPPORTED );
1683 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001684}
1685
Gilles Peskinec5487a82018-12-03 18:08:14 +01001686psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001687 uint8_t *data,
1688 size_t data_size,
1689 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001690{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001691 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001692 psa_status_t status;
1693
1694 /* Set the key to empty now, so that even when there are errors, we always
1695 * set data_length to a value between 0 and data_size. On error, setting
1696 * the key to empty is a good choice because an empty key representation is
1697 * unlikely to be accepted anywhere. */
1698 *data_length = 0;
1699
1700 /* Export requires the EXPORT flag. There is an exception for public keys,
1701 * which don't require any flag, but psa_get_key_from_slot takes
1702 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001703 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001704 if( status != PSA_SUCCESS )
1705 return( status );
1706 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001707 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001708}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001709
Gilles Peskinec5487a82018-12-03 18:08:14 +01001710psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001711 uint8_t *data,
1712 size_t data_size,
1713 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001714{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001715 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001716 psa_status_t status;
1717
1718 /* Set the key to empty now, so that even when there are errors, we always
1719 * set data_length to a value between 0 and data_size. On error, setting
1720 * the key to empty is a good choice because an empty key representation is
1721 * unlikely to be accepted anywhere. */
1722 *data_length = 0;
1723
1724 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001725 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001726 if( status != PSA_SUCCESS )
1727 return( status );
1728 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001729 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001730}
1731
Gilles Peskine91e8c332019-08-02 19:19:39 +02001732#if defined(static_assert)
1733static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1734 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001735static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001736 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001737static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001738 "One or more key attribute flag is listed as both internal-only and external-only" );
1739#endif
1740
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001741/** Validate that a key policy is internally well-formed.
1742 *
1743 * This function only rejects invalid policies. It does not validate the
1744 * consistency of the policy with respect to other attributes of the key
1745 * such as the key type.
1746 */
1747static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001748{
1749 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001750 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001751 PSA_KEY_USAGE_ENCRYPT |
1752 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001753 PSA_KEY_USAGE_SIGN_HASH |
1754 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001755 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1756 return( PSA_ERROR_INVALID_ARGUMENT );
1757
Gilles Peskine4747d192019-04-17 15:05:45 +02001758 return( PSA_SUCCESS );
1759}
1760
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001761/** Validate the internal consistency of key attributes.
1762 *
1763 * This function only rejects invalid attribute values. If does not
1764 * validate the consistency of the attributes with any key data that may
1765 * be involved in the creation of the key.
1766 *
1767 * Call this function early in the key creation process.
1768 *
1769 * \param[in] attributes Key attributes for the new key.
1770 * \param[out] p_drv On any return, the driver for the key, if any.
1771 * NULL for a transparent key.
1772 *
1773 */
1774static psa_status_t psa_validate_key_attributes(
1775 const psa_key_attributes_t *attributes,
1776 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001777{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001778 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001779 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001780
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001781 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1782 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001783 if( status != PSA_SUCCESS )
1784 return( status );
1785
Ronald Crond2ed4812020-07-17 16:11:30 +02001786 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001787 if( status != PSA_SUCCESS )
1788 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001789
Ronald Crond2ed4812020-07-17 16:11:30 +02001790 /* Validate the key identifier only in the case of a persistent key. */
1791 if ( ! PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1792 {
1793 status = psa_validate_key_id(
1794 psa_get_key_id( attributes ),
1795 psa_key_lifetime_is_external( lifetime ) );
1796
1797 if( status != PSA_SUCCESS )
1798 return( status );
1799 }
1800
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001801 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001802 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001803 return( status );
1804
1805 /* Refuse to create overly large keys.
1806 * Note that this doesn't trigger on import if the attributes don't
1807 * explicitly specify a size (so psa_get_key_bits returns 0), so
1808 * psa_import_key() needs its own checks. */
1809 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1810 return( PSA_ERROR_NOT_SUPPORTED );
1811
Gilles Peskine91e8c332019-08-02 19:19:39 +02001812 /* Reject invalid flags. These should not be reachable through the API. */
1813 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1814 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1815 return( PSA_ERROR_INVALID_ARGUMENT );
1816
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001817 return( PSA_SUCCESS );
1818}
1819
Gilles Peskine4747d192019-04-17 15:05:45 +02001820/** Prepare a key slot to receive key material.
1821 *
1822 * This function allocates a key slot and sets its metadata.
1823 *
1824 * If this function fails, call psa_fail_key_creation().
1825 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001826 * This function is intended to be used as follows:
1827 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1828 * it with the specified attributes, and assign it a handle.
1829 * -# Populate the slot with the key material.
1830 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1831 * In case of failure at any step, stop the sequence and call
1832 * psa_fail_key_creation().
1833 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001834 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001835 * \param[in] attributes Key attributes for the new key.
1836 * \param[out] handle On success, a handle for the allocated slot.
1837 * \param[out] p_slot On success, a pointer to the prepared slot.
1838 * \param[out] p_drv On any return, the driver for the key, if any.
1839 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001840 *
1841 * \retval #PSA_SUCCESS
1842 * The key slot is ready to receive key material.
1843 * \return If this function fails, the key slot is an invalid state.
1844 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001845 */
1846static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001847 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001848 const psa_key_attributes_t *attributes,
1849 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001850 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001851 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001852{
1853 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001854 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001855 psa_key_slot_t *slot;
1856
Gilles Peskinedf179142019-07-15 22:02:14 +02001857 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001858 *p_drv = NULL;
1859
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001860 status = psa_validate_key_attributes( attributes, p_drv );
1861 if( status != PSA_SUCCESS )
1862 return( status );
1863
Ronald Cron2a993152020-07-17 14:13:26 +02001864 status = psa_get_empty_key_slot( handle, &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001865 if( status != PSA_SUCCESS )
1866 return( status );
1867 slot = *p_slot;
1868
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001869 /* We're storing the declared bit-size of the key. It's up to each
1870 * creation mechanism to verify that this information is correct.
1871 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001872 * an input (generate, device) but not for those where the bit-size
1873 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001874
1875 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001876
Gilles Peskine91e8c332019-08-02 19:19:39 +02001877 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001878 * external-only flags, query `attributes`. Thanks to the check
1879 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001880 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001881 * may have set. */
1882 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001883
Gilles Peskinecbaff462019-07-12 23:46:04 +02001884#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001885 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001886 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001887 * create the key file in internal storage, create the
1888 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001889 * persistent data. This is done by starting a transaction that will
1890 * encompass these three actions.
1891 * For registering a volatile key, we just need to find an appropriate
1892 * slot number inside the SE. Since the key is designated volatile, creating
1893 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001894 /* The first thing to do is to find a slot number for the new key.
1895 * We save the slot number in persistent storage as part of the
1896 * transaction data. It will be needed to recover if the power
1897 * fails during the key creation process, to clean up on the secure
1898 * element side after restarting. Obtaining a slot number from the
1899 * secure element driver updates its persistent state, but we do not yet
1900 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001901 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001902 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001903 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001904 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001905 &slot->data.se.slot_number );
1906 if( status != PSA_SUCCESS )
1907 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001908
1909 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001910 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001911 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1912 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1913 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1914 psa_crypto_transaction.key.id = slot->attr.id;
1915 status = psa_crypto_save_transaction( );
1916 if( status != PSA_SUCCESS )
1917 {
1918 (void) psa_crypto_stop_transaction( );
1919 return( status );
1920 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001921 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001922 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001923
1924 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1925 {
1926 /* Key registration only makes sense with a secure element. */
1927 return( PSA_ERROR_INVALID_ARGUMENT );
1928 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001929#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1930
Darryl Green0c6575a2018-11-07 16:05:30 +00001931 return( status );
1932}
Gilles Peskine4747d192019-04-17 15:05:45 +02001933
1934/** Finalize the creation of a key once its key material has been set.
1935 *
1936 * This entails writing the key to persistent storage.
1937 *
1938 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001939 * See the documentation of psa_start_key_creation() for the intended use
1940 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001941 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001942 * \param[in,out] slot Pointer to the slot with key material.
1943 * \param[in] driver The secure element driver for the key,
1944 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001945 *
1946 * \retval #PSA_SUCCESS
1947 * The key was successfully created. The handle is now valid.
1948 * \return If this function fails, the key slot is an invalid state.
1949 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001950 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001951static psa_status_t psa_finish_key_creation(
1952 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001953 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001954{
1955 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001956 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001957 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001958
1959#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001960 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001961 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001962#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1963 if( driver != NULL )
1964 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001965 psa_se_key_data_storage_t data;
1966#if defined(static_assert)
1967 static_assert( sizeof( slot->data.se.slot_number ) ==
1968 sizeof( data.slot_number ),
1969 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001970#endif
1971 memcpy( &data.slot_number, &slot->data.se.slot_number,
1972 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001973 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001974 (uint8_t*) &data,
1975 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001976 }
1977 else
1978#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1979 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001980 /* Key material is saved in export representation in the slot, so
1981 * just pass the slot buffer for storage. */
1982 status = psa_save_persistent_key( &slot->attr,
1983 slot->data.key.data,
1984 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001985 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001986 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001987#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1988
Gilles Peskinecbaff462019-07-12 23:46:04 +02001989#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001990 /* Finish the transaction for a key creation. This does not
1991 * happen when registering an existing key. Detect this case
1992 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001993 * creation of a persistent key in a secure element requires a transaction,
1994 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001995 if( driver != NULL &&
1996 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001997 {
1998 status = psa_save_se_persistent_data( driver );
1999 if( status != PSA_SUCCESS )
2000 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002001 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002002 return( status );
2003 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002004 status = psa_crypto_stop_transaction( );
2005 if( status != PSA_SUCCESS )
2006 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002007 }
2008#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2009
Gilles Peskine4747d192019-04-17 15:05:45 +02002010 return( status );
2011}
2012
2013/** Abort the creation of a key.
2014 *
2015 * You may call this function after calling psa_start_key_creation(),
2016 * or after psa_finish_key_creation() fails. In other circumstances, this
2017 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002018 * See the documentation of psa_start_key_creation() for the intended use
2019 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002020 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002021 * \param[in,out] slot Pointer to the slot with key material.
2022 * \param[in] driver The secure element driver for the key,
2023 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002024 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002025static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002026 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002027{
Gilles Peskine011e4282019-06-26 18:34:38 +02002028 (void) driver;
2029
Gilles Peskine4747d192019-04-17 15:05:45 +02002030 if( slot == NULL )
2031 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002032
2033#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002034 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002035 * element, and the failure happened later (when saving metadata
2036 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002037 * element.
2038 * https://github.com/ARMmbed/mbed-crypto/issues/217
2039 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002040
Gilles Peskined7729582019-08-05 15:55:54 +02002041 /* Abort the ongoing transaction if any (there may not be one if
2042 * the creation process failed before starting one, or if the
2043 * key creation is a registration of a key in a secure element).
2044 * Earlier functions must already have done what it takes to undo any
2045 * partial creation. All that's left is to update the transaction data
2046 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002047 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002048#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2049
Gilles Peskine4747d192019-04-17 15:05:45 +02002050 psa_wipe_key_slot( slot );
2051}
2052
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002053/** Validate optional attributes during key creation.
2054 *
2055 * Some key attributes are optional during key creation. If they are
2056 * specified in the attributes structure, check that they are consistent
2057 * with the data in the slot.
2058 *
2059 * This function should be called near the end of key creation, after
2060 * the slot in memory is fully populated but before saving persistent data.
2061 */
2062static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002063 const psa_key_slot_t *slot,
2064 const psa_key_attributes_t *attributes )
2065{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002066 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002067 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002068 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002069 return( PSA_ERROR_INVALID_ARGUMENT );
2070 }
2071
2072 if( attributes->domain_parameters_size != 0 )
2073 {
2074#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002075 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002076 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002077 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002078 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002079 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002080
Steven Cooreman7f391872020-07-30 14:57:44 +02002081 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002082 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002083 slot->data.key.data,
2084 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002085 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002086 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002087 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002088
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002089 mbedtls_mpi_init( &actual );
2090 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002091 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002092 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002093 mbedtls_rsa_free( rsa );
2094 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002095 if( ret != 0 )
2096 goto rsa_exit;
2097 ret = mbedtls_mpi_read_binary( &required,
2098 attributes->domain_parameters,
2099 attributes->domain_parameters_size );
2100 if( ret != 0 )
2101 goto rsa_exit;
2102 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2103 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2104 rsa_exit:
2105 mbedtls_mpi_free( &actual );
2106 mbedtls_mpi_free( &required );
2107 if( ret != 0)
2108 return( mbedtls_to_psa_error( ret ) );
2109 }
2110 else
2111#endif
2112 {
2113 return( PSA_ERROR_INVALID_ARGUMENT );
2114 }
2115 }
2116
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002117 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002118 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002119 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002120 return( PSA_ERROR_INVALID_ARGUMENT );
2121 }
2122
2123 return( PSA_SUCCESS );
2124}
2125
Gilles Peskine4747d192019-04-17 15:05:45 +02002126psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002127 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002128 size_t data_length,
2129 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002130{
2131 psa_status_t status;
2132 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002133 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002134
Gilles Peskine0f84d622019-09-12 19:03:13 +02002135 /* Reject zero-length symmetric keys (including raw data key objects).
2136 * This also rejects any key which might be encoded as an empty string,
2137 * which is never valid. */
2138 if( data_length == 0 )
2139 return( PSA_ERROR_INVALID_ARGUMENT );
2140
Gilles Peskinedf179142019-07-15 22:02:14 +02002141 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2142 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002143 if( status != PSA_SUCCESS )
2144 goto exit;
2145
Gilles Peskine5d309672019-07-12 23:47:28 +02002146#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2147 if( driver != NULL )
2148 {
2149 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002150 /* The driver should set the number of key bits, however in
2151 * case it doesn't, we initialize bits to an invalid value. */
2152 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002153 if( drv->key_management == NULL ||
2154 drv->key_management->p_import == NULL )
2155 {
2156 status = PSA_ERROR_NOT_SUPPORTED;
2157 goto exit;
2158 }
2159 status = drv->key_management->p_import(
2160 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002161 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002162 &bits );
2163 if( status != PSA_SUCCESS )
2164 goto exit;
2165 if( bits > PSA_MAX_KEY_BITS )
2166 {
2167 status = PSA_ERROR_NOT_SUPPORTED;
2168 goto exit;
2169 }
2170 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002171 }
2172 else
2173#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2174 {
2175 status = psa_import_key_into_slot( slot, data, data_length );
2176 if( status != PSA_SUCCESS )
2177 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002178 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002179 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002180 if( status != PSA_SUCCESS )
2181 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002182
Gilles Peskine011e4282019-06-26 18:34:38 +02002183 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002184exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002185 if( status != PSA_SUCCESS )
2186 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002187 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002188 *handle = 0;
2189 }
2190 return( status );
2191}
2192
Gilles Peskined7729582019-08-05 15:55:54 +02002193#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2194psa_status_t mbedtls_psa_register_se_key(
2195 const psa_key_attributes_t *attributes )
2196{
2197 psa_status_t status;
2198 psa_key_slot_t *slot = NULL;
2199 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002200 psa_key_handle_t handle = 0;
2201
2202 /* Leaving attributes unspecified is not currently supported.
2203 * It could make sense to query the key type and size from the
2204 * secure element, but not all secure elements support this
2205 * and the driver HAL doesn't currently support it. */
2206 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2207 return( PSA_ERROR_NOT_SUPPORTED );
2208 if( psa_get_key_bits( attributes ) == 0 )
2209 return( PSA_ERROR_NOT_SUPPORTED );
2210
2211 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2212 &handle, &slot, &driver );
2213 if( status != PSA_SUCCESS )
2214 goto exit;
2215
Gilles Peskined7729582019-08-05 15:55:54 +02002216 status = psa_finish_key_creation( slot, driver );
2217
2218exit:
2219 if( status != PSA_SUCCESS )
2220 {
2221 psa_fail_key_creation( slot, driver );
2222 }
2223 /* Registration doesn't keep the key in RAM. */
2224 psa_close_key( handle );
2225 return( status );
2226}
2227#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2228
Gilles Peskinef603c712019-01-19 13:40:11 +01002229static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002230 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002231{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002232 psa_status_t status = psa_copy_key_material_into_slot( target,
2233 source->data.key.data,
2234 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002235 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002236 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002237
Steven Cooreman398aee52020-10-13 14:35:45 +02002238 target->attr.type = source->attr.type;
2239 target->attr.bits = source->attr.bits;
2240
2241 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002242}
2243
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002244psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2245 const psa_key_attributes_t *specified_attributes,
2246 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002247{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002248 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002249 psa_key_slot_t *source_slot = NULL;
2250 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002251 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002252 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002253
Gilles Peskine28f8f302019-07-24 13:30:31 +02002254 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002255 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002256 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002257 goto exit;
2258
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002259 status = psa_validate_optional_attributes( source_slot,
2260 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002261 if( status != PSA_SUCCESS )
2262 goto exit;
2263
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002264 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002265 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002266 if( status != PSA_SUCCESS )
2267 goto exit;
2268
Gilles Peskinedf179142019-07-15 22:02:14 +02002269 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2270 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002271 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002272 if( status != PSA_SUCCESS )
2273 goto exit;
2274
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002275#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2276 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002277 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002278 /* Copying to a secure element is not implemented yet. */
2279 status = PSA_ERROR_NOT_SUPPORTED;
2280 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002281 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002282#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002283
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002284 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002285 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002286 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002287
Gilles Peskine011e4282019-06-26 18:34:38 +02002288 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002289exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002290 if( status != PSA_SUCCESS )
2291 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002292 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002293 *target_handle = 0;
2294 }
2295 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002296}
2297
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002298
2299
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002300/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002301/* Message digests */
2302/****************************************************************/
2303
John Durkop0ea39e02020-10-13 19:58:20 -07002304#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002305static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002306{
2307 switch( alg )
2308 {
2309#if defined(MBEDTLS_MD2_C)
2310 case PSA_ALG_MD2:
2311 return( &mbedtls_md2_info );
2312#endif
2313#if defined(MBEDTLS_MD4_C)
2314 case PSA_ALG_MD4:
2315 return( &mbedtls_md4_info );
2316#endif
2317#if defined(MBEDTLS_MD5_C)
2318 case PSA_ALG_MD5:
2319 return( &mbedtls_md5_info );
2320#endif
2321#if defined(MBEDTLS_RIPEMD160_C)
2322 case PSA_ALG_RIPEMD160:
2323 return( &mbedtls_ripemd160_info );
2324#endif
2325#if defined(MBEDTLS_SHA1_C)
2326 case PSA_ALG_SHA_1:
2327 return( &mbedtls_sha1_info );
2328#endif
2329#if defined(MBEDTLS_SHA256_C)
2330 case PSA_ALG_SHA_224:
2331 return( &mbedtls_sha224_info );
2332 case PSA_ALG_SHA_256:
2333 return( &mbedtls_sha256_info );
2334#endif
2335#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002336#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002337 case PSA_ALG_SHA_384:
2338 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002339#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002340 case PSA_ALG_SHA_512:
2341 return( &mbedtls_sha512_info );
2342#endif
2343 default:
2344 return( NULL );
2345 }
2346}
John Durkop0ea39e02020-10-13 19:58:20 -07002347#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002348
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002349psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2350{
2351 switch( operation->alg )
2352 {
Gilles Peskine81736312018-06-26 15:04:31 +02002353 case 0:
2354 /* The object has (apparently) been initialized but it is not
2355 * in use. It's ok to call abort on such an object, and there's
2356 * nothing to do. */
2357 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002358#if defined(MBEDTLS_MD2_C)
2359 case PSA_ALG_MD2:
2360 mbedtls_md2_free( &operation->ctx.md2 );
2361 break;
2362#endif
2363#if defined(MBEDTLS_MD4_C)
2364 case PSA_ALG_MD4:
2365 mbedtls_md4_free( &operation->ctx.md4 );
2366 break;
2367#endif
2368#if defined(MBEDTLS_MD5_C)
2369 case PSA_ALG_MD5:
2370 mbedtls_md5_free( &operation->ctx.md5 );
2371 break;
2372#endif
2373#if defined(MBEDTLS_RIPEMD160_C)
2374 case PSA_ALG_RIPEMD160:
2375 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2376 break;
2377#endif
2378#if defined(MBEDTLS_SHA1_C)
2379 case PSA_ALG_SHA_1:
2380 mbedtls_sha1_free( &operation->ctx.sha1 );
2381 break;
2382#endif
2383#if defined(MBEDTLS_SHA256_C)
2384 case PSA_ALG_SHA_224:
2385 case PSA_ALG_SHA_256:
2386 mbedtls_sha256_free( &operation->ctx.sha256 );
2387 break;
2388#endif
2389#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002390#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002391 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002392#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002393 case PSA_ALG_SHA_512:
2394 mbedtls_sha512_free( &operation->ctx.sha512 );
2395 break;
2396#endif
2397 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002398 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002399 }
2400 operation->alg = 0;
2401 return( PSA_SUCCESS );
2402}
2403
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002404psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002405 psa_algorithm_t alg )
2406{
Janos Follath24eed8d2019-11-22 13:21:35 +00002407 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002408
2409 /* A context must be freshly initialized before it can be set up. */
2410 if( operation->alg != 0 )
2411 {
2412 return( PSA_ERROR_BAD_STATE );
2413 }
2414
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002415 switch( alg )
2416 {
2417#if defined(MBEDTLS_MD2_C)
2418 case PSA_ALG_MD2:
2419 mbedtls_md2_init( &operation->ctx.md2 );
2420 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2421 break;
2422#endif
2423#if defined(MBEDTLS_MD4_C)
2424 case PSA_ALG_MD4:
2425 mbedtls_md4_init( &operation->ctx.md4 );
2426 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2427 break;
2428#endif
2429#if defined(MBEDTLS_MD5_C)
2430 case PSA_ALG_MD5:
2431 mbedtls_md5_init( &operation->ctx.md5 );
2432 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2433 break;
2434#endif
2435#if defined(MBEDTLS_RIPEMD160_C)
2436 case PSA_ALG_RIPEMD160:
2437 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2438 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2439 break;
2440#endif
2441#if defined(MBEDTLS_SHA1_C)
2442 case PSA_ALG_SHA_1:
2443 mbedtls_sha1_init( &operation->ctx.sha1 );
2444 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2445 break;
2446#endif
2447#if defined(MBEDTLS_SHA256_C)
2448 case PSA_ALG_SHA_224:
2449 mbedtls_sha256_init( &operation->ctx.sha256 );
2450 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2451 break;
2452 case PSA_ALG_SHA_256:
2453 mbedtls_sha256_init( &operation->ctx.sha256 );
2454 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2455 break;
2456#endif
2457#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002458#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002459 case PSA_ALG_SHA_384:
2460 mbedtls_sha512_init( &operation->ctx.sha512 );
2461 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2462 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002463#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002464 case PSA_ALG_SHA_512:
2465 mbedtls_sha512_init( &operation->ctx.sha512 );
2466 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2467 break;
2468#endif
2469 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002470 return( PSA_ALG_IS_HASH( alg ) ?
2471 PSA_ERROR_NOT_SUPPORTED :
2472 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002473 }
2474 if( ret == 0 )
2475 operation->alg = alg;
2476 else
2477 psa_hash_abort( operation );
2478 return( mbedtls_to_psa_error( ret ) );
2479}
2480
2481psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2482 const uint8_t *input,
2483 size_t input_length )
2484{
Janos Follath24eed8d2019-11-22 13:21:35 +00002485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002486
2487 /* Don't require hash implementations to behave correctly on a
2488 * zero-length input, which may have an invalid pointer. */
2489 if( input_length == 0 )
2490 return( PSA_SUCCESS );
2491
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002492 switch( operation->alg )
2493 {
2494#if defined(MBEDTLS_MD2_C)
2495 case PSA_ALG_MD2:
2496 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2497 input, input_length );
2498 break;
2499#endif
2500#if defined(MBEDTLS_MD4_C)
2501 case PSA_ALG_MD4:
2502 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2503 input, input_length );
2504 break;
2505#endif
2506#if defined(MBEDTLS_MD5_C)
2507 case PSA_ALG_MD5:
2508 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2509 input, input_length );
2510 break;
2511#endif
2512#if defined(MBEDTLS_RIPEMD160_C)
2513 case PSA_ALG_RIPEMD160:
2514 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2515 input, input_length );
2516 break;
2517#endif
2518#if defined(MBEDTLS_SHA1_C)
2519 case PSA_ALG_SHA_1:
2520 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2521 input, input_length );
2522 break;
2523#endif
2524#if defined(MBEDTLS_SHA256_C)
2525 case PSA_ALG_SHA_224:
2526 case PSA_ALG_SHA_256:
2527 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2528 input, input_length );
2529 break;
2530#endif
2531#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002532#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002533 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002534#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002535 case PSA_ALG_SHA_512:
2536 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2537 input, input_length );
2538 break;
2539#endif
2540 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002541 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002542 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002543
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002544 if( ret != 0 )
2545 psa_hash_abort( operation );
2546 return( mbedtls_to_psa_error( ret ) );
2547}
2548
2549psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2550 uint8_t *hash,
2551 size_t hash_size,
2552 size_t *hash_length )
2553{
itayzafrir40835d42018-08-02 13:14:17 +03002554 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002555 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002556 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002557
2558 /* Fill the output buffer with something that isn't a valid hash
2559 * (barring an attack on the hash and deliberately-crafted input),
2560 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002561 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002562 /* If hash_size is 0 then hash may be NULL and then the
2563 * call to memset would have undefined behavior. */
2564 if( hash_size != 0 )
2565 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002566
2567 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002568 {
2569 status = PSA_ERROR_BUFFER_TOO_SMALL;
2570 goto exit;
2571 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002572
2573 switch( operation->alg )
2574 {
2575#if defined(MBEDTLS_MD2_C)
2576 case PSA_ALG_MD2:
2577 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2578 break;
2579#endif
2580#if defined(MBEDTLS_MD4_C)
2581 case PSA_ALG_MD4:
2582 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2583 break;
2584#endif
2585#if defined(MBEDTLS_MD5_C)
2586 case PSA_ALG_MD5:
2587 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2588 break;
2589#endif
2590#if defined(MBEDTLS_RIPEMD160_C)
2591 case PSA_ALG_RIPEMD160:
2592 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2593 break;
2594#endif
2595#if defined(MBEDTLS_SHA1_C)
2596 case PSA_ALG_SHA_1:
2597 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2598 break;
2599#endif
2600#if defined(MBEDTLS_SHA256_C)
2601 case PSA_ALG_SHA_224:
2602 case PSA_ALG_SHA_256:
2603 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2604 break;
2605#endif
2606#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002607#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002608 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002609#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002610 case PSA_ALG_SHA_512:
2611 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2612 break;
2613#endif
2614 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002615 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002616 }
itayzafrir40835d42018-08-02 13:14:17 +03002617 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002618
itayzafrir40835d42018-08-02 13:14:17 +03002619exit:
2620 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002621 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002622 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002623 return( psa_hash_abort( operation ) );
2624 }
2625 else
2626 {
2627 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002628 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002629 }
2630}
2631
Gilles Peskine2d277862018-06-18 15:41:12 +02002632psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2633 const uint8_t *hash,
2634 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002635{
2636 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2637 size_t actual_hash_length;
2638 psa_status_t status = psa_hash_finish( operation,
2639 actual_hash, sizeof( actual_hash ),
2640 &actual_hash_length );
2641 if( status != PSA_SUCCESS )
2642 return( status );
2643 if( actual_hash_length != hash_length )
2644 return( PSA_ERROR_INVALID_SIGNATURE );
2645 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2646 return( PSA_ERROR_INVALID_SIGNATURE );
2647 return( PSA_SUCCESS );
2648}
2649
Gilles Peskine0a749c82019-11-28 19:33:58 +01002650psa_status_t psa_hash_compute( psa_algorithm_t alg,
2651 const uint8_t *input, size_t input_length,
2652 uint8_t *hash, size_t hash_size,
2653 size_t *hash_length )
2654{
2655 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2656 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2657
2658 *hash_length = hash_size;
2659 status = psa_hash_setup( &operation, alg );
2660 if( status != PSA_SUCCESS )
2661 goto exit;
2662 status = psa_hash_update( &operation, input, input_length );
2663 if( status != PSA_SUCCESS )
2664 goto exit;
2665 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2666 if( status != PSA_SUCCESS )
2667 goto exit;
2668
2669exit:
2670 if( status == PSA_SUCCESS )
2671 status = psa_hash_abort( &operation );
2672 else
2673 psa_hash_abort( &operation );
2674 return( status );
2675}
2676
2677psa_status_t psa_hash_compare( psa_algorithm_t alg,
2678 const uint8_t *input, size_t input_length,
2679 const uint8_t *hash, size_t hash_length )
2680{
2681 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2682 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2683
2684 status = psa_hash_setup( &operation, alg );
2685 if( status != PSA_SUCCESS )
2686 goto exit;
2687 status = psa_hash_update( &operation, input, input_length );
2688 if( status != PSA_SUCCESS )
2689 goto exit;
2690 status = psa_hash_verify( &operation, hash, hash_length );
2691 if( status != PSA_SUCCESS )
2692 goto exit;
2693
2694exit:
2695 if( status == PSA_SUCCESS )
2696 status = psa_hash_abort( &operation );
2697 else
2698 psa_hash_abort( &operation );
2699 return( status );
2700}
2701
Gilles Peskineeb35d782019-01-22 17:56:16 +01002702psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2703 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002704{
2705 if( target_operation->alg != 0 )
2706 return( PSA_ERROR_BAD_STATE );
2707
2708 switch( source_operation->alg )
2709 {
2710 case 0:
2711 return( PSA_ERROR_BAD_STATE );
2712#if defined(MBEDTLS_MD2_C)
2713 case PSA_ALG_MD2:
2714 mbedtls_md2_clone( &target_operation->ctx.md2,
2715 &source_operation->ctx.md2 );
2716 break;
2717#endif
2718#if defined(MBEDTLS_MD4_C)
2719 case PSA_ALG_MD4:
2720 mbedtls_md4_clone( &target_operation->ctx.md4,
2721 &source_operation->ctx.md4 );
2722 break;
2723#endif
2724#if defined(MBEDTLS_MD5_C)
2725 case PSA_ALG_MD5:
2726 mbedtls_md5_clone( &target_operation->ctx.md5,
2727 &source_operation->ctx.md5 );
2728 break;
2729#endif
2730#if defined(MBEDTLS_RIPEMD160_C)
2731 case PSA_ALG_RIPEMD160:
2732 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2733 &source_operation->ctx.ripemd160 );
2734 break;
2735#endif
2736#if defined(MBEDTLS_SHA1_C)
2737 case PSA_ALG_SHA_1:
2738 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2739 &source_operation->ctx.sha1 );
2740 break;
2741#endif
2742#if defined(MBEDTLS_SHA256_C)
2743 case PSA_ALG_SHA_224:
2744 case PSA_ALG_SHA_256:
2745 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2746 &source_operation->ctx.sha256 );
2747 break;
2748#endif
2749#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002750#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002751 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002752#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002753 case PSA_ALG_SHA_512:
2754 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2755 &source_operation->ctx.sha512 );
2756 break;
2757#endif
2758 default:
2759 return( PSA_ERROR_NOT_SUPPORTED );
2760 }
2761
2762 target_operation->alg = source_operation->alg;
2763 return( PSA_SUCCESS );
2764}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002765
2766
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002767/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002768/* MAC */
2769/****************************************************************/
2770
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002771static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002772 psa_algorithm_t alg,
2773 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002774 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002775 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002776{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002777 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002778 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002779
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002780 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002781 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002782
Gilles Peskine8c9def32018-02-08 10:02:12 +01002783 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2784 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002785 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002786 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002787 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002788 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002789 mode = MBEDTLS_MODE_STREAM;
2790 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002791 case PSA_ALG_CTR:
2792 mode = MBEDTLS_MODE_CTR;
2793 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002794 case PSA_ALG_CFB:
2795 mode = MBEDTLS_MODE_CFB;
2796 break;
2797 case PSA_ALG_OFB:
2798 mode = MBEDTLS_MODE_OFB;
2799 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002800 case PSA_ALG_ECB_NO_PADDING:
2801 mode = MBEDTLS_MODE_ECB;
2802 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002803 case PSA_ALG_CBC_NO_PADDING:
2804 mode = MBEDTLS_MODE_CBC;
2805 break;
2806 case PSA_ALG_CBC_PKCS7:
2807 mode = MBEDTLS_MODE_CBC;
2808 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002809 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002810 mode = MBEDTLS_MODE_CCM;
2811 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002812 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002813 mode = MBEDTLS_MODE_GCM;
2814 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002815 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2816 mode = MBEDTLS_MODE_CHACHAPOLY;
2817 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002818 default:
2819 return( NULL );
2820 }
2821 }
2822 else if( alg == PSA_ALG_CMAC )
2823 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002824 else
2825 return( NULL );
2826
2827 switch( key_type )
2828 {
2829 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002830 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002831 break;
2832 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002833 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2834 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002835 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002836 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002837 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002838 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002839 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2840 * but two-key Triple-DES is functionally three-key Triple-DES
2841 * with K1=K3, so that's how we present it to mbedtls. */
2842 if( key_bits == 128 )
2843 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002844 break;
2845 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002846 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002847 break;
2848 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002849 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002850 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002851 case PSA_KEY_TYPE_CHACHA20:
2852 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2853 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002854 default:
2855 return( NULL );
2856 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002857 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002858 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002859
Jaeden Amero23bbb752018-06-26 14:16:54 +01002860 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2861 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002862}
2863
Gilles Peskinea05219c2018-11-16 16:02:56 +01002864#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002865static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002866{
Gilles Peskine2d277862018-06-18 15:41:12 +02002867 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002868 {
2869 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002870 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002871 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002872 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002873 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002874 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002875 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002876 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002877 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002878 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002879 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002880 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002881 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002882 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002883 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002884 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002885 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002886 return( 128 );
2887 default:
2888 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002889 }
2890}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002891#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002892
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002893/* Initialize the MAC operation structure. Once this function has been
2894 * called, psa_mac_abort can run and will do the right thing. */
2895static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2896 psa_algorithm_t alg )
2897{
2898 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2899
2900 operation->alg = alg;
2901 operation->key_set = 0;
2902 operation->iv_set = 0;
2903 operation->iv_required = 0;
2904 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002905 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002906
2907#if defined(MBEDTLS_CMAC_C)
2908 if( alg == PSA_ALG_CMAC )
2909 {
2910 operation->iv_required = 0;
2911 mbedtls_cipher_init( &operation->ctx.cmac );
2912 status = PSA_SUCCESS;
2913 }
2914 else
2915#endif /* MBEDTLS_CMAC_C */
2916#if defined(MBEDTLS_MD_C)
2917 if( PSA_ALG_IS_HMAC( operation->alg ) )
2918 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002919 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2920 operation->ctx.hmac.hash_ctx.alg = 0;
2921 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002922 }
2923 else
2924#endif /* MBEDTLS_MD_C */
2925 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002926 if( ! PSA_ALG_IS_MAC( alg ) )
2927 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002928 }
2929
2930 if( status != PSA_SUCCESS )
2931 memset( operation, 0, sizeof( *operation ) );
2932 return( status );
2933}
2934
Gilles Peskine01126fa2018-07-12 17:04:55 +02002935#if defined(MBEDTLS_MD_C)
2936static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2937{
Gilles Peskine3f108122018-12-07 18:14:53 +01002938 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002939 return( psa_hash_abort( &hmac->hash_ctx ) );
2940}
2941#endif /* MBEDTLS_MD_C */
2942
Gilles Peskine8c9def32018-02-08 10:02:12 +01002943psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2944{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002945 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002946 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002947 /* The object has (apparently) been initialized but it is not
2948 * in use. It's ok to call abort on such an object, and there's
2949 * nothing to do. */
2950 return( PSA_SUCCESS );
2951 }
2952 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002953#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002954 if( operation->alg == PSA_ALG_CMAC )
2955 {
2956 mbedtls_cipher_free( &operation->ctx.cmac );
2957 }
2958 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002959#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002960#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002961 if( PSA_ALG_IS_HMAC( operation->alg ) )
2962 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002963 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002964 }
2965 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002966#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002967 {
2968 /* Sanity check (shouldn't happen: operation->alg should
2969 * always have been initialized to a valid value). */
2970 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002971 }
Moran Peker41deec42018-04-04 15:43:05 +03002972
Gilles Peskine8c9def32018-02-08 10:02:12 +01002973 operation->alg = 0;
2974 operation->key_set = 0;
2975 operation->iv_set = 0;
2976 operation->iv_required = 0;
2977 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002978 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002979
Gilles Peskine8c9def32018-02-08 10:02:12 +01002980 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002981
2982bad_state:
2983 /* If abort is called on an uninitialized object, we can't trust
2984 * anything. Wipe the object in case it contains confidential data.
2985 * This may result in a memory leak if a pointer gets overwritten,
2986 * but it's too late to do anything about this. */
2987 memset( operation, 0, sizeof( *operation ) );
2988 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002989}
2990
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002991#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002992static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002993 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002994 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002995 const mbedtls_cipher_info_t *cipher_info )
2996{
Janos Follath24eed8d2019-11-22 13:21:35 +00002997 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002998
2999 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003000
3001 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3002 if( ret != 0 )
3003 return( ret );
3004
3005 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003006 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003007 key_bits );
3008 return( ret );
3009}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003010#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003011
Gilles Peskine248051a2018-06-20 16:09:38 +02003012#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003013static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3014 const uint8_t *key,
3015 size_t key_length,
3016 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003017{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003018 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003019 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003020 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003021 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003022 psa_status_t status;
3023
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003024 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3025 * overflow below. This should never trigger if the hash algorithm
3026 * is implemented correctly. */
3027 /* The size checks against the ipad and opad buffers cannot be written
3028 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3029 * because that triggers -Wlogical-op on GCC 7.3. */
3030 if( block_size > sizeof( ipad ) )
3031 return( PSA_ERROR_NOT_SUPPORTED );
3032 if( block_size > sizeof( hmac->opad ) )
3033 return( PSA_ERROR_NOT_SUPPORTED );
3034 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003035 return( PSA_ERROR_NOT_SUPPORTED );
3036
Gilles Peskined223b522018-06-11 18:12:58 +02003037 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003038 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003039 status = psa_hash_compute( hash_alg, key, key_length,
3040 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003041 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003042 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003043 }
Gilles Peskine96889972018-07-12 17:07:03 +02003044 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3045 * but it is permitted. It is common when HMAC is used in HKDF, for
3046 * example. Don't call `memcpy` in the 0-length because `key` could be
3047 * an invalid pointer which would make the behavior undefined. */
3048 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003049 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003050
Gilles Peskined223b522018-06-11 18:12:58 +02003051 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3052 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003053 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003054 ipad[i] ^= 0x36;
3055 memset( ipad + key_length, 0x36, block_size - key_length );
3056
3057 /* Copy the key material from ipad to opad, flipping the requisite bits,
3058 * and filling the rest of opad with the requisite constant. */
3059 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003060 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3061 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003062
Gilles Peskine01126fa2018-07-12 17:04:55 +02003063 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003064 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003065 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003066
Gilles Peskine01126fa2018-07-12 17:04:55 +02003067 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003068
3069cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003070 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003071
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003072 return( status );
3073}
Gilles Peskine248051a2018-06-20 16:09:38 +02003074#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003075
Gilles Peskine89167cb2018-07-08 20:12:23 +02003076static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003077 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003078 psa_algorithm_t alg,
3079 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003080{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003081 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003082 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003083 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003084 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003085 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003086 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003087 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003088
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003089 /* A context must be freshly initialized before it can be set up. */
3090 if( operation->alg != 0 )
3091 {
3092 return( PSA_ERROR_BAD_STATE );
3093 }
3094
Gilles Peskined911eb72018-08-14 15:18:45 +02003095 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003096 if( status != PSA_SUCCESS )
3097 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003098 if( is_sign )
3099 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003100
Gilles Peskine28f8f302019-07-24 13:30:31 +02003101 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003102 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003103 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003104 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003105
Gilles Peskine8c9def32018-02-08 10:02:12 +01003106#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003107 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003108 {
3109 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003110 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003111 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003113 if( cipher_info == NULL )
3114 {
3115 status = PSA_ERROR_NOT_SUPPORTED;
3116 goto exit;
3117 }
3118 operation->mac_size = cipher_info->block_size;
3119 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3120 status = mbedtls_to_psa_error( ret );
3121 }
3122 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003123#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003124#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003125 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003126 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003127 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003128 if( hash_alg == 0 )
3129 {
3130 status = PSA_ERROR_NOT_SUPPORTED;
3131 goto exit;
3132 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003133
3134 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3135 /* Sanity check. This shouldn't fail on a valid configuration. */
3136 if( operation->mac_size == 0 ||
3137 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3138 {
3139 status = PSA_ERROR_NOT_SUPPORTED;
3140 goto exit;
3141 }
3142
Gilles Peskine8e338702019-07-30 20:06:31 +02003143 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003144 {
3145 status = PSA_ERROR_INVALID_ARGUMENT;
3146 goto exit;
3147 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003148
Gilles Peskine01126fa2018-07-12 17:04:55 +02003149 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003150 slot->data.key.data,
3151 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003152 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003153 }
3154 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003155#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003156 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003157 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003158 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003159 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003160
Gilles Peskined911eb72018-08-14 15:18:45 +02003161 if( truncated == 0 )
3162 {
3163 /* The "normal" case: untruncated algorithm. Nothing to do. */
3164 }
3165 else if( truncated < 4 )
3166 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003167 /* A very short MAC is too short for security since it can be
3168 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3169 * so we make this our minimum, even though 32 bits is still
3170 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003171 status = PSA_ERROR_NOT_SUPPORTED;
3172 }
3173 else if( truncated > operation->mac_size )
3174 {
3175 /* It's impossible to "truncate" to a larger length. */
3176 status = PSA_ERROR_INVALID_ARGUMENT;
3177 }
3178 else
3179 operation->mac_size = truncated;
3180
Gilles Peskinefbfac682018-07-08 20:51:54 +02003181exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003182 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003183 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003184 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003185 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003186 else
3187 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003188 operation->key_set = 1;
3189 }
3190 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003191}
3192
Gilles Peskine89167cb2018-07-08 20:12:23 +02003193psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003194 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003195 psa_algorithm_t alg )
3196{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003197 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003198}
3199
3200psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003201 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003202 psa_algorithm_t alg )
3203{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003204 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003205}
3206
Gilles Peskine8c9def32018-02-08 10:02:12 +01003207psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3208 const uint8_t *input,
3209 size_t input_length )
3210{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003211 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003212 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003213 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003214 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003215 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003216 operation->has_input = 1;
3217
Gilles Peskine8c9def32018-02-08 10:02:12 +01003218#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003219 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003220 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003221 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3222 input, input_length );
3223 status = mbedtls_to_psa_error( ret );
3224 }
3225 else
3226#endif /* MBEDTLS_CMAC_C */
3227#if defined(MBEDTLS_MD_C)
3228 if( PSA_ALG_IS_HMAC( operation->alg ) )
3229 {
3230 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3231 input_length );
3232 }
3233 else
3234#endif /* MBEDTLS_MD_C */
3235 {
3236 /* This shouldn't happen if `operation` was initialized by
3237 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003238 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003239 }
3240
Gilles Peskinefbfac682018-07-08 20:51:54 +02003241 if( status != PSA_SUCCESS )
3242 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003243 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003244}
3245
Gilles Peskine01126fa2018-07-12 17:04:55 +02003246#if defined(MBEDTLS_MD_C)
3247static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3248 uint8_t *mac,
3249 size_t mac_size )
3250{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003251 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003252 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3253 size_t hash_size = 0;
3254 size_t block_size = psa_get_hash_block_size( hash_alg );
3255 psa_status_t status;
3256
Gilles Peskine01126fa2018-07-12 17:04:55 +02003257 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3258 if( status != PSA_SUCCESS )
3259 return( status );
3260 /* From here on, tmp needs to be wiped. */
3261
3262 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3263 if( status != PSA_SUCCESS )
3264 goto exit;
3265
3266 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3267 if( status != PSA_SUCCESS )
3268 goto exit;
3269
3270 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3271 if( status != PSA_SUCCESS )
3272 goto exit;
3273
Gilles Peskined911eb72018-08-14 15:18:45 +02003274 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3275 if( status != PSA_SUCCESS )
3276 goto exit;
3277
3278 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003279
3280exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003281 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003282 return( status );
3283}
3284#endif /* MBEDTLS_MD_C */
3285
mohammad16036df908f2018-04-02 08:34:15 -07003286static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003287 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003288 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003289{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003290 if( ! operation->key_set )
3291 return( PSA_ERROR_BAD_STATE );
3292 if( operation->iv_required && ! operation->iv_set )
3293 return( PSA_ERROR_BAD_STATE );
3294
Gilles Peskine8c9def32018-02-08 10:02:12 +01003295 if( mac_size < operation->mac_size )
3296 return( PSA_ERROR_BUFFER_TOO_SMALL );
3297
Gilles Peskine8c9def32018-02-08 10:02:12 +01003298#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003299 if( operation->alg == PSA_ALG_CMAC )
3300 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003301 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3302 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3303 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003304 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003305 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003306 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003307 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003308 else
3309#endif /* MBEDTLS_CMAC_C */
3310#if defined(MBEDTLS_MD_C)
3311 if( PSA_ALG_IS_HMAC( operation->alg ) )
3312 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003313 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003314 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003315 }
3316 else
3317#endif /* MBEDTLS_MD_C */
3318 {
3319 /* This shouldn't happen if `operation` was initialized by
3320 * a setup function. */
3321 return( PSA_ERROR_BAD_STATE );
3322 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003323}
3324
Gilles Peskineacd4be32018-07-08 19:56:25 +02003325psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3326 uint8_t *mac,
3327 size_t mac_size,
3328 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003329{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003330 psa_status_t status;
3331
Jaeden Amero252ef282019-02-15 14:05:35 +00003332 if( operation->alg == 0 )
3333 {
3334 return( PSA_ERROR_BAD_STATE );
3335 }
3336
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003337 /* Fill the output buffer with something that isn't a valid mac
3338 * (barring an attack on the mac and deliberately-crafted input),
3339 * in case the caller doesn't check the return status properly. */
3340 *mac_length = mac_size;
3341 /* If mac_size is 0 then mac may be NULL and then the
3342 * call to memset would have undefined behavior. */
3343 if( mac_size != 0 )
3344 memset( mac, '!', mac_size );
3345
Gilles Peskine89167cb2018-07-08 20:12:23 +02003346 if( ! operation->is_sign )
3347 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003348 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003349 }
mohammad16036df908f2018-04-02 08:34:15 -07003350
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003351 status = psa_mac_finish_internal( operation, mac, mac_size );
3352
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003353 if( status == PSA_SUCCESS )
3354 {
3355 status = psa_mac_abort( operation );
3356 if( status == PSA_SUCCESS )
3357 *mac_length = operation->mac_size;
3358 else
3359 memset( mac, '!', mac_size );
3360 }
3361 else
3362 psa_mac_abort( operation );
3363 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003364}
3365
Gilles Peskineacd4be32018-07-08 19:56:25 +02003366psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3367 const uint8_t *mac,
3368 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003369{
Gilles Peskine828ed142018-06-18 23:25:51 +02003370 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003371 psa_status_t status;
3372
Jaeden Amero252ef282019-02-15 14:05:35 +00003373 if( operation->alg == 0 )
3374 {
3375 return( PSA_ERROR_BAD_STATE );
3376 }
3377
Gilles Peskine89167cb2018-07-08 20:12:23 +02003378 if( operation->is_sign )
3379 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003380 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003381 }
3382 if( operation->mac_size != mac_length )
3383 {
3384 status = PSA_ERROR_INVALID_SIGNATURE;
3385 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003386 }
mohammad16036df908f2018-04-02 08:34:15 -07003387
3388 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003389 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003390 if( status != PSA_SUCCESS )
3391 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003392
3393 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3394 status = PSA_ERROR_INVALID_SIGNATURE;
3395
3396cleanup:
3397 if( status == PSA_SUCCESS )
3398 status = psa_mac_abort( operation );
3399 else
3400 psa_mac_abort( operation );
3401
Gilles Peskine3f108122018-12-07 18:14:53 +01003402 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003403
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003404 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003405}
3406
3407
Gilles Peskine20035e32018-02-03 22:44:14 +01003408
Gilles Peskine20035e32018-02-03 22:44:14 +01003409/****************************************************************/
3410/* Asymmetric cryptography */
3411/****************************************************************/
3412
Gilles Peskine2b450e32018-06-27 15:42:46 +02003413#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003414/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003415 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003416static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3417 size_t hash_length,
3418 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003419{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003420 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003421 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003422 *md_alg = mbedtls_md_get_type( md_info );
3423
3424 /* The Mbed TLS RSA module uses an unsigned int for hash length
3425 * parameters. Validate that it fits so that we don't risk an
3426 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003427#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003428 if( hash_length > UINT_MAX )
3429 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003430#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003431
3432#if defined(MBEDTLS_PKCS1_V15)
3433 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3434 * must be correct. */
3435 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3436 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003437 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003438 if( md_info == NULL )
3439 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003440 if( mbedtls_md_get_size( md_info ) != hash_length )
3441 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003442 }
3443#endif /* MBEDTLS_PKCS1_V15 */
3444
3445#if defined(MBEDTLS_PKCS1_V21)
3446 /* PSS requires a hash internally. */
3447 if( PSA_ALG_IS_RSA_PSS( alg ) )
3448 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003449 if( md_info == NULL )
3450 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003451 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003452#endif /* MBEDTLS_PKCS1_V21 */
3453
Gilles Peskine61b91d42018-06-08 16:09:36 +02003454 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003455}
3456
Gilles Peskine2b450e32018-06-27 15:42:46 +02003457static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3458 psa_algorithm_t alg,
3459 const uint8_t *hash,
3460 size_t hash_length,
3461 uint8_t *signature,
3462 size_t signature_size,
3463 size_t *signature_length )
3464{
3465 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003466 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003467 mbedtls_md_type_t md_alg;
3468
3469 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3470 if( status != PSA_SUCCESS )
3471 return( status );
3472
Gilles Peskine630a18a2018-06-29 17:49:35 +02003473 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003474 return( PSA_ERROR_BUFFER_TOO_SMALL );
3475
3476#if defined(MBEDTLS_PKCS1_V15)
3477 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3478 {
3479 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3480 MBEDTLS_MD_NONE );
3481 ret = mbedtls_rsa_pkcs1_sign( rsa,
3482 mbedtls_ctr_drbg_random,
3483 &global_data.ctr_drbg,
3484 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003485 md_alg,
3486 (unsigned int) hash_length,
3487 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003488 signature );
3489 }
3490 else
3491#endif /* MBEDTLS_PKCS1_V15 */
3492#if defined(MBEDTLS_PKCS1_V21)
3493 if( PSA_ALG_IS_RSA_PSS( alg ) )
3494 {
3495 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3496 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3497 mbedtls_ctr_drbg_random,
3498 &global_data.ctr_drbg,
3499 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003500 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003501 (unsigned int) hash_length,
3502 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003503 signature );
3504 }
3505 else
3506#endif /* MBEDTLS_PKCS1_V21 */
3507 {
3508 return( PSA_ERROR_INVALID_ARGUMENT );
3509 }
3510
3511 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003512 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003513 return( mbedtls_to_psa_error( ret ) );
3514}
3515
3516static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3517 psa_algorithm_t alg,
3518 const uint8_t *hash,
3519 size_t hash_length,
3520 const uint8_t *signature,
3521 size_t signature_length )
3522{
3523 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003524 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003525 mbedtls_md_type_t md_alg;
3526
3527 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3528 if( status != PSA_SUCCESS )
3529 return( status );
3530
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003531 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3532 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003533
3534#if defined(MBEDTLS_PKCS1_V15)
3535 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3536 {
3537 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3538 MBEDTLS_MD_NONE );
3539 ret = mbedtls_rsa_pkcs1_verify( rsa,
3540 mbedtls_ctr_drbg_random,
3541 &global_data.ctr_drbg,
3542 MBEDTLS_RSA_PUBLIC,
3543 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003544 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003545 hash,
3546 signature );
3547 }
3548 else
3549#endif /* MBEDTLS_PKCS1_V15 */
3550#if defined(MBEDTLS_PKCS1_V21)
3551 if( PSA_ALG_IS_RSA_PSS( alg ) )
3552 {
3553 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3554 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3555 mbedtls_ctr_drbg_random,
3556 &global_data.ctr_drbg,
3557 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003558 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003559 (unsigned int) hash_length,
3560 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003561 signature );
3562 }
3563 else
3564#endif /* MBEDTLS_PKCS1_V21 */
3565 {
3566 return( PSA_ERROR_INVALID_ARGUMENT );
3567 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003568
3569 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3570 * the rest of the signature is invalid". This has little use in
3571 * practice and PSA doesn't report this distinction. */
3572 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3573 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003574 return( mbedtls_to_psa_error( ret ) );
3575}
3576#endif /* MBEDTLS_RSA_C */
3577
John Durkopf87e3ae2020-10-26 15:25:23 -07003578#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003579/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3580 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3581 * (even though these functions don't modify it). */
3582static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3583 psa_algorithm_t alg,
3584 const uint8_t *hash,
3585 size_t hash_length,
3586 uint8_t *signature,
3587 size_t signature_size,
3588 size_t *signature_length )
3589{
Janos Follath24eed8d2019-11-22 13:21:35 +00003590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003591 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003592 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003593 mbedtls_mpi_init( &r );
3594 mbedtls_mpi_init( &s );
3595
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003596 if( signature_size < 2 * curve_bytes )
3597 {
3598 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3599 goto cleanup;
3600 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003601
John Durkop0ea39e02020-10-13 19:58:20 -07003602#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003603 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3604 {
3605 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3606 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3607 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003608 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3609 &ecp->d, hash,
3610 hash_length, md_alg,
3611 mbedtls_ctr_drbg_random,
3612 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003613 }
3614 else
John Durkop0ea39e02020-10-13 19:58:20 -07003615#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003616 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003617 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003618 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3619 hash, hash_length,
3620 mbedtls_ctr_drbg_random,
3621 &global_data.ctr_drbg ) );
3622 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003623
3624 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3625 signature,
3626 curve_bytes ) );
3627 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3628 signature + curve_bytes,
3629 curve_bytes ) );
3630
3631cleanup:
3632 mbedtls_mpi_free( &r );
3633 mbedtls_mpi_free( &s );
3634 if( ret == 0 )
3635 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003636 return( mbedtls_to_psa_error( ret ) );
3637}
3638
3639static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3640 const uint8_t *hash,
3641 size_t hash_length,
3642 const uint8_t *signature,
3643 size_t signature_length )
3644{
Janos Follath24eed8d2019-11-22 13:21:35 +00003645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003646 mbedtls_mpi r, s;
3647 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3648 mbedtls_mpi_init( &r );
3649 mbedtls_mpi_init( &s );
3650
3651 if( signature_length != 2 * curve_bytes )
3652 return( PSA_ERROR_INVALID_SIGNATURE );
3653
3654 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3655 signature,
3656 curve_bytes ) );
3657 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3658 signature + curve_bytes,
3659 curve_bytes ) );
3660
Steven Cooremanacda8342020-07-24 23:09:52 +02003661 /* Check whether the public part is loaded. If not, load it. */
3662 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3663 {
3664 MBEDTLS_MPI_CHK(
3665 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3666 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3667 }
3668
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003669 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3670 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003671
3672cleanup:
3673 mbedtls_mpi_free( &r );
3674 mbedtls_mpi_free( &s );
3675 return( mbedtls_to_psa_error( ret ) );
3676}
John Durkopf87e3ae2020-10-26 15:25:23 -07003677#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003678
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003679psa_status_t psa_sign_hash( psa_key_handle_t handle,
3680 psa_algorithm_t alg,
3681 const uint8_t *hash,
3682 size_t hash_length,
3683 uint8_t *signature,
3684 size_t signature_size,
3685 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003686{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003687 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003688 psa_status_t status;
3689
3690 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003691 /* Immediately reject a zero-length signature buffer. This guarantees
3692 * that signature must be a valid pointer. (On the other hand, the hash
3693 * buffer can in principle be empty since it doesn't actually have
3694 * to be a hash.) */
3695 if( signature_size == 0 )
3696 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003697
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003698 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003699 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003700 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003701 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003702 {
3703 status = PSA_ERROR_INVALID_ARGUMENT;
3704 goto exit;
3705 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003706
Steven Cooremancd84cb42020-07-16 20:28:36 +02003707 /* Try any of the available accelerators first */
3708 status = psa_driver_wrapper_sign_hash( slot,
3709 alg,
3710 hash,
3711 hash_length,
3712 signature,
3713 signature_size,
3714 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003715 if( status != PSA_ERROR_NOT_SUPPORTED ||
3716 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003717 goto exit;
3718
Steven Cooreman7a250572020-07-17 16:43:05 +02003719 /* If the operation was not supported by any accelerator, try fallback. */
Gilles Peskine20035e32018-02-03 22:44:14 +01003720#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003721 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003722 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003723 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003724
Steven Cooreman4fed4552020-08-03 14:46:03 +02003725 status = psa_load_rsa_representation( slot->attr.type,
3726 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003727 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003728 &rsa );
3729 if( status != PSA_SUCCESS )
3730 goto exit;
3731
Steven Cooremana2371e52020-07-28 14:30:39 +02003732 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003733 alg,
3734 hash, hash_length,
3735 signature, signature_size,
3736 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003737
Steven Cooremana2371e52020-07-28 14:30:39 +02003738 mbedtls_rsa_free( rsa );
3739 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003740 }
3741 else
3742#endif /* defined(MBEDTLS_RSA_C) */
3743#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003744 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003745 {
John Durkop0ea39e02020-10-13 19:58:20 -07003746#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003747 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003748#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003749 PSA_ALG_IS_ECDSA( alg )
3750#else
3751 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3752#endif
3753 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003754 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003755 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003756 status = psa_load_ecp_representation( slot->attr.type,
3757 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003758 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003759 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003760 if( status != PSA_SUCCESS )
3761 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003762 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003763 alg,
3764 hash, hash_length,
3765 signature, signature_size,
3766 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003767 mbedtls_ecp_keypair_free( ecp );
3768 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003769 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003770 else
John Durkop0ea39e02020-10-13 19:58:20 -07003771#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003772 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003773 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003774 }
itayzafrir5c753392018-05-08 11:18:38 +03003775 }
3776 else
3777#endif /* defined(MBEDTLS_ECP_C) */
3778 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003779 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003780 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003781
3782exit:
3783 /* Fill the unused part of the output buffer (the whole buffer on error,
3784 * the trailing part on success) with something that isn't a valid mac
3785 * (barring an attack on the mac and deliberately-crafted input),
3786 * in case the caller doesn't check the return status properly. */
3787 if( status == PSA_SUCCESS )
3788 memset( signature + *signature_length, '!',
3789 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003790 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003791 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003792 /* If signature_size is 0 then we have nothing to do. We must not call
3793 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003794 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003795}
3796
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003797psa_status_t psa_verify_hash( psa_key_handle_t handle,
3798 psa_algorithm_t alg,
3799 const uint8_t *hash,
3800 size_t hash_length,
3801 const uint8_t *signature,
3802 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003803{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003804 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003805 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003806
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003807 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003808 if( status != PSA_SUCCESS )
3809 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003810
Steven Cooreman55ae2172020-07-17 19:46:15 +02003811 /* Try any of the available accelerators first */
3812 status = psa_driver_wrapper_verify_hash( slot,
3813 alg,
3814 hash,
3815 hash_length,
3816 signature,
3817 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003818 if( status != PSA_ERROR_NOT_SUPPORTED ||
3819 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +02003820 return status;
3821
Gilles Peskine61b91d42018-06-08 16:09:36 +02003822#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003823 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003824 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003825 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003826
Steven Cooreman4fed4552020-08-03 14:46:03 +02003827 status = psa_load_rsa_representation( slot->attr.type,
3828 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003829 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003830 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003831 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003832 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02003833
Steven Cooremana2371e52020-07-28 14:30:39 +02003834 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003835 alg,
3836 hash, hash_length,
3837 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003838 mbedtls_rsa_free( rsa );
3839 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003840 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003841 }
3842 else
3843#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003844#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003845 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003846 {
John Durkopf87e3ae2020-10-26 15:25:23 -07003847#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003848 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003849 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003850 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003851 status = psa_load_ecp_representation( slot->attr.type,
3852 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003853 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003854 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003855 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003856 return( status );
Steven Cooremana2371e52020-07-28 14:30:39 +02003857 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003858 hash, hash_length,
3859 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003860 mbedtls_ecp_keypair_free( ecp );
3861 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02003862 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02003863 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003864 else
John Durkopf87e3ae2020-10-26 15:25:23 -07003865#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003866 {
3867 return( PSA_ERROR_INVALID_ARGUMENT );
3868 }
itayzafrir5c753392018-05-08 11:18:38 +03003869 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003870 else
3871#endif /* defined(MBEDTLS_ECP_C) */
3872 {
3873 return( PSA_ERROR_NOT_SUPPORTED );
3874 }
3875}
3876
Gilles Peskine072ac562018-06-30 00:21:29 +02003877#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3878static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3879 mbedtls_rsa_context *rsa )
3880{
3881 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3882 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3883 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3884 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3885}
3886#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3887
Gilles Peskinec5487a82018-12-03 18:08:14 +01003888psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003889 psa_algorithm_t alg,
3890 const uint8_t *input,
3891 size_t input_length,
3892 const uint8_t *salt,
3893 size_t salt_length,
3894 uint8_t *output,
3895 size_t output_size,
3896 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003897{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003898 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003899 psa_status_t status;
3900
Darryl Green5cc689a2018-07-24 15:34:10 +01003901 (void) input;
3902 (void) input_length;
3903 (void) salt;
3904 (void) output;
3905 (void) output_size;
3906
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003907 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003908
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003909 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3910 return( PSA_ERROR_INVALID_ARGUMENT );
3911
Gilles Peskine28f8f302019-07-24 13:30:31 +02003912 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003913 if( status != PSA_SUCCESS )
3914 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003915 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3916 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003917 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003918
3919#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003920 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003921 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003922 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003923 status = psa_load_rsa_representation( slot->attr.type,
3924 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003925 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003926 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003927 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003928 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003929
3930 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003931 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003932 status = PSA_ERROR_BUFFER_TOO_SMALL;
3933 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003934 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003936 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003937 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003938 status = mbedtls_to_psa_error(
3939 mbedtls_rsa_pkcs1_encrypt( rsa,
3940 mbedtls_ctr_drbg_random,
3941 &global_data.ctr_drbg,
3942 MBEDTLS_RSA_PUBLIC,
3943 input_length,
3944 input,
3945 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003946 }
3947 else
3948#endif /* MBEDTLS_PKCS1_V15 */
3949#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003950 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003951 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003952 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003953 status = mbedtls_to_psa_error(
3954 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3955 mbedtls_ctr_drbg_random,
3956 &global_data.ctr_drbg,
3957 MBEDTLS_RSA_PUBLIC,
3958 salt, salt_length,
3959 input_length,
3960 input,
3961 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003962 }
3963 else
3964#endif /* MBEDTLS_PKCS1_V21 */
3965 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003966 status = PSA_ERROR_INVALID_ARGUMENT;
3967 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003969rsa_exit:
3970 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003971 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003972
Steven Cooremana2371e52020-07-28 14:30:39 +02003973 mbedtls_rsa_free( rsa );
3974 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003975 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003976 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003977 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003978#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003979 {
3980 return( PSA_ERROR_NOT_SUPPORTED );
3981 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003982}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003983
Gilles Peskinec5487a82018-12-03 18:08:14 +01003984psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003985 psa_algorithm_t alg,
3986 const uint8_t *input,
3987 size_t input_length,
3988 const uint8_t *salt,
3989 size_t salt_length,
3990 uint8_t *output,
3991 size_t output_size,
3992 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003993{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003994 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003995 psa_status_t status;
3996
Darryl Green5cc689a2018-07-24 15:34:10 +01003997 (void) input;
3998 (void) input_length;
3999 (void) salt;
4000 (void) output;
4001 (void) output_size;
4002
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004003 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004004
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004005 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4006 return( PSA_ERROR_INVALID_ARGUMENT );
4007
Gilles Peskine28f8f302019-07-24 13:30:31 +02004008 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004009 if( status != PSA_SUCCESS )
4010 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004011 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004012 return( PSA_ERROR_INVALID_ARGUMENT );
4013
4014#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004015 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004016 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004017 mbedtls_rsa_context *rsa = NULL;
4018 status = psa_load_rsa_representation( slot->attr.type,
4019 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004020 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004021 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004022 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004023 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004024
Steven Cooremana2371e52020-07-28 14:30:39 +02004025 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004026 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004027 status = PSA_ERROR_INVALID_ARGUMENT;
4028 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004029 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004030
4031#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004032 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004033 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004034 status = mbedtls_to_psa_error(
4035 mbedtls_rsa_pkcs1_decrypt( rsa,
4036 mbedtls_ctr_drbg_random,
4037 &global_data.ctr_drbg,
4038 MBEDTLS_RSA_PRIVATE,
4039 output_length,
4040 input,
4041 output,
4042 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004043 }
4044 else
4045#endif /* MBEDTLS_PKCS1_V15 */
4046#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004047 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004048 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004049 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004050 status = mbedtls_to_psa_error(
4051 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4052 mbedtls_ctr_drbg_random,
4053 &global_data.ctr_drbg,
4054 MBEDTLS_RSA_PRIVATE,
4055 salt, salt_length,
4056 output_length,
4057 input,
4058 output,
4059 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004060 }
4061 else
4062#endif /* MBEDTLS_PKCS1_V21 */
4063 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004064 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004065 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004066
Steven Cooreman4fed4552020-08-03 14:46:03 +02004067rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004068 mbedtls_rsa_free( rsa );
4069 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004070 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004071 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004072 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004073#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004074 {
4075 return( PSA_ERROR_NOT_SUPPORTED );
4076 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004077}
Gilles Peskine20035e32018-02-03 22:44:14 +01004078
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004079
4080
mohammad1603503973b2018-03-12 15:59:30 +02004081/****************************************************************/
4082/* Symmetric cryptography */
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
Steven Cooremand3feccd2020-09-01 15:56:14 +02004099 /* A context must be freshly initialized before it can be set up. */
4100 if( operation->alg != 0 )
4101 return( PSA_ERROR_BAD_STATE );
4102
Steven Cooremana07b9972020-09-10 14:54:14 +02004103 /* The requested algorithm must be one that can be processed by cipher. */
4104 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004105 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004106
Steven Cooremanef8575e2020-09-11 11:44:50 +02004107 /* Fetch key material from key storage. */
4108 status = psa_get_key_from_slot( handle, &slot, usage, alg );
4109 if( status != PSA_SUCCESS )
4110 goto exit;
4111
4112 /* Initialize the operation struct members, except for alg. The alg member
4113 * is used to indicate to psa_cipher_abort that there are resources to free,
4114 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004115 operation->key_set = 0;
4116 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004117 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004118 operation->iv_size = 0;
4119 operation->block_size = 0;
4120 if( alg == PSA_ALG_ECB_NO_PADDING )
4121 operation->iv_required = 0;
4122 else
4123 operation->iv_required = 1;
4124
Steven Cooremana07b9972020-09-10 14:54:14 +02004125 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004126 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004127 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004128 slot,
4129 alg );
4130 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004131 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004132 slot,
4133 alg );
4134
Steven Cooremanef8575e2020-09-11 11:44:50 +02004135 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004136 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004137 /* Once the driver context is initialised, it needs to be freed using
4138 * psa_cipher_abort. Indicate this through setting alg. */
4139 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004140 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004141
Steven Cooremand3feccd2020-09-01 15:56:14 +02004142 if( status != PSA_ERROR_NOT_SUPPORTED ||
4143 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4144 goto exit;
4145
Steven Cooremana07b9972020-09-10 14:54:14 +02004146 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004147 * available for the given algorithm & key. */
4148 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004149
Steven Cooremana07b9972020-09-10 14:54:14 +02004150 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004151 * psa_cipher_abort. Indicate there is something to be freed through setting
4152 * alg, and indicate the operation is being done using mbedtls crypto through
4153 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004154 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004155 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004156
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004157 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004158 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004159 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004160 {
4161 status = PSA_ERROR_NOT_SUPPORTED;
4162 goto exit;
4163 }
mohammad1603503973b2018-03-12 15:59:30 +02004164
mohammad1603503973b2018-03-12 15:59:30 +02004165 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004166 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004167 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004168
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004169#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004170 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004171 {
4172 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004173 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004174 memcpy( keys, slot->data.key.data, 16 );
4175 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004176 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4177 keys,
4178 192, cipher_operation );
4179 }
4180 else
4181#endif
4182 {
4183 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004184 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004185 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004186 }
Moran Peker41deec42018-04-04 15:43:05 +03004187 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004188 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004189
mohammad16038481e742018-03-18 13:57:31 +02004190#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004191 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004192 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004193 case PSA_ALG_CBC_NO_PADDING:
4194 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4195 MBEDTLS_PADDING_NONE );
4196 break;
4197 case PSA_ALG_CBC_PKCS7:
4198 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4199 MBEDTLS_PADDING_PKCS7 );
4200 break;
4201 default:
4202 /* The algorithm doesn't involve padding. */
4203 ret = 0;
4204 break;
4205 }
4206 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004207 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004208#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4209
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004210 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004211 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004212 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4213 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004214 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004215 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004216 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004217#if defined(MBEDTLS_CHACHA20_C)
4218 else
4219 if( alg == PSA_ALG_CHACHA20 )
4220 operation->iv_size = 12;
4221#endif
mohammad1603503973b2018-03-12 15:59:30 +02004222
Steven Cooreman7df02922020-09-09 15:28:49 +02004223 status = PSA_SUCCESS;
4224
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004225exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004226 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004227 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004228 if( status == PSA_SUCCESS )
4229 {
4230 /* Update operation flags for both driver and software implementations */
4231 operation->key_set = 1;
4232 }
4233 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004234 psa_cipher_abort( operation );
4235 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004236}
4237
Gilles Peskinefe119512018-07-08 21:39:34 +02004238psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004239 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004240 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004241{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004242 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004243}
4244
Gilles Peskinefe119512018-07-08 21:39:34 +02004245psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004246 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004247 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004248{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004249 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004250}
4251
Gilles Peskinefe119512018-07-08 21:39:34 +02004252psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004253 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004254 size_t iv_size,
4255 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004256{
itayzafrir534bd7c2018-08-02 13:56:32 +03004257 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004258 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004259 if( operation->iv_set || ! operation->iv_required )
4260 {
4261 return( PSA_ERROR_BAD_STATE );
4262 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004263
Steven Cooremanef8575e2020-09-11 11:44:50 +02004264 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004265 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004266 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004267 iv,
4268 iv_size,
4269 iv_length );
4270 goto exit;
4271 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004272
Moran Peker41deec42018-04-04 15:43:05 +03004273 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004274 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004275 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004276 goto exit;
4277 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004278 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4279 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004280 if( ret != 0 )
4281 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004282 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004283 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004284 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004285
mohammad16038481e742018-03-18 13:57:31 +02004286 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004287 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004288
Moran Peker395db872018-05-31 14:07:14 +03004289exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004290 if( status == PSA_SUCCESS )
4291 operation->iv_set = 1;
4292 else
Moran Peker395db872018-05-31 14:07:14 +03004293 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004294 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004295}
4296
Gilles Peskinefe119512018-07-08 21:39:34 +02004297psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004298 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004299 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004300{
itayzafrir534bd7c2018-08-02 13:56:32 +03004301 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004303 if( operation->iv_set || ! operation->iv_required )
4304 {
4305 return( PSA_ERROR_BAD_STATE );
4306 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004307
Steven Cooremanef8575e2020-09-11 11:44:50 +02004308 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004309 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004310 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004311 iv,
4312 iv_length );
4313 goto exit;
4314 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004315
Moran Pekera28258c2018-05-29 16:25:04 +03004316 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004317 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004318 status = PSA_ERROR_INVALID_ARGUMENT;
4319 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004320 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004321 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4322 status = mbedtls_to_psa_error( ret );
4323exit:
4324 if( status == PSA_SUCCESS )
4325 operation->iv_set = 1;
4326 else
mohammad1603503973b2018-03-12 15:59:30 +02004327 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004328 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004329}
4330
Steven Cooreman177deba2020-09-07 17:14:14 +02004331/* Process input for which the algorithm is set to ECB mode. This requires
4332 * manual processing, since the PSA API is defined as being able to process
4333 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4334 * underlying mbedtls_cipher_update only takes full blocks. */
4335static psa_status_t psa_cipher_update_ecb_internal(
4336 mbedtls_cipher_context_t *ctx,
4337 const uint8_t *input,
4338 size_t input_length,
4339 uint8_t *output,
4340 size_t output_size,
4341 size_t *output_length )
4342{
4343 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4344 size_t block_size = ctx->cipher_info->block_size;
4345 size_t internal_output_length = 0;
4346 *output_length = 0;
4347
4348 if( input_length == 0 )
4349 {
4350 status = PSA_SUCCESS;
4351 goto exit;
4352 }
4353
4354 if( ctx->unprocessed_len > 0 )
4355 {
4356 /* Fill up to block size, and run the block if there's a full one. */
4357 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4358
4359 if( input_length < bytes_to_copy )
4360 bytes_to_copy = input_length;
4361
4362 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4363 input, bytes_to_copy );
4364 input_length -= bytes_to_copy;
4365 input += bytes_to_copy;
4366 ctx->unprocessed_len += bytes_to_copy;
4367
4368 if( ctx->unprocessed_len == block_size )
4369 {
4370 status = mbedtls_to_psa_error(
4371 mbedtls_cipher_update( ctx,
4372 ctx->unprocessed_data,
4373 block_size,
4374 output, &internal_output_length ) );
4375
4376 if( status != PSA_SUCCESS )
4377 goto exit;
4378
4379 output += internal_output_length;
4380 output_size -= internal_output_length;
4381 *output_length += internal_output_length;
4382 ctx->unprocessed_len = 0;
4383 }
4384 }
4385
4386 while( input_length >= block_size )
4387 {
4388 /* Run all full blocks we have, one by one */
4389 status = mbedtls_to_psa_error(
4390 mbedtls_cipher_update( ctx, input,
4391 block_size,
4392 output, &internal_output_length ) );
4393
4394 if( status != PSA_SUCCESS )
4395 goto exit;
4396
4397 input_length -= block_size;
4398 input += block_size;
4399
4400 output += internal_output_length;
4401 output_size -= internal_output_length;
4402 *output_length += internal_output_length;
4403 }
4404
4405 if( input_length > 0 )
4406 {
4407 /* Save unprocessed bytes for later processing */
4408 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4409 input, input_length );
4410 ctx->unprocessed_len += input_length;
4411 }
4412
4413 status = PSA_SUCCESS;
4414
4415exit:
4416 return( status );
4417}
4418
Gilles Peskinee553c652018-06-04 16:22:46 +02004419psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4420 const uint8_t *input,
4421 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004422 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004423 size_t output_size,
4424 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004425{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004426 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004427 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004428 if( operation->alg == 0 )
4429 {
4430 return( PSA_ERROR_BAD_STATE );
4431 }
4432 if( operation->iv_required && ! operation->iv_set )
4433 {
4434 return( PSA_ERROR_BAD_STATE );
4435 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004436
Steven Cooremanef8575e2020-09-11 11:44:50 +02004437 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004438 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004439 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004440 input,
4441 input_length,
4442 output,
4443 output_size,
4444 output_length );
4445 goto exit;
4446 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004447
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004448 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004449 {
4450 /* Take the unprocessed partial block left over from previous
4451 * update calls, if any, plus the input to this call. Remove
4452 * the last partial block, if any. You get the data that will be
4453 * output in this call. */
4454 expected_output_size =
4455 ( operation->ctx.cipher.unprocessed_len + input_length )
4456 / operation->block_size * operation->block_size;
4457 }
4458 else
4459 {
4460 expected_output_size = input_length;
4461 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004462
Gilles Peskine89d789c2018-06-04 17:17:16 +02004463 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004464 {
4465 status = PSA_ERROR_BUFFER_TOO_SMALL;
4466 goto exit;
4467 }
mohammad160382759612018-03-12 18:16:40 +02004468
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004469 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4470 {
4471 /* mbedtls_cipher_update has an API inconsistency: it will only
4472 * process a single block at a time in ECB mode. Abstract away that
4473 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004474 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4475 input,
4476 input_length,
4477 output,
4478 output_size,
4479 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004480 }
4481 else
4482 {
4483 status = mbedtls_to_psa_error(
4484 mbedtls_cipher_update( &operation->ctx.cipher, input,
4485 input_length, output, output_length ) );
4486 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004487exit:
4488 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004489 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004490 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004491}
4492
Gilles Peskinee553c652018-06-04 16:22:46 +02004493psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4494 uint8_t *output,
4495 size_t output_size,
4496 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004497{
David Saadab4ecc272019-02-14 13:48:10 +02004498 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004499 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004500 if( operation->alg == 0 )
4501 {
4502 return( PSA_ERROR_BAD_STATE );
4503 }
4504 if( operation->iv_required && ! operation->iv_set )
4505 {
4506 return( PSA_ERROR_BAD_STATE );
4507 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004508
Steven Cooremanef8575e2020-09-11 11:44:50 +02004509 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004510 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004511 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004512 output,
4513 output_size,
4514 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004515 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004516 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004517
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004518 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004519 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004520 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004521 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004522 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004523 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004524 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004525 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004526 }
4527
Steven Cooremanef8575e2020-09-11 11:44:50 +02004528 status = mbedtls_to_psa_error(
4529 mbedtls_cipher_finish( &operation->ctx.cipher,
4530 temp_output_buffer,
4531 output_length ) );
4532 if( status != PSA_SUCCESS )
4533 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004534
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004535 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004536 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004537 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004538 memcpy( output, temp_output_buffer, *output_length );
4539 else
Janos Follath315b51c2018-07-09 16:04:51 +01004540 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004541
Steven Cooremanef8575e2020-09-11 11:44:50 +02004542exit:
4543 if( operation->mbedtls_in_use == 1 )
4544 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004545
Steven Cooremanef8575e2020-09-11 11:44:50 +02004546 if( status == PSA_SUCCESS )
4547 return( psa_cipher_abort( operation ) );
4548 else
4549 {
4550 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004551 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004552
Steven Cooremanef8575e2020-09-11 11:44:50 +02004553 return( status );
4554 }
mohammad1603503973b2018-03-12 15:59:30 +02004555}
4556
Gilles Peskinee553c652018-06-04 16:22:46 +02004557psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4558{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004559 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004560 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004561 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004562 * in use. It's ok to call abort on such an object, and there's
4563 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004564 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004565 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004566
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004567 /* Sanity check (shouldn't happen: operation->alg should
4568 * always have been initialized to a valid value). */
4569 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4570 return( PSA_ERROR_BAD_STATE );
4571
Steven Cooremanef8575e2020-09-11 11:44:50 +02004572 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004573 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004574 else
4575 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004576
Moran Peker41deec42018-04-04 15:43:05 +03004577 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004578 operation->key_set = 0;
4579 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004580 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004581 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004582 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004583 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004584
Moran Peker395db872018-05-31 14:07:14 +03004585 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004586}
4587
Gilles Peskinea0655c32018-04-30 17:06:50 +02004588
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004589
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004590
mohammad16035955c982018-04-26 00:53:03 +03004591/****************************************************************/
4592/* AEAD */
4593/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004594
Gilles Peskineedf9a652018-08-17 18:11:56 +02004595typedef struct
4596{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004597 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004598 const mbedtls_cipher_info_t *cipher_info;
4599 union
4600 {
4601#if defined(MBEDTLS_CCM_C)
4602 mbedtls_ccm_context ccm;
4603#endif /* MBEDTLS_CCM_C */
4604#if defined(MBEDTLS_GCM_C)
4605 mbedtls_gcm_context gcm;
4606#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004607#if defined(MBEDTLS_CHACHAPOLY_C)
4608 mbedtls_chachapoly_context chachapoly;
4609#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004610 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004611 psa_algorithm_t core_alg;
4612 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004613 uint8_t tag_length;
4614} aead_operation_t;
4615
Gilles Peskine30a9e412019-01-14 18:36:12 +01004616static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004617{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004618 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004619 {
4620#if defined(MBEDTLS_CCM_C)
4621 case PSA_ALG_CCM:
4622 mbedtls_ccm_free( &operation->ctx.ccm );
4623 break;
4624#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004625#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004626 case PSA_ALG_GCM:
4627 mbedtls_gcm_free( &operation->ctx.gcm );
4628 break;
4629#endif /* MBEDTLS_GCM_C */
4630 }
4631}
4632
4633static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004634 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004635 psa_key_usage_t usage,
4636 psa_algorithm_t alg )
4637{
4638 psa_status_t status;
4639 size_t key_bits;
4640 mbedtls_cipher_id_t cipher_id;
4641
Gilles Peskine28f8f302019-07-24 13:30:31 +02004642 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004643 if( status != PSA_SUCCESS )
4644 return( status );
4645
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004646 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004647
4648 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004649 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004650 &cipher_id );
4651 if( operation->cipher_info == NULL )
4652 return( PSA_ERROR_NOT_SUPPORTED );
4653
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004654 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004655 {
4656#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004657 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4658 operation->core_alg = PSA_ALG_CCM;
4659 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004660 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4661 * The call to mbedtls_ccm_encrypt_and_tag or
4662 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004663 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004664 return( PSA_ERROR_INVALID_ARGUMENT );
4665 mbedtls_ccm_init( &operation->ctx.ccm );
4666 status = mbedtls_to_psa_error(
4667 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004668 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004669 (unsigned int) key_bits ) );
4670 if( status != 0 )
4671 goto cleanup;
4672 break;
4673#endif /* MBEDTLS_CCM_C */
4674
4675#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004676 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4677 operation->core_alg = PSA_ALG_GCM;
4678 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004679 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4680 * The call to mbedtls_gcm_crypt_and_tag or
4681 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004682 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004683 return( PSA_ERROR_INVALID_ARGUMENT );
4684 mbedtls_gcm_init( &operation->ctx.gcm );
4685 status = mbedtls_to_psa_error(
4686 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004687 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004688 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004689 if( status != 0 )
4690 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004691 break;
4692#endif /* MBEDTLS_GCM_C */
4693
Gilles Peskine26869f22019-05-06 15:25:00 +02004694#if defined(MBEDTLS_CHACHAPOLY_C)
4695 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4696 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4697 operation->full_tag_length = 16;
4698 /* We only support the default tag length. */
4699 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4700 return( PSA_ERROR_NOT_SUPPORTED );
4701 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4702 status = mbedtls_to_psa_error(
4703 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004704 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004705 if( status != 0 )
4706 goto cleanup;
4707 break;
4708#endif /* MBEDTLS_CHACHAPOLY_C */
4709
Gilles Peskineedf9a652018-08-17 18:11:56 +02004710 default:
4711 return( PSA_ERROR_NOT_SUPPORTED );
4712 }
4713
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004714 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4715 {
4716 status = PSA_ERROR_INVALID_ARGUMENT;
4717 goto cleanup;
4718 }
4719 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004720
Gilles Peskineedf9a652018-08-17 18:11:56 +02004721 return( PSA_SUCCESS );
4722
4723cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004724 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004725 return( status );
4726}
4727
Gilles Peskinec5487a82018-12-03 18:08:14 +01004728psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004729 psa_algorithm_t alg,
4730 const uint8_t *nonce,
4731 size_t nonce_length,
4732 const uint8_t *additional_data,
4733 size_t additional_data_length,
4734 const uint8_t *plaintext,
4735 size_t plaintext_length,
4736 uint8_t *ciphertext,
4737 size_t ciphertext_size,
4738 size_t *ciphertext_length )
4739{
mohammad16035955c982018-04-26 00:53:03 +03004740 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004741 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004742 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004743
mohammad1603f08a5502018-06-03 15:05:47 +03004744 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004745
Gilles Peskinec5487a82018-12-03 18:08:14 +01004746 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004747 if( status != PSA_SUCCESS )
4748 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004749
Gilles Peskineedf9a652018-08-17 18:11:56 +02004750 /* For all currently supported modes, the tag is at the end of the
4751 * ciphertext. */
4752 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4753 {
4754 status = PSA_ERROR_BUFFER_TOO_SMALL;
4755 goto exit;
4756 }
4757 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004758
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004759#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004760 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004761 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004762 status = mbedtls_to_psa_error(
4763 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4764 MBEDTLS_GCM_ENCRYPT,
4765 plaintext_length,
4766 nonce, nonce_length,
4767 additional_data, additional_data_length,
4768 plaintext, ciphertext,
4769 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004770 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004771 else
4772#endif /* MBEDTLS_GCM_C */
4773#if defined(MBEDTLS_CCM_C)
4774 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004775 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004776 status = mbedtls_to_psa_error(
4777 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4778 plaintext_length,
4779 nonce, nonce_length,
4780 additional_data,
4781 additional_data_length,
4782 plaintext, ciphertext,
4783 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004784 }
mohammad16035c8845f2018-05-09 05:40:09 -07004785 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004786#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004787#if defined(MBEDTLS_CHACHAPOLY_C)
4788 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4789 {
4790 if( nonce_length != 12 || operation.tag_length != 16 )
4791 {
4792 status = PSA_ERROR_NOT_SUPPORTED;
4793 goto exit;
4794 }
4795 status = mbedtls_to_psa_error(
4796 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4797 plaintext_length,
4798 nonce,
4799 additional_data,
4800 additional_data_length,
4801 plaintext,
4802 ciphertext,
4803 tag ) );
4804 }
4805 else
4806#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004807 {
mohammad1603554faad2018-06-03 15:07:38 +03004808 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004809 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004810
Gilles Peskineedf9a652018-08-17 18:11:56 +02004811 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4812 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004813
Gilles Peskineedf9a652018-08-17 18:11:56 +02004814exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004815 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004816 if( status == PSA_SUCCESS )
4817 *ciphertext_length = plaintext_length + operation.tag_length;
4818 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004819}
4820
Gilles Peskineee652a32018-06-01 19:23:52 +02004821/* Locate the tag in a ciphertext buffer containing the encrypted data
4822 * followed by the tag. Return the length of the part preceding the tag in
4823 * *plaintext_length. This is the size of the plaintext in modes where
4824 * the encrypted data has the same size as the plaintext, such as
4825 * CCM and GCM. */
4826static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4827 const uint8_t *ciphertext,
4828 size_t ciphertext_length,
4829 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004830 const uint8_t **p_tag )
4831{
4832 size_t payload_length;
4833 if( tag_length > ciphertext_length )
4834 return( PSA_ERROR_INVALID_ARGUMENT );
4835 payload_length = ciphertext_length - tag_length;
4836 if( payload_length > plaintext_size )
4837 return( PSA_ERROR_BUFFER_TOO_SMALL );
4838 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004839 return( PSA_SUCCESS );
4840}
4841
Gilles Peskinec5487a82018-12-03 18:08:14 +01004842psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004843 psa_algorithm_t alg,
4844 const uint8_t *nonce,
4845 size_t nonce_length,
4846 const uint8_t *additional_data,
4847 size_t additional_data_length,
4848 const uint8_t *ciphertext,
4849 size_t ciphertext_length,
4850 uint8_t *plaintext,
4851 size_t plaintext_size,
4852 size_t *plaintext_length )
4853{
mohammad16035955c982018-04-26 00:53:03 +03004854 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004855 aead_operation_t operation;
4856 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004857
Gilles Peskineee652a32018-06-01 19:23:52 +02004858 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004859
Gilles Peskinec5487a82018-12-03 18:08:14 +01004860 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004861 if( status != PSA_SUCCESS )
4862 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004863
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004864 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4865 ciphertext, ciphertext_length,
4866 plaintext_size, &tag );
4867 if( status != PSA_SUCCESS )
4868 goto exit;
4869
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004870#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004871 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004872 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004873 status = mbedtls_to_psa_error(
4874 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4875 ciphertext_length - operation.tag_length,
4876 nonce, nonce_length,
4877 additional_data,
4878 additional_data_length,
4879 tag, operation.tag_length,
4880 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004881 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004882 else
4883#endif /* MBEDTLS_GCM_C */
4884#if defined(MBEDTLS_CCM_C)
4885 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004886 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004887 status = mbedtls_to_psa_error(
4888 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4889 ciphertext_length - operation.tag_length,
4890 nonce, nonce_length,
4891 additional_data,
4892 additional_data_length,
4893 ciphertext, plaintext,
4894 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004895 }
mohammad160339574652018-06-01 04:39:53 -07004896 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004897#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004898#if defined(MBEDTLS_CHACHAPOLY_C)
4899 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4900 {
4901 if( nonce_length != 12 || operation.tag_length != 16 )
4902 {
4903 status = PSA_ERROR_NOT_SUPPORTED;
4904 goto exit;
4905 }
4906 status = mbedtls_to_psa_error(
4907 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4908 ciphertext_length - operation.tag_length,
4909 nonce,
4910 additional_data,
4911 additional_data_length,
4912 tag,
4913 ciphertext,
4914 plaintext ) );
4915 }
4916 else
4917#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004918 {
mohammad1603554faad2018-06-03 15:07:38 +03004919 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004920 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004921
Gilles Peskineedf9a652018-08-17 18:11:56 +02004922 if( status != PSA_SUCCESS && plaintext_size != 0 )
4923 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004924
Gilles Peskineedf9a652018-08-17 18:11:56 +02004925exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004926 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004927 if( status == PSA_SUCCESS )
4928 *plaintext_length = ciphertext_length - operation.tag_length;
4929 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004930}
4931
Gilles Peskinea0655c32018-04-30 17:06:50 +02004932
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004933
Gilles Peskine20035e32018-02-03 22:44:14 +01004934/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004935/* Generators */
4936/****************************************************************/
4937
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004938#define HKDF_STATE_INIT 0 /* no input yet */
4939#define HKDF_STATE_STARTED 1 /* got salt */
4940#define HKDF_STATE_KEYED 2 /* got key */
4941#define HKDF_STATE_OUTPUT 3 /* output started */
4942
Gilles Peskinecbe66502019-05-16 16:59:18 +02004943static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004944 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004945{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004946 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4947 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004948 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004950}
4951
4952
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004953psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004954{
4955 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004956 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004957 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004958 {
4959 /* The object has (apparently) been initialized but it is not
4960 * in use. It's ok to call abort on such an object, and there's
4961 * nothing to do. */
4962 }
4963 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004964#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004965 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004966 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004967 mbedtls_free( operation->ctx.hkdf.info );
4968 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004969 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004970 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004972 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004973 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004974 if( operation->ctx.tls12_prf.seed != NULL )
4975 {
4976 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4977 operation->ctx.tls12_prf.seed_length );
4978 mbedtls_free( operation->ctx.tls12_prf.seed );
4979 }
4980
4981 if( operation->ctx.tls12_prf.label != NULL )
4982 {
4983 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4984 operation->ctx.tls12_prf.label_length );
4985 mbedtls_free( operation->ctx.tls12_prf.label );
4986 }
4987
4988 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4989
4990 /* We leave the fields Ai and output_block to be erased safely by the
4991 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004992 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004993 else
4994#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004995 {
4996 status = PSA_ERROR_BAD_STATE;
4997 }
Janos Follath083036a2019-06-11 10:22:26 +01004998 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004999 return( status );
5000}
5001
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005002psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005003 size_t *capacity)
5004{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005005 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005006 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005007 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005008 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005009 }
5010
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005011 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005012 return( PSA_SUCCESS );
5013}
5014
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005015psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005016 size_t capacity )
5017{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005018 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005019 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005020 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005021 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005022 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005023 return( PSA_SUCCESS );
5024}
5025
Gilles Peskinebef7f142018-07-12 17:22:21 +02005026#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005027/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005028 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005029static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005030 psa_algorithm_t hash_alg,
5031 uint8_t *output,
5032 size_t output_length )
5033{
5034 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5035 psa_status_t status;
5036
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005037 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5038 return( PSA_ERROR_BAD_STATE );
5039 hkdf->state = HKDF_STATE_OUTPUT;
5040
Gilles Peskinebef7f142018-07-12 17:22:21 +02005041 while( output_length != 0 )
5042 {
5043 /* Copy what remains of the current block */
5044 uint8_t n = hash_length - hkdf->offset_in_block;
5045 if( n > output_length )
5046 n = (uint8_t) output_length;
5047 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5048 output += n;
5049 output_length -= n;
5050 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005051 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005052 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005053 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005054 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005055 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005056 * object was corrupted or if this function is called directly
5057 * inside the library. */
5058 if( hkdf->block_number == 0xff )
5059 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005060
5061 /* We need a new block */
5062 ++hkdf->block_number;
5063 hkdf->offset_in_block = 0;
5064 status = psa_hmac_setup_internal( &hkdf->hmac,
5065 hkdf->prk, hash_length,
5066 hash_alg );
5067 if( status != PSA_SUCCESS )
5068 return( status );
5069 if( hkdf->block_number != 1 )
5070 {
5071 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5072 hkdf->output_block,
5073 hash_length );
5074 if( status != PSA_SUCCESS )
5075 return( status );
5076 }
5077 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5078 hkdf->info,
5079 hkdf->info_length );
5080 if( status != PSA_SUCCESS )
5081 return( status );
5082 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5083 &hkdf->block_number, 1 );
5084 if( status != PSA_SUCCESS )
5085 return( status );
5086 status = psa_hmac_finish_internal( &hkdf->hmac,
5087 hkdf->output_block,
5088 sizeof( hkdf->output_block ) );
5089 if( status != PSA_SUCCESS )
5090 return( status );
5091 }
5092
5093 return( PSA_SUCCESS );
5094}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005095
Janos Follath7742fee2019-06-17 12:58:10 +01005096static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5097 psa_tls12_prf_key_derivation_t *tls12_prf,
5098 psa_algorithm_t alg )
5099{
5100 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5101 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005102 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5103 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005104
Janos Follath7742fee2019-06-17 12:58:10 +01005105 /* We can't be wanting more output after block 0xff, otherwise
5106 * the capacity check in psa_key_derivation_output_bytes() would have
5107 * prevented this call. It could happen only if the operation
5108 * object was corrupted or if this function is called directly
5109 * inside the library. */
5110 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005111 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005112
5113 /* We need a new block */
5114 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005115 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005116
5117 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5118 *
5119 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5120 *
5121 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5122 * HMAC_hash(secret, A(2) + seed) +
5123 * HMAC_hash(secret, A(3) + seed) + ...
5124 *
5125 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005126 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005127 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005128 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005129 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005130 * is currently extracted as `output_block` and where i is
5131 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005132 */
5133
Janos Follathea29bfb2019-06-19 12:21:20 +01005134 /* Save the hash context before using it, to preserve the hash state with
5135 * only the inner padding in it. We need this, because inner padding depends
5136 * on the key (secret in the RFC's terminology). */
5137 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5138 if( status != PSA_SUCCESS )
5139 goto cleanup;
5140
5141 /* Calculate A(i) where i = tls12_prf->block_number. */
5142 if( tls12_prf->block_number == 1 )
5143 {
5144 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5145 * the variable seed and in this instance means it in the context of the
5146 * P_hash function, where seed = label + seed.) */
5147 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5148 tls12_prf->label, tls12_prf->label_length );
5149 if( status != PSA_SUCCESS )
5150 goto cleanup;
5151 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5152 tls12_prf->seed, tls12_prf->seed_length );
5153 if( status != PSA_SUCCESS )
5154 goto cleanup;
5155 }
5156 else
5157 {
5158 /* A(i) = HMAC_hash(secret, A(i-1)) */
5159 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5160 tls12_prf->Ai, hash_length );
5161 if( status != PSA_SUCCESS )
5162 goto cleanup;
5163 }
5164
5165 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5166 tls12_prf->Ai, hash_length );
5167 if( status != PSA_SUCCESS )
5168 goto cleanup;
5169 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5170 if( status != PSA_SUCCESS )
5171 goto cleanup;
5172
5173 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5174 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5175 tls12_prf->Ai, hash_length );
5176 if( status != PSA_SUCCESS )
5177 goto cleanup;
5178 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5179 tls12_prf->label, tls12_prf->label_length );
5180 if( status != PSA_SUCCESS )
5181 goto cleanup;
5182 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5183 tls12_prf->seed, tls12_prf->seed_length );
5184 if( status != PSA_SUCCESS )
5185 goto cleanup;
5186 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5187 tls12_prf->output_block, hash_length );
5188 if( status != PSA_SUCCESS )
5189 goto cleanup;
5190 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5191 if( status != PSA_SUCCESS )
5192 goto cleanup;
5193
Janos Follath7742fee2019-06-17 12:58:10 +01005194
5195cleanup:
5196
Janos Follathea29bfb2019-06-19 12:21:20 +01005197 cleanup_status = psa_hash_abort( &backup );
5198 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5199 status = cleanup_status;
5200
5201 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005202}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005203
Janos Follath844eb0e2019-06-19 12:10:49 +01005204static psa_status_t psa_key_derivation_tls12_prf_read(
5205 psa_tls12_prf_key_derivation_t *tls12_prf,
5206 psa_algorithm_t alg,
5207 uint8_t *output,
5208 size_t output_length )
5209{
5210 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5211 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5212 psa_status_t status;
5213 uint8_t offset, length;
5214
5215 while( output_length != 0 )
5216 {
5217 /* Check if we have fully processed the current block. */
5218 if( tls12_prf->left_in_block == 0 )
5219 {
5220 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5221 alg );
5222 if( status != PSA_SUCCESS )
5223 return( status );
5224
5225 continue;
5226 }
5227
5228 if( tls12_prf->left_in_block > output_length )
5229 length = (uint8_t) output_length;
5230 else
5231 length = tls12_prf->left_in_block;
5232
5233 offset = hash_length - tls12_prf->left_in_block;
5234 memcpy( output, tls12_prf->output_block + offset, length );
5235 output += length;
5236 output_length -= length;
5237 tls12_prf->left_in_block -= length;
5238 }
5239
5240 return( PSA_SUCCESS );
5241}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005242#endif /* MBEDTLS_MD_C */
5243
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005244psa_status_t psa_key_derivation_output_bytes(
5245 psa_key_derivation_operation_t *operation,
5246 uint8_t *output,
5247 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005248{
5249 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005250 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005251
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005252 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005253 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005254 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005255 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005256 }
5257
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005258 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005259 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005260 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005261 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005262 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005263 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005264 goto exit;
5265 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005266 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005267 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005268 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005269 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005270 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5271 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005273 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005274 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005275 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005276 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005277
Gilles Peskinebef7f142018-07-12 17:22:21 +02005278#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005279 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005280 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005281 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005282 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005283 output, output_length );
5284 }
Janos Follathadbec812019-06-14 11:05:39 +01005285 else
Janos Follathadbec812019-06-14 11:05:39 +01005286 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005287 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005288 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005289 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005290 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005291 output_length );
5292 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005293 else
5294#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005295 {
5296 return( PSA_ERROR_BAD_STATE );
5297 }
5298
5299exit:
5300 if( status != PSA_SUCCESS )
5301 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005302 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005303 * This allows us to differentiate between exhausted operations and
5304 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5305 * operations. */
5306 psa_algorithm_t alg = operation->alg;
5307 psa_key_derivation_abort( operation );
5308 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005309 memset( output, '!', output_length );
5310 }
5311 return( status );
5312}
5313
Gilles Peskine08542d82018-07-19 17:05:42 +02005314#if defined(MBEDTLS_DES_C)
5315static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5316{
5317 if( data_size >= 8 )
5318 mbedtls_des_key_set_parity( data );
5319 if( data_size >= 16 )
5320 mbedtls_des_key_set_parity( data + 8 );
5321 if( data_size >= 24 )
5322 mbedtls_des_key_set_parity( data + 16 );
5323}
5324#endif /* MBEDTLS_DES_C */
5325
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005326static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005327 psa_key_slot_t *slot,
5328 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005329 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005330{
5331 uint8_t *data = NULL;
5332 size_t bytes = PSA_BITS_TO_BYTES( bits );
5333 psa_status_t status;
5334
Gilles Peskine8e338702019-07-30 20:06:31 +02005335 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005336 return( PSA_ERROR_INVALID_ARGUMENT );
5337 if( bits % 8 != 0 )
5338 return( PSA_ERROR_INVALID_ARGUMENT );
5339 data = mbedtls_calloc( 1, bytes );
5340 if( data == NULL )
5341 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5342
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005343 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005344 if( status != PSA_SUCCESS )
5345 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005346#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005347 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005348 psa_des_set_key_parity( data, bytes );
5349#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005350 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005351
5352exit:
5353 mbedtls_free( data );
5354 return( status );
5355}
5356
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005357psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005358 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005359 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005360{
5361 psa_status_t status;
5362 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005363 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005364
5365 /* Reject any attempt to create a zero-length key so that we don't
5366 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5367 if( psa_get_key_bits( attributes ) == 0 )
5368 return( PSA_ERROR_INVALID_ARGUMENT );
5369
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005370 if( ! operation->can_output_key )
5371 return( PSA_ERROR_NOT_PERMITTED );
5372
Gilles Peskinedf179142019-07-15 22:02:14 +02005373 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5374 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005375#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5376 if( driver != NULL )
5377 {
5378 /* Deriving a key in a secure element is not implemented yet. */
5379 status = PSA_ERROR_NOT_SUPPORTED;
5380 }
5381#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005382 if( status == PSA_SUCCESS )
5383 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005384 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005385 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005386 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005387 }
5388 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005389 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005390 if( status != PSA_SUCCESS )
5391 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005392 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005393 *handle = 0;
5394 }
5395 return( status );
5396}
5397
Gilles Peskineeab56e42018-07-12 17:12:33 +02005398
5399
5400/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005401/* Key derivation */
5402/****************************************************************/
5403
Gilles Peskine969c5d62019-01-16 15:53:06 +01005404static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005405 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005406 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005407{
Janos Follath5fe19732019-06-20 15:09:30 +01005408 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5409 * initialisers for this union leave some bytes unspecified.) */
5410 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5411
Gilles Peskine969c5d62019-01-16 15:53:06 +01005412 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005413#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005414 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5415 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5416 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005417 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005418 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005419 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5420 if( hash_size == 0 )
5421 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005422 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5423 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005424 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005425 {
5426 return( PSA_ERROR_NOT_SUPPORTED );
5427 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005428 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005429 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005430 }
5431#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005432 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005433 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005434}
5435
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005436psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005437 psa_algorithm_t alg )
5438{
5439 psa_status_t status;
5440
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005441 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005442 return( PSA_ERROR_BAD_STATE );
5443
Gilles Peskine6843c292019-01-18 16:44:49 +01005444 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5445 return( PSA_ERROR_INVALID_ARGUMENT );
5446 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005447 {
5448 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005449 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005450 }
5451 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5452 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005453 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005454 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005455 else
5456 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005457
5458 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005459 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005460 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005461}
5462
5463#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005464static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005465 psa_algorithm_t hash_alg,
5466 psa_key_derivation_step_t step,
5467 const uint8_t *data,
5468 size_t data_length )
5469{
5470 psa_status_t status;
5471 switch( step )
5472 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005473 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005474 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005475 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005476 status = psa_hmac_setup_internal( &hkdf->hmac,
5477 data, data_length,
5478 hash_alg );
5479 if( status != PSA_SUCCESS )
5480 return( status );
5481 hkdf->state = HKDF_STATE_STARTED;
5482 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005483 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005484 /* If no salt was provided, use an empty salt. */
5485 if( hkdf->state == HKDF_STATE_INIT )
5486 {
5487 status = psa_hmac_setup_internal( &hkdf->hmac,
5488 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005489 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005490 if( status != PSA_SUCCESS )
5491 return( status );
5492 hkdf->state = HKDF_STATE_STARTED;
5493 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005494 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005495 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005496 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5497 data, data_length );
5498 if( status != PSA_SUCCESS )
5499 return( status );
5500 status = psa_hmac_finish_internal( &hkdf->hmac,
5501 hkdf->prk,
5502 sizeof( hkdf->prk ) );
5503 if( status != PSA_SUCCESS )
5504 return( status );
5505 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5506 hkdf->block_number = 0;
5507 hkdf->state = HKDF_STATE_KEYED;
5508 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005509 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005510 if( hkdf->state == HKDF_STATE_OUTPUT )
5511 return( PSA_ERROR_BAD_STATE );
5512 if( hkdf->info_set )
5513 return( PSA_ERROR_BAD_STATE );
5514 hkdf->info_length = data_length;
5515 if( data_length != 0 )
5516 {
5517 hkdf->info = mbedtls_calloc( 1, data_length );
5518 if( hkdf->info == NULL )
5519 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5520 memcpy( hkdf->info, data, data_length );
5521 }
5522 hkdf->info_set = 1;
5523 return( PSA_SUCCESS );
5524 default:
5525 return( PSA_ERROR_INVALID_ARGUMENT );
5526 }
5527}
Janos Follathb03233e2019-06-11 15:30:30 +01005528
Janos Follathf08e2652019-06-13 09:05:41 +01005529static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5530 const uint8_t *data,
5531 size_t data_length )
5532{
5533 if( prf->state != TLS12_PRF_STATE_INIT )
5534 return( PSA_ERROR_BAD_STATE );
5535
Janos Follathd6dce9f2019-07-04 09:11:38 +01005536 if( data_length != 0 )
5537 {
5538 prf->seed = mbedtls_calloc( 1, data_length );
5539 if( prf->seed == NULL )
5540 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005541
Janos Follathd6dce9f2019-07-04 09:11:38 +01005542 memcpy( prf->seed, data, data_length );
5543 prf->seed_length = data_length;
5544 }
Janos Follathf08e2652019-06-13 09:05:41 +01005545
5546 prf->state = TLS12_PRF_STATE_SEED_SET;
5547
5548 return( PSA_SUCCESS );
5549}
5550
Janos Follath81550542019-06-13 14:26:34 +01005551static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5552 psa_algorithm_t hash_alg,
5553 const uint8_t *data,
5554 size_t data_length )
5555{
5556 psa_status_t status;
5557 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5558 return( PSA_ERROR_BAD_STATE );
5559
5560 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5561 if( status != PSA_SUCCESS )
5562 return( status );
5563
5564 prf->state = TLS12_PRF_STATE_KEY_SET;
5565
5566 return( PSA_SUCCESS );
5567}
5568
Janos Follath6660f0e2019-06-17 08:44:03 +01005569static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5570 psa_tls12_prf_key_derivation_t *prf,
5571 psa_algorithm_t hash_alg,
5572 const uint8_t *data,
5573 size_t data_length )
5574{
5575 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005576 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5577 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005578
5579 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5580 return( PSA_ERROR_INVALID_ARGUMENT );
5581
5582 /* Quoting RFC 4279, Section 2:
5583 *
5584 * The premaster secret is formed as follows: if the PSK is N octets
5585 * long, concatenate a uint16 with the value N, N zero octets, a second
5586 * uint16 with the value N, and the PSK itself.
5587 */
5588
Janos Follath40e13932019-06-26 13:22:29 +01005589 *cur++ = ( data_length >> 8 ) & 0xff;
5590 *cur++ = ( data_length >> 0 ) & 0xff;
5591 memset( cur, 0, data_length );
5592 cur += data_length;
5593 *cur++ = pms[0];
5594 *cur++ = pms[1];
5595 memcpy( cur, data, data_length );
5596 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005597
Janos Follath40e13932019-06-26 13:22:29 +01005598 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005599
5600 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5601 return( status );
5602}
5603
Janos Follath63028dd2019-06-13 09:15:47 +01005604static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5605 const uint8_t *data,
5606 size_t data_length )
5607{
5608 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5609 return( PSA_ERROR_BAD_STATE );
5610
Janos Follathd6dce9f2019-07-04 09:11:38 +01005611 if( data_length != 0 )
5612 {
5613 prf->label = mbedtls_calloc( 1, data_length );
5614 if( prf->label == NULL )
5615 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005616
Janos Follathd6dce9f2019-07-04 09:11:38 +01005617 memcpy( prf->label, data, data_length );
5618 prf->label_length = data_length;
5619 }
Janos Follath63028dd2019-06-13 09:15:47 +01005620
5621 prf->state = TLS12_PRF_STATE_LABEL_SET;
5622
5623 return( PSA_SUCCESS );
5624}
5625
Janos Follathb03233e2019-06-11 15:30:30 +01005626static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5627 psa_algorithm_t hash_alg,
5628 psa_key_derivation_step_t step,
5629 const uint8_t *data,
5630 size_t data_length )
5631{
Janos Follathb03233e2019-06-11 15:30:30 +01005632 switch( step )
5633 {
Janos Follathf08e2652019-06-13 09:05:41 +01005634 case PSA_KEY_DERIVATION_INPUT_SEED:
5635 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005636 case PSA_KEY_DERIVATION_INPUT_SECRET:
5637 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005638 case PSA_KEY_DERIVATION_INPUT_LABEL:
5639 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005640 default:
5641 return( PSA_ERROR_INVALID_ARGUMENT );
5642 }
5643}
Janos Follath6660f0e2019-06-17 08:44:03 +01005644
5645static psa_status_t psa_tls12_prf_psk_to_ms_input(
5646 psa_tls12_prf_key_derivation_t *prf,
5647 psa_algorithm_t hash_alg,
5648 psa_key_derivation_step_t step,
5649 const uint8_t *data,
5650 size_t data_length )
5651{
5652 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005653 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005654 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5655 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005656 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005657
5658 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5659}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005660#endif /* MBEDTLS_MD_C */
5661
Gilles Peskineb8965192019-09-24 16:21:10 +02005662/** Check whether the given key type is acceptable for the given
5663 * input step of a key derivation.
5664 *
5665 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5666 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5667 * Both secret and non-secret inputs can alternatively have the type
5668 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5669 * that the input was passed as a buffer rather than via a key object.
5670 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005671static int psa_key_derivation_check_input_type(
5672 psa_key_derivation_step_t step,
5673 psa_key_type_t key_type )
5674{
5675 switch( step )
5676 {
5677 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005678 if( key_type == PSA_KEY_TYPE_DERIVE )
5679 return( PSA_SUCCESS );
5680 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005681 return( PSA_SUCCESS );
5682 break;
5683 case PSA_KEY_DERIVATION_INPUT_LABEL:
5684 case PSA_KEY_DERIVATION_INPUT_SALT:
5685 case PSA_KEY_DERIVATION_INPUT_INFO:
5686 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005687 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5688 return( PSA_SUCCESS );
5689 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005690 return( PSA_SUCCESS );
5691 break;
5692 }
5693 return( PSA_ERROR_INVALID_ARGUMENT );
5694}
5695
Janos Follathb80a94e2019-06-12 15:54:46 +01005696static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005697 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005698 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005699 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005700 const uint8_t *data,
5701 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005702{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005703 psa_status_t status;
5704 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5705
5706 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005707 if( status != PSA_SUCCESS )
5708 goto exit;
5709
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005710#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005711 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005712 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005713 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005714 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005715 step, data, data_length );
5716 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005717 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005718 {
Janos Follathb03233e2019-06-11 15:30:30 +01005719 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5720 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5721 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005722 }
5723 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5724 {
5725 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5726 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5727 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005728 }
5729 else
5730#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005731 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005732 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005733 return( PSA_ERROR_BAD_STATE );
5734 }
5735
Gilles Peskine224b0d62019-09-23 18:13:17 +02005736exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005737 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005738 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005739 return( status );
5740}
5741
Janos Follath51f4a0f2019-06-14 11:35:55 +01005742psa_status_t psa_key_derivation_input_bytes(
5743 psa_key_derivation_operation_t *operation,
5744 psa_key_derivation_step_t step,
5745 const uint8_t *data,
5746 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005747{
Gilles Peskineb8965192019-09-24 16:21:10 +02005748 return( psa_key_derivation_input_internal( operation, step,
5749 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005750 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005751}
5752
Janos Follath51f4a0f2019-06-14 11:35:55 +01005753psa_status_t psa_key_derivation_input_key(
5754 psa_key_derivation_operation_t *operation,
5755 psa_key_derivation_step_t step,
5756 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005757{
5758 psa_key_slot_t *slot;
5759 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005760
Gilles Peskine28f8f302019-07-24 13:30:31 +02005761 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005762 PSA_KEY_USAGE_DERIVE,
5763 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005764 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005765 {
5766 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005767 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005768 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005769
5770 /* Passing a key object as a SECRET input unlocks the permission
5771 * to output to a key object. */
5772 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5773 operation->can_output_key = 1;
5774
Janos Follathb80a94e2019-06-12 15:54:46 +01005775 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005776 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005777 slot->data.key.data,
5778 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005779}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005780
5781
5782
5783/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005784/* Key agreement */
5785/****************************************************************/
5786
Gilles Peskinea05219c2018-11-16 16:02:56 +01005787#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005788static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5789 size_t peer_key_length,
5790 const mbedtls_ecp_keypair *our_key,
5791 uint8_t *shared_secret,
5792 size_t shared_secret_size,
5793 size_t *shared_secret_length )
5794{
Steven Cooremand4867872020-08-05 16:31:39 +02005795 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005796 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005797 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005798 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005799 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005800 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005801
Steven Cooreman4fed4552020-08-03 14:46:03 +02005802 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5803 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005804 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005805 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005806 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005807 goto exit;
5808
Jaeden Amero97271b32019-01-10 19:38:51 +00005809 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005810 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005811 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005812 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005813 status = mbedtls_to_psa_error(
5814 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5815 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005816 goto exit;
5817
Jaeden Amero97271b32019-01-10 19:38:51 +00005818 status = mbedtls_to_psa_error(
5819 mbedtls_ecdh_calc_secret( &ecdh,
5820 shared_secret_length,
5821 shared_secret, shared_secret_size,
5822 mbedtls_ctr_drbg_random,
5823 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005824 if( status != PSA_SUCCESS )
5825 goto exit;
5826 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5827 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005828
5829exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005830 if( status != PSA_SUCCESS )
5831 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005832 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005833 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005834 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005835
Jaeden Amero97271b32019-01-10 19:38:51 +00005836 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005837}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005838#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005839
Gilles Peskine01d718c2018-09-18 12:01:02 +02005840#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5841
Gilles Peskine0216fe12019-04-11 21:23:21 +02005842static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5843 psa_key_slot_t *private_key,
5844 const uint8_t *peer_key,
5845 size_t peer_key_length,
5846 uint8_t *shared_secret,
5847 size_t shared_secret_size,
5848 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005849{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005850 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005851 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005852#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005853 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005854 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005855 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005856 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005857 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005858 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005859 private_key->data.key.data,
5860 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005861 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005862 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005863 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005864 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005865 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005866 shared_secret, shared_secret_size,
5867 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005868 mbedtls_ecp_keypair_free( ecp );
5869 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005870 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005871#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005872 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005873 (void) private_key;
5874 (void) peer_key;
5875 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005876 (void) shared_secret;
5877 (void) shared_secret_size;
5878 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005879 return( PSA_ERROR_NOT_SUPPORTED );
5880 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005881}
5882
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005883/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005884 * to potentially free embedded data structures and wipe confidential data.
5885 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005886static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005887 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005888 psa_key_slot_t *private_key,
5889 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005890 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005891{
5892 psa_status_t status;
5893 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5894 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005895 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005896
5897 /* Step 1: run the secret agreement algorithm to generate the shared
5898 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005899 status = psa_key_agreement_raw_internal( ka_alg,
5900 private_key,
5901 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005902 shared_secret,
5903 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005904 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005905 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005906 goto exit;
5907
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005908 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005909 * the shared secret. A shared secret is permitted wherever a key
5910 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005911 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005912 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005913 shared_secret,
5914 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005915exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005916 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005917 return( status );
5918}
5919
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005920psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005921 psa_key_derivation_step_t step,
5922 psa_key_handle_t private_key,
5923 const uint8_t *peer_key,
5924 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005925{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005926 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005927 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005928 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005929 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005930 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005931 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005932 if( status != PSA_SUCCESS )
5933 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005934 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005935 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005936 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005937 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005938 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005939 else
5940 {
5941 /* If a private key has been added as SECRET, we allow the derived
5942 * key material to be used as a key in PSA Crypto. */
5943 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5944 operation->can_output_key = 1;
5945 }
Gilles Peskine346797d2018-11-16 16:05:06 +01005946 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005947}
5948
Gilles Peskinebe697d82019-05-16 18:00:41 +02005949psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5950 psa_key_handle_t private_key,
5951 const uint8_t *peer_key,
5952 size_t peer_key_length,
5953 uint8_t *output,
5954 size_t output_size,
5955 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005956{
5957 psa_key_slot_t *slot;
5958 psa_status_t status;
5959
5960 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5961 {
5962 status = PSA_ERROR_INVALID_ARGUMENT;
5963 goto exit;
5964 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005965 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005966 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005967 if( status != PSA_SUCCESS )
5968 goto exit;
5969
5970 status = psa_key_agreement_raw_internal( alg, slot,
5971 peer_key, peer_key_length,
5972 output, output_size,
5973 output_length );
5974
5975exit:
5976 if( status != PSA_SUCCESS )
5977 {
5978 /* If an error happens and is not handled properly, the output
5979 * may be used as a key to protect sensitive data. Arrange for such
5980 * a key to be random, which is likely to result in decryption or
5981 * verification errors. This is better than filling the buffer with
5982 * some constant data such as zeros, which would result in the data
5983 * being protected with a reproducible, easily knowable key.
5984 */
5985 psa_generate_random( output, output_size );
5986 *output_length = output_size;
5987 }
5988 return( status );
5989}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005990
5991
5992/****************************************************************/
5993/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005994/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005995
Gilles Peskine4c317f42018-07-12 01:24:09 +02005996psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005997 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005998{
Janos Follath24eed8d2019-11-22 13:21:35 +00005999 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006000 GUARD_MODULE_INITIALIZED;
6001
Gilles Peskinef181eca2019-08-07 13:49:00 +02006002 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6003 {
6004 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6005 output,
6006 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6007 if( ret != 0 )
6008 return( mbedtls_to_psa_error( ret ) );
6009 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6010 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6011 }
6012
Gilles Peskinee59236f2018-01-27 23:32:46 +01006013 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6014 return( mbedtls_to_psa_error( ret ) );
6015}
6016
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006017#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6018#include "mbedtls/entropy_poll.h"
6019
Gilles Peskine7228da22019-07-15 11:06:15 +02006020psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006021 size_t seed_size )
6022{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006023 if( global_data.initialized )
6024 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006025
6026 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6027 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6028 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6029 return( PSA_ERROR_INVALID_ARGUMENT );
6030
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006031 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006032}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006033#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006034
Gilles Peskinee56e8782019-04-26 17:34:02 +02006035#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
6036static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6037 size_t domain_parameters_size,
6038 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006039{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006040 size_t i;
6041 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006042
Gilles Peskinee56e8782019-04-26 17:34:02 +02006043 if( domain_parameters_size == 0 )
6044 {
6045 *exponent = 65537;
6046 return( PSA_SUCCESS );
6047 }
6048
6049 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6050 * support values that fit in a 32-bit integer, which is larger than
6051 * int on just about every platform anyway. */
6052 if( domain_parameters_size > sizeof( acc ) )
6053 return( PSA_ERROR_NOT_SUPPORTED );
6054 for( i = 0; i < domain_parameters_size; i++ )
6055 acc = ( acc << 8 ) | domain_parameters[i];
6056 if( acc > INT_MAX )
6057 return( PSA_ERROR_NOT_SUPPORTED );
6058 *exponent = acc;
6059 return( PSA_SUCCESS );
6060}
6061#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6062
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006063static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006064 psa_key_slot_t *slot, size_t bits,
6065 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006066{
Gilles Peskine8e338702019-07-30 20:06:31 +02006067 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006068
Gilles Peskinee56e8782019-04-26 17:34:02 +02006069 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006070 return( PSA_ERROR_INVALID_ARGUMENT );
6071
Gilles Peskinee59236f2018-01-27 23:32:46 +01006072 if( key_type_is_raw_bytes( type ) )
6073 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006074 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006075
6076 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006077 if( status != PSA_SUCCESS )
6078 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006079
6080 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006081 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6082 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006083 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006084
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006085 status = psa_generate_random( slot->data.key.data,
6086 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006087 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006088 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006089
6090 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006091#if defined(MBEDTLS_DES_C)
6092 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006093 psa_des_set_key_parity( slot->data.key.data,
6094 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006095#endif /* MBEDTLS_DES_C */
6096 }
6097 else
6098
6099#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006100 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006101 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006102 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006103 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006104 int exponent;
6105 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006106 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6107 return( PSA_ERROR_NOT_SUPPORTED );
6108 /* Accept only byte-aligned keys, for the same reasons as
6109 * in psa_import_rsa_key(). */
6110 if( bits % 8 != 0 )
6111 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006112 status = psa_read_rsa_exponent( domain_parameters,
6113 domain_parameters_size,
6114 &exponent );
6115 if( status != PSA_SUCCESS )
6116 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006117 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6118 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006119 mbedtls_ctr_drbg_random,
6120 &global_data.ctr_drbg,
6121 (unsigned int) bits,
6122 exponent );
6123 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006124 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006125
6126 /* Make sure to always have an export representation available */
6127 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6128
Steven Cooreman75b74362020-07-28 14:30:13 +02006129 status = psa_allocate_buffer_to_slot( slot, bytes );
6130 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006131 {
6132 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006133 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006134 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006135
6136 status = psa_export_rsa_key( type,
6137 &rsa,
6138 slot->data.key.data,
6139 bytes,
6140 &slot->data.key.bytes );
6141 mbedtls_rsa_free( &rsa );
6142 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006143 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006144 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006145 }
6146 else
6147#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6148
6149#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006150 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006151 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006152 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006153 mbedtls_ecp_group_id grp_id =
6154 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006155 const mbedtls_ecp_curve_info *curve_info =
6156 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006157 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006158 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006159 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006160 return( PSA_ERROR_NOT_SUPPORTED );
6161 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6162 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006163 mbedtls_ecp_keypair_init( &ecp );
6164 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006165 mbedtls_ctr_drbg_random,
6166 &global_data.ctr_drbg );
6167 if( ret != 0 )
6168 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006169 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006170 return( mbedtls_to_psa_error( ret ) );
6171 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006172
6173
6174 /* Make sure to always have an export representation available */
6175 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006176 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6177 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006178 {
6179 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006180 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006181 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006182
6183 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006184 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6185
6186 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006187 if( status != PSA_SUCCESS ) {
6188 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006189 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006190 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006191 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006192 }
6193 else
6194#endif /* MBEDTLS_ECP_C */
Steven Cooreman29149862020-08-05 15:43:42 +02006195 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006196 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006197 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006198
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006199 return( PSA_SUCCESS );
6200}
Darryl Green0c6575a2018-11-07 16:05:30 +00006201
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006202psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006203 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006204{
6205 psa_status_t status;
6206 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006207 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006208
Gilles Peskine0f84d622019-09-12 19:03:13 +02006209 /* Reject any attempt to create a zero-length key so that we don't
6210 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6211 if( psa_get_key_bits( attributes ) == 0 )
6212 return( PSA_ERROR_INVALID_ARGUMENT );
6213
Gilles Peskinedf179142019-07-15 22:02:14 +02006214 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6215 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006216 if( status != PSA_SUCCESS )
6217 goto exit;
6218
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006219 status = psa_driver_wrapper_generate_key( attributes,
6220 slot );
6221 if( status != PSA_ERROR_NOT_SUPPORTED ||
6222 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6223 goto exit;
6224
6225 status = psa_generate_key_internal(
6226 slot, attributes->core.bits,
6227 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006228
6229exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006230 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006231 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006232 if( status != PSA_SUCCESS )
6233 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006234 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006235 *handle = 0;
6236 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006237 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006238}
6239
6240
Gilles Peskinee59236f2018-01-27 23:32:46 +01006241
6242/****************************************************************/
6243/* Module setup */
6244/****************************************************************/
6245
Gilles Peskine5e769522018-11-20 21:59:56 +01006246psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6247 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6248 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6249{
6250 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6251 return( PSA_ERROR_BAD_STATE );
6252 global_data.entropy_init = entropy_init;
6253 global_data.entropy_free = entropy_free;
6254 return( PSA_SUCCESS );
6255}
6256
Gilles Peskinee59236f2018-01-27 23:32:46 +01006257void mbedtls_psa_crypto_free( void )
6258{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006259 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006260 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6261 {
6262 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006263 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006264 }
6265 /* Wipe all remaining data, including configuration.
6266 * In particular, this sets all state indicator to the value
6267 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006268 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006269#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006270 /* Unregister all secure element drivers, so that we restart from
6271 * a pristine state. */
6272 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006273#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006274}
6275
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006276#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6277/** Recover a transaction that was interrupted by a power failure.
6278 *
6279 * This function is called during initialization, before psa_crypto_init()
6280 * returns. If this function returns a failure status, the initialization
6281 * fails.
6282 */
6283static psa_status_t psa_crypto_recover_transaction(
6284 const psa_crypto_transaction_t *transaction )
6285{
6286 switch( transaction->unknown.type )
6287 {
6288 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6289 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006290 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006291 * is implemented.
6292 * https://github.com/ARMmbed/mbed-crypto/issues/218
6293 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006294 default:
6295 /* We found an unsupported transaction in the storage.
6296 * We don't know what state the storage is in. Give up. */
6297 return( PSA_ERROR_STORAGE_FAILURE );
6298 }
6299}
6300#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6301
Gilles Peskinee59236f2018-01-27 23:32:46 +01006302psa_status_t psa_crypto_init( void )
6303{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006304 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006305 const unsigned char drbg_seed[] = "PSA";
6306
Gilles Peskinec6b69072018-11-20 21:42:52 +01006307 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006308 if( global_data.initialized != 0 )
6309 return( PSA_SUCCESS );
6310
Gilles Peskine5e769522018-11-20 21:59:56 +01006311 /* Set default configuration if
6312 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6313 if( global_data.entropy_init == NULL )
6314 global_data.entropy_init = mbedtls_entropy_init;
6315 if( global_data.entropy_free == NULL )
6316 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006317
Gilles Peskinec6b69072018-11-20 21:42:52 +01006318 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006319 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006320#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6321 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6322 /* The PSA entropy injection feature depends on using NV seed as an entropy
6323 * source. Add NV seed as an entropy source for PSA entropy injection. */
6324 mbedtls_entropy_add_source( &global_data.entropy,
6325 mbedtls_nv_seed_poll, NULL,
6326 MBEDTLS_ENTROPY_BLOCK_SIZE,
6327 MBEDTLS_ENTROPY_SOURCE_STRONG );
6328#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006329 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006330 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006331 status = mbedtls_to_psa_error(
6332 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6333 mbedtls_entropy_func,
6334 &global_data.entropy,
6335 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6336 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006337 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006338 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006339
Gilles Peskine66fb1262018-12-10 16:29:04 +01006340 status = psa_initialize_key_slots( );
6341 if( status != PSA_SUCCESS )
6342 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006343
Gilles Peskined9348f22019-10-01 15:22:29 +02006344#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6345 status = psa_init_all_se_drivers( );
6346 if( status != PSA_SUCCESS )
6347 goto exit;
6348#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6349
Gilles Peskine4b734222019-07-24 15:56:31 +02006350#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006351 status = psa_crypto_load_transaction( );
6352 if( status == PSA_SUCCESS )
6353 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006354 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6355 if( status != PSA_SUCCESS )
6356 goto exit;
6357 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006358 }
6359 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6360 {
6361 /* There's no transaction to complete. It's all good. */
6362 status = PSA_SUCCESS;
6363 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006364#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006365
Gilles Peskinec6b69072018-11-20 21:42:52 +01006366 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006367 global_data.initialized = 1;
6368
6369exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006370 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006371 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006372 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006373}
6374
6375#endif /* MBEDTLS_PSA_CRYPTO_C */