blob: 4efebbb5e976abbc64953a7f5e560b9abfafa17b [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:
202 return( PSA_ERROR_BAD_STATE );
203 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
205 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200206 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
208 return( PSA_ERROR_HARDWARE_FAILURE );
209
210 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
214 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
215 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
216 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
217 return( PSA_ERROR_NOT_SUPPORTED );
218 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
220
221 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
Gilles Peskinee59236f2018-01-27 23:32:46 +0100226 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
227 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
228 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
229 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100230
231 case MBEDTLS_ERR_GCM_AUTH_FAILED:
232 return( PSA_ERROR_INVALID_SIGNATURE );
233 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200234 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
243 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
244 return( PSA_ERROR_NOT_SUPPORTED );
245 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
246 return( PSA_ERROR_INVALID_ARGUMENT );
247 case MBEDTLS_ERR_MD_ALLOC_FAILED:
248 return( PSA_ERROR_INSUFFICIENT_MEMORY );
249 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
250 return( PSA_ERROR_STORAGE_FAILURE );
251 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
255 return( PSA_ERROR_STORAGE_FAILURE );
256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
261 return( PSA_ERROR_BUFFER_TOO_SMALL );
262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
269 return( PSA_ERROR_INSUFFICIENT_MEMORY );
270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
282 return( PSA_ERROR_NOT_SUPPORTED );
283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
285 return( PSA_ERROR_NOT_PERMITTED );
286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
291 return( PSA_ERROR_NOT_SUPPORTED );
292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
293 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
300 return( PSA_ERROR_NOT_SUPPORTED );
301
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
303 return( PSA_ERROR_HARDWARE_FAILURE );
304
305 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_RSA_INVALID_PADDING:
308 return( PSA_ERROR_INVALID_PADDING );
309 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
312 return( PSA_ERROR_INVALID_ARGUMENT );
313 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
314 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200315 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100316 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
317 return( PSA_ERROR_INVALID_SIGNATURE );
318 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
319 return( PSA_ERROR_BUFFER_TOO_SMALL );
320 case MBEDTLS_ERR_RSA_RNG_FAILED:
321 return( PSA_ERROR_INSUFFICIENT_MEMORY );
322 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
323 return( PSA_ERROR_NOT_SUPPORTED );
324 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
327 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
328 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
329 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
330 return( PSA_ERROR_HARDWARE_FAILURE );
331
332 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
333 return( PSA_ERROR_INVALID_ARGUMENT );
334 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
336
itayzafrir5c753392018-05-08 11:18:38 +0300337 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300338 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300339 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
341 return( PSA_ERROR_BUFFER_TOO_SMALL );
342 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
343 return( PSA_ERROR_NOT_SUPPORTED );
344 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
345 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
346 return( PSA_ERROR_INVALID_SIGNATURE );
347 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
348 return( PSA_ERROR_INSUFFICIENT_MEMORY );
349 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
350 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000351 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
352 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
Gilles Peskine8e338702019-07-30 20:06:31 +0200369 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100374mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100375 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200376{
377 switch( curve )
378 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100379 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100380 switch( byte_length )
381 {
382 case PSA_BITS_TO_BYTES( 192 ):
383 return( MBEDTLS_ECP_DP_SECP192R1 );
384 case PSA_BITS_TO_BYTES( 224 ):
385 return( MBEDTLS_ECP_DP_SECP224R1 );
386 case PSA_BITS_TO_BYTES( 256 ):
387 return( MBEDTLS_ECP_DP_SECP256R1 );
388 case PSA_BITS_TO_BYTES( 384 ):
389 return( MBEDTLS_ECP_DP_SECP384R1 );
390 case PSA_BITS_TO_BYTES( 521 ):
391 return( MBEDTLS_ECP_DP_SECP521R1 );
392 default:
393 return( MBEDTLS_ECP_DP_NONE );
394 }
395 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100396
Paul Elliott8ff510a2020-06-02 17:19:28 +0100397 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100398 switch( byte_length )
399 {
400 case PSA_BITS_TO_BYTES( 256 ):
401 return( MBEDTLS_ECP_DP_BP256R1 );
402 case PSA_BITS_TO_BYTES( 384 ):
403 return( MBEDTLS_ECP_DP_BP384R1 );
404 case PSA_BITS_TO_BYTES( 512 ):
405 return( MBEDTLS_ECP_DP_BP512R1 );
406 default:
407 return( MBEDTLS_ECP_DP_NONE );
408 }
409 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100410
Paul Elliott8ff510a2020-06-02 17:19:28 +0100411 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 switch( byte_length )
413 {
414 case PSA_BITS_TO_BYTES( 255 ):
415 return( MBEDTLS_ECP_DP_CURVE25519 );
416 case PSA_BITS_TO_BYTES( 448 ):
417 return( MBEDTLS_ECP_DP_CURVE448 );
418 default:
419 return( MBEDTLS_ECP_DP_NONE );
420 }
421 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100422
Paul Elliott8ff510a2020-06-02 17:19:28 +0100423 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 switch( byte_length )
425 {
426 case PSA_BITS_TO_BYTES( 192 ):
427 return( MBEDTLS_ECP_DP_SECP192K1 );
428 case PSA_BITS_TO_BYTES( 224 ):
429 return( MBEDTLS_ECP_DP_SECP224K1 );
430 case PSA_BITS_TO_BYTES( 256 ):
431 return( MBEDTLS_ECP_DP_SECP256K1 );
432 default:
433 return( MBEDTLS_ECP_DP_NONE );
434 }
435 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100436
Gilles Peskine12313cd2018-06-20 00:20:32 +0200437 default:
438 return( MBEDTLS_ECP_DP_NONE );
439 }
440}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100441#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200442
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200443static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
444 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445{
446 /* Check that the bit size is acceptable for the key type */
447 switch( type )
448 {
449 case PSA_KEY_TYPE_RAW_DATA:
450#if defined(MBEDTLS_MD_C)
451 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200452#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200453 case PSA_KEY_TYPE_DERIVE:
454 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200455#if defined(MBEDTLS_AES_C)
456 case PSA_KEY_TYPE_AES:
457 if( bits != 128 && bits != 192 && bits != 256 )
458 return( PSA_ERROR_INVALID_ARGUMENT );
459 break;
460#endif
461#if defined(MBEDTLS_CAMELLIA_C)
462 case PSA_KEY_TYPE_CAMELLIA:
463 if( bits != 128 && bits != 192 && bits != 256 )
464 return( PSA_ERROR_INVALID_ARGUMENT );
465 break;
466#endif
467#if defined(MBEDTLS_DES_C)
468 case PSA_KEY_TYPE_DES:
469 if( bits != 64 && bits != 128 && bits != 192 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
471 break;
472#endif
473#if defined(MBEDTLS_ARC4_C)
474 case PSA_KEY_TYPE_ARC4:
475 if( bits < 8 || bits > 2048 )
476 return( PSA_ERROR_INVALID_ARGUMENT );
477 break;
478#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200479#if defined(MBEDTLS_CHACHA20_C)
480 case PSA_KEY_TYPE_CHACHA20:
481 if( bits != 256 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200485 default:
486 return( PSA_ERROR_NOT_SUPPORTED );
487 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200488 if( bits % 8 != 0 )
489 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491 return( PSA_SUCCESS );
492}
493
Steven Cooremana01795d2020-07-24 22:48:15 +0200494#if defined(MBEDTLS_RSA_C)
495
496#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100497/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
498 * that are not a multiple of 8) well. For example, there is only
499 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
500 * way to return the exact bit size of a key.
501 * To keep things simple, reject non-byte-aligned key sizes. */
502static psa_status_t psa_check_rsa_key_byte_aligned(
503 const mbedtls_rsa_context *rsa )
504{
505 mbedtls_mpi n;
506 psa_status_t status;
507 mbedtls_mpi_init( &n );
508 status = mbedtls_to_psa_error(
509 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
510 if( status == PSA_SUCCESS )
511 {
512 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
513 status = PSA_ERROR_NOT_SUPPORTED;
514 }
515 mbedtls_mpi_free( &n );
516 return( status );
517}
Steven Cooremana01795d2020-07-24 22:48:15 +0200518#endif /* MBEDTLS_PK_PARSE_C */
Gilles Peskine86a440b2018-11-12 18:39:40 +0100519
Steven Cooreman7f391872020-07-30 14:57:44 +0200520/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200521 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200522 * \param[in] type The type of key contained in \p data.
523 * \param[in] data The buffer from which to load the representation.
524 * \param[in] data_length The size in bytes of \p data.
525 * \param[out] p_rsa Returns a pointer to an RSA context on success.
526 * The caller is responsible for freeing both the
527 * contents of the context and the context itself
528 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200529 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200530static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
531 const uint8_t *data,
532 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200533 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200534{
Steven Cooremana01795d2020-07-24 22:48:15 +0200535#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000536 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200537 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000538 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200539 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000540
541 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200542 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200544 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200547 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000548 if( status != PSA_SUCCESS )
549 goto exit;
550
551 /* We have something that the pkparse module recognizes. If it is a
552 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200553 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555 status = PSA_ERROR_INVALID_ARGUMENT;
556 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
560 * supports non-byte-aligned key sizes, but not well. For example,
561 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200562 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000563 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
564 {
565 status = PSA_ERROR_NOT_SUPPORTED;
566 goto exit;
567 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200568 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200569 if( status != PSA_SUCCESS )
570 goto exit;
571
Steven Cooremana2371e52020-07-28 14:30:39 +0200572 /* Copy out the pointer to the RSA context, and reset the PK context
573 * such that pk_free doesn't free the RSA context we just grabbed. */
574 *p_rsa = mbedtls_pk_rsa( ctx );
575 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000576
577exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200578 mbedtls_pk_free( &ctx );
579 return( status );
580#else
Steven Cooreman4fed4552020-08-03 14:46:03 +0200581 (void) data;
582 (void) data_length;
Steven Cooreman7f391872020-07-30 14:57:44 +0200583 (void) type;
Steven Cooremana01795d2020-07-24 22:48:15 +0200584 (void) rsa;
585 return( PSA_ERROR_NOT_SUPPORTED );
586#endif /* MBEDTLS_PK_PARSE_C */
587}
588
589/** Export an RSA key to export representation
590 *
591 * \param[in] type The type of key (public/private) to export
592 * \param[in] rsa The internal RSA representation from which to export
593 * \param[out] data The buffer to export to
594 * \param[in] data_size The length of the buffer to export to
595 * \param[out] data_length The amount of bytes written to \p data
596 */
597static psa_status_t psa_export_rsa_key( psa_key_type_t type,
598 mbedtls_rsa_context *rsa,
599 uint8_t *data,
600 size_t data_size,
601 size_t *data_length )
602{
603#if defined(MBEDTLS_PK_WRITE_C)
604 int ret;
605 mbedtls_pk_context pk;
606 uint8_t *pos = data + data_size;
607
608 mbedtls_pk_init( &pk );
609 pk.pk_info = &mbedtls_rsa_info;
610 pk.pk_ctx = rsa;
611
612 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200613 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
614 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200615 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
616 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
617 else
618 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
619
620 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200621 {
622 /* Clean up in case pk_write failed halfway through. */
623 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200624 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200625 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200626
627 /* The mbedtls_pk_xxx functions write to the end of the buffer.
628 * Move the data to the beginning and erase remaining data
629 * at the original location. */
630 if( 2 * (size_t) ret <= data_size )
631 {
632 memcpy( data, data + data_size - ret, ret );
633 memset( data + data_size - ret, 0, ret );
634 }
635 else if( (size_t) ret < data_size )
636 {
637 memmove( data, data + data_size - ret, ret );
638 memset( data + ret, 0, data_size - ret );
639 }
640
641 *data_length = ret;
642 return( PSA_SUCCESS );
643#else
644 (void) type;
645 (void) rsa;
646 (void) data;
647 (void) data_size;
648 (void) data_length;
649 return( PSA_ERROR_NOT_SUPPORTED );
650#endif /* MBEDTLS_PK_WRITE_C */
651}
652
653/** Import an RSA key from import representation to a slot
654 *
655 * \param[in,out] slot The slot where to store the export representation to
656 * \param[in] data The buffer containing the import representation
657 * \param[in] data_length The amount of bytes in \p data
658 */
659static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
660 const uint8_t *data,
661 size_t data_length )
662{
663 psa_status_t status;
664 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200665 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200666
Steven Cooremana01795d2020-07-24 22:48:15 +0200667 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200668 status = psa_load_rsa_representation( slot->attr.type,
669 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200670 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200671 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200672 if( status != PSA_SUCCESS )
673 goto exit;
674
675 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200676 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200677
Steven Cooreman75b74362020-07-28 14:30:13 +0200678 /* Re-export the data to PSA export format, such that we can store export
679 * representation in the key slot. Export representation in case of RSA is
680 * the smallest representation that's allowed as input, so a straight-up
681 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200682 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200683 if( output == NULL )
684 {
685 status = PSA_ERROR_INSUFFICIENT_MEMORY;
686 goto exit;
687 }
688
Steven Cooremana01795d2020-07-24 22:48:15 +0200689 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200690 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200691 output,
692 data_length,
693 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200694exit:
695 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200696 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200697 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200698
699 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000700 if( status != PSA_SUCCESS )
701 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200702 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000703 return( status );
704 }
705
Steven Cooremana01795d2020-07-24 22:48:15 +0200706 /* On success, store the allocated export-formatted key. */
707 slot->data.key.data = output;
708 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000709
710 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200711}
Steven Cooremana01795d2020-07-24 22:48:15 +0200712#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200713
Jaeden Ameroccdce902019-01-10 11:42:27 +0000714#if defined(MBEDTLS_ECP_C)
Steven Cooreman7f391872020-07-30 14:57:44 +0200715/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200716 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200717 * \param[in] type The type of key contained in \p data.
718 * \param[in] data The buffer from which to load the representation.
719 * \param[in] data_length The size in bytes of \p data.
720 * \param[out] p_ecp Returns a pointer to an ECP context on success.
721 * The caller is responsible for freeing both the
722 * contents of the context and the context itself
723 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200724 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200725static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
726 const uint8_t *data,
727 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200728 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100729{
730 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200731 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200732 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200733 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100734
Steven Cooreman4fed4552020-08-03 14:46:03 +0200735 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
736 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100737 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200738 /* A Weierstrass public key is represented as:
739 * - The byte 0x04;
740 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
741 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
742 * So its data length is 2m+1 where n is the key size in bits.
743 */
744 if( ( data_length & 1 ) == 0 )
745 return( PSA_ERROR_INVALID_ARGUMENT );
746 curve_size = data_length / 2;
747
748 /* Montgomery public keys are represented in compressed format, meaning
749 * their curve_size is equal to the amount of input. */
750
751 /* Private keys are represented in uncompressed private random integer
752 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100753 }
754
Steven Cooreman6d839f02020-07-30 11:36:45 +0200755 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200756 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200757 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200758 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200759 mbedtls_ecp_keypair_init( ecp );
760
Gilles Peskine4cd32772019-12-02 20:49:42 +0100761 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200762 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
763 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100764 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200765 {
766 status = PSA_ERROR_INVALID_ARGUMENT;
767 goto exit;
768 }
769
Steven Cooremanacda8342020-07-24 23:09:52 +0200770 status = mbedtls_to_psa_error(
771 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
772 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200773 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200774
Steven Cooreman6d839f02020-07-30 11:36:45 +0200775 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200776 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200777 {
778 /* Load the public value. */
779 status = mbedtls_to_psa_error(
780 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200781 data,
782 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200783 if( status != PSA_SUCCESS )
784 goto exit;
785
786 /* Check that the point is on the curve. */
787 status = mbedtls_to_psa_error(
788 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
789 if( status != PSA_SUCCESS )
790 goto exit;
791 }
792 else
793 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200794 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200795 status = mbedtls_to_psa_error(
796 mbedtls_ecp_read_key( ecp->grp.id,
797 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200798 data,
799 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200800 if( status != PSA_SUCCESS )
801 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200802 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200803
804 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200805exit:
806 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200807 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200808 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200809 mbedtls_free( ecp );
810 }
811
Steven Cooreman29149862020-08-05 15:43:42 +0200812 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100813}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000814
Steven Cooreman4fed4552020-08-03 14:46:03 +0200815/** Export an ECP key to export representation
816 *
817 * \param[in] type The type of key (public/private) to export
818 * \param[in] ecp The internal ECP representation from which to export
819 * \param[out] data The buffer to export to
820 * \param[in] data_size The length of the buffer to export to
821 * \param[out] data_length The amount of bytes written to \p data
822 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200823static psa_status_t psa_export_ecp_key( psa_key_type_t type,
824 mbedtls_ecp_keypair *ecp,
825 uint8_t *data,
826 size_t data_size,
827 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200828{
Steven Cooremanacda8342020-07-24 23:09:52 +0200829 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000830
Steven Cooremanacda8342020-07-24 23:09:52 +0200831 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200832 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200833 /* Check whether the public part is loaded */
834 if( mbedtls_ecp_is_zero( &ecp->Q ) )
835 {
836 /* Calculate the public key */
837 status = mbedtls_to_psa_error(
838 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
839 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
840 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200841 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200842 }
843
Steven Cooreman4fed4552020-08-03 14:46:03 +0200844 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200845 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
846 MBEDTLS_ECP_PF_UNCOMPRESSED,
847 data_length,
848 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200849 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200850 if( status != PSA_SUCCESS )
851 memset( data, 0, data_size );
852
Steven Cooreman29149862020-08-05 15:43:42 +0200853 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200854 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200855 else
856 {
Steven Cooreman29149862020-08-05 15:43:42 +0200857 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200858 return( PSA_ERROR_BUFFER_TOO_SMALL );
859
860 status = mbedtls_to_psa_error(
861 mbedtls_ecp_write_key( ecp,
862 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200863 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200864 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200865 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200866 else
867 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200868
869 return( status );
870 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200871}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200872
Steven Cooreman4fed4552020-08-03 14:46:03 +0200873/** Import an ECP key from import representation to a slot
874 *
875 * \param[in,out] slot The slot where to store the export representation to
876 * \param[in] data The buffer containing the import representation
877 * \param[in] data_length The amount of bytes in \p data
878 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200879static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
880 const uint8_t *data,
881 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100882{
Steven Cooremanacda8342020-07-24 23:09:52 +0200883 psa_status_t status;
884 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200885 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100886
Steven Cooremanacda8342020-07-24 23:09:52 +0200887 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200888 status = psa_load_ecp_representation( slot->attr.type,
889 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200890 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200891 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100892 if( status != PSA_SUCCESS )
893 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100894
Steven Cooremanacda8342020-07-24 23:09:52 +0200895 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200896 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200897 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200898 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200899
Steven Cooremanacda8342020-07-24 23:09:52 +0200900 /* Re-export the data to PSA export format. There is currently no support
901 * for other input formats then the export format, so this is a 1-1
902 * copy operation. */
903 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200904 if( output == NULL )
905 {
906 status = PSA_ERROR_INSUFFICIENT_MEMORY;
907 goto exit;
908 }
909
910 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200911 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200912 output,
913 data_length,
914 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100915exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200916 /* Always free the PK object (will also free contained ECP context) */
917 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200918 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200919
920 /* Free the allocated buffer only on error. */
921 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100922 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200923 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200924 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100925 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200926
927 /* On success, store the allocated export-formatted key. */
928 slot->data.key.data = output;
929 slot->data.key.bytes = data_length;
930
931 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100932}
933#endif /* defined(MBEDTLS_ECP_C) */
934
Gilles Peskineb46bef22019-07-30 21:32:04 +0200935/** Return the size of the key in the given slot, in bits.
936 *
937 * \param[in] slot A key slot.
938 *
939 * \return The key size in bits, read from the metadata in the slot.
940 */
941static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
942{
943 return( slot->attr.bits );
944}
945
Steven Cooreman75b74362020-07-28 14:30:13 +0200946/** Try to allocate a buffer to an empty key slot.
947 *
948 * \param[in,out] slot Key slot to attach buffer to.
949 * \param[in] buffer_length Requested size of the buffer.
950 *
951 * \retval #PSA_SUCCESS
952 * The buffer has been successfully allocated.
953 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
954 * Not enough memory was available for allocation.
955 * \retval #PSA_ERROR_ALREADY_EXISTS
956 * Trying to allocate a buffer to a non-empty key slot.
957 */
958static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
959 size_t buffer_length )
960{
961 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200962 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200963
964 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
965 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200966 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200967
968 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200969 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200970}
971
Gilles Peskine8e338702019-07-30 20:06:31 +0200972/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100973 * previously. This function assumes that the slot does not contain
974 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100975psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
976 const uint8_t *data,
977 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100978{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200979 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200981 /* zero-length keys are never supported. */
982 if( data_length == 0 )
983 return( PSA_ERROR_NOT_SUPPORTED );
984
Gilles Peskine8e338702019-07-30 20:06:31 +0200985 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200987 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200988
Steven Cooreman75b74362020-07-28 14:30:13 +0200989 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
990 if( data_length > SIZE_MAX / 8 )
991 return( PSA_ERROR_NOT_SUPPORTED );
992
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200993 /* Enforce a size limit, and in particular ensure that the bit
994 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200995 if( bit_size > PSA_MAX_KEY_BITS )
996 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200997
998 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200999 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001000 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001001
1002 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02001003 status = psa_allocate_buffer_to_slot( slot, data_length );
1004 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001005 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001006
1007 /* copy key into allocated buffer */
Steven Cooreman29149862020-08-05 15:43:42 +02001008 memcpy( slot->data.key.data, data, data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001009
1010 /* Write the actual key size to the slot.
1011 * psa_start_key_creation() wrote the size declared by the
1012 * caller, which may be 0 (meaning unspecified) or wrong. */
1013 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001015 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1016 {
Gilles Peskinef76aa772018-10-29 19:24:33 +01001017#if defined(MBEDTLS_ECP_C)
Steven Cooreman19fd5742020-07-24 23:31:01 +02001018 status = psa_import_ecp_key( slot,
1019 data, data_length );
1020#else
1021 /* No drivers have been implemented yet, so without mbed TLS backing
1022 * there's no way to do ECP with the current library. */
1023 return( PSA_ERROR_NOT_SUPPORTED );
1024#endif /* defined(MBEDTLS_ECP_C) */
1025 }
1026 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +01001027 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001028#if defined(MBEDTLS_RSA_C)
1029 status = psa_import_rsa_key( slot,
1030 data, data_length );
1031#else
1032 /* No drivers have been implemented yet, so without mbed TLS backing
1033 * there's no way to do RSA with the current library. */
1034 status = PSA_ERROR_NOT_SUPPORTED;
1035#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036 }
1037 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001038 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001039 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001041 }
Darryl Green940d72c2018-07-13 13:18:51 +01001042
Darryl Green06fd18d2018-07-16 11:21:11 +01001043 return( status );
1044}
1045
Gilles Peskinef603c712019-01-19 13:40:11 +01001046/** Calculate the intersection of two algorithm usage policies.
1047 *
1048 * Return 0 (which allows no operation) on incompatibility.
1049 */
1050static psa_algorithm_t psa_key_policy_algorithm_intersection(
1051 psa_algorithm_t alg1,
1052 psa_algorithm_t alg2 )
1053{
Gilles Peskine549ea862019-05-22 11:45:59 +02001054 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001055 if( alg1 == alg2 )
1056 return( alg1 );
1057 /* If the policies are from the same hash-and-sign family, check
1058 * if one is a wildcard. If so the other has the specific algorithm. */
1059 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1060 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1061 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1062 {
1063 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1064 return( alg2 );
1065 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1066 return( alg1 );
1067 }
1068 /* If the policies are incompatible, allow nothing. */
1069 return( 0 );
1070}
1071
Gilles Peskined6f371b2019-05-10 19:33:38 +02001072static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1073 psa_algorithm_t requested_alg )
1074{
Gilles Peskine549ea862019-05-22 11:45:59 +02001075 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001076 if( requested_alg == policy_alg )
1077 return( 1 );
1078 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001079 * and requested_alg is the same hash-and-sign family with any hash,
1080 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001081 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1082 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1083 {
1084 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1085 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1086 }
1087 /* If it isn't permitted, it's forbidden. */
1088 return( 0 );
1089}
1090
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001091/** Test whether a policy permits an algorithm.
1092 *
1093 * The caller must test usage flags separately.
1094 */
1095static int psa_key_policy_permits( const psa_key_policy_t *policy,
1096 psa_algorithm_t alg )
1097{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001098 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1099 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001100}
1101
Gilles Peskinef603c712019-01-19 13:40:11 +01001102/** Restrict a key policy based on a constraint.
1103 *
1104 * \param[in,out] policy The policy to restrict.
1105 * \param[in] constraint The policy constraint to apply.
1106 *
1107 * \retval #PSA_SUCCESS
1108 * \c *policy contains the intersection of the original value of
1109 * \c *policy and \c *constraint.
1110 * \retval #PSA_ERROR_INVALID_ARGUMENT
1111 * \c *policy and \c *constraint are incompatible.
1112 * \c *policy is unchanged.
1113 */
1114static psa_status_t psa_restrict_key_policy(
1115 psa_key_policy_t *policy,
1116 const psa_key_policy_t *constraint )
1117{
1118 psa_algorithm_t intersection_alg =
1119 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001120 psa_algorithm_t intersection_alg2 =
1121 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001122 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1123 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001124 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1125 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001126 policy->usage &= constraint->usage;
1127 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001128 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001129 return( PSA_SUCCESS );
1130}
1131
Darryl Green06fd18d2018-07-16 11:21:11 +01001132/** Retrieve a slot which must contain a key. The key must have allow all the
1133 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1134 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001135static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001136 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001137 psa_key_usage_t usage,
1138 psa_algorithm_t alg )
1139{
1140 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001141 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001142
1143 *p_slot = NULL;
1144
Gilles Peskinec5487a82018-12-03 18:08:14 +01001145 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001146 if( status != PSA_SUCCESS )
1147 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001148
1149 /* Enforce that usage policy for the key slot contains all the flags
1150 * required by the usage parameter. There is one exception: public
1151 * keys can always be exported, so we treat public key objects as
1152 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001153 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001154 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001155 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001156 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001157
1158 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001159 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001160 return( PSA_ERROR_NOT_PERMITTED );
1161
1162 *p_slot = slot;
1163 return( PSA_SUCCESS );
1164}
Darryl Green940d72c2018-07-13 13:18:51 +01001165
Gilles Peskine28f8f302019-07-24 13:30:31 +02001166/** Retrieve a slot which must contain a transparent key.
1167 *
1168 * A transparent key is a key for which the key material is directly
1169 * available, as opposed to a key in a secure element.
1170 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001171 * This is a temporary function to use instead of psa_get_key_from_slot()
1172 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001173 */
1174#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1175static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1176 psa_key_slot_t **p_slot,
1177 psa_key_usage_t usage,
1178 psa_algorithm_t alg )
1179{
1180 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1181 if( status != PSA_SUCCESS )
1182 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001183 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001184 {
1185 *p_slot = NULL;
1186 return( PSA_ERROR_NOT_SUPPORTED );
1187 }
1188 return( PSA_SUCCESS );
1189}
1190#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1191/* With no secure element support, all keys are transparent. */
1192#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1193 psa_get_key_from_slot( handle, p_slot, usage, alg )
1194#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1195
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001196/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001197static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001198{
Gilles Peskine73167e12019-07-12 23:44:37 +02001199#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Fredrik Strupe462aa572020-12-17 10:44:38 +01001200 if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) &&
1201 psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001202 {
1203 /* No key material to clean. */
1204 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001205 else
1206#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001207 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001208 /* Data pointer will always be either a valid pointer or NULL in an
1209 * initialized slot, so we can just free it. */
1210 if( slot->data.key.data != NULL )
1211 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001212 mbedtls_free( slot->data.key.data );
1213 slot->data.key.data = NULL;
1214 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001215 }
Darryl Green40225ba2018-11-15 14:48:15 +00001216
1217 return( PSA_SUCCESS );
1218}
1219
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001220/** Completely wipe a slot in memory, including its policy.
1221 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001222psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001223{
1224 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001225 /* Multipart operations may still be using the key. This is safe
1226 * because all multipart operation objects are independent from
1227 * the key slot: if they need to access the key after the setup
1228 * phase, they have a copy of the key. Note that this means that
1229 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001230 /* At this point, key material and other type-specific content has
1231 * been wiped. Clear remaining metadata. We can call memset and not
1232 * zeroize because the metadata is not particularly sensitive. */
1233 memset( slot, 0, sizeof( *slot ) );
1234 return( status );
1235}
1236
Gilles Peskinec5487a82018-12-03 18:08:14 +01001237psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001238{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001239 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001240 psa_status_t status; /* status of the last operation */
1241 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001242#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1243 psa_se_drv_table_entry_t *driver;
1244#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001245
Gilles Peskine1841cf42019-10-08 15:48:25 +02001246 if( handle == 0 )
1247 return( PSA_SUCCESS );
1248
Gilles Peskinec5487a82018-12-03 18:08:14 +01001249 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001250 if( status != PSA_SUCCESS )
1251 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001252
1253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001254 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001255 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001256 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001257 /* For a key in a secure element, we need to do three things:
1258 * remove the key file in internal storage, destroy the
1259 * key inside the secure element, and update the driver's
1260 * persistent data. Start a transaction that will encompass these
1261 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001262 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001263 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001264 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001265 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001266 status = psa_crypto_save_transaction( );
1267 if( status != PSA_SUCCESS )
1268 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001269 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001270 /* We should still try to destroy the key in the secure
1271 * element and the key metadata in storage. This is especially
1272 * important if the error is that the storage is full.
1273 * But how to do it exactly without risking an inconsistent
1274 * state after a reset?
1275 * https://github.com/ARMmbed/mbed-crypto/issues/215
1276 */
1277 overall_status = status;
1278 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001279 }
1280
Gilles Peskine354f7672019-07-12 23:46:38 +02001281 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001282 if( overall_status == PSA_SUCCESS )
1283 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001284 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001285#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1286
Darryl Greend49a4992018-06-18 17:27:26 +01001287#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001288 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001289 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001290 status = psa_destroy_persistent_key( slot->attr.id );
1291 if( overall_status == PSA_SUCCESS )
1292 overall_status = status;
1293
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001294 /* TODO: other slots may have a copy of the same key. We should
1295 * invalidate them.
1296 * https://github.com/ARMmbed/mbed-crypto/issues/214
1297 */
Darryl Greend49a4992018-06-18 17:27:26 +01001298 }
1299#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001300
Gilles Peskinefc762652019-07-22 19:30:34 +02001301#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1302 if( driver != NULL )
1303 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001304 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001305 if( overall_status == PSA_SUCCESS )
1306 overall_status = status;
1307 status = psa_crypto_stop_transaction( );
1308 if( overall_status == PSA_SUCCESS )
1309 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001310 }
1311#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1312
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001313#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1314exit:
1315#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001316 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001317 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1318 if( overall_status == PSA_SUCCESS )
1319 overall_status = status;
1320 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001321}
1322
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001323void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001324{
Gilles Peskineb699f072019-04-26 16:06:02 +02001325 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001326 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001327}
1328
Gilles Peskineb699f072019-04-26 16:06:02 +02001329psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1330 psa_key_type_t type,
1331 const uint8_t *data,
1332 size_t data_length )
1333{
1334 uint8_t *copy = NULL;
1335
1336 if( data_length != 0 )
1337 {
1338 copy = mbedtls_calloc( 1, data_length );
1339 if( copy == NULL )
1340 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1341 memcpy( copy, data, data_length );
1342 }
1343 /* After this point, this function is guaranteed to succeed, so it
1344 * can start modifying `*attributes`. */
1345
1346 if( attributes->domain_parameters != NULL )
1347 {
1348 mbedtls_free( attributes->domain_parameters );
1349 attributes->domain_parameters = NULL;
1350 attributes->domain_parameters_size = 0;
1351 }
1352
1353 attributes->domain_parameters = copy;
1354 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001355 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001356 return( PSA_SUCCESS );
1357}
1358
1359psa_status_t psa_get_key_domain_parameters(
1360 const psa_key_attributes_t *attributes,
1361 uint8_t *data, size_t data_size, size_t *data_length )
1362{
1363 if( attributes->domain_parameters_size > data_size )
1364 return( PSA_ERROR_BUFFER_TOO_SMALL );
1365 *data_length = attributes->domain_parameters_size;
1366 if( attributes->domain_parameters_size != 0 )
1367 memcpy( data, attributes->domain_parameters,
1368 attributes->domain_parameters_size );
1369 return( PSA_SUCCESS );
1370}
1371
1372#if defined(MBEDTLS_RSA_C)
1373static psa_status_t psa_get_rsa_public_exponent(
1374 const mbedtls_rsa_context *rsa,
1375 psa_key_attributes_t *attributes )
1376{
1377 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001379 uint8_t *buffer = NULL;
1380 size_t buflen;
1381 mbedtls_mpi_init( &mpi );
1382
1383 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1384 if( ret != 0 )
1385 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001386 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1387 {
1388 /* It's the default value, which is reported as an empty string,
1389 * so there's nothing to do. */
1390 goto exit;
1391 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001392
1393 buflen = mbedtls_mpi_size( &mpi );
1394 buffer = mbedtls_calloc( 1, buflen );
1395 if( buffer == NULL )
1396 {
1397 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1398 goto exit;
1399 }
1400 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1401 if( ret != 0 )
1402 goto exit;
1403 attributes->domain_parameters = buffer;
1404 attributes->domain_parameters_size = buflen;
1405
1406exit:
1407 mbedtls_mpi_free( &mpi );
1408 if( ret != 0 )
1409 mbedtls_free( buffer );
1410 return( mbedtls_to_psa_error( ret ) );
1411}
1412#endif /* MBEDTLS_RSA_C */
1413
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001414/** Retrieve all the publicly-accessible attributes of a key.
1415 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001416psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1417 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001418{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001419 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001420 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001421
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001422 psa_reset_key_attributes( attributes );
1423
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001424 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001425 if( status != PSA_SUCCESS )
1426 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001427
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001428 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001429 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1430 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1431
1432#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1433 if( psa_key_slot_is_external( slot ) )
1434 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1435#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001436
Gilles Peskine8e338702019-07-30 20:06:31 +02001437 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001438 {
1439#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001440 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001441 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001442#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001443 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001444 * is not yet implemented.
1445 * https://github.com/ARMmbed/mbed-crypto/issues/216
1446 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001447 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001448 break;
1449#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001450 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001451 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001452
Steven Cooreman4fed4552020-08-03 14:46:03 +02001453 status = psa_load_rsa_representation( slot->attr.type,
1454 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001455 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001456 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001457 if( status != PSA_SUCCESS )
1458 break;
1459
Steven Cooremana2371e52020-07-28 14:30:39 +02001460 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001461 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001462 mbedtls_rsa_free( rsa );
1463 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001464 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001465 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001466#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001467 default:
1468 /* Nothing else to do. */
1469 break;
1470 }
1471
1472 if( status != PSA_SUCCESS )
1473 psa_reset_key_attributes( attributes );
1474 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001475}
1476
Gilles Peskinec8000c02019-08-02 20:15:51 +02001477#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1478psa_status_t psa_get_key_slot_number(
1479 const psa_key_attributes_t *attributes,
1480 psa_key_slot_number_t *slot_number )
1481{
1482 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1483 {
1484 *slot_number = attributes->slot_number;
1485 return( PSA_SUCCESS );
1486 }
1487 else
1488 return( PSA_ERROR_INVALID_ARGUMENT );
1489}
1490#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1491
Steven Cooremana01795d2020-07-24 22:48:15 +02001492static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1493 uint8_t *data,
1494 size_t data_size,
1495 size_t *data_length )
1496{
1497 if( slot->data.key.bytes > data_size )
1498 return( PSA_ERROR_BUFFER_TOO_SMALL );
1499 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1500 memset( data + slot->data.key.bytes, 0,
1501 data_size - slot->data.key.bytes );
1502 *data_length = slot->data.key.bytes;
1503 return( PSA_SUCCESS );
1504}
1505
Gilles Peskinef603c712019-01-19 13:40:11 +01001506static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1507 uint8_t *data,
1508 size_t data_size,
1509 size_t *data_length,
1510 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001511{
Gilles Peskine5d309672019-07-12 23:47:28 +02001512#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1513 const psa_drv_se_t *drv;
1514 psa_drv_se_context_t *drv_context;
1515#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1516
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001517 *data_length = 0;
1518
Gilles Peskine8e338702019-07-30 20:06:31 +02001519 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001520 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001521
Gilles Peskinef9168942019-09-12 19:20:29 +02001522 /* Reject a zero-length output buffer now, since this can never be a
1523 * valid key representation. This way we know that data must be a valid
1524 * pointer and we can do things like memset(data, ..., data_size). */
1525 if( data_size == 0 )
1526 return( PSA_ERROR_BUFFER_TOO_SMALL );
1527
Gilles Peskine5d309672019-07-12 23:47:28 +02001528#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001529 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001530 {
1531 psa_drv_se_export_key_t method;
1532 if( drv->key_management == NULL )
1533 return( PSA_ERROR_NOT_SUPPORTED );
1534 method = ( export_public_key ?
1535 drv->key_management->p_export_public :
1536 drv->key_management->p_export );
1537 if( method == NULL )
1538 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001539 return( method( drv_context,
1540 slot->data.se.slot_number,
1541 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001542 }
1543#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1544
Gilles Peskine8e338702019-07-30 20:06:31 +02001545 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001546 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001547 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001548 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001549 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1550 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001551 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001552 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001553 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001554 /* Exporting public -> public */
1555 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1556 }
1557 else if( !export_public_key )
1558 {
1559 /* Exporting private -> private */
1560 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1561 }
1562 /* Need to export the public part of a private key,
1563 * so conversion is needed */
1564 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1565 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001566#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001567 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001568 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001569 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001570 slot->data.key.data,
1571 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001572 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001573 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001574 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001575
Steven Cooreman560c28a2020-07-24 23:20:24 +02001576 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001577 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001578 data,
1579 data_size,
1580 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001581
Steven Cooremana2371e52020-07-28 14:30:39 +02001582 mbedtls_rsa_free( rsa );
1583 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001584
Steven Cooreman560c28a2020-07-24 23:20:24 +02001585 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001586#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001587 /* We don't know how to convert a private RSA key to public. */
1588 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001589#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001590 }
1591 else
Moran Pekera998bc62018-04-16 18:16:20 +03001592 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001593#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001594 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001595 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001596 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001597 slot->data.key.data,
1598 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001599 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001600 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001601 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001602
1603 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1604 PSA_KEY_TYPE_ECC_GET_FAMILY(
1605 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001606 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001607 data,
1608 data_size,
1609 data_length );
1610
Steven Cooremana2371e52020-07-28 14:30:39 +02001611 mbedtls_ecp_keypair_free( ecp );
1612 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001613 return( status );
1614#else
1615 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001616 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001617#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001618 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001619 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001620 else
1621 {
1622 /* This shouldn't happen in the reference implementation, but
1623 it is valid for a special-purpose implementation to omit
1624 support for exporting certain key types. */
1625 return( PSA_ERROR_NOT_SUPPORTED );
1626 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001627}
1628
Gilles Peskinec5487a82018-12-03 18:08:14 +01001629psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001630 uint8_t *data,
1631 size_t data_size,
1632 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001633{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001634 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001635 psa_status_t status;
1636
1637 /* Set the key to empty now, so that even when there are errors, we always
1638 * set data_length to a value between 0 and data_size. On error, setting
1639 * the key to empty is a good choice because an empty key representation is
1640 * unlikely to be accepted anywhere. */
1641 *data_length = 0;
1642
1643 /* Export requires the EXPORT flag. There is an exception for public keys,
1644 * which don't require any flag, but psa_get_key_from_slot takes
1645 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001646 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001647 if( status != PSA_SUCCESS )
1648 return( status );
1649 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001650 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001651}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001652
Gilles Peskinec5487a82018-12-03 18:08:14 +01001653psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001654 uint8_t *data,
1655 size_t data_size,
1656 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001657{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001658 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001659 psa_status_t status;
1660
1661 /* Set the key to empty now, so that even when there are errors, we always
1662 * set data_length to a value between 0 and data_size. On error, setting
1663 * the key to empty is a good choice because an empty key representation is
1664 * unlikely to be accepted anywhere. */
1665 *data_length = 0;
1666
1667 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001668 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001669 if( status != PSA_SUCCESS )
1670 return( status );
1671 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001672 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001673}
1674
Gilles Peskine91e8c332019-08-02 19:19:39 +02001675#if defined(static_assert)
1676static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1677 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001678static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001679 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001680static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001681 "One or more key attribute flag is listed as both internal-only and external-only" );
1682#endif
1683
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001684/** Validate that a key policy is internally well-formed.
1685 *
1686 * This function only rejects invalid policies. It does not validate the
1687 * consistency of the policy with respect to other attributes of the key
1688 * such as the key type.
1689 */
1690static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001691{
1692 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001693 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001694 PSA_KEY_USAGE_ENCRYPT |
1695 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001696 PSA_KEY_USAGE_SIGN_HASH |
1697 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001698 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1699 return( PSA_ERROR_INVALID_ARGUMENT );
1700
Gilles Peskine4747d192019-04-17 15:05:45 +02001701 return( PSA_SUCCESS );
1702}
1703
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001704/** Validate the internal consistency of key attributes.
1705 *
1706 * This function only rejects invalid attribute values. If does not
1707 * validate the consistency of the attributes with any key data that may
1708 * be involved in the creation of the key.
1709 *
1710 * Call this function early in the key creation process.
1711 *
1712 * \param[in] attributes Key attributes for the new key.
1713 * \param[out] p_drv On any return, the driver for the key, if any.
1714 * NULL for a transparent key.
1715 *
1716 */
1717static psa_status_t psa_validate_key_attributes(
1718 const psa_key_attributes_t *attributes,
1719 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001720{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001721 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001722
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001723 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1724 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001725 if( status != PSA_SUCCESS )
1726 return( status );
1727
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001728 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1729 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001730 if( status != PSA_SUCCESS )
1731 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001732
1733 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001734 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001735 return( status );
1736
1737 /* Refuse to create overly large keys.
1738 * Note that this doesn't trigger on import if the attributes don't
1739 * explicitly specify a size (so psa_get_key_bits returns 0), so
1740 * psa_import_key() needs its own checks. */
1741 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1742 return( PSA_ERROR_NOT_SUPPORTED );
1743
Gilles Peskine91e8c332019-08-02 19:19:39 +02001744 /* Reject invalid flags. These should not be reachable through the API. */
1745 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1746 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1747 return( PSA_ERROR_INVALID_ARGUMENT );
1748
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001749 return( PSA_SUCCESS );
1750}
1751
Gilles Peskine4747d192019-04-17 15:05:45 +02001752/** Prepare a key slot to receive key material.
1753 *
1754 * This function allocates a key slot and sets its metadata.
1755 *
1756 * If this function fails, call psa_fail_key_creation().
1757 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001758 * This function is intended to be used as follows:
1759 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1760 * it with the specified attributes, and assign it a handle.
1761 * -# Populate the slot with the key material.
1762 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1763 * In case of failure at any step, stop the sequence and call
1764 * psa_fail_key_creation().
1765 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001766 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001767 * \param[in] attributes Key attributes for the new key.
1768 * \param[out] handle On success, a handle for the allocated slot.
1769 * \param[out] p_slot On success, a pointer to the prepared slot.
1770 * \param[out] p_drv On any return, the driver for the key, if any.
1771 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001772 *
1773 * \retval #PSA_SUCCESS
1774 * The key slot is ready to receive key material.
1775 * \return If this function fails, the key slot is an invalid state.
1776 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001777 */
1778static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001779 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001780 const psa_key_attributes_t *attributes,
1781 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001782 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001783 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001784{
1785 psa_status_t status;
1786 psa_key_slot_t *slot;
1787
Gilles Peskinedf179142019-07-15 22:02:14 +02001788 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001789 *p_drv = NULL;
1790
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001791 status = psa_validate_key_attributes( attributes, p_drv );
1792 if( status != PSA_SUCCESS )
1793 return( status );
1794
Gilles Peskineedbed562019-08-07 18:19:59 +02001795 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001796 if( status != PSA_SUCCESS )
1797 return( status );
1798 slot = *p_slot;
1799
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001800 /* We're storing the declared bit-size of the key. It's up to each
1801 * creation mechanism to verify that this information is correct.
1802 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001803 * an input (generate, device) but not for those where the bit-size
1804 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001805
1806 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001807
Gilles Peskine91e8c332019-08-02 19:19:39 +02001808 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001809 * external-only flags, query `attributes`. Thanks to the check
1810 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001811 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001812 * may have set. */
1813 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001814
Gilles Peskinecbaff462019-07-12 23:46:04 +02001815#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001816 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001817 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001818 * create the key file in internal storage, create the
1819 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001820 * persistent data. This is done by starting a transaction that will
1821 * encompass these three actions.
1822 * For registering a volatile key, we just need to find an appropriate
1823 * slot number inside the SE. Since the key is designated volatile, creating
1824 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001825 /* The first thing to do is to find a slot number for the new key.
1826 * We save the slot number in persistent storage as part of the
1827 * transaction data. It will be needed to recover if the power
1828 * fails during the key creation process, to clean up on the secure
1829 * element side after restarting. Obtaining a slot number from the
1830 * secure element driver updates its persistent state, but we do not yet
1831 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001832 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001833 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001834 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001835 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001836 &slot->data.se.slot_number );
1837 if( status != PSA_SUCCESS )
1838 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001839
1840 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001841 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001842 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1843 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1844 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1845 psa_crypto_transaction.key.id = slot->attr.id;
1846 status = psa_crypto_save_transaction( );
1847 if( status != PSA_SUCCESS )
1848 {
1849 (void) psa_crypto_stop_transaction( );
1850 return( status );
1851 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001852 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001853 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001854
1855 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1856 {
1857 /* Key registration only makes sense with a secure element. */
1858 return( PSA_ERROR_INVALID_ARGUMENT );
1859 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001860#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1861
Darryl Green0c6575a2018-11-07 16:05:30 +00001862 return( status );
1863}
Gilles Peskine4747d192019-04-17 15:05:45 +02001864
1865/** Finalize the creation of a key once its key material has been set.
1866 *
1867 * This entails writing the key to persistent storage.
1868 *
1869 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001870 * See the documentation of psa_start_key_creation() for the intended use
1871 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001872 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001873 * \param[in,out] slot Pointer to the slot with key material.
1874 * \param[in] driver The secure element driver for the key,
1875 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001876 *
1877 * \retval #PSA_SUCCESS
1878 * The key was successfully created. The handle is now valid.
1879 * \return If this function fails, the key slot is an invalid state.
1880 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001881 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001882static psa_status_t psa_finish_key_creation(
1883 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001884 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001885{
1886 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001887 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001888 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001889
1890#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001891 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001892 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001893#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1894 if( driver != NULL )
1895 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001896 psa_se_key_data_storage_t data;
1897#if defined(static_assert)
1898 static_assert( sizeof( slot->data.se.slot_number ) ==
1899 sizeof( data.slot_number ),
1900 "Slot number size does not match psa_se_key_data_storage_t" );
1901 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1902 "Bit-size size does not match psa_se_key_data_storage_t" );
1903#endif
1904 memcpy( &data.slot_number, &slot->data.se.slot_number,
1905 sizeof( slot->data.se.slot_number ) );
1906 memcpy( &data.bits, &slot->attr.bits,
1907 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001908 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001909 (uint8_t*) &data,
1910 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001911 }
1912 else
1913#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1914 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001915 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001916 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001917 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001918 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1919 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001920 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001921 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1922 status = psa_internal_export_key( slot,
1923 buffer, buffer_size, &length,
1924 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001925 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001926 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001927 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001928
Gilles Peskinef9168942019-09-12 19:20:29 +02001929 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001930 mbedtls_free( buffer );
1931 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001932 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001933#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1934
Gilles Peskinecbaff462019-07-12 23:46:04 +02001935#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001936 /* Finish the transaction for a key creation. This does not
1937 * happen when registering an existing key. Detect this case
1938 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001939 * creation of a persistent key in a secure element requires a transaction,
1940 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001941 if( driver != NULL &&
1942 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001943 {
1944 status = psa_save_se_persistent_data( driver );
1945 if( status != PSA_SUCCESS )
1946 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001947 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001948 return( status );
1949 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001950 status = psa_crypto_stop_transaction( );
1951 if( status != PSA_SUCCESS )
1952 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001953 }
1954#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1955
Gilles Peskine4747d192019-04-17 15:05:45 +02001956 return( status );
1957}
1958
1959/** Abort the creation of a key.
1960 *
1961 * You may call this function after calling psa_start_key_creation(),
1962 * or after psa_finish_key_creation() fails. In other circumstances, this
1963 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001964 * See the documentation of psa_start_key_creation() for the intended use
1965 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001966 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001967 * \param[in,out] slot Pointer to the slot with key material.
1968 * \param[in] driver The secure element driver for the key,
1969 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001970 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001971static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001972 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001973{
Gilles Peskine011e4282019-06-26 18:34:38 +02001974 (void) driver;
1975
Gilles Peskine4747d192019-04-17 15:05:45 +02001976 if( slot == NULL )
1977 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001978
1979#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001980 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001981 * element, and the failure happened later (when saving metadata
1982 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001983 * element.
1984 * https://github.com/ARMmbed/mbed-crypto/issues/217
1985 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001986
Gilles Peskined7729582019-08-05 15:55:54 +02001987 /* Abort the ongoing transaction if any (there may not be one if
1988 * the creation process failed before starting one, or if the
1989 * key creation is a registration of a key in a secure element).
1990 * Earlier functions must already have done what it takes to undo any
1991 * partial creation. All that's left is to update the transaction data
1992 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001993 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001994#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1995
Gilles Peskine4747d192019-04-17 15:05:45 +02001996 psa_wipe_key_slot( slot );
1997}
1998
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001999/** Validate optional attributes during key creation.
2000 *
2001 * Some key attributes are optional during key creation. If they are
2002 * specified in the attributes structure, check that they are consistent
2003 * with the data in the slot.
2004 *
2005 * This function should be called near the end of key creation, after
2006 * the slot in memory is fully populated but before saving persistent data.
2007 */
2008static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002009 const psa_key_slot_t *slot,
2010 const psa_key_attributes_t *attributes )
2011{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002012 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002013 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002014 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002015 return( PSA_ERROR_INVALID_ARGUMENT );
2016 }
2017
2018 if( attributes->domain_parameters_size != 0 )
2019 {
2020#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002021 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002022 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002023 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002024 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002025 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002026
Steven Cooreman7f391872020-07-30 14:57:44 +02002027 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002028 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002029 slot->data.key.data,
2030 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002031 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002032 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002033 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002034
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002035 mbedtls_mpi_init( &actual );
2036 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002037 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002038 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002039 mbedtls_rsa_free( rsa );
2040 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041 if( ret != 0 )
2042 goto rsa_exit;
2043 ret = mbedtls_mpi_read_binary( &required,
2044 attributes->domain_parameters,
2045 attributes->domain_parameters_size );
2046 if( ret != 0 )
2047 goto rsa_exit;
2048 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2049 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2050 rsa_exit:
2051 mbedtls_mpi_free( &actual );
2052 mbedtls_mpi_free( &required );
2053 if( ret != 0)
2054 return( mbedtls_to_psa_error( ret ) );
2055 }
2056 else
2057#endif
2058 {
2059 return( PSA_ERROR_INVALID_ARGUMENT );
2060 }
2061 }
2062
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002063 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002064 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002065 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002066 return( PSA_ERROR_INVALID_ARGUMENT );
2067 }
2068
2069 return( PSA_SUCCESS );
2070}
2071
Gilles Peskine4747d192019-04-17 15:05:45 +02002072psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002073 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002074 size_t data_length,
2075 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002076{
2077 psa_status_t status;
2078 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002079 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002080
Gilles Peskine0f84d622019-09-12 19:03:13 +02002081 /* Reject zero-length symmetric keys (including raw data key objects).
2082 * This also rejects any key which might be encoded as an empty string,
2083 * which is never valid. */
2084 if( data_length == 0 )
2085 return( PSA_ERROR_INVALID_ARGUMENT );
2086
Gilles Peskinedf179142019-07-15 22:02:14 +02002087 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2088 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002089 if( status != PSA_SUCCESS )
2090 goto exit;
2091
Gilles Peskine5d309672019-07-12 23:47:28 +02002092#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2093 if( driver != NULL )
2094 {
2095 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002096 /* The driver should set the number of key bits, however in
2097 * case it doesn't, we initialize bits to an invalid value. */
2098 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002099 if( drv->key_management == NULL ||
2100 drv->key_management->p_import == NULL )
2101 {
2102 status = PSA_ERROR_NOT_SUPPORTED;
2103 goto exit;
2104 }
2105 status = drv->key_management->p_import(
2106 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002107 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002108 &bits );
2109 if( status != PSA_SUCCESS )
2110 goto exit;
2111 if( bits > PSA_MAX_KEY_BITS )
2112 {
2113 status = PSA_ERROR_NOT_SUPPORTED;
2114 goto exit;
2115 }
2116 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002117 }
2118 else
2119#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2120 {
2121 status = psa_import_key_into_slot( slot, data, data_length );
2122 if( status != PSA_SUCCESS )
2123 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002124 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002125 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002126 if( status != PSA_SUCCESS )
2127 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002128
Gilles Peskine011e4282019-06-26 18:34:38 +02002129 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002130exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002131 if( status != PSA_SUCCESS )
2132 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002133 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002134 *handle = 0;
2135 }
2136 return( status );
2137}
2138
Gilles Peskined7729582019-08-05 15:55:54 +02002139#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2140psa_status_t mbedtls_psa_register_se_key(
2141 const psa_key_attributes_t *attributes )
2142{
2143 psa_status_t status;
2144 psa_key_slot_t *slot = NULL;
2145 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002146 psa_key_handle_t handle = 0;
2147
2148 /* Leaving attributes unspecified is not currently supported.
2149 * It could make sense to query the key type and size from the
2150 * secure element, but not all secure elements support this
2151 * and the driver HAL doesn't currently support it. */
2152 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2153 return( PSA_ERROR_NOT_SUPPORTED );
2154 if( psa_get_key_bits( attributes ) == 0 )
2155 return( PSA_ERROR_NOT_SUPPORTED );
2156
2157 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2158 &handle, &slot, &driver );
2159 if( status != PSA_SUCCESS )
2160 goto exit;
2161
Gilles Peskined7729582019-08-05 15:55:54 +02002162 status = psa_finish_key_creation( slot, driver );
2163
2164exit:
2165 if( status != PSA_SUCCESS )
2166 {
2167 psa_fail_key_creation( slot, driver );
2168 }
2169 /* Registration doesn't keep the key in RAM. */
2170 psa_close_key( handle );
2171 return( status );
2172}
2173#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2174
Gilles Peskinef603c712019-01-19 13:40:11 +01002175static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002176 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002177{
2178 psa_status_t status;
2179 uint8_t *buffer = NULL;
2180 size_t buffer_size = 0;
2181 size_t length;
2182
Gilles Peskine8e338702019-07-30 20:06:31 +02002183 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002184 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002185 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002186 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002187 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002188 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2189 if( status != PSA_SUCCESS )
2190 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002191 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002192 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002193
2194exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002195 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002196 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002197 return( status );
2198}
2199
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002200psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2201 const psa_key_attributes_t *specified_attributes,
2202 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002203{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002204 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002205 psa_key_slot_t *source_slot = NULL;
2206 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002207 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002208 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002209
Gilles Peskine28f8f302019-07-24 13:30:31 +02002210 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002211 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002212 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002213 goto exit;
2214
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002215 status = psa_validate_optional_attributes( source_slot,
2216 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002217 if( status != PSA_SUCCESS )
2218 goto exit;
2219
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002220 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002221 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002222 if( status != PSA_SUCCESS )
2223 goto exit;
2224
Gilles Peskinedf179142019-07-15 22:02:14 +02002225 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2226 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002227 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002228 if( status != PSA_SUCCESS )
2229 goto exit;
2230
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002231#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2232 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002233 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002234 /* Copying to a secure element is not implemented yet. */
2235 status = PSA_ERROR_NOT_SUPPORTED;
2236 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002237 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002238#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002239
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002240 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002241 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002242 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002243
Gilles Peskine011e4282019-06-26 18:34:38 +02002244 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002245exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002246 if( status != PSA_SUCCESS )
2247 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002248 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002249 *target_handle = 0;
2250 }
2251 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002252}
2253
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002254
2255
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002256/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002257/* Message digests */
2258/****************************************************************/
2259
Gilles Peskineb16841e2019-10-10 20:36:12 +02002260#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002261static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002262{
2263 switch( alg )
2264 {
2265#if defined(MBEDTLS_MD2_C)
2266 case PSA_ALG_MD2:
2267 return( &mbedtls_md2_info );
2268#endif
2269#if defined(MBEDTLS_MD4_C)
2270 case PSA_ALG_MD4:
2271 return( &mbedtls_md4_info );
2272#endif
2273#if defined(MBEDTLS_MD5_C)
2274 case PSA_ALG_MD5:
2275 return( &mbedtls_md5_info );
2276#endif
2277#if defined(MBEDTLS_RIPEMD160_C)
2278 case PSA_ALG_RIPEMD160:
2279 return( &mbedtls_ripemd160_info );
2280#endif
2281#if defined(MBEDTLS_SHA1_C)
2282 case PSA_ALG_SHA_1:
2283 return( &mbedtls_sha1_info );
2284#endif
2285#if defined(MBEDTLS_SHA256_C)
2286 case PSA_ALG_SHA_224:
2287 return( &mbedtls_sha224_info );
2288 case PSA_ALG_SHA_256:
2289 return( &mbedtls_sha256_info );
2290#endif
2291#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002292#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002293 case PSA_ALG_SHA_384:
2294 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002295#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002296 case PSA_ALG_SHA_512:
2297 return( &mbedtls_sha512_info );
2298#endif
2299 default:
2300 return( NULL );
2301 }
2302}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002303#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002304
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002305psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2306{
2307 switch( operation->alg )
2308 {
Gilles Peskine81736312018-06-26 15:04:31 +02002309 case 0:
2310 /* The object has (apparently) been initialized but it is not
2311 * in use. It's ok to call abort on such an object, and there's
2312 * nothing to do. */
2313 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002314#if defined(MBEDTLS_MD2_C)
2315 case PSA_ALG_MD2:
2316 mbedtls_md2_free( &operation->ctx.md2 );
2317 break;
2318#endif
2319#if defined(MBEDTLS_MD4_C)
2320 case PSA_ALG_MD4:
2321 mbedtls_md4_free( &operation->ctx.md4 );
2322 break;
2323#endif
2324#if defined(MBEDTLS_MD5_C)
2325 case PSA_ALG_MD5:
2326 mbedtls_md5_free( &operation->ctx.md5 );
2327 break;
2328#endif
2329#if defined(MBEDTLS_RIPEMD160_C)
2330 case PSA_ALG_RIPEMD160:
2331 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2332 break;
2333#endif
2334#if defined(MBEDTLS_SHA1_C)
2335 case PSA_ALG_SHA_1:
2336 mbedtls_sha1_free( &operation->ctx.sha1 );
2337 break;
2338#endif
2339#if defined(MBEDTLS_SHA256_C)
2340 case PSA_ALG_SHA_224:
2341 case PSA_ALG_SHA_256:
2342 mbedtls_sha256_free( &operation->ctx.sha256 );
2343 break;
2344#endif
2345#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002346#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002348#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002349 case PSA_ALG_SHA_512:
2350 mbedtls_sha512_free( &operation->ctx.sha512 );
2351 break;
2352#endif
2353 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002354 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002355 }
2356 operation->alg = 0;
2357 return( PSA_SUCCESS );
2358}
2359
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002360psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002361 psa_algorithm_t alg )
2362{
Janos Follath24eed8d2019-11-22 13:21:35 +00002363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002364
2365 /* A context must be freshly initialized before it can be set up. */
2366 if( operation->alg != 0 )
2367 {
2368 return( PSA_ERROR_BAD_STATE );
2369 }
2370
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002371 switch( alg )
2372 {
2373#if defined(MBEDTLS_MD2_C)
2374 case PSA_ALG_MD2:
2375 mbedtls_md2_init( &operation->ctx.md2 );
2376 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2377 break;
2378#endif
2379#if defined(MBEDTLS_MD4_C)
2380 case PSA_ALG_MD4:
2381 mbedtls_md4_init( &operation->ctx.md4 );
2382 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2383 break;
2384#endif
2385#if defined(MBEDTLS_MD5_C)
2386 case PSA_ALG_MD5:
2387 mbedtls_md5_init( &operation->ctx.md5 );
2388 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2389 break;
2390#endif
2391#if defined(MBEDTLS_RIPEMD160_C)
2392 case PSA_ALG_RIPEMD160:
2393 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2394 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2395 break;
2396#endif
2397#if defined(MBEDTLS_SHA1_C)
2398 case PSA_ALG_SHA_1:
2399 mbedtls_sha1_init( &operation->ctx.sha1 );
2400 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2401 break;
2402#endif
2403#if defined(MBEDTLS_SHA256_C)
2404 case PSA_ALG_SHA_224:
2405 mbedtls_sha256_init( &operation->ctx.sha256 );
2406 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2407 break;
2408 case PSA_ALG_SHA_256:
2409 mbedtls_sha256_init( &operation->ctx.sha256 );
2410 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2411 break;
2412#endif
2413#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002414#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002415 case PSA_ALG_SHA_384:
2416 mbedtls_sha512_init( &operation->ctx.sha512 );
2417 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2418 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002419#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002420 case PSA_ALG_SHA_512:
2421 mbedtls_sha512_init( &operation->ctx.sha512 );
2422 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2423 break;
2424#endif
2425 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002426 return( PSA_ALG_IS_HASH( alg ) ?
2427 PSA_ERROR_NOT_SUPPORTED :
2428 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002429 }
2430 if( ret == 0 )
2431 operation->alg = alg;
2432 else
2433 psa_hash_abort( operation );
2434 return( mbedtls_to_psa_error( ret ) );
2435}
2436
2437psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2438 const uint8_t *input,
2439 size_t input_length )
2440{
Janos Follath24eed8d2019-11-22 13:21:35 +00002441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002442
2443 /* Don't require hash implementations to behave correctly on a
2444 * zero-length input, which may have an invalid pointer. */
2445 if( input_length == 0 )
2446 return( PSA_SUCCESS );
2447
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002448 switch( operation->alg )
2449 {
2450#if defined(MBEDTLS_MD2_C)
2451 case PSA_ALG_MD2:
2452 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2453 input, input_length );
2454 break;
2455#endif
2456#if defined(MBEDTLS_MD4_C)
2457 case PSA_ALG_MD4:
2458 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2459 input, input_length );
2460 break;
2461#endif
2462#if defined(MBEDTLS_MD5_C)
2463 case PSA_ALG_MD5:
2464 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2465 input, input_length );
2466 break;
2467#endif
2468#if defined(MBEDTLS_RIPEMD160_C)
2469 case PSA_ALG_RIPEMD160:
2470 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2471 input, input_length );
2472 break;
2473#endif
2474#if defined(MBEDTLS_SHA1_C)
2475 case PSA_ALG_SHA_1:
2476 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2477 input, input_length );
2478 break;
2479#endif
2480#if defined(MBEDTLS_SHA256_C)
2481 case PSA_ALG_SHA_224:
2482 case PSA_ALG_SHA_256:
2483 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2484 input, input_length );
2485 break;
2486#endif
2487#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002488#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002489 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002490#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002491 case PSA_ALG_SHA_512:
2492 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2493 input, input_length );
2494 break;
2495#endif
2496 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002497 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002498 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002499
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002500 if( ret != 0 )
2501 psa_hash_abort( operation );
2502 return( mbedtls_to_psa_error( ret ) );
2503}
2504
2505psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2506 uint8_t *hash,
2507 size_t hash_size,
2508 size_t *hash_length )
2509{
itayzafrir40835d42018-08-02 13:14:17 +03002510 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002511 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002512 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002513
2514 /* Fill the output buffer with something that isn't a valid hash
2515 * (barring an attack on the hash and deliberately-crafted input),
2516 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002517 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002518 /* If hash_size is 0 then hash may be NULL and then the
2519 * call to memset would have undefined behavior. */
2520 if( hash_size != 0 )
2521 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002522
2523 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002524 {
2525 status = PSA_ERROR_BUFFER_TOO_SMALL;
2526 goto exit;
2527 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002528
2529 switch( operation->alg )
2530 {
2531#if defined(MBEDTLS_MD2_C)
2532 case PSA_ALG_MD2:
2533 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2534 break;
2535#endif
2536#if defined(MBEDTLS_MD4_C)
2537 case PSA_ALG_MD4:
2538 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2539 break;
2540#endif
2541#if defined(MBEDTLS_MD5_C)
2542 case PSA_ALG_MD5:
2543 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2544 break;
2545#endif
2546#if defined(MBEDTLS_RIPEMD160_C)
2547 case PSA_ALG_RIPEMD160:
2548 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2549 break;
2550#endif
2551#if defined(MBEDTLS_SHA1_C)
2552 case PSA_ALG_SHA_1:
2553 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2554 break;
2555#endif
2556#if defined(MBEDTLS_SHA256_C)
2557 case PSA_ALG_SHA_224:
2558 case PSA_ALG_SHA_256:
2559 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2560 break;
2561#endif
2562#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002563#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002564 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002565#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002566 case PSA_ALG_SHA_512:
2567 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2568 break;
2569#endif
2570 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002571 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002572 }
itayzafrir40835d42018-08-02 13:14:17 +03002573 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002574
itayzafrir40835d42018-08-02 13:14:17 +03002575exit:
2576 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002577 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002578 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002579 return( psa_hash_abort( operation ) );
2580 }
2581 else
2582 {
2583 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002584 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002585 }
2586}
2587
Gilles Peskine2d277862018-06-18 15:41:12 +02002588psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2589 const uint8_t *hash,
2590 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002591{
2592 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2593 size_t actual_hash_length;
2594 psa_status_t status = psa_hash_finish( operation,
2595 actual_hash, sizeof( actual_hash ),
2596 &actual_hash_length );
2597 if( status != PSA_SUCCESS )
2598 return( status );
2599 if( actual_hash_length != hash_length )
2600 return( PSA_ERROR_INVALID_SIGNATURE );
2601 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2602 return( PSA_ERROR_INVALID_SIGNATURE );
2603 return( PSA_SUCCESS );
2604}
2605
Gilles Peskine0a749c82019-11-28 19:33:58 +01002606psa_status_t psa_hash_compute( psa_algorithm_t alg,
2607 const uint8_t *input, size_t input_length,
2608 uint8_t *hash, size_t hash_size,
2609 size_t *hash_length )
2610{
2611 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2612 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2613
2614 *hash_length = hash_size;
2615 status = psa_hash_setup( &operation, alg );
2616 if( status != PSA_SUCCESS )
2617 goto exit;
2618 status = psa_hash_update( &operation, input, input_length );
2619 if( status != PSA_SUCCESS )
2620 goto exit;
2621 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2622 if( status != PSA_SUCCESS )
2623 goto exit;
2624
2625exit:
2626 if( status == PSA_SUCCESS )
2627 status = psa_hash_abort( &operation );
2628 else
2629 psa_hash_abort( &operation );
2630 return( status );
2631}
2632
2633psa_status_t psa_hash_compare( psa_algorithm_t alg,
2634 const uint8_t *input, size_t input_length,
2635 const uint8_t *hash, size_t hash_length )
2636{
2637 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2638 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2639
2640 status = psa_hash_setup( &operation, alg );
2641 if( status != PSA_SUCCESS )
2642 goto exit;
2643 status = psa_hash_update( &operation, input, input_length );
2644 if( status != PSA_SUCCESS )
2645 goto exit;
2646 status = psa_hash_verify( &operation, hash, hash_length );
2647 if( status != PSA_SUCCESS )
2648 goto exit;
2649
2650exit:
2651 if( status == PSA_SUCCESS )
2652 status = psa_hash_abort( &operation );
2653 else
2654 psa_hash_abort( &operation );
2655 return( status );
2656}
2657
Gilles Peskineeb35d782019-01-22 17:56:16 +01002658psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2659 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002660{
2661 if( target_operation->alg != 0 )
2662 return( PSA_ERROR_BAD_STATE );
2663
2664 switch( source_operation->alg )
2665 {
2666 case 0:
2667 return( PSA_ERROR_BAD_STATE );
2668#if defined(MBEDTLS_MD2_C)
2669 case PSA_ALG_MD2:
2670 mbedtls_md2_clone( &target_operation->ctx.md2,
2671 &source_operation->ctx.md2 );
2672 break;
2673#endif
2674#if defined(MBEDTLS_MD4_C)
2675 case PSA_ALG_MD4:
2676 mbedtls_md4_clone( &target_operation->ctx.md4,
2677 &source_operation->ctx.md4 );
2678 break;
2679#endif
2680#if defined(MBEDTLS_MD5_C)
2681 case PSA_ALG_MD5:
2682 mbedtls_md5_clone( &target_operation->ctx.md5,
2683 &source_operation->ctx.md5 );
2684 break;
2685#endif
2686#if defined(MBEDTLS_RIPEMD160_C)
2687 case PSA_ALG_RIPEMD160:
2688 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2689 &source_operation->ctx.ripemd160 );
2690 break;
2691#endif
2692#if defined(MBEDTLS_SHA1_C)
2693 case PSA_ALG_SHA_1:
2694 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2695 &source_operation->ctx.sha1 );
2696 break;
2697#endif
2698#if defined(MBEDTLS_SHA256_C)
2699 case PSA_ALG_SHA_224:
2700 case PSA_ALG_SHA_256:
2701 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2702 &source_operation->ctx.sha256 );
2703 break;
2704#endif
2705#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002706#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002707 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002708#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002709 case PSA_ALG_SHA_512:
2710 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2711 &source_operation->ctx.sha512 );
2712 break;
2713#endif
2714 default:
2715 return( PSA_ERROR_NOT_SUPPORTED );
2716 }
2717
2718 target_operation->alg = source_operation->alg;
2719 return( PSA_SUCCESS );
2720}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002721
2722
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002723/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002724/* MAC */
2725/****************************************************************/
2726
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002727static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002728 psa_algorithm_t alg,
2729 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002730 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002731 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002732{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002733 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002734 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002735
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002736 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002737 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002738
Gilles Peskine8c9def32018-02-08 10:02:12 +01002739 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2740 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002741 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002742 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002743 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002744 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002745 mode = MBEDTLS_MODE_STREAM;
2746 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002747 case PSA_ALG_CTR:
2748 mode = MBEDTLS_MODE_CTR;
2749 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002750 case PSA_ALG_CFB:
2751 mode = MBEDTLS_MODE_CFB;
2752 break;
2753 case PSA_ALG_OFB:
2754 mode = MBEDTLS_MODE_OFB;
2755 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002756 case PSA_ALG_ECB_NO_PADDING:
2757 mode = MBEDTLS_MODE_ECB;
2758 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002759 case PSA_ALG_CBC_NO_PADDING:
2760 mode = MBEDTLS_MODE_CBC;
2761 break;
2762 case PSA_ALG_CBC_PKCS7:
2763 mode = MBEDTLS_MODE_CBC;
2764 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002765 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002766 mode = MBEDTLS_MODE_CCM;
2767 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002768 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002769 mode = MBEDTLS_MODE_GCM;
2770 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002771 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2772 mode = MBEDTLS_MODE_CHACHAPOLY;
2773 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002774 default:
2775 return( NULL );
2776 }
2777 }
2778 else if( alg == PSA_ALG_CMAC )
2779 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002780 else
2781 return( NULL );
2782
2783 switch( key_type )
2784 {
2785 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002786 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002787 break;
2788 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002789 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2790 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002791 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002792 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002793 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002794 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002795 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2796 * but two-key Triple-DES is functionally three-key Triple-DES
2797 * with K1=K3, so that's how we present it to mbedtls. */
2798 if( key_bits == 128 )
2799 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002800 break;
2801 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002802 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002803 break;
2804 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002805 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002806 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002807 case PSA_KEY_TYPE_CHACHA20:
2808 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2809 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002810 default:
2811 return( NULL );
2812 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002813 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002814 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002815
Jaeden Amero23bbb752018-06-26 14:16:54 +01002816 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2817 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002818}
2819
Gilles Peskinea05219c2018-11-16 16:02:56 +01002820#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002821static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002822{
Gilles Peskine2d277862018-06-18 15:41:12 +02002823 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002824 {
2825 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002826 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002827 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002828 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002829 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002830 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002831 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002832 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002833 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002834 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002835 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002836 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002837 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002838 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002839 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002840 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002841 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002842 return( 128 );
2843 default:
2844 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002845 }
2846}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002847#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002848
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002849/* Initialize the MAC operation structure. Once this function has been
2850 * called, psa_mac_abort can run and will do the right thing. */
2851static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2852 psa_algorithm_t alg )
2853{
2854 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2855
2856 operation->alg = alg;
2857 operation->key_set = 0;
2858 operation->iv_set = 0;
2859 operation->iv_required = 0;
2860 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002861 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002862
2863#if defined(MBEDTLS_CMAC_C)
2864 if( alg == PSA_ALG_CMAC )
2865 {
2866 operation->iv_required = 0;
2867 mbedtls_cipher_init( &operation->ctx.cmac );
2868 status = PSA_SUCCESS;
2869 }
2870 else
2871#endif /* MBEDTLS_CMAC_C */
2872#if defined(MBEDTLS_MD_C)
2873 if( PSA_ALG_IS_HMAC( operation->alg ) )
2874 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002875 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2876 operation->ctx.hmac.hash_ctx.alg = 0;
2877 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002878 }
2879 else
2880#endif /* MBEDTLS_MD_C */
2881 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002882 if( ! PSA_ALG_IS_MAC( alg ) )
2883 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002884 }
2885
2886 if( status != PSA_SUCCESS )
2887 memset( operation, 0, sizeof( *operation ) );
2888 return( status );
2889}
2890
Gilles Peskine01126fa2018-07-12 17:04:55 +02002891#if defined(MBEDTLS_MD_C)
2892static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2893{
Gilles Peskine3f108122018-12-07 18:14:53 +01002894 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002895 return( psa_hash_abort( &hmac->hash_ctx ) );
2896}
2897#endif /* MBEDTLS_MD_C */
2898
Gilles Peskine8c9def32018-02-08 10:02:12 +01002899psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2900{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002901 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002902 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002903 /* The object has (apparently) been initialized but it is not
2904 * in use. It's ok to call abort on such an object, and there's
2905 * nothing to do. */
2906 return( PSA_SUCCESS );
2907 }
2908 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002909#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002910 if( operation->alg == PSA_ALG_CMAC )
2911 {
2912 mbedtls_cipher_free( &operation->ctx.cmac );
2913 }
2914 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002915#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002916#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002917 if( PSA_ALG_IS_HMAC( operation->alg ) )
2918 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002919 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002920 }
2921 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002922#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002923 {
2924 /* Sanity check (shouldn't happen: operation->alg should
2925 * always have been initialized to a valid value). */
2926 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002927 }
Moran Peker41deec42018-04-04 15:43:05 +03002928
Gilles Peskine8c9def32018-02-08 10:02:12 +01002929 operation->alg = 0;
2930 operation->key_set = 0;
2931 operation->iv_set = 0;
2932 operation->iv_required = 0;
2933 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002934 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002935
Gilles Peskine8c9def32018-02-08 10:02:12 +01002936 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002937
2938bad_state:
2939 /* If abort is called on an uninitialized object, we can't trust
2940 * anything. Wipe the object in case it contains confidential data.
2941 * This may result in a memory leak if a pointer gets overwritten,
2942 * but it's too late to do anything about this. */
2943 memset( operation, 0, sizeof( *operation ) );
2944 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002945}
2946
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002947#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002948static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002949 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002950 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002951 const mbedtls_cipher_info_t *cipher_info )
2952{
Janos Follath24eed8d2019-11-22 13:21:35 +00002953 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002954
2955 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002956
2957 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2958 if( ret != 0 )
2959 return( ret );
2960
2961 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002962 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002963 key_bits );
2964 return( ret );
2965}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002966#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002967
Gilles Peskine248051a2018-06-20 16:09:38 +02002968#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002969static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2970 const uint8_t *key,
2971 size_t key_length,
2972 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002973{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002974 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002975 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002976 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002977 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002978 psa_status_t status;
2979
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002980 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2981 * overflow below. This should never trigger if the hash algorithm
2982 * is implemented correctly. */
2983 /* The size checks against the ipad and opad buffers cannot be written
2984 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2985 * because that triggers -Wlogical-op on GCC 7.3. */
2986 if( block_size > sizeof( ipad ) )
2987 return( PSA_ERROR_NOT_SUPPORTED );
2988 if( block_size > sizeof( hmac->opad ) )
2989 return( PSA_ERROR_NOT_SUPPORTED );
2990 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002991 return( PSA_ERROR_NOT_SUPPORTED );
2992
Gilles Peskined223b522018-06-11 18:12:58 +02002993 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002994 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002995 status = psa_hash_compute( hash_alg, key, key_length,
2996 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002997 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002998 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002999 }
Gilles Peskine96889972018-07-12 17:07:03 +02003000 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3001 * but it is permitted. It is common when HMAC is used in HKDF, for
3002 * example. Don't call `memcpy` in the 0-length because `key` could be
3003 * an invalid pointer which would make the behavior undefined. */
3004 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003005 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003006
Gilles Peskined223b522018-06-11 18:12:58 +02003007 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3008 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003009 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003010 ipad[i] ^= 0x36;
3011 memset( ipad + key_length, 0x36, block_size - key_length );
3012
3013 /* Copy the key material from ipad to opad, flipping the requisite bits,
3014 * and filling the rest of opad with the requisite constant. */
3015 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003016 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3017 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003018
Gilles Peskine01126fa2018-07-12 17:04:55 +02003019 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003020 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003021 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003022
Gilles Peskine01126fa2018-07-12 17:04:55 +02003023 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003024
3025cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003026 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003027
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003028 return( status );
3029}
Gilles Peskine248051a2018-06-20 16:09:38 +02003030#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003031
Gilles Peskine89167cb2018-07-08 20:12:23 +02003032static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003033 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003034 psa_algorithm_t alg,
3035 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003036{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003037 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003038 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003039 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003040 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003041 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003042 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003043 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003044
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003045 /* A context must be freshly initialized before it can be set up. */
3046 if( operation->alg != 0 )
3047 {
3048 return( PSA_ERROR_BAD_STATE );
3049 }
3050
Gilles Peskined911eb72018-08-14 15:18:45 +02003051 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003052 if( status != PSA_SUCCESS )
3053 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003054 if( is_sign )
3055 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003056
Gilles Peskine28f8f302019-07-24 13:30:31 +02003057 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003058 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003059 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003060 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003061
Gilles Peskine8c9def32018-02-08 10:02:12 +01003062#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003063 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003064 {
3065 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003066 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003067 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003068 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003069 if( cipher_info == NULL )
3070 {
3071 status = PSA_ERROR_NOT_SUPPORTED;
3072 goto exit;
3073 }
3074 operation->mac_size = cipher_info->block_size;
3075 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3076 status = mbedtls_to_psa_error( ret );
3077 }
3078 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003079#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003080#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003081 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003082 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003083 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003084 if( hash_alg == 0 )
3085 {
3086 status = PSA_ERROR_NOT_SUPPORTED;
3087 goto exit;
3088 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003089
3090 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3091 /* Sanity check. This shouldn't fail on a valid configuration. */
3092 if( operation->mac_size == 0 ||
3093 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3094 {
3095 status = PSA_ERROR_NOT_SUPPORTED;
3096 goto exit;
3097 }
3098
Gilles Peskine8e338702019-07-30 20:06:31 +02003099 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003100 {
3101 status = PSA_ERROR_INVALID_ARGUMENT;
3102 goto exit;
3103 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003104
Gilles Peskine01126fa2018-07-12 17:04:55 +02003105 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003106 slot->data.key.data,
3107 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003108 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003109 }
3110 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003111#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003112 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003113 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003114 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003115 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003116
Gilles Peskined911eb72018-08-14 15:18:45 +02003117 if( truncated == 0 )
3118 {
3119 /* The "normal" case: untruncated algorithm. Nothing to do. */
3120 }
3121 else if( truncated < 4 )
3122 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003123 /* A very short MAC is too short for security since it can be
3124 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3125 * so we make this our minimum, even though 32 bits is still
3126 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003127 status = PSA_ERROR_NOT_SUPPORTED;
3128 }
3129 else if( truncated > operation->mac_size )
3130 {
3131 /* It's impossible to "truncate" to a larger length. */
3132 status = PSA_ERROR_INVALID_ARGUMENT;
3133 }
3134 else
3135 operation->mac_size = truncated;
3136
Gilles Peskinefbfac682018-07-08 20:51:54 +02003137exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003138 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003139 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003140 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003141 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003142 else
3143 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003144 operation->key_set = 1;
3145 }
3146 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003147}
3148
Gilles Peskine89167cb2018-07-08 20:12:23 +02003149psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003150 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003151 psa_algorithm_t alg )
3152{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003153 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003154}
3155
3156psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003157 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003158 psa_algorithm_t alg )
3159{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003160 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003161}
3162
Gilles Peskine8c9def32018-02-08 10:02:12 +01003163psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3164 const uint8_t *input,
3165 size_t input_length )
3166{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003167 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003168 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003169 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003170 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003171 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003172 operation->has_input = 1;
3173
Gilles Peskine8c9def32018-02-08 10:02:12 +01003174#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003175 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003176 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003177 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3178 input, input_length );
3179 status = mbedtls_to_psa_error( ret );
3180 }
3181 else
3182#endif /* MBEDTLS_CMAC_C */
3183#if defined(MBEDTLS_MD_C)
3184 if( PSA_ALG_IS_HMAC( operation->alg ) )
3185 {
3186 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3187 input_length );
3188 }
3189 else
3190#endif /* MBEDTLS_MD_C */
3191 {
3192 /* This shouldn't happen if `operation` was initialized by
3193 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003194 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003195 }
3196
Gilles Peskinefbfac682018-07-08 20:51:54 +02003197 if( status != PSA_SUCCESS )
3198 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003199 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003200}
3201
Gilles Peskine01126fa2018-07-12 17:04:55 +02003202#if defined(MBEDTLS_MD_C)
3203static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3204 uint8_t *mac,
3205 size_t mac_size )
3206{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003207 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003208 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3209 size_t hash_size = 0;
3210 size_t block_size = psa_get_hash_block_size( hash_alg );
3211 psa_status_t status;
3212
Gilles Peskine01126fa2018-07-12 17:04:55 +02003213 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3214 if( status != PSA_SUCCESS )
3215 return( status );
3216 /* From here on, tmp needs to be wiped. */
3217
3218 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3219 if( status != PSA_SUCCESS )
3220 goto exit;
3221
3222 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3223 if( status != PSA_SUCCESS )
3224 goto exit;
3225
3226 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3227 if( status != PSA_SUCCESS )
3228 goto exit;
3229
Gilles Peskined911eb72018-08-14 15:18:45 +02003230 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3231 if( status != PSA_SUCCESS )
3232 goto exit;
3233
3234 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003235
3236exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003237 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003238 return( status );
3239}
3240#endif /* MBEDTLS_MD_C */
3241
mohammad16036df908f2018-04-02 08:34:15 -07003242static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003243 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003244 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003245{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003246 if( ! operation->key_set )
3247 return( PSA_ERROR_BAD_STATE );
3248 if( operation->iv_required && ! operation->iv_set )
3249 return( PSA_ERROR_BAD_STATE );
3250
Gilles Peskine8c9def32018-02-08 10:02:12 +01003251 if( mac_size < operation->mac_size )
3252 return( PSA_ERROR_BUFFER_TOO_SMALL );
3253
Gilles Peskine8c9def32018-02-08 10:02:12 +01003254#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003255 if( operation->alg == PSA_ALG_CMAC )
3256 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003257 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3258 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3259 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003260 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003261 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003262 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003263 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003264 else
3265#endif /* MBEDTLS_CMAC_C */
3266#if defined(MBEDTLS_MD_C)
3267 if( PSA_ALG_IS_HMAC( operation->alg ) )
3268 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003269 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003270 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003271 }
3272 else
3273#endif /* MBEDTLS_MD_C */
3274 {
3275 /* This shouldn't happen if `operation` was initialized by
3276 * a setup function. */
3277 return( PSA_ERROR_BAD_STATE );
3278 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003279}
3280
Gilles Peskineacd4be32018-07-08 19:56:25 +02003281psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3282 uint8_t *mac,
3283 size_t mac_size,
3284 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003285{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003286 psa_status_t status;
3287
Jaeden Amero252ef282019-02-15 14:05:35 +00003288 if( operation->alg == 0 )
3289 {
3290 return( PSA_ERROR_BAD_STATE );
3291 }
3292
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003293 /* Fill the output buffer with something that isn't a valid mac
3294 * (barring an attack on the mac and deliberately-crafted input),
3295 * in case the caller doesn't check the return status properly. */
3296 *mac_length = mac_size;
3297 /* If mac_size is 0 then mac may be NULL and then the
3298 * call to memset would have undefined behavior. */
3299 if( mac_size != 0 )
3300 memset( mac, '!', mac_size );
3301
Gilles Peskine89167cb2018-07-08 20:12:23 +02003302 if( ! operation->is_sign )
3303 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003304 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003305 }
mohammad16036df908f2018-04-02 08:34:15 -07003306
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003307 status = psa_mac_finish_internal( operation, mac, mac_size );
3308
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003309 if( status == PSA_SUCCESS )
3310 {
3311 status = psa_mac_abort( operation );
3312 if( status == PSA_SUCCESS )
3313 *mac_length = operation->mac_size;
3314 else
3315 memset( mac, '!', mac_size );
3316 }
3317 else
3318 psa_mac_abort( operation );
3319 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003320}
3321
Gilles Peskineacd4be32018-07-08 19:56:25 +02003322psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3323 const uint8_t *mac,
3324 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003325{
Gilles Peskine828ed142018-06-18 23:25:51 +02003326 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003327 psa_status_t status;
3328
Jaeden Amero252ef282019-02-15 14:05:35 +00003329 if( operation->alg == 0 )
3330 {
3331 return( PSA_ERROR_BAD_STATE );
3332 }
3333
Gilles Peskine89167cb2018-07-08 20:12:23 +02003334 if( operation->is_sign )
3335 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003336 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003337 }
3338 if( operation->mac_size != mac_length )
3339 {
3340 status = PSA_ERROR_INVALID_SIGNATURE;
3341 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003342 }
mohammad16036df908f2018-04-02 08:34:15 -07003343
3344 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003345 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003346 if( status != PSA_SUCCESS )
3347 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003348
3349 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3350 status = PSA_ERROR_INVALID_SIGNATURE;
3351
3352cleanup:
3353 if( status == PSA_SUCCESS )
3354 status = psa_mac_abort( operation );
3355 else
3356 psa_mac_abort( operation );
3357
Gilles Peskine3f108122018-12-07 18:14:53 +01003358 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003359
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003360 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003361}
3362
3363
Gilles Peskine20035e32018-02-03 22:44:14 +01003364
Gilles Peskine20035e32018-02-03 22:44:14 +01003365/****************************************************************/
3366/* Asymmetric cryptography */
3367/****************************************************************/
3368
Gilles Peskine2b450e32018-06-27 15:42:46 +02003369#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003370/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003371 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003372static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3373 size_t hash_length,
3374 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003375{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003376 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003377 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003378 *md_alg = mbedtls_md_get_type( md_info );
3379
3380 /* The Mbed TLS RSA module uses an unsigned int for hash length
3381 * parameters. Validate that it fits so that we don't risk an
3382 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003383#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003384 if( hash_length > UINT_MAX )
3385 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003386#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003387
3388#if defined(MBEDTLS_PKCS1_V15)
3389 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3390 * must be correct. */
3391 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3392 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003393 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003394 if( md_info == NULL )
3395 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003396 if( mbedtls_md_get_size( md_info ) != hash_length )
3397 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003398 }
3399#endif /* MBEDTLS_PKCS1_V15 */
3400
3401#if defined(MBEDTLS_PKCS1_V21)
3402 /* PSS requires a hash internally. */
3403 if( PSA_ALG_IS_RSA_PSS( alg ) )
3404 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003405 if( md_info == NULL )
3406 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003407 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003408#endif /* MBEDTLS_PKCS1_V21 */
3409
Gilles Peskine61b91d42018-06-08 16:09:36 +02003410 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003411}
3412
Gilles Peskine2b450e32018-06-27 15:42:46 +02003413static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3414 psa_algorithm_t alg,
3415 const uint8_t *hash,
3416 size_t hash_length,
3417 uint8_t *signature,
3418 size_t signature_size,
3419 size_t *signature_length )
3420{
3421 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003423 mbedtls_md_type_t md_alg;
3424
3425 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3426 if( status != PSA_SUCCESS )
3427 return( status );
3428
Gilles Peskine630a18a2018-06-29 17:49:35 +02003429 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003430 return( PSA_ERROR_BUFFER_TOO_SMALL );
3431
3432#if defined(MBEDTLS_PKCS1_V15)
3433 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3434 {
3435 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3436 MBEDTLS_MD_NONE );
3437 ret = mbedtls_rsa_pkcs1_sign( rsa,
3438 mbedtls_ctr_drbg_random,
3439 &global_data.ctr_drbg,
3440 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003441 md_alg,
3442 (unsigned int) hash_length,
3443 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003444 signature );
3445 }
3446 else
3447#endif /* MBEDTLS_PKCS1_V15 */
3448#if defined(MBEDTLS_PKCS1_V21)
3449 if( PSA_ALG_IS_RSA_PSS( alg ) )
3450 {
3451 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3452 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3453 mbedtls_ctr_drbg_random,
3454 &global_data.ctr_drbg,
3455 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003456 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003457 (unsigned int) hash_length,
3458 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003459 signature );
3460 }
3461 else
3462#endif /* MBEDTLS_PKCS1_V21 */
3463 {
3464 return( PSA_ERROR_INVALID_ARGUMENT );
3465 }
3466
3467 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003468 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003469 return( mbedtls_to_psa_error( ret ) );
3470}
3471
3472static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3473 psa_algorithm_t alg,
3474 const uint8_t *hash,
3475 size_t hash_length,
3476 const uint8_t *signature,
3477 size_t signature_length )
3478{
3479 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003481 mbedtls_md_type_t md_alg;
3482
3483 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3484 if( status != PSA_SUCCESS )
3485 return( status );
3486
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003487 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3488 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003489
3490#if defined(MBEDTLS_PKCS1_V15)
3491 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3492 {
3493 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3494 MBEDTLS_MD_NONE );
3495 ret = mbedtls_rsa_pkcs1_verify( rsa,
3496 mbedtls_ctr_drbg_random,
3497 &global_data.ctr_drbg,
3498 MBEDTLS_RSA_PUBLIC,
3499 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003500 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003501 hash,
3502 signature );
3503 }
3504 else
3505#endif /* MBEDTLS_PKCS1_V15 */
3506#if defined(MBEDTLS_PKCS1_V21)
3507 if( PSA_ALG_IS_RSA_PSS( alg ) )
3508 {
3509 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3510 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3511 mbedtls_ctr_drbg_random,
3512 &global_data.ctr_drbg,
3513 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003514 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003515 (unsigned int) hash_length,
3516 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003517 signature );
3518 }
3519 else
3520#endif /* MBEDTLS_PKCS1_V21 */
3521 {
3522 return( PSA_ERROR_INVALID_ARGUMENT );
3523 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003524
3525 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3526 * the rest of the signature is invalid". This has little use in
3527 * practice and PSA doesn't report this distinction. */
3528 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3529 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003530 return( mbedtls_to_psa_error( ret ) );
3531}
3532#endif /* MBEDTLS_RSA_C */
3533
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003534#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003535/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3536 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3537 * (even though these functions don't modify it). */
3538static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3539 psa_algorithm_t alg,
3540 const uint8_t *hash,
3541 size_t hash_length,
3542 uint8_t *signature,
3543 size_t signature_size,
3544 size_t *signature_length )
3545{
Janos Follath24eed8d2019-11-22 13:21:35 +00003546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003547 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003548 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003549 mbedtls_mpi_init( &r );
3550 mbedtls_mpi_init( &s );
3551
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003552 if( signature_size < 2 * curve_bytes )
3553 {
3554 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3555 goto cleanup;
3556 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003557
Gilles Peskinea05219c2018-11-16 16:02:56 +01003558#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003559 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3560 {
3561 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3562 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3563 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003564 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3565 &ecp->d, hash,
3566 hash_length, md_alg,
3567 mbedtls_ctr_drbg_random,
3568 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003569 }
3570 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003571#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003572 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003573 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003574 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3575 hash, hash_length,
3576 mbedtls_ctr_drbg_random,
3577 &global_data.ctr_drbg ) );
3578 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003579
3580 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3581 signature,
3582 curve_bytes ) );
3583 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3584 signature + curve_bytes,
3585 curve_bytes ) );
3586
3587cleanup:
3588 mbedtls_mpi_free( &r );
3589 mbedtls_mpi_free( &s );
3590 if( ret == 0 )
3591 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003592 return( mbedtls_to_psa_error( ret ) );
3593}
3594
3595static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3596 const uint8_t *hash,
3597 size_t hash_length,
3598 const uint8_t *signature,
3599 size_t signature_length )
3600{
Janos Follath24eed8d2019-11-22 13:21:35 +00003601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003602 mbedtls_mpi r, s;
3603 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3604 mbedtls_mpi_init( &r );
3605 mbedtls_mpi_init( &s );
3606
3607 if( signature_length != 2 * curve_bytes )
3608 return( PSA_ERROR_INVALID_SIGNATURE );
3609
3610 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3611 signature,
3612 curve_bytes ) );
3613 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3614 signature + curve_bytes,
3615 curve_bytes ) );
3616
Steven Cooremanacda8342020-07-24 23:09:52 +02003617 /* Check whether the public part is loaded. If not, load it. */
3618 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3619 {
3620 MBEDTLS_MPI_CHK(
3621 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3622 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3623 }
3624
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003625 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3626 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003627
3628cleanup:
3629 mbedtls_mpi_free( &r );
3630 mbedtls_mpi_free( &s );
3631 return( mbedtls_to_psa_error( ret ) );
3632}
3633#endif /* MBEDTLS_ECDSA_C */
3634
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003635psa_status_t psa_sign_hash( psa_key_handle_t handle,
3636 psa_algorithm_t alg,
3637 const uint8_t *hash,
3638 size_t hash_length,
3639 uint8_t *signature,
3640 size_t signature_size,
3641 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003642{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003643 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003644 psa_status_t status;
3645
3646 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003647 /* Immediately reject a zero-length signature buffer. This guarantees
3648 * that signature must be a valid pointer. (On the other hand, the hash
3649 * buffer can in principle be empty since it doesn't actually have
3650 * to be a hash.) */
3651 if( signature_size == 0 )
3652 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003653
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003654 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003655 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003656 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003657 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003658 {
3659 status = PSA_ERROR_INVALID_ARGUMENT;
3660 goto exit;
3661 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003662
Steven Cooremancd84cb42020-07-16 20:28:36 +02003663 /* Try any of the available accelerators first */
3664 status = psa_driver_wrapper_sign_hash( slot,
3665 alg,
3666 hash,
3667 hash_length,
3668 signature,
3669 signature_size,
3670 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003671 if( status != PSA_ERROR_NOT_SUPPORTED ||
3672 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003673 goto exit;
3674
Steven Cooreman7a250572020-07-17 16:43:05 +02003675 /* If the operation was not supported by any accelerator, try fallback. */
Gilles Peskine20035e32018-02-03 22:44:14 +01003676#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003677 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003678 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003679 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003680
Steven Cooreman4fed4552020-08-03 14:46:03 +02003681 status = psa_load_rsa_representation( slot->attr.type,
3682 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003683 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003684 &rsa );
3685 if( status != PSA_SUCCESS )
3686 goto exit;
3687
Steven Cooremana2371e52020-07-28 14:30:39 +02003688 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003689 alg,
3690 hash, hash_length,
3691 signature, signature_size,
3692 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003693
Steven Cooremana2371e52020-07-28 14:30:39 +02003694 mbedtls_rsa_free( rsa );
3695 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003696 }
3697 else
3698#endif /* defined(MBEDTLS_RSA_C) */
3699#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003700 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003701 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003702#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003703 if(
3704#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3705 PSA_ALG_IS_ECDSA( alg )
3706#else
3707 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3708#endif
3709 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003710 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003711 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003712 status = psa_load_ecp_representation( slot->attr.type,
3713 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003714 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003715 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003716 if( status != PSA_SUCCESS )
3717 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003718 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003719 alg,
3720 hash, hash_length,
3721 signature, signature_size,
3722 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003723 mbedtls_ecp_keypair_free( ecp );
3724 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003725 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003726 else
3727#endif /* defined(MBEDTLS_ECDSA_C) */
3728 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003729 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003730 }
itayzafrir5c753392018-05-08 11:18:38 +03003731 }
3732 else
3733#endif /* defined(MBEDTLS_ECP_C) */
3734 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003735 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003736 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003737
3738exit:
3739 /* Fill the unused part of the output buffer (the whole buffer on error,
3740 * the trailing part on success) with something that isn't a valid mac
3741 * (barring an attack on the mac and deliberately-crafted input),
3742 * in case the caller doesn't check the return status properly. */
3743 if( status == PSA_SUCCESS )
3744 memset( signature + *signature_length, '!',
3745 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003746 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003747 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003748 /* If signature_size is 0 then we have nothing to do. We must not call
3749 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003750 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003751}
3752
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003753psa_status_t psa_verify_hash( psa_key_handle_t handle,
3754 psa_algorithm_t alg,
3755 const uint8_t *hash,
3756 size_t hash_length,
3757 const uint8_t *signature,
3758 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003759{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003760 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003761 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003762
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003763 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003764 if( status != PSA_SUCCESS )
3765 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003766
Steven Cooreman55ae2172020-07-17 19:46:15 +02003767 /* Try any of the available accelerators first */
3768 status = psa_driver_wrapper_verify_hash( slot,
3769 alg,
3770 hash,
3771 hash_length,
3772 signature,
3773 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003774 if( status != PSA_ERROR_NOT_SUPPORTED ||
3775 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +02003776 return status;
3777
Gilles Peskine61b91d42018-06-08 16:09:36 +02003778#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003779 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003780 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003781 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003782
Steven Cooreman4fed4552020-08-03 14:46:03 +02003783 status = psa_load_rsa_representation( slot->attr.type,
3784 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003785 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003786 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003787 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003788 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02003789
Steven Cooremana2371e52020-07-28 14:30:39 +02003790 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003791 alg,
3792 hash, hash_length,
3793 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003794 mbedtls_rsa_free( rsa );
3795 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003796 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003797 }
3798 else
3799#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003800#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003801 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003802 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003803#if defined(MBEDTLS_ECDSA_C)
3804 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003805 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003806 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003807 status = psa_load_ecp_representation( slot->attr.type,
3808 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003809 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003810 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003811 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003812 return( status );
Steven Cooremana2371e52020-07-28 14:30:39 +02003813 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003814 hash, hash_length,
3815 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003816 mbedtls_ecp_keypair_free( ecp );
3817 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02003818 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02003819 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003820 else
3821#endif /* defined(MBEDTLS_ECDSA_C) */
3822 {
3823 return( PSA_ERROR_INVALID_ARGUMENT );
3824 }
itayzafrir5c753392018-05-08 11:18:38 +03003825 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003826 else
3827#endif /* defined(MBEDTLS_ECP_C) */
3828 {
3829 return( PSA_ERROR_NOT_SUPPORTED );
3830 }
3831}
3832
Gilles Peskine072ac562018-06-30 00:21:29 +02003833#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3834static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3835 mbedtls_rsa_context *rsa )
3836{
3837 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3838 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3839 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3840 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3841}
3842#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3843
Gilles Peskinec5487a82018-12-03 18:08:14 +01003844psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003845 psa_algorithm_t alg,
3846 const uint8_t *input,
3847 size_t input_length,
3848 const uint8_t *salt,
3849 size_t salt_length,
3850 uint8_t *output,
3851 size_t output_size,
3852 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003853{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003854 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003855 psa_status_t status;
3856
Darryl Green5cc689a2018-07-24 15:34:10 +01003857 (void) input;
3858 (void) input_length;
3859 (void) salt;
3860 (void) output;
3861 (void) output_size;
3862
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003863 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003864
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003865 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3866 return( PSA_ERROR_INVALID_ARGUMENT );
3867
Gilles Peskine28f8f302019-07-24 13:30:31 +02003868 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003869 if( status != PSA_SUCCESS )
3870 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003871 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3872 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003873 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003874
3875#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003876 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003877 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003878 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003879 status = psa_load_rsa_representation( slot->attr.type,
3880 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003881 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003882 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003883 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003884 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003885
3886 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003887 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003888 status = PSA_ERROR_BUFFER_TOO_SMALL;
3889 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003890 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003891#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003892 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003893 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003894 status = mbedtls_to_psa_error(
3895 mbedtls_rsa_pkcs1_encrypt( rsa,
3896 mbedtls_ctr_drbg_random,
3897 &global_data.ctr_drbg,
3898 MBEDTLS_RSA_PUBLIC,
3899 input_length,
3900 input,
3901 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003902 }
3903 else
3904#endif /* MBEDTLS_PKCS1_V15 */
3905#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003906 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003907 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003908 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003909 status = mbedtls_to_psa_error(
3910 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3911 mbedtls_ctr_drbg_random,
3912 &global_data.ctr_drbg,
3913 MBEDTLS_RSA_PUBLIC,
3914 salt, salt_length,
3915 input_length,
3916 input,
3917 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003918 }
3919 else
3920#endif /* MBEDTLS_PKCS1_V21 */
3921 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003922 status = PSA_ERROR_INVALID_ARGUMENT;
3923 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003924 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003925rsa_exit:
3926 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003927 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003928
Steven Cooremana2371e52020-07-28 14:30:39 +02003929 mbedtls_rsa_free( rsa );
3930 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003931 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003932 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003933 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003934#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935 {
3936 return( PSA_ERROR_NOT_SUPPORTED );
3937 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003938}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003939
Gilles Peskinec5487a82018-12-03 18:08:14 +01003940psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003941 psa_algorithm_t alg,
3942 const uint8_t *input,
3943 size_t input_length,
3944 const uint8_t *salt,
3945 size_t salt_length,
3946 uint8_t *output,
3947 size_t output_size,
3948 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003949{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003950 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003951 psa_status_t status;
3952
Darryl Green5cc689a2018-07-24 15:34:10 +01003953 (void) input;
3954 (void) input_length;
3955 (void) salt;
3956 (void) output;
3957 (void) output_size;
3958
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003959 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003960
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003961 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3962 return( PSA_ERROR_INVALID_ARGUMENT );
3963
Gilles Peskine28f8f302019-07-24 13:30:31 +02003964 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003965 if( status != PSA_SUCCESS )
3966 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003967 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968 return( PSA_ERROR_INVALID_ARGUMENT );
3969
3970#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003971 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003972 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003973 mbedtls_rsa_context *rsa = NULL;
3974 status = psa_load_rsa_representation( slot->attr.type,
3975 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003976 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003977 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003978 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003979 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003980
Steven Cooremana2371e52020-07-28 14:30:39 +02003981 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003982 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003983 status = PSA_ERROR_INVALID_ARGUMENT;
3984 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003985 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003986
3987#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003988 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003989 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003990 status = mbedtls_to_psa_error(
3991 mbedtls_rsa_pkcs1_decrypt( rsa,
3992 mbedtls_ctr_drbg_random,
3993 &global_data.ctr_drbg,
3994 MBEDTLS_RSA_PRIVATE,
3995 output_length,
3996 input,
3997 output,
3998 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003999 }
4000 else
4001#endif /* MBEDTLS_PKCS1_V15 */
4002#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004003 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004004 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004005 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004006 status = mbedtls_to_psa_error(
4007 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4008 mbedtls_ctr_drbg_random,
4009 &global_data.ctr_drbg,
4010 MBEDTLS_RSA_PRIVATE,
4011 salt, salt_length,
4012 output_length,
4013 input,
4014 output,
4015 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004016 }
4017 else
4018#endif /* MBEDTLS_PKCS1_V21 */
4019 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004020 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004021 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004022
Steven Cooreman4fed4552020-08-03 14:46:03 +02004023rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004024 mbedtls_rsa_free( rsa );
4025 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004026 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004027 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004028 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004029#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004030 {
4031 return( PSA_ERROR_NOT_SUPPORTED );
4032 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004033}
Gilles Peskine20035e32018-02-03 22:44:14 +01004034
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004035
4036
mohammad1603503973b2018-03-12 15:59:30 +02004037/****************************************************************/
4038/* Symmetric cryptography */
4039/****************************************************************/
4040
Gilles Peskinee553c652018-06-04 16:22:46 +02004041static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004042 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004043 psa_algorithm_t alg,
4044 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004045{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004046 int ret = 0;
4047 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004048 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004049 size_t key_bits;
4050 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004051 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4052 PSA_KEY_USAGE_ENCRYPT :
4053 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004054
Steven Cooremand3feccd2020-09-01 15:56:14 +02004055 /* A context must be freshly initialized before it can be set up. */
4056 if( operation->alg != 0 )
4057 return( PSA_ERROR_BAD_STATE );
4058
Steven Cooremana07b9972020-09-10 14:54:14 +02004059 /* The requested algorithm must be one that can be processed by cipher. */
4060 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004061 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004062
Steven Cooremanef8575e2020-09-11 11:44:50 +02004063 /* Fetch key material from key storage. */
4064 status = psa_get_key_from_slot( handle, &slot, usage, alg );
4065 if( status != PSA_SUCCESS )
4066 goto exit;
4067
4068 /* Initialize the operation struct members, except for alg. The alg member
4069 * is used to indicate to psa_cipher_abort that there are resources to free,
4070 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004071 operation->key_set = 0;
4072 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004073 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004074 operation->iv_size = 0;
4075 operation->block_size = 0;
4076 if( alg == PSA_ALG_ECB_NO_PADDING )
4077 operation->iv_required = 0;
4078 else
4079 operation->iv_required = 1;
4080
Steven Cooremana07b9972020-09-10 14:54:14 +02004081 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004082 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004083 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004084 slot,
4085 alg );
4086 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004087 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004088 slot,
4089 alg );
4090
Steven Cooremanef8575e2020-09-11 11:44:50 +02004091 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004092 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004093 /* Once the driver context is initialised, it needs to be freed using
4094 * psa_cipher_abort. Indicate this through setting alg. */
4095 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004096 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004097
Steven Cooremand3feccd2020-09-01 15:56:14 +02004098 if( status != PSA_ERROR_NOT_SUPPORTED ||
4099 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4100 goto exit;
4101
Steven Cooremana07b9972020-09-10 14:54:14 +02004102 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004103 * available for the given algorithm & key. */
4104 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004105
Steven Cooremana07b9972020-09-10 14:54:14 +02004106 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004107 * psa_cipher_abort. Indicate there is something to be freed through setting
4108 * alg, and indicate the operation is being done using mbedtls crypto through
4109 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004110 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004111 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004112
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004113 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004114 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004115 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004116 {
4117 status = PSA_ERROR_NOT_SUPPORTED;
4118 goto exit;
4119 }
mohammad1603503973b2018-03-12 15:59:30 +02004120
mohammad1603503973b2018-03-12 15:59:30 +02004121 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004122 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004123 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004124
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004125#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004126 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004127 {
4128 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004129 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004130 memcpy( keys, slot->data.key.data, 16 );
4131 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004132 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4133 keys,
4134 192, cipher_operation );
4135 }
4136 else
4137#endif
4138 {
4139 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004140 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004141 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004142 }
Moran Peker41deec42018-04-04 15:43:05 +03004143 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004144 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004145
mohammad16038481e742018-03-18 13:57:31 +02004146#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004147 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004148 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004149 case PSA_ALG_CBC_NO_PADDING:
4150 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4151 MBEDTLS_PADDING_NONE );
4152 break;
4153 case PSA_ALG_CBC_PKCS7:
4154 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4155 MBEDTLS_PADDING_PKCS7 );
4156 break;
4157 default:
4158 /* The algorithm doesn't involve padding. */
4159 ret = 0;
4160 break;
4161 }
4162 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004163 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004164#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4165
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004166 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004167 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004168 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4169 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004170 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004171 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004172 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004173#if defined(MBEDTLS_CHACHA20_C)
4174 else
4175 if( alg == PSA_ALG_CHACHA20 )
4176 operation->iv_size = 12;
4177#endif
mohammad1603503973b2018-03-12 15:59:30 +02004178
Steven Cooreman7df02922020-09-09 15:28:49 +02004179 status = PSA_SUCCESS;
4180
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004181exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004182 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004183 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004184 if( status == PSA_SUCCESS )
4185 {
4186 /* Update operation flags for both driver and software implementations */
4187 operation->key_set = 1;
4188 }
4189 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004190 psa_cipher_abort( operation );
4191 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004192}
4193
Gilles Peskinefe119512018-07-08 21:39:34 +02004194psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004195 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004196 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004197{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004198 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004199}
4200
Gilles Peskinefe119512018-07-08 21:39:34 +02004201psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004202 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004203 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004204{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004205 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004206}
4207
Gilles Peskinefe119512018-07-08 21:39:34 +02004208psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004209 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004210 size_t iv_size,
4211 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004212{
itayzafrir534bd7c2018-08-02 13:56:32 +03004213 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004215 if( operation->iv_set || ! operation->iv_required )
4216 {
4217 return( PSA_ERROR_BAD_STATE );
4218 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004219
Steven Cooremanef8575e2020-09-11 11:44:50 +02004220 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004221 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004222 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004223 iv,
4224 iv_size,
4225 iv_length );
4226 goto exit;
4227 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004228
Moran Peker41deec42018-04-04 15:43:05 +03004229 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004230 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004231 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004232 goto exit;
4233 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004234 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4235 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004236 if( ret != 0 )
4237 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004238 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004239 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004240 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004241
mohammad16038481e742018-03-18 13:57:31 +02004242 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004243 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004244
Moran Peker395db872018-05-31 14:07:14 +03004245exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004246 if( status == PSA_SUCCESS )
4247 operation->iv_set = 1;
4248 else
Moran Peker395db872018-05-31 14:07:14 +03004249 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004250 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004251}
4252
Gilles Peskinefe119512018-07-08 21:39:34 +02004253psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004254 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004255 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004256{
itayzafrir534bd7c2018-08-02 13:56:32 +03004257 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004258 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004259 if( operation->iv_set || ! operation->iv_required )
4260 {
4261 return( PSA_ERROR_BAD_STATE );
4262 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004263
Steven Cooremanef8575e2020-09-11 11:44:50 +02004264 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004265 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004266 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004267 iv,
4268 iv_length );
4269 goto exit;
4270 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004271
Moran Pekera28258c2018-05-29 16:25:04 +03004272 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004273 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004274 status = PSA_ERROR_INVALID_ARGUMENT;
4275 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004276 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004277 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4278 status = mbedtls_to_psa_error( ret );
4279exit:
4280 if( status == PSA_SUCCESS )
4281 operation->iv_set = 1;
4282 else
mohammad1603503973b2018-03-12 15:59:30 +02004283 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004284 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004285}
4286
Steven Cooreman177deba2020-09-07 17:14:14 +02004287/* Process input for which the algorithm is set to ECB mode. This requires
4288 * manual processing, since the PSA API is defined as being able to process
4289 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4290 * underlying mbedtls_cipher_update only takes full blocks. */
4291static psa_status_t psa_cipher_update_ecb_internal(
4292 mbedtls_cipher_context_t *ctx,
4293 const uint8_t *input,
4294 size_t input_length,
4295 uint8_t *output,
4296 size_t output_size,
4297 size_t *output_length )
4298{
4299 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4300 size_t block_size = ctx->cipher_info->block_size;
4301 size_t internal_output_length = 0;
4302 *output_length = 0;
4303
4304 if( input_length == 0 )
4305 {
4306 status = PSA_SUCCESS;
4307 goto exit;
4308 }
4309
4310 if( ctx->unprocessed_len > 0 )
4311 {
4312 /* Fill up to block size, and run the block if there's a full one. */
4313 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4314
4315 if( input_length < bytes_to_copy )
4316 bytes_to_copy = input_length;
4317
4318 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4319 input, bytes_to_copy );
4320 input_length -= bytes_to_copy;
4321 input += bytes_to_copy;
4322 ctx->unprocessed_len += bytes_to_copy;
4323
4324 if( ctx->unprocessed_len == block_size )
4325 {
4326 status = mbedtls_to_psa_error(
4327 mbedtls_cipher_update( ctx,
4328 ctx->unprocessed_data,
4329 block_size,
4330 output, &internal_output_length ) );
4331
4332 if( status != PSA_SUCCESS )
4333 goto exit;
4334
4335 output += internal_output_length;
4336 output_size -= internal_output_length;
4337 *output_length += internal_output_length;
4338 ctx->unprocessed_len = 0;
4339 }
4340 }
4341
4342 while( input_length >= block_size )
4343 {
4344 /* Run all full blocks we have, one by one */
4345 status = mbedtls_to_psa_error(
4346 mbedtls_cipher_update( ctx, input,
4347 block_size,
4348 output, &internal_output_length ) );
4349
4350 if( status != PSA_SUCCESS )
4351 goto exit;
4352
4353 input_length -= block_size;
4354 input += block_size;
4355
4356 output += internal_output_length;
4357 output_size -= internal_output_length;
4358 *output_length += internal_output_length;
4359 }
4360
4361 if( input_length > 0 )
4362 {
4363 /* Save unprocessed bytes for later processing */
4364 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4365 input, input_length );
4366 ctx->unprocessed_len += input_length;
4367 }
4368
4369 status = PSA_SUCCESS;
4370
4371exit:
4372 return( status );
4373}
4374
Gilles Peskinee553c652018-06-04 16:22:46 +02004375psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4376 const uint8_t *input,
4377 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004378 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004379 size_t output_size,
4380 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004381{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004382 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004383 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004384 if( operation->alg == 0 )
4385 {
4386 return( PSA_ERROR_BAD_STATE );
4387 }
4388 if( operation->iv_required && ! operation->iv_set )
4389 {
4390 return( PSA_ERROR_BAD_STATE );
4391 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004392
Steven Cooremanef8575e2020-09-11 11:44:50 +02004393 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004394 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004395 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004396 input,
4397 input_length,
4398 output,
4399 output_size,
4400 output_length );
4401 goto exit;
4402 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004403
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004404 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004405 {
4406 /* Take the unprocessed partial block left over from previous
4407 * update calls, if any, plus the input to this call. Remove
4408 * the last partial block, if any. You get the data that will be
4409 * output in this call. */
4410 expected_output_size =
4411 ( operation->ctx.cipher.unprocessed_len + input_length )
4412 / operation->block_size * operation->block_size;
4413 }
4414 else
4415 {
4416 expected_output_size = input_length;
4417 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004418
Gilles Peskine89d789c2018-06-04 17:17:16 +02004419 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004420 {
4421 status = PSA_ERROR_BUFFER_TOO_SMALL;
4422 goto exit;
4423 }
mohammad160382759612018-03-12 18:16:40 +02004424
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004425 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4426 {
4427 /* mbedtls_cipher_update has an API inconsistency: it will only
4428 * process a single block at a time in ECB mode. Abstract away that
4429 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004430 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4431 input,
4432 input_length,
4433 output,
4434 output_size,
4435 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004436 }
4437 else
4438 {
4439 status = mbedtls_to_psa_error(
4440 mbedtls_cipher_update( &operation->ctx.cipher, input,
4441 input_length, output, output_length ) );
4442 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004443exit:
4444 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004445 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004446 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004447}
4448
Gilles Peskinee553c652018-06-04 16:22:46 +02004449psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4450 uint8_t *output,
4451 size_t output_size,
4452 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004453{
David Saadab4ecc272019-02-14 13:48:10 +02004454 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004455 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004456 if( operation->alg == 0 )
4457 {
4458 return( PSA_ERROR_BAD_STATE );
4459 }
4460 if( operation->iv_required && ! operation->iv_set )
4461 {
4462 return( PSA_ERROR_BAD_STATE );
4463 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004464
Steven Cooremanef8575e2020-09-11 11:44:50 +02004465 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004466 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004467 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004468 output,
4469 output_size,
4470 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004471 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004472 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004473
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004474 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004475 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004476 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
4477 ( operation->alg == PSA_ALG_CBC_NO_PADDING &&
Steven Cooremana6033e92020-08-25 11:47:50 +02004478 operation->ctx.cipher.operation == MBEDTLS_ENCRYPT ) )
4479 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004480 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004481 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004482 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004483 }
4484
Steven Cooremanef8575e2020-09-11 11:44:50 +02004485 status = mbedtls_to_psa_error(
4486 mbedtls_cipher_finish( &operation->ctx.cipher,
4487 temp_output_buffer,
4488 output_length ) );
4489 if( status != PSA_SUCCESS )
4490 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004491
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004492 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004493 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004494 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004495 memcpy( output, temp_output_buffer, *output_length );
4496 else
Janos Follath315b51c2018-07-09 16:04:51 +01004497 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004498
Steven Cooremanef8575e2020-09-11 11:44:50 +02004499exit:
4500 if( operation->mbedtls_in_use == 1 )
4501 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004502
Steven Cooremanef8575e2020-09-11 11:44:50 +02004503 if( status == PSA_SUCCESS )
4504 return( psa_cipher_abort( operation ) );
4505 else
4506 {
4507 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004508 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004509
Steven Cooremanef8575e2020-09-11 11:44:50 +02004510 return( status );
4511 }
mohammad1603503973b2018-03-12 15:59:30 +02004512}
4513
Gilles Peskinee553c652018-06-04 16:22:46 +02004514psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4515{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004516 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004517 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004518 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004519 * in use. It's ok to call abort on such an object, and there's
4520 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004521 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004522 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004523
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004524 /* Sanity check (shouldn't happen: operation->alg should
4525 * always have been initialized to a valid value). */
4526 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4527 return( PSA_ERROR_BAD_STATE );
4528
Steven Cooremanef8575e2020-09-11 11:44:50 +02004529 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004530 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004531 else
4532 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004533
Moran Peker41deec42018-04-04 15:43:05 +03004534 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004535 operation->key_set = 0;
4536 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004537 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004538 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004539 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004540 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004541
Moran Peker395db872018-05-31 14:07:14 +03004542 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004543}
4544
Gilles Peskinea0655c32018-04-30 17:06:50 +02004545
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004546
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004547
mohammad16035955c982018-04-26 00:53:03 +03004548/****************************************************************/
4549/* AEAD */
4550/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004551
Gilles Peskineedf9a652018-08-17 18:11:56 +02004552typedef struct
4553{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004554 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004555 const mbedtls_cipher_info_t *cipher_info;
4556 union
4557 {
4558#if defined(MBEDTLS_CCM_C)
4559 mbedtls_ccm_context ccm;
4560#endif /* MBEDTLS_CCM_C */
4561#if defined(MBEDTLS_GCM_C)
4562 mbedtls_gcm_context gcm;
4563#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004564#if defined(MBEDTLS_CHACHAPOLY_C)
4565 mbedtls_chachapoly_context chachapoly;
4566#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004567 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004568 psa_algorithm_t core_alg;
4569 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004570 uint8_t tag_length;
4571} aead_operation_t;
4572
Gilles Peskine30a9e412019-01-14 18:36:12 +01004573static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004574{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004575 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004576 {
4577#if defined(MBEDTLS_CCM_C)
4578 case PSA_ALG_CCM:
4579 mbedtls_ccm_free( &operation->ctx.ccm );
4580 break;
4581#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004582#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004583 case PSA_ALG_GCM:
4584 mbedtls_gcm_free( &operation->ctx.gcm );
4585 break;
4586#endif /* MBEDTLS_GCM_C */
4587 }
4588}
4589
4590static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004591 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004592 psa_key_usage_t usage,
4593 psa_algorithm_t alg )
4594{
4595 psa_status_t status;
4596 size_t key_bits;
4597 mbedtls_cipher_id_t cipher_id;
4598
Gilles Peskine28f8f302019-07-24 13:30:31 +02004599 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004600 if( status != PSA_SUCCESS )
4601 return( status );
4602
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004603 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004604
4605 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004606 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004607 &cipher_id );
4608 if( operation->cipher_info == NULL )
4609 return( PSA_ERROR_NOT_SUPPORTED );
4610
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004611 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004612 {
4613#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004614 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4615 operation->core_alg = PSA_ALG_CCM;
4616 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004617 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4618 * The call to mbedtls_ccm_encrypt_and_tag or
4619 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004620 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004621 return( PSA_ERROR_INVALID_ARGUMENT );
4622 mbedtls_ccm_init( &operation->ctx.ccm );
4623 status = mbedtls_to_psa_error(
4624 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004625 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004626 (unsigned int) key_bits ) );
4627 if( status != 0 )
4628 goto cleanup;
4629 break;
4630#endif /* MBEDTLS_CCM_C */
4631
4632#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004633 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4634 operation->core_alg = PSA_ALG_GCM;
4635 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004636 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4637 * The call to mbedtls_gcm_crypt_and_tag or
4638 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004639 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004640 return( PSA_ERROR_INVALID_ARGUMENT );
4641 mbedtls_gcm_init( &operation->ctx.gcm );
4642 status = mbedtls_to_psa_error(
4643 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004644 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004645 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004646 if( status != 0 )
4647 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004648 break;
4649#endif /* MBEDTLS_GCM_C */
4650
Gilles Peskine26869f22019-05-06 15:25:00 +02004651#if defined(MBEDTLS_CHACHAPOLY_C)
4652 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4653 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4654 operation->full_tag_length = 16;
4655 /* We only support the default tag length. */
4656 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4657 return( PSA_ERROR_NOT_SUPPORTED );
4658 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4659 status = mbedtls_to_psa_error(
4660 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004661 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004662 if( status != 0 )
4663 goto cleanup;
4664 break;
4665#endif /* MBEDTLS_CHACHAPOLY_C */
4666
Gilles Peskineedf9a652018-08-17 18:11:56 +02004667 default:
4668 return( PSA_ERROR_NOT_SUPPORTED );
4669 }
4670
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004671 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4672 {
4673 status = PSA_ERROR_INVALID_ARGUMENT;
4674 goto cleanup;
4675 }
4676 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004677
Gilles Peskineedf9a652018-08-17 18:11:56 +02004678 return( PSA_SUCCESS );
4679
4680cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004681 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004682 return( status );
4683}
4684
Gilles Peskinec5487a82018-12-03 18:08:14 +01004685psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004686 psa_algorithm_t alg,
4687 const uint8_t *nonce,
4688 size_t nonce_length,
4689 const uint8_t *additional_data,
4690 size_t additional_data_length,
4691 const uint8_t *plaintext,
4692 size_t plaintext_length,
4693 uint8_t *ciphertext,
4694 size_t ciphertext_size,
4695 size_t *ciphertext_length )
4696{
mohammad16035955c982018-04-26 00:53:03 +03004697 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004698 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004699 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004700
mohammad1603f08a5502018-06-03 15:05:47 +03004701 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004702
Gilles Peskinec5487a82018-12-03 18:08:14 +01004703 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004704 if( status != PSA_SUCCESS )
4705 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004706
Gilles Peskineedf9a652018-08-17 18:11:56 +02004707 /* For all currently supported modes, the tag is at the end of the
4708 * ciphertext. */
4709 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4710 {
4711 status = PSA_ERROR_BUFFER_TOO_SMALL;
4712 goto exit;
4713 }
4714 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004715
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004716#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004717 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004718 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004719 status = mbedtls_to_psa_error(
4720 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4721 MBEDTLS_GCM_ENCRYPT,
4722 plaintext_length,
4723 nonce, nonce_length,
4724 additional_data, additional_data_length,
4725 plaintext, ciphertext,
4726 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004727 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004728 else
4729#endif /* MBEDTLS_GCM_C */
4730#if defined(MBEDTLS_CCM_C)
4731 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004732 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004733 status = mbedtls_to_psa_error(
4734 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4735 plaintext_length,
4736 nonce, nonce_length,
4737 additional_data,
4738 additional_data_length,
4739 plaintext, ciphertext,
4740 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004741 }
mohammad16035c8845f2018-05-09 05:40:09 -07004742 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004743#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004744#if defined(MBEDTLS_CHACHAPOLY_C)
4745 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4746 {
4747 if( nonce_length != 12 || operation.tag_length != 16 )
4748 {
4749 status = PSA_ERROR_NOT_SUPPORTED;
4750 goto exit;
4751 }
4752 status = mbedtls_to_psa_error(
4753 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4754 plaintext_length,
4755 nonce,
4756 additional_data,
4757 additional_data_length,
4758 plaintext,
4759 ciphertext,
4760 tag ) );
4761 }
4762 else
4763#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004764 {
mohammad1603554faad2018-06-03 15:07:38 +03004765 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004766 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004767
Gilles Peskineedf9a652018-08-17 18:11:56 +02004768 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4769 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004770
Gilles Peskineedf9a652018-08-17 18:11:56 +02004771exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004772 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004773 if( status == PSA_SUCCESS )
4774 *ciphertext_length = plaintext_length + operation.tag_length;
4775 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004776}
4777
Gilles Peskineee652a32018-06-01 19:23:52 +02004778/* Locate the tag in a ciphertext buffer containing the encrypted data
4779 * followed by the tag. Return the length of the part preceding the tag in
4780 * *plaintext_length. This is the size of the plaintext in modes where
4781 * the encrypted data has the same size as the plaintext, such as
4782 * CCM and GCM. */
4783static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4784 const uint8_t *ciphertext,
4785 size_t ciphertext_length,
4786 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004787 const uint8_t **p_tag )
4788{
4789 size_t payload_length;
4790 if( tag_length > ciphertext_length )
4791 return( PSA_ERROR_INVALID_ARGUMENT );
4792 payload_length = ciphertext_length - tag_length;
4793 if( payload_length > plaintext_size )
4794 return( PSA_ERROR_BUFFER_TOO_SMALL );
4795 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004796 return( PSA_SUCCESS );
4797}
4798
Gilles Peskinec5487a82018-12-03 18:08:14 +01004799psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004800 psa_algorithm_t alg,
4801 const uint8_t *nonce,
4802 size_t nonce_length,
4803 const uint8_t *additional_data,
4804 size_t additional_data_length,
4805 const uint8_t *ciphertext,
4806 size_t ciphertext_length,
4807 uint8_t *plaintext,
4808 size_t plaintext_size,
4809 size_t *plaintext_length )
4810{
mohammad16035955c982018-04-26 00:53:03 +03004811 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004812 aead_operation_t operation;
4813 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004814
Gilles Peskineee652a32018-06-01 19:23:52 +02004815 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004816
Gilles Peskinec5487a82018-12-03 18:08:14 +01004817 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004818 if( status != PSA_SUCCESS )
4819 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004820
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004821 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4822 ciphertext, ciphertext_length,
4823 plaintext_size, &tag );
4824 if( status != PSA_SUCCESS )
4825 goto exit;
4826
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004827#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004828 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004829 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004830 status = mbedtls_to_psa_error(
4831 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4832 ciphertext_length - operation.tag_length,
4833 nonce, nonce_length,
4834 additional_data,
4835 additional_data_length,
4836 tag, operation.tag_length,
4837 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004838 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004839 else
4840#endif /* MBEDTLS_GCM_C */
4841#if defined(MBEDTLS_CCM_C)
4842 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004843 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004844 status = mbedtls_to_psa_error(
4845 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4846 ciphertext_length - operation.tag_length,
4847 nonce, nonce_length,
4848 additional_data,
4849 additional_data_length,
4850 ciphertext, plaintext,
4851 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004852 }
mohammad160339574652018-06-01 04:39:53 -07004853 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004854#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004855#if defined(MBEDTLS_CHACHAPOLY_C)
4856 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4857 {
4858 if( nonce_length != 12 || operation.tag_length != 16 )
4859 {
4860 status = PSA_ERROR_NOT_SUPPORTED;
4861 goto exit;
4862 }
4863 status = mbedtls_to_psa_error(
4864 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4865 ciphertext_length - operation.tag_length,
4866 nonce,
4867 additional_data,
4868 additional_data_length,
4869 tag,
4870 ciphertext,
4871 plaintext ) );
4872 }
4873 else
4874#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004875 {
mohammad1603554faad2018-06-03 15:07:38 +03004876 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004877 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004878
Gilles Peskineedf9a652018-08-17 18:11:56 +02004879 if( status != PSA_SUCCESS && plaintext_size != 0 )
4880 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004881
Gilles Peskineedf9a652018-08-17 18:11:56 +02004882exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004883 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004884 if( status == PSA_SUCCESS )
4885 *plaintext_length = ciphertext_length - operation.tag_length;
4886 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004887}
4888
Gilles Peskinea0655c32018-04-30 17:06:50 +02004889
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004890
Gilles Peskine20035e32018-02-03 22:44:14 +01004891/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004892/* Generators */
4893/****************************************************************/
4894
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004895#define HKDF_STATE_INIT 0 /* no input yet */
4896#define HKDF_STATE_STARTED 1 /* got salt */
4897#define HKDF_STATE_KEYED 2 /* got key */
4898#define HKDF_STATE_OUTPUT 3 /* output started */
4899
Gilles Peskinecbe66502019-05-16 16:59:18 +02004900static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004901 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004902{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004903 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4904 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004905 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004907}
4908
4909
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004911{
4912 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004913 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004914 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004915 {
4916 /* The object has (apparently) been initialized but it is not
4917 * in use. It's ok to call abort on such an object, and there's
4918 * nothing to do. */
4919 }
4920 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004921#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004922 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004923 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004924 mbedtls_free( operation->ctx.hkdf.info );
4925 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004926 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004927 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004928 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004929 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004930 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004931 if( operation->ctx.tls12_prf.seed != NULL )
4932 {
4933 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4934 operation->ctx.tls12_prf.seed_length );
4935 mbedtls_free( operation->ctx.tls12_prf.seed );
4936 }
4937
4938 if( operation->ctx.tls12_prf.label != NULL )
4939 {
4940 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4941 operation->ctx.tls12_prf.label_length );
4942 mbedtls_free( operation->ctx.tls12_prf.label );
4943 }
4944
4945 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4946
4947 /* We leave the fields Ai and output_block to be erased safely by the
4948 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004949 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004950 else
4951#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004952 {
4953 status = PSA_ERROR_BAD_STATE;
4954 }
Janos Follath083036a2019-06-11 10:22:26 +01004955 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004956 return( status );
4957}
4958
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004959psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004960 size_t *capacity)
4961{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004962 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004963 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004964 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004965 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004966 }
4967
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004968 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004969 return( PSA_SUCCESS );
4970}
4971
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004972psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004973 size_t capacity )
4974{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004975 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004976 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004977 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004978 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004979 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004980 return( PSA_SUCCESS );
4981}
4982
Gilles Peskinebef7f142018-07-12 17:22:21 +02004983#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004984/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004985 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004986static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004987 psa_algorithm_t hash_alg,
4988 uint8_t *output,
4989 size_t output_length )
4990{
4991 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4992 psa_status_t status;
4993
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004994 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4995 return( PSA_ERROR_BAD_STATE );
4996 hkdf->state = HKDF_STATE_OUTPUT;
4997
Gilles Peskinebef7f142018-07-12 17:22:21 +02004998 while( output_length != 0 )
4999 {
5000 /* Copy what remains of the current block */
5001 uint8_t n = hash_length - hkdf->offset_in_block;
5002 if( n > output_length )
5003 n = (uint8_t) output_length;
5004 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5005 output += n;
5006 output_length -= n;
5007 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005008 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005009 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005010 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005011 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005012 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005013 * object was corrupted or if this function is called directly
5014 * inside the library. */
5015 if( hkdf->block_number == 0xff )
5016 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005017
5018 /* We need a new block */
5019 ++hkdf->block_number;
5020 hkdf->offset_in_block = 0;
5021 status = psa_hmac_setup_internal( &hkdf->hmac,
5022 hkdf->prk, hash_length,
5023 hash_alg );
5024 if( status != PSA_SUCCESS )
5025 return( status );
5026 if( hkdf->block_number != 1 )
5027 {
5028 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5029 hkdf->output_block,
5030 hash_length );
5031 if( status != PSA_SUCCESS )
5032 return( status );
5033 }
5034 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5035 hkdf->info,
5036 hkdf->info_length );
5037 if( status != PSA_SUCCESS )
5038 return( status );
5039 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5040 &hkdf->block_number, 1 );
5041 if( status != PSA_SUCCESS )
5042 return( status );
5043 status = psa_hmac_finish_internal( &hkdf->hmac,
5044 hkdf->output_block,
5045 sizeof( hkdf->output_block ) );
5046 if( status != PSA_SUCCESS )
5047 return( status );
5048 }
5049
5050 return( PSA_SUCCESS );
5051}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005052
Janos Follath7742fee2019-06-17 12:58:10 +01005053static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5054 psa_tls12_prf_key_derivation_t *tls12_prf,
5055 psa_algorithm_t alg )
5056{
5057 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5058 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005059 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5060 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005061
Janos Follath7742fee2019-06-17 12:58:10 +01005062 /* We can't be wanting more output after block 0xff, otherwise
5063 * the capacity check in psa_key_derivation_output_bytes() would have
5064 * prevented this call. It could happen only if the operation
5065 * object was corrupted or if this function is called directly
5066 * inside the library. */
5067 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005068 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005069
5070 /* We need a new block */
5071 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005072 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005073
5074 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5075 *
5076 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5077 *
5078 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5079 * HMAC_hash(secret, A(2) + seed) +
5080 * HMAC_hash(secret, A(3) + seed) + ...
5081 *
5082 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005083 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005084 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005085 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005086 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005087 * is currently extracted as `output_block` and where i is
5088 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005089 */
5090
Janos Follathea29bfb2019-06-19 12:21:20 +01005091 /* Save the hash context before using it, to preserve the hash state with
5092 * only the inner padding in it. We need this, because inner padding depends
5093 * on the key (secret in the RFC's terminology). */
5094 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5095 if( status != PSA_SUCCESS )
5096 goto cleanup;
5097
5098 /* Calculate A(i) where i = tls12_prf->block_number. */
5099 if( tls12_prf->block_number == 1 )
5100 {
5101 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5102 * the variable seed and in this instance means it in the context of the
5103 * P_hash function, where seed = label + seed.) */
5104 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5105 tls12_prf->label, tls12_prf->label_length );
5106 if( status != PSA_SUCCESS )
5107 goto cleanup;
5108 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5109 tls12_prf->seed, tls12_prf->seed_length );
5110 if( status != PSA_SUCCESS )
5111 goto cleanup;
5112 }
5113 else
5114 {
5115 /* A(i) = HMAC_hash(secret, A(i-1)) */
5116 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5117 tls12_prf->Ai, hash_length );
5118 if( status != PSA_SUCCESS )
5119 goto cleanup;
5120 }
5121
5122 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5123 tls12_prf->Ai, hash_length );
5124 if( status != PSA_SUCCESS )
5125 goto cleanup;
5126 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5127 if( status != PSA_SUCCESS )
5128 goto cleanup;
5129
5130 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5131 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5132 tls12_prf->Ai, hash_length );
5133 if( status != PSA_SUCCESS )
5134 goto cleanup;
5135 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5136 tls12_prf->label, tls12_prf->label_length );
5137 if( status != PSA_SUCCESS )
5138 goto cleanup;
5139 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5140 tls12_prf->seed, tls12_prf->seed_length );
5141 if( status != PSA_SUCCESS )
5142 goto cleanup;
5143 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5144 tls12_prf->output_block, hash_length );
5145 if( status != PSA_SUCCESS )
5146 goto cleanup;
5147 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5148 if( status != PSA_SUCCESS )
5149 goto cleanup;
5150
Janos Follath7742fee2019-06-17 12:58:10 +01005151
5152cleanup:
5153
Janos Follathea29bfb2019-06-19 12:21:20 +01005154 cleanup_status = psa_hash_abort( &backup );
5155 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5156 status = cleanup_status;
5157
5158 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005159}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005160
Janos Follath844eb0e2019-06-19 12:10:49 +01005161static psa_status_t psa_key_derivation_tls12_prf_read(
5162 psa_tls12_prf_key_derivation_t *tls12_prf,
5163 psa_algorithm_t alg,
5164 uint8_t *output,
5165 size_t output_length )
5166{
5167 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5168 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5169 psa_status_t status;
5170 uint8_t offset, length;
5171
5172 while( output_length != 0 )
5173 {
5174 /* Check if we have fully processed the current block. */
5175 if( tls12_prf->left_in_block == 0 )
5176 {
5177 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5178 alg );
5179 if( status != PSA_SUCCESS )
5180 return( status );
5181
5182 continue;
5183 }
5184
5185 if( tls12_prf->left_in_block > output_length )
5186 length = (uint8_t) output_length;
5187 else
5188 length = tls12_prf->left_in_block;
5189
5190 offset = hash_length - tls12_prf->left_in_block;
5191 memcpy( output, tls12_prf->output_block + offset, length );
5192 output += length;
5193 output_length -= length;
5194 tls12_prf->left_in_block -= length;
5195 }
5196
5197 return( PSA_SUCCESS );
5198}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005199#endif /* MBEDTLS_MD_C */
5200
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005201psa_status_t psa_key_derivation_output_bytes(
5202 psa_key_derivation_operation_t *operation,
5203 uint8_t *output,
5204 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005205{
5206 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005207 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005208
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005210 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005211 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005212 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005213 }
5214
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005215 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005216 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005217 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005218 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005219 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005220 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005221 goto exit;
5222 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005223 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005224 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005225 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005226 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005227 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5228 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005229 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005230 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005231 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005232 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005233 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005234
Gilles Peskinebef7f142018-07-12 17:22:21 +02005235#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005236 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005237 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005238 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005239 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005240 output, output_length );
5241 }
Janos Follathadbec812019-06-14 11:05:39 +01005242 else
Janos Follathadbec812019-06-14 11:05:39 +01005243 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005244 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005245 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005246 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005247 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005248 output_length );
5249 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005250 else
5251#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005252 {
5253 return( PSA_ERROR_BAD_STATE );
5254 }
5255
5256exit:
5257 if( status != PSA_SUCCESS )
5258 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005259 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005260 * This allows us to differentiate between exhausted operations and
5261 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5262 * operations. */
5263 psa_algorithm_t alg = operation->alg;
5264 psa_key_derivation_abort( operation );
5265 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005266 memset( output, '!', output_length );
5267 }
5268 return( status );
5269}
5270
Gilles Peskine08542d82018-07-19 17:05:42 +02005271#if defined(MBEDTLS_DES_C)
5272static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5273{
5274 if( data_size >= 8 )
5275 mbedtls_des_key_set_parity( data );
5276 if( data_size >= 16 )
5277 mbedtls_des_key_set_parity( data + 8 );
5278 if( data_size >= 24 )
5279 mbedtls_des_key_set_parity( data + 16 );
5280}
5281#endif /* MBEDTLS_DES_C */
5282
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005283static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005284 psa_key_slot_t *slot,
5285 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005286 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005287{
5288 uint8_t *data = NULL;
5289 size_t bytes = PSA_BITS_TO_BYTES( bits );
5290 psa_status_t status;
5291
Gilles Peskine8e338702019-07-30 20:06:31 +02005292 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005293 return( PSA_ERROR_INVALID_ARGUMENT );
5294 if( bits % 8 != 0 )
5295 return( PSA_ERROR_INVALID_ARGUMENT );
5296 data = mbedtls_calloc( 1, bytes );
5297 if( data == NULL )
5298 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5299
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005300 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005301 if( status != PSA_SUCCESS )
5302 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005303#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005304 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005305 psa_des_set_key_parity( data, bytes );
5306#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005307 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005308
5309exit:
5310 mbedtls_free( data );
5311 return( status );
5312}
5313
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005314psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005315 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005316 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005317{
5318 psa_status_t status;
5319 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005320 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005321
5322 /* Reject any attempt to create a zero-length key so that we don't
5323 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5324 if( psa_get_key_bits( attributes ) == 0 )
5325 return( PSA_ERROR_INVALID_ARGUMENT );
5326
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005327 if( ! operation->can_output_key )
5328 return( PSA_ERROR_NOT_PERMITTED );
5329
Gilles Peskinedf179142019-07-15 22:02:14 +02005330 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5331 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005332#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5333 if( driver != NULL )
5334 {
5335 /* Deriving a key in a secure element is not implemented yet. */
5336 status = PSA_ERROR_NOT_SUPPORTED;
5337 }
5338#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005339 if( status == PSA_SUCCESS )
5340 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005341 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005342 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005343 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005344 }
5345 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005346 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005347 if( status != PSA_SUCCESS )
5348 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005349 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005350 *handle = 0;
5351 }
5352 return( status );
5353}
5354
Gilles Peskineeab56e42018-07-12 17:12:33 +02005355
5356
5357/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005358/* Key derivation */
5359/****************************************************************/
5360
Gilles Peskine969c5d62019-01-16 15:53:06 +01005361static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005362 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005363 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005364{
Janos Follath5fe19732019-06-20 15:09:30 +01005365 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5366 * initialisers for this union leave some bytes unspecified.) */
5367 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5368
Gilles Peskine969c5d62019-01-16 15:53:06 +01005369 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005370#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005371 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5372 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5373 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005374 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005375 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005376 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5377 if( hash_size == 0 )
5378 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005379 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5380 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005381 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005382 {
5383 return( PSA_ERROR_NOT_SUPPORTED );
5384 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005385 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005386 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005387 }
5388#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005389 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005390 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005391}
5392
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005393psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005394 psa_algorithm_t alg )
5395{
5396 psa_status_t status;
5397
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005398 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005399 return( PSA_ERROR_BAD_STATE );
5400
Gilles Peskine6843c292019-01-18 16:44:49 +01005401 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5402 return( PSA_ERROR_INVALID_ARGUMENT );
5403 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005404 {
5405 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005406 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005407 }
5408 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5409 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005410 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005411 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005412 else
5413 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005414
5415 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005416 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005417 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005418}
5419
5420#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005421static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005422 psa_algorithm_t hash_alg,
5423 psa_key_derivation_step_t step,
5424 const uint8_t *data,
5425 size_t data_length )
5426{
5427 psa_status_t status;
5428 switch( step )
5429 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005430 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005431 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005432 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005433 status = psa_hmac_setup_internal( &hkdf->hmac,
5434 data, data_length,
5435 hash_alg );
5436 if( status != PSA_SUCCESS )
5437 return( status );
5438 hkdf->state = HKDF_STATE_STARTED;
5439 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005440 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005441 /* If no salt was provided, use an empty salt. */
5442 if( hkdf->state == HKDF_STATE_INIT )
5443 {
5444 status = psa_hmac_setup_internal( &hkdf->hmac,
5445 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005446 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005447 if( status != PSA_SUCCESS )
5448 return( status );
5449 hkdf->state = HKDF_STATE_STARTED;
5450 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005451 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005452 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005453 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5454 data, data_length );
5455 if( status != PSA_SUCCESS )
5456 return( status );
5457 status = psa_hmac_finish_internal( &hkdf->hmac,
5458 hkdf->prk,
5459 sizeof( hkdf->prk ) );
5460 if( status != PSA_SUCCESS )
5461 return( status );
5462 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5463 hkdf->block_number = 0;
5464 hkdf->state = HKDF_STATE_KEYED;
5465 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005466 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005467 if( hkdf->state == HKDF_STATE_OUTPUT )
5468 return( PSA_ERROR_BAD_STATE );
5469 if( hkdf->info_set )
5470 return( PSA_ERROR_BAD_STATE );
5471 hkdf->info_length = data_length;
5472 if( data_length != 0 )
5473 {
5474 hkdf->info = mbedtls_calloc( 1, data_length );
5475 if( hkdf->info == NULL )
5476 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5477 memcpy( hkdf->info, data, data_length );
5478 }
5479 hkdf->info_set = 1;
5480 return( PSA_SUCCESS );
5481 default:
5482 return( PSA_ERROR_INVALID_ARGUMENT );
5483 }
5484}
Janos Follathb03233e2019-06-11 15:30:30 +01005485
Janos Follathf08e2652019-06-13 09:05:41 +01005486static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5487 const uint8_t *data,
5488 size_t data_length )
5489{
5490 if( prf->state != TLS12_PRF_STATE_INIT )
5491 return( PSA_ERROR_BAD_STATE );
5492
Janos Follathd6dce9f2019-07-04 09:11:38 +01005493 if( data_length != 0 )
5494 {
5495 prf->seed = mbedtls_calloc( 1, data_length );
5496 if( prf->seed == NULL )
5497 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005498
Janos Follathd6dce9f2019-07-04 09:11:38 +01005499 memcpy( prf->seed, data, data_length );
5500 prf->seed_length = data_length;
5501 }
Janos Follathf08e2652019-06-13 09:05:41 +01005502
5503 prf->state = TLS12_PRF_STATE_SEED_SET;
5504
5505 return( PSA_SUCCESS );
5506}
5507
Janos Follath81550542019-06-13 14:26:34 +01005508static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5509 psa_algorithm_t hash_alg,
5510 const uint8_t *data,
5511 size_t data_length )
5512{
5513 psa_status_t status;
5514 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5515 return( PSA_ERROR_BAD_STATE );
5516
5517 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5518 if( status != PSA_SUCCESS )
5519 return( status );
5520
5521 prf->state = TLS12_PRF_STATE_KEY_SET;
5522
5523 return( PSA_SUCCESS );
5524}
5525
Janos Follath6660f0e2019-06-17 08:44:03 +01005526static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5527 psa_tls12_prf_key_derivation_t *prf,
5528 psa_algorithm_t hash_alg,
5529 const uint8_t *data,
5530 size_t data_length )
5531{
5532 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005533 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5534 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005535
5536 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5537 return( PSA_ERROR_INVALID_ARGUMENT );
5538
5539 /* Quoting RFC 4279, Section 2:
5540 *
5541 * The premaster secret is formed as follows: if the PSK is N octets
5542 * long, concatenate a uint16 with the value N, N zero octets, a second
5543 * uint16 with the value N, and the PSK itself.
5544 */
5545
Janos Follath40e13932019-06-26 13:22:29 +01005546 *cur++ = ( data_length >> 8 ) & 0xff;
5547 *cur++ = ( data_length >> 0 ) & 0xff;
5548 memset( cur, 0, data_length );
5549 cur += data_length;
5550 *cur++ = pms[0];
5551 *cur++ = pms[1];
5552 memcpy( cur, data, data_length );
5553 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005554
Janos Follath40e13932019-06-26 13:22:29 +01005555 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005556
5557 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5558 return( status );
5559}
5560
Janos Follath63028dd2019-06-13 09:15:47 +01005561static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5562 const uint8_t *data,
5563 size_t data_length )
5564{
5565 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5566 return( PSA_ERROR_BAD_STATE );
5567
Janos Follathd6dce9f2019-07-04 09:11:38 +01005568 if( data_length != 0 )
5569 {
5570 prf->label = mbedtls_calloc( 1, data_length );
5571 if( prf->label == NULL )
5572 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005573
Janos Follathd6dce9f2019-07-04 09:11:38 +01005574 memcpy( prf->label, data, data_length );
5575 prf->label_length = data_length;
5576 }
Janos Follath63028dd2019-06-13 09:15:47 +01005577
5578 prf->state = TLS12_PRF_STATE_LABEL_SET;
5579
5580 return( PSA_SUCCESS );
5581}
5582
Janos Follathb03233e2019-06-11 15:30:30 +01005583static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5584 psa_algorithm_t hash_alg,
5585 psa_key_derivation_step_t step,
5586 const uint8_t *data,
5587 size_t data_length )
5588{
Janos Follathb03233e2019-06-11 15:30:30 +01005589 switch( step )
5590 {
Janos Follathf08e2652019-06-13 09:05:41 +01005591 case PSA_KEY_DERIVATION_INPUT_SEED:
5592 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005593 case PSA_KEY_DERIVATION_INPUT_SECRET:
5594 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005595 case PSA_KEY_DERIVATION_INPUT_LABEL:
5596 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005597 default:
5598 return( PSA_ERROR_INVALID_ARGUMENT );
5599 }
5600}
Janos Follath6660f0e2019-06-17 08:44:03 +01005601
5602static psa_status_t psa_tls12_prf_psk_to_ms_input(
5603 psa_tls12_prf_key_derivation_t *prf,
5604 psa_algorithm_t hash_alg,
5605 psa_key_derivation_step_t step,
5606 const uint8_t *data,
5607 size_t data_length )
5608{
5609 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005610 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005611 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5612 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005613 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005614
5615 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5616}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005617#endif /* MBEDTLS_MD_C */
5618
Gilles Peskineb8965192019-09-24 16:21:10 +02005619/** Check whether the given key type is acceptable for the given
5620 * input step of a key derivation.
5621 *
5622 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5623 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5624 * Both secret and non-secret inputs can alternatively have the type
5625 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5626 * that the input was passed as a buffer rather than via a key object.
5627 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005628static int psa_key_derivation_check_input_type(
5629 psa_key_derivation_step_t step,
5630 psa_key_type_t key_type )
5631{
5632 switch( step )
5633 {
5634 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005635 if( key_type == PSA_KEY_TYPE_DERIVE )
5636 return( PSA_SUCCESS );
5637 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005638 return( PSA_SUCCESS );
5639 break;
5640 case PSA_KEY_DERIVATION_INPUT_LABEL:
5641 case PSA_KEY_DERIVATION_INPUT_SALT:
5642 case PSA_KEY_DERIVATION_INPUT_INFO:
5643 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005644 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5645 return( PSA_SUCCESS );
5646 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005647 return( PSA_SUCCESS );
5648 break;
5649 }
5650 return( PSA_ERROR_INVALID_ARGUMENT );
5651}
5652
Janos Follathb80a94e2019-06-12 15:54:46 +01005653static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005654 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005655 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005656 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005657 const uint8_t *data,
5658 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005659{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005660 psa_status_t status;
5661 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5662
5663 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005664 if( status != PSA_SUCCESS )
5665 goto exit;
5666
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005667#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005668 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005669 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005670 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005671 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005672 step, data, data_length );
5673 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005674 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005675 {
Janos Follathb03233e2019-06-11 15:30:30 +01005676 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5677 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5678 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005679 }
5680 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5681 {
5682 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5683 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5684 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005685 }
5686 else
5687#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005688 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005689 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005690 return( PSA_ERROR_BAD_STATE );
5691 }
5692
Gilles Peskine224b0d62019-09-23 18:13:17 +02005693exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005694 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005695 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005696 return( status );
5697}
5698
Janos Follath51f4a0f2019-06-14 11:35:55 +01005699psa_status_t psa_key_derivation_input_bytes(
5700 psa_key_derivation_operation_t *operation,
5701 psa_key_derivation_step_t step,
5702 const uint8_t *data,
5703 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005704{
Gilles Peskineb8965192019-09-24 16:21:10 +02005705 return( psa_key_derivation_input_internal( operation, step,
5706 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005707 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005708}
5709
Janos Follath51f4a0f2019-06-14 11:35:55 +01005710psa_status_t psa_key_derivation_input_key(
5711 psa_key_derivation_operation_t *operation,
5712 psa_key_derivation_step_t step,
5713 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005714{
5715 psa_key_slot_t *slot;
5716 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005717
Gilles Peskine28f8f302019-07-24 13:30:31 +02005718 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005719 PSA_KEY_USAGE_DERIVE,
5720 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005721 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005722 {
5723 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005724 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005725 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005726
5727 /* Passing a key object as a SECRET input unlocks the permission
5728 * to output to a key object. */
5729 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5730 operation->can_output_key = 1;
5731
Janos Follathb80a94e2019-06-12 15:54:46 +01005732 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005733 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005734 slot->data.key.data,
5735 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005736}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005737
5738
5739
5740/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005741/* Key agreement */
5742/****************************************************************/
5743
Gilles Peskinea05219c2018-11-16 16:02:56 +01005744#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005745static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5746 size_t peer_key_length,
5747 const mbedtls_ecp_keypair *our_key,
5748 uint8_t *shared_secret,
5749 size_t shared_secret_size,
5750 size_t *shared_secret_length )
5751{
Steven Cooremand4867872020-08-05 16:31:39 +02005752 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005753 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005754 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005755 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005756 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005757 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005758
Steven Cooreman4fed4552020-08-03 14:46:03 +02005759 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5760 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005761 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005762 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005763 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005764 goto exit;
5765
Jaeden Amero97271b32019-01-10 19:38:51 +00005766 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005767 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005768 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005769 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005770 status = mbedtls_to_psa_error(
5771 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5772 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005773 goto exit;
5774
Jaeden Amero97271b32019-01-10 19:38:51 +00005775 status = mbedtls_to_psa_error(
5776 mbedtls_ecdh_calc_secret( &ecdh,
5777 shared_secret_length,
5778 shared_secret, shared_secret_size,
5779 mbedtls_ctr_drbg_random,
5780 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005781 if( status != PSA_SUCCESS )
5782 goto exit;
5783 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5784 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005785
5786exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005787 if( status != PSA_SUCCESS )
5788 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005789 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005790 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005791 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005792
Jaeden Amero97271b32019-01-10 19:38:51 +00005793 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005794}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005795#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005796
Gilles Peskine01d718c2018-09-18 12:01:02 +02005797#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5798
Gilles Peskine0216fe12019-04-11 21:23:21 +02005799static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5800 psa_key_slot_t *private_key,
5801 const uint8_t *peer_key,
5802 size_t peer_key_length,
5803 uint8_t *shared_secret,
5804 size_t shared_secret_size,
5805 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005806{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005807 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005808 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005809#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005810 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005811 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005812 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005813 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005814 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005815 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005816 private_key->data.key.data,
5817 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005818 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005819 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005820 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005821 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005822 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005823 shared_secret, shared_secret_size,
5824 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005825 mbedtls_ecp_keypair_free( ecp );
5826 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005827 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005828#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005829 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005830 (void) private_key;
5831 (void) peer_key;
5832 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005833 (void) shared_secret;
5834 (void) shared_secret_size;
5835 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005836 return( PSA_ERROR_NOT_SUPPORTED );
5837 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005838}
5839
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005840/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005841 * to potentially free embedded data structures and wipe confidential data.
5842 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005843static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005844 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005845 psa_key_slot_t *private_key,
5846 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005847 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005848{
5849 psa_status_t status;
5850 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5851 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005852 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005853
5854 /* Step 1: run the secret agreement algorithm to generate the shared
5855 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005856 status = psa_key_agreement_raw_internal( ka_alg,
5857 private_key,
5858 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005859 shared_secret,
5860 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005861 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005862 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005863 goto exit;
5864
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005865 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005866 * the shared secret. A shared secret is permitted wherever a key
5867 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005868 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005869 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005870 shared_secret,
5871 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005872
Gilles Peskine12313cd2018-06-20 00:20:32 +02005873exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005874 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005875 return( status );
5876}
5877
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005878psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005879 psa_key_derivation_step_t step,
5880 psa_key_handle_t private_key,
5881 const uint8_t *peer_key,
5882 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005883{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005884 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005885 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005886 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005887 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005888 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005889 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005890 if( status != PSA_SUCCESS )
5891 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005892 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005893 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005894 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005895 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005896 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005897 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005898}
5899
Gilles Peskinebe697d82019-05-16 18:00:41 +02005900psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5901 psa_key_handle_t private_key,
5902 const uint8_t *peer_key,
5903 size_t peer_key_length,
5904 uint8_t *output,
5905 size_t output_size,
5906 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005907{
5908 psa_key_slot_t *slot;
5909 psa_status_t status;
5910
5911 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5912 {
5913 status = PSA_ERROR_INVALID_ARGUMENT;
5914 goto exit;
5915 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005916 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005917 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005918 if( status != PSA_SUCCESS )
5919 goto exit;
5920
5921 status = psa_key_agreement_raw_internal( alg, slot,
5922 peer_key, peer_key_length,
5923 output, output_size,
5924 output_length );
5925
5926exit:
5927 if( status != PSA_SUCCESS )
5928 {
5929 /* If an error happens and is not handled properly, the output
5930 * may be used as a key to protect sensitive data. Arrange for such
5931 * a key to be random, which is likely to result in decryption or
5932 * verification errors. This is better than filling the buffer with
5933 * some constant data such as zeros, which would result in the data
5934 * being protected with a reproducible, easily knowable key.
5935 */
5936 psa_generate_random( output, output_size );
5937 *output_length = output_size;
5938 }
5939 return( status );
5940}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005941
5942
5943/****************************************************************/
5944/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005945/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005946
Gilles Peskine4c317f42018-07-12 01:24:09 +02005947psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005948 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005949{
Janos Follath24eed8d2019-11-22 13:21:35 +00005950 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005951 GUARD_MODULE_INITIALIZED;
5952
Gilles Peskinef181eca2019-08-07 13:49:00 +02005953 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5954 {
5955 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5956 output,
5957 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5958 if( ret != 0 )
5959 return( mbedtls_to_psa_error( ret ) );
5960 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5961 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5962 }
5963
Gilles Peskinee59236f2018-01-27 23:32:46 +01005964 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5965 return( mbedtls_to_psa_error( ret ) );
5966}
5967
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005968#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5969#include "mbedtls/entropy_poll.h"
5970
Gilles Peskine7228da22019-07-15 11:06:15 +02005971psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005972 size_t seed_size )
5973{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005974 if( global_data.initialized )
5975 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005976
5977 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5978 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5979 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5980 return( PSA_ERROR_INVALID_ARGUMENT );
5981
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005982 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005983}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005984#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005985
Gilles Peskinee56e8782019-04-26 17:34:02 +02005986#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5987static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5988 size_t domain_parameters_size,
5989 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005990{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005991 size_t i;
5992 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005993
Gilles Peskinee56e8782019-04-26 17:34:02 +02005994 if( domain_parameters_size == 0 )
5995 {
5996 *exponent = 65537;
5997 return( PSA_SUCCESS );
5998 }
5999
6000 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6001 * support values that fit in a 32-bit integer, which is larger than
6002 * int on just about every platform anyway. */
6003 if( domain_parameters_size > sizeof( acc ) )
6004 return( PSA_ERROR_NOT_SUPPORTED );
6005 for( i = 0; i < domain_parameters_size; i++ )
6006 acc = ( acc << 8 ) | domain_parameters[i];
6007 if( acc > INT_MAX )
6008 return( PSA_ERROR_NOT_SUPPORTED );
6009 *exponent = acc;
6010 return( PSA_SUCCESS );
6011}
6012#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6013
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006014static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006015 psa_key_slot_t *slot, size_t bits,
6016 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006017{
Gilles Peskine8e338702019-07-30 20:06:31 +02006018 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006019
Gilles Peskinee56e8782019-04-26 17:34:02 +02006020 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006021 return( PSA_ERROR_INVALID_ARGUMENT );
6022
Gilles Peskinee59236f2018-01-27 23:32:46 +01006023 if( key_type_is_raw_bytes( type ) )
6024 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006025 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006026
6027 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006028 if( status != PSA_SUCCESS )
6029 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006030
6031 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006032 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6033 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006034 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006035
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006036 status = psa_generate_random( slot->data.key.data,
6037 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006038 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006039 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006040
6041 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006042#if defined(MBEDTLS_DES_C)
6043 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006044 psa_des_set_key_parity( slot->data.key.data,
6045 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006046#endif /* MBEDTLS_DES_C */
6047 }
6048 else
6049
6050#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006051 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006052 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006053 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006055 int exponent;
6056 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006057 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6058 return( PSA_ERROR_NOT_SUPPORTED );
6059 /* Accept only byte-aligned keys, for the same reasons as
6060 * in psa_import_rsa_key(). */
6061 if( bits % 8 != 0 )
6062 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006063 status = psa_read_rsa_exponent( domain_parameters,
6064 domain_parameters_size,
6065 &exponent );
6066 if( status != PSA_SUCCESS )
6067 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006068 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6069 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006070 mbedtls_ctr_drbg_random,
6071 &global_data.ctr_drbg,
6072 (unsigned int) bits,
6073 exponent );
6074 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006075 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006076
6077 /* Make sure to always have an export representation available */
6078 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6079
Steven Cooreman75b74362020-07-28 14:30:13 +02006080 status = psa_allocate_buffer_to_slot( slot, bytes );
6081 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006082 {
6083 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006084 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006085 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006086
6087 status = psa_export_rsa_key( type,
6088 &rsa,
6089 slot->data.key.data,
6090 bytes,
6091 &slot->data.key.bytes );
6092 mbedtls_rsa_free( &rsa );
6093 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006094 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006095 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006096 }
6097 else
6098#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
6099
6100#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006101 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006102 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006103 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006104 mbedtls_ecp_group_id grp_id =
6105 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006106 const mbedtls_ecp_curve_info *curve_info =
6107 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006108 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006110 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006111 return( PSA_ERROR_NOT_SUPPORTED );
6112 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6113 return( PSA_ERROR_NOT_SUPPORTED );
6114 if( curve_info->bit_size != bits )
6115 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02006116 mbedtls_ecp_keypair_init( &ecp );
6117 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006118 mbedtls_ctr_drbg_random,
6119 &global_data.ctr_drbg );
6120 if( ret != 0 )
6121 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006122 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006123 return( mbedtls_to_psa_error( ret ) );
6124 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006125
6126
6127 /* Make sure to always have an export representation available */
6128 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006129 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6130 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006131 {
6132 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006133 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006134 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006135
6136 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006137 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6138
6139 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006140 if( status != PSA_SUCCESS ) {
6141 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006142 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006143 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006144 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006145 }
6146 else
6147#endif /* MBEDTLS_ECP_C */
Steven Cooreman29149862020-08-05 15:43:42 +02006148 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006149 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006150 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006151
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006152 return( PSA_SUCCESS );
6153}
Darryl Green0c6575a2018-11-07 16:05:30 +00006154
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006155psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006156 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006157{
6158 psa_status_t status;
6159 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006160 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006161
Gilles Peskine0f84d622019-09-12 19:03:13 +02006162 /* Reject any attempt to create a zero-length key so that we don't
6163 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6164 if( psa_get_key_bits( attributes ) == 0 )
6165 return( PSA_ERROR_INVALID_ARGUMENT );
6166
Gilles Peskinedf179142019-07-15 22:02:14 +02006167 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6168 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006169 if( status != PSA_SUCCESS )
6170 goto exit;
6171
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006172 status = psa_driver_wrapper_generate_key( attributes,
6173 slot );
6174 if( status != PSA_ERROR_NOT_SUPPORTED ||
6175 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6176 goto exit;
6177
6178 status = psa_generate_key_internal(
6179 slot, attributes->core.bits,
6180 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006181
6182exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006183 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006184 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006185 if( status != PSA_SUCCESS )
6186 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006187 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006188 *handle = 0;
6189 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006190 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006191}
6192
6193
Gilles Peskinee59236f2018-01-27 23:32:46 +01006194
6195/****************************************************************/
6196/* Module setup */
6197/****************************************************************/
6198
Gilles Peskine5e769522018-11-20 21:59:56 +01006199psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6200 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6201 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6202{
6203 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6204 return( PSA_ERROR_BAD_STATE );
6205 global_data.entropy_init = entropy_init;
6206 global_data.entropy_free = entropy_free;
6207 return( PSA_SUCCESS );
6208}
6209
Gilles Peskinee59236f2018-01-27 23:32:46 +01006210void mbedtls_psa_crypto_free( void )
6211{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006212 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006213 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6214 {
6215 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006216 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006217 }
6218 /* Wipe all remaining data, including configuration.
6219 * In particular, this sets all state indicator to the value
6220 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006221 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006222#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006223 /* Unregister all secure element drivers, so that we restart from
6224 * a pristine state. */
6225 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006226#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006227}
6228
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006229#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6230/** Recover a transaction that was interrupted by a power failure.
6231 *
6232 * This function is called during initialization, before psa_crypto_init()
6233 * returns. If this function returns a failure status, the initialization
6234 * fails.
6235 */
6236static psa_status_t psa_crypto_recover_transaction(
6237 const psa_crypto_transaction_t *transaction )
6238{
6239 switch( transaction->unknown.type )
6240 {
6241 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6242 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006243 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006244 * is implemented.
6245 * https://github.com/ARMmbed/mbed-crypto/issues/218
6246 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006247 default:
6248 /* We found an unsupported transaction in the storage.
6249 * We don't know what state the storage is in. Give up. */
6250 return( PSA_ERROR_STORAGE_FAILURE );
6251 }
6252}
6253#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6254
Gilles Peskinee59236f2018-01-27 23:32:46 +01006255psa_status_t psa_crypto_init( void )
6256{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006257 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006258 const unsigned char drbg_seed[] = "PSA";
6259
Gilles Peskinec6b69072018-11-20 21:42:52 +01006260 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006261 if( global_data.initialized != 0 )
6262 return( PSA_SUCCESS );
6263
Gilles Peskine5e769522018-11-20 21:59:56 +01006264 /* Set default configuration if
6265 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6266 if( global_data.entropy_init == NULL )
6267 global_data.entropy_init = mbedtls_entropy_init;
6268 if( global_data.entropy_free == NULL )
6269 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006270
Gilles Peskinec6b69072018-11-20 21:42:52 +01006271 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006272 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006273#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6274 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6275 /* The PSA entropy injection feature depends on using NV seed as an entropy
6276 * source. Add NV seed as an entropy source for PSA entropy injection. */
6277 mbedtls_entropy_add_source( &global_data.entropy,
6278 mbedtls_nv_seed_poll, NULL,
6279 MBEDTLS_ENTROPY_BLOCK_SIZE,
6280 MBEDTLS_ENTROPY_SOURCE_STRONG );
6281#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006282 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006283 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006284 status = mbedtls_to_psa_error(
6285 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6286 mbedtls_entropy_func,
6287 &global_data.entropy,
6288 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6289 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006290 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006291 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006292
Gilles Peskine66fb1262018-12-10 16:29:04 +01006293 status = psa_initialize_key_slots( );
6294 if( status != PSA_SUCCESS )
6295 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006296
Gilles Peskined9348f22019-10-01 15:22:29 +02006297#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6298 status = psa_init_all_se_drivers( );
6299 if( status != PSA_SUCCESS )
6300 goto exit;
6301#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6302
Gilles Peskine4b734222019-07-24 15:56:31 +02006303#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006304 status = psa_crypto_load_transaction( );
6305 if( status == PSA_SUCCESS )
6306 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006307 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6308 if( status != PSA_SUCCESS )
6309 goto exit;
6310 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006311 }
6312 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6313 {
6314 /* There's no transaction to complete. It's all good. */
6315 status = PSA_SUCCESS;
6316 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006317#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006318
Gilles Peskinec6b69072018-11-20 21:42:52 +01006319 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006320 global_data.initialized = 1;
6321
6322exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006323 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006324 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006325 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006326}
6327
6328#endif /* MBEDTLS_PSA_CRYPTO_C */