blob: 5a5dc8d4ed0afdb88527b5fbeeffc349f8f37107 [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.
742 * So its data length is 2m+1 where n is the key size in bits.
743 */
744 if( ( data_length & 1 ) == 0 )
745 return( PSA_ERROR_INVALID_ARGUMENT );
746 curve_size = data_length / 2;
747
748 /* Montgomery public keys are represented in compressed format, meaning
749 * their curve_size is equal to the amount of input. */
750
751 /* Private keys are represented in uncompressed private random integer
752 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100753 }
754
Steven Cooreman6d839f02020-07-30 11:36:45 +0200755 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200756 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200757 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200758 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200759 mbedtls_ecp_keypair_init( ecp );
760
Gilles Peskine4cd32772019-12-02 20:49:42 +0100761 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200762 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
763 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100764 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200765 {
766 status = PSA_ERROR_INVALID_ARGUMENT;
767 goto exit;
768 }
769
Steven Cooremanacda8342020-07-24 23:09:52 +0200770 status = mbedtls_to_psa_error(
771 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
772 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200773 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200774
Steven Cooreman6d839f02020-07-30 11:36:45 +0200775 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200776 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200777 {
778 /* Load the public value. */
779 status = mbedtls_to_psa_error(
780 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200781 data,
782 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200783 if( status != PSA_SUCCESS )
784 goto exit;
785
786 /* Check that the point is on the curve. */
787 status = mbedtls_to_psa_error(
788 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
789 if( status != PSA_SUCCESS )
790 goto exit;
791 }
792 else
793 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200794 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200795 status = mbedtls_to_psa_error(
796 mbedtls_ecp_read_key( ecp->grp.id,
797 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200798 data,
799 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200800 if( status != PSA_SUCCESS )
801 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200802 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200803
804 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200805exit:
806 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200807 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200808 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200809 mbedtls_free( ecp );
810 }
811
Steven Cooreman29149862020-08-05 15:43:42 +0200812 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100813}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000814
Steven Cooreman4fed4552020-08-03 14:46:03 +0200815/** Export an ECP key to export representation
816 *
817 * \param[in] type The type of key (public/private) to export
818 * \param[in] ecp The internal ECP representation from which to export
819 * \param[out] data The buffer to export to
820 * \param[in] data_size The length of the buffer to export to
821 * \param[out] data_length The amount of bytes written to \p data
822 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200823static psa_status_t psa_export_ecp_key( psa_key_type_t type,
824 mbedtls_ecp_keypair *ecp,
825 uint8_t *data,
826 size_t data_size,
827 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200828{
Steven Cooremanacda8342020-07-24 23:09:52 +0200829 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000830
Steven Cooremanacda8342020-07-24 23:09:52 +0200831 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200832 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200833 /* Check whether the public part is loaded */
834 if( mbedtls_ecp_is_zero( &ecp->Q ) )
835 {
836 /* Calculate the public key */
837 status = mbedtls_to_psa_error(
838 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
839 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
840 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200841 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200842 }
843
Steven Cooreman4fed4552020-08-03 14:46:03 +0200844 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200845 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
846 MBEDTLS_ECP_PF_UNCOMPRESSED,
847 data_length,
848 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200849 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200850 if( status != PSA_SUCCESS )
851 memset( data, 0, data_size );
852
Steven Cooreman29149862020-08-05 15:43:42 +0200853 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200854 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200855 else
856 {
Steven Cooreman29149862020-08-05 15:43:42 +0200857 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200858 return( PSA_ERROR_BUFFER_TOO_SMALL );
859
860 status = mbedtls_to_psa_error(
861 mbedtls_ecp_write_key( ecp,
862 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200863 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200864 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200865 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200866 else
867 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200868
869 return( status );
870 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200871}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200872
Steven Cooreman4fed4552020-08-03 14:46:03 +0200873/** Import an ECP key from import representation to a slot
874 *
875 * \param[in,out] slot The slot where to store the export representation to
876 * \param[in] data The buffer containing the import representation
877 * \param[in] data_length The amount of bytes in \p data
878 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200879static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
880 const uint8_t *data,
881 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100882{
Steven Cooremanacda8342020-07-24 23:09:52 +0200883 psa_status_t status;
884 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200885 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100886
Steven Cooremanacda8342020-07-24 23:09:52 +0200887 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200888 status = psa_load_ecp_representation( slot->attr.type,
889 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200890 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200891 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100892 if( status != PSA_SUCCESS )
893 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100894
Steven Cooremanacda8342020-07-24 23:09:52 +0200895 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200896 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200897 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200898 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200899
Steven Cooremanacda8342020-07-24 23:09:52 +0200900 /* Re-export the data to PSA export format. There is currently no support
901 * for other input formats then the export format, so this is a 1-1
902 * copy operation. */
903 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200904 if( output == NULL )
905 {
906 status = PSA_ERROR_INSUFFICIENT_MEMORY;
907 goto exit;
908 }
909
910 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200911 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200912 output,
913 data_length,
914 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100915exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200916 /* Always free the PK object (will also free contained ECP context) */
917 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200918 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200919
920 /* Free the allocated buffer only on error. */
921 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100922 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200923 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200924 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100925 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200926
927 /* On success, store the allocated export-formatted key. */
928 slot->data.key.data = output;
929 slot->data.key.bytes = data_length;
930
931 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100932}
933#endif /* defined(MBEDTLS_ECP_C) */
934
Gilles Peskineb46bef22019-07-30 21:32:04 +0200935/** Return the size of the key in the given slot, in bits.
936 *
937 * \param[in] slot A key slot.
938 *
939 * \return The key size in bits, read from the metadata in the slot.
940 */
941static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
942{
943 return( slot->attr.bits );
944}
945
Steven Cooreman75b74362020-07-28 14:30:13 +0200946/** Try to allocate a buffer to an empty key slot.
947 *
948 * \param[in,out] slot Key slot to attach buffer to.
949 * \param[in] buffer_length Requested size of the buffer.
950 *
951 * \retval #PSA_SUCCESS
952 * The buffer has been successfully allocated.
953 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
954 * Not enough memory was available for allocation.
955 * \retval #PSA_ERROR_ALREADY_EXISTS
956 * Trying to allocate a buffer to a non-empty key slot.
957 */
958static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
959 size_t buffer_length )
960{
961 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200962 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200963
964 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
965 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200966 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200967
968 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200969 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200970}
971
Gilles Peskine8e338702019-07-30 20:06:31 +0200972/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100973 * previously. This function assumes that the slot does not contain
974 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100975psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
976 const uint8_t *data,
977 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100978{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200979 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200981 /* zero-length keys are never supported. */
982 if( data_length == 0 )
983 return( PSA_ERROR_NOT_SUPPORTED );
984
Gilles Peskine8e338702019-07-30 20:06:31 +0200985 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200987 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200988
Steven Cooreman75b74362020-07-28 14:30:13 +0200989 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
990 if( data_length > SIZE_MAX / 8 )
991 return( PSA_ERROR_NOT_SUPPORTED );
992
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200993 /* Enforce a size limit, and in particular ensure that the bit
994 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200995 if( bit_size > PSA_MAX_KEY_BITS )
996 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200997
998 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200999 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001000 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001001
1002 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02001003 status = psa_allocate_buffer_to_slot( slot, data_length );
1004 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001005 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001006
1007 /* copy key into allocated buffer */
Steven Cooreman29149862020-08-05 15:43:42 +02001008 memcpy( slot->data.key.data, data, data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001009
1010 /* Write the actual key size to the slot.
1011 * psa_start_key_creation() wrote the size declared by the
1012 * caller, which may be 0 (meaning unspecified) or wrong. */
1013 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001015 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1016 {
Gilles Peskinef76aa772018-10-29 19:24:33 +01001017#if defined(MBEDTLS_ECP_C)
Steven Cooreman19fd5742020-07-24 23:31:01 +02001018 status = psa_import_ecp_key( slot,
1019 data, data_length );
1020#else
1021 /* No drivers have been implemented yet, so without mbed TLS backing
1022 * there's no way to do ECP with the current library. */
1023 return( PSA_ERROR_NOT_SUPPORTED );
1024#endif /* defined(MBEDTLS_ECP_C) */
1025 }
1026 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +01001027 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001028#if defined(MBEDTLS_RSA_C)
1029 status = psa_import_rsa_key( slot,
1030 data, data_length );
1031#else
1032 /* No drivers have been implemented yet, so without mbed TLS backing
1033 * there's no way to do RSA with the current library. */
1034 status = PSA_ERROR_NOT_SUPPORTED;
1035#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036 }
1037 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001038 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001039 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001041 }
Darryl Green940d72c2018-07-13 13:18:51 +01001042
Darryl Green06fd18d2018-07-16 11:21:11 +01001043 return( status );
1044}
1045
Gilles Peskinef603c712019-01-19 13:40:11 +01001046/** Calculate the intersection of two algorithm usage policies.
1047 *
1048 * Return 0 (which allows no operation) on incompatibility.
1049 */
1050static psa_algorithm_t psa_key_policy_algorithm_intersection(
1051 psa_algorithm_t alg1,
1052 psa_algorithm_t alg2 )
1053{
Gilles Peskine549ea862019-05-22 11:45:59 +02001054 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001055 if( alg1 == alg2 )
1056 return( alg1 );
1057 /* If the policies are from the same hash-and-sign family, check
1058 * if one is a wildcard. If so the other has the specific algorithm. */
1059 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1060 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1061 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1062 {
1063 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1064 return( alg2 );
1065 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1066 return( alg1 );
1067 }
1068 /* If the policies are incompatible, allow nothing. */
1069 return( 0 );
1070}
1071
Gilles Peskined6f371b2019-05-10 19:33:38 +02001072static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1073 psa_algorithm_t requested_alg )
1074{
Gilles Peskine549ea862019-05-22 11:45:59 +02001075 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001076 if( requested_alg == policy_alg )
1077 return( 1 );
1078 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001079 * and requested_alg is the same hash-and-sign family with any hash,
1080 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001081 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1082 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1083 {
1084 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1085 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1086 }
Steven Cooremance48e852020-10-05 16:02:45 +02001087 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001088 * a key derivation with that key agreement should also be allowed. This
1089 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001090 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1091 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1092 {
1093 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1094 policy_alg );
1095 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001096 /* If it isn't permitted, it's forbidden. */
1097 return( 0 );
1098}
1099
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001100/** Test whether a policy permits an algorithm.
1101 *
1102 * The caller must test usage flags separately.
1103 */
1104static int psa_key_policy_permits( const psa_key_policy_t *policy,
1105 psa_algorithm_t alg )
1106{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001107 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1108 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001109}
1110
Gilles Peskinef603c712019-01-19 13:40:11 +01001111/** Restrict a key policy based on a constraint.
1112 *
1113 * \param[in,out] policy The policy to restrict.
1114 * \param[in] constraint The policy constraint to apply.
1115 *
1116 * \retval #PSA_SUCCESS
1117 * \c *policy contains the intersection of the original value of
1118 * \c *policy and \c *constraint.
1119 * \retval #PSA_ERROR_INVALID_ARGUMENT
1120 * \c *policy and \c *constraint are incompatible.
1121 * \c *policy is unchanged.
1122 */
1123static psa_status_t psa_restrict_key_policy(
1124 psa_key_policy_t *policy,
1125 const psa_key_policy_t *constraint )
1126{
1127 psa_algorithm_t intersection_alg =
1128 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001129 psa_algorithm_t intersection_alg2 =
1130 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001131 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1132 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001133 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1134 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001135 policy->usage &= constraint->usage;
1136 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001137 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001138 return( PSA_SUCCESS );
1139}
1140
Darryl Green06fd18d2018-07-16 11:21:11 +01001141/** Retrieve a slot which must contain a key. The key must have allow all the
1142 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1143 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001144static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001145 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001146 psa_key_usage_t usage,
1147 psa_algorithm_t alg )
1148{
1149 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001150 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001151
1152 *p_slot = NULL;
1153
Gilles Peskinec5487a82018-12-03 18:08:14 +01001154 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001155 if( status != PSA_SUCCESS )
1156 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001157
1158 /* Enforce that usage policy for the key slot contains all the flags
1159 * required by the usage parameter. There is one exception: public
1160 * keys can always be exported, so we treat public key objects as
1161 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001162 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001163 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001164 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001165 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001166
1167 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001168 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001169 return( PSA_ERROR_NOT_PERMITTED );
1170
1171 *p_slot = slot;
1172 return( PSA_SUCCESS );
1173}
Darryl Green940d72c2018-07-13 13:18:51 +01001174
Gilles Peskine28f8f302019-07-24 13:30:31 +02001175/** Retrieve a slot which must contain a transparent key.
1176 *
1177 * A transparent key is a key for which the key material is directly
1178 * available, as opposed to a key in a secure element.
1179 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001180 * This is a temporary function to use instead of psa_get_key_from_slot()
1181 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001182 */
1183#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1184static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1185 psa_key_slot_t **p_slot,
1186 psa_key_usage_t usage,
1187 psa_algorithm_t alg )
1188{
1189 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1190 if( status != PSA_SUCCESS )
1191 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001192 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001193 {
1194 *p_slot = NULL;
1195 return( PSA_ERROR_NOT_SUPPORTED );
1196 }
1197 return( PSA_SUCCESS );
1198}
1199#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1200/* With no secure element support, all keys are transparent. */
1201#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1202 psa_get_key_from_slot( handle, p_slot, usage, alg )
1203#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1204
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001205/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001206static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001207{
Gilles Peskine73167e12019-07-12 23:44:37 +02001208#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1209 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001210 {
1211 /* No key material to clean. */
1212 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001213 else
1214#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001215 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001216 /* Data pointer will always be either a valid pointer or NULL in an
1217 * initialized slot, so we can just free it. */
1218 if( slot->data.key.data != NULL )
1219 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001220 mbedtls_free( slot->data.key.data );
1221 slot->data.key.data = NULL;
1222 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001223 }
Darryl Green40225ba2018-11-15 14:48:15 +00001224
1225 return( PSA_SUCCESS );
1226}
1227
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001228/** Completely wipe a slot in memory, including its policy.
1229 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001230psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001231{
1232 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001233 /* Multipart operations may still be using the key. This is safe
1234 * because all multipart operation objects are independent from
1235 * the key slot: if they need to access the key after the setup
1236 * phase, they have a copy of the key. Note that this means that
1237 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001238 /* At this point, key material and other type-specific content has
1239 * been wiped. Clear remaining metadata. We can call memset and not
1240 * zeroize because the metadata is not particularly sensitive. */
1241 memset( slot, 0, sizeof( *slot ) );
1242 return( status );
1243}
1244
Gilles Peskinec5487a82018-12-03 18:08:14 +01001245psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001246{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001247 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001248 psa_status_t status; /* status of the last operation */
1249 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001250#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1251 psa_se_drv_table_entry_t *driver;
1252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001253
Gilles Peskine1841cf42019-10-08 15:48:25 +02001254 if( handle == 0 )
1255 return( PSA_SUCCESS );
1256
Gilles Peskinec5487a82018-12-03 18:08:14 +01001257 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001258 if( status != PSA_SUCCESS )
1259 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001260
1261#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001262 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001263 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001264 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001265 /* For a key in a secure element, we need to do three things:
1266 * remove the key file in internal storage, destroy the
1267 * key inside the secure element, and update the driver's
1268 * persistent data. Start a transaction that will encompass these
1269 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001270 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001271 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001272 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001273 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001274 status = psa_crypto_save_transaction( );
1275 if( status != PSA_SUCCESS )
1276 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001277 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001278 /* We should still try to destroy the key in the secure
1279 * element and the key metadata in storage. This is especially
1280 * important if the error is that the storage is full.
1281 * But how to do it exactly without risking an inconsistent
1282 * state after a reset?
1283 * https://github.com/ARMmbed/mbed-crypto/issues/215
1284 */
1285 overall_status = status;
1286 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001287 }
1288
Gilles Peskine354f7672019-07-12 23:46:38 +02001289 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001290 if( overall_status == PSA_SUCCESS )
1291 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001292 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001293#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1294
Darryl Greend49a4992018-06-18 17:27:26 +01001295#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001296 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001297 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001298 status = psa_destroy_persistent_key( slot->attr.id );
1299 if( overall_status == PSA_SUCCESS )
1300 overall_status = status;
1301
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001302 /* TODO: other slots may have a copy of the same key. We should
1303 * invalidate them.
1304 * https://github.com/ARMmbed/mbed-crypto/issues/214
1305 */
Darryl Greend49a4992018-06-18 17:27:26 +01001306 }
1307#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001308
Gilles Peskinefc762652019-07-22 19:30:34 +02001309#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1310 if( driver != NULL )
1311 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001312 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001313 if( overall_status == PSA_SUCCESS )
1314 overall_status = status;
1315 status = psa_crypto_stop_transaction( );
1316 if( overall_status == PSA_SUCCESS )
1317 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001318 }
1319#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1320
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001321#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1322exit:
1323#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001324 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001325 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1326 if( overall_status == PSA_SUCCESS )
1327 overall_status = status;
1328 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001329}
1330
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001331void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001332{
Gilles Peskineb699f072019-04-26 16:06:02 +02001333 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001334 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001335}
1336
Gilles Peskineb699f072019-04-26 16:06:02 +02001337psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1338 psa_key_type_t type,
1339 const uint8_t *data,
1340 size_t data_length )
1341{
1342 uint8_t *copy = NULL;
1343
1344 if( data_length != 0 )
1345 {
1346 copy = mbedtls_calloc( 1, data_length );
1347 if( copy == NULL )
1348 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1349 memcpy( copy, data, data_length );
1350 }
1351 /* After this point, this function is guaranteed to succeed, so it
1352 * can start modifying `*attributes`. */
1353
1354 if( attributes->domain_parameters != NULL )
1355 {
1356 mbedtls_free( attributes->domain_parameters );
1357 attributes->domain_parameters = NULL;
1358 attributes->domain_parameters_size = 0;
1359 }
1360
1361 attributes->domain_parameters = copy;
1362 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001363 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001364 return( PSA_SUCCESS );
1365}
1366
1367psa_status_t psa_get_key_domain_parameters(
1368 const psa_key_attributes_t *attributes,
1369 uint8_t *data, size_t data_size, size_t *data_length )
1370{
1371 if( attributes->domain_parameters_size > data_size )
1372 return( PSA_ERROR_BUFFER_TOO_SMALL );
1373 *data_length = attributes->domain_parameters_size;
1374 if( attributes->domain_parameters_size != 0 )
1375 memcpy( data, attributes->domain_parameters,
1376 attributes->domain_parameters_size );
1377 return( PSA_SUCCESS );
1378}
1379
1380#if defined(MBEDTLS_RSA_C)
1381static psa_status_t psa_get_rsa_public_exponent(
1382 const mbedtls_rsa_context *rsa,
1383 psa_key_attributes_t *attributes )
1384{
1385 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001386 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001387 uint8_t *buffer = NULL;
1388 size_t buflen;
1389 mbedtls_mpi_init( &mpi );
1390
1391 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1392 if( ret != 0 )
1393 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001394 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1395 {
1396 /* It's the default value, which is reported as an empty string,
1397 * so there's nothing to do. */
1398 goto exit;
1399 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001400
1401 buflen = mbedtls_mpi_size( &mpi );
1402 buffer = mbedtls_calloc( 1, buflen );
1403 if( buffer == NULL )
1404 {
1405 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1406 goto exit;
1407 }
1408 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1409 if( ret != 0 )
1410 goto exit;
1411 attributes->domain_parameters = buffer;
1412 attributes->domain_parameters_size = buflen;
1413
1414exit:
1415 mbedtls_mpi_free( &mpi );
1416 if( ret != 0 )
1417 mbedtls_free( buffer );
1418 return( mbedtls_to_psa_error( ret ) );
1419}
1420#endif /* MBEDTLS_RSA_C */
1421
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001422/** Retrieve all the publicly-accessible attributes of a key.
1423 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001424psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1425 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001426{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001427 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001428 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001429
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001430 psa_reset_key_attributes( attributes );
1431
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001432 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001433 if( status != PSA_SUCCESS )
1434 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001435
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001436 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001437 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1438 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1439
1440#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1441 if( psa_key_slot_is_external( slot ) )
1442 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1443#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001444
Gilles Peskine8e338702019-07-30 20:06:31 +02001445 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001446 {
1447#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001448 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001449 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001450#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001451 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001452 * is not yet implemented.
1453 * https://github.com/ARMmbed/mbed-crypto/issues/216
1454 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001455 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001456 break;
1457#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001458 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001459 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001460
Steven Cooreman4fed4552020-08-03 14:46:03 +02001461 status = psa_load_rsa_representation( slot->attr.type,
1462 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001463 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001464 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001465 if( status != PSA_SUCCESS )
1466 break;
1467
Steven Cooremana2371e52020-07-28 14:30:39 +02001468 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001469 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001470 mbedtls_rsa_free( rsa );
1471 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001472 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001473 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001474#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001475 default:
1476 /* Nothing else to do. */
1477 break;
1478 }
1479
1480 if( status != PSA_SUCCESS )
1481 psa_reset_key_attributes( attributes );
1482 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001483}
1484
Gilles Peskinec8000c02019-08-02 20:15:51 +02001485#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1486psa_status_t psa_get_key_slot_number(
1487 const psa_key_attributes_t *attributes,
1488 psa_key_slot_number_t *slot_number )
1489{
1490 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1491 {
1492 *slot_number = attributes->slot_number;
1493 return( PSA_SUCCESS );
1494 }
1495 else
1496 return( PSA_ERROR_INVALID_ARGUMENT );
1497}
1498#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1499
Steven Cooremana01795d2020-07-24 22:48:15 +02001500static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1501 uint8_t *data,
1502 size_t data_size,
1503 size_t *data_length )
1504{
1505 if( slot->data.key.bytes > data_size )
1506 return( PSA_ERROR_BUFFER_TOO_SMALL );
1507 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1508 memset( data + slot->data.key.bytes, 0,
1509 data_size - slot->data.key.bytes );
1510 *data_length = slot->data.key.bytes;
1511 return( PSA_SUCCESS );
1512}
1513
Gilles Peskinef603c712019-01-19 13:40:11 +01001514static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1515 uint8_t *data,
1516 size_t data_size,
1517 size_t *data_length,
1518 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001519{
Gilles Peskine5d309672019-07-12 23:47:28 +02001520#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1521 const psa_drv_se_t *drv;
1522 psa_drv_se_context_t *drv_context;
1523#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1524
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001525 *data_length = 0;
1526
Gilles Peskine8e338702019-07-30 20:06:31 +02001527 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001528 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001529
Gilles Peskinef9168942019-09-12 19:20:29 +02001530 /* Reject a zero-length output buffer now, since this can never be a
1531 * valid key representation. This way we know that data must be a valid
1532 * pointer and we can do things like memset(data, ..., data_size). */
1533 if( data_size == 0 )
1534 return( PSA_ERROR_BUFFER_TOO_SMALL );
1535
Gilles Peskine5d309672019-07-12 23:47:28 +02001536#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001537 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001538 {
1539 psa_drv_se_export_key_t method;
1540 if( drv->key_management == NULL )
1541 return( PSA_ERROR_NOT_SUPPORTED );
1542 method = ( export_public_key ?
1543 drv->key_management->p_export_public :
1544 drv->key_management->p_export );
1545 if( method == NULL )
1546 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001547 return( method( drv_context,
1548 slot->data.se.slot_number,
1549 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001550 }
1551#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1552
Gilles Peskine8e338702019-07-30 20:06:31 +02001553 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001554 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001555 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001556 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001557 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1558 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001559 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001560 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001561 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001562 /* Exporting public -> public */
1563 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1564 }
1565 else if( !export_public_key )
1566 {
1567 /* Exporting private -> private */
1568 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1569 }
1570 /* Need to export the public part of a private key,
1571 * so conversion is needed */
1572 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1573 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001574#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001575 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001576 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001577 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001578 slot->data.key.data,
1579 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001580 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001581 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001582 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001583
Steven Cooreman560c28a2020-07-24 23:20:24 +02001584 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001585 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001586 data,
1587 data_size,
1588 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001589
Steven Cooremana2371e52020-07-28 14:30:39 +02001590 mbedtls_rsa_free( rsa );
1591 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001592
Steven Cooreman560c28a2020-07-24 23:20:24 +02001593 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001594#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001595 /* We don't know how to convert a private RSA key to public. */
1596 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001597#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001598 }
1599 else
Moran Pekera998bc62018-04-16 18:16:20 +03001600 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001601#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001602 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001603 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001604 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001605 slot->data.key.data,
1606 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001607 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001608 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001609 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001610
1611 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1612 PSA_KEY_TYPE_ECC_GET_FAMILY(
1613 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001614 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001615 data,
1616 data_size,
1617 data_length );
1618
Steven Cooremana2371e52020-07-28 14:30:39 +02001619 mbedtls_ecp_keypair_free( ecp );
1620 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001621 return( status );
1622#else
1623 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001624 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001625#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001626 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001627 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001628 else
1629 {
1630 /* This shouldn't happen in the reference implementation, but
1631 it is valid for a special-purpose implementation to omit
1632 support for exporting certain key types. */
1633 return( PSA_ERROR_NOT_SUPPORTED );
1634 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001635}
1636
Gilles Peskinec5487a82018-12-03 18:08:14 +01001637psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001638 uint8_t *data,
1639 size_t data_size,
1640 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001641{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001642 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001643 psa_status_t status;
1644
1645 /* Set the key to empty now, so that even when there are errors, we always
1646 * set data_length to a value between 0 and data_size. On error, setting
1647 * the key to empty is a good choice because an empty key representation is
1648 * unlikely to be accepted anywhere. */
1649 *data_length = 0;
1650
1651 /* Export requires the EXPORT flag. There is an exception for public keys,
1652 * which don't require any flag, but psa_get_key_from_slot takes
1653 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001654 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001655 if( status != PSA_SUCCESS )
1656 return( status );
1657 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001658 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001659}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001660
Gilles Peskinec5487a82018-12-03 18:08:14 +01001661psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001662 uint8_t *data,
1663 size_t data_size,
1664 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001665{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001666 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001667 psa_status_t status;
1668
1669 /* Set the key to empty now, so that even when there are errors, we always
1670 * set data_length to a value between 0 and data_size. On error, setting
1671 * the key to empty is a good choice because an empty key representation is
1672 * unlikely to be accepted anywhere. */
1673 *data_length = 0;
1674
1675 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001676 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001677 if( status != PSA_SUCCESS )
1678 return( status );
1679 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001680 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001681}
1682
Gilles Peskine91e8c332019-08-02 19:19:39 +02001683#if defined(static_assert)
1684static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1685 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001686static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001687 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001688static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001689 "One or more key attribute flag is listed as both internal-only and external-only" );
1690#endif
1691
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001692/** Validate that a key policy is internally well-formed.
1693 *
1694 * This function only rejects invalid policies. It does not validate the
1695 * consistency of the policy with respect to other attributes of the key
1696 * such as the key type.
1697 */
1698static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001699{
1700 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001701 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001702 PSA_KEY_USAGE_ENCRYPT |
1703 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001704 PSA_KEY_USAGE_SIGN_HASH |
1705 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001706 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1707 return( PSA_ERROR_INVALID_ARGUMENT );
1708
Gilles Peskine4747d192019-04-17 15:05:45 +02001709 return( PSA_SUCCESS );
1710}
1711
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001712/** Validate the internal consistency of key attributes.
1713 *
1714 * This function only rejects invalid attribute values. If does not
1715 * validate the consistency of the attributes with any key data that may
1716 * be involved in the creation of the key.
1717 *
1718 * Call this function early in the key creation process.
1719 *
1720 * \param[in] attributes Key attributes for the new key.
1721 * \param[out] p_drv On any return, the driver for the key, if any.
1722 * NULL for a transparent key.
1723 *
1724 */
1725static psa_status_t psa_validate_key_attributes(
1726 const psa_key_attributes_t *attributes,
1727 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001728{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001729 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001730
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001731 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1732 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001733 if( status != PSA_SUCCESS )
1734 return( status );
1735
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001736 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1737 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001738 if( status != PSA_SUCCESS )
1739 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001740
1741 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001742 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001743 return( status );
1744
1745 /* Refuse to create overly large keys.
1746 * Note that this doesn't trigger on import if the attributes don't
1747 * explicitly specify a size (so psa_get_key_bits returns 0), so
1748 * psa_import_key() needs its own checks. */
1749 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1750 return( PSA_ERROR_NOT_SUPPORTED );
1751
Gilles Peskine91e8c332019-08-02 19:19:39 +02001752 /* Reject invalid flags. These should not be reachable through the API. */
1753 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1754 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1755 return( PSA_ERROR_INVALID_ARGUMENT );
1756
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001757 return( PSA_SUCCESS );
1758}
1759
Gilles Peskine4747d192019-04-17 15:05:45 +02001760/** Prepare a key slot to receive key material.
1761 *
1762 * This function allocates a key slot and sets its metadata.
1763 *
1764 * If this function fails, call psa_fail_key_creation().
1765 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001766 * This function is intended to be used as follows:
1767 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1768 * it with the specified attributes, and assign it a handle.
1769 * -# Populate the slot with the key material.
1770 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1771 * In case of failure at any step, stop the sequence and call
1772 * psa_fail_key_creation().
1773 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001774 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001775 * \param[in] attributes Key attributes for the new key.
1776 * \param[out] handle On success, a handle for the allocated slot.
1777 * \param[out] p_slot On success, a pointer to the prepared slot.
1778 * \param[out] p_drv On any return, the driver for the key, if any.
1779 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001780 *
1781 * \retval #PSA_SUCCESS
1782 * The key slot is ready to receive key material.
1783 * \return If this function fails, the key slot is an invalid state.
1784 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001785 */
1786static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001787 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001788 const psa_key_attributes_t *attributes,
1789 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001790 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001791 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001792{
1793 psa_status_t status;
1794 psa_key_slot_t *slot;
1795
Gilles Peskinedf179142019-07-15 22:02:14 +02001796 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001797 *p_drv = NULL;
1798
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001799 status = psa_validate_key_attributes( attributes, p_drv );
1800 if( status != PSA_SUCCESS )
1801 return( status );
1802
Gilles Peskineedbed562019-08-07 18:19:59 +02001803 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001804 if( status != PSA_SUCCESS )
1805 return( status );
1806 slot = *p_slot;
1807
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001808 /* We're storing the declared bit-size of the key. It's up to each
1809 * creation mechanism to verify that this information is correct.
1810 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001811 * an input (generate, device) but not for those where the bit-size
1812 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001813
1814 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001815
Gilles Peskine91e8c332019-08-02 19:19:39 +02001816 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001817 * external-only flags, query `attributes`. Thanks to the check
1818 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001819 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001820 * may have set. */
1821 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001822
Gilles Peskinecbaff462019-07-12 23:46:04 +02001823#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001824 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001825 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001826 * create the key file in internal storage, create the
1827 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001828 * persistent data. This is done by starting a transaction that will
1829 * encompass these three actions.
1830 * For registering a volatile key, we just need to find an appropriate
1831 * slot number inside the SE. Since the key is designated volatile, creating
1832 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001833 /* The first thing to do is to find a slot number for the new key.
1834 * We save the slot number in persistent storage as part of the
1835 * transaction data. It will be needed to recover if the power
1836 * fails during the key creation process, to clean up on the secure
1837 * element side after restarting. Obtaining a slot number from the
1838 * secure element driver updates its persistent state, but we do not yet
1839 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001840 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001841 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001842 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001843 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001844 &slot->data.se.slot_number );
1845 if( status != PSA_SUCCESS )
1846 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001847
1848 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001849 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001850 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1851 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1852 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1853 psa_crypto_transaction.key.id = slot->attr.id;
1854 status = psa_crypto_save_transaction( );
1855 if( status != PSA_SUCCESS )
1856 {
1857 (void) psa_crypto_stop_transaction( );
1858 return( status );
1859 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001860 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001861 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001862
1863 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1864 {
1865 /* Key registration only makes sense with a secure element. */
1866 return( PSA_ERROR_INVALID_ARGUMENT );
1867 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001868#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1869
Darryl Green0c6575a2018-11-07 16:05:30 +00001870 return( status );
1871}
Gilles Peskine4747d192019-04-17 15:05:45 +02001872
1873/** Finalize the creation of a key once its key material has been set.
1874 *
1875 * This entails writing the key to persistent storage.
1876 *
1877 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001878 * See the documentation of psa_start_key_creation() for the intended use
1879 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001880 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001881 * \param[in,out] slot Pointer to the slot with key material.
1882 * \param[in] driver The secure element driver for the key,
1883 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001884 *
1885 * \retval #PSA_SUCCESS
1886 * The key was successfully created. The handle is now valid.
1887 * \return If this function fails, the key slot is an invalid state.
1888 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001889 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001890static psa_status_t psa_finish_key_creation(
1891 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001892 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001893{
1894 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001895 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001896 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001897
1898#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001899 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001900 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001901#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1902 if( driver != NULL )
1903 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001904 psa_se_key_data_storage_t data;
1905#if defined(static_assert)
1906 static_assert( sizeof( slot->data.se.slot_number ) ==
1907 sizeof( data.slot_number ),
1908 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001909#endif
1910 memcpy( &data.slot_number, &slot->data.se.slot_number,
1911 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001912 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001913 (uint8_t*) &data,
1914 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001915 }
1916 else
1917#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1918 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001919 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001920 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001921 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001922 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1923 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001924 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001925 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1926 status = psa_internal_export_key( slot,
1927 buffer, buffer_size, &length,
1928 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001929 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001930 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001931 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001932
Gilles Peskinef9168942019-09-12 19:20:29 +02001933 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001934 mbedtls_free( buffer );
1935 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001936 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001937#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1938
Gilles Peskinecbaff462019-07-12 23:46:04 +02001939#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001940 /* Finish the transaction for a key creation. This does not
1941 * happen when registering an existing key. Detect this case
1942 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001943 * creation of a persistent key in a secure element requires a transaction,
1944 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001945 if( driver != NULL &&
1946 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001947 {
1948 status = psa_save_se_persistent_data( driver );
1949 if( status != PSA_SUCCESS )
1950 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001951 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001952 return( status );
1953 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001954 status = psa_crypto_stop_transaction( );
1955 if( status != PSA_SUCCESS )
1956 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001957 }
1958#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1959
Gilles Peskine4747d192019-04-17 15:05:45 +02001960 return( status );
1961}
1962
1963/** Abort the creation of a key.
1964 *
1965 * You may call this function after calling psa_start_key_creation(),
1966 * or after psa_finish_key_creation() fails. In other circumstances, this
1967 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001968 * See the documentation of psa_start_key_creation() for the intended use
1969 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001970 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001971 * \param[in,out] slot Pointer to the slot with key material.
1972 * \param[in] driver The secure element driver for the key,
1973 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001974 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001975static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001976 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001977{
Gilles Peskine011e4282019-06-26 18:34:38 +02001978 (void) driver;
1979
Gilles Peskine4747d192019-04-17 15:05:45 +02001980 if( slot == NULL )
1981 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001982
1983#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001984 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001985 * element, and the failure happened later (when saving metadata
1986 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001987 * element.
1988 * https://github.com/ARMmbed/mbed-crypto/issues/217
1989 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001990
Gilles Peskined7729582019-08-05 15:55:54 +02001991 /* Abort the ongoing transaction if any (there may not be one if
1992 * the creation process failed before starting one, or if the
1993 * key creation is a registration of a key in a secure element).
1994 * Earlier functions must already have done what it takes to undo any
1995 * partial creation. All that's left is to update the transaction data
1996 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001997 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001998#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1999
Gilles Peskine4747d192019-04-17 15:05:45 +02002000 psa_wipe_key_slot( slot );
2001}
2002
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002003/** Validate optional attributes during key creation.
2004 *
2005 * Some key attributes are optional during key creation. If they are
2006 * specified in the attributes structure, check that they are consistent
2007 * with the data in the slot.
2008 *
2009 * This function should be called near the end of key creation, after
2010 * the slot in memory is fully populated but before saving persistent data.
2011 */
2012static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002013 const psa_key_slot_t *slot,
2014 const psa_key_attributes_t *attributes )
2015{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002016 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002017 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002018 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002019 return( PSA_ERROR_INVALID_ARGUMENT );
2020 }
2021
2022 if( attributes->domain_parameters_size != 0 )
2023 {
2024#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002025 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002026 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002027 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002028 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002029 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002030
Steven Cooreman7f391872020-07-30 14:57:44 +02002031 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002032 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002033 slot->data.key.data,
2034 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002035 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002036 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002037 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002038
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002039 mbedtls_mpi_init( &actual );
2040 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002041 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002042 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002043 mbedtls_rsa_free( rsa );
2044 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002045 if( ret != 0 )
2046 goto rsa_exit;
2047 ret = mbedtls_mpi_read_binary( &required,
2048 attributes->domain_parameters,
2049 attributes->domain_parameters_size );
2050 if( ret != 0 )
2051 goto rsa_exit;
2052 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2053 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2054 rsa_exit:
2055 mbedtls_mpi_free( &actual );
2056 mbedtls_mpi_free( &required );
2057 if( ret != 0)
2058 return( mbedtls_to_psa_error( ret ) );
2059 }
2060 else
2061#endif
2062 {
2063 return( PSA_ERROR_INVALID_ARGUMENT );
2064 }
2065 }
2066
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002067 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002068 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002069 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002070 return( PSA_ERROR_INVALID_ARGUMENT );
2071 }
2072
2073 return( PSA_SUCCESS );
2074}
2075
Gilles Peskine4747d192019-04-17 15:05:45 +02002076psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002077 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002078 size_t data_length,
2079 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002080{
2081 psa_status_t status;
2082 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002083 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002084
Gilles Peskine0f84d622019-09-12 19:03:13 +02002085 /* Reject zero-length symmetric keys (including raw data key objects).
2086 * This also rejects any key which might be encoded as an empty string,
2087 * which is never valid. */
2088 if( data_length == 0 )
2089 return( PSA_ERROR_INVALID_ARGUMENT );
2090
Gilles Peskinedf179142019-07-15 22:02:14 +02002091 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2092 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002093 if( status != PSA_SUCCESS )
2094 goto exit;
2095
Gilles Peskine5d309672019-07-12 23:47:28 +02002096#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2097 if( driver != NULL )
2098 {
2099 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002100 /* The driver should set the number of key bits, however in
2101 * case it doesn't, we initialize bits to an invalid value. */
2102 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002103 if( drv->key_management == NULL ||
2104 drv->key_management->p_import == NULL )
2105 {
2106 status = PSA_ERROR_NOT_SUPPORTED;
2107 goto exit;
2108 }
2109 status = drv->key_management->p_import(
2110 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002111 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002112 &bits );
2113 if( status != PSA_SUCCESS )
2114 goto exit;
2115 if( bits > PSA_MAX_KEY_BITS )
2116 {
2117 status = PSA_ERROR_NOT_SUPPORTED;
2118 goto exit;
2119 }
2120 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002121 }
2122 else
2123#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2124 {
2125 status = psa_import_key_into_slot( slot, data, data_length );
2126 if( status != PSA_SUCCESS )
2127 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002128 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002129 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002130 if( status != PSA_SUCCESS )
2131 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002132
Gilles Peskine011e4282019-06-26 18:34:38 +02002133 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002134exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002135 if( status != PSA_SUCCESS )
2136 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002137 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002138 *handle = 0;
2139 }
2140 return( status );
2141}
2142
Gilles Peskined7729582019-08-05 15:55:54 +02002143#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2144psa_status_t mbedtls_psa_register_se_key(
2145 const psa_key_attributes_t *attributes )
2146{
2147 psa_status_t status;
2148 psa_key_slot_t *slot = NULL;
2149 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002150 psa_key_handle_t handle = 0;
2151
2152 /* Leaving attributes unspecified is not currently supported.
2153 * It could make sense to query the key type and size from the
2154 * secure element, but not all secure elements support this
2155 * and the driver HAL doesn't currently support it. */
2156 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2157 return( PSA_ERROR_NOT_SUPPORTED );
2158 if( psa_get_key_bits( attributes ) == 0 )
2159 return( PSA_ERROR_NOT_SUPPORTED );
2160
2161 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2162 &handle, &slot, &driver );
2163 if( status != PSA_SUCCESS )
2164 goto exit;
2165
Gilles Peskined7729582019-08-05 15:55:54 +02002166 status = psa_finish_key_creation( slot, driver );
2167
2168exit:
2169 if( status != PSA_SUCCESS )
2170 {
2171 psa_fail_key_creation( slot, driver );
2172 }
2173 /* Registration doesn't keep the key in RAM. */
2174 psa_close_key( handle );
2175 return( status );
2176}
2177#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2178
Gilles Peskinef603c712019-01-19 13:40:11 +01002179static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002180 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002181{
Steven Cooreman398aee52020-10-13 14:35:45 +02002182 psa_status_t status = psa_allocate_buffer_to_slot( target,
2183 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002184 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002185 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002186
Steven Cooreman398aee52020-10-13 14:35:45 +02002187 memcpy( target->data.key.data, source->data.key.data, source->data.key.bytes );
2188 target->attr.type = source->attr.type;
2189 target->attr.bits = source->attr.bits;
2190
2191 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002192}
2193
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002194psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2195 const psa_key_attributes_t *specified_attributes,
2196 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002197{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002198 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002199 psa_key_slot_t *source_slot = NULL;
2200 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002201 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002202 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002203
Gilles Peskine28f8f302019-07-24 13:30:31 +02002204 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002205 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002206 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002207 goto exit;
2208
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002209 status = psa_validate_optional_attributes( source_slot,
2210 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002211 if( status != PSA_SUCCESS )
2212 goto exit;
2213
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002214 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002215 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002216 if( status != PSA_SUCCESS )
2217 goto exit;
2218
Gilles Peskinedf179142019-07-15 22:02:14 +02002219 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2220 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002221 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002222 if( status != PSA_SUCCESS )
2223 goto exit;
2224
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002225#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2226 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002227 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002228 /* Copying to a secure element is not implemented yet. */
2229 status = PSA_ERROR_NOT_SUPPORTED;
2230 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002231 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002232#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002233
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002234 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002235 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002236 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002237
Gilles Peskine011e4282019-06-26 18:34:38 +02002238 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002239exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002240 if( status != PSA_SUCCESS )
2241 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002242 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002243 *target_handle = 0;
2244 }
2245 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002246}
2247
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002248
2249
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002250/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002251/* Message digests */
2252/****************************************************************/
2253
Gilles Peskineb16841e2019-10-10 20:36:12 +02002254#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002255static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002256{
2257 switch( alg )
2258 {
2259#if defined(MBEDTLS_MD2_C)
2260 case PSA_ALG_MD2:
2261 return( &mbedtls_md2_info );
2262#endif
2263#if defined(MBEDTLS_MD4_C)
2264 case PSA_ALG_MD4:
2265 return( &mbedtls_md4_info );
2266#endif
2267#if defined(MBEDTLS_MD5_C)
2268 case PSA_ALG_MD5:
2269 return( &mbedtls_md5_info );
2270#endif
2271#if defined(MBEDTLS_RIPEMD160_C)
2272 case PSA_ALG_RIPEMD160:
2273 return( &mbedtls_ripemd160_info );
2274#endif
2275#if defined(MBEDTLS_SHA1_C)
2276 case PSA_ALG_SHA_1:
2277 return( &mbedtls_sha1_info );
2278#endif
2279#if defined(MBEDTLS_SHA256_C)
2280 case PSA_ALG_SHA_224:
2281 return( &mbedtls_sha224_info );
2282 case PSA_ALG_SHA_256:
2283 return( &mbedtls_sha256_info );
2284#endif
2285#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002286#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002287 case PSA_ALG_SHA_384:
2288 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002289#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002290 case PSA_ALG_SHA_512:
2291 return( &mbedtls_sha512_info );
2292#endif
2293 default:
2294 return( NULL );
2295 }
2296}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002297#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002298
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002299psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2300{
2301 switch( operation->alg )
2302 {
Gilles Peskine81736312018-06-26 15:04:31 +02002303 case 0:
2304 /* The object has (apparently) been initialized but it is not
2305 * in use. It's ok to call abort on such an object, and there's
2306 * nothing to do. */
2307 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002308#if defined(MBEDTLS_MD2_C)
2309 case PSA_ALG_MD2:
2310 mbedtls_md2_free( &operation->ctx.md2 );
2311 break;
2312#endif
2313#if defined(MBEDTLS_MD4_C)
2314 case PSA_ALG_MD4:
2315 mbedtls_md4_free( &operation->ctx.md4 );
2316 break;
2317#endif
2318#if defined(MBEDTLS_MD5_C)
2319 case PSA_ALG_MD5:
2320 mbedtls_md5_free( &operation->ctx.md5 );
2321 break;
2322#endif
2323#if defined(MBEDTLS_RIPEMD160_C)
2324 case PSA_ALG_RIPEMD160:
2325 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2326 break;
2327#endif
2328#if defined(MBEDTLS_SHA1_C)
2329 case PSA_ALG_SHA_1:
2330 mbedtls_sha1_free( &operation->ctx.sha1 );
2331 break;
2332#endif
2333#if defined(MBEDTLS_SHA256_C)
2334 case PSA_ALG_SHA_224:
2335 case PSA_ALG_SHA_256:
2336 mbedtls_sha256_free( &operation->ctx.sha256 );
2337 break;
2338#endif
2339#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002340#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002341 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002342#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002343 case PSA_ALG_SHA_512:
2344 mbedtls_sha512_free( &operation->ctx.sha512 );
2345 break;
2346#endif
2347 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002348 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002349 }
2350 operation->alg = 0;
2351 return( PSA_SUCCESS );
2352}
2353
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002354psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002355 psa_algorithm_t alg )
2356{
Janos Follath24eed8d2019-11-22 13:21:35 +00002357 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002358
2359 /* A context must be freshly initialized before it can be set up. */
2360 if( operation->alg != 0 )
2361 {
2362 return( PSA_ERROR_BAD_STATE );
2363 }
2364
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002365 switch( alg )
2366 {
2367#if defined(MBEDTLS_MD2_C)
2368 case PSA_ALG_MD2:
2369 mbedtls_md2_init( &operation->ctx.md2 );
2370 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2371 break;
2372#endif
2373#if defined(MBEDTLS_MD4_C)
2374 case PSA_ALG_MD4:
2375 mbedtls_md4_init( &operation->ctx.md4 );
2376 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2377 break;
2378#endif
2379#if defined(MBEDTLS_MD5_C)
2380 case PSA_ALG_MD5:
2381 mbedtls_md5_init( &operation->ctx.md5 );
2382 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2383 break;
2384#endif
2385#if defined(MBEDTLS_RIPEMD160_C)
2386 case PSA_ALG_RIPEMD160:
2387 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2388 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2389 break;
2390#endif
2391#if defined(MBEDTLS_SHA1_C)
2392 case PSA_ALG_SHA_1:
2393 mbedtls_sha1_init( &operation->ctx.sha1 );
2394 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2395 break;
2396#endif
2397#if defined(MBEDTLS_SHA256_C)
2398 case PSA_ALG_SHA_224:
2399 mbedtls_sha256_init( &operation->ctx.sha256 );
2400 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2401 break;
2402 case PSA_ALG_SHA_256:
2403 mbedtls_sha256_init( &operation->ctx.sha256 );
2404 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2405 break;
2406#endif
2407#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002408#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002409 case PSA_ALG_SHA_384:
2410 mbedtls_sha512_init( &operation->ctx.sha512 );
2411 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2412 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002413#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002414 case PSA_ALG_SHA_512:
2415 mbedtls_sha512_init( &operation->ctx.sha512 );
2416 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2417 break;
2418#endif
2419 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002420 return( PSA_ALG_IS_HASH( alg ) ?
2421 PSA_ERROR_NOT_SUPPORTED :
2422 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002423 }
2424 if( ret == 0 )
2425 operation->alg = alg;
2426 else
2427 psa_hash_abort( operation );
2428 return( mbedtls_to_psa_error( ret ) );
2429}
2430
2431psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2432 const uint8_t *input,
2433 size_t input_length )
2434{
Janos Follath24eed8d2019-11-22 13:21:35 +00002435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002436
2437 /* Don't require hash implementations to behave correctly on a
2438 * zero-length input, which may have an invalid pointer. */
2439 if( input_length == 0 )
2440 return( PSA_SUCCESS );
2441
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002442 switch( operation->alg )
2443 {
2444#if defined(MBEDTLS_MD2_C)
2445 case PSA_ALG_MD2:
2446 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2447 input, input_length );
2448 break;
2449#endif
2450#if defined(MBEDTLS_MD4_C)
2451 case PSA_ALG_MD4:
2452 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2453 input, input_length );
2454 break;
2455#endif
2456#if defined(MBEDTLS_MD5_C)
2457 case PSA_ALG_MD5:
2458 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2459 input, input_length );
2460 break;
2461#endif
2462#if defined(MBEDTLS_RIPEMD160_C)
2463 case PSA_ALG_RIPEMD160:
2464 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2465 input, input_length );
2466 break;
2467#endif
2468#if defined(MBEDTLS_SHA1_C)
2469 case PSA_ALG_SHA_1:
2470 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2471 input, input_length );
2472 break;
2473#endif
2474#if defined(MBEDTLS_SHA256_C)
2475 case PSA_ALG_SHA_224:
2476 case PSA_ALG_SHA_256:
2477 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2478 input, input_length );
2479 break;
2480#endif
2481#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002482#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002483 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002484#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002485 case PSA_ALG_SHA_512:
2486 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2487 input, input_length );
2488 break;
2489#endif
2490 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002491 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002492 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002493
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002494 if( ret != 0 )
2495 psa_hash_abort( operation );
2496 return( mbedtls_to_psa_error( ret ) );
2497}
2498
2499psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2500 uint8_t *hash,
2501 size_t hash_size,
2502 size_t *hash_length )
2503{
itayzafrir40835d42018-08-02 13:14:17 +03002504 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002505 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002506 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002507
2508 /* Fill the output buffer with something that isn't a valid hash
2509 * (barring an attack on the hash and deliberately-crafted input),
2510 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002511 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002512 /* If hash_size is 0 then hash may be NULL and then the
2513 * call to memset would have undefined behavior. */
2514 if( hash_size != 0 )
2515 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002516
2517 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002518 {
2519 status = PSA_ERROR_BUFFER_TOO_SMALL;
2520 goto exit;
2521 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002522
2523 switch( operation->alg )
2524 {
2525#if defined(MBEDTLS_MD2_C)
2526 case PSA_ALG_MD2:
2527 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2528 break;
2529#endif
2530#if defined(MBEDTLS_MD4_C)
2531 case PSA_ALG_MD4:
2532 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2533 break;
2534#endif
2535#if defined(MBEDTLS_MD5_C)
2536 case PSA_ALG_MD5:
2537 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2538 break;
2539#endif
2540#if defined(MBEDTLS_RIPEMD160_C)
2541 case PSA_ALG_RIPEMD160:
2542 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2543 break;
2544#endif
2545#if defined(MBEDTLS_SHA1_C)
2546 case PSA_ALG_SHA_1:
2547 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2548 break;
2549#endif
2550#if defined(MBEDTLS_SHA256_C)
2551 case PSA_ALG_SHA_224:
2552 case PSA_ALG_SHA_256:
2553 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2554 break;
2555#endif
2556#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002557#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002558 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002559#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002560 case PSA_ALG_SHA_512:
2561 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2562 break;
2563#endif
2564 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002565 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002566 }
itayzafrir40835d42018-08-02 13:14:17 +03002567 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002568
itayzafrir40835d42018-08-02 13:14:17 +03002569exit:
2570 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002571 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002572 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002573 return( psa_hash_abort( operation ) );
2574 }
2575 else
2576 {
2577 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002578 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002579 }
2580}
2581
Gilles Peskine2d277862018-06-18 15:41:12 +02002582psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2583 const uint8_t *hash,
2584 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002585{
2586 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2587 size_t actual_hash_length;
2588 psa_status_t status = psa_hash_finish( operation,
2589 actual_hash, sizeof( actual_hash ),
2590 &actual_hash_length );
2591 if( status != PSA_SUCCESS )
2592 return( status );
2593 if( actual_hash_length != hash_length )
2594 return( PSA_ERROR_INVALID_SIGNATURE );
2595 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2596 return( PSA_ERROR_INVALID_SIGNATURE );
2597 return( PSA_SUCCESS );
2598}
2599
Gilles Peskine0a749c82019-11-28 19:33:58 +01002600psa_status_t psa_hash_compute( psa_algorithm_t alg,
2601 const uint8_t *input, size_t input_length,
2602 uint8_t *hash, size_t hash_size,
2603 size_t *hash_length )
2604{
2605 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2606 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2607
2608 *hash_length = hash_size;
2609 status = psa_hash_setup( &operation, alg );
2610 if( status != PSA_SUCCESS )
2611 goto exit;
2612 status = psa_hash_update( &operation, input, input_length );
2613 if( status != PSA_SUCCESS )
2614 goto exit;
2615 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2616 if( status != PSA_SUCCESS )
2617 goto exit;
2618
2619exit:
2620 if( status == PSA_SUCCESS )
2621 status = psa_hash_abort( &operation );
2622 else
2623 psa_hash_abort( &operation );
2624 return( status );
2625}
2626
2627psa_status_t psa_hash_compare( psa_algorithm_t alg,
2628 const uint8_t *input, size_t input_length,
2629 const uint8_t *hash, size_t hash_length )
2630{
2631 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2632 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2633
2634 status = psa_hash_setup( &operation, alg );
2635 if( status != PSA_SUCCESS )
2636 goto exit;
2637 status = psa_hash_update( &operation, input, input_length );
2638 if( status != PSA_SUCCESS )
2639 goto exit;
2640 status = psa_hash_verify( &operation, hash, hash_length );
2641 if( status != PSA_SUCCESS )
2642 goto exit;
2643
2644exit:
2645 if( status == PSA_SUCCESS )
2646 status = psa_hash_abort( &operation );
2647 else
2648 psa_hash_abort( &operation );
2649 return( status );
2650}
2651
Gilles Peskineeb35d782019-01-22 17:56:16 +01002652psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2653 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002654{
2655 if( target_operation->alg != 0 )
2656 return( PSA_ERROR_BAD_STATE );
2657
2658 switch( source_operation->alg )
2659 {
2660 case 0:
2661 return( PSA_ERROR_BAD_STATE );
2662#if defined(MBEDTLS_MD2_C)
2663 case PSA_ALG_MD2:
2664 mbedtls_md2_clone( &target_operation->ctx.md2,
2665 &source_operation->ctx.md2 );
2666 break;
2667#endif
2668#if defined(MBEDTLS_MD4_C)
2669 case PSA_ALG_MD4:
2670 mbedtls_md4_clone( &target_operation->ctx.md4,
2671 &source_operation->ctx.md4 );
2672 break;
2673#endif
2674#if defined(MBEDTLS_MD5_C)
2675 case PSA_ALG_MD5:
2676 mbedtls_md5_clone( &target_operation->ctx.md5,
2677 &source_operation->ctx.md5 );
2678 break;
2679#endif
2680#if defined(MBEDTLS_RIPEMD160_C)
2681 case PSA_ALG_RIPEMD160:
2682 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2683 &source_operation->ctx.ripemd160 );
2684 break;
2685#endif
2686#if defined(MBEDTLS_SHA1_C)
2687 case PSA_ALG_SHA_1:
2688 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2689 &source_operation->ctx.sha1 );
2690 break;
2691#endif
2692#if defined(MBEDTLS_SHA256_C)
2693 case PSA_ALG_SHA_224:
2694 case PSA_ALG_SHA_256:
2695 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2696 &source_operation->ctx.sha256 );
2697 break;
2698#endif
2699#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002700#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002701 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002702#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002703 case PSA_ALG_SHA_512:
2704 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2705 &source_operation->ctx.sha512 );
2706 break;
2707#endif
2708 default:
2709 return( PSA_ERROR_NOT_SUPPORTED );
2710 }
2711
2712 target_operation->alg = source_operation->alg;
2713 return( PSA_SUCCESS );
2714}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002715
2716
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002717/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002718/* MAC */
2719/****************************************************************/
2720
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002721static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722 psa_algorithm_t alg,
2723 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002724 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002725 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002726{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002728 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002729
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002730 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002731 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002732
Gilles Peskine8c9def32018-02-08 10:02:12 +01002733 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2734 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002735 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002736 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002737 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002738 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002739 mode = MBEDTLS_MODE_STREAM;
2740 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741 case PSA_ALG_CTR:
2742 mode = MBEDTLS_MODE_CTR;
2743 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002744 case PSA_ALG_CFB:
2745 mode = MBEDTLS_MODE_CFB;
2746 break;
2747 case PSA_ALG_OFB:
2748 mode = MBEDTLS_MODE_OFB;
2749 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002750 case PSA_ALG_ECB_NO_PADDING:
2751 mode = MBEDTLS_MODE_ECB;
2752 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002753 case PSA_ALG_CBC_NO_PADDING:
2754 mode = MBEDTLS_MODE_CBC;
2755 break;
2756 case PSA_ALG_CBC_PKCS7:
2757 mode = MBEDTLS_MODE_CBC;
2758 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002759 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002760 mode = MBEDTLS_MODE_CCM;
2761 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002762 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002763 mode = MBEDTLS_MODE_GCM;
2764 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002765 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2766 mode = MBEDTLS_MODE_CHACHAPOLY;
2767 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002768 default:
2769 return( NULL );
2770 }
2771 }
2772 else if( alg == PSA_ALG_CMAC )
2773 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002774 else
2775 return( NULL );
2776
2777 switch( key_type )
2778 {
2779 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002780 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002781 break;
2782 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002783 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2784 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002785 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002786 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002787 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002788 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002789 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2790 * but two-key Triple-DES is functionally three-key Triple-DES
2791 * with K1=K3, so that's how we present it to mbedtls. */
2792 if( key_bits == 128 )
2793 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002794 break;
2795 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002796 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002797 break;
2798 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002799 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002800 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002801 case PSA_KEY_TYPE_CHACHA20:
2802 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2803 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002804 default:
2805 return( NULL );
2806 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002807 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002808 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002809
Jaeden Amero23bbb752018-06-26 14:16:54 +01002810 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2811 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002812}
2813
Gilles Peskinea05219c2018-11-16 16:02:56 +01002814#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002815static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002816{
Gilles Peskine2d277862018-06-18 15:41:12 +02002817 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002818 {
2819 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002820 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002821 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002822 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002823 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002824 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002825 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002826 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002827 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002828 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002829 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002830 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002831 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002832 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002833 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002834 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002835 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002836 return( 128 );
2837 default:
2838 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002839 }
2840}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002841#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002842
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002843/* Initialize the MAC operation structure. Once this function has been
2844 * called, psa_mac_abort can run and will do the right thing. */
2845static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2846 psa_algorithm_t alg )
2847{
2848 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2849
2850 operation->alg = alg;
2851 operation->key_set = 0;
2852 operation->iv_set = 0;
2853 operation->iv_required = 0;
2854 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002855 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002856
2857#if defined(MBEDTLS_CMAC_C)
2858 if( alg == PSA_ALG_CMAC )
2859 {
2860 operation->iv_required = 0;
2861 mbedtls_cipher_init( &operation->ctx.cmac );
2862 status = PSA_SUCCESS;
2863 }
2864 else
2865#endif /* MBEDTLS_CMAC_C */
2866#if defined(MBEDTLS_MD_C)
2867 if( PSA_ALG_IS_HMAC( operation->alg ) )
2868 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002869 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2870 operation->ctx.hmac.hash_ctx.alg = 0;
2871 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002872 }
2873 else
2874#endif /* MBEDTLS_MD_C */
2875 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002876 if( ! PSA_ALG_IS_MAC( alg ) )
2877 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002878 }
2879
2880 if( status != PSA_SUCCESS )
2881 memset( operation, 0, sizeof( *operation ) );
2882 return( status );
2883}
2884
Gilles Peskine01126fa2018-07-12 17:04:55 +02002885#if defined(MBEDTLS_MD_C)
2886static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2887{
Gilles Peskine3f108122018-12-07 18:14:53 +01002888 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002889 return( psa_hash_abort( &hmac->hash_ctx ) );
2890}
2891#endif /* MBEDTLS_MD_C */
2892
Gilles Peskine8c9def32018-02-08 10:02:12 +01002893psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2894{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002895 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002896 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002897 /* The object has (apparently) been initialized but it is not
2898 * in use. It's ok to call abort on such an object, and there's
2899 * nothing to do. */
2900 return( PSA_SUCCESS );
2901 }
2902 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002903#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002904 if( operation->alg == PSA_ALG_CMAC )
2905 {
2906 mbedtls_cipher_free( &operation->ctx.cmac );
2907 }
2908 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002909#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002910#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002911 if( PSA_ALG_IS_HMAC( operation->alg ) )
2912 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002913 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002914 }
2915 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002916#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002917 {
2918 /* Sanity check (shouldn't happen: operation->alg should
2919 * always have been initialized to a valid value). */
2920 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002921 }
Moran Peker41deec42018-04-04 15:43:05 +03002922
Gilles Peskine8c9def32018-02-08 10:02:12 +01002923 operation->alg = 0;
2924 operation->key_set = 0;
2925 operation->iv_set = 0;
2926 operation->iv_required = 0;
2927 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002928 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002929
Gilles Peskine8c9def32018-02-08 10:02:12 +01002930 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002931
2932bad_state:
2933 /* If abort is called on an uninitialized object, we can't trust
2934 * anything. Wipe the object in case it contains confidential data.
2935 * This may result in a memory leak if a pointer gets overwritten,
2936 * but it's too late to do anything about this. */
2937 memset( operation, 0, sizeof( *operation ) );
2938 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002939}
2940
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002941#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002942static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002943 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002944 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002945 const mbedtls_cipher_info_t *cipher_info )
2946{
Janos Follath24eed8d2019-11-22 13:21:35 +00002947 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002948
2949 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002950
2951 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2952 if( ret != 0 )
2953 return( ret );
2954
2955 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002956 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002957 key_bits );
2958 return( ret );
2959}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002960#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002961
Gilles Peskine248051a2018-06-20 16:09:38 +02002962#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002963static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2964 const uint8_t *key,
2965 size_t key_length,
2966 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002967{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002968 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002969 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002970 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002971 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002972 psa_status_t status;
2973
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002974 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2975 * overflow below. This should never trigger if the hash algorithm
2976 * is implemented correctly. */
2977 /* The size checks against the ipad and opad buffers cannot be written
2978 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2979 * because that triggers -Wlogical-op on GCC 7.3. */
2980 if( block_size > sizeof( ipad ) )
2981 return( PSA_ERROR_NOT_SUPPORTED );
2982 if( block_size > sizeof( hmac->opad ) )
2983 return( PSA_ERROR_NOT_SUPPORTED );
2984 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002985 return( PSA_ERROR_NOT_SUPPORTED );
2986
Gilles Peskined223b522018-06-11 18:12:58 +02002987 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002988 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002989 status = psa_hash_compute( hash_alg, key, key_length,
2990 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002991 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002992 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002993 }
Gilles Peskine96889972018-07-12 17:07:03 +02002994 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2995 * but it is permitted. It is common when HMAC is used in HKDF, for
2996 * example. Don't call `memcpy` in the 0-length because `key` could be
2997 * an invalid pointer which would make the behavior undefined. */
2998 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002999 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003000
Gilles Peskined223b522018-06-11 18:12:58 +02003001 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3002 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003003 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003004 ipad[i] ^= 0x36;
3005 memset( ipad + key_length, 0x36, block_size - key_length );
3006
3007 /* Copy the key material from ipad to opad, flipping the requisite bits,
3008 * and filling the rest of opad with the requisite constant. */
3009 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003010 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3011 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003012
Gilles Peskine01126fa2018-07-12 17:04:55 +02003013 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003014 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003015 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003016
Gilles Peskine01126fa2018-07-12 17:04:55 +02003017 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003018
3019cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003020 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003021
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003022 return( status );
3023}
Gilles Peskine248051a2018-06-20 16:09:38 +02003024#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003025
Gilles Peskine89167cb2018-07-08 20:12:23 +02003026static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003027 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003028 psa_algorithm_t alg,
3029 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003030{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003031 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003032 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003033 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003034 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003035 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003036 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003037 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003038
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003039 /* A context must be freshly initialized before it can be set up. */
3040 if( operation->alg != 0 )
3041 {
3042 return( PSA_ERROR_BAD_STATE );
3043 }
3044
Gilles Peskined911eb72018-08-14 15:18:45 +02003045 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003046 if( status != PSA_SUCCESS )
3047 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003048 if( is_sign )
3049 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003050
Gilles Peskine28f8f302019-07-24 13:30:31 +02003051 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003052 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003053 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003054 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003055
Gilles Peskine8c9def32018-02-08 10:02:12 +01003056#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003057 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003058 {
3059 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003060 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003061 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003062 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003063 if( cipher_info == NULL )
3064 {
3065 status = PSA_ERROR_NOT_SUPPORTED;
3066 goto exit;
3067 }
3068 operation->mac_size = cipher_info->block_size;
3069 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3070 status = mbedtls_to_psa_error( ret );
3071 }
3072 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003073#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003074#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003075 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003076 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003077 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003078 if( hash_alg == 0 )
3079 {
3080 status = PSA_ERROR_NOT_SUPPORTED;
3081 goto exit;
3082 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003083
3084 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3085 /* Sanity check. This shouldn't fail on a valid configuration. */
3086 if( operation->mac_size == 0 ||
3087 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3088 {
3089 status = PSA_ERROR_NOT_SUPPORTED;
3090 goto exit;
3091 }
3092
Gilles Peskine8e338702019-07-30 20:06:31 +02003093 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003094 {
3095 status = PSA_ERROR_INVALID_ARGUMENT;
3096 goto exit;
3097 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003098
Gilles Peskine01126fa2018-07-12 17:04:55 +02003099 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003100 slot->data.key.data,
3101 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003102 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003103 }
3104 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003105#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003106 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003107 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003108 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003109 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003110
Gilles Peskined911eb72018-08-14 15:18:45 +02003111 if( truncated == 0 )
3112 {
3113 /* The "normal" case: untruncated algorithm. Nothing to do. */
3114 }
3115 else if( truncated < 4 )
3116 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003117 /* A very short MAC is too short for security since it can be
3118 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3119 * so we make this our minimum, even though 32 bits is still
3120 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003121 status = PSA_ERROR_NOT_SUPPORTED;
3122 }
3123 else if( truncated > operation->mac_size )
3124 {
3125 /* It's impossible to "truncate" to a larger length. */
3126 status = PSA_ERROR_INVALID_ARGUMENT;
3127 }
3128 else
3129 operation->mac_size = truncated;
3130
Gilles Peskinefbfac682018-07-08 20:51:54 +02003131exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003132 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003133 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003134 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003135 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003136 else
3137 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003138 operation->key_set = 1;
3139 }
3140 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003141}
3142
Gilles Peskine89167cb2018-07-08 20:12:23 +02003143psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003144 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003145 psa_algorithm_t alg )
3146{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003147 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003148}
3149
3150psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003151 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003152 psa_algorithm_t alg )
3153{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003154 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003155}
3156
Gilles Peskine8c9def32018-02-08 10:02:12 +01003157psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3158 const uint8_t *input,
3159 size_t input_length )
3160{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003161 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003162 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003163 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003164 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003165 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003166 operation->has_input = 1;
3167
Gilles Peskine8c9def32018-02-08 10:02:12 +01003168#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003169 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003170 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003171 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3172 input, input_length );
3173 status = mbedtls_to_psa_error( ret );
3174 }
3175 else
3176#endif /* MBEDTLS_CMAC_C */
3177#if defined(MBEDTLS_MD_C)
3178 if( PSA_ALG_IS_HMAC( operation->alg ) )
3179 {
3180 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3181 input_length );
3182 }
3183 else
3184#endif /* MBEDTLS_MD_C */
3185 {
3186 /* This shouldn't happen if `operation` was initialized by
3187 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003188 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003189 }
3190
Gilles Peskinefbfac682018-07-08 20:51:54 +02003191 if( status != PSA_SUCCESS )
3192 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003193 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003194}
3195
Gilles Peskine01126fa2018-07-12 17:04:55 +02003196#if defined(MBEDTLS_MD_C)
3197static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3198 uint8_t *mac,
3199 size_t mac_size )
3200{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003201 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003202 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3203 size_t hash_size = 0;
3204 size_t block_size = psa_get_hash_block_size( hash_alg );
3205 psa_status_t status;
3206
Gilles Peskine01126fa2018-07-12 17:04:55 +02003207 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3208 if( status != PSA_SUCCESS )
3209 return( status );
3210 /* From here on, tmp needs to be wiped. */
3211
3212 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3213 if( status != PSA_SUCCESS )
3214 goto exit;
3215
3216 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3217 if( status != PSA_SUCCESS )
3218 goto exit;
3219
3220 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3221 if( status != PSA_SUCCESS )
3222 goto exit;
3223
Gilles Peskined911eb72018-08-14 15:18:45 +02003224 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3225 if( status != PSA_SUCCESS )
3226 goto exit;
3227
3228 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003229
3230exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003231 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003232 return( status );
3233}
3234#endif /* MBEDTLS_MD_C */
3235
mohammad16036df908f2018-04-02 08:34:15 -07003236static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003237 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003238 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003239{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003240 if( ! operation->key_set )
3241 return( PSA_ERROR_BAD_STATE );
3242 if( operation->iv_required && ! operation->iv_set )
3243 return( PSA_ERROR_BAD_STATE );
3244
Gilles Peskine8c9def32018-02-08 10:02:12 +01003245 if( mac_size < operation->mac_size )
3246 return( PSA_ERROR_BUFFER_TOO_SMALL );
3247
Gilles Peskine8c9def32018-02-08 10:02:12 +01003248#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003249 if( operation->alg == PSA_ALG_CMAC )
3250 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003251 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3252 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3253 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003254 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003255 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003256 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003257 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003258 else
3259#endif /* MBEDTLS_CMAC_C */
3260#if defined(MBEDTLS_MD_C)
3261 if( PSA_ALG_IS_HMAC( operation->alg ) )
3262 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003263 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003264 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003265 }
3266 else
3267#endif /* MBEDTLS_MD_C */
3268 {
3269 /* This shouldn't happen if `operation` was initialized by
3270 * a setup function. */
3271 return( PSA_ERROR_BAD_STATE );
3272 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003273}
3274
Gilles Peskineacd4be32018-07-08 19:56:25 +02003275psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3276 uint8_t *mac,
3277 size_t mac_size,
3278 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003279{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003280 psa_status_t status;
3281
Jaeden Amero252ef282019-02-15 14:05:35 +00003282 if( operation->alg == 0 )
3283 {
3284 return( PSA_ERROR_BAD_STATE );
3285 }
3286
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003287 /* Fill the output buffer with something that isn't a valid mac
3288 * (barring an attack on the mac and deliberately-crafted input),
3289 * in case the caller doesn't check the return status properly. */
3290 *mac_length = mac_size;
3291 /* If mac_size is 0 then mac may be NULL and then the
3292 * call to memset would have undefined behavior. */
3293 if( mac_size != 0 )
3294 memset( mac, '!', mac_size );
3295
Gilles Peskine89167cb2018-07-08 20:12:23 +02003296 if( ! operation->is_sign )
3297 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003298 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003299 }
mohammad16036df908f2018-04-02 08:34:15 -07003300
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003301 status = psa_mac_finish_internal( operation, mac, mac_size );
3302
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003303 if( status == PSA_SUCCESS )
3304 {
3305 status = psa_mac_abort( operation );
3306 if( status == PSA_SUCCESS )
3307 *mac_length = operation->mac_size;
3308 else
3309 memset( mac, '!', mac_size );
3310 }
3311 else
3312 psa_mac_abort( operation );
3313 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003314}
3315
Gilles Peskineacd4be32018-07-08 19:56:25 +02003316psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3317 const uint8_t *mac,
3318 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003319{
Gilles Peskine828ed142018-06-18 23:25:51 +02003320 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003321 psa_status_t status;
3322
Jaeden Amero252ef282019-02-15 14:05:35 +00003323 if( operation->alg == 0 )
3324 {
3325 return( PSA_ERROR_BAD_STATE );
3326 }
3327
Gilles Peskine89167cb2018-07-08 20:12:23 +02003328 if( operation->is_sign )
3329 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003330 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003331 }
3332 if( operation->mac_size != mac_length )
3333 {
3334 status = PSA_ERROR_INVALID_SIGNATURE;
3335 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003336 }
mohammad16036df908f2018-04-02 08:34:15 -07003337
3338 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003339 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003340 if( status != PSA_SUCCESS )
3341 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003342
3343 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3344 status = PSA_ERROR_INVALID_SIGNATURE;
3345
3346cleanup:
3347 if( status == PSA_SUCCESS )
3348 status = psa_mac_abort( operation );
3349 else
3350 psa_mac_abort( operation );
3351
Gilles Peskine3f108122018-12-07 18:14:53 +01003352 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003353
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003354 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003355}
3356
3357
Gilles Peskine20035e32018-02-03 22:44:14 +01003358
Gilles Peskine20035e32018-02-03 22:44:14 +01003359/****************************************************************/
3360/* Asymmetric cryptography */
3361/****************************************************************/
3362
Gilles Peskine2b450e32018-06-27 15:42:46 +02003363#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003364/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003365 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003366static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3367 size_t hash_length,
3368 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003369{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003370 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003371 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003372 *md_alg = mbedtls_md_get_type( md_info );
3373
3374 /* The Mbed TLS RSA module uses an unsigned int for hash length
3375 * parameters. Validate that it fits so that we don't risk an
3376 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003377#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003378 if( hash_length > UINT_MAX )
3379 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003380#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003381
3382#if defined(MBEDTLS_PKCS1_V15)
3383 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3384 * must be correct. */
3385 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3386 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003387 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003388 if( md_info == NULL )
3389 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003390 if( mbedtls_md_get_size( md_info ) != hash_length )
3391 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003392 }
3393#endif /* MBEDTLS_PKCS1_V15 */
3394
3395#if defined(MBEDTLS_PKCS1_V21)
3396 /* PSS requires a hash internally. */
3397 if( PSA_ALG_IS_RSA_PSS( alg ) )
3398 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003399 if( md_info == NULL )
3400 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003401 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003402#endif /* MBEDTLS_PKCS1_V21 */
3403
Gilles Peskine61b91d42018-06-08 16:09:36 +02003404 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003405}
3406
Gilles Peskine2b450e32018-06-27 15:42:46 +02003407static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3408 psa_algorithm_t alg,
3409 const uint8_t *hash,
3410 size_t hash_length,
3411 uint8_t *signature,
3412 size_t signature_size,
3413 size_t *signature_length )
3414{
3415 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003417 mbedtls_md_type_t md_alg;
3418
3419 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3420 if( status != PSA_SUCCESS )
3421 return( status );
3422
Gilles Peskine630a18a2018-06-29 17:49:35 +02003423 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003424 return( PSA_ERROR_BUFFER_TOO_SMALL );
3425
3426#if defined(MBEDTLS_PKCS1_V15)
3427 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3428 {
3429 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3430 MBEDTLS_MD_NONE );
3431 ret = mbedtls_rsa_pkcs1_sign( rsa,
3432 mbedtls_ctr_drbg_random,
3433 &global_data.ctr_drbg,
3434 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003435 md_alg,
3436 (unsigned int) hash_length,
3437 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003438 signature );
3439 }
3440 else
3441#endif /* MBEDTLS_PKCS1_V15 */
3442#if defined(MBEDTLS_PKCS1_V21)
3443 if( PSA_ALG_IS_RSA_PSS( alg ) )
3444 {
3445 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3446 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3447 mbedtls_ctr_drbg_random,
3448 &global_data.ctr_drbg,
3449 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003450 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003451 (unsigned int) hash_length,
3452 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003453 signature );
3454 }
3455 else
3456#endif /* MBEDTLS_PKCS1_V21 */
3457 {
3458 return( PSA_ERROR_INVALID_ARGUMENT );
3459 }
3460
3461 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003462 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003463 return( mbedtls_to_psa_error( ret ) );
3464}
3465
3466static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3467 psa_algorithm_t alg,
3468 const uint8_t *hash,
3469 size_t hash_length,
3470 const uint8_t *signature,
3471 size_t signature_length )
3472{
3473 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003474 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003475 mbedtls_md_type_t md_alg;
3476
3477 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3478 if( status != PSA_SUCCESS )
3479 return( status );
3480
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003481 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3482 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003483
3484#if defined(MBEDTLS_PKCS1_V15)
3485 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3486 {
3487 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3488 MBEDTLS_MD_NONE );
3489 ret = mbedtls_rsa_pkcs1_verify( rsa,
3490 mbedtls_ctr_drbg_random,
3491 &global_data.ctr_drbg,
3492 MBEDTLS_RSA_PUBLIC,
3493 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003494 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003495 hash,
3496 signature );
3497 }
3498 else
3499#endif /* MBEDTLS_PKCS1_V15 */
3500#if defined(MBEDTLS_PKCS1_V21)
3501 if( PSA_ALG_IS_RSA_PSS( alg ) )
3502 {
3503 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3504 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3505 mbedtls_ctr_drbg_random,
3506 &global_data.ctr_drbg,
3507 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003508 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003509 (unsigned int) hash_length,
3510 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003511 signature );
3512 }
3513 else
3514#endif /* MBEDTLS_PKCS1_V21 */
3515 {
3516 return( PSA_ERROR_INVALID_ARGUMENT );
3517 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003518
3519 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3520 * the rest of the signature is invalid". This has little use in
3521 * practice and PSA doesn't report this distinction. */
3522 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3523 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003524 return( mbedtls_to_psa_error( ret ) );
3525}
3526#endif /* MBEDTLS_RSA_C */
3527
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003528#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003529/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3530 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3531 * (even though these functions don't modify it). */
3532static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3533 psa_algorithm_t alg,
3534 const uint8_t *hash,
3535 size_t hash_length,
3536 uint8_t *signature,
3537 size_t signature_size,
3538 size_t *signature_length )
3539{
Janos Follath24eed8d2019-11-22 13:21:35 +00003540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003541 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003542 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003543 mbedtls_mpi_init( &r );
3544 mbedtls_mpi_init( &s );
3545
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003546 if( signature_size < 2 * curve_bytes )
3547 {
3548 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3549 goto cleanup;
3550 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003551
Gilles Peskinea05219c2018-11-16 16:02:56 +01003552#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003553 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3554 {
3555 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3556 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3557 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003558 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3559 &ecp->d, hash,
3560 hash_length, md_alg,
3561 mbedtls_ctr_drbg_random,
3562 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003563 }
3564 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003565#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003566 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003567 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003568 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3569 hash, hash_length,
3570 mbedtls_ctr_drbg_random,
3571 &global_data.ctr_drbg ) );
3572 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003573
3574 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3575 signature,
3576 curve_bytes ) );
3577 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3578 signature + curve_bytes,
3579 curve_bytes ) );
3580
3581cleanup:
3582 mbedtls_mpi_free( &r );
3583 mbedtls_mpi_free( &s );
3584 if( ret == 0 )
3585 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003586 return( mbedtls_to_psa_error( ret ) );
3587}
3588
3589static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3590 const uint8_t *hash,
3591 size_t hash_length,
3592 const uint8_t *signature,
3593 size_t signature_length )
3594{
Janos Follath24eed8d2019-11-22 13:21:35 +00003595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003596 mbedtls_mpi r, s;
3597 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3598 mbedtls_mpi_init( &r );
3599 mbedtls_mpi_init( &s );
3600
3601 if( signature_length != 2 * curve_bytes )
3602 return( PSA_ERROR_INVALID_SIGNATURE );
3603
3604 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3605 signature,
3606 curve_bytes ) );
3607 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3608 signature + curve_bytes,
3609 curve_bytes ) );
3610
Steven Cooremanacda8342020-07-24 23:09:52 +02003611 /* Check whether the public part is loaded. If not, load it. */
3612 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3613 {
3614 MBEDTLS_MPI_CHK(
3615 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3616 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3617 }
3618
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003619 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3620 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003621
3622cleanup:
3623 mbedtls_mpi_free( &r );
3624 mbedtls_mpi_free( &s );
3625 return( mbedtls_to_psa_error( ret ) );
3626}
3627#endif /* MBEDTLS_ECDSA_C */
3628
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003629psa_status_t psa_sign_hash( psa_key_handle_t handle,
3630 psa_algorithm_t alg,
3631 const uint8_t *hash,
3632 size_t hash_length,
3633 uint8_t *signature,
3634 size_t signature_size,
3635 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003636{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003637 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003638 psa_status_t status;
3639
3640 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003641 /* Immediately reject a zero-length signature buffer. This guarantees
3642 * that signature must be a valid pointer. (On the other hand, the hash
3643 * buffer can in principle be empty since it doesn't actually have
3644 * to be a hash.) */
3645 if( signature_size == 0 )
3646 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003647
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003648 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003649 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003650 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003651 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003652 {
3653 status = PSA_ERROR_INVALID_ARGUMENT;
3654 goto exit;
3655 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003656
Steven Cooremancd84cb42020-07-16 20:28:36 +02003657 /* Try any of the available accelerators first */
3658 status = psa_driver_wrapper_sign_hash( slot,
3659 alg,
3660 hash,
3661 hash_length,
3662 signature,
3663 signature_size,
3664 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003665 if( status != PSA_ERROR_NOT_SUPPORTED ||
3666 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003667 goto exit;
3668
Steven Cooreman7a250572020-07-17 16:43:05 +02003669 /* If the operation was not supported by any accelerator, try fallback. */
Gilles Peskine20035e32018-02-03 22:44:14 +01003670#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003671 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003672 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003673 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003674
Steven Cooreman4fed4552020-08-03 14:46:03 +02003675 status = psa_load_rsa_representation( slot->attr.type,
3676 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003677 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003678 &rsa );
3679 if( status != PSA_SUCCESS )
3680 goto exit;
3681
Steven Cooremana2371e52020-07-28 14:30:39 +02003682 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003683 alg,
3684 hash, hash_length,
3685 signature, signature_size,
3686 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003687
Steven Cooremana2371e52020-07-28 14:30:39 +02003688 mbedtls_rsa_free( rsa );
3689 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003690 }
3691 else
3692#endif /* defined(MBEDTLS_RSA_C) */
3693#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003694 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003695 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003696#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003697 if(
3698#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3699 PSA_ALG_IS_ECDSA( alg )
3700#else
3701 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3702#endif
3703 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003704 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003705 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003706 status = psa_load_ecp_representation( slot->attr.type,
3707 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003708 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003709 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003710 if( status != PSA_SUCCESS )
3711 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003712 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003713 alg,
3714 hash, hash_length,
3715 signature, signature_size,
3716 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003717 mbedtls_ecp_keypair_free( ecp );
3718 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003719 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003720 else
3721#endif /* defined(MBEDTLS_ECDSA_C) */
3722 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003723 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003724 }
itayzafrir5c753392018-05-08 11:18:38 +03003725 }
3726 else
3727#endif /* defined(MBEDTLS_ECP_C) */
3728 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003729 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003730 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003731
3732exit:
3733 /* Fill the unused part of the output buffer (the whole buffer on error,
3734 * the trailing part on success) with something that isn't a valid mac
3735 * (barring an attack on the mac and deliberately-crafted input),
3736 * in case the caller doesn't check the return status properly. */
3737 if( status == PSA_SUCCESS )
3738 memset( signature + *signature_length, '!',
3739 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003740 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003741 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003742 /* If signature_size is 0 then we have nothing to do. We must not call
3743 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003744 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003745}
3746
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003747psa_status_t psa_verify_hash( psa_key_handle_t handle,
3748 psa_algorithm_t alg,
3749 const uint8_t *hash,
3750 size_t hash_length,
3751 const uint8_t *signature,
3752 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003753{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003754 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003755 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003756
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003757 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003758 if( status != PSA_SUCCESS )
3759 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003760
Steven Cooreman55ae2172020-07-17 19:46:15 +02003761 /* Try any of the available accelerators first */
3762 status = psa_driver_wrapper_verify_hash( slot,
3763 alg,
3764 hash,
3765 hash_length,
3766 signature,
3767 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003768 if( status != PSA_ERROR_NOT_SUPPORTED ||
3769 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +02003770 return status;
3771
Gilles Peskine61b91d42018-06-08 16:09:36 +02003772#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003773 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003774 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003775 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003776
Steven Cooreman4fed4552020-08-03 14:46:03 +02003777 status = psa_load_rsa_representation( slot->attr.type,
3778 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003779 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003780 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003781 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003782 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02003783
Steven Cooremana2371e52020-07-28 14:30:39 +02003784 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003785 alg,
3786 hash, hash_length,
3787 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003788 mbedtls_rsa_free( rsa );
3789 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003790 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003791 }
3792 else
3793#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003794#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003795 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003796 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003797#if defined(MBEDTLS_ECDSA_C)
3798 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003799 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003800 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003801 status = psa_load_ecp_representation( slot->attr.type,
3802 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003803 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003804 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003805 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003806 return( status );
Steven Cooremana2371e52020-07-28 14:30:39 +02003807 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003808 hash, hash_length,
3809 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003810 mbedtls_ecp_keypair_free( ecp );
3811 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02003812 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02003813 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003814 else
3815#endif /* defined(MBEDTLS_ECDSA_C) */
3816 {
3817 return( PSA_ERROR_INVALID_ARGUMENT );
3818 }
itayzafrir5c753392018-05-08 11:18:38 +03003819 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003820 else
3821#endif /* defined(MBEDTLS_ECP_C) */
3822 {
3823 return( PSA_ERROR_NOT_SUPPORTED );
3824 }
3825}
3826
Gilles Peskine072ac562018-06-30 00:21:29 +02003827#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3828static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3829 mbedtls_rsa_context *rsa )
3830{
3831 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3832 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3833 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3834 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3835}
3836#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3837
Gilles Peskinec5487a82018-12-03 18:08:14 +01003838psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003839 psa_algorithm_t alg,
3840 const uint8_t *input,
3841 size_t input_length,
3842 const uint8_t *salt,
3843 size_t salt_length,
3844 uint8_t *output,
3845 size_t output_size,
3846 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003847{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003848 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003849 psa_status_t status;
3850
Darryl Green5cc689a2018-07-24 15:34:10 +01003851 (void) input;
3852 (void) input_length;
3853 (void) salt;
3854 (void) output;
3855 (void) output_size;
3856
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003857 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003858
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003859 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3860 return( PSA_ERROR_INVALID_ARGUMENT );
3861
Gilles Peskine28f8f302019-07-24 13:30:31 +02003862 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003863 if( status != PSA_SUCCESS )
3864 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003865 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3866 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003867 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003868
3869#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003870 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003871 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003872 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003873 status = psa_load_rsa_representation( slot->attr.type,
3874 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003875 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003876 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003877 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003878 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003879
3880 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003881 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003882 status = PSA_ERROR_BUFFER_TOO_SMALL;
3883 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003884 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003885#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003886 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003887 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003888 status = mbedtls_to_psa_error(
3889 mbedtls_rsa_pkcs1_encrypt( rsa,
3890 mbedtls_ctr_drbg_random,
3891 &global_data.ctr_drbg,
3892 MBEDTLS_RSA_PUBLIC,
3893 input_length,
3894 input,
3895 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003896 }
3897 else
3898#endif /* MBEDTLS_PKCS1_V15 */
3899#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003900 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003901 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003902 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003903 status = mbedtls_to_psa_error(
3904 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3905 mbedtls_ctr_drbg_random,
3906 &global_data.ctr_drbg,
3907 MBEDTLS_RSA_PUBLIC,
3908 salt, salt_length,
3909 input_length,
3910 input,
3911 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003912 }
3913 else
3914#endif /* MBEDTLS_PKCS1_V21 */
3915 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003916 status = PSA_ERROR_INVALID_ARGUMENT;
3917 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003918 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003919rsa_exit:
3920 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003921 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003922
Steven Cooremana2371e52020-07-28 14:30:39 +02003923 mbedtls_rsa_free( rsa );
3924 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003925 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003926 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003927 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003928#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003929 {
3930 return( PSA_ERROR_NOT_SUPPORTED );
3931 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003932}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003933
Gilles Peskinec5487a82018-12-03 18:08:14 +01003934psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003935 psa_algorithm_t alg,
3936 const uint8_t *input,
3937 size_t input_length,
3938 const uint8_t *salt,
3939 size_t salt_length,
3940 uint8_t *output,
3941 size_t output_size,
3942 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003943{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003944 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003945 psa_status_t status;
3946
Darryl Green5cc689a2018-07-24 15:34:10 +01003947 (void) input;
3948 (void) input_length;
3949 (void) salt;
3950 (void) output;
3951 (void) output_size;
3952
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003953 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003954
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003955 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3956 return( PSA_ERROR_INVALID_ARGUMENT );
3957
Gilles Peskine28f8f302019-07-24 13:30:31 +02003958 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003959 if( status != PSA_SUCCESS )
3960 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003961 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003962 return( PSA_ERROR_INVALID_ARGUMENT );
3963
3964#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003965 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003966 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003967 mbedtls_rsa_context *rsa = NULL;
3968 status = psa_load_rsa_representation( slot->attr.type,
3969 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003970 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003971 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003972 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003973 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003974
Steven Cooremana2371e52020-07-28 14:30:39 +02003975 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003976 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003977 status = PSA_ERROR_INVALID_ARGUMENT;
3978 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003979 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003980
3981#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003982 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003983 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003984 status = mbedtls_to_psa_error(
3985 mbedtls_rsa_pkcs1_decrypt( rsa,
3986 mbedtls_ctr_drbg_random,
3987 &global_data.ctr_drbg,
3988 MBEDTLS_RSA_PRIVATE,
3989 output_length,
3990 input,
3991 output,
3992 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003993 }
3994 else
3995#endif /* MBEDTLS_PKCS1_V15 */
3996#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003997 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003998 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003999 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004000 status = mbedtls_to_psa_error(
4001 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4002 mbedtls_ctr_drbg_random,
4003 &global_data.ctr_drbg,
4004 MBEDTLS_RSA_PRIVATE,
4005 salt, salt_length,
4006 output_length,
4007 input,
4008 output,
4009 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004010 }
4011 else
4012#endif /* MBEDTLS_PKCS1_V21 */
4013 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004014 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004015 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004016
Steven Cooreman4fed4552020-08-03 14:46:03 +02004017rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004018 mbedtls_rsa_free( rsa );
4019 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004020 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004021 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004022 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004023#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004024 {
4025 return( PSA_ERROR_NOT_SUPPORTED );
4026 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004027}
Gilles Peskine20035e32018-02-03 22:44:14 +01004028
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004029
4030
mohammad1603503973b2018-03-12 15:59:30 +02004031/****************************************************************/
4032/* Symmetric cryptography */
4033/****************************************************************/
4034
Gilles Peskinee553c652018-06-04 16:22:46 +02004035static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004036 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004037 psa_algorithm_t alg,
4038 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004039{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004040 int ret = 0;
4041 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004042 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004043 size_t key_bits;
4044 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004045 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4046 PSA_KEY_USAGE_ENCRYPT :
4047 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004048
Steven Cooremand3feccd2020-09-01 15:56:14 +02004049 /* A context must be freshly initialized before it can be set up. */
4050 if( operation->alg != 0 )
4051 return( PSA_ERROR_BAD_STATE );
4052
Steven Cooremana07b9972020-09-10 14:54:14 +02004053 /* The requested algorithm must be one that can be processed by cipher. */
4054 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004055 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004056
Steven Cooremanef8575e2020-09-11 11:44:50 +02004057 /* Fetch key material from key storage. */
4058 status = psa_get_key_from_slot( handle, &slot, usage, alg );
4059 if( status != PSA_SUCCESS )
4060 goto exit;
4061
4062 /* Initialize the operation struct members, except for alg. The alg member
4063 * is used to indicate to psa_cipher_abort that there are resources to free,
4064 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004065 operation->key_set = 0;
4066 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004067 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004068 operation->iv_size = 0;
4069 operation->block_size = 0;
4070 if( alg == PSA_ALG_ECB_NO_PADDING )
4071 operation->iv_required = 0;
4072 else
4073 operation->iv_required = 1;
4074
Steven Cooremana07b9972020-09-10 14:54:14 +02004075 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004076 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004077 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004078 slot,
4079 alg );
4080 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004081 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004082 slot,
4083 alg );
4084
Steven Cooremanef8575e2020-09-11 11:44:50 +02004085 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004086 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004087 /* Once the driver context is initialised, it needs to be freed using
4088 * psa_cipher_abort. Indicate this through setting alg. */
4089 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004090 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004091
Steven Cooremand3feccd2020-09-01 15:56:14 +02004092 if( status != PSA_ERROR_NOT_SUPPORTED ||
4093 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4094 goto exit;
4095
Steven Cooremana07b9972020-09-10 14:54:14 +02004096 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004097 * available for the given algorithm & key. */
4098 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004099
Steven Cooremana07b9972020-09-10 14:54:14 +02004100 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004101 * psa_cipher_abort. Indicate there is something to be freed through setting
4102 * alg, and indicate the operation is being done using mbedtls crypto through
4103 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004104 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004105 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004106
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004107 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004108 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004109 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004110 {
4111 status = PSA_ERROR_NOT_SUPPORTED;
4112 goto exit;
4113 }
mohammad1603503973b2018-03-12 15:59:30 +02004114
mohammad1603503973b2018-03-12 15:59:30 +02004115 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004116 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004117 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004118
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004119#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004120 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004121 {
4122 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004123 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004124 memcpy( keys, slot->data.key.data, 16 );
4125 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004126 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4127 keys,
4128 192, cipher_operation );
4129 }
4130 else
4131#endif
4132 {
4133 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004134 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004135 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004136 }
Moran Peker41deec42018-04-04 15:43:05 +03004137 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004138 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004139
mohammad16038481e742018-03-18 13:57:31 +02004140#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004141 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004142 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004143 case PSA_ALG_CBC_NO_PADDING:
4144 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4145 MBEDTLS_PADDING_NONE );
4146 break;
4147 case PSA_ALG_CBC_PKCS7:
4148 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4149 MBEDTLS_PADDING_PKCS7 );
4150 break;
4151 default:
4152 /* The algorithm doesn't involve padding. */
4153 ret = 0;
4154 break;
4155 }
4156 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004157 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004158#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4159
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004160 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004161 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004162 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4163 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004164 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004165 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004166 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004167#if defined(MBEDTLS_CHACHA20_C)
4168 else
4169 if( alg == PSA_ALG_CHACHA20 )
4170 operation->iv_size = 12;
4171#endif
mohammad1603503973b2018-03-12 15:59:30 +02004172
Steven Cooreman7df02922020-09-09 15:28:49 +02004173 status = PSA_SUCCESS;
4174
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004175exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004176 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004177 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004178 if( status == PSA_SUCCESS )
4179 {
4180 /* Update operation flags for both driver and software implementations */
4181 operation->key_set = 1;
4182 }
4183 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004184 psa_cipher_abort( operation );
4185 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004186}
4187
Gilles Peskinefe119512018-07-08 21:39:34 +02004188psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004189 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004190 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004191{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004192 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004193}
4194
Gilles Peskinefe119512018-07-08 21:39:34 +02004195psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004196 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004197 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004198{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004199 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004200}
4201
Gilles Peskinefe119512018-07-08 21:39:34 +02004202psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004203 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004204 size_t iv_size,
4205 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004206{
itayzafrir534bd7c2018-08-02 13:56:32 +03004207 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004208 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004209 if( operation->iv_set || ! operation->iv_required )
4210 {
4211 return( PSA_ERROR_BAD_STATE );
4212 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004213
Steven Cooremanef8575e2020-09-11 11:44:50 +02004214 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004215 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004216 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004217 iv,
4218 iv_size,
4219 iv_length );
4220 goto exit;
4221 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004222
Moran Peker41deec42018-04-04 15:43:05 +03004223 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004224 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004225 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004226 goto exit;
4227 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004228 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4229 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004230 if( ret != 0 )
4231 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004232 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004233 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004234 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004235
mohammad16038481e742018-03-18 13:57:31 +02004236 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004237 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004238
Moran Peker395db872018-05-31 14:07:14 +03004239exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004240 if( status == PSA_SUCCESS )
4241 operation->iv_set = 1;
4242 else
Moran Peker395db872018-05-31 14:07:14 +03004243 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004244 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004245}
4246
Gilles Peskinefe119512018-07-08 21:39:34 +02004247psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004248 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004249 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004250{
itayzafrir534bd7c2018-08-02 13:56:32 +03004251 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004252 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004253 if( operation->iv_set || ! operation->iv_required )
4254 {
4255 return( PSA_ERROR_BAD_STATE );
4256 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004257
Steven Cooremanef8575e2020-09-11 11:44:50 +02004258 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004259 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004260 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004261 iv,
4262 iv_length );
4263 goto exit;
4264 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004265
Moran Pekera28258c2018-05-29 16:25:04 +03004266 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004267 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004268 status = PSA_ERROR_INVALID_ARGUMENT;
4269 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004270 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004271 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4272 status = mbedtls_to_psa_error( ret );
4273exit:
4274 if( status == PSA_SUCCESS )
4275 operation->iv_set = 1;
4276 else
mohammad1603503973b2018-03-12 15:59:30 +02004277 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004278 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004279}
4280
Steven Cooreman177deba2020-09-07 17:14:14 +02004281/* Process input for which the algorithm is set to ECB mode. This requires
4282 * manual processing, since the PSA API is defined as being able to process
4283 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4284 * underlying mbedtls_cipher_update only takes full blocks. */
4285static psa_status_t psa_cipher_update_ecb_internal(
4286 mbedtls_cipher_context_t *ctx,
4287 const uint8_t *input,
4288 size_t input_length,
4289 uint8_t *output,
4290 size_t output_size,
4291 size_t *output_length )
4292{
4293 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4294 size_t block_size = ctx->cipher_info->block_size;
4295 size_t internal_output_length = 0;
4296 *output_length = 0;
4297
4298 if( input_length == 0 )
4299 {
4300 status = PSA_SUCCESS;
4301 goto exit;
4302 }
4303
4304 if( ctx->unprocessed_len > 0 )
4305 {
4306 /* Fill up to block size, and run the block if there's a full one. */
4307 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4308
4309 if( input_length < bytes_to_copy )
4310 bytes_to_copy = input_length;
4311
4312 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4313 input, bytes_to_copy );
4314 input_length -= bytes_to_copy;
4315 input += bytes_to_copy;
4316 ctx->unprocessed_len += bytes_to_copy;
4317
4318 if( ctx->unprocessed_len == block_size )
4319 {
4320 status = mbedtls_to_psa_error(
4321 mbedtls_cipher_update( ctx,
4322 ctx->unprocessed_data,
4323 block_size,
4324 output, &internal_output_length ) );
4325
4326 if( status != PSA_SUCCESS )
4327 goto exit;
4328
4329 output += internal_output_length;
4330 output_size -= internal_output_length;
4331 *output_length += internal_output_length;
4332 ctx->unprocessed_len = 0;
4333 }
4334 }
4335
4336 while( input_length >= block_size )
4337 {
4338 /* Run all full blocks we have, one by one */
4339 status = mbedtls_to_psa_error(
4340 mbedtls_cipher_update( ctx, input,
4341 block_size,
4342 output, &internal_output_length ) );
4343
4344 if( status != PSA_SUCCESS )
4345 goto exit;
4346
4347 input_length -= block_size;
4348 input += block_size;
4349
4350 output += internal_output_length;
4351 output_size -= internal_output_length;
4352 *output_length += internal_output_length;
4353 }
4354
4355 if( input_length > 0 )
4356 {
4357 /* Save unprocessed bytes for later processing */
4358 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4359 input, input_length );
4360 ctx->unprocessed_len += input_length;
4361 }
4362
4363 status = PSA_SUCCESS;
4364
4365exit:
4366 return( status );
4367}
4368
Gilles Peskinee553c652018-06-04 16:22:46 +02004369psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4370 const uint8_t *input,
4371 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004372 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004373 size_t output_size,
4374 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004375{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004376 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004377 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004378 if( operation->alg == 0 )
4379 {
4380 return( PSA_ERROR_BAD_STATE );
4381 }
4382 if( operation->iv_required && ! operation->iv_set )
4383 {
4384 return( PSA_ERROR_BAD_STATE );
4385 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004386
Steven Cooremanef8575e2020-09-11 11:44:50 +02004387 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004388 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004389 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004390 input,
4391 input_length,
4392 output,
4393 output_size,
4394 output_length );
4395 goto exit;
4396 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004397
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004398 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004399 {
4400 /* Take the unprocessed partial block left over from previous
4401 * update calls, if any, plus the input to this call. Remove
4402 * the last partial block, if any. You get the data that will be
4403 * output in this call. */
4404 expected_output_size =
4405 ( operation->ctx.cipher.unprocessed_len + input_length )
4406 / operation->block_size * operation->block_size;
4407 }
4408 else
4409 {
4410 expected_output_size = input_length;
4411 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004412
Gilles Peskine89d789c2018-06-04 17:17:16 +02004413 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004414 {
4415 status = PSA_ERROR_BUFFER_TOO_SMALL;
4416 goto exit;
4417 }
mohammad160382759612018-03-12 18:16:40 +02004418
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004419 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4420 {
4421 /* mbedtls_cipher_update has an API inconsistency: it will only
4422 * process a single block at a time in ECB mode. Abstract away that
4423 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004424 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4425 input,
4426 input_length,
4427 output,
4428 output_size,
4429 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004430 }
4431 else
4432 {
4433 status = mbedtls_to_psa_error(
4434 mbedtls_cipher_update( &operation->ctx.cipher, input,
4435 input_length, output, output_length ) );
4436 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004437exit:
4438 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004439 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004440 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004441}
4442
Gilles Peskinee553c652018-06-04 16:22:46 +02004443psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4444 uint8_t *output,
4445 size_t output_size,
4446 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004447{
David Saadab4ecc272019-02-14 13:48:10 +02004448 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004449 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004450 if( operation->alg == 0 )
4451 {
4452 return( PSA_ERROR_BAD_STATE );
4453 }
4454 if( operation->iv_required && ! operation->iv_set )
4455 {
4456 return( PSA_ERROR_BAD_STATE );
4457 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004458
Steven Cooremanef8575e2020-09-11 11:44:50 +02004459 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004460 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004461 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004462 output,
4463 output_size,
4464 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004465 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004466 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004467
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004468 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004469 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004470 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004471 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004472 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004473 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004474 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004475 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004476 }
4477
Steven Cooremanef8575e2020-09-11 11:44:50 +02004478 status = mbedtls_to_psa_error(
4479 mbedtls_cipher_finish( &operation->ctx.cipher,
4480 temp_output_buffer,
4481 output_length ) );
4482 if( status != PSA_SUCCESS )
4483 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004484
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004485 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004486 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004487 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004488 memcpy( output, temp_output_buffer, *output_length );
4489 else
Janos Follath315b51c2018-07-09 16:04:51 +01004490 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004491
Steven Cooremanef8575e2020-09-11 11:44:50 +02004492exit:
4493 if( operation->mbedtls_in_use == 1 )
4494 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004495
Steven Cooremanef8575e2020-09-11 11:44:50 +02004496 if( status == PSA_SUCCESS )
4497 return( psa_cipher_abort( operation ) );
4498 else
4499 {
4500 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004501 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004502
Steven Cooremanef8575e2020-09-11 11:44:50 +02004503 return( status );
4504 }
mohammad1603503973b2018-03-12 15:59:30 +02004505}
4506
Gilles Peskinee553c652018-06-04 16:22:46 +02004507psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4508{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004509 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004510 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004511 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004512 * in use. It's ok to call abort on such an object, and there's
4513 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004514 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004515 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004516
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004517 /* Sanity check (shouldn't happen: operation->alg should
4518 * always have been initialized to a valid value). */
4519 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4520 return( PSA_ERROR_BAD_STATE );
4521
Steven Cooremanef8575e2020-09-11 11:44:50 +02004522 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004523 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004524 else
4525 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004526
Moran Peker41deec42018-04-04 15:43:05 +03004527 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004528 operation->key_set = 0;
4529 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004530 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004531 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004532 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004533 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004534
Moran Peker395db872018-05-31 14:07:14 +03004535 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004536}
4537
Gilles Peskinea0655c32018-04-30 17:06:50 +02004538
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004539
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004540
mohammad16035955c982018-04-26 00:53:03 +03004541/****************************************************************/
4542/* AEAD */
4543/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004544
Gilles Peskineedf9a652018-08-17 18:11:56 +02004545typedef struct
4546{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004547 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004548 const mbedtls_cipher_info_t *cipher_info;
4549 union
4550 {
4551#if defined(MBEDTLS_CCM_C)
4552 mbedtls_ccm_context ccm;
4553#endif /* MBEDTLS_CCM_C */
4554#if defined(MBEDTLS_GCM_C)
4555 mbedtls_gcm_context gcm;
4556#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004557#if defined(MBEDTLS_CHACHAPOLY_C)
4558 mbedtls_chachapoly_context chachapoly;
4559#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004560 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004561 psa_algorithm_t core_alg;
4562 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004563 uint8_t tag_length;
4564} aead_operation_t;
4565
Gilles Peskine30a9e412019-01-14 18:36:12 +01004566static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004567{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004568 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004569 {
4570#if defined(MBEDTLS_CCM_C)
4571 case PSA_ALG_CCM:
4572 mbedtls_ccm_free( &operation->ctx.ccm );
4573 break;
4574#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004575#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004576 case PSA_ALG_GCM:
4577 mbedtls_gcm_free( &operation->ctx.gcm );
4578 break;
4579#endif /* MBEDTLS_GCM_C */
4580 }
4581}
4582
4583static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004584 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004585 psa_key_usage_t usage,
4586 psa_algorithm_t alg )
4587{
4588 psa_status_t status;
4589 size_t key_bits;
4590 mbedtls_cipher_id_t cipher_id;
4591
Gilles Peskine28f8f302019-07-24 13:30:31 +02004592 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004593 if( status != PSA_SUCCESS )
4594 return( status );
4595
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004596 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004597
4598 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004599 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004600 &cipher_id );
4601 if( operation->cipher_info == NULL )
4602 return( PSA_ERROR_NOT_SUPPORTED );
4603
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004604 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004605 {
4606#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004607 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4608 operation->core_alg = PSA_ALG_CCM;
4609 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004610 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4611 * The call to mbedtls_ccm_encrypt_and_tag or
4612 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004613 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004614 return( PSA_ERROR_INVALID_ARGUMENT );
4615 mbedtls_ccm_init( &operation->ctx.ccm );
4616 status = mbedtls_to_psa_error(
4617 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004618 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004619 (unsigned int) key_bits ) );
4620 if( status != 0 )
4621 goto cleanup;
4622 break;
4623#endif /* MBEDTLS_CCM_C */
4624
4625#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004626 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4627 operation->core_alg = PSA_ALG_GCM;
4628 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004629 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4630 * The call to mbedtls_gcm_crypt_and_tag or
4631 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004632 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004633 return( PSA_ERROR_INVALID_ARGUMENT );
4634 mbedtls_gcm_init( &operation->ctx.gcm );
4635 status = mbedtls_to_psa_error(
4636 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004637 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004638 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004639 if( status != 0 )
4640 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004641 break;
4642#endif /* MBEDTLS_GCM_C */
4643
Gilles Peskine26869f22019-05-06 15:25:00 +02004644#if defined(MBEDTLS_CHACHAPOLY_C)
4645 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4646 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4647 operation->full_tag_length = 16;
4648 /* We only support the default tag length. */
4649 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4650 return( PSA_ERROR_NOT_SUPPORTED );
4651 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4652 status = mbedtls_to_psa_error(
4653 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004654 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004655 if( status != 0 )
4656 goto cleanup;
4657 break;
4658#endif /* MBEDTLS_CHACHAPOLY_C */
4659
Gilles Peskineedf9a652018-08-17 18:11:56 +02004660 default:
4661 return( PSA_ERROR_NOT_SUPPORTED );
4662 }
4663
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004664 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4665 {
4666 status = PSA_ERROR_INVALID_ARGUMENT;
4667 goto cleanup;
4668 }
4669 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004670
Gilles Peskineedf9a652018-08-17 18:11:56 +02004671 return( PSA_SUCCESS );
4672
4673cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004674 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004675 return( status );
4676}
4677
Gilles Peskinec5487a82018-12-03 18:08:14 +01004678psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004679 psa_algorithm_t alg,
4680 const uint8_t *nonce,
4681 size_t nonce_length,
4682 const uint8_t *additional_data,
4683 size_t additional_data_length,
4684 const uint8_t *plaintext,
4685 size_t plaintext_length,
4686 uint8_t *ciphertext,
4687 size_t ciphertext_size,
4688 size_t *ciphertext_length )
4689{
mohammad16035955c982018-04-26 00:53:03 +03004690 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004691 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004692 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004693
mohammad1603f08a5502018-06-03 15:05:47 +03004694 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004695
Gilles Peskinec5487a82018-12-03 18:08:14 +01004696 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004697 if( status != PSA_SUCCESS )
4698 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004699
Gilles Peskineedf9a652018-08-17 18:11:56 +02004700 /* For all currently supported modes, the tag is at the end of the
4701 * ciphertext. */
4702 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4703 {
4704 status = PSA_ERROR_BUFFER_TOO_SMALL;
4705 goto exit;
4706 }
4707 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004708
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004709#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004710 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004711 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004712 status = mbedtls_to_psa_error(
4713 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4714 MBEDTLS_GCM_ENCRYPT,
4715 plaintext_length,
4716 nonce, nonce_length,
4717 additional_data, additional_data_length,
4718 plaintext, ciphertext,
4719 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004720 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004721 else
4722#endif /* MBEDTLS_GCM_C */
4723#if defined(MBEDTLS_CCM_C)
4724 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004725 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004726 status = mbedtls_to_psa_error(
4727 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4728 plaintext_length,
4729 nonce, nonce_length,
4730 additional_data,
4731 additional_data_length,
4732 plaintext, ciphertext,
4733 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004734 }
mohammad16035c8845f2018-05-09 05:40:09 -07004735 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004736#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004737#if defined(MBEDTLS_CHACHAPOLY_C)
4738 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4739 {
4740 if( nonce_length != 12 || operation.tag_length != 16 )
4741 {
4742 status = PSA_ERROR_NOT_SUPPORTED;
4743 goto exit;
4744 }
4745 status = mbedtls_to_psa_error(
4746 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4747 plaintext_length,
4748 nonce,
4749 additional_data,
4750 additional_data_length,
4751 plaintext,
4752 ciphertext,
4753 tag ) );
4754 }
4755 else
4756#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004757 {
mohammad1603554faad2018-06-03 15:07:38 +03004758 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004759 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004760
Gilles Peskineedf9a652018-08-17 18:11:56 +02004761 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4762 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004763
Gilles Peskineedf9a652018-08-17 18:11:56 +02004764exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004765 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004766 if( status == PSA_SUCCESS )
4767 *ciphertext_length = plaintext_length + operation.tag_length;
4768 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004769}
4770
Gilles Peskineee652a32018-06-01 19:23:52 +02004771/* Locate the tag in a ciphertext buffer containing the encrypted data
4772 * followed by the tag. Return the length of the part preceding the tag in
4773 * *plaintext_length. This is the size of the plaintext in modes where
4774 * the encrypted data has the same size as the plaintext, such as
4775 * CCM and GCM. */
4776static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4777 const uint8_t *ciphertext,
4778 size_t ciphertext_length,
4779 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004780 const uint8_t **p_tag )
4781{
4782 size_t payload_length;
4783 if( tag_length > ciphertext_length )
4784 return( PSA_ERROR_INVALID_ARGUMENT );
4785 payload_length = ciphertext_length - tag_length;
4786 if( payload_length > plaintext_size )
4787 return( PSA_ERROR_BUFFER_TOO_SMALL );
4788 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004789 return( PSA_SUCCESS );
4790}
4791
Gilles Peskinec5487a82018-12-03 18:08:14 +01004792psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004793 psa_algorithm_t alg,
4794 const uint8_t *nonce,
4795 size_t nonce_length,
4796 const uint8_t *additional_data,
4797 size_t additional_data_length,
4798 const uint8_t *ciphertext,
4799 size_t ciphertext_length,
4800 uint8_t *plaintext,
4801 size_t plaintext_size,
4802 size_t *plaintext_length )
4803{
mohammad16035955c982018-04-26 00:53:03 +03004804 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004805 aead_operation_t operation;
4806 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004807
Gilles Peskineee652a32018-06-01 19:23:52 +02004808 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004809
Gilles Peskinec5487a82018-12-03 18:08:14 +01004810 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004811 if( status != PSA_SUCCESS )
4812 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004813
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004814 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4815 ciphertext, ciphertext_length,
4816 plaintext_size, &tag );
4817 if( status != PSA_SUCCESS )
4818 goto exit;
4819
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004820#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004821 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004822 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004823 status = mbedtls_to_psa_error(
4824 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4825 ciphertext_length - operation.tag_length,
4826 nonce, nonce_length,
4827 additional_data,
4828 additional_data_length,
4829 tag, operation.tag_length,
4830 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004831 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004832 else
4833#endif /* MBEDTLS_GCM_C */
4834#if defined(MBEDTLS_CCM_C)
4835 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004836 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004837 status = mbedtls_to_psa_error(
4838 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4839 ciphertext_length - operation.tag_length,
4840 nonce, nonce_length,
4841 additional_data,
4842 additional_data_length,
4843 ciphertext, plaintext,
4844 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004845 }
mohammad160339574652018-06-01 04:39:53 -07004846 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004847#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004848#if defined(MBEDTLS_CHACHAPOLY_C)
4849 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4850 {
4851 if( nonce_length != 12 || operation.tag_length != 16 )
4852 {
4853 status = PSA_ERROR_NOT_SUPPORTED;
4854 goto exit;
4855 }
4856 status = mbedtls_to_psa_error(
4857 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4858 ciphertext_length - operation.tag_length,
4859 nonce,
4860 additional_data,
4861 additional_data_length,
4862 tag,
4863 ciphertext,
4864 plaintext ) );
4865 }
4866 else
4867#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004868 {
mohammad1603554faad2018-06-03 15:07:38 +03004869 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004870 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004871
Gilles Peskineedf9a652018-08-17 18:11:56 +02004872 if( status != PSA_SUCCESS && plaintext_size != 0 )
4873 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004874
Gilles Peskineedf9a652018-08-17 18:11:56 +02004875exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004876 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004877 if( status == PSA_SUCCESS )
4878 *plaintext_length = ciphertext_length - operation.tag_length;
4879 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004880}
4881
Gilles Peskinea0655c32018-04-30 17:06:50 +02004882
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004883
Gilles Peskine20035e32018-02-03 22:44:14 +01004884/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004885/* Generators */
4886/****************************************************************/
4887
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004888#define HKDF_STATE_INIT 0 /* no input yet */
4889#define HKDF_STATE_STARTED 1 /* got salt */
4890#define HKDF_STATE_KEYED 2 /* got key */
4891#define HKDF_STATE_OUTPUT 3 /* output started */
4892
Gilles Peskinecbe66502019-05-16 16:59:18 +02004893static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004894 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004895{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004896 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4897 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004898 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004899 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004900}
4901
4902
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004903psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004904{
4905 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004907 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004908 {
4909 /* The object has (apparently) been initialized but it is not
4910 * in use. It's ok to call abort on such an object, and there's
4911 * nothing to do. */
4912 }
4913 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004914#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004915 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004916 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004917 mbedtls_free( operation->ctx.hkdf.info );
4918 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004919 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004920 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004921 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004922 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004923 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004924 if( operation->ctx.tls12_prf.seed != NULL )
4925 {
4926 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4927 operation->ctx.tls12_prf.seed_length );
4928 mbedtls_free( operation->ctx.tls12_prf.seed );
4929 }
4930
4931 if( operation->ctx.tls12_prf.label != NULL )
4932 {
4933 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4934 operation->ctx.tls12_prf.label_length );
4935 mbedtls_free( operation->ctx.tls12_prf.label );
4936 }
4937
4938 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4939
4940 /* We leave the fields Ai and output_block to be erased safely by the
4941 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004942 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004943 else
4944#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004945 {
4946 status = PSA_ERROR_BAD_STATE;
4947 }
Janos Follath083036a2019-06-11 10:22:26 +01004948 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004949 return( status );
4950}
4951
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004952psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004953 size_t *capacity)
4954{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004955 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004956 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004957 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004958 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004959 }
4960
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004961 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004962 return( PSA_SUCCESS );
4963}
4964
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004965psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004966 size_t capacity )
4967{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004968 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004969 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004971 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004972 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004973 return( PSA_SUCCESS );
4974}
4975
Gilles Peskinebef7f142018-07-12 17:22:21 +02004976#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004977/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004978 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004979static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004980 psa_algorithm_t hash_alg,
4981 uint8_t *output,
4982 size_t output_length )
4983{
4984 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4985 psa_status_t status;
4986
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004987 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4988 return( PSA_ERROR_BAD_STATE );
4989 hkdf->state = HKDF_STATE_OUTPUT;
4990
Gilles Peskinebef7f142018-07-12 17:22:21 +02004991 while( output_length != 0 )
4992 {
4993 /* Copy what remains of the current block */
4994 uint8_t n = hash_length - hkdf->offset_in_block;
4995 if( n > output_length )
4996 n = (uint8_t) output_length;
4997 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4998 output += n;
4999 output_length -= n;
5000 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005001 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005002 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005003 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005004 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005005 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005006 * object was corrupted or if this function is called directly
5007 * inside the library. */
5008 if( hkdf->block_number == 0xff )
5009 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005010
5011 /* We need a new block */
5012 ++hkdf->block_number;
5013 hkdf->offset_in_block = 0;
5014 status = psa_hmac_setup_internal( &hkdf->hmac,
5015 hkdf->prk, hash_length,
5016 hash_alg );
5017 if( status != PSA_SUCCESS )
5018 return( status );
5019 if( hkdf->block_number != 1 )
5020 {
5021 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5022 hkdf->output_block,
5023 hash_length );
5024 if( status != PSA_SUCCESS )
5025 return( status );
5026 }
5027 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5028 hkdf->info,
5029 hkdf->info_length );
5030 if( status != PSA_SUCCESS )
5031 return( status );
5032 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5033 &hkdf->block_number, 1 );
5034 if( status != PSA_SUCCESS )
5035 return( status );
5036 status = psa_hmac_finish_internal( &hkdf->hmac,
5037 hkdf->output_block,
5038 sizeof( hkdf->output_block ) );
5039 if( status != PSA_SUCCESS )
5040 return( status );
5041 }
5042
5043 return( PSA_SUCCESS );
5044}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005045
Janos Follath7742fee2019-06-17 12:58:10 +01005046static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5047 psa_tls12_prf_key_derivation_t *tls12_prf,
5048 psa_algorithm_t alg )
5049{
5050 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5051 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005052 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5053 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005054
Janos Follath7742fee2019-06-17 12:58:10 +01005055 /* We can't be wanting more output after block 0xff, otherwise
5056 * the capacity check in psa_key_derivation_output_bytes() would have
5057 * prevented this call. It could happen only if the operation
5058 * object was corrupted or if this function is called directly
5059 * inside the library. */
5060 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005061 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005062
5063 /* We need a new block */
5064 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005065 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005066
5067 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5068 *
5069 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5070 *
5071 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5072 * HMAC_hash(secret, A(2) + seed) +
5073 * HMAC_hash(secret, A(3) + seed) + ...
5074 *
5075 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005076 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005077 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005078 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005079 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005080 * is currently extracted as `output_block` and where i is
5081 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005082 */
5083
Janos Follathea29bfb2019-06-19 12:21:20 +01005084 /* Save the hash context before using it, to preserve the hash state with
5085 * only the inner padding in it. We need this, because inner padding depends
5086 * on the key (secret in the RFC's terminology). */
5087 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5088 if( status != PSA_SUCCESS )
5089 goto cleanup;
5090
5091 /* Calculate A(i) where i = tls12_prf->block_number. */
5092 if( tls12_prf->block_number == 1 )
5093 {
5094 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5095 * the variable seed and in this instance means it in the context of the
5096 * P_hash function, where seed = label + seed.) */
5097 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5098 tls12_prf->label, tls12_prf->label_length );
5099 if( status != PSA_SUCCESS )
5100 goto cleanup;
5101 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5102 tls12_prf->seed, tls12_prf->seed_length );
5103 if( status != PSA_SUCCESS )
5104 goto cleanup;
5105 }
5106 else
5107 {
5108 /* A(i) = HMAC_hash(secret, A(i-1)) */
5109 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5110 tls12_prf->Ai, hash_length );
5111 if( status != PSA_SUCCESS )
5112 goto cleanup;
5113 }
5114
5115 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5116 tls12_prf->Ai, hash_length );
5117 if( status != PSA_SUCCESS )
5118 goto cleanup;
5119 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5120 if( status != PSA_SUCCESS )
5121 goto cleanup;
5122
5123 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5124 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5125 tls12_prf->Ai, hash_length );
5126 if( status != PSA_SUCCESS )
5127 goto cleanup;
5128 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5129 tls12_prf->label, tls12_prf->label_length );
5130 if( status != PSA_SUCCESS )
5131 goto cleanup;
5132 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5133 tls12_prf->seed, tls12_prf->seed_length );
5134 if( status != PSA_SUCCESS )
5135 goto cleanup;
5136 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5137 tls12_prf->output_block, hash_length );
5138 if( status != PSA_SUCCESS )
5139 goto cleanup;
5140 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5141 if( status != PSA_SUCCESS )
5142 goto cleanup;
5143
Janos Follath7742fee2019-06-17 12:58:10 +01005144
5145cleanup:
5146
Janos Follathea29bfb2019-06-19 12:21:20 +01005147 cleanup_status = psa_hash_abort( &backup );
5148 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5149 status = cleanup_status;
5150
5151 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005152}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005153
Janos Follath844eb0e2019-06-19 12:10:49 +01005154static psa_status_t psa_key_derivation_tls12_prf_read(
5155 psa_tls12_prf_key_derivation_t *tls12_prf,
5156 psa_algorithm_t alg,
5157 uint8_t *output,
5158 size_t output_length )
5159{
5160 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5161 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5162 psa_status_t status;
5163 uint8_t offset, length;
5164
5165 while( output_length != 0 )
5166 {
5167 /* Check if we have fully processed the current block. */
5168 if( tls12_prf->left_in_block == 0 )
5169 {
5170 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5171 alg );
5172 if( status != PSA_SUCCESS )
5173 return( status );
5174
5175 continue;
5176 }
5177
5178 if( tls12_prf->left_in_block > output_length )
5179 length = (uint8_t) output_length;
5180 else
5181 length = tls12_prf->left_in_block;
5182
5183 offset = hash_length - tls12_prf->left_in_block;
5184 memcpy( output, tls12_prf->output_block + offset, length );
5185 output += length;
5186 output_length -= length;
5187 tls12_prf->left_in_block -= length;
5188 }
5189
5190 return( PSA_SUCCESS );
5191}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005192#endif /* MBEDTLS_MD_C */
5193
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005194psa_status_t psa_key_derivation_output_bytes(
5195 psa_key_derivation_operation_t *operation,
5196 uint8_t *output,
5197 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005198{
5199 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005200 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005201
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005202 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005203 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005204 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005205 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005206 }
5207
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005209 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005210 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005211 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005212 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005213 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005214 goto exit;
5215 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005216 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005217 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005218 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005219 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005220 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5221 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005222 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005223 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005224 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005225 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005226 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005227
Gilles Peskinebef7f142018-07-12 17:22:21 +02005228#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005229 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005230 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005231 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005232 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005233 output, output_length );
5234 }
Janos Follathadbec812019-06-14 11:05:39 +01005235 else
Janos Follathadbec812019-06-14 11:05:39 +01005236 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005237 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005238 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005239 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005240 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005241 output_length );
5242 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005243 else
5244#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005245 {
5246 return( PSA_ERROR_BAD_STATE );
5247 }
5248
5249exit:
5250 if( status != PSA_SUCCESS )
5251 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005252 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005253 * This allows us to differentiate between exhausted operations and
5254 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5255 * operations. */
5256 psa_algorithm_t alg = operation->alg;
5257 psa_key_derivation_abort( operation );
5258 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005259 memset( output, '!', output_length );
5260 }
5261 return( status );
5262}
5263
Gilles Peskine08542d82018-07-19 17:05:42 +02005264#if defined(MBEDTLS_DES_C)
5265static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5266{
5267 if( data_size >= 8 )
5268 mbedtls_des_key_set_parity( data );
5269 if( data_size >= 16 )
5270 mbedtls_des_key_set_parity( data + 8 );
5271 if( data_size >= 24 )
5272 mbedtls_des_key_set_parity( data + 16 );
5273}
5274#endif /* MBEDTLS_DES_C */
5275
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005276static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005277 psa_key_slot_t *slot,
5278 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005279 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005280{
5281 uint8_t *data = NULL;
5282 size_t bytes = PSA_BITS_TO_BYTES( bits );
5283 psa_status_t status;
5284
Gilles Peskine8e338702019-07-30 20:06:31 +02005285 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005286 return( PSA_ERROR_INVALID_ARGUMENT );
5287 if( bits % 8 != 0 )
5288 return( PSA_ERROR_INVALID_ARGUMENT );
5289 data = mbedtls_calloc( 1, bytes );
5290 if( data == NULL )
5291 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5292
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005293 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005294 if( status != PSA_SUCCESS )
5295 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005296#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005297 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005298 psa_des_set_key_parity( data, bytes );
5299#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005300 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005301
5302exit:
5303 mbedtls_free( data );
5304 return( status );
5305}
5306
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005307psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005308 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005309 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005310{
5311 psa_status_t status;
5312 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005313 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005314
5315 /* Reject any attempt to create a zero-length key so that we don't
5316 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5317 if( psa_get_key_bits( attributes ) == 0 )
5318 return( PSA_ERROR_INVALID_ARGUMENT );
5319
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005320 if( ! operation->can_output_key )
5321 return( PSA_ERROR_NOT_PERMITTED );
5322
Gilles Peskinedf179142019-07-15 22:02:14 +02005323 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5324 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005325#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5326 if( driver != NULL )
5327 {
5328 /* Deriving a key in a secure element is not implemented yet. */
5329 status = PSA_ERROR_NOT_SUPPORTED;
5330 }
5331#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005332 if( status == PSA_SUCCESS )
5333 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005334 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005335 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005336 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005337 }
5338 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005339 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005340 if( status != PSA_SUCCESS )
5341 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005342 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005343 *handle = 0;
5344 }
5345 return( status );
5346}
5347
Gilles Peskineeab56e42018-07-12 17:12:33 +02005348
5349
5350/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005351/* Key derivation */
5352/****************************************************************/
5353
Gilles Peskine969c5d62019-01-16 15:53:06 +01005354static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005355 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005356 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005357{
Janos Follath5fe19732019-06-20 15:09:30 +01005358 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5359 * initialisers for this union leave some bytes unspecified.) */
5360 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5361
Gilles Peskine969c5d62019-01-16 15:53:06 +01005362 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005363#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005364 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5365 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5366 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005367 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005368 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005369 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5370 if( hash_size == 0 )
5371 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005372 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5373 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005374 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005375 {
5376 return( PSA_ERROR_NOT_SUPPORTED );
5377 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005378 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005379 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005380 }
5381#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005382 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005383 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005384}
5385
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005386psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005387 psa_algorithm_t alg )
5388{
5389 psa_status_t status;
5390
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005391 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005392 return( PSA_ERROR_BAD_STATE );
5393
Gilles Peskine6843c292019-01-18 16:44:49 +01005394 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5395 return( PSA_ERROR_INVALID_ARGUMENT );
5396 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005397 {
5398 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005399 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005400 }
5401 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5402 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005403 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005404 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005405 else
5406 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005407
5408 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005409 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005410 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005411}
5412
5413#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005414static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005415 psa_algorithm_t hash_alg,
5416 psa_key_derivation_step_t step,
5417 const uint8_t *data,
5418 size_t data_length )
5419{
5420 psa_status_t status;
5421 switch( step )
5422 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005423 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005424 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005425 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005426 status = psa_hmac_setup_internal( &hkdf->hmac,
5427 data, data_length,
5428 hash_alg );
5429 if( status != PSA_SUCCESS )
5430 return( status );
5431 hkdf->state = HKDF_STATE_STARTED;
5432 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005433 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005434 /* If no salt was provided, use an empty salt. */
5435 if( hkdf->state == HKDF_STATE_INIT )
5436 {
5437 status = psa_hmac_setup_internal( &hkdf->hmac,
5438 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005439 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005440 if( status != PSA_SUCCESS )
5441 return( status );
5442 hkdf->state = HKDF_STATE_STARTED;
5443 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005444 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005445 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005446 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5447 data, data_length );
5448 if( status != PSA_SUCCESS )
5449 return( status );
5450 status = psa_hmac_finish_internal( &hkdf->hmac,
5451 hkdf->prk,
5452 sizeof( hkdf->prk ) );
5453 if( status != PSA_SUCCESS )
5454 return( status );
5455 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5456 hkdf->block_number = 0;
5457 hkdf->state = HKDF_STATE_KEYED;
5458 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005459 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005460 if( hkdf->state == HKDF_STATE_OUTPUT )
5461 return( PSA_ERROR_BAD_STATE );
5462 if( hkdf->info_set )
5463 return( PSA_ERROR_BAD_STATE );
5464 hkdf->info_length = data_length;
5465 if( data_length != 0 )
5466 {
5467 hkdf->info = mbedtls_calloc( 1, data_length );
5468 if( hkdf->info == NULL )
5469 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5470 memcpy( hkdf->info, data, data_length );
5471 }
5472 hkdf->info_set = 1;
5473 return( PSA_SUCCESS );
5474 default:
5475 return( PSA_ERROR_INVALID_ARGUMENT );
5476 }
5477}
Janos Follathb03233e2019-06-11 15:30:30 +01005478
Janos Follathf08e2652019-06-13 09:05:41 +01005479static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5480 const uint8_t *data,
5481 size_t data_length )
5482{
5483 if( prf->state != TLS12_PRF_STATE_INIT )
5484 return( PSA_ERROR_BAD_STATE );
5485
Janos Follathd6dce9f2019-07-04 09:11:38 +01005486 if( data_length != 0 )
5487 {
5488 prf->seed = mbedtls_calloc( 1, data_length );
5489 if( prf->seed == NULL )
5490 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005491
Janos Follathd6dce9f2019-07-04 09:11:38 +01005492 memcpy( prf->seed, data, data_length );
5493 prf->seed_length = data_length;
5494 }
Janos Follathf08e2652019-06-13 09:05:41 +01005495
5496 prf->state = TLS12_PRF_STATE_SEED_SET;
5497
5498 return( PSA_SUCCESS );
5499}
5500
Janos Follath81550542019-06-13 14:26:34 +01005501static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5502 psa_algorithm_t hash_alg,
5503 const uint8_t *data,
5504 size_t data_length )
5505{
5506 psa_status_t status;
5507 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5508 return( PSA_ERROR_BAD_STATE );
5509
5510 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5511 if( status != PSA_SUCCESS )
5512 return( status );
5513
5514 prf->state = TLS12_PRF_STATE_KEY_SET;
5515
5516 return( PSA_SUCCESS );
5517}
5518
Janos Follath6660f0e2019-06-17 08:44:03 +01005519static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5520 psa_tls12_prf_key_derivation_t *prf,
5521 psa_algorithm_t hash_alg,
5522 const uint8_t *data,
5523 size_t data_length )
5524{
5525 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005526 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5527 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005528
5529 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5530 return( PSA_ERROR_INVALID_ARGUMENT );
5531
5532 /* Quoting RFC 4279, Section 2:
5533 *
5534 * The premaster secret is formed as follows: if the PSK is N octets
5535 * long, concatenate a uint16 with the value N, N zero octets, a second
5536 * uint16 with the value N, and the PSK itself.
5537 */
5538
Janos Follath40e13932019-06-26 13:22:29 +01005539 *cur++ = ( data_length >> 8 ) & 0xff;
5540 *cur++ = ( data_length >> 0 ) & 0xff;
5541 memset( cur, 0, data_length );
5542 cur += data_length;
5543 *cur++ = pms[0];
5544 *cur++ = pms[1];
5545 memcpy( cur, data, data_length );
5546 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005547
Janos Follath40e13932019-06-26 13:22:29 +01005548 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005549
5550 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5551 return( status );
5552}
5553
Janos Follath63028dd2019-06-13 09:15:47 +01005554static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5555 const uint8_t *data,
5556 size_t data_length )
5557{
5558 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5559 return( PSA_ERROR_BAD_STATE );
5560
Janos Follathd6dce9f2019-07-04 09:11:38 +01005561 if( data_length != 0 )
5562 {
5563 prf->label = mbedtls_calloc( 1, data_length );
5564 if( prf->label == NULL )
5565 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005566
Janos Follathd6dce9f2019-07-04 09:11:38 +01005567 memcpy( prf->label, data, data_length );
5568 prf->label_length = data_length;
5569 }
Janos Follath63028dd2019-06-13 09:15:47 +01005570
5571 prf->state = TLS12_PRF_STATE_LABEL_SET;
5572
5573 return( PSA_SUCCESS );
5574}
5575
Janos Follathb03233e2019-06-11 15:30:30 +01005576static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5577 psa_algorithm_t hash_alg,
5578 psa_key_derivation_step_t step,
5579 const uint8_t *data,
5580 size_t data_length )
5581{
Janos Follathb03233e2019-06-11 15:30:30 +01005582 switch( step )
5583 {
Janos Follathf08e2652019-06-13 09:05:41 +01005584 case PSA_KEY_DERIVATION_INPUT_SEED:
5585 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005586 case PSA_KEY_DERIVATION_INPUT_SECRET:
5587 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005588 case PSA_KEY_DERIVATION_INPUT_LABEL:
5589 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005590 default:
5591 return( PSA_ERROR_INVALID_ARGUMENT );
5592 }
5593}
Janos Follath6660f0e2019-06-17 08:44:03 +01005594
5595static psa_status_t psa_tls12_prf_psk_to_ms_input(
5596 psa_tls12_prf_key_derivation_t *prf,
5597 psa_algorithm_t hash_alg,
5598 psa_key_derivation_step_t step,
5599 const uint8_t *data,
5600 size_t data_length )
5601{
5602 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005603 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005604 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5605 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005606 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005607
5608 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5609}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005610#endif /* MBEDTLS_MD_C */
5611
Gilles Peskineb8965192019-09-24 16:21:10 +02005612/** Check whether the given key type is acceptable for the given
5613 * input step of a key derivation.
5614 *
5615 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5616 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5617 * Both secret and non-secret inputs can alternatively have the type
5618 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5619 * that the input was passed as a buffer rather than via a key object.
5620 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005621static int psa_key_derivation_check_input_type(
5622 psa_key_derivation_step_t step,
5623 psa_key_type_t key_type )
5624{
5625 switch( step )
5626 {
5627 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005628 if( key_type == PSA_KEY_TYPE_DERIVE )
5629 return( PSA_SUCCESS );
5630 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005631 return( PSA_SUCCESS );
5632 break;
5633 case PSA_KEY_DERIVATION_INPUT_LABEL:
5634 case PSA_KEY_DERIVATION_INPUT_SALT:
5635 case PSA_KEY_DERIVATION_INPUT_INFO:
5636 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005637 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5638 return( PSA_SUCCESS );
5639 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005640 return( PSA_SUCCESS );
5641 break;
5642 }
5643 return( PSA_ERROR_INVALID_ARGUMENT );
5644}
5645
Janos Follathb80a94e2019-06-12 15:54:46 +01005646static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005647 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005648 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005649 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005650 const uint8_t *data,
5651 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005652{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005653 psa_status_t status;
5654 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5655
5656 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005657 if( status != PSA_SUCCESS )
5658 goto exit;
5659
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005660#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005661 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005662 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005663 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005664 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005665 step, data, data_length );
5666 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005667 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005668 {
Janos Follathb03233e2019-06-11 15:30:30 +01005669 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5670 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5671 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005672 }
5673 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5674 {
5675 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5676 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5677 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005678 }
5679 else
5680#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005681 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005682 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005683 return( PSA_ERROR_BAD_STATE );
5684 }
5685
Gilles Peskine224b0d62019-09-23 18:13:17 +02005686exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005687 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005688 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005689 return( status );
5690}
5691
Janos Follath51f4a0f2019-06-14 11:35:55 +01005692psa_status_t psa_key_derivation_input_bytes(
5693 psa_key_derivation_operation_t *operation,
5694 psa_key_derivation_step_t step,
5695 const uint8_t *data,
5696 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005697{
Gilles Peskineb8965192019-09-24 16:21:10 +02005698 return( psa_key_derivation_input_internal( operation, step,
5699 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005700 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005701}
5702
Janos Follath51f4a0f2019-06-14 11:35:55 +01005703psa_status_t psa_key_derivation_input_key(
5704 psa_key_derivation_operation_t *operation,
5705 psa_key_derivation_step_t step,
5706 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005707{
5708 psa_key_slot_t *slot;
5709 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005710
Gilles Peskine28f8f302019-07-24 13:30:31 +02005711 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005712 PSA_KEY_USAGE_DERIVE,
5713 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005714 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005715 {
5716 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005717 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005718 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005719
5720 /* Passing a key object as a SECRET input unlocks the permission
5721 * to output to a key object. */
5722 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5723 operation->can_output_key = 1;
5724
Janos Follathb80a94e2019-06-12 15:54:46 +01005725 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005726 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005727 slot->data.key.data,
5728 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005729}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005730
5731
5732
5733/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005734/* Key agreement */
5735/****************************************************************/
5736
Gilles Peskinea05219c2018-11-16 16:02:56 +01005737#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005738static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5739 size_t peer_key_length,
5740 const mbedtls_ecp_keypair *our_key,
5741 uint8_t *shared_secret,
5742 size_t shared_secret_size,
5743 size_t *shared_secret_length )
5744{
Steven Cooremand4867872020-08-05 16:31:39 +02005745 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005746 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005747 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005748 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005749 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005750 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005751
Steven Cooreman4fed4552020-08-03 14:46:03 +02005752 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5753 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005754 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005755 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005756 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005757 goto exit;
5758
Jaeden Amero97271b32019-01-10 19:38:51 +00005759 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005760 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005761 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005762 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005763 status = mbedtls_to_psa_error(
5764 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5765 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005766 goto exit;
5767
Jaeden Amero97271b32019-01-10 19:38:51 +00005768 status = mbedtls_to_psa_error(
5769 mbedtls_ecdh_calc_secret( &ecdh,
5770 shared_secret_length,
5771 shared_secret, shared_secret_size,
5772 mbedtls_ctr_drbg_random,
5773 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005774 if( status != PSA_SUCCESS )
5775 goto exit;
5776 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5777 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005778
5779exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005780 if( status != PSA_SUCCESS )
5781 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005782 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005783 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005784 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005785
Jaeden Amero97271b32019-01-10 19:38:51 +00005786 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005787}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005788#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005789
Gilles Peskine01d718c2018-09-18 12:01:02 +02005790#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5791
Gilles Peskine0216fe12019-04-11 21:23:21 +02005792static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5793 psa_key_slot_t *private_key,
5794 const uint8_t *peer_key,
5795 size_t peer_key_length,
5796 uint8_t *shared_secret,
5797 size_t shared_secret_size,
5798 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005799{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005800 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005801 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005802#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005803 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005804 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005805 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005806 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005807 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005808 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005809 private_key->data.key.data,
5810 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005811 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005812 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005813 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005814 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005815 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005816 shared_secret, shared_secret_size,
5817 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005818 mbedtls_ecp_keypair_free( ecp );
5819 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005820 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005821#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005822 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005823 (void) private_key;
5824 (void) peer_key;
5825 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005826 (void) shared_secret;
5827 (void) shared_secret_size;
5828 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005829 return( PSA_ERROR_NOT_SUPPORTED );
5830 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005831}
5832
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005833/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005834 * to potentially free embedded data structures and wipe confidential data.
5835 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005836static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005837 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005838 psa_key_slot_t *private_key,
5839 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005840 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005841{
5842 psa_status_t status;
5843 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5844 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005845 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005846
5847 /* Step 1: run the secret agreement algorithm to generate the shared
5848 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005849 status = psa_key_agreement_raw_internal( ka_alg,
5850 private_key,
5851 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005852 shared_secret,
5853 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005854 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005855 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005856 goto exit;
5857
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005858 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005859 * the shared secret. A shared secret is permitted wherever a key
5860 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005861 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005862 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005863 shared_secret,
5864 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005865exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005866 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005867 return( status );
5868}
5869
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005870psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005871 psa_key_derivation_step_t step,
5872 psa_key_handle_t private_key,
5873 const uint8_t *peer_key,
5874 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005875{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005876 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005877 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005878 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005879 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005880 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005881 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005882 if( status != PSA_SUCCESS )
5883 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005884 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005885 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005886 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005887 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005888 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005889 else
5890 {
5891 /* If a private key has been added as SECRET, we allow the derived
5892 * key material to be used as a key in PSA Crypto. */
5893 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5894 operation->can_output_key = 1;
5895 }
Gilles Peskine346797d2018-11-16 16:05:06 +01005896 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005897}
5898
Gilles Peskinebe697d82019-05-16 18:00:41 +02005899psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5900 psa_key_handle_t private_key,
5901 const uint8_t *peer_key,
5902 size_t peer_key_length,
5903 uint8_t *output,
5904 size_t output_size,
5905 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005906{
5907 psa_key_slot_t *slot;
5908 psa_status_t status;
5909
5910 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5911 {
5912 status = PSA_ERROR_INVALID_ARGUMENT;
5913 goto exit;
5914 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005915 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005916 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005917 if( status != PSA_SUCCESS )
5918 goto exit;
5919
5920 status = psa_key_agreement_raw_internal( alg, slot,
5921 peer_key, peer_key_length,
5922 output, output_size,
5923 output_length );
5924
5925exit:
5926 if( status != PSA_SUCCESS )
5927 {
5928 /* If an error happens and is not handled properly, the output
5929 * may be used as a key to protect sensitive data. Arrange for such
5930 * a key to be random, which is likely to result in decryption or
5931 * verification errors. This is better than filling the buffer with
5932 * some constant data such as zeros, which would result in the data
5933 * being protected with a reproducible, easily knowable key.
5934 */
5935 psa_generate_random( output, output_size );
5936 *output_length = output_size;
5937 }
5938 return( status );
5939}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005940
5941
5942/****************************************************************/
5943/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005944/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005945
Gilles Peskine4c317f42018-07-12 01:24:09 +02005946psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005947 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005948{
Janos Follath24eed8d2019-11-22 13:21:35 +00005949 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005950 GUARD_MODULE_INITIALIZED;
5951
Gilles Peskinef181eca2019-08-07 13:49:00 +02005952 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5953 {
5954 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5955 output,
5956 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5957 if( ret != 0 )
5958 return( mbedtls_to_psa_error( ret ) );
5959 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5960 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5961 }
5962
Gilles Peskinee59236f2018-01-27 23:32:46 +01005963 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5964 return( mbedtls_to_psa_error( ret ) );
5965}
5966
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005967#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5968#include "mbedtls/entropy_poll.h"
5969
Gilles Peskine7228da22019-07-15 11:06:15 +02005970psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005971 size_t seed_size )
5972{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005973 if( global_data.initialized )
5974 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005975
5976 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5977 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5978 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5979 return( PSA_ERROR_INVALID_ARGUMENT );
5980
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005981 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005982}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005983#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005984
Gilles Peskinee56e8782019-04-26 17:34:02 +02005985#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5986static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5987 size_t domain_parameters_size,
5988 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005989{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005990 size_t i;
5991 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005992
Gilles Peskinee56e8782019-04-26 17:34:02 +02005993 if( domain_parameters_size == 0 )
5994 {
5995 *exponent = 65537;
5996 return( PSA_SUCCESS );
5997 }
5998
5999 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6000 * support values that fit in a 32-bit integer, which is larger than
6001 * int on just about every platform anyway. */
6002 if( domain_parameters_size > sizeof( acc ) )
6003 return( PSA_ERROR_NOT_SUPPORTED );
6004 for( i = 0; i < domain_parameters_size; i++ )
6005 acc = ( acc << 8 ) | domain_parameters[i];
6006 if( acc > INT_MAX )
6007 return( PSA_ERROR_NOT_SUPPORTED );
6008 *exponent = acc;
6009 return( PSA_SUCCESS );
6010}
6011#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6012
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006013static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006014 psa_key_slot_t *slot, size_t bits,
6015 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006016{
Gilles Peskine8e338702019-07-30 20:06:31 +02006017 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006018
Gilles Peskinee56e8782019-04-26 17:34:02 +02006019 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006020 return( PSA_ERROR_INVALID_ARGUMENT );
6021
Gilles Peskinee59236f2018-01-27 23:32:46 +01006022 if( key_type_is_raw_bytes( type ) )
6023 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006024 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006025
6026 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006027 if( status != PSA_SUCCESS )
6028 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006029
6030 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006031 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6032 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006033 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006034
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006035 status = psa_generate_random( slot->data.key.data,
6036 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006037 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006038 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006039
6040 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006041#if defined(MBEDTLS_DES_C)
6042 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006043 psa_des_set_key_parity( slot->data.key.data,
6044 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006045#endif /* MBEDTLS_DES_C */
6046 }
6047 else
6048
6049#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006050 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006051 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006052 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006053 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006054 int exponent;
6055 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006056 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6057 return( PSA_ERROR_NOT_SUPPORTED );
6058 /* Accept only byte-aligned keys, for the same reasons as
6059 * in psa_import_rsa_key(). */
6060 if( bits % 8 != 0 )
6061 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006062 status = psa_read_rsa_exponent( domain_parameters,
6063 domain_parameters_size,
6064 &exponent );
6065 if( status != PSA_SUCCESS )
6066 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006067 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6068 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006069 mbedtls_ctr_drbg_random,
6070 &global_data.ctr_drbg,
6071 (unsigned int) bits,
6072 exponent );
6073 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006074 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006075
6076 /* Make sure to always have an export representation available */
6077 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6078
Steven Cooreman75b74362020-07-28 14:30:13 +02006079 status = psa_allocate_buffer_to_slot( slot, bytes );
6080 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006081 {
6082 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006083 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006084 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006085
6086 status = psa_export_rsa_key( type,
6087 &rsa,
6088 slot->data.key.data,
6089 bytes,
6090 &slot->data.key.bytes );
6091 mbedtls_rsa_free( &rsa );
6092 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006093 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006094 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006095 }
6096 else
6097#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6098
6099#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006100 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006101 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006102 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006103 mbedtls_ecp_group_id grp_id =
6104 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006105 const mbedtls_ecp_curve_info *curve_info =
6106 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006107 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006108 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006109 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006110 return( PSA_ERROR_NOT_SUPPORTED );
6111 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6112 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006113 mbedtls_ecp_keypair_init( &ecp );
6114 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006115 mbedtls_ctr_drbg_random,
6116 &global_data.ctr_drbg );
6117 if( ret != 0 )
6118 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006119 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006120 return( mbedtls_to_psa_error( ret ) );
6121 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006122
6123
6124 /* Make sure to always have an export representation available */
6125 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006126 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6127 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006128 {
6129 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006130 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006131 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006132
6133 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006134 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6135
6136 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006137 if( status != PSA_SUCCESS ) {
6138 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006139 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006140 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006141 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006142 }
6143 else
6144#endif /* MBEDTLS_ECP_C */
Steven Cooreman29149862020-08-05 15:43:42 +02006145 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006146 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006147 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006148
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006149 return( PSA_SUCCESS );
6150}
Darryl Green0c6575a2018-11-07 16:05:30 +00006151
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006152psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006153 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006154{
6155 psa_status_t status;
6156 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006157 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006158
Gilles Peskine0f84d622019-09-12 19:03:13 +02006159 /* Reject any attempt to create a zero-length key so that we don't
6160 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6161 if( psa_get_key_bits( attributes ) == 0 )
6162 return( PSA_ERROR_INVALID_ARGUMENT );
6163
Gilles Peskinedf179142019-07-15 22:02:14 +02006164 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6165 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006166 if( status != PSA_SUCCESS )
6167 goto exit;
6168
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006169 status = psa_driver_wrapper_generate_key( attributes,
6170 slot );
6171 if( status != PSA_ERROR_NOT_SUPPORTED ||
6172 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6173 goto exit;
6174
6175 status = psa_generate_key_internal(
6176 slot, attributes->core.bits,
6177 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006178
6179exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006180 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006181 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006182 if( status != PSA_SUCCESS )
6183 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006184 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006185 *handle = 0;
6186 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006187 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006188}
6189
6190
Gilles Peskinee59236f2018-01-27 23:32:46 +01006191
6192/****************************************************************/
6193/* Module setup */
6194/****************************************************************/
6195
Gilles Peskine5e769522018-11-20 21:59:56 +01006196psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6197 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6198 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6199{
6200 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6201 return( PSA_ERROR_BAD_STATE );
6202 global_data.entropy_init = entropy_init;
6203 global_data.entropy_free = entropy_free;
6204 return( PSA_SUCCESS );
6205}
6206
Gilles Peskinee59236f2018-01-27 23:32:46 +01006207void mbedtls_psa_crypto_free( void )
6208{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006209 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006210 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6211 {
6212 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006213 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006214 }
6215 /* Wipe all remaining data, including configuration.
6216 * In particular, this sets all state indicator to the value
6217 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006218 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006219#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006220 /* Unregister all secure element drivers, so that we restart from
6221 * a pristine state. */
6222 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006223#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006224}
6225
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006226#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6227/** Recover a transaction that was interrupted by a power failure.
6228 *
6229 * This function is called during initialization, before psa_crypto_init()
6230 * returns. If this function returns a failure status, the initialization
6231 * fails.
6232 */
6233static psa_status_t psa_crypto_recover_transaction(
6234 const psa_crypto_transaction_t *transaction )
6235{
6236 switch( transaction->unknown.type )
6237 {
6238 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6239 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006240 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006241 * is implemented.
6242 * https://github.com/ARMmbed/mbed-crypto/issues/218
6243 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006244 default:
6245 /* We found an unsupported transaction in the storage.
6246 * We don't know what state the storage is in. Give up. */
6247 return( PSA_ERROR_STORAGE_FAILURE );
6248 }
6249}
6250#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6251
Gilles Peskinee59236f2018-01-27 23:32:46 +01006252psa_status_t psa_crypto_init( void )
6253{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006254 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006255 const unsigned char drbg_seed[] = "PSA";
6256
Gilles Peskinec6b69072018-11-20 21:42:52 +01006257 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006258 if( global_data.initialized != 0 )
6259 return( PSA_SUCCESS );
6260
Gilles Peskine5e769522018-11-20 21:59:56 +01006261 /* Set default configuration if
6262 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6263 if( global_data.entropy_init == NULL )
6264 global_data.entropy_init = mbedtls_entropy_init;
6265 if( global_data.entropy_free == NULL )
6266 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006267
Gilles Peskinec6b69072018-11-20 21:42:52 +01006268 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006269 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006270#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6271 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6272 /* The PSA entropy injection feature depends on using NV seed as an entropy
6273 * source. Add NV seed as an entropy source for PSA entropy injection. */
6274 mbedtls_entropy_add_source( &global_data.entropy,
6275 mbedtls_nv_seed_poll, NULL,
6276 MBEDTLS_ENTROPY_BLOCK_SIZE,
6277 MBEDTLS_ENTROPY_SOURCE_STRONG );
6278#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006279 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006280 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006281 status = mbedtls_to_psa_error(
6282 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6283 mbedtls_entropy_func,
6284 &global_data.entropy,
6285 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6286 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006287 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006288 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006289
Gilles Peskine66fb1262018-12-10 16:29:04 +01006290 status = psa_initialize_key_slots( );
6291 if( status != PSA_SUCCESS )
6292 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006293
Gilles Peskined9348f22019-10-01 15:22:29 +02006294#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6295 status = psa_init_all_se_drivers( );
6296 if( status != PSA_SUCCESS )
6297 goto exit;
6298#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6299
Gilles Peskine4b734222019-07-24 15:56:31 +02006300#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006301 status = psa_crypto_load_transaction( );
6302 if( status == PSA_SUCCESS )
6303 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006304 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6305 if( status != PSA_SUCCESS )
6306 goto exit;
6307 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006308 }
6309 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6310 {
6311 /* There's no transaction to complete. It's all good. */
6312 status = PSA_SUCCESS;
6313 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006314#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006315
Gilles Peskinec6b69072018-11-20 21:42:52 +01006316 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006317 global_data.initialized = 1;
6318
6319exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006320 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006321 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006322 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006323}
6324
6325#endif /* MBEDTLS_PSA_CRYPTO_C */