blob: 74b98714a6d086844a6c6fa17e6cc0c9b1733ea4 [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;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001042 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001043 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001044 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001045 /* Try validation through accelerators first. */
1046 bit_size = slot->attr.bits;
1047 psa_key_attributes_t attributes = {
1048 .core = slot->attr
1049 };
1050 status = psa_driver_wrapper_validate_key( &attributes,
1051 data,
1052 data_length,
1053 &bit_size );
1054 if( status == PSA_SUCCESS )
1055 {
1056 /* Key has been validated successfully by an accelerator.
1057 * Copy key material into slot. */
1058 status = psa_copy_key_material_into_slot( slot, data, data_length );
1059 if( status != PSA_SUCCESS )
1060 return( status );
1061
1062 slot->attr.bits = (psa_key_bits_t) bit_size;
1063 return( PSA_SUCCESS );
1064 }
1065 else if( status != PSA_ERROR_NOT_SUPPORTED )
1066 return( status );
1067
1068 /* Key format is not supported by any accelerator, try software fallback
1069 * if present. */
1070 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1071 {
Gilles Peskinef76aa772018-10-29 19:24:33 +01001072#if defined(MBEDTLS_ECP_C)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001073 status = psa_import_ecp_key( slot,
1074 data, data_length );
Steven Cooreman19fd5742020-07-24 23:31:01 +02001075#else
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001076 /* No drivers have been implemented yet, so without mbed TLS backing
1077 * there's no way to do ECP with the current library. */
1078 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman19fd5742020-07-24 23:31:01 +02001079#endif /* defined(MBEDTLS_ECP_C) */
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001080 }
1081 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1082 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001083#if defined(MBEDTLS_RSA_C)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001084 status = psa_import_rsa_key( slot,
1085 data, data_length );
Steven Cooreman19fd5742020-07-24 23:31:01 +02001086#else
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001087 /* No drivers have been implemented yet, so without mbed TLS backing
1088 * there's no way to do RSA with the current library. */
1089 status = PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman19fd5742020-07-24 23:31:01 +02001090#endif /* defined(MBEDTLS_RSA_C) */
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001091 }
1092 else
1093 {
1094 /* Unsupported asymmetric key type */
1095 status = PSA_ERROR_NOT_SUPPORTED;
1096 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001097 }
1098 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001099 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001100 /* Unknown key type */
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001101 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine969ac722018-01-28 18:16:59 +01001102 }
Darryl Green940d72c2018-07-13 13:18:51 +01001103
Darryl Green06fd18d2018-07-16 11:21:11 +01001104 return( status );
1105}
1106
Gilles Peskinef603c712019-01-19 13:40:11 +01001107/** Calculate the intersection of two algorithm usage policies.
1108 *
1109 * Return 0 (which allows no operation) on incompatibility.
1110 */
1111static psa_algorithm_t psa_key_policy_algorithm_intersection(
1112 psa_algorithm_t alg1,
1113 psa_algorithm_t alg2 )
1114{
Gilles Peskine549ea862019-05-22 11:45:59 +02001115 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001116 if( alg1 == alg2 )
1117 return( alg1 );
1118 /* If the policies are from the same hash-and-sign family, check
1119 * if one is a wildcard. If so the other has the specific algorithm. */
1120 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1121 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1122 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1123 {
1124 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1125 return( alg2 );
1126 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1127 return( alg1 );
1128 }
1129 /* If the policies are incompatible, allow nothing. */
1130 return( 0 );
1131}
1132
Gilles Peskined6f371b2019-05-10 19:33:38 +02001133static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1134 psa_algorithm_t requested_alg )
1135{
Gilles Peskine549ea862019-05-22 11:45:59 +02001136 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001137 if( requested_alg == policy_alg )
1138 return( 1 );
1139 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001140 * and requested_alg is the same hash-and-sign family with any hash,
1141 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001142 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1143 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1144 {
1145 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1146 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1147 }
Steven Cooremance48e852020-10-05 16:02:45 +02001148 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001149 * a key derivation with that key agreement should also be allowed. This
1150 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001151 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1152 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1153 {
1154 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1155 policy_alg );
1156 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001157 /* If it isn't permitted, it's forbidden. */
1158 return( 0 );
1159}
1160
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001161/** Test whether a policy permits an algorithm.
1162 *
1163 * The caller must test usage flags separately.
1164 */
1165static int psa_key_policy_permits( const psa_key_policy_t *policy,
1166 psa_algorithm_t alg )
1167{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001168 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1169 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001170}
1171
Gilles Peskinef603c712019-01-19 13:40:11 +01001172/** Restrict a key policy based on a constraint.
1173 *
1174 * \param[in,out] policy The policy to restrict.
1175 * \param[in] constraint The policy constraint to apply.
1176 *
1177 * \retval #PSA_SUCCESS
1178 * \c *policy contains the intersection of the original value of
1179 * \c *policy and \c *constraint.
1180 * \retval #PSA_ERROR_INVALID_ARGUMENT
1181 * \c *policy and \c *constraint are incompatible.
1182 * \c *policy is unchanged.
1183 */
1184static psa_status_t psa_restrict_key_policy(
1185 psa_key_policy_t *policy,
1186 const psa_key_policy_t *constraint )
1187{
1188 psa_algorithm_t intersection_alg =
1189 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001190 psa_algorithm_t intersection_alg2 =
1191 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001192 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1193 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001194 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1195 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001196 policy->usage &= constraint->usage;
1197 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001198 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001199 return( PSA_SUCCESS );
1200}
1201
Darryl Green06fd18d2018-07-16 11:21:11 +01001202/** Retrieve a slot which must contain a key. The key must have allow all the
1203 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1204 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001205static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001206 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001207 psa_key_usage_t usage,
1208 psa_algorithm_t alg )
1209{
1210 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001211 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001212
1213 *p_slot = NULL;
1214
Gilles Peskinec5487a82018-12-03 18:08:14 +01001215 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001216 if( status != PSA_SUCCESS )
1217 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001218
1219 /* Enforce that usage policy for the key slot contains all the flags
1220 * required by the usage parameter. There is one exception: public
1221 * keys can always be exported, so we treat public key objects as
1222 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001223 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001224 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001225 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001226 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001227
1228 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001229 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001230 return( PSA_ERROR_NOT_PERMITTED );
1231
1232 *p_slot = slot;
1233 return( PSA_SUCCESS );
1234}
Darryl Green940d72c2018-07-13 13:18:51 +01001235
Gilles Peskine28f8f302019-07-24 13:30:31 +02001236/** Retrieve a slot which must contain a transparent key.
1237 *
1238 * A transparent key is a key for which the key material is directly
1239 * available, as opposed to a key in a secure element.
1240 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001241 * This is a temporary function to use instead of psa_get_key_from_slot()
1242 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001243 */
1244#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1245static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1246 psa_key_slot_t **p_slot,
1247 psa_key_usage_t usage,
1248 psa_algorithm_t alg )
1249{
1250 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1251 if( status != PSA_SUCCESS )
1252 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001253 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001254 {
1255 *p_slot = NULL;
1256 return( PSA_ERROR_NOT_SUPPORTED );
1257 }
1258 return( PSA_SUCCESS );
1259}
1260#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1261/* With no secure element support, all keys are transparent. */
1262#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1263 psa_get_key_from_slot( handle, p_slot, usage, alg )
1264#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1265
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001266/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001267static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001268{
Gilles Peskine73167e12019-07-12 23:44:37 +02001269#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1270 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001271 {
1272 /* No key material to clean. */
1273 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001274 else
1275#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001276 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001277 /* Data pointer will always be either a valid pointer or NULL in an
1278 * initialized slot, so we can just free it. */
1279 if( slot->data.key.data != NULL )
1280 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001281 mbedtls_free( slot->data.key.data );
1282 slot->data.key.data = NULL;
1283 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001284 }
Darryl Green40225ba2018-11-15 14:48:15 +00001285
1286 return( PSA_SUCCESS );
1287}
1288
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001289/** Completely wipe a slot in memory, including its policy.
1290 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001291psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001292{
1293 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001294 /* Multipart operations may still be using the key. This is safe
1295 * because all multipart operation objects are independent from
1296 * the key slot: if they need to access the key after the setup
1297 * phase, they have a copy of the key. Note that this means that
1298 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001299 /* At this point, key material and other type-specific content has
1300 * been wiped. Clear remaining metadata. We can call memset and not
1301 * zeroize because the metadata is not particularly sensitive. */
1302 memset( slot, 0, sizeof( *slot ) );
1303 return( status );
1304}
1305
Gilles Peskinec5487a82018-12-03 18:08:14 +01001306psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001307{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001308 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001309 psa_status_t status; /* status of the last operation */
1310 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001311#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1312 psa_se_drv_table_entry_t *driver;
1313#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001314
Gilles Peskine1841cf42019-10-08 15:48:25 +02001315 if( handle == 0 )
1316 return( PSA_SUCCESS );
1317
Gilles Peskinec5487a82018-12-03 18:08:14 +01001318 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001319 if( status != PSA_SUCCESS )
1320 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001321
1322#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001323 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001324 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001325 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001326 /* For a key in a secure element, we need to do three things:
1327 * remove the key file in internal storage, destroy the
1328 * key inside the secure element, and update the driver's
1329 * persistent data. Start a transaction that will encompass these
1330 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001331 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001332 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001333 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001334 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001335 status = psa_crypto_save_transaction( );
1336 if( status != PSA_SUCCESS )
1337 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001338 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001339 /* We should still try to destroy the key in the secure
1340 * element and the key metadata in storage. This is especially
1341 * important if the error is that the storage is full.
1342 * But how to do it exactly without risking an inconsistent
1343 * state after a reset?
1344 * https://github.com/ARMmbed/mbed-crypto/issues/215
1345 */
1346 overall_status = status;
1347 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001348 }
1349
Gilles Peskine354f7672019-07-12 23:46:38 +02001350 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001351 if( overall_status == PSA_SUCCESS )
1352 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001353 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001354#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1355
Darryl Greend49a4992018-06-18 17:27:26 +01001356#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001357 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001358 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001359 status = psa_destroy_persistent_key( slot->attr.id );
1360 if( overall_status == PSA_SUCCESS )
1361 overall_status = status;
1362
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001363 /* TODO: other slots may have a copy of the same key. We should
1364 * invalidate them.
1365 * https://github.com/ARMmbed/mbed-crypto/issues/214
1366 */
Darryl Greend49a4992018-06-18 17:27:26 +01001367 }
1368#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001369
Gilles Peskinefc762652019-07-22 19:30:34 +02001370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1371 if( driver != NULL )
1372 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001373 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001374 if( overall_status == PSA_SUCCESS )
1375 overall_status = status;
1376 status = psa_crypto_stop_transaction( );
1377 if( overall_status == PSA_SUCCESS )
1378 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001379 }
1380#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1381
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001382#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1383exit:
1384#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001385 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001386 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1387 if( overall_status == PSA_SUCCESS )
1388 overall_status = status;
1389 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001390}
1391
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001392void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001393{
Gilles Peskineb699f072019-04-26 16:06:02 +02001394 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001395 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001396}
1397
Gilles Peskineb699f072019-04-26 16:06:02 +02001398psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1399 psa_key_type_t type,
1400 const uint8_t *data,
1401 size_t data_length )
1402{
1403 uint8_t *copy = NULL;
1404
1405 if( data_length != 0 )
1406 {
1407 copy = mbedtls_calloc( 1, data_length );
1408 if( copy == NULL )
1409 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1410 memcpy( copy, data, data_length );
1411 }
1412 /* After this point, this function is guaranteed to succeed, so it
1413 * can start modifying `*attributes`. */
1414
1415 if( attributes->domain_parameters != NULL )
1416 {
1417 mbedtls_free( attributes->domain_parameters );
1418 attributes->domain_parameters = NULL;
1419 attributes->domain_parameters_size = 0;
1420 }
1421
1422 attributes->domain_parameters = copy;
1423 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001424 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001425 return( PSA_SUCCESS );
1426}
1427
1428psa_status_t psa_get_key_domain_parameters(
1429 const psa_key_attributes_t *attributes,
1430 uint8_t *data, size_t data_size, size_t *data_length )
1431{
1432 if( attributes->domain_parameters_size > data_size )
1433 return( PSA_ERROR_BUFFER_TOO_SMALL );
1434 *data_length = attributes->domain_parameters_size;
1435 if( attributes->domain_parameters_size != 0 )
1436 memcpy( data, attributes->domain_parameters,
1437 attributes->domain_parameters_size );
1438 return( PSA_SUCCESS );
1439}
1440
1441#if defined(MBEDTLS_RSA_C)
1442static psa_status_t psa_get_rsa_public_exponent(
1443 const mbedtls_rsa_context *rsa,
1444 psa_key_attributes_t *attributes )
1445{
1446 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001448 uint8_t *buffer = NULL;
1449 size_t buflen;
1450 mbedtls_mpi_init( &mpi );
1451
1452 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1453 if( ret != 0 )
1454 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001455 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1456 {
1457 /* It's the default value, which is reported as an empty string,
1458 * so there's nothing to do. */
1459 goto exit;
1460 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001461
1462 buflen = mbedtls_mpi_size( &mpi );
1463 buffer = mbedtls_calloc( 1, buflen );
1464 if( buffer == NULL )
1465 {
1466 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1467 goto exit;
1468 }
1469 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1470 if( ret != 0 )
1471 goto exit;
1472 attributes->domain_parameters = buffer;
1473 attributes->domain_parameters_size = buflen;
1474
1475exit:
1476 mbedtls_mpi_free( &mpi );
1477 if( ret != 0 )
1478 mbedtls_free( buffer );
1479 return( mbedtls_to_psa_error( ret ) );
1480}
1481#endif /* MBEDTLS_RSA_C */
1482
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001483/** Retrieve all the publicly-accessible attributes of a key.
1484 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001485psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1486 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001487{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001488 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001489 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001490
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001491 psa_reset_key_attributes( attributes );
1492
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001493 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001494 if( status != PSA_SUCCESS )
1495 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001496
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001497 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001498 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1499 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1500
1501#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1502 if( psa_key_slot_is_external( slot ) )
1503 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1504#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001505
Gilles Peskine8e338702019-07-30 20:06:31 +02001506 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001507 {
1508#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001509 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001510 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001511#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001512 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001513 * is not yet implemented.
1514 * https://github.com/ARMmbed/mbed-crypto/issues/216
1515 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001516 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001517 break;
1518#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001519 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001520 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001521
Steven Cooreman4fed4552020-08-03 14:46:03 +02001522 status = psa_load_rsa_representation( slot->attr.type,
1523 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001524 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001525 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001526 if( status != PSA_SUCCESS )
1527 break;
1528
Steven Cooremana2371e52020-07-28 14:30:39 +02001529 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001530 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001531 mbedtls_rsa_free( rsa );
1532 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001533 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001534 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001535#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001536 default:
1537 /* Nothing else to do. */
1538 break;
1539 }
1540
1541 if( status != PSA_SUCCESS )
1542 psa_reset_key_attributes( attributes );
1543 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001544}
1545
Gilles Peskinec8000c02019-08-02 20:15:51 +02001546#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1547psa_status_t psa_get_key_slot_number(
1548 const psa_key_attributes_t *attributes,
1549 psa_key_slot_number_t *slot_number )
1550{
1551 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1552 {
1553 *slot_number = attributes->slot_number;
1554 return( PSA_SUCCESS );
1555 }
1556 else
1557 return( PSA_ERROR_INVALID_ARGUMENT );
1558}
1559#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1560
Steven Cooremana01795d2020-07-24 22:48:15 +02001561static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1562 uint8_t *data,
1563 size_t data_size,
1564 size_t *data_length )
1565{
1566 if( slot->data.key.bytes > data_size )
1567 return( PSA_ERROR_BUFFER_TOO_SMALL );
1568 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1569 memset( data + slot->data.key.bytes, 0,
1570 data_size - slot->data.key.bytes );
1571 *data_length = slot->data.key.bytes;
1572 return( PSA_SUCCESS );
1573}
1574
Gilles Peskinef603c712019-01-19 13:40:11 +01001575static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1576 uint8_t *data,
1577 size_t data_size,
1578 size_t *data_length,
1579 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001580{
Gilles Peskine5d309672019-07-12 23:47:28 +02001581#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1582 const psa_drv_se_t *drv;
1583 psa_drv_se_context_t *drv_context;
1584#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1585
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001586 *data_length = 0;
1587
Gilles Peskine8e338702019-07-30 20:06:31 +02001588 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001589 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001590
Gilles Peskinef9168942019-09-12 19:20:29 +02001591 /* Reject a zero-length output buffer now, since this can never be a
1592 * valid key representation. This way we know that data must be a valid
1593 * pointer and we can do things like memset(data, ..., data_size). */
1594 if( data_size == 0 )
1595 return( PSA_ERROR_BUFFER_TOO_SMALL );
1596
Gilles Peskine5d309672019-07-12 23:47:28 +02001597#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001598 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001599 {
1600 psa_drv_se_export_key_t method;
1601 if( drv->key_management == NULL )
1602 return( PSA_ERROR_NOT_SUPPORTED );
1603 method = ( export_public_key ?
1604 drv->key_management->p_export_public :
1605 drv->key_management->p_export );
1606 if( method == NULL )
1607 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001608 return( method( drv_context,
1609 slot->data.se.slot_number,
1610 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001611 }
1612#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1613
Gilles Peskine8e338702019-07-30 20:06:31 +02001614 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001615 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001616 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001617 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001618 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1619 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001620 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001621 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001622 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001623 /* Exporting public -> public */
1624 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1625 }
1626 else if( !export_public_key )
1627 {
1628 /* Exporting private -> private */
1629 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1630 }
1631 /* Need to export the public part of a private key,
1632 * so conversion is needed */
1633 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1634 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001635#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001636 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001637 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001638 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001639 slot->data.key.data,
1640 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001641 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001642 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001643 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001644
Steven Cooreman560c28a2020-07-24 23:20:24 +02001645 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001646 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001647 data,
1648 data_size,
1649 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001650
Steven Cooremana2371e52020-07-28 14:30:39 +02001651 mbedtls_rsa_free( rsa );
1652 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001653
Steven Cooreman560c28a2020-07-24 23:20:24 +02001654 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001655#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001656 /* We don't know how to convert a private RSA key to public. */
1657 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001658#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001659 }
1660 else
Moran Pekera998bc62018-04-16 18:16:20 +03001661 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001662#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001663 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001664 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001665 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001666 slot->data.key.data,
1667 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001668 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001669 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001670 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001671
1672 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1673 PSA_KEY_TYPE_ECC_GET_FAMILY(
1674 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001675 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001676 data,
1677 data_size,
1678 data_length );
1679
Steven Cooremana2371e52020-07-28 14:30:39 +02001680 mbedtls_ecp_keypair_free( ecp );
1681 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001682 return( status );
1683#else
1684 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001685 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001686#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001687 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001688 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001689 else
1690 {
1691 /* This shouldn't happen in the reference implementation, but
1692 it is valid for a special-purpose implementation to omit
1693 support for exporting certain key types. */
1694 return( PSA_ERROR_NOT_SUPPORTED );
1695 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001696}
1697
Gilles Peskinec5487a82018-12-03 18:08:14 +01001698psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001699 uint8_t *data,
1700 size_t data_size,
1701 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001702{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001703 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001704 psa_status_t status;
1705
1706 /* Set the key to empty now, so that even when there are errors, we always
1707 * set data_length to a value between 0 and data_size. On error, setting
1708 * the key to empty is a good choice because an empty key representation is
1709 * unlikely to be accepted anywhere. */
1710 *data_length = 0;
1711
1712 /* Export requires the EXPORT flag. There is an exception for public keys,
1713 * which don't require any flag, but psa_get_key_from_slot takes
1714 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001715 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001716 if( status != PSA_SUCCESS )
1717 return( status );
1718 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001719 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001720}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001721
Gilles Peskinec5487a82018-12-03 18:08:14 +01001722psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001723 uint8_t *data,
1724 size_t data_size,
1725 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001726{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001727 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001728 psa_status_t status;
1729
1730 /* Set the key to empty now, so that even when there are errors, we always
1731 * set data_length to a value between 0 and data_size. On error, setting
1732 * the key to empty is a good choice because an empty key representation is
1733 * unlikely to be accepted anywhere. */
1734 *data_length = 0;
1735
1736 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001737 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001738 if( status != PSA_SUCCESS )
1739 return( status );
1740 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001741 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001742}
1743
Gilles Peskine91e8c332019-08-02 19:19:39 +02001744#if defined(static_assert)
1745static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1746 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001747static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001748 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001749static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001750 "One or more key attribute flag is listed as both internal-only and external-only" );
1751#endif
1752
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001753/** Validate that a key policy is internally well-formed.
1754 *
1755 * This function only rejects invalid policies. It does not validate the
1756 * consistency of the policy with respect to other attributes of the key
1757 * such as the key type.
1758 */
1759static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001760{
1761 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001762 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001763 PSA_KEY_USAGE_ENCRYPT |
1764 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001765 PSA_KEY_USAGE_SIGN_HASH |
1766 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001767 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1768 return( PSA_ERROR_INVALID_ARGUMENT );
1769
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 return( PSA_SUCCESS );
1771}
1772
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001773/** Validate the internal consistency of key attributes.
1774 *
1775 * This function only rejects invalid attribute values. If does not
1776 * validate the consistency of the attributes with any key data that may
1777 * be involved in the creation of the key.
1778 *
1779 * Call this function early in the key creation process.
1780 *
1781 * \param[in] attributes Key attributes for the new key.
1782 * \param[out] p_drv On any return, the driver for the key, if any.
1783 * NULL for a transparent key.
1784 *
1785 */
1786static psa_status_t psa_validate_key_attributes(
1787 const psa_key_attributes_t *attributes,
1788 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001789{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001790 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001791
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001792 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1793 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001794 if( status != PSA_SUCCESS )
1795 return( status );
1796
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001797 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1798 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001799 if( status != PSA_SUCCESS )
1800 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001801
1802 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001803 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001804 return( status );
1805
1806 /* Refuse to create overly large keys.
1807 * Note that this doesn't trigger on import if the attributes don't
1808 * explicitly specify a size (so psa_get_key_bits returns 0), so
1809 * psa_import_key() needs its own checks. */
1810 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1811 return( PSA_ERROR_NOT_SUPPORTED );
1812
Gilles Peskine91e8c332019-08-02 19:19:39 +02001813 /* Reject invalid flags. These should not be reachable through the API. */
1814 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1815 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1816 return( PSA_ERROR_INVALID_ARGUMENT );
1817
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001818 return( PSA_SUCCESS );
1819}
1820
Gilles Peskine4747d192019-04-17 15:05:45 +02001821/** Prepare a key slot to receive key material.
1822 *
1823 * This function allocates a key slot and sets its metadata.
1824 *
1825 * If this function fails, call psa_fail_key_creation().
1826 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001827 * This function is intended to be used as follows:
1828 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1829 * it with the specified attributes, and assign it a handle.
1830 * -# Populate the slot with the key material.
1831 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1832 * In case of failure at any step, stop the sequence and call
1833 * psa_fail_key_creation().
1834 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001835 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001836 * \param[in] attributes Key attributes for the new key.
1837 * \param[out] handle On success, a handle for the allocated slot.
1838 * \param[out] p_slot On success, a pointer to the prepared slot.
1839 * \param[out] p_drv On any return, the driver for the key, if any.
1840 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001841 *
1842 * \retval #PSA_SUCCESS
1843 * The key slot is ready to receive key material.
1844 * \return If this function fails, the key slot is an invalid state.
1845 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001846 */
1847static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001848 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001849 const psa_key_attributes_t *attributes,
1850 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001851 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001852 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001853{
1854 psa_status_t status;
1855 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
Gilles Peskineedbed562019-08-07 18:19:59 +02001864 status = psa_get_empty_key_slot( handle, 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 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001980 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001981 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001982 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001983 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1984 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001985 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001986 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1987 status = psa_internal_export_key( slot,
1988 buffer, buffer_size, &length,
1989 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001990 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001991 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001992 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001993
Gilles Peskinef9168942019-09-12 19:20:29 +02001994 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001995 mbedtls_free( buffer );
1996 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001997 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001998#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1999
Gilles Peskinecbaff462019-07-12 23:46:04 +02002000#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002001 /* Finish the transaction for a key creation. This does not
2002 * happen when registering an existing key. Detect this case
2003 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002004 * creation of a persistent key in a secure element requires a transaction,
2005 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002006 if( driver != NULL &&
2007 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002008 {
2009 status = psa_save_se_persistent_data( driver );
2010 if( status != PSA_SUCCESS )
2011 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002012 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002013 return( status );
2014 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002015 status = psa_crypto_stop_transaction( );
2016 if( status != PSA_SUCCESS )
2017 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002018 }
2019#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2020
Gilles Peskine4747d192019-04-17 15:05:45 +02002021 return( status );
2022}
2023
2024/** Abort the creation of a key.
2025 *
2026 * You may call this function after calling psa_start_key_creation(),
2027 * or after psa_finish_key_creation() fails. In other circumstances, this
2028 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002029 * See the documentation of psa_start_key_creation() for the intended use
2030 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002031 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002032 * \param[in,out] slot Pointer to the slot with key material.
2033 * \param[in] driver The secure element driver for the key,
2034 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002035 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002036static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002037 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002038{
Gilles Peskine011e4282019-06-26 18:34:38 +02002039 (void) driver;
2040
Gilles Peskine4747d192019-04-17 15:05:45 +02002041 if( slot == NULL )
2042 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002043
2044#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002045 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002046 * element, and the failure happened later (when saving metadata
2047 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002048 * element.
2049 * https://github.com/ARMmbed/mbed-crypto/issues/217
2050 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002051
Gilles Peskined7729582019-08-05 15:55:54 +02002052 /* Abort the ongoing transaction if any (there may not be one if
2053 * the creation process failed before starting one, or if the
2054 * key creation is a registration of a key in a secure element).
2055 * Earlier functions must already have done what it takes to undo any
2056 * partial creation. All that's left is to update the transaction data
2057 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002058 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002059#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2060
Gilles Peskine4747d192019-04-17 15:05:45 +02002061 psa_wipe_key_slot( slot );
2062}
2063
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002064/** Validate optional attributes during key creation.
2065 *
2066 * Some key attributes are optional during key creation. If they are
2067 * specified in the attributes structure, check that they are consistent
2068 * with the data in the slot.
2069 *
2070 * This function should be called near the end of key creation, after
2071 * the slot in memory is fully populated but before saving persistent data.
2072 */
2073static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002074 const psa_key_slot_t *slot,
2075 const psa_key_attributes_t *attributes )
2076{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002077 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002078 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002079 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002080 return( PSA_ERROR_INVALID_ARGUMENT );
2081 }
2082
2083 if( attributes->domain_parameters_size != 0 )
2084 {
2085#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002086 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002087 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002088 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002089 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002090 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002091
Steven Cooreman7f391872020-07-30 14:57:44 +02002092 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002093 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002094 slot->data.key.data,
2095 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002096 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002097 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002098 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002099
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002100 mbedtls_mpi_init( &actual );
2101 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002102 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002103 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002104 mbedtls_rsa_free( rsa );
2105 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002106 if( ret != 0 )
2107 goto rsa_exit;
2108 ret = mbedtls_mpi_read_binary( &required,
2109 attributes->domain_parameters,
2110 attributes->domain_parameters_size );
2111 if( ret != 0 )
2112 goto rsa_exit;
2113 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2114 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2115 rsa_exit:
2116 mbedtls_mpi_free( &actual );
2117 mbedtls_mpi_free( &required );
2118 if( ret != 0)
2119 return( mbedtls_to_psa_error( ret ) );
2120 }
2121 else
2122#endif
2123 {
2124 return( PSA_ERROR_INVALID_ARGUMENT );
2125 }
2126 }
2127
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002128 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002129 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002130 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002131 return( PSA_ERROR_INVALID_ARGUMENT );
2132 }
2133
2134 return( PSA_SUCCESS );
2135}
2136
Gilles Peskine4747d192019-04-17 15:05:45 +02002137psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002138 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002139 size_t data_length,
2140 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002141{
2142 psa_status_t status;
2143 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002144 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002145
Gilles Peskine0f84d622019-09-12 19:03:13 +02002146 /* Reject zero-length symmetric keys (including raw data key objects).
2147 * This also rejects any key which might be encoded as an empty string,
2148 * which is never valid. */
2149 if( data_length == 0 )
2150 return( PSA_ERROR_INVALID_ARGUMENT );
2151
Gilles Peskinedf179142019-07-15 22:02:14 +02002152 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2153 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002154 if( status != PSA_SUCCESS )
2155 goto exit;
2156
Gilles Peskine5d309672019-07-12 23:47:28 +02002157#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2158 if( driver != NULL )
2159 {
2160 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002161 /* The driver should set the number of key bits, however in
2162 * case it doesn't, we initialize bits to an invalid value. */
2163 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002164 if( drv->key_management == NULL ||
2165 drv->key_management->p_import == NULL )
2166 {
2167 status = PSA_ERROR_NOT_SUPPORTED;
2168 goto exit;
2169 }
2170 status = drv->key_management->p_import(
2171 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002172 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002173 &bits );
2174 if( status != PSA_SUCCESS )
2175 goto exit;
2176 if( bits > PSA_MAX_KEY_BITS )
2177 {
2178 status = PSA_ERROR_NOT_SUPPORTED;
2179 goto exit;
2180 }
2181 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002182 }
2183 else
2184#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2185 {
2186 status = psa_import_key_into_slot( slot, data, data_length );
2187 if( status != PSA_SUCCESS )
2188 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002189 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002190 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002191 if( status != PSA_SUCCESS )
2192 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002193
Gilles Peskine011e4282019-06-26 18:34:38 +02002194 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002195exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002196 if( status != PSA_SUCCESS )
2197 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002198 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002199 *handle = 0;
2200 }
2201 return( status );
2202}
2203
Gilles Peskined7729582019-08-05 15:55:54 +02002204#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2205psa_status_t mbedtls_psa_register_se_key(
2206 const psa_key_attributes_t *attributes )
2207{
2208 psa_status_t status;
2209 psa_key_slot_t *slot = NULL;
2210 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002211 psa_key_handle_t handle = 0;
2212
2213 /* Leaving attributes unspecified is not currently supported.
2214 * It could make sense to query the key type and size from the
2215 * secure element, but not all secure elements support this
2216 * and the driver HAL doesn't currently support it. */
2217 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2218 return( PSA_ERROR_NOT_SUPPORTED );
2219 if( psa_get_key_bits( attributes ) == 0 )
2220 return( PSA_ERROR_NOT_SUPPORTED );
2221
2222 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2223 &handle, &slot, &driver );
2224 if( status != PSA_SUCCESS )
2225 goto exit;
2226
Gilles Peskined7729582019-08-05 15:55:54 +02002227 status = psa_finish_key_creation( slot, driver );
2228
2229exit:
2230 if( status != PSA_SUCCESS )
2231 {
2232 psa_fail_key_creation( slot, driver );
2233 }
2234 /* Registration doesn't keep the key in RAM. */
2235 psa_close_key( handle );
2236 return( status );
2237}
2238#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2239
Gilles Peskinef603c712019-01-19 13:40:11 +01002240static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002241 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002242{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002243 psa_status_t status = psa_copy_key_material_into_slot( target,
2244 source->data.key.data,
2245 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002246 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002247 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002248
Steven Cooreman398aee52020-10-13 14:35:45 +02002249 target->attr.type = source->attr.type;
2250 target->attr.bits = source->attr.bits;
2251
2252 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002253}
2254
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002255psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2256 const psa_key_attributes_t *specified_attributes,
2257 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002258{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002259 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002260 psa_key_slot_t *source_slot = NULL;
2261 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002262 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002263 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002264
Gilles Peskine28f8f302019-07-24 13:30:31 +02002265 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002266 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002267 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002268 goto exit;
2269
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002270 status = psa_validate_optional_attributes( source_slot,
2271 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002272 if( status != PSA_SUCCESS )
2273 goto exit;
2274
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002275 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002276 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002277 if( status != PSA_SUCCESS )
2278 goto exit;
2279
Gilles Peskinedf179142019-07-15 22:02:14 +02002280 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2281 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002282 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002283 if( status != PSA_SUCCESS )
2284 goto exit;
2285
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002286#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2287 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002288 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002289 /* Copying to a secure element is not implemented yet. */
2290 status = PSA_ERROR_NOT_SUPPORTED;
2291 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002292 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002293#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002294
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002295 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002296 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002297 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002298
Gilles Peskine011e4282019-06-26 18:34:38 +02002299 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002300exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002301 if( status != PSA_SUCCESS )
2302 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002303 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002304 *target_handle = 0;
2305 }
2306 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002307}
2308
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002309
2310
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002311/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002312/* Message digests */
2313/****************************************************************/
2314
Gilles Peskineb16841e2019-10-10 20:36:12 +02002315#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002316static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002317{
2318 switch( alg )
2319 {
2320#if defined(MBEDTLS_MD2_C)
2321 case PSA_ALG_MD2:
2322 return( &mbedtls_md2_info );
2323#endif
2324#if defined(MBEDTLS_MD4_C)
2325 case PSA_ALG_MD4:
2326 return( &mbedtls_md4_info );
2327#endif
2328#if defined(MBEDTLS_MD5_C)
2329 case PSA_ALG_MD5:
2330 return( &mbedtls_md5_info );
2331#endif
2332#if defined(MBEDTLS_RIPEMD160_C)
2333 case PSA_ALG_RIPEMD160:
2334 return( &mbedtls_ripemd160_info );
2335#endif
2336#if defined(MBEDTLS_SHA1_C)
2337 case PSA_ALG_SHA_1:
2338 return( &mbedtls_sha1_info );
2339#endif
2340#if defined(MBEDTLS_SHA256_C)
2341 case PSA_ALG_SHA_224:
2342 return( &mbedtls_sha224_info );
2343 case PSA_ALG_SHA_256:
2344 return( &mbedtls_sha256_info );
2345#endif
2346#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002347#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002348 case PSA_ALG_SHA_384:
2349 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002350#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002351 case PSA_ALG_SHA_512:
2352 return( &mbedtls_sha512_info );
2353#endif
2354 default:
2355 return( NULL );
2356 }
2357}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002358#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002359
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002360psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2361{
2362 switch( operation->alg )
2363 {
Gilles Peskine81736312018-06-26 15:04:31 +02002364 case 0:
2365 /* The object has (apparently) been initialized but it is not
2366 * in use. It's ok to call abort on such an object, and there's
2367 * nothing to do. */
2368 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002369#if defined(MBEDTLS_MD2_C)
2370 case PSA_ALG_MD2:
2371 mbedtls_md2_free( &operation->ctx.md2 );
2372 break;
2373#endif
2374#if defined(MBEDTLS_MD4_C)
2375 case PSA_ALG_MD4:
2376 mbedtls_md4_free( &operation->ctx.md4 );
2377 break;
2378#endif
2379#if defined(MBEDTLS_MD5_C)
2380 case PSA_ALG_MD5:
2381 mbedtls_md5_free( &operation->ctx.md5 );
2382 break;
2383#endif
2384#if defined(MBEDTLS_RIPEMD160_C)
2385 case PSA_ALG_RIPEMD160:
2386 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2387 break;
2388#endif
2389#if defined(MBEDTLS_SHA1_C)
2390 case PSA_ALG_SHA_1:
2391 mbedtls_sha1_free( &operation->ctx.sha1 );
2392 break;
2393#endif
2394#if defined(MBEDTLS_SHA256_C)
2395 case PSA_ALG_SHA_224:
2396 case PSA_ALG_SHA_256:
2397 mbedtls_sha256_free( &operation->ctx.sha256 );
2398 break;
2399#endif
2400#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002401#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002402 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002403#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002404 case PSA_ALG_SHA_512:
2405 mbedtls_sha512_free( &operation->ctx.sha512 );
2406 break;
2407#endif
2408 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002409 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002410 }
2411 operation->alg = 0;
2412 return( PSA_SUCCESS );
2413}
2414
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002415psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002416 psa_algorithm_t alg )
2417{
Janos Follath24eed8d2019-11-22 13:21:35 +00002418 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002419
2420 /* A context must be freshly initialized before it can be set up. */
2421 if( operation->alg != 0 )
2422 {
2423 return( PSA_ERROR_BAD_STATE );
2424 }
2425
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002426 switch( alg )
2427 {
2428#if defined(MBEDTLS_MD2_C)
2429 case PSA_ALG_MD2:
2430 mbedtls_md2_init( &operation->ctx.md2 );
2431 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2432 break;
2433#endif
2434#if defined(MBEDTLS_MD4_C)
2435 case PSA_ALG_MD4:
2436 mbedtls_md4_init( &operation->ctx.md4 );
2437 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2438 break;
2439#endif
2440#if defined(MBEDTLS_MD5_C)
2441 case PSA_ALG_MD5:
2442 mbedtls_md5_init( &operation->ctx.md5 );
2443 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2444 break;
2445#endif
2446#if defined(MBEDTLS_RIPEMD160_C)
2447 case PSA_ALG_RIPEMD160:
2448 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2449 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2450 break;
2451#endif
2452#if defined(MBEDTLS_SHA1_C)
2453 case PSA_ALG_SHA_1:
2454 mbedtls_sha1_init( &operation->ctx.sha1 );
2455 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2456 break;
2457#endif
2458#if defined(MBEDTLS_SHA256_C)
2459 case PSA_ALG_SHA_224:
2460 mbedtls_sha256_init( &operation->ctx.sha256 );
2461 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2462 break;
2463 case PSA_ALG_SHA_256:
2464 mbedtls_sha256_init( &operation->ctx.sha256 );
2465 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2466 break;
2467#endif
2468#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002469#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002470 case PSA_ALG_SHA_384:
2471 mbedtls_sha512_init( &operation->ctx.sha512 );
2472 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2473 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002474#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002475 case PSA_ALG_SHA_512:
2476 mbedtls_sha512_init( &operation->ctx.sha512 );
2477 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2478 break;
2479#endif
2480 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002481 return( PSA_ALG_IS_HASH( alg ) ?
2482 PSA_ERROR_NOT_SUPPORTED :
2483 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002484 }
2485 if( ret == 0 )
2486 operation->alg = alg;
2487 else
2488 psa_hash_abort( operation );
2489 return( mbedtls_to_psa_error( ret ) );
2490}
2491
2492psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2493 const uint8_t *input,
2494 size_t input_length )
2495{
Janos Follath24eed8d2019-11-22 13:21:35 +00002496 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002497
2498 /* Don't require hash implementations to behave correctly on a
2499 * zero-length input, which may have an invalid pointer. */
2500 if( input_length == 0 )
2501 return( PSA_SUCCESS );
2502
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002503 switch( operation->alg )
2504 {
2505#if defined(MBEDTLS_MD2_C)
2506 case PSA_ALG_MD2:
2507 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2508 input, input_length );
2509 break;
2510#endif
2511#if defined(MBEDTLS_MD4_C)
2512 case PSA_ALG_MD4:
2513 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2514 input, input_length );
2515 break;
2516#endif
2517#if defined(MBEDTLS_MD5_C)
2518 case PSA_ALG_MD5:
2519 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2520 input, input_length );
2521 break;
2522#endif
2523#if defined(MBEDTLS_RIPEMD160_C)
2524 case PSA_ALG_RIPEMD160:
2525 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2526 input, input_length );
2527 break;
2528#endif
2529#if defined(MBEDTLS_SHA1_C)
2530 case PSA_ALG_SHA_1:
2531 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2532 input, input_length );
2533 break;
2534#endif
2535#if defined(MBEDTLS_SHA256_C)
2536 case PSA_ALG_SHA_224:
2537 case PSA_ALG_SHA_256:
2538 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2539 input, input_length );
2540 break;
2541#endif
2542#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002543#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002544 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002545#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002546 case PSA_ALG_SHA_512:
2547 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2548 input, input_length );
2549 break;
2550#endif
2551 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002552 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002553 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002554
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002555 if( ret != 0 )
2556 psa_hash_abort( operation );
2557 return( mbedtls_to_psa_error( ret ) );
2558}
2559
2560psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2561 uint8_t *hash,
2562 size_t hash_size,
2563 size_t *hash_length )
2564{
itayzafrir40835d42018-08-02 13:14:17 +03002565 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002567 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002568
2569 /* Fill the output buffer with something that isn't a valid hash
2570 * (barring an attack on the hash and deliberately-crafted input),
2571 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002572 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002573 /* If hash_size is 0 then hash may be NULL and then the
2574 * call to memset would have undefined behavior. */
2575 if( hash_size != 0 )
2576 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002577
2578 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002579 {
2580 status = PSA_ERROR_BUFFER_TOO_SMALL;
2581 goto exit;
2582 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002583
2584 switch( operation->alg )
2585 {
2586#if defined(MBEDTLS_MD2_C)
2587 case PSA_ALG_MD2:
2588 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2589 break;
2590#endif
2591#if defined(MBEDTLS_MD4_C)
2592 case PSA_ALG_MD4:
2593 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2594 break;
2595#endif
2596#if defined(MBEDTLS_MD5_C)
2597 case PSA_ALG_MD5:
2598 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2599 break;
2600#endif
2601#if defined(MBEDTLS_RIPEMD160_C)
2602 case PSA_ALG_RIPEMD160:
2603 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2604 break;
2605#endif
2606#if defined(MBEDTLS_SHA1_C)
2607 case PSA_ALG_SHA_1:
2608 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2609 break;
2610#endif
2611#if defined(MBEDTLS_SHA256_C)
2612 case PSA_ALG_SHA_224:
2613 case PSA_ALG_SHA_256:
2614 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2615 break;
2616#endif
2617#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002618#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002619 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002620#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002621 case PSA_ALG_SHA_512:
2622 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2623 break;
2624#endif
2625 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002626 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002627 }
itayzafrir40835d42018-08-02 13:14:17 +03002628 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002629
itayzafrir40835d42018-08-02 13:14:17 +03002630exit:
2631 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002632 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002633 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002634 return( psa_hash_abort( operation ) );
2635 }
2636 else
2637 {
2638 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002639 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002640 }
2641}
2642
Gilles Peskine2d277862018-06-18 15:41:12 +02002643psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2644 const uint8_t *hash,
2645 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002646{
2647 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2648 size_t actual_hash_length;
2649 psa_status_t status = psa_hash_finish( operation,
2650 actual_hash, sizeof( actual_hash ),
2651 &actual_hash_length );
2652 if( status != PSA_SUCCESS )
2653 return( status );
2654 if( actual_hash_length != hash_length )
2655 return( PSA_ERROR_INVALID_SIGNATURE );
2656 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2657 return( PSA_ERROR_INVALID_SIGNATURE );
2658 return( PSA_SUCCESS );
2659}
2660
Gilles Peskine0a749c82019-11-28 19:33:58 +01002661psa_status_t psa_hash_compute( psa_algorithm_t alg,
2662 const uint8_t *input, size_t input_length,
2663 uint8_t *hash, size_t hash_size,
2664 size_t *hash_length )
2665{
2666 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2667 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2668
2669 *hash_length = hash_size;
2670 status = psa_hash_setup( &operation, alg );
2671 if( status != PSA_SUCCESS )
2672 goto exit;
2673 status = psa_hash_update( &operation, input, input_length );
2674 if( status != PSA_SUCCESS )
2675 goto exit;
2676 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2677 if( status != PSA_SUCCESS )
2678 goto exit;
2679
2680exit:
2681 if( status == PSA_SUCCESS )
2682 status = psa_hash_abort( &operation );
2683 else
2684 psa_hash_abort( &operation );
2685 return( status );
2686}
2687
2688psa_status_t psa_hash_compare( psa_algorithm_t alg,
2689 const uint8_t *input, size_t input_length,
2690 const uint8_t *hash, size_t hash_length )
2691{
2692 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2693 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2694
2695 status = psa_hash_setup( &operation, alg );
2696 if( status != PSA_SUCCESS )
2697 goto exit;
2698 status = psa_hash_update( &operation, input, input_length );
2699 if( status != PSA_SUCCESS )
2700 goto exit;
2701 status = psa_hash_verify( &operation, hash, hash_length );
2702 if( status != PSA_SUCCESS )
2703 goto exit;
2704
2705exit:
2706 if( status == PSA_SUCCESS )
2707 status = psa_hash_abort( &operation );
2708 else
2709 psa_hash_abort( &operation );
2710 return( status );
2711}
2712
Gilles Peskineeb35d782019-01-22 17:56:16 +01002713psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2714 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002715{
2716 if( target_operation->alg != 0 )
2717 return( PSA_ERROR_BAD_STATE );
2718
2719 switch( source_operation->alg )
2720 {
2721 case 0:
2722 return( PSA_ERROR_BAD_STATE );
2723#if defined(MBEDTLS_MD2_C)
2724 case PSA_ALG_MD2:
2725 mbedtls_md2_clone( &target_operation->ctx.md2,
2726 &source_operation->ctx.md2 );
2727 break;
2728#endif
2729#if defined(MBEDTLS_MD4_C)
2730 case PSA_ALG_MD4:
2731 mbedtls_md4_clone( &target_operation->ctx.md4,
2732 &source_operation->ctx.md4 );
2733 break;
2734#endif
2735#if defined(MBEDTLS_MD5_C)
2736 case PSA_ALG_MD5:
2737 mbedtls_md5_clone( &target_operation->ctx.md5,
2738 &source_operation->ctx.md5 );
2739 break;
2740#endif
2741#if defined(MBEDTLS_RIPEMD160_C)
2742 case PSA_ALG_RIPEMD160:
2743 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2744 &source_operation->ctx.ripemd160 );
2745 break;
2746#endif
2747#if defined(MBEDTLS_SHA1_C)
2748 case PSA_ALG_SHA_1:
2749 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2750 &source_operation->ctx.sha1 );
2751 break;
2752#endif
2753#if defined(MBEDTLS_SHA256_C)
2754 case PSA_ALG_SHA_224:
2755 case PSA_ALG_SHA_256:
2756 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2757 &source_operation->ctx.sha256 );
2758 break;
2759#endif
2760#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002761#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002762 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002763#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002764 case PSA_ALG_SHA_512:
2765 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2766 &source_operation->ctx.sha512 );
2767 break;
2768#endif
2769 default:
2770 return( PSA_ERROR_NOT_SUPPORTED );
2771 }
2772
2773 target_operation->alg = source_operation->alg;
2774 return( PSA_SUCCESS );
2775}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002776
2777
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002778/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002779/* MAC */
2780/****************************************************************/
2781
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002782static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002783 psa_algorithm_t alg,
2784 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002785 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002786 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002787{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002788 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002789 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002790
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002791 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002792 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002793
Gilles Peskine8c9def32018-02-08 10:02:12 +01002794 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2795 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002796 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002797 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002798 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002799 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002800 mode = MBEDTLS_MODE_STREAM;
2801 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002802 case PSA_ALG_CTR:
2803 mode = MBEDTLS_MODE_CTR;
2804 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002805 case PSA_ALG_CFB:
2806 mode = MBEDTLS_MODE_CFB;
2807 break;
2808 case PSA_ALG_OFB:
2809 mode = MBEDTLS_MODE_OFB;
2810 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002811 case PSA_ALG_ECB_NO_PADDING:
2812 mode = MBEDTLS_MODE_ECB;
2813 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002814 case PSA_ALG_CBC_NO_PADDING:
2815 mode = MBEDTLS_MODE_CBC;
2816 break;
2817 case PSA_ALG_CBC_PKCS7:
2818 mode = MBEDTLS_MODE_CBC;
2819 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002820 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002821 mode = MBEDTLS_MODE_CCM;
2822 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002823 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002824 mode = MBEDTLS_MODE_GCM;
2825 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002826 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2827 mode = MBEDTLS_MODE_CHACHAPOLY;
2828 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002829 default:
2830 return( NULL );
2831 }
2832 }
2833 else if( alg == PSA_ALG_CMAC )
2834 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002835 else
2836 return( NULL );
2837
2838 switch( key_type )
2839 {
2840 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002841 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002842 break;
2843 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002844 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2845 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002846 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002847 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002848 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002849 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002850 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2851 * but two-key Triple-DES is functionally three-key Triple-DES
2852 * with K1=K3, so that's how we present it to mbedtls. */
2853 if( key_bits == 128 )
2854 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002855 break;
2856 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002857 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002858 break;
2859 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002860 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002861 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002862 case PSA_KEY_TYPE_CHACHA20:
2863 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2864 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002865 default:
2866 return( NULL );
2867 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002868 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002869 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002870
Jaeden Amero23bbb752018-06-26 14:16:54 +01002871 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2872 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002873}
2874
Gilles Peskinea05219c2018-11-16 16:02:56 +01002875#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002876static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002877{
Gilles Peskine2d277862018-06-18 15:41:12 +02002878 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002879 {
2880 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002881 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002882 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002883 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002884 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002885 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002886 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002887 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002888 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002889 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002890 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002891 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002892 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002893 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002894 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002895 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002896 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002897 return( 128 );
2898 default:
2899 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002900 }
2901}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002902#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002903
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002904/* Initialize the MAC operation structure. Once this function has been
2905 * called, psa_mac_abort can run and will do the right thing. */
2906static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2907 psa_algorithm_t alg )
2908{
2909 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2910
2911 operation->alg = alg;
2912 operation->key_set = 0;
2913 operation->iv_set = 0;
2914 operation->iv_required = 0;
2915 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002916 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002917
2918#if defined(MBEDTLS_CMAC_C)
2919 if( alg == PSA_ALG_CMAC )
2920 {
2921 operation->iv_required = 0;
2922 mbedtls_cipher_init( &operation->ctx.cmac );
2923 status = PSA_SUCCESS;
2924 }
2925 else
2926#endif /* MBEDTLS_CMAC_C */
2927#if defined(MBEDTLS_MD_C)
2928 if( PSA_ALG_IS_HMAC( operation->alg ) )
2929 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002930 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2931 operation->ctx.hmac.hash_ctx.alg = 0;
2932 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002933 }
2934 else
2935#endif /* MBEDTLS_MD_C */
2936 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002937 if( ! PSA_ALG_IS_MAC( alg ) )
2938 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002939 }
2940
2941 if( status != PSA_SUCCESS )
2942 memset( operation, 0, sizeof( *operation ) );
2943 return( status );
2944}
2945
Gilles Peskine01126fa2018-07-12 17:04:55 +02002946#if defined(MBEDTLS_MD_C)
2947static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2948{
Gilles Peskine3f108122018-12-07 18:14:53 +01002949 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002950 return( psa_hash_abort( &hmac->hash_ctx ) );
2951}
2952#endif /* MBEDTLS_MD_C */
2953
Gilles Peskine8c9def32018-02-08 10:02:12 +01002954psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2955{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002956 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002957 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002958 /* The object has (apparently) been initialized but it is not
2959 * in use. It's ok to call abort on such an object, and there's
2960 * nothing to do. */
2961 return( PSA_SUCCESS );
2962 }
2963 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002964#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002965 if( operation->alg == PSA_ALG_CMAC )
2966 {
2967 mbedtls_cipher_free( &operation->ctx.cmac );
2968 }
2969 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002970#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002971#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002972 if( PSA_ALG_IS_HMAC( operation->alg ) )
2973 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002974 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002975 }
2976 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002977#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002978 {
2979 /* Sanity check (shouldn't happen: operation->alg should
2980 * always have been initialized to a valid value). */
2981 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002982 }
Moran Peker41deec42018-04-04 15:43:05 +03002983
Gilles Peskine8c9def32018-02-08 10:02:12 +01002984 operation->alg = 0;
2985 operation->key_set = 0;
2986 operation->iv_set = 0;
2987 operation->iv_required = 0;
2988 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002989 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002990
Gilles Peskine8c9def32018-02-08 10:02:12 +01002991 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002992
2993bad_state:
2994 /* If abort is called on an uninitialized object, we can't trust
2995 * anything. Wipe the object in case it contains confidential data.
2996 * This may result in a memory leak if a pointer gets overwritten,
2997 * but it's too late to do anything about this. */
2998 memset( operation, 0, sizeof( *operation ) );
2999 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003000}
3001
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003002#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003003static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003004 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003005 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003006 const mbedtls_cipher_info_t *cipher_info )
3007{
Janos Follath24eed8d2019-11-22 13:21:35 +00003008 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003009
3010 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003011
3012 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3013 if( ret != 0 )
3014 return( ret );
3015
3016 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003017 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003018 key_bits );
3019 return( ret );
3020}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003021#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003022
Gilles Peskine248051a2018-06-20 16:09:38 +02003023#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003024static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3025 const uint8_t *key,
3026 size_t key_length,
3027 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003028{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003029 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003030 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003031 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003032 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003033 psa_status_t status;
3034
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003035 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3036 * overflow below. This should never trigger if the hash algorithm
3037 * is implemented correctly. */
3038 /* The size checks against the ipad and opad buffers cannot be written
3039 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3040 * because that triggers -Wlogical-op on GCC 7.3. */
3041 if( block_size > sizeof( ipad ) )
3042 return( PSA_ERROR_NOT_SUPPORTED );
3043 if( block_size > sizeof( hmac->opad ) )
3044 return( PSA_ERROR_NOT_SUPPORTED );
3045 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003046 return( PSA_ERROR_NOT_SUPPORTED );
3047
Gilles Peskined223b522018-06-11 18:12:58 +02003048 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003049 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003050 status = psa_hash_compute( hash_alg, key, key_length,
3051 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003052 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003053 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003054 }
Gilles Peskine96889972018-07-12 17:07:03 +02003055 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3056 * but it is permitted. It is common when HMAC is used in HKDF, for
3057 * example. Don't call `memcpy` in the 0-length because `key` could be
3058 * an invalid pointer which would make the behavior undefined. */
3059 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003060 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003061
Gilles Peskined223b522018-06-11 18:12:58 +02003062 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3063 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003064 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003065 ipad[i] ^= 0x36;
3066 memset( ipad + key_length, 0x36, block_size - key_length );
3067
3068 /* Copy the key material from ipad to opad, flipping the requisite bits,
3069 * and filling the rest of opad with the requisite constant. */
3070 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003071 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3072 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003073
Gilles Peskine01126fa2018-07-12 17:04:55 +02003074 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003075 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003076 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003077
Gilles Peskine01126fa2018-07-12 17:04:55 +02003078 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003079
3080cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003081 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003082
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003083 return( status );
3084}
Gilles Peskine248051a2018-06-20 16:09:38 +02003085#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003086
Gilles Peskine89167cb2018-07-08 20:12:23 +02003087static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003088 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003089 psa_algorithm_t alg,
3090 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003091{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003092 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003093 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003094 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003095 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003096 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003097 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003098 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003099
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003100 /* A context must be freshly initialized before it can be set up. */
3101 if( operation->alg != 0 )
3102 {
3103 return( PSA_ERROR_BAD_STATE );
3104 }
3105
Gilles Peskined911eb72018-08-14 15:18:45 +02003106 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003107 if( status != PSA_SUCCESS )
3108 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003109 if( is_sign )
3110 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003111
Gilles Peskine28f8f302019-07-24 13:30:31 +02003112 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003113 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003114 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003115 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003116
Gilles Peskine8c9def32018-02-08 10:02:12 +01003117#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003118 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003119 {
3120 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003121 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003122 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003123 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003124 if( cipher_info == NULL )
3125 {
3126 status = PSA_ERROR_NOT_SUPPORTED;
3127 goto exit;
3128 }
3129 operation->mac_size = cipher_info->block_size;
3130 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3131 status = mbedtls_to_psa_error( ret );
3132 }
3133 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003134#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003135#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003136 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003137 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003138 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003139 if( hash_alg == 0 )
3140 {
3141 status = PSA_ERROR_NOT_SUPPORTED;
3142 goto exit;
3143 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003144
3145 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3146 /* Sanity check. This shouldn't fail on a valid configuration. */
3147 if( operation->mac_size == 0 ||
3148 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3149 {
3150 status = PSA_ERROR_NOT_SUPPORTED;
3151 goto exit;
3152 }
3153
Gilles Peskine8e338702019-07-30 20:06:31 +02003154 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003155 {
3156 status = PSA_ERROR_INVALID_ARGUMENT;
3157 goto exit;
3158 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003159
Gilles Peskine01126fa2018-07-12 17:04:55 +02003160 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003161 slot->data.key.data,
3162 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003163 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003164 }
3165 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003166#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003167 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003168 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003169 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003170 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003171
Gilles Peskined911eb72018-08-14 15:18:45 +02003172 if( truncated == 0 )
3173 {
3174 /* The "normal" case: untruncated algorithm. Nothing to do. */
3175 }
3176 else if( truncated < 4 )
3177 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003178 /* A very short MAC is too short for security since it can be
3179 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3180 * so we make this our minimum, even though 32 bits is still
3181 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003182 status = PSA_ERROR_NOT_SUPPORTED;
3183 }
3184 else if( truncated > operation->mac_size )
3185 {
3186 /* It's impossible to "truncate" to a larger length. */
3187 status = PSA_ERROR_INVALID_ARGUMENT;
3188 }
3189 else
3190 operation->mac_size = truncated;
3191
Gilles Peskinefbfac682018-07-08 20:51:54 +02003192exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003193 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003194 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003195 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003196 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003197 else
3198 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003199 operation->key_set = 1;
3200 }
3201 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003202}
3203
Gilles Peskine89167cb2018-07-08 20:12:23 +02003204psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003205 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003206 psa_algorithm_t alg )
3207{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003208 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003209}
3210
3211psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003212 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003213 psa_algorithm_t alg )
3214{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003215 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003216}
3217
Gilles Peskine8c9def32018-02-08 10:02:12 +01003218psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3219 const uint8_t *input,
3220 size_t input_length )
3221{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003222 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003223 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003224 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003225 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003226 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003227 operation->has_input = 1;
3228
Gilles Peskine8c9def32018-02-08 10:02:12 +01003229#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003230 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003231 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003232 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3233 input, input_length );
3234 status = mbedtls_to_psa_error( ret );
3235 }
3236 else
3237#endif /* MBEDTLS_CMAC_C */
3238#if defined(MBEDTLS_MD_C)
3239 if( PSA_ALG_IS_HMAC( operation->alg ) )
3240 {
3241 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3242 input_length );
3243 }
3244 else
3245#endif /* MBEDTLS_MD_C */
3246 {
3247 /* This shouldn't happen if `operation` was initialized by
3248 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003249 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003250 }
3251
Gilles Peskinefbfac682018-07-08 20:51:54 +02003252 if( status != PSA_SUCCESS )
3253 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003254 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003255}
3256
Gilles Peskine01126fa2018-07-12 17:04:55 +02003257#if defined(MBEDTLS_MD_C)
3258static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3259 uint8_t *mac,
3260 size_t mac_size )
3261{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003262 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003263 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3264 size_t hash_size = 0;
3265 size_t block_size = psa_get_hash_block_size( hash_alg );
3266 psa_status_t status;
3267
Gilles Peskine01126fa2018-07-12 17:04:55 +02003268 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3269 if( status != PSA_SUCCESS )
3270 return( status );
3271 /* From here on, tmp needs to be wiped. */
3272
3273 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3274 if( status != PSA_SUCCESS )
3275 goto exit;
3276
3277 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3278 if( status != PSA_SUCCESS )
3279 goto exit;
3280
3281 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3282 if( status != PSA_SUCCESS )
3283 goto exit;
3284
Gilles Peskined911eb72018-08-14 15:18:45 +02003285 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3286 if( status != PSA_SUCCESS )
3287 goto exit;
3288
3289 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003290
3291exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003292 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003293 return( status );
3294}
3295#endif /* MBEDTLS_MD_C */
3296
mohammad16036df908f2018-04-02 08:34:15 -07003297static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003298 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003299 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003300{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003301 if( ! operation->key_set )
3302 return( PSA_ERROR_BAD_STATE );
3303 if( operation->iv_required && ! operation->iv_set )
3304 return( PSA_ERROR_BAD_STATE );
3305
Gilles Peskine8c9def32018-02-08 10:02:12 +01003306 if( mac_size < operation->mac_size )
3307 return( PSA_ERROR_BUFFER_TOO_SMALL );
3308
Gilles Peskine8c9def32018-02-08 10:02:12 +01003309#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003310 if( operation->alg == PSA_ALG_CMAC )
3311 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003312 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3313 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3314 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003315 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003316 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003317 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003318 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003319 else
3320#endif /* MBEDTLS_CMAC_C */
3321#if defined(MBEDTLS_MD_C)
3322 if( PSA_ALG_IS_HMAC( operation->alg ) )
3323 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003324 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003325 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003326 }
3327 else
3328#endif /* MBEDTLS_MD_C */
3329 {
3330 /* This shouldn't happen if `operation` was initialized by
3331 * a setup function. */
3332 return( PSA_ERROR_BAD_STATE );
3333 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003334}
3335
Gilles Peskineacd4be32018-07-08 19:56:25 +02003336psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3337 uint8_t *mac,
3338 size_t mac_size,
3339 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003340{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003341 psa_status_t status;
3342
Jaeden Amero252ef282019-02-15 14:05:35 +00003343 if( operation->alg == 0 )
3344 {
3345 return( PSA_ERROR_BAD_STATE );
3346 }
3347
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003348 /* Fill the output buffer with something that isn't a valid mac
3349 * (barring an attack on the mac and deliberately-crafted input),
3350 * in case the caller doesn't check the return status properly. */
3351 *mac_length = mac_size;
3352 /* If mac_size is 0 then mac may be NULL and then the
3353 * call to memset would have undefined behavior. */
3354 if( mac_size != 0 )
3355 memset( mac, '!', mac_size );
3356
Gilles Peskine89167cb2018-07-08 20:12:23 +02003357 if( ! operation->is_sign )
3358 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003359 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003360 }
mohammad16036df908f2018-04-02 08:34:15 -07003361
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003362 status = psa_mac_finish_internal( operation, mac, mac_size );
3363
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003364 if( status == PSA_SUCCESS )
3365 {
3366 status = psa_mac_abort( operation );
3367 if( status == PSA_SUCCESS )
3368 *mac_length = operation->mac_size;
3369 else
3370 memset( mac, '!', mac_size );
3371 }
3372 else
3373 psa_mac_abort( operation );
3374 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003375}
3376
Gilles Peskineacd4be32018-07-08 19:56:25 +02003377psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3378 const uint8_t *mac,
3379 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003380{
Gilles Peskine828ed142018-06-18 23:25:51 +02003381 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003382 psa_status_t status;
3383
Jaeden Amero252ef282019-02-15 14:05:35 +00003384 if( operation->alg == 0 )
3385 {
3386 return( PSA_ERROR_BAD_STATE );
3387 }
3388
Gilles Peskine89167cb2018-07-08 20:12:23 +02003389 if( operation->is_sign )
3390 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003391 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003392 }
3393 if( operation->mac_size != mac_length )
3394 {
3395 status = PSA_ERROR_INVALID_SIGNATURE;
3396 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003397 }
mohammad16036df908f2018-04-02 08:34:15 -07003398
3399 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003400 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003401 if( status != PSA_SUCCESS )
3402 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003403
3404 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3405 status = PSA_ERROR_INVALID_SIGNATURE;
3406
3407cleanup:
3408 if( status == PSA_SUCCESS )
3409 status = psa_mac_abort( operation );
3410 else
3411 psa_mac_abort( operation );
3412
Gilles Peskine3f108122018-12-07 18:14:53 +01003413 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003414
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003415 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003416}
3417
3418
Gilles Peskine20035e32018-02-03 22:44:14 +01003419
Gilles Peskine20035e32018-02-03 22:44:14 +01003420/****************************************************************/
3421/* Asymmetric cryptography */
3422/****************************************************************/
3423
Gilles Peskine2b450e32018-06-27 15:42:46 +02003424#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003425/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003426 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003427static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3428 size_t hash_length,
3429 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003430{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003431 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003432 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003433 *md_alg = mbedtls_md_get_type( md_info );
3434
3435 /* The Mbed TLS RSA module uses an unsigned int for hash length
3436 * parameters. Validate that it fits so that we don't risk an
3437 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003438#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003439 if( hash_length > UINT_MAX )
3440 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003441#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003442
3443#if defined(MBEDTLS_PKCS1_V15)
3444 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3445 * must be correct. */
3446 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3447 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003448 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003449 if( md_info == NULL )
3450 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003451 if( mbedtls_md_get_size( md_info ) != hash_length )
3452 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003453 }
3454#endif /* MBEDTLS_PKCS1_V15 */
3455
3456#if defined(MBEDTLS_PKCS1_V21)
3457 /* PSS requires a hash internally. */
3458 if( PSA_ALG_IS_RSA_PSS( alg ) )
3459 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003460 if( md_info == NULL )
3461 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003462 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003463#endif /* MBEDTLS_PKCS1_V21 */
3464
Gilles Peskine61b91d42018-06-08 16:09:36 +02003465 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003466}
3467
Gilles Peskine2b450e32018-06-27 15:42:46 +02003468static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3469 psa_algorithm_t alg,
3470 const uint8_t *hash,
3471 size_t hash_length,
3472 uint8_t *signature,
3473 size_t signature_size,
3474 size_t *signature_length )
3475{
3476 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003478 mbedtls_md_type_t md_alg;
3479
3480 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3481 if( status != PSA_SUCCESS )
3482 return( status );
3483
Gilles Peskine630a18a2018-06-29 17:49:35 +02003484 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003485 return( PSA_ERROR_BUFFER_TOO_SMALL );
3486
3487#if defined(MBEDTLS_PKCS1_V15)
3488 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3489 {
3490 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3491 MBEDTLS_MD_NONE );
3492 ret = mbedtls_rsa_pkcs1_sign( rsa,
3493 mbedtls_ctr_drbg_random,
3494 &global_data.ctr_drbg,
3495 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003496 md_alg,
3497 (unsigned int) hash_length,
3498 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003499 signature );
3500 }
3501 else
3502#endif /* MBEDTLS_PKCS1_V15 */
3503#if defined(MBEDTLS_PKCS1_V21)
3504 if( PSA_ALG_IS_RSA_PSS( alg ) )
3505 {
3506 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3507 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3508 mbedtls_ctr_drbg_random,
3509 &global_data.ctr_drbg,
3510 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003511 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003512 (unsigned int) hash_length,
3513 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003514 signature );
3515 }
3516 else
3517#endif /* MBEDTLS_PKCS1_V21 */
3518 {
3519 return( PSA_ERROR_INVALID_ARGUMENT );
3520 }
3521
3522 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003523 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003524 return( mbedtls_to_psa_error( ret ) );
3525}
3526
3527static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3528 psa_algorithm_t alg,
3529 const uint8_t *hash,
3530 size_t hash_length,
3531 const uint8_t *signature,
3532 size_t signature_length )
3533{
3534 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003536 mbedtls_md_type_t md_alg;
3537
3538 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3539 if( status != PSA_SUCCESS )
3540 return( status );
3541
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003542 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3543 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003544
3545#if defined(MBEDTLS_PKCS1_V15)
3546 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3547 {
3548 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3549 MBEDTLS_MD_NONE );
3550 ret = mbedtls_rsa_pkcs1_verify( rsa,
3551 mbedtls_ctr_drbg_random,
3552 &global_data.ctr_drbg,
3553 MBEDTLS_RSA_PUBLIC,
3554 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003555 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003556 hash,
3557 signature );
3558 }
3559 else
3560#endif /* MBEDTLS_PKCS1_V15 */
3561#if defined(MBEDTLS_PKCS1_V21)
3562 if( PSA_ALG_IS_RSA_PSS( alg ) )
3563 {
3564 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3565 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3566 mbedtls_ctr_drbg_random,
3567 &global_data.ctr_drbg,
3568 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003569 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003570 (unsigned int) hash_length,
3571 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003572 signature );
3573 }
3574 else
3575#endif /* MBEDTLS_PKCS1_V21 */
3576 {
3577 return( PSA_ERROR_INVALID_ARGUMENT );
3578 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003579
3580 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3581 * the rest of the signature is invalid". This has little use in
3582 * practice and PSA doesn't report this distinction. */
3583 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3584 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003585 return( mbedtls_to_psa_error( ret ) );
3586}
3587#endif /* MBEDTLS_RSA_C */
3588
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003589#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003590/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3591 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3592 * (even though these functions don't modify it). */
3593static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3594 psa_algorithm_t alg,
3595 const uint8_t *hash,
3596 size_t hash_length,
3597 uint8_t *signature,
3598 size_t signature_size,
3599 size_t *signature_length )
3600{
Janos Follath24eed8d2019-11-22 13:21:35 +00003601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003602 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003603 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003604 mbedtls_mpi_init( &r );
3605 mbedtls_mpi_init( &s );
3606
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003607 if( signature_size < 2 * curve_bytes )
3608 {
3609 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3610 goto cleanup;
3611 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003612
Gilles Peskinea05219c2018-11-16 16:02:56 +01003613#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003614 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3615 {
3616 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3617 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3618 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003619 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3620 &ecp->d, hash,
3621 hash_length, md_alg,
3622 mbedtls_ctr_drbg_random,
3623 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003624 }
3625 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003626#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003627 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003628 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003629 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3630 hash, hash_length,
3631 mbedtls_ctr_drbg_random,
3632 &global_data.ctr_drbg ) );
3633 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003634
3635 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3636 signature,
3637 curve_bytes ) );
3638 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3639 signature + curve_bytes,
3640 curve_bytes ) );
3641
3642cleanup:
3643 mbedtls_mpi_free( &r );
3644 mbedtls_mpi_free( &s );
3645 if( ret == 0 )
3646 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003647 return( mbedtls_to_psa_error( ret ) );
3648}
3649
3650static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3651 const uint8_t *hash,
3652 size_t hash_length,
3653 const uint8_t *signature,
3654 size_t signature_length )
3655{
Janos Follath24eed8d2019-11-22 13:21:35 +00003656 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003657 mbedtls_mpi r, s;
3658 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3659 mbedtls_mpi_init( &r );
3660 mbedtls_mpi_init( &s );
3661
3662 if( signature_length != 2 * curve_bytes )
3663 return( PSA_ERROR_INVALID_SIGNATURE );
3664
3665 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3666 signature,
3667 curve_bytes ) );
3668 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3669 signature + curve_bytes,
3670 curve_bytes ) );
3671
Steven Cooremanacda8342020-07-24 23:09:52 +02003672 /* Check whether the public part is loaded. If not, load it. */
3673 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3674 {
3675 MBEDTLS_MPI_CHK(
3676 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3677 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3678 }
3679
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003680 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3681 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003682
3683cleanup:
3684 mbedtls_mpi_free( &r );
3685 mbedtls_mpi_free( &s );
3686 return( mbedtls_to_psa_error( ret ) );
3687}
3688#endif /* MBEDTLS_ECDSA_C */
3689
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003690psa_status_t psa_sign_hash( psa_key_handle_t handle,
3691 psa_algorithm_t alg,
3692 const uint8_t *hash,
3693 size_t hash_length,
3694 uint8_t *signature,
3695 size_t signature_size,
3696 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003697{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003698 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003699 psa_status_t status;
3700
3701 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003702 /* Immediately reject a zero-length signature buffer. This guarantees
3703 * that signature must be a valid pointer. (On the other hand, the hash
3704 * buffer can in principle be empty since it doesn't actually have
3705 * to be a hash.) */
3706 if( signature_size == 0 )
3707 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003708
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003709 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003710 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003711 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003712 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003713 {
3714 status = PSA_ERROR_INVALID_ARGUMENT;
3715 goto exit;
3716 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003717
Steven Cooremancd84cb42020-07-16 20:28:36 +02003718 /* Try any of the available accelerators first */
3719 status = psa_driver_wrapper_sign_hash( slot,
3720 alg,
3721 hash,
3722 hash_length,
3723 signature,
3724 signature_size,
3725 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003726 if( status != PSA_ERROR_NOT_SUPPORTED ||
3727 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003728 goto exit;
3729
Steven Cooreman7a250572020-07-17 16:43:05 +02003730 /* If the operation was not supported by any accelerator, try fallback. */
Gilles Peskine20035e32018-02-03 22:44:14 +01003731#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003732 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003733 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003734 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003735
Steven Cooreman4fed4552020-08-03 14:46:03 +02003736 status = psa_load_rsa_representation( slot->attr.type,
3737 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003738 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003739 &rsa );
3740 if( status != PSA_SUCCESS )
3741 goto exit;
3742
Steven Cooremana2371e52020-07-28 14:30:39 +02003743 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003744 alg,
3745 hash, hash_length,
3746 signature, signature_size,
3747 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003748
Steven Cooremana2371e52020-07-28 14:30:39 +02003749 mbedtls_rsa_free( rsa );
3750 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003751 }
3752 else
3753#endif /* defined(MBEDTLS_RSA_C) */
3754#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003755 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003756 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003757#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003758 if(
3759#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3760 PSA_ALG_IS_ECDSA( alg )
3761#else
3762 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3763#endif
3764 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003765 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003766 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003767 status = psa_load_ecp_representation( slot->attr.type,
3768 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003769 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003770 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003771 if( status != PSA_SUCCESS )
3772 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003773 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003774 alg,
3775 hash, hash_length,
3776 signature, signature_size,
3777 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003778 mbedtls_ecp_keypair_free( ecp );
3779 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003780 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003781 else
3782#endif /* defined(MBEDTLS_ECDSA_C) */
3783 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003784 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003785 }
itayzafrir5c753392018-05-08 11:18:38 +03003786 }
3787 else
3788#endif /* defined(MBEDTLS_ECP_C) */
3789 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003790 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003791 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003792
3793exit:
3794 /* Fill the unused part of the output buffer (the whole buffer on error,
3795 * the trailing part on success) with something that isn't a valid mac
3796 * (barring an attack on the mac and deliberately-crafted input),
3797 * in case the caller doesn't check the return status properly. */
3798 if( status == PSA_SUCCESS )
3799 memset( signature + *signature_length, '!',
3800 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003801 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003802 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003803 /* If signature_size is 0 then we have nothing to do. We must not call
3804 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003805 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003806}
3807
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003808psa_status_t psa_verify_hash( psa_key_handle_t handle,
3809 psa_algorithm_t alg,
3810 const uint8_t *hash,
3811 size_t hash_length,
3812 const uint8_t *signature,
3813 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003814{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003815 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003816 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003817
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003818 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003819 if( status != PSA_SUCCESS )
3820 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003821
Steven Cooreman55ae2172020-07-17 19:46:15 +02003822 /* Try any of the available accelerators first */
3823 status = psa_driver_wrapper_verify_hash( slot,
3824 alg,
3825 hash,
3826 hash_length,
3827 signature,
3828 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003829 if( status != PSA_ERROR_NOT_SUPPORTED ||
3830 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +02003831 return status;
3832
Gilles Peskine61b91d42018-06-08 16:09:36 +02003833#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003834 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003835 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003836 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003837
Steven Cooreman4fed4552020-08-03 14:46:03 +02003838 status = psa_load_rsa_representation( slot->attr.type,
3839 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003840 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003841 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003842 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003843 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02003844
Steven Cooremana2371e52020-07-28 14:30:39 +02003845 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003846 alg,
3847 hash, hash_length,
3848 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003849 mbedtls_rsa_free( rsa );
3850 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003851 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003852 }
3853 else
3854#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003855#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003856 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003857 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003858#if defined(MBEDTLS_ECDSA_C)
3859 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003860 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003861 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003862 status = psa_load_ecp_representation( slot->attr.type,
3863 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003864 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003865 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003866 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003867 return( status );
Steven Cooremana2371e52020-07-28 14:30:39 +02003868 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003869 hash, hash_length,
3870 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003871 mbedtls_ecp_keypair_free( ecp );
3872 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02003873 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02003874 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003875 else
3876#endif /* defined(MBEDTLS_ECDSA_C) */
3877 {
3878 return( PSA_ERROR_INVALID_ARGUMENT );
3879 }
itayzafrir5c753392018-05-08 11:18:38 +03003880 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003881 else
3882#endif /* defined(MBEDTLS_ECP_C) */
3883 {
3884 return( PSA_ERROR_NOT_SUPPORTED );
3885 }
3886}
3887
Gilles Peskine072ac562018-06-30 00:21:29 +02003888#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3889static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3890 mbedtls_rsa_context *rsa )
3891{
3892 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3893 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3894 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3895 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3896}
3897#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3898
Gilles Peskinec5487a82018-12-03 18:08:14 +01003899psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003900 psa_algorithm_t alg,
3901 const uint8_t *input,
3902 size_t input_length,
3903 const uint8_t *salt,
3904 size_t salt_length,
3905 uint8_t *output,
3906 size_t output_size,
3907 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003908{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003909 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003910 psa_status_t status;
3911
Darryl Green5cc689a2018-07-24 15:34:10 +01003912 (void) input;
3913 (void) input_length;
3914 (void) salt;
3915 (void) output;
3916 (void) output_size;
3917
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003918 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003919
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003920 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3921 return( PSA_ERROR_INVALID_ARGUMENT );
3922
Gilles Peskine28f8f302019-07-24 13:30:31 +02003923 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003924 if( status != PSA_SUCCESS )
3925 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003926 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3927 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003928 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003929
3930#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003931 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003932 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003933 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003934 status = psa_load_rsa_representation( slot->attr.type,
3935 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003936 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003937 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003938 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003939 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003940
3941 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003942 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003943 status = PSA_ERROR_BUFFER_TOO_SMALL;
3944 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003945 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003946#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003947 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003948 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003949 status = mbedtls_to_psa_error(
3950 mbedtls_rsa_pkcs1_encrypt( rsa,
3951 mbedtls_ctr_drbg_random,
3952 &global_data.ctr_drbg,
3953 MBEDTLS_RSA_PUBLIC,
3954 input_length,
3955 input,
3956 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003957 }
3958 else
3959#endif /* MBEDTLS_PKCS1_V15 */
3960#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003961 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003962 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003963 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003964 status = mbedtls_to_psa_error(
3965 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3966 mbedtls_ctr_drbg_random,
3967 &global_data.ctr_drbg,
3968 MBEDTLS_RSA_PUBLIC,
3969 salt, salt_length,
3970 input_length,
3971 input,
3972 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003973 }
3974 else
3975#endif /* MBEDTLS_PKCS1_V21 */
3976 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003977 status = PSA_ERROR_INVALID_ARGUMENT;
3978 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003979 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003980rsa_exit:
3981 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003982 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003983
Steven Cooremana2371e52020-07-28 14:30:39 +02003984 mbedtls_rsa_free( rsa );
3985 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003986 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003987 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003988 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003989#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003990 {
3991 return( PSA_ERROR_NOT_SUPPORTED );
3992 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003993}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003994
Gilles Peskinec5487a82018-12-03 18:08:14 +01003995psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003996 psa_algorithm_t alg,
3997 const uint8_t *input,
3998 size_t input_length,
3999 const uint8_t *salt,
4000 size_t salt_length,
4001 uint8_t *output,
4002 size_t output_size,
4003 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004004{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004005 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004006 psa_status_t status;
4007
Darryl Green5cc689a2018-07-24 15:34:10 +01004008 (void) input;
4009 (void) input_length;
4010 (void) salt;
4011 (void) output;
4012 (void) output_size;
4013
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004014 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004015
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004016 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4017 return( PSA_ERROR_INVALID_ARGUMENT );
4018
Gilles Peskine28f8f302019-07-24 13:30:31 +02004019 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004020 if( status != PSA_SUCCESS )
4021 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004022 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004023 return( PSA_ERROR_INVALID_ARGUMENT );
4024
4025#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004026 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004027 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004028 mbedtls_rsa_context *rsa = NULL;
4029 status = psa_load_rsa_representation( slot->attr.type,
4030 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004031 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004032 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004033 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004034 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004035
Steven Cooremana2371e52020-07-28 14:30:39 +02004036 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004037 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004038 status = PSA_ERROR_INVALID_ARGUMENT;
4039 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004040 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004041
4042#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004043 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004044 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004045 status = mbedtls_to_psa_error(
4046 mbedtls_rsa_pkcs1_decrypt( rsa,
4047 mbedtls_ctr_drbg_random,
4048 &global_data.ctr_drbg,
4049 MBEDTLS_RSA_PRIVATE,
4050 output_length,
4051 input,
4052 output,
4053 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004054 }
4055 else
4056#endif /* MBEDTLS_PKCS1_V15 */
4057#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004058 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004059 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004060 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004061 status = mbedtls_to_psa_error(
4062 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4063 mbedtls_ctr_drbg_random,
4064 &global_data.ctr_drbg,
4065 MBEDTLS_RSA_PRIVATE,
4066 salt, salt_length,
4067 output_length,
4068 input,
4069 output,
4070 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004071 }
4072 else
4073#endif /* MBEDTLS_PKCS1_V21 */
4074 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004075 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004076 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004077
Steven Cooreman4fed4552020-08-03 14:46:03 +02004078rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004079 mbedtls_rsa_free( rsa );
4080 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004081 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004082 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004083 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004084#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004085 {
4086 return( PSA_ERROR_NOT_SUPPORTED );
4087 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004088}
Gilles Peskine20035e32018-02-03 22:44:14 +01004089
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004090
4091
mohammad1603503973b2018-03-12 15:59:30 +02004092/****************************************************************/
4093/* Symmetric cryptography */
4094/****************************************************************/
4095
Gilles Peskinee553c652018-06-04 16:22:46 +02004096static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004097 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004098 psa_algorithm_t alg,
4099 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004100{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004101 int ret = 0;
4102 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004103 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004104 size_t key_bits;
4105 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004106 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4107 PSA_KEY_USAGE_ENCRYPT :
4108 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004109
Steven Cooremand3feccd2020-09-01 15:56:14 +02004110 /* A context must be freshly initialized before it can be set up. */
4111 if( operation->alg != 0 )
4112 return( PSA_ERROR_BAD_STATE );
4113
Steven Cooremana07b9972020-09-10 14:54:14 +02004114 /* The requested algorithm must be one that can be processed by cipher. */
4115 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004116 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004117
Steven Cooremanef8575e2020-09-11 11:44:50 +02004118 /* Fetch key material from key storage. */
4119 status = psa_get_key_from_slot( handle, &slot, usage, alg );
4120 if( status != PSA_SUCCESS )
4121 goto exit;
4122
4123 /* Initialize the operation struct members, except for alg. The alg member
4124 * is used to indicate to psa_cipher_abort that there are resources to free,
4125 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004126 operation->key_set = 0;
4127 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004128 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004129 operation->iv_size = 0;
4130 operation->block_size = 0;
4131 if( alg == PSA_ALG_ECB_NO_PADDING )
4132 operation->iv_required = 0;
4133 else
4134 operation->iv_required = 1;
4135
Steven Cooremana07b9972020-09-10 14:54:14 +02004136 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004137 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004138 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004139 slot,
4140 alg );
4141 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004142 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004143 slot,
4144 alg );
4145
Steven Cooremanef8575e2020-09-11 11:44:50 +02004146 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004147 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004148 /* Once the driver context is initialised, it needs to be freed using
4149 * psa_cipher_abort. Indicate this through setting alg. */
4150 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004151 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004152
Steven Cooremand3feccd2020-09-01 15:56:14 +02004153 if( status != PSA_ERROR_NOT_SUPPORTED ||
4154 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4155 goto exit;
4156
Steven Cooremana07b9972020-09-10 14:54:14 +02004157 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004158 * available for the given algorithm & key. */
4159 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004160
Steven Cooremana07b9972020-09-10 14:54:14 +02004161 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004162 * psa_cipher_abort. Indicate there is something to be freed through setting
4163 * alg, and indicate the operation is being done using mbedtls crypto through
4164 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004165 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004166 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004167
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004168 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004169 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004170 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004171 {
4172 status = PSA_ERROR_NOT_SUPPORTED;
4173 goto exit;
4174 }
mohammad1603503973b2018-03-12 15:59:30 +02004175
mohammad1603503973b2018-03-12 15:59:30 +02004176 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004177 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004178 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004179
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004180#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004181 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004182 {
4183 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004184 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004185 memcpy( keys, slot->data.key.data, 16 );
4186 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004187 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4188 keys,
4189 192, cipher_operation );
4190 }
4191 else
4192#endif
4193 {
4194 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004195 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004196 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004197 }
Moran Peker41deec42018-04-04 15:43:05 +03004198 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004199 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004200
mohammad16038481e742018-03-18 13:57:31 +02004201#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004202 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004203 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004204 case PSA_ALG_CBC_NO_PADDING:
4205 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4206 MBEDTLS_PADDING_NONE );
4207 break;
4208 case PSA_ALG_CBC_PKCS7:
4209 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4210 MBEDTLS_PADDING_PKCS7 );
4211 break;
4212 default:
4213 /* The algorithm doesn't involve padding. */
4214 ret = 0;
4215 break;
4216 }
4217 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004218 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004219#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4220
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004221 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004222 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004223 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4224 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004225 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004226 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004227 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004228#if defined(MBEDTLS_CHACHA20_C)
4229 else
4230 if( alg == PSA_ALG_CHACHA20 )
4231 operation->iv_size = 12;
4232#endif
mohammad1603503973b2018-03-12 15:59:30 +02004233
Steven Cooreman7df02922020-09-09 15:28:49 +02004234 status = PSA_SUCCESS;
4235
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004236exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004237 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004238 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004239 if( status == PSA_SUCCESS )
4240 {
4241 /* Update operation flags for both driver and software implementations */
4242 operation->key_set = 1;
4243 }
4244 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004245 psa_cipher_abort( operation );
4246 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004247}
4248
Gilles Peskinefe119512018-07-08 21:39:34 +02004249psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004250 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004251 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004252{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004253 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004254}
4255
Gilles Peskinefe119512018-07-08 21:39:34 +02004256psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004257 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004258 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004259{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004260 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004261}
4262
Gilles Peskinefe119512018-07-08 21:39:34 +02004263psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004264 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004265 size_t iv_size,
4266 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004267{
itayzafrir534bd7c2018-08-02 13:56:32 +03004268 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004270 if( operation->iv_set || ! operation->iv_required )
4271 {
4272 return( PSA_ERROR_BAD_STATE );
4273 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004274
Steven Cooremanef8575e2020-09-11 11:44:50 +02004275 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004276 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004277 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004278 iv,
4279 iv_size,
4280 iv_length );
4281 goto exit;
4282 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004283
Moran Peker41deec42018-04-04 15:43:05 +03004284 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004285 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004286 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004287 goto exit;
4288 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004289 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4290 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004291 if( ret != 0 )
4292 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004293 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004294 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004295 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004296
mohammad16038481e742018-03-18 13:57:31 +02004297 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004298 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004299
Moran Peker395db872018-05-31 14:07:14 +03004300exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004301 if( status == PSA_SUCCESS )
4302 operation->iv_set = 1;
4303 else
Moran Peker395db872018-05-31 14:07:14 +03004304 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004305 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004306}
4307
Gilles Peskinefe119512018-07-08 21:39:34 +02004308psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004309 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004310 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004311{
itayzafrir534bd7c2018-08-02 13:56:32 +03004312 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004313 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004314 if( operation->iv_set || ! operation->iv_required )
4315 {
4316 return( PSA_ERROR_BAD_STATE );
4317 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004318
Steven Cooremanef8575e2020-09-11 11:44:50 +02004319 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004320 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004321 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004322 iv,
4323 iv_length );
4324 goto exit;
4325 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004326
Moran Pekera28258c2018-05-29 16:25:04 +03004327 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004328 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004329 status = PSA_ERROR_INVALID_ARGUMENT;
4330 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004331 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004332 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4333 status = mbedtls_to_psa_error( ret );
4334exit:
4335 if( status == PSA_SUCCESS )
4336 operation->iv_set = 1;
4337 else
mohammad1603503973b2018-03-12 15:59:30 +02004338 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004339 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004340}
4341
Steven Cooreman177deba2020-09-07 17:14:14 +02004342/* Process input for which the algorithm is set to ECB mode. This requires
4343 * manual processing, since the PSA API is defined as being able to process
4344 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4345 * underlying mbedtls_cipher_update only takes full blocks. */
4346static psa_status_t psa_cipher_update_ecb_internal(
4347 mbedtls_cipher_context_t *ctx,
4348 const uint8_t *input,
4349 size_t input_length,
4350 uint8_t *output,
4351 size_t output_size,
4352 size_t *output_length )
4353{
4354 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4355 size_t block_size = ctx->cipher_info->block_size;
4356 size_t internal_output_length = 0;
4357 *output_length = 0;
4358
4359 if( input_length == 0 )
4360 {
4361 status = PSA_SUCCESS;
4362 goto exit;
4363 }
4364
4365 if( ctx->unprocessed_len > 0 )
4366 {
4367 /* Fill up to block size, and run the block if there's a full one. */
4368 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4369
4370 if( input_length < bytes_to_copy )
4371 bytes_to_copy = input_length;
4372
4373 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4374 input, bytes_to_copy );
4375 input_length -= bytes_to_copy;
4376 input += bytes_to_copy;
4377 ctx->unprocessed_len += bytes_to_copy;
4378
4379 if( ctx->unprocessed_len == block_size )
4380 {
4381 status = mbedtls_to_psa_error(
4382 mbedtls_cipher_update( ctx,
4383 ctx->unprocessed_data,
4384 block_size,
4385 output, &internal_output_length ) );
4386
4387 if( status != PSA_SUCCESS )
4388 goto exit;
4389
4390 output += internal_output_length;
4391 output_size -= internal_output_length;
4392 *output_length += internal_output_length;
4393 ctx->unprocessed_len = 0;
4394 }
4395 }
4396
4397 while( input_length >= block_size )
4398 {
4399 /* Run all full blocks we have, one by one */
4400 status = mbedtls_to_psa_error(
4401 mbedtls_cipher_update( ctx, input,
4402 block_size,
4403 output, &internal_output_length ) );
4404
4405 if( status != PSA_SUCCESS )
4406 goto exit;
4407
4408 input_length -= block_size;
4409 input += block_size;
4410
4411 output += internal_output_length;
4412 output_size -= internal_output_length;
4413 *output_length += internal_output_length;
4414 }
4415
4416 if( input_length > 0 )
4417 {
4418 /* Save unprocessed bytes for later processing */
4419 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4420 input, input_length );
4421 ctx->unprocessed_len += input_length;
4422 }
4423
4424 status = PSA_SUCCESS;
4425
4426exit:
4427 return( status );
4428}
4429
Gilles Peskinee553c652018-06-04 16:22:46 +02004430psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4431 const uint8_t *input,
4432 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004433 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004434 size_t output_size,
4435 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004436{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004437 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004438 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004439 if( operation->alg == 0 )
4440 {
4441 return( PSA_ERROR_BAD_STATE );
4442 }
4443 if( operation->iv_required && ! operation->iv_set )
4444 {
4445 return( PSA_ERROR_BAD_STATE );
4446 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004447
Steven Cooremanef8575e2020-09-11 11:44:50 +02004448 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004449 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004450 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004451 input,
4452 input_length,
4453 output,
4454 output_size,
4455 output_length );
4456 goto exit;
4457 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004458
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004459 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004460 {
4461 /* Take the unprocessed partial block left over from previous
4462 * update calls, if any, plus the input to this call. Remove
4463 * the last partial block, if any. You get the data that will be
4464 * output in this call. */
4465 expected_output_size =
4466 ( operation->ctx.cipher.unprocessed_len + input_length )
4467 / operation->block_size * operation->block_size;
4468 }
4469 else
4470 {
4471 expected_output_size = input_length;
4472 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004473
Gilles Peskine89d789c2018-06-04 17:17:16 +02004474 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004475 {
4476 status = PSA_ERROR_BUFFER_TOO_SMALL;
4477 goto exit;
4478 }
mohammad160382759612018-03-12 18:16:40 +02004479
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004480 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4481 {
4482 /* mbedtls_cipher_update has an API inconsistency: it will only
4483 * process a single block at a time in ECB mode. Abstract away that
4484 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004485 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4486 input,
4487 input_length,
4488 output,
4489 output_size,
4490 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004491 }
4492 else
4493 {
4494 status = mbedtls_to_psa_error(
4495 mbedtls_cipher_update( &operation->ctx.cipher, input,
4496 input_length, output, output_length ) );
4497 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004498exit:
4499 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004500 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004501 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004502}
4503
Gilles Peskinee553c652018-06-04 16:22:46 +02004504psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4505 uint8_t *output,
4506 size_t output_size,
4507 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004508{
David Saadab4ecc272019-02-14 13:48:10 +02004509 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004510 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004511 if( operation->alg == 0 )
4512 {
4513 return( PSA_ERROR_BAD_STATE );
4514 }
4515 if( operation->iv_required && ! operation->iv_set )
4516 {
4517 return( PSA_ERROR_BAD_STATE );
4518 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004519
Steven Cooremanef8575e2020-09-11 11:44:50 +02004520 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004521 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004522 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004523 output,
4524 output_size,
4525 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004526 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004527 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004528
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004529 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004530 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004531 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004532 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004533 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004534 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004535 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004536 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004537 }
4538
Steven Cooremanef8575e2020-09-11 11:44:50 +02004539 status = mbedtls_to_psa_error(
4540 mbedtls_cipher_finish( &operation->ctx.cipher,
4541 temp_output_buffer,
4542 output_length ) );
4543 if( status != PSA_SUCCESS )
4544 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004545
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004546 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004547 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004548 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004549 memcpy( output, temp_output_buffer, *output_length );
4550 else
Janos Follath315b51c2018-07-09 16:04:51 +01004551 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004552
Steven Cooremanef8575e2020-09-11 11:44:50 +02004553exit:
4554 if( operation->mbedtls_in_use == 1 )
4555 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004556
Steven Cooremanef8575e2020-09-11 11:44:50 +02004557 if( status == PSA_SUCCESS )
4558 return( psa_cipher_abort( operation ) );
4559 else
4560 {
4561 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004562 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004563
Steven Cooremanef8575e2020-09-11 11:44:50 +02004564 return( status );
4565 }
mohammad1603503973b2018-03-12 15:59:30 +02004566}
4567
Gilles Peskinee553c652018-06-04 16:22:46 +02004568psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4569{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004570 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004571 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004572 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004573 * in use. It's ok to call abort on such an object, and there's
4574 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004575 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004576 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004577
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004578 /* Sanity check (shouldn't happen: operation->alg should
4579 * always have been initialized to a valid value). */
4580 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4581 return( PSA_ERROR_BAD_STATE );
4582
Steven Cooremanef8575e2020-09-11 11:44:50 +02004583 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004584 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004585 else
4586 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004587
Moran Peker41deec42018-04-04 15:43:05 +03004588 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004589 operation->key_set = 0;
4590 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004591 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004592 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004593 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004594 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004595
Moran Peker395db872018-05-31 14:07:14 +03004596 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004597}
4598
Gilles Peskinea0655c32018-04-30 17:06:50 +02004599
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004600
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004601
mohammad16035955c982018-04-26 00:53:03 +03004602/****************************************************************/
4603/* AEAD */
4604/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004605
Gilles Peskineedf9a652018-08-17 18:11:56 +02004606typedef struct
4607{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004608 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004609 const mbedtls_cipher_info_t *cipher_info;
4610 union
4611 {
4612#if defined(MBEDTLS_CCM_C)
4613 mbedtls_ccm_context ccm;
4614#endif /* MBEDTLS_CCM_C */
4615#if defined(MBEDTLS_GCM_C)
4616 mbedtls_gcm_context gcm;
4617#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004618#if defined(MBEDTLS_CHACHAPOLY_C)
4619 mbedtls_chachapoly_context chachapoly;
4620#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004621 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004622 psa_algorithm_t core_alg;
4623 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004624 uint8_t tag_length;
4625} aead_operation_t;
4626
Gilles Peskine30a9e412019-01-14 18:36:12 +01004627static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004628{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004629 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004630 {
4631#if defined(MBEDTLS_CCM_C)
4632 case PSA_ALG_CCM:
4633 mbedtls_ccm_free( &operation->ctx.ccm );
4634 break;
4635#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004636#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004637 case PSA_ALG_GCM:
4638 mbedtls_gcm_free( &operation->ctx.gcm );
4639 break;
4640#endif /* MBEDTLS_GCM_C */
4641 }
4642}
4643
4644static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004645 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004646 psa_key_usage_t usage,
4647 psa_algorithm_t alg )
4648{
4649 psa_status_t status;
4650 size_t key_bits;
4651 mbedtls_cipher_id_t cipher_id;
4652
Gilles Peskine28f8f302019-07-24 13:30:31 +02004653 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004654 if( status != PSA_SUCCESS )
4655 return( status );
4656
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004657 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004658
4659 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004660 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004661 &cipher_id );
4662 if( operation->cipher_info == NULL )
4663 return( PSA_ERROR_NOT_SUPPORTED );
4664
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004665 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004666 {
4667#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004668 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4669 operation->core_alg = PSA_ALG_CCM;
4670 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004671 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4672 * The call to mbedtls_ccm_encrypt_and_tag or
4673 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004674 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004675 return( PSA_ERROR_INVALID_ARGUMENT );
4676 mbedtls_ccm_init( &operation->ctx.ccm );
4677 status = mbedtls_to_psa_error(
4678 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004679 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004680 (unsigned int) key_bits ) );
4681 if( status != 0 )
4682 goto cleanup;
4683 break;
4684#endif /* MBEDTLS_CCM_C */
4685
4686#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004687 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4688 operation->core_alg = PSA_ALG_GCM;
4689 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004690 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4691 * The call to mbedtls_gcm_crypt_and_tag or
4692 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004693 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004694 return( PSA_ERROR_INVALID_ARGUMENT );
4695 mbedtls_gcm_init( &operation->ctx.gcm );
4696 status = mbedtls_to_psa_error(
4697 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004698 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004699 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004700 if( status != 0 )
4701 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004702 break;
4703#endif /* MBEDTLS_GCM_C */
4704
Gilles Peskine26869f22019-05-06 15:25:00 +02004705#if defined(MBEDTLS_CHACHAPOLY_C)
4706 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4707 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4708 operation->full_tag_length = 16;
4709 /* We only support the default tag length. */
4710 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4711 return( PSA_ERROR_NOT_SUPPORTED );
4712 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4713 status = mbedtls_to_psa_error(
4714 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004715 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004716 if( status != 0 )
4717 goto cleanup;
4718 break;
4719#endif /* MBEDTLS_CHACHAPOLY_C */
4720
Gilles Peskineedf9a652018-08-17 18:11:56 +02004721 default:
4722 return( PSA_ERROR_NOT_SUPPORTED );
4723 }
4724
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004725 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4726 {
4727 status = PSA_ERROR_INVALID_ARGUMENT;
4728 goto cleanup;
4729 }
4730 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004731
Gilles Peskineedf9a652018-08-17 18:11:56 +02004732 return( PSA_SUCCESS );
4733
4734cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004735 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004736 return( status );
4737}
4738
Gilles Peskinec5487a82018-12-03 18:08:14 +01004739psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004740 psa_algorithm_t alg,
4741 const uint8_t *nonce,
4742 size_t nonce_length,
4743 const uint8_t *additional_data,
4744 size_t additional_data_length,
4745 const uint8_t *plaintext,
4746 size_t plaintext_length,
4747 uint8_t *ciphertext,
4748 size_t ciphertext_size,
4749 size_t *ciphertext_length )
4750{
mohammad16035955c982018-04-26 00:53:03 +03004751 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004752 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004753 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004754
mohammad1603f08a5502018-06-03 15:05:47 +03004755 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004756
Gilles Peskinec5487a82018-12-03 18:08:14 +01004757 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004758 if( status != PSA_SUCCESS )
4759 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004760
Gilles Peskineedf9a652018-08-17 18:11:56 +02004761 /* For all currently supported modes, the tag is at the end of the
4762 * ciphertext. */
4763 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4764 {
4765 status = PSA_ERROR_BUFFER_TOO_SMALL;
4766 goto exit;
4767 }
4768 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004769
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004770#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004771 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004772 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004773 status = mbedtls_to_psa_error(
4774 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4775 MBEDTLS_GCM_ENCRYPT,
4776 plaintext_length,
4777 nonce, nonce_length,
4778 additional_data, additional_data_length,
4779 plaintext, ciphertext,
4780 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004781 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004782 else
4783#endif /* MBEDTLS_GCM_C */
4784#if defined(MBEDTLS_CCM_C)
4785 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004786 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004787 status = mbedtls_to_psa_error(
4788 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4789 plaintext_length,
4790 nonce, nonce_length,
4791 additional_data,
4792 additional_data_length,
4793 plaintext, ciphertext,
4794 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004795 }
mohammad16035c8845f2018-05-09 05:40:09 -07004796 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004797#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004798#if defined(MBEDTLS_CHACHAPOLY_C)
4799 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4800 {
4801 if( nonce_length != 12 || operation.tag_length != 16 )
4802 {
4803 status = PSA_ERROR_NOT_SUPPORTED;
4804 goto exit;
4805 }
4806 status = mbedtls_to_psa_error(
4807 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4808 plaintext_length,
4809 nonce,
4810 additional_data,
4811 additional_data_length,
4812 plaintext,
4813 ciphertext,
4814 tag ) );
4815 }
4816 else
4817#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004818 {
mohammad1603554faad2018-06-03 15:07:38 +03004819 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004820 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004821
Gilles Peskineedf9a652018-08-17 18:11:56 +02004822 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4823 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004824
Gilles Peskineedf9a652018-08-17 18:11:56 +02004825exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004826 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004827 if( status == PSA_SUCCESS )
4828 *ciphertext_length = plaintext_length + operation.tag_length;
4829 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004830}
4831
Gilles Peskineee652a32018-06-01 19:23:52 +02004832/* Locate the tag in a ciphertext buffer containing the encrypted data
4833 * followed by the tag. Return the length of the part preceding the tag in
4834 * *plaintext_length. This is the size of the plaintext in modes where
4835 * the encrypted data has the same size as the plaintext, such as
4836 * CCM and GCM. */
4837static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4838 const uint8_t *ciphertext,
4839 size_t ciphertext_length,
4840 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004841 const uint8_t **p_tag )
4842{
4843 size_t payload_length;
4844 if( tag_length > ciphertext_length )
4845 return( PSA_ERROR_INVALID_ARGUMENT );
4846 payload_length = ciphertext_length - tag_length;
4847 if( payload_length > plaintext_size )
4848 return( PSA_ERROR_BUFFER_TOO_SMALL );
4849 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004850 return( PSA_SUCCESS );
4851}
4852
Gilles Peskinec5487a82018-12-03 18:08:14 +01004853psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004854 psa_algorithm_t alg,
4855 const uint8_t *nonce,
4856 size_t nonce_length,
4857 const uint8_t *additional_data,
4858 size_t additional_data_length,
4859 const uint8_t *ciphertext,
4860 size_t ciphertext_length,
4861 uint8_t *plaintext,
4862 size_t plaintext_size,
4863 size_t *plaintext_length )
4864{
mohammad16035955c982018-04-26 00:53:03 +03004865 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004866 aead_operation_t operation;
4867 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004868
Gilles Peskineee652a32018-06-01 19:23:52 +02004869 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004870
Gilles Peskinec5487a82018-12-03 18:08:14 +01004871 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004872 if( status != PSA_SUCCESS )
4873 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004874
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004875 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4876 ciphertext, ciphertext_length,
4877 plaintext_size, &tag );
4878 if( status != PSA_SUCCESS )
4879 goto exit;
4880
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004881#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004882 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004883 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004884 status = mbedtls_to_psa_error(
4885 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4886 ciphertext_length - operation.tag_length,
4887 nonce, nonce_length,
4888 additional_data,
4889 additional_data_length,
4890 tag, operation.tag_length,
4891 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004892 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004893 else
4894#endif /* MBEDTLS_GCM_C */
4895#if defined(MBEDTLS_CCM_C)
4896 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004897 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004898 status = mbedtls_to_psa_error(
4899 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4900 ciphertext_length - operation.tag_length,
4901 nonce, nonce_length,
4902 additional_data,
4903 additional_data_length,
4904 ciphertext, plaintext,
4905 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004906 }
mohammad160339574652018-06-01 04:39:53 -07004907 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004908#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004909#if defined(MBEDTLS_CHACHAPOLY_C)
4910 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4911 {
4912 if( nonce_length != 12 || operation.tag_length != 16 )
4913 {
4914 status = PSA_ERROR_NOT_SUPPORTED;
4915 goto exit;
4916 }
4917 status = mbedtls_to_psa_error(
4918 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4919 ciphertext_length - operation.tag_length,
4920 nonce,
4921 additional_data,
4922 additional_data_length,
4923 tag,
4924 ciphertext,
4925 plaintext ) );
4926 }
4927 else
4928#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004929 {
mohammad1603554faad2018-06-03 15:07:38 +03004930 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004931 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004932
Gilles Peskineedf9a652018-08-17 18:11:56 +02004933 if( status != PSA_SUCCESS && plaintext_size != 0 )
4934 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004935
Gilles Peskineedf9a652018-08-17 18:11:56 +02004936exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004937 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004938 if( status == PSA_SUCCESS )
4939 *plaintext_length = ciphertext_length - operation.tag_length;
4940 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004941}
4942
Gilles Peskinea0655c32018-04-30 17:06:50 +02004943
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004944
Gilles Peskine20035e32018-02-03 22:44:14 +01004945/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004946/* Generators */
4947/****************************************************************/
4948
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004949#define HKDF_STATE_INIT 0 /* no input yet */
4950#define HKDF_STATE_STARTED 1 /* got salt */
4951#define HKDF_STATE_KEYED 2 /* got key */
4952#define HKDF_STATE_OUTPUT 3 /* output started */
4953
Gilles Peskinecbe66502019-05-16 16:59:18 +02004954static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004955 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004956{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004957 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4958 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004959 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004960 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004961}
4962
4963
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004964psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004965{
4966 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004967 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004968 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004969 {
4970 /* The object has (apparently) been initialized but it is not
4971 * in use. It's ok to call abort on such an object, and there's
4972 * nothing to do. */
4973 }
4974 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004975#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004976 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004977 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004978 mbedtls_free( operation->ctx.hkdf.info );
4979 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004980 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004981 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004982 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004983 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004984 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004985 if( operation->ctx.tls12_prf.seed != NULL )
4986 {
4987 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4988 operation->ctx.tls12_prf.seed_length );
4989 mbedtls_free( operation->ctx.tls12_prf.seed );
4990 }
4991
4992 if( operation->ctx.tls12_prf.label != NULL )
4993 {
4994 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4995 operation->ctx.tls12_prf.label_length );
4996 mbedtls_free( operation->ctx.tls12_prf.label );
4997 }
4998
4999 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5000
5001 /* We leave the fields Ai and output_block to be erased safely by the
5002 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005003 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005004 else
5005#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005006 {
5007 status = PSA_ERROR_BAD_STATE;
5008 }
Janos Follath083036a2019-06-11 10:22:26 +01005009 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005010 return( status );
5011}
5012
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005013psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005014 size_t *capacity)
5015{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005016 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005017 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005018 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005019 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005020 }
5021
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005022 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005023 return( PSA_SUCCESS );
5024}
5025
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005026psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005027 size_t capacity )
5028{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005029 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005030 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005031 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005032 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005033 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005034 return( PSA_SUCCESS );
5035}
5036
Gilles Peskinebef7f142018-07-12 17:22:21 +02005037#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005038/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005039 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005040static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005041 psa_algorithm_t hash_alg,
5042 uint8_t *output,
5043 size_t output_length )
5044{
5045 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5046 psa_status_t status;
5047
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005048 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5049 return( PSA_ERROR_BAD_STATE );
5050 hkdf->state = HKDF_STATE_OUTPUT;
5051
Gilles Peskinebef7f142018-07-12 17:22:21 +02005052 while( output_length != 0 )
5053 {
5054 /* Copy what remains of the current block */
5055 uint8_t n = hash_length - hkdf->offset_in_block;
5056 if( n > output_length )
5057 n = (uint8_t) output_length;
5058 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5059 output += n;
5060 output_length -= n;
5061 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005062 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005063 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005064 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005065 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005066 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005067 * object was corrupted or if this function is called directly
5068 * inside the library. */
5069 if( hkdf->block_number == 0xff )
5070 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005071
5072 /* We need a new block */
5073 ++hkdf->block_number;
5074 hkdf->offset_in_block = 0;
5075 status = psa_hmac_setup_internal( &hkdf->hmac,
5076 hkdf->prk, hash_length,
5077 hash_alg );
5078 if( status != PSA_SUCCESS )
5079 return( status );
5080 if( hkdf->block_number != 1 )
5081 {
5082 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5083 hkdf->output_block,
5084 hash_length );
5085 if( status != PSA_SUCCESS )
5086 return( status );
5087 }
5088 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5089 hkdf->info,
5090 hkdf->info_length );
5091 if( status != PSA_SUCCESS )
5092 return( status );
5093 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5094 &hkdf->block_number, 1 );
5095 if( status != PSA_SUCCESS )
5096 return( status );
5097 status = psa_hmac_finish_internal( &hkdf->hmac,
5098 hkdf->output_block,
5099 sizeof( hkdf->output_block ) );
5100 if( status != PSA_SUCCESS )
5101 return( status );
5102 }
5103
5104 return( PSA_SUCCESS );
5105}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005106
Janos Follath7742fee2019-06-17 12:58:10 +01005107static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5108 psa_tls12_prf_key_derivation_t *tls12_prf,
5109 psa_algorithm_t alg )
5110{
5111 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5112 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005113 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5114 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005115
Janos Follath7742fee2019-06-17 12:58:10 +01005116 /* We can't be wanting more output after block 0xff, otherwise
5117 * the capacity check in psa_key_derivation_output_bytes() would have
5118 * prevented this call. It could happen only if the operation
5119 * object was corrupted or if this function is called directly
5120 * inside the library. */
5121 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005122 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005123
5124 /* We need a new block */
5125 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005126 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005127
5128 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5129 *
5130 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5131 *
5132 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5133 * HMAC_hash(secret, A(2) + seed) +
5134 * HMAC_hash(secret, A(3) + seed) + ...
5135 *
5136 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005137 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005138 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005139 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005140 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005141 * is currently extracted as `output_block` and where i is
5142 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005143 */
5144
Janos Follathea29bfb2019-06-19 12:21:20 +01005145 /* Save the hash context before using it, to preserve the hash state with
5146 * only the inner padding in it. We need this, because inner padding depends
5147 * on the key (secret in the RFC's terminology). */
5148 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5149 if( status != PSA_SUCCESS )
5150 goto cleanup;
5151
5152 /* Calculate A(i) where i = tls12_prf->block_number. */
5153 if( tls12_prf->block_number == 1 )
5154 {
5155 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5156 * the variable seed and in this instance means it in the context of the
5157 * P_hash function, where seed = label + seed.) */
5158 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5159 tls12_prf->label, tls12_prf->label_length );
5160 if( status != PSA_SUCCESS )
5161 goto cleanup;
5162 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5163 tls12_prf->seed, tls12_prf->seed_length );
5164 if( status != PSA_SUCCESS )
5165 goto cleanup;
5166 }
5167 else
5168 {
5169 /* A(i) = HMAC_hash(secret, A(i-1)) */
5170 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5171 tls12_prf->Ai, hash_length );
5172 if( status != PSA_SUCCESS )
5173 goto cleanup;
5174 }
5175
5176 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5177 tls12_prf->Ai, hash_length );
5178 if( status != PSA_SUCCESS )
5179 goto cleanup;
5180 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5181 if( status != PSA_SUCCESS )
5182 goto cleanup;
5183
5184 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5185 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5186 tls12_prf->Ai, hash_length );
5187 if( status != PSA_SUCCESS )
5188 goto cleanup;
5189 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5190 tls12_prf->label, tls12_prf->label_length );
5191 if( status != PSA_SUCCESS )
5192 goto cleanup;
5193 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5194 tls12_prf->seed, tls12_prf->seed_length );
5195 if( status != PSA_SUCCESS )
5196 goto cleanup;
5197 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5198 tls12_prf->output_block, hash_length );
5199 if( status != PSA_SUCCESS )
5200 goto cleanup;
5201 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5202 if( status != PSA_SUCCESS )
5203 goto cleanup;
5204
Janos Follath7742fee2019-06-17 12:58:10 +01005205
5206cleanup:
5207
Janos Follathea29bfb2019-06-19 12:21:20 +01005208 cleanup_status = psa_hash_abort( &backup );
5209 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5210 status = cleanup_status;
5211
5212 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005213}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005214
Janos Follath844eb0e2019-06-19 12:10:49 +01005215static psa_status_t psa_key_derivation_tls12_prf_read(
5216 psa_tls12_prf_key_derivation_t *tls12_prf,
5217 psa_algorithm_t alg,
5218 uint8_t *output,
5219 size_t output_length )
5220{
5221 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5222 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5223 psa_status_t status;
5224 uint8_t offset, length;
5225
5226 while( output_length != 0 )
5227 {
5228 /* Check if we have fully processed the current block. */
5229 if( tls12_prf->left_in_block == 0 )
5230 {
5231 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5232 alg );
5233 if( status != PSA_SUCCESS )
5234 return( status );
5235
5236 continue;
5237 }
5238
5239 if( tls12_prf->left_in_block > output_length )
5240 length = (uint8_t) output_length;
5241 else
5242 length = tls12_prf->left_in_block;
5243
5244 offset = hash_length - tls12_prf->left_in_block;
5245 memcpy( output, tls12_prf->output_block + offset, length );
5246 output += length;
5247 output_length -= length;
5248 tls12_prf->left_in_block -= length;
5249 }
5250
5251 return( PSA_SUCCESS );
5252}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005253#endif /* MBEDTLS_MD_C */
5254
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005255psa_status_t psa_key_derivation_output_bytes(
5256 psa_key_derivation_operation_t *operation,
5257 uint8_t *output,
5258 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005259{
5260 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005262
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005263 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005264 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005265 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005266 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005267 }
5268
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005270 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005272 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005273 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005274 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005275 goto exit;
5276 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005277 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005278 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005279 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005280 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005281 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5282 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005283 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005284 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005285 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005286 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005288
Gilles Peskinebef7f142018-07-12 17:22:21 +02005289#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005290 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005291 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005292 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005293 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005294 output, output_length );
5295 }
Janos Follathadbec812019-06-14 11:05:39 +01005296 else
Janos Follathadbec812019-06-14 11:05:39 +01005297 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005298 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005299 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005300 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005301 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005302 output_length );
5303 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005304 else
5305#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005306 {
5307 return( PSA_ERROR_BAD_STATE );
5308 }
5309
5310exit:
5311 if( status != PSA_SUCCESS )
5312 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005313 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005314 * This allows us to differentiate between exhausted operations and
5315 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5316 * operations. */
5317 psa_algorithm_t alg = operation->alg;
5318 psa_key_derivation_abort( operation );
5319 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005320 memset( output, '!', output_length );
5321 }
5322 return( status );
5323}
5324
Gilles Peskine08542d82018-07-19 17:05:42 +02005325#if defined(MBEDTLS_DES_C)
5326static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5327{
5328 if( data_size >= 8 )
5329 mbedtls_des_key_set_parity( data );
5330 if( data_size >= 16 )
5331 mbedtls_des_key_set_parity( data + 8 );
5332 if( data_size >= 24 )
5333 mbedtls_des_key_set_parity( data + 16 );
5334}
5335#endif /* MBEDTLS_DES_C */
5336
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005337static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005338 psa_key_slot_t *slot,
5339 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005340 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005341{
5342 uint8_t *data = NULL;
5343 size_t bytes = PSA_BITS_TO_BYTES( bits );
5344 psa_status_t status;
5345
Gilles Peskine8e338702019-07-30 20:06:31 +02005346 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005347 return( PSA_ERROR_INVALID_ARGUMENT );
5348 if( bits % 8 != 0 )
5349 return( PSA_ERROR_INVALID_ARGUMENT );
5350 data = mbedtls_calloc( 1, bytes );
5351 if( data == NULL )
5352 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5353
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005354 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005355 if( status != PSA_SUCCESS )
5356 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005357#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005358 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005359 psa_des_set_key_parity( data, bytes );
5360#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005361 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005362
5363exit:
5364 mbedtls_free( data );
5365 return( status );
5366}
5367
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005368psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005369 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005370 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005371{
5372 psa_status_t status;
5373 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005374 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005375
5376 /* Reject any attempt to create a zero-length key so that we don't
5377 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5378 if( psa_get_key_bits( attributes ) == 0 )
5379 return( PSA_ERROR_INVALID_ARGUMENT );
5380
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005381 if( ! operation->can_output_key )
5382 return( PSA_ERROR_NOT_PERMITTED );
5383
Gilles Peskinedf179142019-07-15 22:02:14 +02005384 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5385 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005386#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5387 if( driver != NULL )
5388 {
5389 /* Deriving a key in a secure element is not implemented yet. */
5390 status = PSA_ERROR_NOT_SUPPORTED;
5391 }
5392#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005393 if( status == PSA_SUCCESS )
5394 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005395 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005396 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005397 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005398 }
5399 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005400 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005401 if( status != PSA_SUCCESS )
5402 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005403 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005404 *handle = 0;
5405 }
5406 return( status );
5407}
5408
Gilles Peskineeab56e42018-07-12 17:12:33 +02005409
5410
5411/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005412/* Key derivation */
5413/****************************************************************/
5414
Gilles Peskine969c5d62019-01-16 15:53:06 +01005415static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005416 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005417 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005418{
Janos Follath5fe19732019-06-20 15:09:30 +01005419 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5420 * initialisers for this union leave some bytes unspecified.) */
5421 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5422
Gilles Peskine969c5d62019-01-16 15:53:06 +01005423 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005424#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005425 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5426 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5427 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005428 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005429 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005430 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5431 if( hash_size == 0 )
5432 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005433 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5434 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005435 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005436 {
5437 return( PSA_ERROR_NOT_SUPPORTED );
5438 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005439 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005440 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005441 }
5442#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005443 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005444 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005445}
5446
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005447psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005448 psa_algorithm_t alg )
5449{
5450 psa_status_t status;
5451
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005452 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005453 return( PSA_ERROR_BAD_STATE );
5454
Gilles Peskine6843c292019-01-18 16:44:49 +01005455 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5456 return( PSA_ERROR_INVALID_ARGUMENT );
5457 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005458 {
5459 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005460 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005461 }
5462 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5463 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005464 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005465 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005466 else
5467 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005468
5469 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005470 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005471 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005472}
5473
5474#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005475static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005476 psa_algorithm_t hash_alg,
5477 psa_key_derivation_step_t step,
5478 const uint8_t *data,
5479 size_t data_length )
5480{
5481 psa_status_t status;
5482 switch( step )
5483 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005484 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005485 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005486 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005487 status = psa_hmac_setup_internal( &hkdf->hmac,
5488 data, data_length,
5489 hash_alg );
5490 if( status != PSA_SUCCESS )
5491 return( status );
5492 hkdf->state = HKDF_STATE_STARTED;
5493 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005494 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005495 /* If no salt was provided, use an empty salt. */
5496 if( hkdf->state == HKDF_STATE_INIT )
5497 {
5498 status = psa_hmac_setup_internal( &hkdf->hmac,
5499 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005500 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005501 if( status != PSA_SUCCESS )
5502 return( status );
5503 hkdf->state = HKDF_STATE_STARTED;
5504 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005505 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005506 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005507 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5508 data, data_length );
5509 if( status != PSA_SUCCESS )
5510 return( status );
5511 status = psa_hmac_finish_internal( &hkdf->hmac,
5512 hkdf->prk,
5513 sizeof( hkdf->prk ) );
5514 if( status != PSA_SUCCESS )
5515 return( status );
5516 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5517 hkdf->block_number = 0;
5518 hkdf->state = HKDF_STATE_KEYED;
5519 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005520 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005521 if( hkdf->state == HKDF_STATE_OUTPUT )
5522 return( PSA_ERROR_BAD_STATE );
5523 if( hkdf->info_set )
5524 return( PSA_ERROR_BAD_STATE );
5525 hkdf->info_length = data_length;
5526 if( data_length != 0 )
5527 {
5528 hkdf->info = mbedtls_calloc( 1, data_length );
5529 if( hkdf->info == NULL )
5530 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5531 memcpy( hkdf->info, data, data_length );
5532 }
5533 hkdf->info_set = 1;
5534 return( PSA_SUCCESS );
5535 default:
5536 return( PSA_ERROR_INVALID_ARGUMENT );
5537 }
5538}
Janos Follathb03233e2019-06-11 15:30:30 +01005539
Janos Follathf08e2652019-06-13 09:05:41 +01005540static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5541 const uint8_t *data,
5542 size_t data_length )
5543{
5544 if( prf->state != TLS12_PRF_STATE_INIT )
5545 return( PSA_ERROR_BAD_STATE );
5546
Janos Follathd6dce9f2019-07-04 09:11:38 +01005547 if( data_length != 0 )
5548 {
5549 prf->seed = mbedtls_calloc( 1, data_length );
5550 if( prf->seed == NULL )
5551 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005552
Janos Follathd6dce9f2019-07-04 09:11:38 +01005553 memcpy( prf->seed, data, data_length );
5554 prf->seed_length = data_length;
5555 }
Janos Follathf08e2652019-06-13 09:05:41 +01005556
5557 prf->state = TLS12_PRF_STATE_SEED_SET;
5558
5559 return( PSA_SUCCESS );
5560}
5561
Janos Follath81550542019-06-13 14:26:34 +01005562static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5563 psa_algorithm_t hash_alg,
5564 const uint8_t *data,
5565 size_t data_length )
5566{
5567 psa_status_t status;
5568 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5569 return( PSA_ERROR_BAD_STATE );
5570
5571 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5572 if( status != PSA_SUCCESS )
5573 return( status );
5574
5575 prf->state = TLS12_PRF_STATE_KEY_SET;
5576
5577 return( PSA_SUCCESS );
5578}
5579
Janos Follath6660f0e2019-06-17 08:44:03 +01005580static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5581 psa_tls12_prf_key_derivation_t *prf,
5582 psa_algorithm_t hash_alg,
5583 const uint8_t *data,
5584 size_t data_length )
5585{
5586 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005587 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5588 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005589
5590 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5591 return( PSA_ERROR_INVALID_ARGUMENT );
5592
5593 /* Quoting RFC 4279, Section 2:
5594 *
5595 * The premaster secret is formed as follows: if the PSK is N octets
5596 * long, concatenate a uint16 with the value N, N zero octets, a second
5597 * uint16 with the value N, and the PSK itself.
5598 */
5599
Janos Follath40e13932019-06-26 13:22:29 +01005600 *cur++ = ( data_length >> 8 ) & 0xff;
5601 *cur++ = ( data_length >> 0 ) & 0xff;
5602 memset( cur, 0, data_length );
5603 cur += data_length;
5604 *cur++ = pms[0];
5605 *cur++ = pms[1];
5606 memcpy( cur, data, data_length );
5607 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005608
Janos Follath40e13932019-06-26 13:22:29 +01005609 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005610
5611 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5612 return( status );
5613}
5614
Janos Follath63028dd2019-06-13 09:15:47 +01005615static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5616 const uint8_t *data,
5617 size_t data_length )
5618{
5619 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5620 return( PSA_ERROR_BAD_STATE );
5621
Janos Follathd6dce9f2019-07-04 09:11:38 +01005622 if( data_length != 0 )
5623 {
5624 prf->label = mbedtls_calloc( 1, data_length );
5625 if( prf->label == NULL )
5626 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005627
Janos Follathd6dce9f2019-07-04 09:11:38 +01005628 memcpy( prf->label, data, data_length );
5629 prf->label_length = data_length;
5630 }
Janos Follath63028dd2019-06-13 09:15:47 +01005631
5632 prf->state = TLS12_PRF_STATE_LABEL_SET;
5633
5634 return( PSA_SUCCESS );
5635}
5636
Janos Follathb03233e2019-06-11 15:30:30 +01005637static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5638 psa_algorithm_t hash_alg,
5639 psa_key_derivation_step_t step,
5640 const uint8_t *data,
5641 size_t data_length )
5642{
Janos Follathb03233e2019-06-11 15:30:30 +01005643 switch( step )
5644 {
Janos Follathf08e2652019-06-13 09:05:41 +01005645 case PSA_KEY_DERIVATION_INPUT_SEED:
5646 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005647 case PSA_KEY_DERIVATION_INPUT_SECRET:
5648 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005649 case PSA_KEY_DERIVATION_INPUT_LABEL:
5650 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005651 default:
5652 return( PSA_ERROR_INVALID_ARGUMENT );
5653 }
5654}
Janos Follath6660f0e2019-06-17 08:44:03 +01005655
5656static psa_status_t psa_tls12_prf_psk_to_ms_input(
5657 psa_tls12_prf_key_derivation_t *prf,
5658 psa_algorithm_t hash_alg,
5659 psa_key_derivation_step_t step,
5660 const uint8_t *data,
5661 size_t data_length )
5662{
5663 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005664 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005665 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5666 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005667 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005668
5669 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5670}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005671#endif /* MBEDTLS_MD_C */
5672
Gilles Peskineb8965192019-09-24 16:21:10 +02005673/** Check whether the given key type is acceptable for the given
5674 * input step of a key derivation.
5675 *
5676 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5677 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5678 * Both secret and non-secret inputs can alternatively have the type
5679 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5680 * that the input was passed as a buffer rather than via a key object.
5681 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005682static int psa_key_derivation_check_input_type(
5683 psa_key_derivation_step_t step,
5684 psa_key_type_t key_type )
5685{
5686 switch( step )
5687 {
5688 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005689 if( key_type == PSA_KEY_TYPE_DERIVE )
5690 return( PSA_SUCCESS );
5691 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005692 return( PSA_SUCCESS );
5693 break;
5694 case PSA_KEY_DERIVATION_INPUT_LABEL:
5695 case PSA_KEY_DERIVATION_INPUT_SALT:
5696 case PSA_KEY_DERIVATION_INPUT_INFO:
5697 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005698 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5699 return( PSA_SUCCESS );
5700 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005701 return( PSA_SUCCESS );
5702 break;
5703 }
5704 return( PSA_ERROR_INVALID_ARGUMENT );
5705}
5706
Janos Follathb80a94e2019-06-12 15:54:46 +01005707static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005708 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005709 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005710 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005711 const uint8_t *data,
5712 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005713{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005714 psa_status_t status;
5715 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5716
5717 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005718 if( status != PSA_SUCCESS )
5719 goto exit;
5720
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005721#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005722 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005723 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005724 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005725 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005726 step, data, data_length );
5727 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005728 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005729 {
Janos Follathb03233e2019-06-11 15:30:30 +01005730 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5731 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5732 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005733 }
5734 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5735 {
5736 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5737 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5738 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005739 }
5740 else
5741#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005742 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005743 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005744 return( PSA_ERROR_BAD_STATE );
5745 }
5746
Gilles Peskine224b0d62019-09-23 18:13:17 +02005747exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005748 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005749 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005750 return( status );
5751}
5752
Janos Follath51f4a0f2019-06-14 11:35:55 +01005753psa_status_t psa_key_derivation_input_bytes(
5754 psa_key_derivation_operation_t *operation,
5755 psa_key_derivation_step_t step,
5756 const uint8_t *data,
5757 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005758{
Gilles Peskineb8965192019-09-24 16:21:10 +02005759 return( psa_key_derivation_input_internal( operation, step,
5760 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005761 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005762}
5763
Janos Follath51f4a0f2019-06-14 11:35:55 +01005764psa_status_t psa_key_derivation_input_key(
5765 psa_key_derivation_operation_t *operation,
5766 psa_key_derivation_step_t step,
5767 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005768{
5769 psa_key_slot_t *slot;
5770 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005771
Gilles Peskine28f8f302019-07-24 13:30:31 +02005772 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005773 PSA_KEY_USAGE_DERIVE,
5774 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005775 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005776 {
5777 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005778 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005779 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005780
5781 /* Passing a key object as a SECRET input unlocks the permission
5782 * to output to a key object. */
5783 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5784 operation->can_output_key = 1;
5785
Janos Follathb80a94e2019-06-12 15:54:46 +01005786 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005787 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005788 slot->data.key.data,
5789 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005790}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005791
5792
5793
5794/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005795/* Key agreement */
5796/****************************************************************/
5797
Gilles Peskinea05219c2018-11-16 16:02:56 +01005798#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005799static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5800 size_t peer_key_length,
5801 const mbedtls_ecp_keypair *our_key,
5802 uint8_t *shared_secret,
5803 size_t shared_secret_size,
5804 size_t *shared_secret_length )
5805{
Steven Cooremand4867872020-08-05 16:31:39 +02005806 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005807 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005808 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005809 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005810 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005811 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005812
Steven Cooreman4fed4552020-08-03 14:46:03 +02005813 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5814 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005815 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005816 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005817 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005818 goto exit;
5819
Jaeden Amero97271b32019-01-10 19:38:51 +00005820 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005821 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005822 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005823 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005824 status = mbedtls_to_psa_error(
5825 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5826 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005827 goto exit;
5828
Jaeden Amero97271b32019-01-10 19:38:51 +00005829 status = mbedtls_to_psa_error(
5830 mbedtls_ecdh_calc_secret( &ecdh,
5831 shared_secret_length,
5832 shared_secret, shared_secret_size,
5833 mbedtls_ctr_drbg_random,
5834 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005835 if( status != PSA_SUCCESS )
5836 goto exit;
5837 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5838 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005839
5840exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005841 if( status != PSA_SUCCESS )
5842 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005843 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005844 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005845 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005846
Jaeden Amero97271b32019-01-10 19:38:51 +00005847 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005848}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005849#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005850
Gilles Peskine01d718c2018-09-18 12:01:02 +02005851#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5852
Gilles Peskine0216fe12019-04-11 21:23:21 +02005853static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5854 psa_key_slot_t *private_key,
5855 const uint8_t *peer_key,
5856 size_t peer_key_length,
5857 uint8_t *shared_secret,
5858 size_t shared_secret_size,
5859 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005860{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005861 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005862 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005863#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005864 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005865 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005866 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005867 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005868 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005869 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005870 private_key->data.key.data,
5871 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005872 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005873 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005874 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005875 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005876 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005877 shared_secret, shared_secret_size,
5878 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005879 mbedtls_ecp_keypair_free( ecp );
5880 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005881 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005882#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005883 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005884 (void) private_key;
5885 (void) peer_key;
5886 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005887 (void) shared_secret;
5888 (void) shared_secret_size;
5889 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005890 return( PSA_ERROR_NOT_SUPPORTED );
5891 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005892}
5893
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005894/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005895 * to potentially free embedded data structures and wipe confidential data.
5896 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005897static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005898 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005899 psa_key_slot_t *private_key,
5900 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005901 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005902{
5903 psa_status_t status;
5904 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5905 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005906 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005907
5908 /* Step 1: run the secret agreement algorithm to generate the shared
5909 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005910 status = psa_key_agreement_raw_internal( ka_alg,
5911 private_key,
5912 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005913 shared_secret,
5914 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005915 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005916 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005917 goto exit;
5918
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005919 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005920 * the shared secret. A shared secret is permitted wherever a key
5921 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005922 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005923 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005924 shared_secret,
5925 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005926exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005927 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005928 return( status );
5929}
5930
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005931psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005932 psa_key_derivation_step_t step,
5933 psa_key_handle_t private_key,
5934 const uint8_t *peer_key,
5935 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005936{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005937 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005938 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005939 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005940 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005941 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005942 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005943 if( status != PSA_SUCCESS )
5944 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005945 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005946 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005947 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005948 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005949 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005950 else
5951 {
5952 /* If a private key has been added as SECRET, we allow the derived
5953 * key material to be used as a key in PSA Crypto. */
5954 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5955 operation->can_output_key = 1;
5956 }
Gilles Peskine346797d2018-11-16 16:05:06 +01005957 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005958}
5959
Gilles Peskinebe697d82019-05-16 18:00:41 +02005960psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5961 psa_key_handle_t private_key,
5962 const uint8_t *peer_key,
5963 size_t peer_key_length,
5964 uint8_t *output,
5965 size_t output_size,
5966 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005967{
5968 psa_key_slot_t *slot;
5969 psa_status_t status;
5970
5971 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5972 {
5973 status = PSA_ERROR_INVALID_ARGUMENT;
5974 goto exit;
5975 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005976 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005977 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005978 if( status != PSA_SUCCESS )
5979 goto exit;
5980
5981 status = psa_key_agreement_raw_internal( alg, slot,
5982 peer_key, peer_key_length,
5983 output, output_size,
5984 output_length );
5985
5986exit:
5987 if( status != PSA_SUCCESS )
5988 {
5989 /* If an error happens and is not handled properly, the output
5990 * may be used as a key to protect sensitive data. Arrange for such
5991 * a key to be random, which is likely to result in decryption or
5992 * verification errors. This is better than filling the buffer with
5993 * some constant data such as zeros, which would result in the data
5994 * being protected with a reproducible, easily knowable key.
5995 */
5996 psa_generate_random( output, output_size );
5997 *output_length = output_size;
5998 }
5999 return( status );
6000}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006001
6002
6003/****************************************************************/
6004/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006005/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006006
Gilles Peskine4c317f42018-07-12 01:24:09 +02006007psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006008 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006009{
Janos Follath24eed8d2019-11-22 13:21:35 +00006010 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006011 GUARD_MODULE_INITIALIZED;
6012
Gilles Peskinef181eca2019-08-07 13:49:00 +02006013 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6014 {
6015 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6016 output,
6017 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6018 if( ret != 0 )
6019 return( mbedtls_to_psa_error( ret ) );
6020 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6021 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6022 }
6023
Gilles Peskinee59236f2018-01-27 23:32:46 +01006024 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6025 return( mbedtls_to_psa_error( ret ) );
6026}
6027
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006028#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6029#include "mbedtls/entropy_poll.h"
6030
Gilles Peskine7228da22019-07-15 11:06:15 +02006031psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006032 size_t seed_size )
6033{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006034 if( global_data.initialized )
6035 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006036
6037 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6038 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6039 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6040 return( PSA_ERROR_INVALID_ARGUMENT );
6041
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006042 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006043}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006044#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006045
Gilles Peskinee56e8782019-04-26 17:34:02 +02006046#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
6047static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6048 size_t domain_parameters_size,
6049 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006050{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006051 size_t i;
6052 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006053
Gilles Peskinee56e8782019-04-26 17:34:02 +02006054 if( domain_parameters_size == 0 )
6055 {
6056 *exponent = 65537;
6057 return( PSA_SUCCESS );
6058 }
6059
6060 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6061 * support values that fit in a 32-bit integer, which is larger than
6062 * int on just about every platform anyway. */
6063 if( domain_parameters_size > sizeof( acc ) )
6064 return( PSA_ERROR_NOT_SUPPORTED );
6065 for( i = 0; i < domain_parameters_size; i++ )
6066 acc = ( acc << 8 ) | domain_parameters[i];
6067 if( acc > INT_MAX )
6068 return( PSA_ERROR_NOT_SUPPORTED );
6069 *exponent = acc;
6070 return( PSA_SUCCESS );
6071}
6072#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6073
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006074static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006075 psa_key_slot_t *slot, size_t bits,
6076 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006077{
Gilles Peskine8e338702019-07-30 20:06:31 +02006078 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006079
Gilles Peskinee56e8782019-04-26 17:34:02 +02006080 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006081 return( PSA_ERROR_INVALID_ARGUMENT );
6082
Gilles Peskinee59236f2018-01-27 23:32:46 +01006083 if( key_type_is_raw_bytes( type ) )
6084 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006085 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006086
6087 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006088 if( status != PSA_SUCCESS )
6089 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006090
6091 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006092 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6093 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006094 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006095
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006096 status = psa_generate_random( slot->data.key.data,
6097 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006098 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006099 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006100
6101 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006102#if defined(MBEDTLS_DES_C)
6103 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006104 psa_des_set_key_parity( slot->data.key.data,
6105 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006106#endif /* MBEDTLS_DES_C */
6107 }
6108 else
6109
6110#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006111 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006112 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006113 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006115 int exponent;
6116 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006117 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6118 return( PSA_ERROR_NOT_SUPPORTED );
6119 /* Accept only byte-aligned keys, for the same reasons as
6120 * in psa_import_rsa_key(). */
6121 if( bits % 8 != 0 )
6122 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006123 status = psa_read_rsa_exponent( domain_parameters,
6124 domain_parameters_size,
6125 &exponent );
6126 if( status != PSA_SUCCESS )
6127 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006128 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6129 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006130 mbedtls_ctr_drbg_random,
6131 &global_data.ctr_drbg,
6132 (unsigned int) bits,
6133 exponent );
6134 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006135 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006136
6137 /* Make sure to always have an export representation available */
6138 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6139
Steven Cooreman75b74362020-07-28 14:30:13 +02006140 status = psa_allocate_buffer_to_slot( slot, bytes );
6141 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006142 {
6143 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006144 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006145 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006146
6147 status = psa_export_rsa_key( type,
6148 &rsa,
6149 slot->data.key.data,
6150 bytes,
6151 &slot->data.key.bytes );
6152 mbedtls_rsa_free( &rsa );
6153 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006154 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006155 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006156 }
6157 else
6158#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6159
6160#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006161 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006162 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006163 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006164 mbedtls_ecp_group_id grp_id =
6165 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006166 const mbedtls_ecp_curve_info *curve_info =
6167 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006168 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006169 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006170 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006171 return( PSA_ERROR_NOT_SUPPORTED );
6172 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6173 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006174 mbedtls_ecp_keypair_init( &ecp );
6175 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006176 mbedtls_ctr_drbg_random,
6177 &global_data.ctr_drbg );
6178 if( ret != 0 )
6179 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006180 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006181 return( mbedtls_to_psa_error( ret ) );
6182 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006183
6184
6185 /* Make sure to always have an export representation available */
6186 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006187 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6188 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006189 {
6190 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006191 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006192 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006193
6194 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006195 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6196
6197 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006198 if( status != PSA_SUCCESS ) {
6199 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006200 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006201 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006202 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006203 }
6204 else
6205#endif /* MBEDTLS_ECP_C */
Steven Cooreman29149862020-08-05 15:43:42 +02006206 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006207 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006208 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006209
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006210 return( PSA_SUCCESS );
6211}
Darryl Green0c6575a2018-11-07 16:05:30 +00006212
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006213psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006214 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006215{
6216 psa_status_t status;
6217 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006218 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006219
Gilles Peskine0f84d622019-09-12 19:03:13 +02006220 /* Reject any attempt to create a zero-length key so that we don't
6221 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6222 if( psa_get_key_bits( attributes ) == 0 )
6223 return( PSA_ERROR_INVALID_ARGUMENT );
6224
Gilles Peskinedf179142019-07-15 22:02:14 +02006225 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6226 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006227 if( status != PSA_SUCCESS )
6228 goto exit;
6229
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006230 status = psa_driver_wrapper_generate_key( attributes,
6231 slot );
6232 if( status != PSA_ERROR_NOT_SUPPORTED ||
6233 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6234 goto exit;
6235
6236 status = psa_generate_key_internal(
6237 slot, attributes->core.bits,
6238 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006239
6240exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006241 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006242 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006243 if( status != PSA_SUCCESS )
6244 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006245 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006246 *handle = 0;
6247 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006248 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006249}
6250
6251
Gilles Peskinee59236f2018-01-27 23:32:46 +01006252
6253/****************************************************************/
6254/* Module setup */
6255/****************************************************************/
6256
Gilles Peskine5e769522018-11-20 21:59:56 +01006257psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6258 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6259 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6260{
6261 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6262 return( PSA_ERROR_BAD_STATE );
6263 global_data.entropy_init = entropy_init;
6264 global_data.entropy_free = entropy_free;
6265 return( PSA_SUCCESS );
6266}
6267
Gilles Peskinee59236f2018-01-27 23:32:46 +01006268void mbedtls_psa_crypto_free( void )
6269{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006270 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006271 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6272 {
6273 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006274 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006275 }
6276 /* Wipe all remaining data, including configuration.
6277 * In particular, this sets all state indicator to the value
6278 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006279 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006280#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006281 /* Unregister all secure element drivers, so that we restart from
6282 * a pristine state. */
6283 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006284#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006285}
6286
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006287#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6288/** Recover a transaction that was interrupted by a power failure.
6289 *
6290 * This function is called during initialization, before psa_crypto_init()
6291 * returns. If this function returns a failure status, the initialization
6292 * fails.
6293 */
6294static psa_status_t psa_crypto_recover_transaction(
6295 const psa_crypto_transaction_t *transaction )
6296{
6297 switch( transaction->unknown.type )
6298 {
6299 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6300 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006301 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006302 * is implemented.
6303 * https://github.com/ARMmbed/mbed-crypto/issues/218
6304 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006305 default:
6306 /* We found an unsupported transaction in the storage.
6307 * We don't know what state the storage is in. Give up. */
6308 return( PSA_ERROR_STORAGE_FAILURE );
6309 }
6310}
6311#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6312
Gilles Peskinee59236f2018-01-27 23:32:46 +01006313psa_status_t psa_crypto_init( void )
6314{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006315 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006316 const unsigned char drbg_seed[] = "PSA";
6317
Gilles Peskinec6b69072018-11-20 21:42:52 +01006318 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006319 if( global_data.initialized != 0 )
6320 return( PSA_SUCCESS );
6321
Gilles Peskine5e769522018-11-20 21:59:56 +01006322 /* Set default configuration if
6323 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6324 if( global_data.entropy_init == NULL )
6325 global_data.entropy_init = mbedtls_entropy_init;
6326 if( global_data.entropy_free == NULL )
6327 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006328
Gilles Peskinec6b69072018-11-20 21:42:52 +01006329 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006330 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006331#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6332 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6333 /* The PSA entropy injection feature depends on using NV seed as an entropy
6334 * source. Add NV seed as an entropy source for PSA entropy injection. */
6335 mbedtls_entropy_add_source( &global_data.entropy,
6336 mbedtls_nv_seed_poll, NULL,
6337 MBEDTLS_ENTROPY_BLOCK_SIZE,
6338 MBEDTLS_ENTROPY_SOURCE_STRONG );
6339#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006340 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006341 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006342 status = mbedtls_to_psa_error(
6343 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6344 mbedtls_entropy_func,
6345 &global_data.entropy,
6346 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6347 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006348 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006349 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006350
Gilles Peskine66fb1262018-12-10 16:29:04 +01006351 status = psa_initialize_key_slots( );
6352 if( status != PSA_SUCCESS )
6353 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006354
Gilles Peskined9348f22019-10-01 15:22:29 +02006355#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6356 status = psa_init_all_se_drivers( );
6357 if( status != PSA_SUCCESS )
6358 goto exit;
6359#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6360
Gilles Peskine4b734222019-07-24 15:56:31 +02006361#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006362 status = psa_crypto_load_transaction( );
6363 if( status == PSA_SUCCESS )
6364 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006365 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6366 if( status != PSA_SUCCESS )
6367 goto exit;
6368 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006369 }
6370 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6371 {
6372 /* There's no transaction to complete. It's all good. */
6373 status = PSA_SUCCESS;
6374 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006375#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006376
Gilles Peskinec6b69072018-11-20 21:42:52 +01006377 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006378 global_data.initialized = 1;
6379
6380exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006381 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006382 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006383 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006384}
6385
6386#endif /* MBEDTLS_PSA_CRYPTO_C */