blob: 78d0b9a3f1775443581492aea738ffec445d7ed5 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
itayzafrir7723ab12019-02-14 10:28:02 +020025#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010026#include "psa/crypto.h"
27
Gilles Peskine039b90c2018-12-07 18:24:41 +010028#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010029#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020030#include "psa_crypto_driver_wrappers.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020031#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020032#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020033#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010034#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010035/* Include internal declarations that are useful for implementing persistently
36 * stored keys. */
37#include "psa_crypto_storage.h"
38
Gilles Peskineb46bef22019-07-30 21:32:04 +020039#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include <stdlib.h>
41#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020043#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#define mbedtls_calloc calloc
45#define mbedtls_free free
46#endif
47
Gilles Peskinea5905292018-02-07 20:59:33 +010048#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020049#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000050#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020051#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/blowfish.h"
53#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020054#include "mbedtls/chacha20.h"
55#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/cipher.h"
57#include "mbedtls/ccm.h"
58#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010059#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020061#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010062#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/error.h"
65#include "mbedtls/gcm.h"
66#include "mbedtls/md2.h"
67#include "mbedtls/md4.h"
68#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010069#include "mbedtls/md.h"
70#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010071#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010073#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000074#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/sha1.h"
78#include "mbedtls/sha256.h"
79#include "mbedtls/sha512.h"
80#include "mbedtls/xtea.h"
81
Gilles Peskine996deb12018-08-01 15:45:45 +020082#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
83
Gilles Peskine9ef733f2018-02-07 21:05:37 +010084/* constant-time buffer comparison */
85static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
86{
87 size_t i;
88 unsigned char diff = 0;
89
90 for( i = 0; i < n; i++ )
91 diff |= a[i] ^ b[i];
92
93 return( diff );
94}
95
96
97
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010098/****************************************************************/
99/* Global data, support functions and library management */
100/****************************************************************/
101
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102static int key_type_is_raw_bytes( psa_key_type_t type )
103{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200104 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200105}
106
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100107/* Values for psa_global_data_t::rng_state */
108#define RNG_NOT_INITIALIZED 0
109#define RNG_INITIALIZED 1
110#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100111
Gilles Peskine2d277862018-06-18 15:41:12 +0200112typedef struct
113{
Gilles Peskine5e769522018-11-20 21:59:56 +0100114 void (* entropy_init )( mbedtls_entropy_context *ctx );
115 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100116 mbedtls_entropy_context entropy;
117 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100118 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100119 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120} psa_global_data_t;
121
122static psa_global_data_t global_data;
123
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124#define GUARD_MODULE_INITIALIZED \
125 if( global_data.initialized == 0 ) \
126 return( PSA_ERROR_BAD_STATE );
127
Steven Cooreman01164162020-07-20 15:31:37 +0200128psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100129{
Gilles Peskinea5905292018-02-07 20:59:33 +0100130 /* If there's both a high-level code and low-level code, dispatch on
131 * the high-level code. */
132 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133 {
134 case 0:
135 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100136
137 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
138 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
139 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
140 return( PSA_ERROR_NOT_SUPPORTED );
141 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
142 return( PSA_ERROR_HARDWARE_FAILURE );
143
144 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
Gilles Peskine9a944802018-06-21 09:35:35 +0200147 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
148 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
149 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
150 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
151 case MBEDTLS_ERR_ASN1_INVALID_DATA:
152 return( PSA_ERROR_INVALID_ARGUMENT );
153 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
154 return( PSA_ERROR_INSUFFICIENT_MEMORY );
155 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
156 return( PSA_ERROR_BUFFER_TOO_SMALL );
157
Jaeden Amero93e21112019-02-20 13:57:28 +0000158#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000159 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
161 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
162#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
164 return( PSA_ERROR_NOT_SUPPORTED );
165 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
166 return( PSA_ERROR_HARDWARE_FAILURE );
167
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000169 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
171 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
174 return( PSA_ERROR_NOT_SUPPORTED );
175 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
176 return( PSA_ERROR_HARDWARE_FAILURE );
177
178 case MBEDTLS_ERR_CCM_BAD_INPUT:
179 return( PSA_ERROR_INVALID_ARGUMENT );
180 case MBEDTLS_ERR_CCM_AUTH_FAILED:
181 return( PSA_ERROR_INVALID_SIGNATURE );
182 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
183 return( PSA_ERROR_HARDWARE_FAILURE );
184
Gilles Peskine26869f22019-05-06 15:25:00 +0200185 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
186 return( PSA_ERROR_INVALID_ARGUMENT );
187
188 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
189 return( PSA_ERROR_BAD_STATE );
190 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
191 return( PSA_ERROR_INVALID_SIGNATURE );
192
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
194 return( PSA_ERROR_NOT_SUPPORTED );
195 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
198 return( PSA_ERROR_INSUFFICIENT_MEMORY );
199 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
200 return( PSA_ERROR_INVALID_PADDING );
201 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200202 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
205 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200206 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
208 return( PSA_ERROR_HARDWARE_FAILURE );
209
210 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
214 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
215 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
216 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
217 return( PSA_ERROR_NOT_SUPPORTED );
218 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
220
221 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
Gilles Peskinee59236f2018-01-27 23:32:46 +0100226 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
227 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
228 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
229 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100230
231 case MBEDTLS_ERR_GCM_AUTH_FAILED:
232 return( PSA_ERROR_INVALID_SIGNATURE );
233 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200234 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
243 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
244 return( PSA_ERROR_NOT_SUPPORTED );
245 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
246 return( PSA_ERROR_INVALID_ARGUMENT );
247 case MBEDTLS_ERR_MD_ALLOC_FAILED:
248 return( PSA_ERROR_INSUFFICIENT_MEMORY );
249 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
250 return( PSA_ERROR_STORAGE_FAILURE );
251 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
255 return( PSA_ERROR_STORAGE_FAILURE );
256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
261 return( PSA_ERROR_BUFFER_TOO_SMALL );
262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
269 return( PSA_ERROR_INSUFFICIENT_MEMORY );
270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
282 return( PSA_ERROR_NOT_SUPPORTED );
283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
285 return( PSA_ERROR_NOT_PERMITTED );
286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
291 return( PSA_ERROR_NOT_SUPPORTED );
292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
293 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
300 return( PSA_ERROR_NOT_SUPPORTED );
301
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
303 return( PSA_ERROR_HARDWARE_FAILURE );
304
305 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_RSA_INVALID_PADDING:
308 return( PSA_ERROR_INVALID_PADDING );
309 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
312 return( PSA_ERROR_INVALID_ARGUMENT );
313 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
314 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200315 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100316 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
317 return( PSA_ERROR_INVALID_SIGNATURE );
318 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
319 return( PSA_ERROR_BUFFER_TOO_SMALL );
320 case MBEDTLS_ERR_RSA_RNG_FAILED:
321 return( PSA_ERROR_INSUFFICIENT_MEMORY );
322 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
323 return( PSA_ERROR_NOT_SUPPORTED );
324 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
327 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
328 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
329 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
330 return( PSA_ERROR_HARDWARE_FAILURE );
331
332 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
333 return( PSA_ERROR_INVALID_ARGUMENT );
334 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
336
itayzafrir5c753392018-05-08 11:18:38 +0300337 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300338 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300339 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
341 return( PSA_ERROR_BUFFER_TOO_SMALL );
342 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
343 return( PSA_ERROR_NOT_SUPPORTED );
344 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
345 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
346 return( PSA_ERROR_INVALID_SIGNATURE );
347 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
348 return( PSA_ERROR_INSUFFICIENT_MEMORY );
349 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
350 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000351 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
352 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
Gilles Peskine8e338702019-07-30 20:06:31 +0200369 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
John Durkop9814fa22020-11-04 12:28:15 -0800373#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800374 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
375 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
376 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100377mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100378 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200379{
380 switch( curve )
381 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100382 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100383 switch( byte_length )
384 {
385 case PSA_BITS_TO_BYTES( 192 ):
386 return( MBEDTLS_ECP_DP_SECP192R1 );
387 case PSA_BITS_TO_BYTES( 224 ):
388 return( MBEDTLS_ECP_DP_SECP224R1 );
389 case PSA_BITS_TO_BYTES( 256 ):
390 return( MBEDTLS_ECP_DP_SECP256R1 );
391 case PSA_BITS_TO_BYTES( 384 ):
392 return( MBEDTLS_ECP_DP_SECP384R1 );
393 case PSA_BITS_TO_BYTES( 521 ):
394 return( MBEDTLS_ECP_DP_SECP521R1 );
395 default:
396 return( MBEDTLS_ECP_DP_NONE );
397 }
398 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100399
Paul Elliott8ff510a2020-06-02 17:19:28 +0100400 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100401 switch( byte_length )
402 {
403 case PSA_BITS_TO_BYTES( 256 ):
404 return( MBEDTLS_ECP_DP_BP256R1 );
405 case PSA_BITS_TO_BYTES( 384 ):
406 return( MBEDTLS_ECP_DP_BP384R1 );
407 case PSA_BITS_TO_BYTES( 512 ):
408 return( MBEDTLS_ECP_DP_BP512R1 );
409 default:
410 return( MBEDTLS_ECP_DP_NONE );
411 }
412 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100413
Paul Elliott8ff510a2020-06-02 17:19:28 +0100414 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100415 switch( byte_length )
416 {
417 case PSA_BITS_TO_BYTES( 255 ):
418 return( MBEDTLS_ECP_DP_CURVE25519 );
419 case PSA_BITS_TO_BYTES( 448 ):
420 return( MBEDTLS_ECP_DP_CURVE448 );
421 default:
422 return( MBEDTLS_ECP_DP_NONE );
423 }
424 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100425
Paul Elliott8ff510a2020-06-02 17:19:28 +0100426 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100427 switch( byte_length )
428 {
429 case PSA_BITS_TO_BYTES( 192 ):
430 return( MBEDTLS_ECP_DP_SECP192K1 );
431 case PSA_BITS_TO_BYTES( 224 ):
432 return( MBEDTLS_ECP_DP_SECP224K1 );
433 case PSA_BITS_TO_BYTES( 256 ):
434 return( MBEDTLS_ECP_DP_SECP256K1 );
435 default:
436 return( MBEDTLS_ECP_DP_NONE );
437 }
438 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100439
Gilles Peskine12313cd2018-06-20 00:20:32 +0200440 default:
441 return( MBEDTLS_ECP_DP_NONE );
442 }
443}
John Durkop9814fa22020-11-04 12:28:15 -0800444#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800445 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
446 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
447 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200448
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200449static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
450 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200451{
452 /* Check that the bit size is acceptable for the key type */
453 switch( type )
454 {
455 case PSA_KEY_TYPE_RAW_DATA:
456#if defined(MBEDTLS_MD_C)
457 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200458#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200459 case PSA_KEY_TYPE_DERIVE:
460 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200461#if defined(MBEDTLS_AES_C)
462 case PSA_KEY_TYPE_AES:
463 if( bits != 128 && bits != 192 && bits != 256 )
464 return( PSA_ERROR_INVALID_ARGUMENT );
465 break;
466#endif
467#if defined(MBEDTLS_CAMELLIA_C)
468 case PSA_KEY_TYPE_CAMELLIA:
469 if( bits != 128 && bits != 192 && bits != 256 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
471 break;
472#endif
473#if defined(MBEDTLS_DES_C)
474 case PSA_KEY_TYPE_DES:
475 if( bits != 64 && bits != 128 && bits != 192 )
476 return( PSA_ERROR_INVALID_ARGUMENT );
477 break;
478#endif
479#if defined(MBEDTLS_ARC4_C)
480 case PSA_KEY_TYPE_ARC4:
481 if( bits < 8 || bits > 2048 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200485#if defined(MBEDTLS_CHACHA20_C)
486 case PSA_KEY_TYPE_CHACHA20:
487 if( bits != 256 )
488 return( PSA_ERROR_INVALID_ARGUMENT );
489 break;
490#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491 default:
492 return( PSA_ERROR_NOT_SUPPORTED );
493 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200494 if( bits % 8 != 0 )
495 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497 return( PSA_SUCCESS );
498}
499
John Durkop0e005192020-10-31 22:06:54 -0700500#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
501 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
502 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
John Durkop9814fa22020-11-04 12:28:15 -0800503 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
504 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
505 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana01795d2020-07-24 22:48:15 +0200506
507#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100508/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
509 * that are not a multiple of 8) well. For example, there is only
510 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
511 * way to return the exact bit size of a key.
512 * To keep things simple, reject non-byte-aligned key sizes. */
513static psa_status_t psa_check_rsa_key_byte_aligned(
514 const mbedtls_rsa_context *rsa )
515{
516 mbedtls_mpi n;
517 psa_status_t status;
518 mbedtls_mpi_init( &n );
519 status = mbedtls_to_psa_error(
520 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
521 if( status == PSA_SUCCESS )
522 {
523 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
524 status = PSA_ERROR_NOT_SUPPORTED;
525 }
526 mbedtls_mpi_free( &n );
527 return( status );
528}
Steven Cooremana01795d2020-07-24 22:48:15 +0200529#endif /* MBEDTLS_PK_PARSE_C */
Gilles Peskine86a440b2018-11-12 18:39:40 +0100530
Steven Cooreman7f391872020-07-30 14:57:44 +0200531/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200532 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200533 * \param[in] type The type of key contained in \p data.
534 * \param[in] data The buffer from which to load the representation.
535 * \param[in] data_length The size in bytes of \p data.
536 * \param[out] p_rsa Returns a pointer to an RSA context on success.
537 * The caller is responsible for freeing both the
538 * contents of the context and the context itself
539 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200540 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200541static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
542 const uint8_t *data,
543 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200544 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545{
Steven Cooremana01795d2020-07-24 22:48:15 +0200546#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000547 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200548 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000549 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200550 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000551
552 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200553 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000554 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200555 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200556 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000557 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200558 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559 if( status != PSA_SUCCESS )
560 goto exit;
561
562 /* We have something that the pkparse module recognizes. If it is a
563 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200564 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200565 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000566 status = PSA_ERROR_INVALID_ARGUMENT;
567 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200568 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000569
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000570 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
571 * supports non-byte-aligned key sizes, but not well. For example,
572 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200573 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000574 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
575 {
576 status = PSA_ERROR_NOT_SUPPORTED;
577 goto exit;
578 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200579 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200580 if( status != PSA_SUCCESS )
581 goto exit;
582
Steven Cooremana2371e52020-07-28 14:30:39 +0200583 /* Copy out the pointer to the RSA context, and reset the PK context
584 * such that pk_free doesn't free the RSA context we just grabbed. */
585 *p_rsa = mbedtls_pk_rsa( ctx );
586 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000587
588exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200589 mbedtls_pk_free( &ctx );
590 return( status );
591#else
Steven Cooreman4fed4552020-08-03 14:46:03 +0200592 (void) data;
593 (void) data_length;
Steven Cooreman7f391872020-07-30 14:57:44 +0200594 (void) type;
Steven Cooremana01795d2020-07-24 22:48:15 +0200595 (void) rsa;
596 return( PSA_ERROR_NOT_SUPPORTED );
597#endif /* MBEDTLS_PK_PARSE_C */
598}
599
John Durkop6ba40d12020-11-10 08:50:04 -0800600#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
601 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
602 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
603 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
604 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
605 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
606
607#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
608 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
609
Steven Cooremana01795d2020-07-24 22:48:15 +0200610/** Export an RSA key to export representation
611 *
612 * \param[in] type The type of key (public/private) to export
613 * \param[in] rsa The internal RSA representation from which to export
614 * \param[out] data The buffer to export to
615 * \param[in] data_size The length of the buffer to export to
616 * \param[out] data_length The amount of bytes written to \p data
617 */
618static psa_status_t psa_export_rsa_key( psa_key_type_t type,
619 mbedtls_rsa_context *rsa,
620 uint8_t *data,
621 size_t data_size,
622 size_t *data_length )
623{
624#if defined(MBEDTLS_PK_WRITE_C)
625 int ret;
626 mbedtls_pk_context pk;
627 uint8_t *pos = data + data_size;
628
629 mbedtls_pk_init( &pk );
630 pk.pk_info = &mbedtls_rsa_info;
631 pk.pk_ctx = rsa;
632
633 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200634 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
635 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200636 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
637 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
638 else
639 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
640
641 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200642 {
643 /* Clean up in case pk_write failed halfway through. */
644 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200645 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200646 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200647
648 /* The mbedtls_pk_xxx functions write to the end of the buffer.
649 * Move the data to the beginning and erase remaining data
650 * at the original location. */
651 if( 2 * (size_t) ret <= data_size )
652 {
653 memcpy( data, data + data_size - ret, ret );
654 memset( data + data_size - ret, 0, ret );
655 }
656 else if( (size_t) ret < data_size )
657 {
658 memmove( data, data + data_size - ret, ret );
659 memset( data + ret, 0, data_size - ret );
660 }
661
662 *data_length = ret;
663 return( PSA_SUCCESS );
664#else
665 (void) type;
666 (void) rsa;
667 (void) data;
668 (void) data_size;
669 (void) data_length;
670 return( PSA_ERROR_NOT_SUPPORTED );
671#endif /* MBEDTLS_PK_WRITE_C */
672}
673
674/** Import an RSA key from import representation to a slot
675 *
676 * \param[in,out] slot The slot where to store the export representation to
677 * \param[in] data The buffer containing the import representation
678 * \param[in] data_length The amount of bytes in \p data
679 */
680static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
681 const uint8_t *data,
682 size_t data_length )
683{
684 psa_status_t status;
685 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200686 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200687
Steven Cooremana01795d2020-07-24 22:48:15 +0200688 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200689 status = psa_load_rsa_representation( slot->attr.type,
690 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200691 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200692 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200693 if( status != PSA_SUCCESS )
694 goto exit;
695
696 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200697 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200698
Steven Cooreman75b74362020-07-28 14:30:13 +0200699 /* Re-export the data to PSA export format, such that we can store export
700 * representation in the key slot. Export representation in case of RSA is
701 * the smallest representation that's allowed as input, so a straight-up
702 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200703 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200704 if( output == NULL )
705 {
706 status = PSA_ERROR_INSUFFICIENT_MEMORY;
707 goto exit;
708 }
709
Steven Cooremana01795d2020-07-24 22:48:15 +0200710 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200711 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200712 output,
713 data_length,
714 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200715exit:
716 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200717 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200718 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200719
720 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000721 if( status != PSA_SUCCESS )
722 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200723 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000724 return( status );
725 }
726
Steven Cooremana01795d2020-07-24 22:48:15 +0200727 /* On success, store the allocated export-formatted key. */
728 slot->data.key.data = output;
729 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000730
731 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200732}
John Durkop6ba40d12020-11-10 08:50:04 -0800733
734#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800735 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200736
John Durkop9814fa22020-11-04 12:28:15 -0800737#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800738 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
739 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
740 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
741 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Steven Cooreman7f391872020-07-30 14:57:44 +0200742/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200743 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200744 * \param[in] type The type of key contained in \p data.
745 * \param[in] data The buffer from which to load the representation.
746 * \param[in] data_length The size in bytes of \p data.
747 * \param[out] p_ecp Returns a pointer to an ECP context on success.
748 * The caller is responsible for freeing both the
749 * contents of the context and the context itself
750 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200751 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200752static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
753 const uint8_t *data,
754 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200755 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100756{
757 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200758 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200759 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200760 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100761
Steven Cooreman4fed4552020-08-03 14:46:03 +0200762 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
763 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100764 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200765 /* A Weierstrass public key is represented as:
766 * - The byte 0x04;
767 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
768 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200769 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200770 */
771 if( ( data_length & 1 ) == 0 )
772 return( PSA_ERROR_INVALID_ARGUMENT );
773 curve_size = data_length / 2;
774
775 /* Montgomery public keys are represented in compressed format, meaning
776 * their curve_size is equal to the amount of input. */
777
778 /* Private keys are represented in uncompressed private random integer
779 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100780 }
781
Steven Cooreman6d839f02020-07-30 11:36:45 +0200782 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200783 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200784 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200785 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200786 mbedtls_ecp_keypair_init( ecp );
787
Gilles Peskine4cd32772019-12-02 20:49:42 +0100788 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200789 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
790 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100791 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200792 {
793 status = PSA_ERROR_INVALID_ARGUMENT;
794 goto exit;
795 }
796
Steven Cooremanacda8342020-07-24 23:09:52 +0200797 status = mbedtls_to_psa_error(
798 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
799 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200800 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200801
Steven Cooreman6d839f02020-07-30 11:36:45 +0200802 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200803 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200804 {
805 /* Load the public value. */
806 status = mbedtls_to_psa_error(
807 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200808 data,
809 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200810 if( status != PSA_SUCCESS )
811 goto exit;
812
813 /* Check that the point is on the curve. */
814 status = mbedtls_to_psa_error(
815 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
816 if( status != PSA_SUCCESS )
817 goto exit;
818 }
819 else
820 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200821 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200822 status = mbedtls_to_psa_error(
823 mbedtls_ecp_read_key( ecp->grp.id,
824 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200825 data,
826 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200827 if( status != PSA_SUCCESS )
828 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200829 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200830
831 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200832exit:
833 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200834 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200835 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200836 mbedtls_free( ecp );
837 }
838
Steven Cooreman29149862020-08-05 15:43:42 +0200839 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100840}
John Durkop6ba40d12020-11-10 08:50:04 -0800841#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
842 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
843 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
844 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
845 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000846
John Durkop6ba40d12020-11-10 08:50:04 -0800847#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
848 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200849/** Export an ECP key to export representation
850 *
851 * \param[in] type The type of key (public/private) to export
852 * \param[in] ecp The internal ECP representation from which to export
853 * \param[out] data The buffer to export to
854 * \param[in] data_size The length of the buffer to export to
855 * \param[out] data_length The amount of bytes written to \p data
856 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200857static psa_status_t psa_export_ecp_key( psa_key_type_t type,
858 mbedtls_ecp_keypair *ecp,
859 uint8_t *data,
860 size_t data_size,
861 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200862{
Steven Cooremanacda8342020-07-24 23:09:52 +0200863 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000864
Steven Cooremanacda8342020-07-24 23:09:52 +0200865 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200866 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200867 /* Check whether the public part is loaded */
868 if( mbedtls_ecp_is_zero( &ecp->Q ) )
869 {
870 /* Calculate the public key */
871 status = mbedtls_to_psa_error(
872 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
873 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
874 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200875 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200876 }
877
Steven Cooreman4fed4552020-08-03 14:46:03 +0200878 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200879 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
880 MBEDTLS_ECP_PF_UNCOMPRESSED,
881 data_length,
882 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200883 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200884 if( status != PSA_SUCCESS )
885 memset( data, 0, data_size );
886
Steven Cooreman29149862020-08-05 15:43:42 +0200887 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200888 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200889 else
890 {
Steven Cooreman29149862020-08-05 15:43:42 +0200891 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200892 return( PSA_ERROR_BUFFER_TOO_SMALL );
893
894 status = mbedtls_to_psa_error(
895 mbedtls_ecp_write_key( ecp,
896 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200897 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200898 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200899 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200900 else
901 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200902
903 return( status );
904 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200905}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200906
Steven Cooreman4fed4552020-08-03 14:46:03 +0200907/** Import an ECP key from import representation to a slot
908 *
909 * \param[in,out] slot The slot where to store the export representation to
910 * \param[in] data The buffer containing the import representation
911 * \param[in] data_length The amount of bytes in \p data
912 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200913static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
914 const uint8_t *data,
915 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100916{
Steven Cooremanacda8342020-07-24 23:09:52 +0200917 psa_status_t status;
918 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200919 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100920
Steven Cooremanacda8342020-07-24 23:09:52 +0200921 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200922 status = psa_load_ecp_representation( slot->attr.type,
923 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200924 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200925 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100926 if( status != PSA_SUCCESS )
927 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100928
Steven Cooremanacda8342020-07-24 23:09:52 +0200929 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200930 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200931 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200932 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200933
Steven Cooremanacda8342020-07-24 23:09:52 +0200934 /* Re-export the data to PSA export format. There is currently no support
935 * for other input formats then the export format, so this is a 1-1
936 * copy operation. */
937 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200938 if( output == NULL )
939 {
940 status = PSA_ERROR_INSUFFICIENT_MEMORY;
941 goto exit;
942 }
943
944 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200945 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200946 output,
947 data_length,
948 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100949exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200950 /* Always free the PK object (will also free contained ECP context) */
951 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200952 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200953
954 /* Free the allocated buffer only on error. */
955 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100956 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200957 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200958 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100959 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200960
961 /* On success, store the allocated export-formatted key. */
962 slot->data.key.data = output;
963 slot->data.key.bytes = data_length;
964
965 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100966}
John Durkop9814fa22020-11-04 12:28:15 -0800967#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
968 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100969
Gilles Peskineb46bef22019-07-30 21:32:04 +0200970/** Return the size of the key in the given slot, in bits.
971 *
972 * \param[in] slot A key slot.
973 *
974 * \return The key size in bits, read from the metadata in the slot.
975 */
976static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
977{
978 return( slot->attr.bits );
979}
980
Steven Cooreman75b74362020-07-28 14:30:13 +0200981/** Try to allocate a buffer to an empty key slot.
982 *
983 * \param[in,out] slot Key slot to attach buffer to.
984 * \param[in] buffer_length Requested size of the buffer.
985 *
986 * \retval #PSA_SUCCESS
987 * The buffer has been successfully allocated.
988 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
989 * Not enough memory was available for allocation.
990 * \retval #PSA_ERROR_ALREADY_EXISTS
991 * Trying to allocate a buffer to a non-empty key slot.
992 */
993static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
994 size_t buffer_length )
995{
996 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200997 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200998
999 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
1000 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001001 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +02001002
1003 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +02001004 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001005}
1006
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001007psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
1008 const uint8_t* data,
1009 size_t data_length )
1010{
1011 psa_status_t status = psa_allocate_buffer_to_slot( slot,
1012 data_length );
1013 if( status != PSA_SUCCESS )
1014 return( status );
1015
1016 memcpy( slot->data.key.data, data, data_length );
1017 return( PSA_SUCCESS );
1018}
1019
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001020/** Import key data into a slot.
1021 *
1022 * `slot->type` must have been set previously.
1023 * This function assumes that the slot does not contain any key material yet.
1024 * On failure, the slot content is unchanged.
1025 *
1026 * Persistent storage is not affected.
1027 *
1028 * \param[in,out] slot The key slot to import data into.
1029 * Its `type` field must have previously been set to
1030 * the desired key type.
1031 * It must not contain any key material yet.
1032 * \param[in] data Buffer containing the key material to parse and import.
1033 * \param data_length Size of \p data in bytes.
1034 *
1035 * \retval #PSA_SUCCESS
1036 * \retval #PSA_ERROR_INVALID_ARGUMENT
1037 * \retval #PSA_ERROR_NOT_SUPPORTED
1038 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1039 */
1040static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1041 const uint8_t *data,
1042 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001043{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001044 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001045 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001046
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001047 /* zero-length keys are never supported. */
1048 if( data_length == 0 )
1049 return( PSA_ERROR_NOT_SUPPORTED );
1050
Gilles Peskine8e338702019-07-30 20:06:31 +02001051 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001052 {
Steven Cooreman04524762020-10-13 17:43:44 +02001053 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001054
Steven Cooreman75b74362020-07-28 14:30:13 +02001055 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1056 if( data_length > SIZE_MAX / 8 )
1057 return( PSA_ERROR_NOT_SUPPORTED );
1058
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001059 /* Enforce a size limit, and in particular ensure that the bit
1060 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001061 if( bit_size > PSA_MAX_KEY_BITS )
1062 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001063
1064 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001065 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001066 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001067
1068 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001069 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001070 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001071 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001072
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001073 /* Write the actual key size to the slot.
1074 * psa_start_key_creation() wrote the size declared by the
1075 * caller, which may be 0 (meaning unspecified) or wrong. */
1076 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001077
1078 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001079 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001080 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001081 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001082 /* Try validation through accelerators first. */
1083 bit_size = slot->attr.bits;
1084 psa_key_attributes_t attributes = {
1085 .core = slot->attr
1086 };
1087 status = psa_driver_wrapper_validate_key( &attributes,
1088 data,
1089 data_length,
1090 &bit_size );
1091 if( status == PSA_SUCCESS )
1092 {
1093 /* Key has been validated successfully by an accelerator.
1094 * Copy key material into slot. */
1095 status = psa_copy_key_material_into_slot( slot, data, data_length );
1096 if( status != PSA_SUCCESS )
1097 return( status );
1098
1099 slot->attr.bits = (psa_key_bits_t) bit_size;
1100 return( PSA_SUCCESS );
1101 }
1102 else if( status != PSA_ERROR_NOT_SUPPORTED )
1103 return( status );
1104
1105 /* Key format is not supported by any accelerator, try software fallback
1106 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -08001107#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1108 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001109 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1110 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001111 return( psa_import_ecp_key( slot, data, data_length ) );
1112 }
John Durkop9814fa22020-11-04 12:28:15 -08001113#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1114 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -07001115#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1116 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +01001117 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001118 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001119 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001120 }
John Durkop9814fa22020-11-04 12:28:15 -08001121#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1122 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001123
1124 /* Fell through the fallback as well, so have nothing else to try. */
1125 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001126 }
1127 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001128 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001129 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001130 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001131 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001132}
1133
Gilles Peskinef603c712019-01-19 13:40:11 +01001134/** Calculate the intersection of two algorithm usage policies.
1135 *
1136 * Return 0 (which allows no operation) on incompatibility.
1137 */
1138static psa_algorithm_t psa_key_policy_algorithm_intersection(
1139 psa_algorithm_t alg1,
1140 psa_algorithm_t alg2 )
1141{
Gilles Peskine549ea862019-05-22 11:45:59 +02001142 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001143 if( alg1 == alg2 )
1144 return( alg1 );
1145 /* If the policies are from the same hash-and-sign family, check
1146 * if one is a wildcard. If so the other has the specific algorithm. */
1147 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1148 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1149 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1150 {
1151 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1152 return( alg2 );
1153 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1154 return( alg1 );
1155 }
1156 /* If the policies are incompatible, allow nothing. */
1157 return( 0 );
1158}
1159
Gilles Peskined6f371b2019-05-10 19:33:38 +02001160static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1161 psa_algorithm_t requested_alg )
1162{
Gilles Peskine549ea862019-05-22 11:45:59 +02001163 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001164 if( requested_alg == policy_alg )
1165 return( 1 );
1166 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001167 * and requested_alg is the same hash-and-sign family with any hash,
1168 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001169 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1170 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1171 {
1172 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1173 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1174 }
Steven Cooremance48e852020-10-05 16:02:45 +02001175 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001176 * a key derivation with that key agreement should also be allowed. This
1177 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001178 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1179 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1180 {
1181 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1182 policy_alg );
1183 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001184 /* If it isn't permitted, it's forbidden. */
1185 return( 0 );
1186}
1187
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001188/** Test whether a policy permits an algorithm.
1189 *
1190 * The caller must test usage flags separately.
1191 */
1192static int psa_key_policy_permits( const psa_key_policy_t *policy,
1193 psa_algorithm_t alg )
1194{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001195 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1196 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001197}
1198
Gilles Peskinef603c712019-01-19 13:40:11 +01001199/** Restrict a key policy based on a constraint.
1200 *
1201 * \param[in,out] policy The policy to restrict.
1202 * \param[in] constraint The policy constraint to apply.
1203 *
1204 * \retval #PSA_SUCCESS
1205 * \c *policy contains the intersection of the original value of
1206 * \c *policy and \c *constraint.
1207 * \retval #PSA_ERROR_INVALID_ARGUMENT
1208 * \c *policy and \c *constraint are incompatible.
1209 * \c *policy is unchanged.
1210 */
1211static psa_status_t psa_restrict_key_policy(
1212 psa_key_policy_t *policy,
1213 const psa_key_policy_t *constraint )
1214{
1215 psa_algorithm_t intersection_alg =
1216 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001217 psa_algorithm_t intersection_alg2 =
1218 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001219 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1220 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001221 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1222 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001223 policy->usage &= constraint->usage;
1224 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001225 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001226 return( PSA_SUCCESS );
1227}
1228
Darryl Green06fd18d2018-07-16 11:21:11 +01001229/** Retrieve a slot which must contain a key. The key must have allow all the
1230 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1231 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001232static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001233 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001234 psa_key_usage_t usage,
1235 psa_algorithm_t alg )
1236{
1237 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001238 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001239
1240 *p_slot = NULL;
1241
Gilles Peskinec5487a82018-12-03 18:08:14 +01001242 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001243 if( status != PSA_SUCCESS )
1244 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001245
1246 /* Enforce that usage policy for the key slot contains all the flags
1247 * required by the usage parameter. There is one exception: public
1248 * keys can always be exported, so we treat public key objects as
1249 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001250 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001251 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001252 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001253 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001254
1255 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001256 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001257 return( PSA_ERROR_NOT_PERMITTED );
1258
1259 *p_slot = slot;
1260 return( PSA_SUCCESS );
1261}
Darryl Green940d72c2018-07-13 13:18:51 +01001262
Gilles Peskine28f8f302019-07-24 13:30:31 +02001263/** Retrieve a slot which must contain a transparent key.
1264 *
1265 * A transparent key is a key for which the key material is directly
1266 * available, as opposed to a key in a secure element.
1267 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001268 * This is a temporary function to use instead of psa_get_key_from_slot()
1269 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001270 */
1271#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1272static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1273 psa_key_slot_t **p_slot,
1274 psa_key_usage_t usage,
1275 psa_algorithm_t alg )
1276{
1277 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1278 if( status != PSA_SUCCESS )
1279 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001280 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001281 {
1282 *p_slot = NULL;
1283 return( PSA_ERROR_NOT_SUPPORTED );
1284 }
1285 return( PSA_SUCCESS );
1286}
1287#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1288/* With no secure element support, all keys are transparent. */
1289#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1290 psa_get_key_from_slot( handle, p_slot, usage, alg )
1291#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1292
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001293/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001294static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001295{
Gilles Peskine73167e12019-07-12 23:44:37 +02001296#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1297 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001298 {
1299 /* No key material to clean. */
1300 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001301 else
1302#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001303 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001304 /* Data pointer will always be either a valid pointer or NULL in an
1305 * initialized slot, so we can just free it. */
1306 if( slot->data.key.data != NULL )
1307 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001308 mbedtls_free( slot->data.key.data );
1309 slot->data.key.data = NULL;
1310 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001311 }
Darryl Green40225ba2018-11-15 14:48:15 +00001312
1313 return( PSA_SUCCESS );
1314}
1315
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001316/** Completely wipe a slot in memory, including its policy.
1317 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001318psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001319{
1320 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001321 /* Multipart operations may still be using the key. This is safe
1322 * because all multipart operation objects are independent from
1323 * the key slot: if they need to access the key after the setup
1324 * phase, they have a copy of the key. Note that this means that
1325 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001326 /* At this point, key material and other type-specific content has
1327 * been wiped. Clear remaining metadata. We can call memset and not
1328 * zeroize because the metadata is not particularly sensitive. */
1329 memset( slot, 0, sizeof( *slot ) );
1330 return( status );
1331}
1332
Gilles Peskinec5487a82018-12-03 18:08:14 +01001333psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001334{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001335 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001336 psa_status_t status; /* status of the last operation */
1337 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001338#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1339 psa_se_drv_table_entry_t *driver;
1340#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001341
Gilles Peskine1841cf42019-10-08 15:48:25 +02001342 if( handle == 0 )
1343 return( PSA_SUCCESS );
1344
Gilles Peskinec5487a82018-12-03 18:08:14 +01001345 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001346 if( status != PSA_SUCCESS )
1347 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001348
1349#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001350 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001351 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001352 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001353 /* For a key in a secure element, we need to do three things:
1354 * remove the key file in internal storage, destroy the
1355 * key inside the secure element, and update the driver's
1356 * persistent data. Start a transaction that will encompass these
1357 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001358 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001359 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001360 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001361 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001362 status = psa_crypto_save_transaction( );
1363 if( status != PSA_SUCCESS )
1364 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001365 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001366 /* We should still try to destroy the key in the secure
1367 * element and the key metadata in storage. This is especially
1368 * important if the error is that the storage is full.
1369 * But how to do it exactly without risking an inconsistent
1370 * state after a reset?
1371 * https://github.com/ARMmbed/mbed-crypto/issues/215
1372 */
1373 overall_status = status;
1374 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001375 }
1376
Gilles Peskine354f7672019-07-12 23:46:38 +02001377 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001378 if( overall_status == PSA_SUCCESS )
1379 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001380 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001381#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1382
Darryl Greend49a4992018-06-18 17:27:26 +01001383#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001384 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001385 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001386 status = psa_destroy_persistent_key( slot->attr.id );
1387 if( overall_status == PSA_SUCCESS )
1388 overall_status = status;
1389
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001390 /* TODO: other slots may have a copy of the same key. We should
1391 * invalidate them.
1392 * https://github.com/ARMmbed/mbed-crypto/issues/214
1393 */
Darryl Greend49a4992018-06-18 17:27:26 +01001394 }
1395#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001396
Gilles Peskinefc762652019-07-22 19:30:34 +02001397#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1398 if( driver != NULL )
1399 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001400 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001401 if( overall_status == PSA_SUCCESS )
1402 overall_status = status;
1403 status = psa_crypto_stop_transaction( );
1404 if( overall_status == PSA_SUCCESS )
1405 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001406 }
1407#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1408
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001409#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1410exit:
1411#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001412 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001413 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1414 if( overall_status == PSA_SUCCESS )
1415 overall_status = status;
1416 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001417}
1418
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001419void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001420{
Gilles Peskineb699f072019-04-26 16:06:02 +02001421 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001422 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001423}
1424
Gilles Peskineb699f072019-04-26 16:06:02 +02001425psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1426 psa_key_type_t type,
1427 const uint8_t *data,
1428 size_t data_length )
1429{
1430 uint8_t *copy = NULL;
1431
1432 if( data_length != 0 )
1433 {
1434 copy = mbedtls_calloc( 1, data_length );
1435 if( copy == NULL )
1436 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1437 memcpy( copy, data, data_length );
1438 }
1439 /* After this point, this function is guaranteed to succeed, so it
1440 * can start modifying `*attributes`. */
1441
1442 if( attributes->domain_parameters != NULL )
1443 {
1444 mbedtls_free( attributes->domain_parameters );
1445 attributes->domain_parameters = NULL;
1446 attributes->domain_parameters_size = 0;
1447 }
1448
1449 attributes->domain_parameters = copy;
1450 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001451 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001452 return( PSA_SUCCESS );
1453}
1454
1455psa_status_t psa_get_key_domain_parameters(
1456 const psa_key_attributes_t *attributes,
1457 uint8_t *data, size_t data_size, size_t *data_length )
1458{
1459 if( attributes->domain_parameters_size > data_size )
1460 return( PSA_ERROR_BUFFER_TOO_SMALL );
1461 *data_length = attributes->domain_parameters_size;
1462 if( attributes->domain_parameters_size != 0 )
1463 memcpy( data, attributes->domain_parameters,
1464 attributes->domain_parameters_size );
1465 return( PSA_SUCCESS );
1466}
1467
John Durkop6ba40d12020-11-10 08:50:04 -08001468#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
1469 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
1470 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
1471 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
1472 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001473 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001474static psa_status_t psa_get_rsa_public_exponent(
1475 const mbedtls_rsa_context *rsa,
1476 psa_key_attributes_t *attributes )
1477{
1478 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001480 uint8_t *buffer = NULL;
1481 size_t buflen;
1482 mbedtls_mpi_init( &mpi );
1483
1484 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1485 if( ret != 0 )
1486 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001487 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1488 {
1489 /* It's the default value, which is reported as an empty string,
1490 * so there's nothing to do. */
1491 goto exit;
1492 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001493
1494 buflen = mbedtls_mpi_size( &mpi );
1495 buffer = mbedtls_calloc( 1, buflen );
1496 if( buffer == NULL )
1497 {
1498 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1499 goto exit;
1500 }
1501 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1502 if( ret != 0 )
1503 goto exit;
1504 attributes->domain_parameters = buffer;
1505 attributes->domain_parameters_size = buflen;
1506
1507exit:
1508 mbedtls_mpi_free( &mpi );
1509 if( ret != 0 )
1510 mbedtls_free( buffer );
1511 return( mbedtls_to_psa_error( ret ) );
1512}
John Durkop6ba40d12020-11-10 08:50:04 -08001513#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
1514 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
1515 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
1516 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
1517 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001518 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001519
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001520/** Retrieve all the publicly-accessible attributes of a key.
1521 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001522psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1523 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001524{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001525 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001526 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001527
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001528 psa_reset_key_attributes( attributes );
1529
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001530 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001531 if( status != PSA_SUCCESS )
1532 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001533
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001534 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001535 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1536 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1537
1538#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1539 if( psa_key_slot_is_external( slot ) )
1540 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1541#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001542
Gilles Peskine8e338702019-07-30 20:06:31 +02001543 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001544 {
John Durkop6ba40d12020-11-10 08:50:04 -08001545#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
1546 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
1547 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
1548 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
1549 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001550 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001551 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001552 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001553#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001554 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001555 * is not yet implemented.
1556 * https://github.com/ARMmbed/mbed-crypto/issues/216
1557 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001558 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001559 break;
1560#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001561 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001562 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001563
Steven Cooreman4fed4552020-08-03 14:46:03 +02001564 status = psa_load_rsa_representation( slot->attr.type,
1565 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001566 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001567 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001568 if( status != PSA_SUCCESS )
1569 break;
1570
Steven Cooremana2371e52020-07-28 14:30:39 +02001571 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001572 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001573 mbedtls_rsa_free( rsa );
1574 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001575 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001576 break;
John Durkop6ba40d12020-11-10 08:50:04 -08001577#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
1578 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
1579 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
1580 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
1581 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001582 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001583 default:
1584 /* Nothing else to do. */
1585 break;
1586 }
1587
1588 if( status != PSA_SUCCESS )
1589 psa_reset_key_attributes( attributes );
1590 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001591}
1592
Gilles Peskinec8000c02019-08-02 20:15:51 +02001593#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1594psa_status_t psa_get_key_slot_number(
1595 const psa_key_attributes_t *attributes,
1596 psa_key_slot_number_t *slot_number )
1597{
1598 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1599 {
1600 *slot_number = attributes->slot_number;
1601 return( PSA_SUCCESS );
1602 }
1603 else
1604 return( PSA_ERROR_INVALID_ARGUMENT );
1605}
1606#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1607
Steven Cooremana01795d2020-07-24 22:48:15 +02001608static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1609 uint8_t *data,
1610 size_t data_size,
1611 size_t *data_length )
1612{
1613 if( slot->data.key.bytes > data_size )
1614 return( PSA_ERROR_BUFFER_TOO_SMALL );
1615 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1616 memset( data + slot->data.key.bytes, 0,
1617 data_size - slot->data.key.bytes );
1618 *data_length = slot->data.key.bytes;
1619 return( PSA_SUCCESS );
1620}
1621
Gilles Peskinef603c712019-01-19 13:40:11 +01001622static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1623 uint8_t *data,
1624 size_t data_size,
1625 size_t *data_length,
1626 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001627{
Gilles Peskine5d309672019-07-12 23:47:28 +02001628#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1629 const psa_drv_se_t *drv;
1630 psa_drv_se_context_t *drv_context;
1631#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1632
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001633 *data_length = 0;
1634
Gilles Peskine8e338702019-07-30 20:06:31 +02001635 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001636 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001637
Gilles Peskinef9168942019-09-12 19:20:29 +02001638 /* Reject a zero-length output buffer now, since this can never be a
1639 * valid key representation. This way we know that data must be a valid
1640 * pointer and we can do things like memset(data, ..., data_size). */
1641 if( data_size == 0 )
1642 return( PSA_ERROR_BUFFER_TOO_SMALL );
1643
Gilles Peskine5d309672019-07-12 23:47:28 +02001644#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001645 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001646 {
1647 psa_drv_se_export_key_t method;
1648 if( drv->key_management == NULL )
1649 return( PSA_ERROR_NOT_SUPPORTED );
1650 method = ( export_public_key ?
1651 drv->key_management->p_export_public :
1652 drv->key_management->p_export );
1653 if( method == NULL )
1654 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001655 return( method( drv_context,
1656 slot->data.se.slot_number,
1657 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001658 }
1659#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1660
Gilles Peskine8e338702019-07-30 20:06:31 +02001661 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001662 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001663 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001664 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001665 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1666 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001667 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001668 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001669 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001670 /* Exporting public -> public */
1671 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1672 }
1673 else if( !export_public_key )
1674 {
1675 /* Exporting private -> private */
1676 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1677 }
1678 /* Need to export the public part of a private key,
1679 * so conversion is needed */
1680 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1681 {
John Durkop0e005192020-10-31 22:06:54 -07001682#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1683 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001684 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001685 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001686 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001687 slot->data.key.data,
1688 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001689 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001690 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001691 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001692
Steven Cooreman560c28a2020-07-24 23:20:24 +02001693 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001694 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001695 data,
1696 data_size,
1697 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001698
Steven Cooremana2371e52020-07-28 14:30:39 +02001699 mbedtls_rsa_free( rsa );
1700 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001701
Steven Cooreman560c28a2020-07-24 23:20:24 +02001702 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001703#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001704 /* We don't know how to convert a private RSA key to public. */
1705 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001706#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1707 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001708 }
1709 else
Moran Pekera998bc62018-04-16 18:16:20 +03001710 {
John Durkop6ba40d12020-11-10 08:50:04 -08001711#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1712 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001713 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001714 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001715 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001716 slot->data.key.data,
1717 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001718 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001719 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001720 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001721
1722 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1723 PSA_KEY_TYPE_ECC_GET_FAMILY(
1724 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001725 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001726 data,
1727 data_size,
1728 data_length );
1729
Steven Cooremana2371e52020-07-28 14:30:39 +02001730 mbedtls_ecp_keypair_free( ecp );
1731 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001732 return( status );
1733#else
1734 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001735 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001736#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1737 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001738 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001739 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001740 else
1741 {
1742 /* This shouldn't happen in the reference implementation, but
1743 it is valid for a special-purpose implementation to omit
1744 support for exporting certain key types. */
1745 return( PSA_ERROR_NOT_SUPPORTED );
1746 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001747}
1748
Gilles Peskinec5487a82018-12-03 18:08:14 +01001749psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001750 uint8_t *data,
1751 size_t data_size,
1752 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001753{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001754 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001755 psa_status_t status;
1756
1757 /* Set the key to empty now, so that even when there are errors, we always
1758 * set data_length to a value between 0 and data_size. On error, setting
1759 * the key to empty is a good choice because an empty key representation is
1760 * unlikely to be accepted anywhere. */
1761 *data_length = 0;
1762
1763 /* Export requires the EXPORT flag. There is an exception for public keys,
1764 * which don't require any flag, but psa_get_key_from_slot takes
1765 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001766 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001767 if( status != PSA_SUCCESS )
1768 return( status );
1769 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001770 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001771}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001772
Gilles Peskinec5487a82018-12-03 18:08:14 +01001773psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001774 uint8_t *data,
1775 size_t data_size,
1776 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001777{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001778 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001779 psa_status_t status;
1780
1781 /* Set the key to empty now, so that even when there are errors, we always
1782 * set data_length to a value between 0 and data_size. On error, setting
1783 * the key to empty is a good choice because an empty key representation is
1784 * unlikely to be accepted anywhere. */
1785 *data_length = 0;
1786
1787 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001788 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001789 if( status != PSA_SUCCESS )
1790 return( status );
1791 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001792 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001793}
1794
Gilles Peskine91e8c332019-08-02 19:19:39 +02001795#if defined(static_assert)
1796static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1797 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001798static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001799 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001800static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001801 "One or more key attribute flag is listed as both internal-only and external-only" );
1802#endif
1803
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001804/** Validate that a key policy is internally well-formed.
1805 *
1806 * This function only rejects invalid policies. It does not validate the
1807 * consistency of the policy with respect to other attributes of the key
1808 * such as the key type.
1809 */
1810static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001811{
1812 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001813 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001814 PSA_KEY_USAGE_ENCRYPT |
1815 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001816 PSA_KEY_USAGE_SIGN_HASH |
1817 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001818 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1819 return( PSA_ERROR_INVALID_ARGUMENT );
1820
Gilles Peskine4747d192019-04-17 15:05:45 +02001821 return( PSA_SUCCESS );
1822}
1823
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001824/** Validate the internal consistency of key attributes.
1825 *
1826 * This function only rejects invalid attribute values. If does not
1827 * validate the consistency of the attributes with any key data that may
1828 * be involved in the creation of the key.
1829 *
1830 * Call this function early in the key creation process.
1831 *
1832 * \param[in] attributes Key attributes for the new key.
1833 * \param[out] p_drv On any return, the driver for the key, if any.
1834 * NULL for a transparent key.
1835 *
1836 */
1837static psa_status_t psa_validate_key_attributes(
1838 const psa_key_attributes_t *attributes,
1839 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001840{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001841 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001842
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001843 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1844 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001845 if( status != PSA_SUCCESS )
1846 return( status );
1847
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001848 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1849 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001850 if( status != PSA_SUCCESS )
1851 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001852
1853 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001854 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001855 return( status );
1856
1857 /* Refuse to create overly large keys.
1858 * Note that this doesn't trigger on import if the attributes don't
1859 * explicitly specify a size (so psa_get_key_bits returns 0), so
1860 * psa_import_key() needs its own checks. */
1861 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1862 return( PSA_ERROR_NOT_SUPPORTED );
1863
Gilles Peskine91e8c332019-08-02 19:19:39 +02001864 /* Reject invalid flags. These should not be reachable through the API. */
1865 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1866 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1867 return( PSA_ERROR_INVALID_ARGUMENT );
1868
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001869 return( PSA_SUCCESS );
1870}
1871
Gilles Peskine4747d192019-04-17 15:05:45 +02001872/** Prepare a key slot to receive key material.
1873 *
1874 * This function allocates a key slot and sets its metadata.
1875 *
1876 * If this function fails, call psa_fail_key_creation().
1877 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001878 * This function is intended to be used as follows:
1879 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1880 * it with the specified attributes, and assign it a handle.
1881 * -# Populate the slot with the key material.
1882 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1883 * In case of failure at any step, stop the sequence and call
1884 * psa_fail_key_creation().
1885 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001886 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001887 * \param[in] attributes Key attributes for the new key.
1888 * \param[out] handle On success, a handle for the allocated slot.
1889 * \param[out] p_slot On success, a pointer to the prepared slot.
1890 * \param[out] p_drv On any return, the driver for the key, if any.
1891 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001892 *
1893 * \retval #PSA_SUCCESS
1894 * The key slot is ready to receive key material.
1895 * \return If this function fails, the key slot is an invalid state.
1896 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001897 */
1898static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001899 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001900 const psa_key_attributes_t *attributes,
1901 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001902 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001903 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001904{
1905 psa_status_t status;
1906 psa_key_slot_t *slot;
1907
Gilles Peskinedf179142019-07-15 22:02:14 +02001908 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001909 *p_drv = NULL;
1910
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001911 status = psa_validate_key_attributes( attributes, p_drv );
1912 if( status != PSA_SUCCESS )
1913 return( status );
1914
Gilles Peskineedbed562019-08-07 18:19:59 +02001915 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001916 if( status != PSA_SUCCESS )
1917 return( status );
1918 slot = *p_slot;
1919
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001920 /* We're storing the declared bit-size of the key. It's up to each
1921 * creation mechanism to verify that this information is correct.
1922 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001923 * an input (generate, device) but not for those where the bit-size
1924 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001925
1926 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001927
Gilles Peskine91e8c332019-08-02 19:19:39 +02001928 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001929 * external-only flags, query `attributes`. Thanks to the check
1930 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001931 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001932 * may have set. */
1933 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001934
Gilles Peskinecbaff462019-07-12 23:46:04 +02001935#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001936 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001937 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001938 * create the key file in internal storage, create the
1939 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001940 * persistent data. This is done by starting a transaction that will
1941 * encompass these three actions.
1942 * For registering a volatile key, we just need to find an appropriate
1943 * slot number inside the SE. Since the key is designated volatile, creating
1944 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001945 /* The first thing to do is to find a slot number for the new key.
1946 * We save the slot number in persistent storage as part of the
1947 * transaction data. It will be needed to recover if the power
1948 * fails during the key creation process, to clean up on the secure
1949 * element side after restarting. Obtaining a slot number from the
1950 * secure element driver updates its persistent state, but we do not yet
1951 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001952 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001953 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001954 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001955 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001956 &slot->data.se.slot_number );
1957 if( status != PSA_SUCCESS )
1958 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001959
1960 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001961 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001962 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1963 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1964 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1965 psa_crypto_transaction.key.id = slot->attr.id;
1966 status = psa_crypto_save_transaction( );
1967 if( status != PSA_SUCCESS )
1968 {
1969 (void) psa_crypto_stop_transaction( );
1970 return( status );
1971 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001972 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001973 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001974
1975 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1976 {
1977 /* Key registration only makes sense with a secure element. */
1978 return( PSA_ERROR_INVALID_ARGUMENT );
1979 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001980#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1981
Darryl Green0c6575a2018-11-07 16:05:30 +00001982 return( status );
1983}
Gilles Peskine4747d192019-04-17 15:05:45 +02001984
1985/** Finalize the creation of a key once its key material has been set.
1986 *
1987 * This entails writing the key to persistent storage.
1988 *
1989 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001990 * See the documentation of psa_start_key_creation() for the intended use
1991 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001992 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001993 * \param[in,out] slot Pointer to the slot with key material.
1994 * \param[in] driver The secure element driver for the key,
1995 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001996 *
1997 * \retval #PSA_SUCCESS
1998 * The key was successfully created. The handle is now valid.
1999 * \return If this function fails, the key slot is an invalid state.
2000 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002001 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002002static psa_status_t psa_finish_key_creation(
2003 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002004 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002005{
2006 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002007 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002008 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002009
2010#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002011 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002012 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002013#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2014 if( driver != NULL )
2015 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002016 psa_se_key_data_storage_t data;
2017#if defined(static_assert)
2018 static_assert( sizeof( slot->data.se.slot_number ) ==
2019 sizeof( data.slot_number ),
2020 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002021#endif
2022 memcpy( &data.slot_number, &slot->data.se.slot_number,
2023 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002024 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002025 (uint8_t*) &data,
2026 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002027 }
2028 else
2029#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2030 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002031 /* Key material is saved in export representation in the slot, so
2032 * just pass the slot buffer for storage. */
2033 status = psa_save_persistent_key( &slot->attr,
2034 slot->data.key.data,
2035 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002036 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002037 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002038#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2039
Gilles Peskinecbaff462019-07-12 23:46:04 +02002040#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002041 /* Finish the transaction for a key creation. This does not
2042 * happen when registering an existing key. Detect this case
2043 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002044 * creation of a persistent key in a secure element requires a transaction,
2045 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002046 if( driver != NULL &&
2047 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002048 {
2049 status = psa_save_se_persistent_data( driver );
2050 if( status != PSA_SUCCESS )
2051 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002052 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002053 return( status );
2054 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002055 status = psa_crypto_stop_transaction( );
2056 if( status != PSA_SUCCESS )
2057 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002058 }
2059#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2060
Gilles Peskine4747d192019-04-17 15:05:45 +02002061 return( status );
2062}
2063
2064/** Abort the creation of a key.
2065 *
2066 * You may call this function after calling psa_start_key_creation(),
2067 * or after psa_finish_key_creation() fails. In other circumstances, this
2068 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002069 * See the documentation of psa_start_key_creation() for the intended use
2070 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002071 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002072 * \param[in,out] slot Pointer to the slot with key material.
2073 * \param[in] driver The secure element driver for the key,
2074 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002075 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002076static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002077 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002078{
Gilles Peskine011e4282019-06-26 18:34:38 +02002079 (void) driver;
2080
Gilles Peskine4747d192019-04-17 15:05:45 +02002081 if( slot == NULL )
2082 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002083
2084#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002085 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002086 * element, and the failure happened later (when saving metadata
2087 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002088 * element.
2089 * https://github.com/ARMmbed/mbed-crypto/issues/217
2090 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002091
Gilles Peskined7729582019-08-05 15:55:54 +02002092 /* Abort the ongoing transaction if any (there may not be one if
2093 * the creation process failed before starting one, or if the
2094 * key creation is a registration of a key in a secure element).
2095 * Earlier functions must already have done what it takes to undo any
2096 * partial creation. All that's left is to update the transaction data
2097 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002098 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002099#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2100
Gilles Peskine4747d192019-04-17 15:05:45 +02002101 psa_wipe_key_slot( slot );
2102}
2103
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002104/** Validate optional attributes during key creation.
2105 *
2106 * Some key attributes are optional during key creation. If they are
2107 * specified in the attributes structure, check that they are consistent
2108 * with the data in the slot.
2109 *
2110 * This function should be called near the end of key creation, after
2111 * the slot in memory is fully populated but before saving persistent data.
2112 */
2113static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002114 const psa_key_slot_t *slot,
2115 const psa_key_attributes_t *attributes )
2116{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002117 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002118 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002119 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002120 return( PSA_ERROR_INVALID_ARGUMENT );
2121 }
2122
2123 if( attributes->domain_parameters_size != 0 )
2124 {
John Durkop0e005192020-10-31 22:06:54 -07002125#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2126 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002127 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002128 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002129 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002130 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002132
Steven Cooreman7f391872020-07-30 14:57:44 +02002133 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002134 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002135 slot->data.key.data,
2136 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002137 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002138 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002139 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002140
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002141 mbedtls_mpi_init( &actual );
2142 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002143 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002144 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002145 mbedtls_rsa_free( rsa );
2146 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002147 if( ret != 0 )
2148 goto rsa_exit;
2149 ret = mbedtls_mpi_read_binary( &required,
2150 attributes->domain_parameters,
2151 attributes->domain_parameters_size );
2152 if( ret != 0 )
2153 goto rsa_exit;
2154 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2155 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2156 rsa_exit:
2157 mbedtls_mpi_free( &actual );
2158 mbedtls_mpi_free( &required );
2159 if( ret != 0)
2160 return( mbedtls_to_psa_error( ret ) );
2161 }
2162 else
John Durkop9814fa22020-11-04 12:28:15 -08002163#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2164 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002165 {
2166 return( PSA_ERROR_INVALID_ARGUMENT );
2167 }
2168 }
2169
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002170 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002171 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002172 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002173 return( PSA_ERROR_INVALID_ARGUMENT );
2174 }
2175
2176 return( PSA_SUCCESS );
2177}
2178
Gilles Peskine4747d192019-04-17 15:05:45 +02002179psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002180 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002181 size_t data_length,
2182 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002183{
2184 psa_status_t status;
2185 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002186 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002187
Gilles Peskine0f84d622019-09-12 19:03:13 +02002188 /* Reject zero-length symmetric keys (including raw data key objects).
2189 * This also rejects any key which might be encoded as an empty string,
2190 * which is never valid. */
2191 if( data_length == 0 )
2192 return( PSA_ERROR_INVALID_ARGUMENT );
2193
Gilles Peskinedf179142019-07-15 22:02:14 +02002194 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2195 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002196 if( status != PSA_SUCCESS )
2197 goto exit;
2198
Gilles Peskine5d309672019-07-12 23:47:28 +02002199#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2200 if( driver != NULL )
2201 {
2202 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002203 /* The driver should set the number of key bits, however in
2204 * case it doesn't, we initialize bits to an invalid value. */
2205 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002206 if( drv->key_management == NULL ||
2207 drv->key_management->p_import == NULL )
2208 {
2209 status = PSA_ERROR_NOT_SUPPORTED;
2210 goto exit;
2211 }
2212 status = drv->key_management->p_import(
2213 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002214 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002215 &bits );
2216 if( status != PSA_SUCCESS )
2217 goto exit;
2218 if( bits > PSA_MAX_KEY_BITS )
2219 {
2220 status = PSA_ERROR_NOT_SUPPORTED;
2221 goto exit;
2222 }
2223 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002224 }
2225 else
2226#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2227 {
2228 status = psa_import_key_into_slot( slot, data, data_length );
2229 if( status != PSA_SUCCESS )
2230 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002231 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002232 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002233 if( status != PSA_SUCCESS )
2234 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002235
Gilles Peskine011e4282019-06-26 18:34:38 +02002236 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002237exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002238 if( status != PSA_SUCCESS )
2239 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002240 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002241 *handle = 0;
2242 }
2243 return( status );
2244}
2245
Gilles Peskined7729582019-08-05 15:55:54 +02002246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2247psa_status_t mbedtls_psa_register_se_key(
2248 const psa_key_attributes_t *attributes )
2249{
2250 psa_status_t status;
2251 psa_key_slot_t *slot = NULL;
2252 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002253 psa_key_handle_t handle = 0;
2254
2255 /* Leaving attributes unspecified is not currently supported.
2256 * It could make sense to query the key type and size from the
2257 * secure element, but not all secure elements support this
2258 * and the driver HAL doesn't currently support it. */
2259 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2260 return( PSA_ERROR_NOT_SUPPORTED );
2261 if( psa_get_key_bits( attributes ) == 0 )
2262 return( PSA_ERROR_NOT_SUPPORTED );
2263
2264 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2265 &handle, &slot, &driver );
2266 if( status != PSA_SUCCESS )
2267 goto exit;
2268
Gilles Peskined7729582019-08-05 15:55:54 +02002269 status = psa_finish_key_creation( slot, driver );
2270
2271exit:
2272 if( status != PSA_SUCCESS )
2273 {
2274 psa_fail_key_creation( slot, driver );
2275 }
2276 /* Registration doesn't keep the key in RAM. */
2277 psa_close_key( handle );
2278 return( status );
2279}
2280#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2281
Gilles Peskinef603c712019-01-19 13:40:11 +01002282static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002283 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002284{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002285 psa_status_t status = psa_copy_key_material_into_slot( target,
2286 source->data.key.data,
2287 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002288 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002289 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002290
Steven Cooreman398aee52020-10-13 14:35:45 +02002291 target->attr.type = source->attr.type;
2292 target->attr.bits = source->attr.bits;
2293
2294 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002295}
2296
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002297psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2298 const psa_key_attributes_t *specified_attributes,
2299 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002300{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002301 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002302 psa_key_slot_t *source_slot = NULL;
2303 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002304 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002305 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002306
Gilles Peskine28f8f302019-07-24 13:30:31 +02002307 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002308 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002309 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002310 goto exit;
2311
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002312 status = psa_validate_optional_attributes( source_slot,
2313 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002314 if( status != PSA_SUCCESS )
2315 goto exit;
2316
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002317 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002318 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002319 if( status != PSA_SUCCESS )
2320 goto exit;
2321
Gilles Peskinedf179142019-07-15 22:02:14 +02002322 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2323 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002324 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002325 if( status != PSA_SUCCESS )
2326 goto exit;
2327
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002328#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2329 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002330 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002331 /* Copying to a secure element is not implemented yet. */
2332 status = PSA_ERROR_NOT_SUPPORTED;
2333 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002334 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002335#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002336
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002337 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002338 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002339 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002340
Gilles Peskine011e4282019-06-26 18:34:38 +02002341 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002342exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002343 if( status != PSA_SUCCESS )
2344 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002345 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002346 *target_handle = 0;
2347 }
2348 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002349}
2350
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002351
2352
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002353/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002354/* Message digests */
2355/****************************************************************/
2356
John Durkop0e005192020-10-31 22:06:54 -07002357#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
2358 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2359 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2360 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002361 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002362static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002363{
2364 switch( alg )
2365 {
2366#if defined(MBEDTLS_MD2_C)
2367 case PSA_ALG_MD2:
2368 return( &mbedtls_md2_info );
2369#endif
2370#if defined(MBEDTLS_MD4_C)
2371 case PSA_ALG_MD4:
2372 return( &mbedtls_md4_info );
2373#endif
2374#if defined(MBEDTLS_MD5_C)
2375 case PSA_ALG_MD5:
2376 return( &mbedtls_md5_info );
2377#endif
2378#if defined(MBEDTLS_RIPEMD160_C)
2379 case PSA_ALG_RIPEMD160:
2380 return( &mbedtls_ripemd160_info );
2381#endif
2382#if defined(MBEDTLS_SHA1_C)
2383 case PSA_ALG_SHA_1:
2384 return( &mbedtls_sha1_info );
2385#endif
2386#if defined(MBEDTLS_SHA256_C)
2387 case PSA_ALG_SHA_224:
2388 return( &mbedtls_sha224_info );
2389 case PSA_ALG_SHA_256:
2390 return( &mbedtls_sha256_info );
2391#endif
2392#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002393#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002394 case PSA_ALG_SHA_384:
2395 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002396#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002397 case PSA_ALG_SHA_512:
2398 return( &mbedtls_sha512_info );
2399#endif
2400 default:
2401 return( NULL );
2402 }
2403}
John Durkop9814fa22020-11-04 12:28:15 -08002404#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
2405 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2406 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2407 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2408 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002409
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002410psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2411{
2412 switch( operation->alg )
2413 {
Gilles Peskine81736312018-06-26 15:04:31 +02002414 case 0:
2415 /* The object has (apparently) been initialized but it is not
2416 * in use. It's ok to call abort on such an object, and there's
2417 * nothing to do. */
2418 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002419#if defined(MBEDTLS_MD2_C)
2420 case PSA_ALG_MD2:
2421 mbedtls_md2_free( &operation->ctx.md2 );
2422 break;
2423#endif
2424#if defined(MBEDTLS_MD4_C)
2425 case PSA_ALG_MD4:
2426 mbedtls_md4_free( &operation->ctx.md4 );
2427 break;
2428#endif
2429#if defined(MBEDTLS_MD5_C)
2430 case PSA_ALG_MD5:
2431 mbedtls_md5_free( &operation->ctx.md5 );
2432 break;
2433#endif
2434#if defined(MBEDTLS_RIPEMD160_C)
2435 case PSA_ALG_RIPEMD160:
2436 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2437 break;
2438#endif
2439#if defined(MBEDTLS_SHA1_C)
2440 case PSA_ALG_SHA_1:
2441 mbedtls_sha1_free( &operation->ctx.sha1 );
2442 break;
2443#endif
2444#if defined(MBEDTLS_SHA256_C)
2445 case PSA_ALG_SHA_224:
2446 case PSA_ALG_SHA_256:
2447 mbedtls_sha256_free( &operation->ctx.sha256 );
2448 break;
2449#endif
2450#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002451#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002452 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002453#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002454 case PSA_ALG_SHA_512:
2455 mbedtls_sha512_free( &operation->ctx.sha512 );
2456 break;
2457#endif
2458 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002459 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002460 }
2461 operation->alg = 0;
2462 return( PSA_SUCCESS );
2463}
2464
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002465psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002466 psa_algorithm_t alg )
2467{
Janos Follath24eed8d2019-11-22 13:21:35 +00002468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002469
2470 /* A context must be freshly initialized before it can be set up. */
2471 if( operation->alg != 0 )
2472 {
2473 return( PSA_ERROR_BAD_STATE );
2474 }
2475
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002476 switch( alg )
2477 {
2478#if defined(MBEDTLS_MD2_C)
2479 case PSA_ALG_MD2:
2480 mbedtls_md2_init( &operation->ctx.md2 );
2481 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2482 break;
2483#endif
2484#if defined(MBEDTLS_MD4_C)
2485 case PSA_ALG_MD4:
2486 mbedtls_md4_init( &operation->ctx.md4 );
2487 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2488 break;
2489#endif
2490#if defined(MBEDTLS_MD5_C)
2491 case PSA_ALG_MD5:
2492 mbedtls_md5_init( &operation->ctx.md5 );
2493 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2494 break;
2495#endif
2496#if defined(MBEDTLS_RIPEMD160_C)
2497 case PSA_ALG_RIPEMD160:
2498 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2499 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2500 break;
2501#endif
2502#if defined(MBEDTLS_SHA1_C)
2503 case PSA_ALG_SHA_1:
2504 mbedtls_sha1_init( &operation->ctx.sha1 );
2505 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2506 break;
2507#endif
2508#if defined(MBEDTLS_SHA256_C)
2509 case PSA_ALG_SHA_224:
2510 mbedtls_sha256_init( &operation->ctx.sha256 );
2511 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2512 break;
2513 case PSA_ALG_SHA_256:
2514 mbedtls_sha256_init( &operation->ctx.sha256 );
2515 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2516 break;
2517#endif
2518#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002519#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002520 case PSA_ALG_SHA_384:
2521 mbedtls_sha512_init( &operation->ctx.sha512 );
2522 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2523 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002524#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002525 case PSA_ALG_SHA_512:
2526 mbedtls_sha512_init( &operation->ctx.sha512 );
2527 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2528 break;
2529#endif
2530 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002531 return( PSA_ALG_IS_HASH( alg ) ?
2532 PSA_ERROR_NOT_SUPPORTED :
2533 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002534 }
2535 if( ret == 0 )
2536 operation->alg = alg;
2537 else
2538 psa_hash_abort( operation );
2539 return( mbedtls_to_psa_error( ret ) );
2540}
2541
2542psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2543 const uint8_t *input,
2544 size_t input_length )
2545{
Janos Follath24eed8d2019-11-22 13:21:35 +00002546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002547
2548 /* Don't require hash implementations to behave correctly on a
2549 * zero-length input, which may have an invalid pointer. */
2550 if( input_length == 0 )
2551 return( PSA_SUCCESS );
2552
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002553 switch( operation->alg )
2554 {
2555#if defined(MBEDTLS_MD2_C)
2556 case PSA_ALG_MD2:
2557 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2558 input, input_length );
2559 break;
2560#endif
2561#if defined(MBEDTLS_MD4_C)
2562 case PSA_ALG_MD4:
2563 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2564 input, input_length );
2565 break;
2566#endif
2567#if defined(MBEDTLS_MD5_C)
2568 case PSA_ALG_MD5:
2569 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2570 input, input_length );
2571 break;
2572#endif
2573#if defined(MBEDTLS_RIPEMD160_C)
2574 case PSA_ALG_RIPEMD160:
2575 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2576 input, input_length );
2577 break;
2578#endif
2579#if defined(MBEDTLS_SHA1_C)
2580 case PSA_ALG_SHA_1:
2581 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2582 input, input_length );
2583 break;
2584#endif
2585#if defined(MBEDTLS_SHA256_C)
2586 case PSA_ALG_SHA_224:
2587 case PSA_ALG_SHA_256:
2588 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2589 input, input_length );
2590 break;
2591#endif
2592#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002593#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002594 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002595#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002596 case PSA_ALG_SHA_512:
2597 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2598 input, input_length );
2599 break;
2600#endif
2601 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002602 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002603 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002604
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002605 if( ret != 0 )
2606 psa_hash_abort( operation );
2607 return( mbedtls_to_psa_error( ret ) );
2608}
2609
2610psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2611 uint8_t *hash,
2612 size_t hash_size,
2613 size_t *hash_length )
2614{
itayzafrir40835d42018-08-02 13:14:17 +03002615 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002616 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002617 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002618
2619 /* Fill the output buffer with something that isn't a valid hash
2620 * (barring an attack on the hash and deliberately-crafted input),
2621 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002622 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002623 /* If hash_size is 0 then hash may be NULL and then the
2624 * call to memset would have undefined behavior. */
2625 if( hash_size != 0 )
2626 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002627
2628 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002629 {
2630 status = PSA_ERROR_BUFFER_TOO_SMALL;
2631 goto exit;
2632 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002633
2634 switch( operation->alg )
2635 {
2636#if defined(MBEDTLS_MD2_C)
2637 case PSA_ALG_MD2:
2638 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2639 break;
2640#endif
2641#if defined(MBEDTLS_MD4_C)
2642 case PSA_ALG_MD4:
2643 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2644 break;
2645#endif
2646#if defined(MBEDTLS_MD5_C)
2647 case PSA_ALG_MD5:
2648 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2649 break;
2650#endif
2651#if defined(MBEDTLS_RIPEMD160_C)
2652 case PSA_ALG_RIPEMD160:
2653 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2654 break;
2655#endif
2656#if defined(MBEDTLS_SHA1_C)
2657 case PSA_ALG_SHA_1:
2658 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2659 break;
2660#endif
2661#if defined(MBEDTLS_SHA256_C)
2662 case PSA_ALG_SHA_224:
2663 case PSA_ALG_SHA_256:
2664 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2665 break;
2666#endif
2667#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002668#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002669 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002670#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002671 case PSA_ALG_SHA_512:
2672 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2673 break;
2674#endif
2675 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002676 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002677 }
itayzafrir40835d42018-08-02 13:14:17 +03002678 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002679
itayzafrir40835d42018-08-02 13:14:17 +03002680exit:
2681 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002682 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002683 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002684 return( psa_hash_abort( operation ) );
2685 }
2686 else
2687 {
2688 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002689 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002690 }
2691}
2692
Gilles Peskine2d277862018-06-18 15:41:12 +02002693psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2694 const uint8_t *hash,
2695 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002696{
2697 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2698 size_t actual_hash_length;
2699 psa_status_t status = psa_hash_finish( operation,
2700 actual_hash, sizeof( actual_hash ),
2701 &actual_hash_length );
2702 if( status != PSA_SUCCESS )
2703 return( status );
2704 if( actual_hash_length != hash_length )
2705 return( PSA_ERROR_INVALID_SIGNATURE );
2706 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2707 return( PSA_ERROR_INVALID_SIGNATURE );
2708 return( PSA_SUCCESS );
2709}
2710
Gilles Peskine0a749c82019-11-28 19:33:58 +01002711psa_status_t psa_hash_compute( psa_algorithm_t alg,
2712 const uint8_t *input, size_t input_length,
2713 uint8_t *hash, size_t hash_size,
2714 size_t *hash_length )
2715{
2716 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2717 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2718
2719 *hash_length = hash_size;
2720 status = psa_hash_setup( &operation, alg );
2721 if( status != PSA_SUCCESS )
2722 goto exit;
2723 status = psa_hash_update( &operation, input, input_length );
2724 if( status != PSA_SUCCESS )
2725 goto exit;
2726 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2727 if( status != PSA_SUCCESS )
2728 goto exit;
2729
2730exit:
2731 if( status == PSA_SUCCESS )
2732 status = psa_hash_abort( &operation );
2733 else
2734 psa_hash_abort( &operation );
2735 return( status );
2736}
2737
2738psa_status_t psa_hash_compare( psa_algorithm_t alg,
2739 const uint8_t *input, size_t input_length,
2740 const uint8_t *hash, size_t hash_length )
2741{
2742 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2743 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2744
2745 status = psa_hash_setup( &operation, alg );
2746 if( status != PSA_SUCCESS )
2747 goto exit;
2748 status = psa_hash_update( &operation, input, input_length );
2749 if( status != PSA_SUCCESS )
2750 goto exit;
2751 status = psa_hash_verify( &operation, hash, hash_length );
2752 if( status != PSA_SUCCESS )
2753 goto exit;
2754
2755exit:
2756 if( status == PSA_SUCCESS )
2757 status = psa_hash_abort( &operation );
2758 else
2759 psa_hash_abort( &operation );
2760 return( status );
2761}
2762
Gilles Peskineeb35d782019-01-22 17:56:16 +01002763psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2764 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002765{
2766 if( target_operation->alg != 0 )
2767 return( PSA_ERROR_BAD_STATE );
2768
2769 switch( source_operation->alg )
2770 {
2771 case 0:
2772 return( PSA_ERROR_BAD_STATE );
2773#if defined(MBEDTLS_MD2_C)
2774 case PSA_ALG_MD2:
2775 mbedtls_md2_clone( &target_operation->ctx.md2,
2776 &source_operation->ctx.md2 );
2777 break;
2778#endif
2779#if defined(MBEDTLS_MD4_C)
2780 case PSA_ALG_MD4:
2781 mbedtls_md4_clone( &target_operation->ctx.md4,
2782 &source_operation->ctx.md4 );
2783 break;
2784#endif
2785#if defined(MBEDTLS_MD5_C)
2786 case PSA_ALG_MD5:
2787 mbedtls_md5_clone( &target_operation->ctx.md5,
2788 &source_operation->ctx.md5 );
2789 break;
2790#endif
2791#if defined(MBEDTLS_RIPEMD160_C)
2792 case PSA_ALG_RIPEMD160:
2793 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2794 &source_operation->ctx.ripemd160 );
2795 break;
2796#endif
2797#if defined(MBEDTLS_SHA1_C)
2798 case PSA_ALG_SHA_1:
2799 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2800 &source_operation->ctx.sha1 );
2801 break;
2802#endif
2803#if defined(MBEDTLS_SHA256_C)
2804 case PSA_ALG_SHA_224:
2805 case PSA_ALG_SHA_256:
2806 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2807 &source_operation->ctx.sha256 );
2808 break;
2809#endif
2810#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002811#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002812 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002813#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002814 case PSA_ALG_SHA_512:
2815 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2816 &source_operation->ctx.sha512 );
2817 break;
2818#endif
2819 default:
2820 return( PSA_ERROR_NOT_SUPPORTED );
2821 }
2822
2823 target_operation->alg = source_operation->alg;
2824 return( PSA_SUCCESS );
2825}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002826
2827
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002828/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002829/* MAC */
2830/****************************************************************/
2831
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002832static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002833 psa_algorithm_t alg,
2834 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002835 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002836 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002837{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002838 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002839 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002840
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002841 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002842 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002843
Gilles Peskine8c9def32018-02-08 10:02:12 +01002844 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2845 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002846 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002847 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002848 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002849 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002850 mode = MBEDTLS_MODE_STREAM;
2851 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002852 case PSA_ALG_CTR:
2853 mode = MBEDTLS_MODE_CTR;
2854 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002855 case PSA_ALG_CFB:
2856 mode = MBEDTLS_MODE_CFB;
2857 break;
2858 case PSA_ALG_OFB:
2859 mode = MBEDTLS_MODE_OFB;
2860 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002861 case PSA_ALG_ECB_NO_PADDING:
2862 mode = MBEDTLS_MODE_ECB;
2863 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002864 case PSA_ALG_CBC_NO_PADDING:
2865 mode = MBEDTLS_MODE_CBC;
2866 break;
2867 case PSA_ALG_CBC_PKCS7:
2868 mode = MBEDTLS_MODE_CBC;
2869 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002870 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002871 mode = MBEDTLS_MODE_CCM;
2872 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002873 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002874 mode = MBEDTLS_MODE_GCM;
2875 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002876 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2877 mode = MBEDTLS_MODE_CHACHAPOLY;
2878 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002879 default:
2880 return( NULL );
2881 }
2882 }
2883 else if( alg == PSA_ALG_CMAC )
2884 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002885 else
2886 return( NULL );
2887
2888 switch( key_type )
2889 {
2890 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002891 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002892 break;
2893 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002894 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2895 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002896 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002897 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002898 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002899 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002900 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2901 * but two-key Triple-DES is functionally three-key Triple-DES
2902 * with K1=K3, so that's how we present it to mbedtls. */
2903 if( key_bits == 128 )
2904 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002905 break;
2906 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002907 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002908 break;
2909 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002910 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002911 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002912 case PSA_KEY_TYPE_CHACHA20:
2913 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2914 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002915 default:
2916 return( NULL );
2917 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002918 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002919 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002920
Jaeden Amero23bbb752018-06-26 14:16:54 +01002921 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2922 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002923}
2924
John Durkop6ba40d12020-11-10 08:50:04 -08002925#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002926static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002927{
Gilles Peskine2d277862018-06-18 15:41:12 +02002928 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002929 {
2930 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002931 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002932 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002933 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002934 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002935 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002936 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002937 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002938 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002939 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002940 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002941 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002942 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002943 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002944 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002945 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002946 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002947 return( 128 );
2948 default:
2949 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002950 }
2951}
John Durkop6ba40d12020-11-10 08:50:04 -08002952#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002953
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002954/* Initialize the MAC operation structure. Once this function has been
2955 * called, psa_mac_abort can run and will do the right thing. */
2956static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2957 psa_algorithm_t alg )
2958{
2959 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2960
2961 operation->alg = alg;
2962 operation->key_set = 0;
2963 operation->iv_set = 0;
2964 operation->iv_required = 0;
2965 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002966 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002967
2968#if defined(MBEDTLS_CMAC_C)
2969 if( alg == PSA_ALG_CMAC )
2970 {
2971 operation->iv_required = 0;
2972 mbedtls_cipher_init( &operation->ctx.cmac );
2973 status = PSA_SUCCESS;
2974 }
2975 else
2976#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002977#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002978 if( PSA_ALG_IS_HMAC( operation->alg ) )
2979 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002980 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2981 operation->ctx.hmac.hash_ctx.alg = 0;
2982 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002983 }
2984 else
John Durkopd0321952020-10-29 21:37:36 -07002985#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002986 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002987 if( ! PSA_ALG_IS_MAC( alg ) )
2988 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002989 }
2990
2991 if( status != PSA_SUCCESS )
2992 memset( operation, 0, sizeof( *operation ) );
2993 return( status );
2994}
2995
John Durkop6ba40d12020-11-10 08:50:04 -08002996#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002997static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2998{
Gilles Peskine3f108122018-12-07 18:14:53 +01002999 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003000 return( psa_hash_abort( &hmac->hash_ctx ) );
3001}
John Durkop6ba40d12020-11-10 08:50:04 -08003002#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003003
Gilles Peskine8c9def32018-02-08 10:02:12 +01003004psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3005{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003006 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003007 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003008 /* The object has (apparently) been initialized but it is not
3009 * in use. It's ok to call abort on such an object, and there's
3010 * nothing to do. */
3011 return( PSA_SUCCESS );
3012 }
3013 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003014#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003015 if( operation->alg == PSA_ALG_CMAC )
3016 {
3017 mbedtls_cipher_free( &operation->ctx.cmac );
3018 }
3019 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003020#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003021#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003022 if( PSA_ALG_IS_HMAC( operation->alg ) )
3023 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003024 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003025 }
3026 else
John Durkopd0321952020-10-29 21:37:36 -07003027#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003028 {
3029 /* Sanity check (shouldn't happen: operation->alg should
3030 * always have been initialized to a valid value). */
3031 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003032 }
Moran Peker41deec42018-04-04 15:43:05 +03003033
Gilles Peskine8c9def32018-02-08 10:02:12 +01003034 operation->alg = 0;
3035 operation->key_set = 0;
3036 operation->iv_set = 0;
3037 operation->iv_required = 0;
3038 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003039 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003040
Gilles Peskine8c9def32018-02-08 10:02:12 +01003041 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003042
3043bad_state:
3044 /* If abort is called on an uninitialized object, we can't trust
3045 * anything. Wipe the object in case it contains confidential data.
3046 * This may result in a memory leak if a pointer gets overwritten,
3047 * but it's too late to do anything about this. */
3048 memset( operation, 0, sizeof( *operation ) );
3049 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003050}
3051
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003052#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003053static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003054 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003055 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003056 const mbedtls_cipher_info_t *cipher_info )
3057{
Janos Follath24eed8d2019-11-22 13:21:35 +00003058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003059
3060 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003061
3062 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3063 if( ret != 0 )
3064 return( ret );
3065
3066 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003067 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003068 key_bits );
3069 return( ret );
3070}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003071#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003072
John Durkop6ba40d12020-11-10 08:50:04 -08003073#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003074static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3075 const uint8_t *key,
3076 size_t key_length,
3077 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003078{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003079 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003080 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003081 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003082 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003083 psa_status_t status;
3084
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003085 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3086 * overflow below. This should never trigger if the hash algorithm
3087 * is implemented correctly. */
3088 /* The size checks against the ipad and opad buffers cannot be written
3089 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3090 * because that triggers -Wlogical-op on GCC 7.3. */
3091 if( block_size > sizeof( ipad ) )
3092 return( PSA_ERROR_NOT_SUPPORTED );
3093 if( block_size > sizeof( hmac->opad ) )
3094 return( PSA_ERROR_NOT_SUPPORTED );
3095 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003096 return( PSA_ERROR_NOT_SUPPORTED );
3097
Gilles Peskined223b522018-06-11 18:12:58 +02003098 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003099 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003100 status = psa_hash_compute( hash_alg, key, key_length,
3101 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003102 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003103 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003104 }
Gilles Peskine96889972018-07-12 17:07:03 +02003105 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3106 * but it is permitted. It is common when HMAC is used in HKDF, for
3107 * example. Don't call `memcpy` in the 0-length because `key` could be
3108 * an invalid pointer which would make the behavior undefined. */
3109 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003110 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003111
Gilles Peskined223b522018-06-11 18:12:58 +02003112 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3113 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003114 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003115 ipad[i] ^= 0x36;
3116 memset( ipad + key_length, 0x36, block_size - key_length );
3117
3118 /* Copy the key material from ipad to opad, flipping the requisite bits,
3119 * and filling the rest of opad with the requisite constant. */
3120 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003121 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3122 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003123
Gilles Peskine01126fa2018-07-12 17:04:55 +02003124 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003125 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003126 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003127
Gilles Peskine01126fa2018-07-12 17:04:55 +02003128 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003129
3130cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003131 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003132
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003133 return( status );
3134}
John Durkop6ba40d12020-11-10 08:50:04 -08003135#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003136
Gilles Peskine89167cb2018-07-08 20:12:23 +02003137static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003138 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003139 psa_algorithm_t alg,
3140 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003141{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003142 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003143 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003144 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003145 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003146 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003147 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003148 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003149
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003150 /* A context must be freshly initialized before it can be set up. */
3151 if( operation->alg != 0 )
3152 {
3153 return( PSA_ERROR_BAD_STATE );
3154 }
3155
Gilles Peskined911eb72018-08-14 15:18:45 +02003156 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003157 if( status != PSA_SUCCESS )
3158 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003159 if( is_sign )
3160 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003161
Gilles Peskine28f8f302019-07-24 13:30:31 +02003162 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003163 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003164 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003165 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003166
Gilles Peskine8c9def32018-02-08 10:02:12 +01003167#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003168 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003169 {
3170 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003171 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003172 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003174 if( cipher_info == NULL )
3175 {
3176 status = PSA_ERROR_NOT_SUPPORTED;
3177 goto exit;
3178 }
3179 operation->mac_size = cipher_info->block_size;
3180 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3181 status = mbedtls_to_psa_error( ret );
3182 }
3183 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003184#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003185#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003186 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003187 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003188 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003189 if( hash_alg == 0 )
3190 {
3191 status = PSA_ERROR_NOT_SUPPORTED;
3192 goto exit;
3193 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003194
3195 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3196 /* Sanity check. This shouldn't fail on a valid configuration. */
3197 if( operation->mac_size == 0 ||
3198 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3199 {
3200 status = PSA_ERROR_NOT_SUPPORTED;
3201 goto exit;
3202 }
3203
Gilles Peskine8e338702019-07-30 20:06:31 +02003204 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003205 {
3206 status = PSA_ERROR_INVALID_ARGUMENT;
3207 goto exit;
3208 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003209
Gilles Peskine01126fa2018-07-12 17:04:55 +02003210 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003211 slot->data.key.data,
3212 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003213 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003214 }
3215 else
John Durkopd0321952020-10-29 21:37:36 -07003216#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003217 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003218 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003219 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003220 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003221
Gilles Peskined911eb72018-08-14 15:18:45 +02003222 if( truncated == 0 )
3223 {
3224 /* The "normal" case: untruncated algorithm. Nothing to do. */
3225 }
3226 else if( truncated < 4 )
3227 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003228 /* A very short MAC is too short for security since it can be
3229 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3230 * so we make this our minimum, even though 32 bits is still
3231 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003232 status = PSA_ERROR_NOT_SUPPORTED;
3233 }
3234 else if( truncated > operation->mac_size )
3235 {
3236 /* It's impossible to "truncate" to a larger length. */
3237 status = PSA_ERROR_INVALID_ARGUMENT;
3238 }
3239 else
3240 operation->mac_size = truncated;
3241
Gilles Peskinefbfac682018-07-08 20:51:54 +02003242exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003243 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003244 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003245 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003246 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003247 else
3248 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003249 operation->key_set = 1;
3250 }
3251 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003252}
3253
Gilles Peskine89167cb2018-07-08 20:12:23 +02003254psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003255 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003256 psa_algorithm_t alg )
3257{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003258 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003259}
3260
3261psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003262 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003263 psa_algorithm_t alg )
3264{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003265 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003266}
3267
Gilles Peskine8c9def32018-02-08 10:02:12 +01003268psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3269 const uint8_t *input,
3270 size_t input_length )
3271{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003272 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003273 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003274 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003275 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003276 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003277 operation->has_input = 1;
3278
Gilles Peskine8c9def32018-02-08 10:02:12 +01003279#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003280 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003281 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003282 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3283 input, input_length );
3284 status = mbedtls_to_psa_error( ret );
3285 }
3286 else
3287#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003288#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003289 if( PSA_ALG_IS_HMAC( operation->alg ) )
3290 {
3291 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3292 input_length );
3293 }
3294 else
John Durkopd0321952020-10-29 21:37:36 -07003295#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003296 {
3297 /* This shouldn't happen if `operation` was initialized by
3298 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003299 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003300 }
3301
Gilles Peskinefbfac682018-07-08 20:51:54 +02003302 if( status != PSA_SUCCESS )
3303 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003304 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003305}
3306
John Durkop6ba40d12020-11-10 08:50:04 -08003307#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003308static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3309 uint8_t *mac,
3310 size_t mac_size )
3311{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003312 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003313 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3314 size_t hash_size = 0;
3315 size_t block_size = psa_get_hash_block_size( hash_alg );
3316 psa_status_t status;
3317
Gilles Peskine01126fa2018-07-12 17:04:55 +02003318 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3319 if( status != PSA_SUCCESS )
3320 return( status );
3321 /* From here on, tmp needs to be wiped. */
3322
3323 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3324 if( status != PSA_SUCCESS )
3325 goto exit;
3326
3327 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3328 if( status != PSA_SUCCESS )
3329 goto exit;
3330
3331 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3332 if( status != PSA_SUCCESS )
3333 goto exit;
3334
Gilles Peskined911eb72018-08-14 15:18:45 +02003335 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3336 if( status != PSA_SUCCESS )
3337 goto exit;
3338
3339 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003340
3341exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003342 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003343 return( status );
3344}
John Durkop6ba40d12020-11-10 08:50:04 -08003345#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003346
mohammad16036df908f2018-04-02 08:34:15 -07003347static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003348 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003349 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003350{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003351 if( ! operation->key_set )
3352 return( PSA_ERROR_BAD_STATE );
3353 if( operation->iv_required && ! operation->iv_set )
3354 return( PSA_ERROR_BAD_STATE );
3355
Gilles Peskine8c9def32018-02-08 10:02:12 +01003356 if( mac_size < operation->mac_size )
3357 return( PSA_ERROR_BUFFER_TOO_SMALL );
3358
Gilles Peskine8c9def32018-02-08 10:02:12 +01003359#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003360 if( operation->alg == PSA_ALG_CMAC )
3361 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003362 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3363 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3364 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003365 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003366 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003367 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003368 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003369 else
3370#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003371#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003372 if( PSA_ALG_IS_HMAC( operation->alg ) )
3373 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003374 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003375 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003376 }
3377 else
John Durkopd0321952020-10-29 21:37:36 -07003378#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003379 {
3380 /* This shouldn't happen if `operation` was initialized by
3381 * a setup function. */
3382 return( PSA_ERROR_BAD_STATE );
3383 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003384}
3385
Gilles Peskineacd4be32018-07-08 19:56:25 +02003386psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3387 uint8_t *mac,
3388 size_t mac_size,
3389 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003390{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003391 psa_status_t status;
3392
Jaeden Amero252ef282019-02-15 14:05:35 +00003393 if( operation->alg == 0 )
3394 {
3395 return( PSA_ERROR_BAD_STATE );
3396 }
3397
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003398 /* Fill the output buffer with something that isn't a valid mac
3399 * (barring an attack on the mac and deliberately-crafted input),
3400 * in case the caller doesn't check the return status properly. */
3401 *mac_length = mac_size;
3402 /* If mac_size is 0 then mac may be NULL and then the
3403 * call to memset would have undefined behavior. */
3404 if( mac_size != 0 )
3405 memset( mac, '!', mac_size );
3406
Gilles Peskine89167cb2018-07-08 20:12:23 +02003407 if( ! operation->is_sign )
3408 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003409 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003410 }
mohammad16036df908f2018-04-02 08:34:15 -07003411
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003412 status = psa_mac_finish_internal( operation, mac, mac_size );
3413
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003414 if( status == PSA_SUCCESS )
3415 {
3416 status = psa_mac_abort( operation );
3417 if( status == PSA_SUCCESS )
3418 *mac_length = operation->mac_size;
3419 else
3420 memset( mac, '!', mac_size );
3421 }
3422 else
3423 psa_mac_abort( operation );
3424 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003425}
3426
Gilles Peskineacd4be32018-07-08 19:56:25 +02003427psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3428 const uint8_t *mac,
3429 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003430{
Gilles Peskine828ed142018-06-18 23:25:51 +02003431 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003432 psa_status_t status;
3433
Jaeden Amero252ef282019-02-15 14:05:35 +00003434 if( operation->alg == 0 )
3435 {
3436 return( PSA_ERROR_BAD_STATE );
3437 }
3438
Gilles Peskine89167cb2018-07-08 20:12:23 +02003439 if( operation->is_sign )
3440 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003441 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003442 }
3443 if( operation->mac_size != mac_length )
3444 {
3445 status = PSA_ERROR_INVALID_SIGNATURE;
3446 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003447 }
mohammad16036df908f2018-04-02 08:34:15 -07003448
3449 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003450 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003451 if( status != PSA_SUCCESS )
3452 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003453
3454 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3455 status = PSA_ERROR_INVALID_SIGNATURE;
3456
3457cleanup:
3458 if( status == PSA_SUCCESS )
3459 status = psa_mac_abort( operation );
3460 else
3461 psa_mac_abort( operation );
3462
Gilles Peskine3f108122018-12-07 18:14:53 +01003463 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003464
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003465 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003466}
3467
3468
Gilles Peskine20035e32018-02-03 22:44:14 +01003469
Gilles Peskine20035e32018-02-03 22:44:14 +01003470/****************************************************************/
3471/* Asymmetric cryptography */
3472/****************************************************************/
3473
John Durkop6ba40d12020-11-10 08:50:04 -08003474#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3475 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003476/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003477 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003478static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3479 size_t hash_length,
3480 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003481{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003482 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003483 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003484 *md_alg = mbedtls_md_get_type( md_info );
3485
3486 /* The Mbed TLS RSA module uses an unsigned int for hash length
3487 * parameters. Validate that it fits so that we don't risk an
3488 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003489#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003490 if( hash_length > UINT_MAX )
3491 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003492#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003493
John Durkop0e005192020-10-31 22:06:54 -07003494#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003495 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3496 * must be correct. */
3497 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3498 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003499 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003500 if( md_info == NULL )
3501 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003502 if( mbedtls_md_get_size( md_info ) != hash_length )
3503 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003504 }
John Durkop0e005192020-10-31 22:06:54 -07003505#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003506
John Durkop0e005192020-10-31 22:06:54 -07003507#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003508 /* PSS requires a hash internally. */
3509 if( PSA_ALG_IS_RSA_PSS( alg ) )
3510 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003511 if( md_info == NULL )
3512 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003513 }
John Durkop0e005192020-10-31 22:06:54 -07003514#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003515
Gilles Peskine61b91d42018-06-08 16:09:36 +02003516 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003517}
3518
Gilles Peskine2b450e32018-06-27 15:42:46 +02003519static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3520 psa_algorithm_t alg,
3521 const uint8_t *hash,
3522 size_t hash_length,
3523 uint8_t *signature,
3524 size_t signature_size,
3525 size_t *signature_length )
3526{
3527 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003529 mbedtls_md_type_t md_alg;
John Durkop0e005192020-10-31 22:06:54 -07003530#if !defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
3531 (void)hash;
3532 (void)signature;
3533#endif /* !MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003534
3535 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3536 if( status != PSA_SUCCESS )
3537 return( status );
3538
Gilles Peskine630a18a2018-06-29 17:49:35 +02003539 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003540 return( PSA_ERROR_BUFFER_TOO_SMALL );
3541
John Durkop0e005192020-10-31 22:06:54 -07003542#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003543 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3544 {
3545 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3546 MBEDTLS_MD_NONE );
3547 ret = mbedtls_rsa_pkcs1_sign( rsa,
3548 mbedtls_ctr_drbg_random,
3549 &global_data.ctr_drbg,
3550 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003551 md_alg,
3552 (unsigned int) hash_length,
3553 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003554 signature );
3555 }
3556 else
John Durkop0e005192020-10-31 22:06:54 -07003557#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3558#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003559 if( PSA_ALG_IS_RSA_PSS( alg ) )
3560 {
3561 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3562 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3563 mbedtls_ctr_drbg_random,
3564 &global_data.ctr_drbg,
3565 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003566 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003567 (unsigned int) hash_length,
3568 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003569 signature );
3570 }
3571 else
John Durkop0e005192020-10-31 22:06:54 -07003572#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003573 {
3574 return( PSA_ERROR_INVALID_ARGUMENT );
3575 }
3576
3577 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003578 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003579 return( mbedtls_to_psa_error( ret ) );
3580}
3581
3582static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3583 psa_algorithm_t alg,
3584 const uint8_t *hash,
3585 size_t hash_length,
3586 const uint8_t *signature,
3587 size_t signature_length )
3588{
3589 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003591 mbedtls_md_type_t md_alg;
John Durkop0e005192020-10-31 22:06:54 -07003592#if !defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
3593 (void)hash;
3594 (void)signature;
3595#endif /* !MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003596
3597 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3598 if( status != PSA_SUCCESS )
3599 return( status );
3600
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003601 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3602 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003603
John Durkop0e005192020-10-31 22:06:54 -07003604#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003605 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3606 {
3607 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3608 MBEDTLS_MD_NONE );
3609 ret = mbedtls_rsa_pkcs1_verify( rsa,
3610 mbedtls_ctr_drbg_random,
3611 &global_data.ctr_drbg,
3612 MBEDTLS_RSA_PUBLIC,
3613 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003614 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003615 hash,
3616 signature );
3617 }
3618 else
John Durkop0e005192020-10-31 22:06:54 -07003619#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3620#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003621 if( PSA_ALG_IS_RSA_PSS( alg ) )
3622 {
3623 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3624 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3625 mbedtls_ctr_drbg_random,
3626 &global_data.ctr_drbg,
3627 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003628 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003629 (unsigned int) hash_length,
3630 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003631 signature );
3632 }
3633 else
John Durkop0e005192020-10-31 22:06:54 -07003634#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003635 {
3636 return( PSA_ERROR_INVALID_ARGUMENT );
3637 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003638
3639 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3640 * the rest of the signature is invalid". This has little use in
3641 * practice and PSA doesn't report this distinction. */
3642 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3643 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003644 return( mbedtls_to_psa_error( ret ) );
3645}
John Durkop6ba40d12020-11-10 08:50:04 -08003646#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3647 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003648
John Durkop6ba40d12020-11-10 08:50:04 -08003649#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3650 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003651/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3652 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3653 * (even though these functions don't modify it). */
3654static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3655 psa_algorithm_t alg,
3656 const uint8_t *hash,
3657 size_t hash_length,
3658 uint8_t *signature,
3659 size_t signature_size,
3660 size_t *signature_length )
3661{
Janos Follath24eed8d2019-11-22 13:21:35 +00003662 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003663 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003664 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003665 mbedtls_mpi_init( &r );
3666 mbedtls_mpi_init( &s );
3667
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003668 if( signature_size < 2 * curve_bytes )
3669 {
3670 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3671 goto cleanup;
3672 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003673
John Durkop0ea39e02020-10-13 19:58:20 -07003674#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003675 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3676 {
3677 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3678 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3679 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003680 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3681 &ecp->d, hash,
3682 hash_length, md_alg,
3683 mbedtls_ctr_drbg_random,
3684 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003685 }
3686 else
John Durkop0ea39e02020-10-13 19:58:20 -07003687#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003688 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003689 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003690 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3691 hash, hash_length,
3692 mbedtls_ctr_drbg_random,
3693 &global_data.ctr_drbg ) );
3694 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003695
3696 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3697 signature,
3698 curve_bytes ) );
3699 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3700 signature + curve_bytes,
3701 curve_bytes ) );
3702
3703cleanup:
3704 mbedtls_mpi_free( &r );
3705 mbedtls_mpi_free( &s );
3706 if( ret == 0 )
3707 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003708 return( mbedtls_to_psa_error( ret ) );
3709}
3710
3711static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3712 const uint8_t *hash,
3713 size_t hash_length,
3714 const uint8_t *signature,
3715 size_t signature_length )
3716{
Janos Follath24eed8d2019-11-22 13:21:35 +00003717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003718 mbedtls_mpi r, s;
3719 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3720 mbedtls_mpi_init( &r );
3721 mbedtls_mpi_init( &s );
3722
3723 if( signature_length != 2 * curve_bytes )
3724 return( PSA_ERROR_INVALID_SIGNATURE );
3725
3726 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3727 signature,
3728 curve_bytes ) );
3729 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3730 signature + curve_bytes,
3731 curve_bytes ) );
3732
Steven Cooremanacda8342020-07-24 23:09:52 +02003733 /* Check whether the public part is loaded. If not, load it. */
3734 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3735 {
3736 MBEDTLS_MPI_CHK(
3737 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3738 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3739 }
3740
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003741 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3742 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003743
3744cleanup:
3745 mbedtls_mpi_free( &r );
3746 mbedtls_mpi_free( &s );
3747 return( mbedtls_to_psa_error( ret ) );
3748}
John Durkop6ba40d12020-11-10 08:50:04 -08003749#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3750 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003751
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003752psa_status_t psa_sign_hash( psa_key_handle_t handle,
3753 psa_algorithm_t alg,
3754 const uint8_t *hash,
3755 size_t hash_length,
3756 uint8_t *signature,
3757 size_t signature_size,
3758 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003759{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003760 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003761 psa_status_t status;
3762
3763 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003764 /* Immediately reject a zero-length signature buffer. This guarantees
3765 * that signature must be a valid pointer. (On the other hand, the hash
3766 * buffer can in principle be empty since it doesn't actually have
3767 * to be a hash.) */
3768 if( signature_size == 0 )
3769 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003770
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003771 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003772 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003773 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003774 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003775 {
3776 status = PSA_ERROR_INVALID_ARGUMENT;
3777 goto exit;
3778 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003779
Steven Cooremancd84cb42020-07-16 20:28:36 +02003780 /* Try any of the available accelerators first */
3781 status = psa_driver_wrapper_sign_hash( slot,
3782 alg,
3783 hash,
3784 hash_length,
3785 signature,
3786 signature_size,
3787 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003788 if( status != PSA_ERROR_NOT_SUPPORTED ||
3789 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003790 goto exit;
3791
Steven Cooreman7a250572020-07-17 16:43:05 +02003792 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003793#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3794 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003795 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003796 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003797 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003798
Steven Cooreman4fed4552020-08-03 14:46:03 +02003799 status = psa_load_rsa_representation( slot->attr.type,
3800 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003801 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003802 &rsa );
3803 if( status != PSA_SUCCESS )
3804 goto exit;
3805
Steven Cooremana2371e52020-07-28 14:30:39 +02003806 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003807 alg,
3808 hash, hash_length,
3809 signature, signature_size,
3810 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003811
Steven Cooremana2371e52020-07-28 14:30:39 +02003812 mbedtls_rsa_free( rsa );
3813 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003814 }
3815 else
John Durkop6ba40d12020-11-10 08:50:04 -08003816#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3817 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003818 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003819 {
John Durkop9814fa22020-11-04 12:28:15 -08003820#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3821 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003822 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003823#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003824 PSA_ALG_IS_ECDSA( alg )
3825#else
3826 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3827#endif
3828 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003829 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003830 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003831 status = psa_load_ecp_representation( slot->attr.type,
3832 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003833 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003834 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003835 if( status != PSA_SUCCESS )
3836 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003837 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003838 alg,
3839 hash, hash_length,
3840 signature, signature_size,
3841 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003842 mbedtls_ecp_keypair_free( ecp );
3843 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003844 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003845 else
John Durkop9814fa22020-11-04 12:28:15 -08003846#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3847 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003848 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003849 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003850 }
itayzafrir5c753392018-05-08 11:18:38 +03003851 }
3852 else
itayzafrir5c753392018-05-08 11:18:38 +03003853 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003854 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003855 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003856
3857exit:
3858 /* Fill the unused part of the output buffer (the whole buffer on error,
3859 * the trailing part on success) with something that isn't a valid mac
3860 * (barring an attack on the mac and deliberately-crafted input),
3861 * in case the caller doesn't check the return status properly. */
3862 if( status == PSA_SUCCESS )
3863 memset( signature + *signature_length, '!',
3864 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003865 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003866 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003867 /* If signature_size is 0 then we have nothing to do. We must not call
3868 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003869 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003870}
3871
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003872psa_status_t psa_verify_hash( psa_key_handle_t handle,
3873 psa_algorithm_t alg,
3874 const uint8_t *hash,
3875 size_t hash_length,
3876 const uint8_t *signature,
3877 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003878{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003879 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003880 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003881
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003882 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003883 if( status != PSA_SUCCESS )
3884 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003885
Steven Cooreman55ae2172020-07-17 19:46:15 +02003886 /* Try any of the available accelerators first */
3887 status = psa_driver_wrapper_verify_hash( slot,
3888 alg,
3889 hash,
3890 hash_length,
3891 signature,
3892 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003893 if( status != PSA_ERROR_NOT_SUPPORTED ||
3894 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +02003895 return status;
3896
John Durkop6ba40d12020-11-10 08:50:04 -08003897#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3898 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003899 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003900 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003901 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003902
Steven Cooreman4fed4552020-08-03 14:46:03 +02003903 status = psa_load_rsa_representation( slot->attr.type,
3904 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003905 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003906 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003907 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003908 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02003909
Steven Cooremana2371e52020-07-28 14:30:39 +02003910 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003911 alg,
3912 hash, hash_length,
3913 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003914 mbedtls_rsa_free( rsa );
3915 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003916 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003917 }
3918 else
John Durkop6ba40d12020-11-10 08:50:04 -08003919#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3920 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003921 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003922 {
John Durkop9814fa22020-11-04 12:28:15 -08003923#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3924 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003925 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003926 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003927 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003928 status = psa_load_ecp_representation( slot->attr.type,
3929 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003930 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003931 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003932 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02003933 return( status );
Steven Cooremana2371e52020-07-28 14:30:39 +02003934 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003935 hash, hash_length,
3936 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003937 mbedtls_ecp_keypair_free( ecp );
3938 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02003939 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02003940 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003941 else
John Durkop9814fa22020-11-04 12:28:15 -08003942#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3943 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003944 {
3945 return( PSA_ERROR_INVALID_ARGUMENT );
3946 }
itayzafrir5c753392018-05-08 11:18:38 +03003947 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003948 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003949 {
3950 return( PSA_ERROR_NOT_SUPPORTED );
3951 }
3952}
3953
John Durkop0e005192020-10-31 22:06:54 -07003954#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003955static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3956 mbedtls_rsa_context *rsa )
3957{
3958 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3959 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3960 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3961 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3962}
John Durkop0e005192020-10-31 22:06:54 -07003963#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003964
Gilles Peskinec5487a82018-12-03 18:08:14 +01003965psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003966 psa_algorithm_t alg,
3967 const uint8_t *input,
3968 size_t input_length,
3969 const uint8_t *salt,
3970 size_t salt_length,
3971 uint8_t *output,
3972 size_t output_size,
3973 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003974{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003975 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003976 psa_status_t status;
3977
Darryl Green5cc689a2018-07-24 15:34:10 +01003978 (void) input;
3979 (void) input_length;
3980 (void) salt;
3981 (void) output;
3982 (void) output_size;
3983
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003984 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003985
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003986 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3987 return( PSA_ERROR_INVALID_ARGUMENT );
3988
Gilles Peskine28f8f302019-07-24 13:30:31 +02003989 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003990 if( status != PSA_SUCCESS )
3991 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003992 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3993 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003994 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003995
John Durkop6ba40d12020-11-10 08:50:04 -08003996#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3997 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003998 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003999 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004000 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004001 status = psa_load_rsa_representation( slot->attr.type,
4002 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004003 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004004 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004005 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004006 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004007
4008 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004009 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004010 status = PSA_ERROR_BUFFER_TOO_SMALL;
4011 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004012 }
John Durkop0e005192020-10-31 22:06:54 -07004013#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004014 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004015 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004016 status = mbedtls_to_psa_error(
4017 mbedtls_rsa_pkcs1_encrypt( rsa,
4018 mbedtls_ctr_drbg_random,
4019 &global_data.ctr_drbg,
4020 MBEDTLS_RSA_PUBLIC,
4021 input_length,
4022 input,
4023 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004024 }
4025 else
John Durkop0e005192020-10-31 22:06:54 -07004026#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4027#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004028 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004029 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004030 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004031 status = mbedtls_to_psa_error(
4032 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
4033 mbedtls_ctr_drbg_random,
4034 &global_data.ctr_drbg,
4035 MBEDTLS_RSA_PUBLIC,
4036 salt, salt_length,
4037 input_length,
4038 input,
4039 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004040 }
4041 else
John Durkop0e005192020-10-31 22:06:54 -07004042#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004043 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004044 status = PSA_ERROR_INVALID_ARGUMENT;
4045 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004046 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004047rsa_exit:
4048 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004049 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004050
Steven Cooremana2371e52020-07-28 14:30:39 +02004051 mbedtls_rsa_free( rsa );
4052 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004053 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004054 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004055 else
John Durkop9814fa22020-11-04 12:28:15 -08004056#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004057 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004058 {
4059 return( PSA_ERROR_NOT_SUPPORTED );
4060 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004061}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004062
Gilles Peskinec5487a82018-12-03 18:08:14 +01004063psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004064 psa_algorithm_t alg,
4065 const uint8_t *input,
4066 size_t input_length,
4067 const uint8_t *salt,
4068 size_t salt_length,
4069 uint8_t *output,
4070 size_t output_size,
4071 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004072{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004073 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004074 psa_status_t status;
4075
Darryl Green5cc689a2018-07-24 15:34:10 +01004076 (void) input;
4077 (void) input_length;
4078 (void) salt;
4079 (void) output;
4080 (void) output_size;
4081
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004082 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004083
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004084 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4085 return( PSA_ERROR_INVALID_ARGUMENT );
4086
Gilles Peskine28f8f302019-07-24 13:30:31 +02004087 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004088 if( status != PSA_SUCCESS )
4089 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004090 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004091 return( PSA_ERROR_INVALID_ARGUMENT );
4092
John Durkop6ba40d12020-11-10 08:50:04 -08004093#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4094 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004095 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004096 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004097 mbedtls_rsa_context *rsa = NULL;
4098 status = psa_load_rsa_representation( slot->attr.type,
4099 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004100 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004101 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004102 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004103 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004104
Steven Cooremana2371e52020-07-28 14:30:39 +02004105 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004106 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004107 status = PSA_ERROR_INVALID_ARGUMENT;
4108 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004109 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004110
John Durkop0e005192020-10-31 22:06:54 -07004111#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004112 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004113 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004114 status = mbedtls_to_psa_error(
4115 mbedtls_rsa_pkcs1_decrypt( rsa,
4116 mbedtls_ctr_drbg_random,
4117 &global_data.ctr_drbg,
4118 MBEDTLS_RSA_PRIVATE,
4119 output_length,
4120 input,
4121 output,
4122 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004123 }
4124 else
John Durkop0e005192020-10-31 22:06:54 -07004125#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4126#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004127 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004128 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004129 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004130 status = mbedtls_to_psa_error(
4131 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
4132 mbedtls_ctr_drbg_random,
4133 &global_data.ctr_drbg,
4134 MBEDTLS_RSA_PRIVATE,
4135 salt, salt_length,
4136 output_length,
4137 input,
4138 output,
4139 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004140 }
4141 else
John Durkop0e005192020-10-31 22:06:54 -07004142#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004143 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004144 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004145 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004146
Steven Cooreman4fed4552020-08-03 14:46:03 +02004147rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004148 mbedtls_rsa_free( rsa );
4149 mbedtls_free( rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004150 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004151 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004152 else
John Durkop6ba40d12020-11-10 08:50:04 -08004153#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4154 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004155 {
4156 return( PSA_ERROR_NOT_SUPPORTED );
4157 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004158}
Gilles Peskine20035e32018-02-03 22:44:14 +01004159
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004160
4161
mohammad1603503973b2018-03-12 15:59:30 +02004162/****************************************************************/
4163/* Symmetric cryptography */
4164/****************************************************************/
4165
Gilles Peskinee553c652018-06-04 16:22:46 +02004166static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004167 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004168 psa_algorithm_t alg,
4169 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004170{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004171 int ret = 0;
4172 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004173 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004174 size_t key_bits;
4175 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004176 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4177 PSA_KEY_USAGE_ENCRYPT :
4178 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004179
Steven Cooremand3feccd2020-09-01 15:56:14 +02004180 /* A context must be freshly initialized before it can be set up. */
4181 if( operation->alg != 0 )
4182 return( PSA_ERROR_BAD_STATE );
4183
Steven Cooremana07b9972020-09-10 14:54:14 +02004184 /* The requested algorithm must be one that can be processed by cipher. */
4185 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004186 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004187
Steven Cooremanef8575e2020-09-11 11:44:50 +02004188 /* Fetch key material from key storage. */
4189 status = psa_get_key_from_slot( handle, &slot, usage, alg );
4190 if( status != PSA_SUCCESS )
4191 goto exit;
4192
4193 /* Initialize the operation struct members, except for alg. The alg member
4194 * is used to indicate to psa_cipher_abort that there are resources to free,
4195 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004196 operation->key_set = 0;
4197 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004198 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004199 operation->iv_size = 0;
4200 operation->block_size = 0;
4201 if( alg == PSA_ALG_ECB_NO_PADDING )
4202 operation->iv_required = 0;
4203 else
4204 operation->iv_required = 1;
4205
Steven Cooremana07b9972020-09-10 14:54:14 +02004206 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004207 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004208 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004209 slot,
4210 alg );
4211 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004212 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004213 slot,
4214 alg );
4215
Steven Cooremanef8575e2020-09-11 11:44:50 +02004216 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004217 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004218 /* Once the driver context is initialised, it needs to be freed using
4219 * psa_cipher_abort. Indicate this through setting alg. */
4220 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004221 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004222
Steven Cooremand3feccd2020-09-01 15:56:14 +02004223 if( status != PSA_ERROR_NOT_SUPPORTED ||
4224 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4225 goto exit;
4226
Steven Cooremana07b9972020-09-10 14:54:14 +02004227 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004228 * available for the given algorithm & key. */
4229 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004230
Steven Cooremana07b9972020-09-10 14:54:14 +02004231 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004232 * psa_cipher_abort. Indicate there is something to be freed through setting
4233 * alg, and indicate the operation is being done using mbedtls crypto through
4234 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004235 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004236 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004237
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004238 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004239 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004240 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004241 {
4242 status = PSA_ERROR_NOT_SUPPORTED;
4243 goto exit;
4244 }
mohammad1603503973b2018-03-12 15:59:30 +02004245
mohammad1603503973b2018-03-12 15:59:30 +02004246 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004247 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004248 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004249
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004250#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004251 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004252 {
4253 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004254 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004255 memcpy( keys, slot->data.key.data, 16 );
4256 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004257 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4258 keys,
4259 192, cipher_operation );
4260 }
4261 else
4262#endif
4263 {
4264 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004265 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004266 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004267 }
Moran Peker41deec42018-04-04 15:43:05 +03004268 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004269 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004270
mohammad16038481e742018-03-18 13:57:31 +02004271#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004272 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004273 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004274 case PSA_ALG_CBC_NO_PADDING:
4275 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4276 MBEDTLS_PADDING_NONE );
4277 break;
4278 case PSA_ALG_CBC_PKCS7:
4279 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4280 MBEDTLS_PADDING_PKCS7 );
4281 break;
4282 default:
4283 /* The algorithm doesn't involve padding. */
4284 ret = 0;
4285 break;
4286 }
4287 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004288 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004289#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4290
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004291 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004292 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004293 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4294 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004295 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004296 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004297 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004298#if defined(MBEDTLS_CHACHA20_C)
4299 else
4300 if( alg == PSA_ALG_CHACHA20 )
4301 operation->iv_size = 12;
4302#endif
mohammad1603503973b2018-03-12 15:59:30 +02004303
Steven Cooreman7df02922020-09-09 15:28:49 +02004304 status = PSA_SUCCESS;
4305
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004306exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004307 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004308 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004309 if( status == PSA_SUCCESS )
4310 {
4311 /* Update operation flags for both driver and software implementations */
4312 operation->key_set = 1;
4313 }
4314 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004315 psa_cipher_abort( operation );
4316 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004317}
4318
Gilles Peskinefe119512018-07-08 21:39:34 +02004319psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004320 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004321 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004322{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004323 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004324}
4325
Gilles Peskinefe119512018-07-08 21:39:34 +02004326psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004327 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004328 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004329{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004330 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004331}
4332
Gilles Peskinefe119512018-07-08 21:39:34 +02004333psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004334 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004335 size_t iv_size,
4336 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004337{
itayzafrir534bd7c2018-08-02 13:56:32 +03004338 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004339 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004340 if( operation->iv_set || ! operation->iv_required )
4341 {
4342 return( PSA_ERROR_BAD_STATE );
4343 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004344
Steven Cooremanef8575e2020-09-11 11:44:50 +02004345 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004346 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004347 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004348 iv,
4349 iv_size,
4350 iv_length );
4351 goto exit;
4352 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004353
Moran Peker41deec42018-04-04 15:43:05 +03004354 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004355 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004356 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004357 goto exit;
4358 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004359 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4360 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004361 if( ret != 0 )
4362 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004363 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004364 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004365 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004366
mohammad16038481e742018-03-18 13:57:31 +02004367 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004368 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004369
Moran Peker395db872018-05-31 14:07:14 +03004370exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004371 if( status == PSA_SUCCESS )
4372 operation->iv_set = 1;
4373 else
Moran Peker395db872018-05-31 14:07:14 +03004374 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004375 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004376}
4377
Gilles Peskinefe119512018-07-08 21:39:34 +02004378psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004379 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004380 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004381{
itayzafrir534bd7c2018-08-02 13:56:32 +03004382 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004383 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004384 if( operation->iv_set || ! operation->iv_required )
4385 {
4386 return( PSA_ERROR_BAD_STATE );
4387 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004388
Steven Cooremanef8575e2020-09-11 11:44:50 +02004389 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004390 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004391 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004392 iv,
4393 iv_length );
4394 goto exit;
4395 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004396
Moran Pekera28258c2018-05-29 16:25:04 +03004397 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004398 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004399 status = PSA_ERROR_INVALID_ARGUMENT;
4400 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004401 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004402 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4403 status = mbedtls_to_psa_error( ret );
4404exit:
4405 if( status == PSA_SUCCESS )
4406 operation->iv_set = 1;
4407 else
mohammad1603503973b2018-03-12 15:59:30 +02004408 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004409 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004410}
4411
Steven Cooreman177deba2020-09-07 17:14:14 +02004412/* Process input for which the algorithm is set to ECB mode. This requires
4413 * manual processing, since the PSA API is defined as being able to process
4414 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4415 * underlying mbedtls_cipher_update only takes full blocks. */
4416static psa_status_t psa_cipher_update_ecb_internal(
4417 mbedtls_cipher_context_t *ctx,
4418 const uint8_t *input,
4419 size_t input_length,
4420 uint8_t *output,
4421 size_t output_size,
4422 size_t *output_length )
4423{
4424 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4425 size_t block_size = ctx->cipher_info->block_size;
4426 size_t internal_output_length = 0;
4427 *output_length = 0;
4428
4429 if( input_length == 0 )
4430 {
4431 status = PSA_SUCCESS;
4432 goto exit;
4433 }
4434
4435 if( ctx->unprocessed_len > 0 )
4436 {
4437 /* Fill up to block size, and run the block if there's a full one. */
4438 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4439
4440 if( input_length < bytes_to_copy )
4441 bytes_to_copy = input_length;
4442
4443 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4444 input, bytes_to_copy );
4445 input_length -= bytes_to_copy;
4446 input += bytes_to_copy;
4447 ctx->unprocessed_len += bytes_to_copy;
4448
4449 if( ctx->unprocessed_len == block_size )
4450 {
4451 status = mbedtls_to_psa_error(
4452 mbedtls_cipher_update( ctx,
4453 ctx->unprocessed_data,
4454 block_size,
4455 output, &internal_output_length ) );
4456
4457 if( status != PSA_SUCCESS )
4458 goto exit;
4459
4460 output += internal_output_length;
4461 output_size -= internal_output_length;
4462 *output_length += internal_output_length;
4463 ctx->unprocessed_len = 0;
4464 }
4465 }
4466
4467 while( input_length >= block_size )
4468 {
4469 /* Run all full blocks we have, one by one */
4470 status = mbedtls_to_psa_error(
4471 mbedtls_cipher_update( ctx, input,
4472 block_size,
4473 output, &internal_output_length ) );
4474
4475 if( status != PSA_SUCCESS )
4476 goto exit;
4477
4478 input_length -= block_size;
4479 input += block_size;
4480
4481 output += internal_output_length;
4482 output_size -= internal_output_length;
4483 *output_length += internal_output_length;
4484 }
4485
4486 if( input_length > 0 )
4487 {
4488 /* Save unprocessed bytes for later processing */
4489 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4490 input, input_length );
4491 ctx->unprocessed_len += input_length;
4492 }
4493
4494 status = PSA_SUCCESS;
4495
4496exit:
4497 return( status );
4498}
4499
Gilles Peskinee553c652018-06-04 16:22:46 +02004500psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4501 const uint8_t *input,
4502 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004503 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004504 size_t output_size,
4505 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004506{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004507 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004508 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004509 if( operation->alg == 0 )
4510 {
4511 return( PSA_ERROR_BAD_STATE );
4512 }
4513 if( operation->iv_required && ! operation->iv_set )
4514 {
4515 return( PSA_ERROR_BAD_STATE );
4516 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004517
Steven Cooremanef8575e2020-09-11 11:44:50 +02004518 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004519 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004520 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004521 input,
4522 input_length,
4523 output,
4524 output_size,
4525 output_length );
4526 goto exit;
4527 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004528
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004529 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004530 {
4531 /* Take the unprocessed partial block left over from previous
4532 * update calls, if any, plus the input to this call. Remove
4533 * the last partial block, if any. You get the data that will be
4534 * output in this call. */
4535 expected_output_size =
4536 ( operation->ctx.cipher.unprocessed_len + input_length )
4537 / operation->block_size * operation->block_size;
4538 }
4539 else
4540 {
4541 expected_output_size = input_length;
4542 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004543
Gilles Peskine89d789c2018-06-04 17:17:16 +02004544 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004545 {
4546 status = PSA_ERROR_BUFFER_TOO_SMALL;
4547 goto exit;
4548 }
mohammad160382759612018-03-12 18:16:40 +02004549
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004550 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4551 {
4552 /* mbedtls_cipher_update has an API inconsistency: it will only
4553 * process a single block at a time in ECB mode. Abstract away that
4554 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004555 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4556 input,
4557 input_length,
4558 output,
4559 output_size,
4560 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004561 }
4562 else
4563 {
4564 status = mbedtls_to_psa_error(
4565 mbedtls_cipher_update( &operation->ctx.cipher, input,
4566 input_length, output, output_length ) );
4567 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004568exit:
4569 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004570 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004571 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004572}
4573
Gilles Peskinee553c652018-06-04 16:22:46 +02004574psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4575 uint8_t *output,
4576 size_t output_size,
4577 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004578{
David Saadab4ecc272019-02-14 13:48:10 +02004579 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004580 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004581 if( operation->alg == 0 )
4582 {
4583 return( PSA_ERROR_BAD_STATE );
4584 }
4585 if( operation->iv_required && ! operation->iv_set )
4586 {
4587 return( PSA_ERROR_BAD_STATE );
4588 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004589
Steven Cooremanef8575e2020-09-11 11:44:50 +02004590 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004591 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004592 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004593 output,
4594 output_size,
4595 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004596 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004597 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004598
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004599 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004600 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004601 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004602 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004603 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004604 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004605 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004606 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004607 }
4608
Steven Cooremanef8575e2020-09-11 11:44:50 +02004609 status = mbedtls_to_psa_error(
4610 mbedtls_cipher_finish( &operation->ctx.cipher,
4611 temp_output_buffer,
4612 output_length ) );
4613 if( status != PSA_SUCCESS )
4614 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004615
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004616 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004617 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004618 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004619 memcpy( output, temp_output_buffer, *output_length );
4620 else
Janos Follath315b51c2018-07-09 16:04:51 +01004621 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004622
Steven Cooremanef8575e2020-09-11 11:44:50 +02004623exit:
4624 if( operation->mbedtls_in_use == 1 )
4625 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004626
Steven Cooremanef8575e2020-09-11 11:44:50 +02004627 if( status == PSA_SUCCESS )
4628 return( psa_cipher_abort( operation ) );
4629 else
4630 {
4631 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004632 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004633
Steven Cooremanef8575e2020-09-11 11:44:50 +02004634 return( status );
4635 }
mohammad1603503973b2018-03-12 15:59:30 +02004636}
4637
Gilles Peskinee553c652018-06-04 16:22:46 +02004638psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4639{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004640 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004641 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004642 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004643 * in use. It's ok to call abort on such an object, and there's
4644 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004645 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004646 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004647
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004648 /* Sanity check (shouldn't happen: operation->alg should
4649 * always have been initialized to a valid value). */
4650 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4651 return( PSA_ERROR_BAD_STATE );
4652
Steven Cooremanef8575e2020-09-11 11:44:50 +02004653 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004654 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004655 else
4656 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004657
Moran Peker41deec42018-04-04 15:43:05 +03004658 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004659 operation->key_set = 0;
4660 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004661 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004662 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004663 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004664 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004665
Moran Peker395db872018-05-31 14:07:14 +03004666 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004667}
4668
Gilles Peskinea0655c32018-04-30 17:06:50 +02004669
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004670
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004671
mohammad16035955c982018-04-26 00:53:03 +03004672/****************************************************************/
4673/* AEAD */
4674/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004675
Gilles Peskineedf9a652018-08-17 18:11:56 +02004676typedef struct
4677{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004678 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004679 const mbedtls_cipher_info_t *cipher_info;
4680 union
4681 {
4682#if defined(MBEDTLS_CCM_C)
4683 mbedtls_ccm_context ccm;
4684#endif /* MBEDTLS_CCM_C */
4685#if defined(MBEDTLS_GCM_C)
4686 mbedtls_gcm_context gcm;
4687#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004688#if defined(MBEDTLS_CHACHAPOLY_C)
4689 mbedtls_chachapoly_context chachapoly;
4690#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004691 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004692 psa_algorithm_t core_alg;
4693 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004694 uint8_t tag_length;
4695} aead_operation_t;
4696
Gilles Peskine30a9e412019-01-14 18:36:12 +01004697static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004698{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004699 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004700 {
4701#if defined(MBEDTLS_CCM_C)
4702 case PSA_ALG_CCM:
4703 mbedtls_ccm_free( &operation->ctx.ccm );
4704 break;
4705#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004706#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004707 case PSA_ALG_GCM:
4708 mbedtls_gcm_free( &operation->ctx.gcm );
4709 break;
4710#endif /* MBEDTLS_GCM_C */
4711 }
4712}
4713
4714static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004715 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004716 psa_key_usage_t usage,
4717 psa_algorithm_t alg )
4718{
4719 psa_status_t status;
4720 size_t key_bits;
4721 mbedtls_cipher_id_t cipher_id;
4722
Gilles Peskine28f8f302019-07-24 13:30:31 +02004723 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004724 if( status != PSA_SUCCESS )
4725 return( status );
4726
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004727 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004728
4729 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004730 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004731 &cipher_id );
4732 if( operation->cipher_info == NULL )
4733 return( PSA_ERROR_NOT_SUPPORTED );
4734
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004735 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004736 {
4737#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004738 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4739 operation->core_alg = PSA_ALG_CCM;
4740 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004741 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4742 * The call to mbedtls_ccm_encrypt_and_tag or
4743 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004744 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004745 return( PSA_ERROR_INVALID_ARGUMENT );
4746 mbedtls_ccm_init( &operation->ctx.ccm );
4747 status = mbedtls_to_psa_error(
4748 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004749 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004750 (unsigned int) key_bits ) );
4751 if( status != 0 )
4752 goto cleanup;
4753 break;
4754#endif /* MBEDTLS_CCM_C */
4755
4756#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004757 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4758 operation->core_alg = PSA_ALG_GCM;
4759 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004760 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4761 * The call to mbedtls_gcm_crypt_and_tag or
4762 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004763 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004764 return( PSA_ERROR_INVALID_ARGUMENT );
4765 mbedtls_gcm_init( &operation->ctx.gcm );
4766 status = mbedtls_to_psa_error(
4767 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004768 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004769 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004770 if( status != 0 )
4771 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004772 break;
4773#endif /* MBEDTLS_GCM_C */
4774
Gilles Peskine26869f22019-05-06 15:25:00 +02004775#if defined(MBEDTLS_CHACHAPOLY_C)
4776 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4777 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4778 operation->full_tag_length = 16;
4779 /* We only support the default tag length. */
4780 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4781 return( PSA_ERROR_NOT_SUPPORTED );
4782 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4783 status = mbedtls_to_psa_error(
4784 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004785 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004786 if( status != 0 )
4787 goto cleanup;
4788 break;
4789#endif /* MBEDTLS_CHACHAPOLY_C */
4790
Gilles Peskineedf9a652018-08-17 18:11:56 +02004791 default:
4792 return( PSA_ERROR_NOT_SUPPORTED );
4793 }
4794
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004795 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4796 {
4797 status = PSA_ERROR_INVALID_ARGUMENT;
4798 goto cleanup;
4799 }
4800 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004801
Gilles Peskineedf9a652018-08-17 18:11:56 +02004802 return( PSA_SUCCESS );
4803
4804cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004805 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004806 return( status );
4807}
4808
Gilles Peskinec5487a82018-12-03 18:08:14 +01004809psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004810 psa_algorithm_t alg,
4811 const uint8_t *nonce,
4812 size_t nonce_length,
4813 const uint8_t *additional_data,
4814 size_t additional_data_length,
4815 const uint8_t *plaintext,
4816 size_t plaintext_length,
4817 uint8_t *ciphertext,
4818 size_t ciphertext_size,
4819 size_t *ciphertext_length )
4820{
mohammad16035955c982018-04-26 00:53:03 +03004821 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004822 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004823 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004824
mohammad1603f08a5502018-06-03 15:05:47 +03004825 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004826
Gilles Peskinec5487a82018-12-03 18:08:14 +01004827 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004828 if( status != PSA_SUCCESS )
4829 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004830
Gilles Peskineedf9a652018-08-17 18:11:56 +02004831 /* For all currently supported modes, the tag is at the end of the
4832 * ciphertext. */
4833 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4834 {
4835 status = PSA_ERROR_BUFFER_TOO_SMALL;
4836 goto exit;
4837 }
4838 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004839
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004840#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004841 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004842 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004843 status = mbedtls_to_psa_error(
4844 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4845 MBEDTLS_GCM_ENCRYPT,
4846 plaintext_length,
4847 nonce, nonce_length,
4848 additional_data, additional_data_length,
4849 plaintext, ciphertext,
4850 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004851 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004852 else
4853#endif /* MBEDTLS_GCM_C */
4854#if defined(MBEDTLS_CCM_C)
4855 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004856 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004857 status = mbedtls_to_psa_error(
4858 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4859 plaintext_length,
4860 nonce, nonce_length,
4861 additional_data,
4862 additional_data_length,
4863 plaintext, ciphertext,
4864 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004865 }
mohammad16035c8845f2018-05-09 05:40:09 -07004866 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004867#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004868#if defined(MBEDTLS_CHACHAPOLY_C)
4869 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4870 {
4871 if( nonce_length != 12 || operation.tag_length != 16 )
4872 {
4873 status = PSA_ERROR_NOT_SUPPORTED;
4874 goto exit;
4875 }
4876 status = mbedtls_to_psa_error(
4877 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4878 plaintext_length,
4879 nonce,
4880 additional_data,
4881 additional_data_length,
4882 plaintext,
4883 ciphertext,
4884 tag ) );
4885 }
4886 else
4887#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004888 {
mohammad1603554faad2018-06-03 15:07:38 +03004889 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004890 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004891
Gilles Peskineedf9a652018-08-17 18:11:56 +02004892 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4893 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004894
Gilles Peskineedf9a652018-08-17 18:11:56 +02004895exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004896 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004897 if( status == PSA_SUCCESS )
4898 *ciphertext_length = plaintext_length + operation.tag_length;
4899 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004900}
4901
Gilles Peskineee652a32018-06-01 19:23:52 +02004902/* Locate the tag in a ciphertext buffer containing the encrypted data
4903 * followed by the tag. Return the length of the part preceding the tag in
4904 * *plaintext_length. This is the size of the plaintext in modes where
4905 * the encrypted data has the same size as the plaintext, such as
4906 * CCM and GCM. */
4907static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4908 const uint8_t *ciphertext,
4909 size_t ciphertext_length,
4910 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004911 const uint8_t **p_tag )
4912{
4913 size_t payload_length;
4914 if( tag_length > ciphertext_length )
4915 return( PSA_ERROR_INVALID_ARGUMENT );
4916 payload_length = ciphertext_length - tag_length;
4917 if( payload_length > plaintext_size )
4918 return( PSA_ERROR_BUFFER_TOO_SMALL );
4919 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004920 return( PSA_SUCCESS );
4921}
4922
Gilles Peskinec5487a82018-12-03 18:08:14 +01004923psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004924 psa_algorithm_t alg,
4925 const uint8_t *nonce,
4926 size_t nonce_length,
4927 const uint8_t *additional_data,
4928 size_t additional_data_length,
4929 const uint8_t *ciphertext,
4930 size_t ciphertext_length,
4931 uint8_t *plaintext,
4932 size_t plaintext_size,
4933 size_t *plaintext_length )
4934{
mohammad16035955c982018-04-26 00:53:03 +03004935 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004936 aead_operation_t operation;
4937 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004938
Gilles Peskineee652a32018-06-01 19:23:52 +02004939 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004940
Gilles Peskinec5487a82018-12-03 18:08:14 +01004941 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004942 if( status != PSA_SUCCESS )
4943 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004944
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004945 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4946 ciphertext, ciphertext_length,
4947 plaintext_size, &tag );
4948 if( status != PSA_SUCCESS )
4949 goto exit;
4950
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004951#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004952 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004953 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004954 status = mbedtls_to_psa_error(
4955 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4956 ciphertext_length - operation.tag_length,
4957 nonce, nonce_length,
4958 additional_data,
4959 additional_data_length,
4960 tag, operation.tag_length,
4961 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004962 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004963 else
4964#endif /* MBEDTLS_GCM_C */
4965#if defined(MBEDTLS_CCM_C)
4966 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004967 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004968 status = mbedtls_to_psa_error(
4969 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4970 ciphertext_length - operation.tag_length,
4971 nonce, nonce_length,
4972 additional_data,
4973 additional_data_length,
4974 ciphertext, plaintext,
4975 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004976 }
mohammad160339574652018-06-01 04:39:53 -07004977 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004978#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004979#if defined(MBEDTLS_CHACHAPOLY_C)
4980 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4981 {
4982 if( nonce_length != 12 || operation.tag_length != 16 )
4983 {
4984 status = PSA_ERROR_NOT_SUPPORTED;
4985 goto exit;
4986 }
4987 status = mbedtls_to_psa_error(
4988 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4989 ciphertext_length - operation.tag_length,
4990 nonce,
4991 additional_data,
4992 additional_data_length,
4993 tag,
4994 ciphertext,
4995 plaintext ) );
4996 }
4997 else
4998#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004999 {
mohammad1603554faad2018-06-03 15:07:38 +03005000 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005001 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005002
Gilles Peskineedf9a652018-08-17 18:11:56 +02005003 if( status != PSA_SUCCESS && plaintext_size != 0 )
5004 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005005
Gilles Peskineedf9a652018-08-17 18:11:56 +02005006exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005007 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005008 if( status == PSA_SUCCESS )
5009 *plaintext_length = ciphertext_length - operation.tag_length;
5010 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005011}
5012
Gilles Peskinea0655c32018-04-30 17:06:50 +02005013
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005014
Gilles Peskine20035e32018-02-03 22:44:14 +01005015/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005016/* Generators */
5017/****************************************************************/
5018
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005019#define HKDF_STATE_INIT 0 /* no input yet */
5020#define HKDF_STATE_STARTED 1 /* got salt */
5021#define HKDF_STATE_KEYED 2 /* got key */
5022#define HKDF_STATE_OUTPUT 3 /* output started */
5023
Gilles Peskinecbe66502019-05-16 16:59:18 +02005024static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005025 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005026{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005027 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5028 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005029 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005030 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005031}
5032
5033
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005034psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005035{
5036 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005037 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005038 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005039 {
5040 /* The object has (apparently) been initialized but it is not
5041 * in use. It's ok to call abort on such an object, and there's
5042 * nothing to do. */
5043 }
5044 else
John Durkopd0321952020-10-29 21:37:36 -07005045#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005046 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005047 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005048 mbedtls_free( operation->ctx.hkdf.info );
5049 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005050 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005051 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005052 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005053 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005054 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005055 if( operation->ctx.tls12_prf.seed != NULL )
5056 {
5057 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5058 operation->ctx.tls12_prf.seed_length );
5059 mbedtls_free( operation->ctx.tls12_prf.seed );
5060 }
5061
5062 if( operation->ctx.tls12_prf.label != NULL )
5063 {
5064 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5065 operation->ctx.tls12_prf.label_length );
5066 mbedtls_free( operation->ctx.tls12_prf.label );
5067 }
5068
5069 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5070
5071 /* We leave the fields Ai and output_block to be erased safely by the
5072 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005073 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005074 else
John Durkopd0321952020-10-29 21:37:36 -07005075#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005076 {
5077 status = PSA_ERROR_BAD_STATE;
5078 }
Janos Follath083036a2019-06-11 10:22:26 +01005079 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005080 return( status );
5081}
5082
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005083psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005084 size_t *capacity)
5085{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005086 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005087 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005088 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005089 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005090 }
5091
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005092 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005093 return( PSA_SUCCESS );
5094}
5095
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005096psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005097 size_t capacity )
5098{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005099 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005100 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005101 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005102 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005103 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005104 return( PSA_SUCCESS );
5105}
5106
John Durkopd0321952020-10-29 21:37:36 -07005107#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005109 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005110static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005111 psa_algorithm_t hash_alg,
5112 uint8_t *output,
5113 size_t output_length )
5114{
5115 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5116 psa_status_t status;
5117
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005118 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5119 return( PSA_ERROR_BAD_STATE );
5120 hkdf->state = HKDF_STATE_OUTPUT;
5121
Gilles Peskinebef7f142018-07-12 17:22:21 +02005122 while( output_length != 0 )
5123 {
5124 /* Copy what remains of the current block */
5125 uint8_t n = hash_length - hkdf->offset_in_block;
5126 if( n > output_length )
5127 n = (uint8_t) output_length;
5128 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5129 output += n;
5130 output_length -= n;
5131 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005132 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005133 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005134 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005135 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005136 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005137 * object was corrupted or if this function is called directly
5138 * inside the library. */
5139 if( hkdf->block_number == 0xff )
5140 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005141
5142 /* We need a new block */
5143 ++hkdf->block_number;
5144 hkdf->offset_in_block = 0;
5145 status = psa_hmac_setup_internal( &hkdf->hmac,
5146 hkdf->prk, hash_length,
5147 hash_alg );
5148 if( status != PSA_SUCCESS )
5149 return( status );
5150 if( hkdf->block_number != 1 )
5151 {
5152 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5153 hkdf->output_block,
5154 hash_length );
5155 if( status != PSA_SUCCESS )
5156 return( status );
5157 }
5158 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5159 hkdf->info,
5160 hkdf->info_length );
5161 if( status != PSA_SUCCESS )
5162 return( status );
5163 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5164 &hkdf->block_number, 1 );
5165 if( status != PSA_SUCCESS )
5166 return( status );
5167 status = psa_hmac_finish_internal( &hkdf->hmac,
5168 hkdf->output_block,
5169 sizeof( hkdf->output_block ) );
5170 if( status != PSA_SUCCESS )
5171 return( status );
5172 }
5173
5174 return( PSA_SUCCESS );
5175}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005176
Janos Follath7742fee2019-06-17 12:58:10 +01005177static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5178 psa_tls12_prf_key_derivation_t *tls12_prf,
5179 psa_algorithm_t alg )
5180{
5181 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5182 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005183 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5184 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005185
Janos Follath7742fee2019-06-17 12:58:10 +01005186 /* We can't be wanting more output after block 0xff, otherwise
5187 * the capacity check in psa_key_derivation_output_bytes() would have
5188 * prevented this call. It could happen only if the operation
5189 * object was corrupted or if this function is called directly
5190 * inside the library. */
5191 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005192 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005193
5194 /* We need a new block */
5195 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005196 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005197
5198 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5199 *
5200 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5201 *
5202 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5203 * HMAC_hash(secret, A(2) + seed) +
5204 * HMAC_hash(secret, A(3) + seed) + ...
5205 *
5206 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005207 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005208 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005209 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005210 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005211 * is currently extracted as `output_block` and where i is
5212 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005213 */
5214
Janos Follathea29bfb2019-06-19 12:21:20 +01005215 /* Save the hash context before using it, to preserve the hash state with
5216 * only the inner padding in it. We need this, because inner padding depends
5217 * on the key (secret in the RFC's terminology). */
5218 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5219 if( status != PSA_SUCCESS )
5220 goto cleanup;
5221
5222 /* Calculate A(i) where i = tls12_prf->block_number. */
5223 if( tls12_prf->block_number == 1 )
5224 {
5225 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5226 * the variable seed and in this instance means it in the context of the
5227 * P_hash function, where seed = label + seed.) */
5228 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5229 tls12_prf->label, tls12_prf->label_length );
5230 if( status != PSA_SUCCESS )
5231 goto cleanup;
5232 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5233 tls12_prf->seed, tls12_prf->seed_length );
5234 if( status != PSA_SUCCESS )
5235 goto cleanup;
5236 }
5237 else
5238 {
5239 /* A(i) = HMAC_hash(secret, A(i-1)) */
5240 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5241 tls12_prf->Ai, hash_length );
5242 if( status != PSA_SUCCESS )
5243 goto cleanup;
5244 }
5245
5246 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5247 tls12_prf->Ai, hash_length );
5248 if( status != PSA_SUCCESS )
5249 goto cleanup;
5250 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5251 if( status != PSA_SUCCESS )
5252 goto cleanup;
5253
5254 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5255 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5256 tls12_prf->Ai, hash_length );
5257 if( status != PSA_SUCCESS )
5258 goto cleanup;
5259 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5260 tls12_prf->label, tls12_prf->label_length );
5261 if( status != PSA_SUCCESS )
5262 goto cleanup;
5263 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5264 tls12_prf->seed, tls12_prf->seed_length );
5265 if( status != PSA_SUCCESS )
5266 goto cleanup;
5267 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5268 tls12_prf->output_block, hash_length );
5269 if( status != PSA_SUCCESS )
5270 goto cleanup;
5271 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5272 if( status != PSA_SUCCESS )
5273 goto cleanup;
5274
Janos Follath7742fee2019-06-17 12:58:10 +01005275
5276cleanup:
5277
Janos Follathea29bfb2019-06-19 12:21:20 +01005278 cleanup_status = psa_hash_abort( &backup );
5279 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5280 status = cleanup_status;
5281
5282 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005283}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005284
Janos Follath844eb0e2019-06-19 12:10:49 +01005285static psa_status_t psa_key_derivation_tls12_prf_read(
5286 psa_tls12_prf_key_derivation_t *tls12_prf,
5287 psa_algorithm_t alg,
5288 uint8_t *output,
5289 size_t output_length )
5290{
5291 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5292 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5293 psa_status_t status;
5294 uint8_t offset, length;
5295
5296 while( output_length != 0 )
5297 {
5298 /* Check if we have fully processed the current block. */
5299 if( tls12_prf->left_in_block == 0 )
5300 {
5301 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5302 alg );
5303 if( status != PSA_SUCCESS )
5304 return( status );
5305
5306 continue;
5307 }
5308
5309 if( tls12_prf->left_in_block > output_length )
5310 length = (uint8_t) output_length;
5311 else
5312 length = tls12_prf->left_in_block;
5313
5314 offset = hash_length - tls12_prf->left_in_block;
5315 memcpy( output, tls12_prf->output_block + offset, length );
5316 output += length;
5317 output_length -= length;
5318 tls12_prf->left_in_block -= length;
5319 }
5320
5321 return( PSA_SUCCESS );
5322}
John Durkopd0321952020-10-29 21:37:36 -07005323#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005324
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005325psa_status_t psa_key_derivation_output_bytes(
5326 psa_key_derivation_operation_t *operation,
5327 uint8_t *output,
5328 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005329{
5330 psa_status_t status;
John Durkopd0321952020-10-29 21:37:36 -07005331#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005332 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
John Durkopd0321952020-10-29 21:37:36 -07005333#endif
Gilles Peskineeab56e42018-07-12 17:12:33 +02005334
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005335 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005336 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005337 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005338 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005339 }
5340
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005341 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005342 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005343 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005344 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005345 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005346 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005347 goto exit;
5348 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005349 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005350 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005351 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005352 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005353 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5354 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005355 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005356 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005357 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005358 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005359 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005360
John Durkopd0321952020-10-29 21:37:36 -07005361#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005362 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005363 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005364 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005365 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005366 output, output_length );
5367 }
Janos Follathadbec812019-06-14 11:05:39 +01005368 else
Janos Follathadbec812019-06-14 11:05:39 +01005369 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005370 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005371 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005372 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005373 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005374 output_length );
5375 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005376 else
John Durkopd0321952020-10-29 21:37:36 -07005377#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005378 {
5379 return( PSA_ERROR_BAD_STATE );
5380 }
5381
5382exit:
5383 if( status != PSA_SUCCESS )
5384 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005385 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005386 * This allows us to differentiate between exhausted operations and
5387 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5388 * operations. */
5389 psa_algorithm_t alg = operation->alg;
5390 psa_key_derivation_abort( operation );
5391 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005392 memset( output, '!', output_length );
5393 }
5394 return( status );
5395}
5396
Gilles Peskine08542d82018-07-19 17:05:42 +02005397#if defined(MBEDTLS_DES_C)
5398static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5399{
5400 if( data_size >= 8 )
5401 mbedtls_des_key_set_parity( data );
5402 if( data_size >= 16 )
5403 mbedtls_des_key_set_parity( data + 8 );
5404 if( data_size >= 24 )
5405 mbedtls_des_key_set_parity( data + 16 );
5406}
5407#endif /* MBEDTLS_DES_C */
5408
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005409static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005410 psa_key_slot_t *slot,
5411 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005412 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005413{
5414 uint8_t *data = NULL;
5415 size_t bytes = PSA_BITS_TO_BYTES( bits );
5416 psa_status_t status;
5417
Gilles Peskine8e338702019-07-30 20:06:31 +02005418 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005419 return( PSA_ERROR_INVALID_ARGUMENT );
5420 if( bits % 8 != 0 )
5421 return( PSA_ERROR_INVALID_ARGUMENT );
5422 data = mbedtls_calloc( 1, bytes );
5423 if( data == NULL )
5424 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5425
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005426 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005427 if( status != PSA_SUCCESS )
5428 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005429#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005430 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005431 psa_des_set_key_parity( data, bytes );
5432#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005433 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005434
5435exit:
5436 mbedtls_free( data );
5437 return( status );
5438}
5439
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005440psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005441 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005442 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005443{
5444 psa_status_t status;
5445 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005446 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005447
5448 /* Reject any attempt to create a zero-length key so that we don't
5449 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5450 if( psa_get_key_bits( attributes ) == 0 )
5451 return( PSA_ERROR_INVALID_ARGUMENT );
5452
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005453 if( ! operation->can_output_key )
5454 return( PSA_ERROR_NOT_PERMITTED );
5455
Gilles Peskinedf179142019-07-15 22:02:14 +02005456 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5457 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005458#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5459 if( driver != NULL )
5460 {
5461 /* Deriving a key in a secure element is not implemented yet. */
5462 status = PSA_ERROR_NOT_SUPPORTED;
5463 }
5464#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005465 if( status == PSA_SUCCESS )
5466 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005467 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005468 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005469 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005470 }
5471 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005472 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005473 if( status != PSA_SUCCESS )
5474 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005475 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005476 *handle = 0;
5477 }
5478 return( status );
5479}
5480
Gilles Peskineeab56e42018-07-12 17:12:33 +02005481
5482
5483/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005484/* Key derivation */
5485/****************************************************************/
5486
Gilles Peskine969c5d62019-01-16 15:53:06 +01005487static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005489 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005490{
John Durkopd0321952020-10-29 21:37:36 -07005491#if !defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
5492 (void)kdf_alg;
5493#endif
Janos Follath5fe19732019-06-20 15:09:30 +01005494 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5495 * initialisers for this union leave some bytes unspecified.) */
5496 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5497
Gilles Peskine969c5d62019-01-16 15:53:06 +01005498 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005499#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005500 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5501 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5502 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005503 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005504 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005505 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5506 if( hash_size == 0 )
5507 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005508 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5509 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005510 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005511 {
5512 return( PSA_ERROR_NOT_SUPPORTED );
5513 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005514 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005515 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005516 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005517 else
John Durkopd0321952020-10-29 21:37:36 -07005518#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005519 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005520}
5521
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005522psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005523 psa_algorithm_t alg )
5524{
5525 psa_status_t status;
5526
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005527 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005528 return( PSA_ERROR_BAD_STATE );
5529
Gilles Peskine6843c292019-01-18 16:44:49 +01005530 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5531 return( PSA_ERROR_INVALID_ARGUMENT );
5532 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005533 {
5534 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005535 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005536 }
5537 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5538 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005539 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005540 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005541 else
5542 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005543
5544 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005545 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005546 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005547}
5548
John Durkopd0321952020-10-29 21:37:36 -07005549#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005550static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005551 psa_algorithm_t hash_alg,
5552 psa_key_derivation_step_t step,
5553 const uint8_t *data,
5554 size_t data_length )
5555{
5556 psa_status_t status;
5557 switch( step )
5558 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005559 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005560 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005561 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005562 status = psa_hmac_setup_internal( &hkdf->hmac,
5563 data, data_length,
5564 hash_alg );
5565 if( status != PSA_SUCCESS )
5566 return( status );
5567 hkdf->state = HKDF_STATE_STARTED;
5568 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005569 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005570 /* If no salt was provided, use an empty salt. */
5571 if( hkdf->state == HKDF_STATE_INIT )
5572 {
5573 status = psa_hmac_setup_internal( &hkdf->hmac,
5574 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005575 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005576 if( status != PSA_SUCCESS )
5577 return( status );
5578 hkdf->state = HKDF_STATE_STARTED;
5579 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005580 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005581 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005582 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5583 data, data_length );
5584 if( status != PSA_SUCCESS )
5585 return( status );
5586 status = psa_hmac_finish_internal( &hkdf->hmac,
5587 hkdf->prk,
5588 sizeof( hkdf->prk ) );
5589 if( status != PSA_SUCCESS )
5590 return( status );
5591 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5592 hkdf->block_number = 0;
5593 hkdf->state = HKDF_STATE_KEYED;
5594 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005595 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005596 if( hkdf->state == HKDF_STATE_OUTPUT )
5597 return( PSA_ERROR_BAD_STATE );
5598 if( hkdf->info_set )
5599 return( PSA_ERROR_BAD_STATE );
5600 hkdf->info_length = data_length;
5601 if( data_length != 0 )
5602 {
5603 hkdf->info = mbedtls_calloc( 1, data_length );
5604 if( hkdf->info == NULL )
5605 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5606 memcpy( hkdf->info, data, data_length );
5607 }
5608 hkdf->info_set = 1;
5609 return( PSA_SUCCESS );
5610 default:
5611 return( PSA_ERROR_INVALID_ARGUMENT );
5612 }
5613}
Janos Follathb03233e2019-06-11 15:30:30 +01005614
Janos Follathf08e2652019-06-13 09:05:41 +01005615static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5616 const uint8_t *data,
5617 size_t data_length )
5618{
5619 if( prf->state != TLS12_PRF_STATE_INIT )
5620 return( PSA_ERROR_BAD_STATE );
5621
Janos Follathd6dce9f2019-07-04 09:11:38 +01005622 if( data_length != 0 )
5623 {
5624 prf->seed = mbedtls_calloc( 1, data_length );
5625 if( prf->seed == NULL )
5626 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005627
Janos Follathd6dce9f2019-07-04 09:11:38 +01005628 memcpy( prf->seed, data, data_length );
5629 prf->seed_length = data_length;
5630 }
Janos Follathf08e2652019-06-13 09:05:41 +01005631
5632 prf->state = TLS12_PRF_STATE_SEED_SET;
5633
5634 return( PSA_SUCCESS );
5635}
5636
Janos Follath81550542019-06-13 14:26:34 +01005637static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5638 psa_algorithm_t hash_alg,
5639 const uint8_t *data,
5640 size_t data_length )
5641{
5642 psa_status_t status;
5643 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5644 return( PSA_ERROR_BAD_STATE );
5645
5646 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5647 if( status != PSA_SUCCESS )
5648 return( status );
5649
5650 prf->state = TLS12_PRF_STATE_KEY_SET;
5651
5652 return( PSA_SUCCESS );
5653}
5654
Janos Follath6660f0e2019-06-17 08:44:03 +01005655static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5656 psa_tls12_prf_key_derivation_t *prf,
5657 psa_algorithm_t hash_alg,
5658 const uint8_t *data,
5659 size_t data_length )
5660{
5661 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005662 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5663 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005664
5665 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5666 return( PSA_ERROR_INVALID_ARGUMENT );
5667
5668 /* Quoting RFC 4279, Section 2:
5669 *
5670 * The premaster secret is formed as follows: if the PSK is N octets
5671 * long, concatenate a uint16 with the value N, N zero octets, a second
5672 * uint16 with the value N, and the PSK itself.
5673 */
5674
Janos Follath40e13932019-06-26 13:22:29 +01005675 *cur++ = ( data_length >> 8 ) & 0xff;
5676 *cur++ = ( data_length >> 0 ) & 0xff;
5677 memset( cur, 0, data_length );
5678 cur += data_length;
5679 *cur++ = pms[0];
5680 *cur++ = pms[1];
5681 memcpy( cur, data, data_length );
5682 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005683
Janos Follath40e13932019-06-26 13:22:29 +01005684 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005685
5686 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5687 return( status );
5688}
5689
Janos Follath63028dd2019-06-13 09:15:47 +01005690static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5691 const uint8_t *data,
5692 size_t data_length )
5693{
5694 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5695 return( PSA_ERROR_BAD_STATE );
5696
Janos Follathd6dce9f2019-07-04 09:11:38 +01005697 if( data_length != 0 )
5698 {
5699 prf->label = mbedtls_calloc( 1, data_length );
5700 if( prf->label == NULL )
5701 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005702
Janos Follathd6dce9f2019-07-04 09:11:38 +01005703 memcpy( prf->label, data, data_length );
5704 prf->label_length = data_length;
5705 }
Janos Follath63028dd2019-06-13 09:15:47 +01005706
5707 prf->state = TLS12_PRF_STATE_LABEL_SET;
5708
5709 return( PSA_SUCCESS );
5710}
5711
Janos Follathb03233e2019-06-11 15:30:30 +01005712static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5713 psa_algorithm_t hash_alg,
5714 psa_key_derivation_step_t step,
5715 const uint8_t *data,
5716 size_t data_length )
5717{
Janos Follathb03233e2019-06-11 15:30:30 +01005718 switch( step )
5719 {
Janos Follathf08e2652019-06-13 09:05:41 +01005720 case PSA_KEY_DERIVATION_INPUT_SEED:
5721 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005722 case PSA_KEY_DERIVATION_INPUT_SECRET:
5723 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005724 case PSA_KEY_DERIVATION_INPUT_LABEL:
5725 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005726 default:
5727 return( PSA_ERROR_INVALID_ARGUMENT );
5728 }
5729}
Janos Follath6660f0e2019-06-17 08:44:03 +01005730
5731static psa_status_t psa_tls12_prf_psk_to_ms_input(
5732 psa_tls12_prf_key_derivation_t *prf,
5733 psa_algorithm_t hash_alg,
5734 psa_key_derivation_step_t step,
5735 const uint8_t *data,
5736 size_t data_length )
5737{
5738 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005739 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005740 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5741 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005742 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005743
5744 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5745}
John Durkopd0321952020-10-29 21:37:36 -07005746#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005747
Gilles Peskineb8965192019-09-24 16:21:10 +02005748/** Check whether the given key type is acceptable for the given
5749 * input step of a key derivation.
5750 *
5751 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5752 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5753 * Both secret and non-secret inputs can alternatively have the type
5754 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5755 * that the input was passed as a buffer rather than via a key object.
5756 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005757static int psa_key_derivation_check_input_type(
5758 psa_key_derivation_step_t step,
5759 psa_key_type_t key_type )
5760{
5761 switch( step )
5762 {
5763 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005764 if( key_type == PSA_KEY_TYPE_DERIVE )
5765 return( PSA_SUCCESS );
5766 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005767 return( PSA_SUCCESS );
5768 break;
5769 case PSA_KEY_DERIVATION_INPUT_LABEL:
5770 case PSA_KEY_DERIVATION_INPUT_SALT:
5771 case PSA_KEY_DERIVATION_INPUT_INFO:
5772 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005773 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5774 return( PSA_SUCCESS );
5775 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005776 return( PSA_SUCCESS );
5777 break;
5778 }
5779 return( PSA_ERROR_INVALID_ARGUMENT );
5780}
5781
Janos Follathb80a94e2019-06-12 15:54:46 +01005782static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005783 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005784 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005785 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005786 const uint8_t *data,
5787 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005788{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005789 psa_status_t status;
John Durkopd0321952020-10-29 21:37:36 -07005790#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005791 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
John Durkopd0321952020-10-29 21:37:36 -07005792#else
5793 (void)data;
5794 (void)data_length;
5795#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005796
5797 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005798 if( status != PSA_SUCCESS )
5799 goto exit;
5800
John Durkopd0321952020-10-29 21:37:36 -07005801#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005802 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005803 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005804 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005805 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005806 step, data, data_length );
5807 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005808 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005809 {
Janos Follathb03233e2019-06-11 15:30:30 +01005810 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5811 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5812 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005813 }
5814 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5815 {
5816 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5817 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5818 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005819 }
5820 else
John Durkopd0321952020-10-29 21:37:36 -07005821#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005822 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005823 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005824 return( PSA_ERROR_BAD_STATE );
5825 }
5826
Gilles Peskine224b0d62019-09-23 18:13:17 +02005827exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005828 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005829 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005830 return( status );
5831}
5832
Janos Follath51f4a0f2019-06-14 11:35:55 +01005833psa_status_t psa_key_derivation_input_bytes(
5834 psa_key_derivation_operation_t *operation,
5835 psa_key_derivation_step_t step,
5836 const uint8_t *data,
5837 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005838{
Gilles Peskineb8965192019-09-24 16:21:10 +02005839 return( psa_key_derivation_input_internal( operation, step,
5840 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005841 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005842}
5843
Janos Follath51f4a0f2019-06-14 11:35:55 +01005844psa_status_t psa_key_derivation_input_key(
5845 psa_key_derivation_operation_t *operation,
5846 psa_key_derivation_step_t step,
5847 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005848{
5849 psa_key_slot_t *slot;
5850 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005851
Gilles Peskine28f8f302019-07-24 13:30:31 +02005852 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005853 PSA_KEY_USAGE_DERIVE,
5854 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005855 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005856 {
5857 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005858 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005859 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005860
5861 /* Passing a key object as a SECRET input unlocks the permission
5862 * to output to a key object. */
5863 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5864 operation->can_output_key = 1;
5865
Janos Follathb80a94e2019-06-12 15:54:46 +01005866 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005867 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005868 slot->data.key.data,
5869 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005870}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005871
5872
5873
5874/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005875/* Key agreement */
5876/****************************************************************/
5877
John Durkop6ba40d12020-11-10 08:50:04 -08005878#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005879static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5880 size_t peer_key_length,
5881 const mbedtls_ecp_keypair *our_key,
5882 uint8_t *shared_secret,
5883 size_t shared_secret_size,
5884 size_t *shared_secret_length )
5885{
Steven Cooremand4867872020-08-05 16:31:39 +02005886 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005887 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005888 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005889 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005890 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005891 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005892
Steven Cooreman4fed4552020-08-03 14:46:03 +02005893 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5894 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02005895 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02005896 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005897 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005898 goto exit;
5899
Jaeden Amero97271b32019-01-10 19:38:51 +00005900 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005901 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005902 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005903 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005904 status = mbedtls_to_psa_error(
5905 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5906 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005907 goto exit;
5908
Jaeden Amero97271b32019-01-10 19:38:51 +00005909 status = mbedtls_to_psa_error(
5910 mbedtls_ecdh_calc_secret( &ecdh,
5911 shared_secret_length,
5912 shared_secret, shared_secret_size,
5913 mbedtls_ctr_drbg_random,
5914 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005915 if( status != PSA_SUCCESS )
5916 goto exit;
5917 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5918 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005919
5920exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005921 if( status != PSA_SUCCESS )
5922 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005923 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005924 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005925 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005926
Jaeden Amero97271b32019-01-10 19:38:51 +00005927 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005928}
John Durkop6ba40d12020-11-10 08:50:04 -08005929#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005930
Gilles Peskine01d718c2018-09-18 12:01:02 +02005931#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5932
Gilles Peskine0216fe12019-04-11 21:23:21 +02005933static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5934 psa_key_slot_t *private_key,
5935 const uint8_t *peer_key,
5936 size_t peer_key_length,
5937 uint8_t *shared_secret,
5938 size_t shared_secret_size,
5939 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005940{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005941 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005942 {
John Durkop6ba40d12020-11-10 08:50:04 -08005943#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005944 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005945 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005946 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005947 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005948 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02005949 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02005950 private_key->data.key.data,
5951 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02005952 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005953 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005954 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005955 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005956 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005957 shared_secret, shared_secret_size,
5958 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005959 mbedtls_ecp_keypair_free( ecp );
5960 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005961 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005962#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005963 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005964 (void) private_key;
5965 (void) peer_key;
5966 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005967 (void) shared_secret;
5968 (void) shared_secret_size;
5969 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005970 return( PSA_ERROR_NOT_SUPPORTED );
5971 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005972}
5973
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005974/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005975 * to potentially free embedded data structures and wipe confidential data.
5976 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005977static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005978 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005979 psa_key_slot_t *private_key,
5980 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005981 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005982{
5983 psa_status_t status;
5984 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5985 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005986 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005987
5988 /* Step 1: run the secret agreement algorithm to generate the shared
5989 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005990 status = psa_key_agreement_raw_internal( ka_alg,
5991 private_key,
5992 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005993 shared_secret,
5994 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005995 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005996 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005997 goto exit;
5998
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005999 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006000 * the shared secret. A shared secret is permitted wherever a key
6001 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006002 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006003 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006004 shared_secret,
6005 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006006exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006007 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006008 return( status );
6009}
6010
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006011psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006012 psa_key_derivation_step_t step,
6013 psa_key_handle_t private_key,
6014 const uint8_t *peer_key,
6015 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006016{
Gilles Peskine2f060a82018-12-04 17:12:32 +01006017 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02006018 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006019 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006020 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02006021 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006022 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006023 if( status != PSA_SUCCESS )
6024 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006025 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006026 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006027 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006028 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006029 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006030 else
6031 {
6032 /* If a private key has been added as SECRET, we allow the derived
6033 * key material to be used as a key in PSA Crypto. */
6034 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6035 operation->can_output_key = 1;
6036 }
Gilles Peskine346797d2018-11-16 16:05:06 +01006037 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006038}
6039
Gilles Peskinebe697d82019-05-16 18:00:41 +02006040psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
6041 psa_key_handle_t private_key,
6042 const uint8_t *peer_key,
6043 size_t peer_key_length,
6044 uint8_t *output,
6045 size_t output_size,
6046 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006047{
6048 psa_key_slot_t *slot;
6049 psa_status_t status;
6050
6051 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6052 {
6053 status = PSA_ERROR_INVALID_ARGUMENT;
6054 goto exit;
6055 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02006056 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02006057 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006058 if( status != PSA_SUCCESS )
6059 goto exit;
6060
6061 status = psa_key_agreement_raw_internal( alg, slot,
6062 peer_key, peer_key_length,
6063 output, output_size,
6064 output_length );
6065
6066exit:
6067 if( status != PSA_SUCCESS )
6068 {
6069 /* If an error happens and is not handled properly, the output
6070 * may be used as a key to protect sensitive data. Arrange for such
6071 * a key to be random, which is likely to result in decryption or
6072 * verification errors. This is better than filling the buffer with
6073 * some constant data such as zeros, which would result in the data
6074 * being protected with a reproducible, easily knowable key.
6075 */
6076 psa_generate_random( output, output_size );
6077 *output_length = output_size;
6078 }
6079 return( status );
6080}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006081
6082
6083/****************************************************************/
6084/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006085/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006086
Gilles Peskine4c317f42018-07-12 01:24:09 +02006087psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02006088 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006089{
Janos Follath24eed8d2019-11-22 13:21:35 +00006090 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006091 GUARD_MODULE_INITIALIZED;
6092
Gilles Peskinef181eca2019-08-07 13:49:00 +02006093 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
6094 {
6095 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
6096 output,
6097 MBEDTLS_CTR_DRBG_MAX_REQUEST );
6098 if( ret != 0 )
6099 return( mbedtls_to_psa_error( ret ) );
6100 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
6101 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
6102 }
6103
Gilles Peskinee59236f2018-01-27 23:32:46 +01006104 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
6105 return( mbedtls_to_psa_error( ret ) );
6106}
6107
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006108#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6109#include "mbedtls/entropy_poll.h"
6110
Gilles Peskine7228da22019-07-15 11:06:15 +02006111psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006112 size_t seed_size )
6113{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006114 if( global_data.initialized )
6115 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006116
6117 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6118 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6119 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6120 return( PSA_ERROR_INVALID_ARGUMENT );
6121
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006122 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006123}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006124#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006125
John Durkop6ba40d12020-11-10 08:50:04 -08006126#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6127 defined(MBEDTLS_GENPRIME)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006128static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6129 size_t domain_parameters_size,
6130 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006131{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006132 size_t i;
6133 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006134
Gilles Peskinee56e8782019-04-26 17:34:02 +02006135 if( domain_parameters_size == 0 )
6136 {
6137 *exponent = 65537;
6138 return( PSA_SUCCESS );
6139 }
6140
6141 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6142 * support values that fit in a 32-bit integer, which is larger than
6143 * int on just about every platform anyway. */
6144 if( domain_parameters_size > sizeof( acc ) )
6145 return( PSA_ERROR_NOT_SUPPORTED );
6146 for( i = 0; i < domain_parameters_size; i++ )
6147 acc = ( acc << 8 ) | domain_parameters[i];
6148 if( acc > INT_MAX )
6149 return( PSA_ERROR_NOT_SUPPORTED );
6150 *exponent = acc;
6151 return( PSA_SUCCESS );
6152}
John Durkop6ba40d12020-11-10 08:50:04 -08006153#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) &&
6154 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006155
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006156static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006157 psa_key_slot_t *slot, size_t bits,
6158 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006159{
Gilles Peskine8e338702019-07-30 20:06:31 +02006160 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006161
Gilles Peskinee56e8782019-04-26 17:34:02 +02006162 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006163 return( PSA_ERROR_INVALID_ARGUMENT );
6164
Gilles Peskinee59236f2018-01-27 23:32:46 +01006165 if( key_type_is_raw_bytes( type ) )
6166 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006167 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006168
6169 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006170 if( status != PSA_SUCCESS )
6171 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006172
6173 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006174 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6175 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006176 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006177
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006178 status = psa_generate_random( slot->data.key.data,
6179 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006180 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006181 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006182
6183 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006184#if defined(MBEDTLS_DES_C)
6185 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006186 psa_des_set_key_parity( slot->data.key.data,
6187 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006188#endif /* MBEDTLS_DES_C */
6189 }
6190 else
6191
John Durkop6ba40d12020-11-10 08:50:04 -08006192#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
6193 defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006194 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006195 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006196 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006197 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006198 int exponent;
6199 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006200 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6201 return( PSA_ERROR_NOT_SUPPORTED );
6202 /* Accept only byte-aligned keys, for the same reasons as
6203 * in psa_import_rsa_key(). */
6204 if( bits % 8 != 0 )
6205 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006206 status = psa_read_rsa_exponent( domain_parameters,
6207 domain_parameters_size,
6208 &exponent );
6209 if( status != PSA_SUCCESS )
6210 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006211 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6212 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006213 mbedtls_ctr_drbg_random,
6214 &global_data.ctr_drbg,
6215 (unsigned int) bits,
6216 exponent );
6217 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006218 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006219
6220 /* Make sure to always have an export representation available */
6221 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6222
Steven Cooreman75b74362020-07-28 14:30:13 +02006223 status = psa_allocate_buffer_to_slot( slot, bytes );
6224 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006225 {
6226 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006227 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006228 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006229
6230 status = psa_export_rsa_key( type,
6231 &rsa,
6232 slot->data.key.data,
6233 bytes,
6234 &slot->data.key.bytes );
6235 mbedtls_rsa_free( &rsa );
6236 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006237 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006238 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006239 }
6240 else
John Durkop6ba40d12020-11-10 08:50:04 -08006241#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) &&
6242 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006243
John Durkop9814fa22020-11-04 12:28:15 -08006244#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006245 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006246 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006247 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006248 mbedtls_ecp_group_id grp_id =
6249 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006250 const mbedtls_ecp_curve_info *curve_info =
6251 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006252 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006254 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006255 return( PSA_ERROR_NOT_SUPPORTED );
6256 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6257 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006258 mbedtls_ecp_keypair_init( &ecp );
6259 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006260 mbedtls_ctr_drbg_random,
6261 &global_data.ctr_drbg );
6262 if( ret != 0 )
6263 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006264 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006265 return( mbedtls_to_psa_error( ret ) );
6266 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006267
6268
6269 /* Make sure to always have an export representation available */
6270 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006271 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6272 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006273 {
6274 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006275 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006276 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006277
6278 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006279 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6280
6281 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006282 if( status != PSA_SUCCESS ) {
6283 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006284 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006285 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006286 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006287 }
6288 else
John Durkop9814fa22020-11-04 12:28:15 -08006289#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006290 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006291 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006292 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006293
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006294 return( PSA_SUCCESS );
6295}
Darryl Green0c6575a2018-11-07 16:05:30 +00006296
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006297psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006298 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006299{
6300 psa_status_t status;
6301 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006302 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006303
Gilles Peskine0f84d622019-09-12 19:03:13 +02006304 /* Reject any attempt to create a zero-length key so that we don't
6305 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6306 if( psa_get_key_bits( attributes ) == 0 )
6307 return( PSA_ERROR_INVALID_ARGUMENT );
6308
Gilles Peskinedf179142019-07-15 22:02:14 +02006309 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6310 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006311 if( status != PSA_SUCCESS )
6312 goto exit;
6313
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006314 status = psa_driver_wrapper_generate_key( attributes,
6315 slot );
6316 if( status != PSA_ERROR_NOT_SUPPORTED ||
6317 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6318 goto exit;
6319
6320 status = psa_generate_key_internal(
6321 slot, attributes->core.bits,
6322 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006323
6324exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006325 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006326 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006327 if( status != PSA_SUCCESS )
6328 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006329 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006330 *handle = 0;
6331 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006332 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006333}
6334
6335
Gilles Peskinee59236f2018-01-27 23:32:46 +01006336
6337/****************************************************************/
6338/* Module setup */
6339/****************************************************************/
6340
Gilles Peskine5e769522018-11-20 21:59:56 +01006341psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6342 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6343 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6344{
6345 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6346 return( PSA_ERROR_BAD_STATE );
6347 global_data.entropy_init = entropy_init;
6348 global_data.entropy_free = entropy_free;
6349 return( PSA_SUCCESS );
6350}
6351
Gilles Peskinee59236f2018-01-27 23:32:46 +01006352void mbedtls_psa_crypto_free( void )
6353{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006354 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006355 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6356 {
6357 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006358 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006359 }
6360 /* Wipe all remaining data, including configuration.
6361 * In particular, this sets all state indicator to the value
6362 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006363 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006364#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006365 /* Unregister all secure element drivers, so that we restart from
6366 * a pristine state. */
6367 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006368#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006369}
6370
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006371#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6372/** Recover a transaction that was interrupted by a power failure.
6373 *
6374 * This function is called during initialization, before psa_crypto_init()
6375 * returns. If this function returns a failure status, the initialization
6376 * fails.
6377 */
6378static psa_status_t psa_crypto_recover_transaction(
6379 const psa_crypto_transaction_t *transaction )
6380{
6381 switch( transaction->unknown.type )
6382 {
6383 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6384 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006385 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006386 * is implemented.
6387 * https://github.com/ARMmbed/mbed-crypto/issues/218
6388 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006389 default:
6390 /* We found an unsupported transaction in the storage.
6391 * We don't know what state the storage is in. Give up. */
6392 return( PSA_ERROR_STORAGE_FAILURE );
6393 }
6394}
6395#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6396
Gilles Peskinee59236f2018-01-27 23:32:46 +01006397psa_status_t psa_crypto_init( void )
6398{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006399 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006400 const unsigned char drbg_seed[] = "PSA";
6401
Gilles Peskinec6b69072018-11-20 21:42:52 +01006402 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006403 if( global_data.initialized != 0 )
6404 return( PSA_SUCCESS );
6405
Gilles Peskine5e769522018-11-20 21:59:56 +01006406 /* Set default configuration if
6407 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6408 if( global_data.entropy_init == NULL )
6409 global_data.entropy_init = mbedtls_entropy_init;
6410 if( global_data.entropy_free == NULL )
6411 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006412
Gilles Peskinec6b69072018-11-20 21:42:52 +01006413 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006414 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006415#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6416 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6417 /* The PSA entropy injection feature depends on using NV seed as an entropy
6418 * source. Add NV seed as an entropy source for PSA entropy injection. */
6419 mbedtls_entropy_add_source( &global_data.entropy,
6420 mbedtls_nv_seed_poll, NULL,
6421 MBEDTLS_ENTROPY_BLOCK_SIZE,
6422 MBEDTLS_ENTROPY_SOURCE_STRONG );
6423#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006424 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006425 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006426 status = mbedtls_to_psa_error(
6427 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6428 mbedtls_entropy_func,
6429 &global_data.entropy,
6430 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6431 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006432 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006433 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006434
Gilles Peskine66fb1262018-12-10 16:29:04 +01006435 status = psa_initialize_key_slots( );
6436 if( status != PSA_SUCCESS )
6437 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006438
Gilles Peskined9348f22019-10-01 15:22:29 +02006439#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6440 status = psa_init_all_se_drivers( );
6441 if( status != PSA_SUCCESS )
6442 goto exit;
6443#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6444
Gilles Peskine4b734222019-07-24 15:56:31 +02006445#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006446 status = psa_crypto_load_transaction( );
6447 if( status == PSA_SUCCESS )
6448 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006449 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6450 if( status != PSA_SUCCESS )
6451 goto exit;
6452 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006453 }
6454 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6455 {
6456 /* There's no transaction to complete. It's all good. */
6457 status = PSA_SUCCESS;
6458 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006459#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006460
Gilles Peskinec6b69072018-11-20 21:42:52 +01006461 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006462 global_data.initialized = 1;
6463
6464exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006465 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006466 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006467 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006468}
6469
6470#endif /* MBEDTLS_PSA_CRYPTO_C */