blob: a8d2621a88f6d66bc6233a0093b6880daa841699 [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
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
itayzafrir7723ab12019-02-14 10:28:02 +020029#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
31
Gilles Peskine039b90c2018-12-07 18:24:41 +010032#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010033#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "psa_crypto_driver_wrappers.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskine90edc992020-11-13 15:39:19 +010043#include "psa_crypto_random.h"
44
Gilles Peskineb46bef22019-07-30 21:32:04 +020045#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include <stdlib.h>
47#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020049#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010050#define mbedtls_calloc calloc
51#define mbedtls_free free
52#endif
53
Gilles Peskinea5905292018-02-07 20:59:33 +010054#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020055#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000056#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020057#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/blowfish.h"
59#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020060#include "mbedtls/chacha20.h"
61#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/cipher.h"
63#include "mbedtls/ccm.h"
64#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020066#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010067#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010068#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/error.h"
70#include "mbedtls/gcm.h"
71#include "mbedtls/md2.h"
72#include "mbedtls/md4.h"
73#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010074#include "mbedtls/md.h"
75#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010076#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010077#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010078#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000079#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010082#include "mbedtls/sha1.h"
83#include "mbedtls/sha256.h"
84#include "mbedtls/sha512.h"
85#include "mbedtls/xtea.h"
86
Gilles Peskine996deb12018-08-01 15:45:45 +020087#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
88
Gilles Peskine9ef733f2018-02-07 21:05:37 +010089/* constant-time buffer comparison */
90static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
91{
92 size_t i;
93 unsigned char diff = 0;
94
95 for( i = 0; i < n; i++ )
96 diff |= a[i] ^ b[i];
97
98 return( diff );
99}
100
101
102
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100103/****************************************************************/
104/* Global data, support functions and library management */
105/****************************************************************/
106
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200107static int key_type_is_raw_bytes( psa_key_type_t type )
108{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200109 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200110}
111
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100112/* Values for psa_global_data_t::rng_state */
113#define RNG_NOT_INITIALIZED 0
114#define RNG_INITIALIZED 1
115#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100116
Gilles Peskine2d277862018-06-18 15:41:12 +0200117typedef struct
118{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100119 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100120 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100121 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100122} psa_global_data_t;
123
124static psa_global_data_t global_data;
125
itayzafrir0adf0fc2018-09-06 16:24:41 +0300126#define GUARD_MODULE_INITIALIZED \
127 if( global_data.initialized == 0 ) \
128 return( PSA_ERROR_BAD_STATE );
129
Steven Cooreman01164162020-07-20 15:31:37 +0200130psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131{
Gilles Peskinea5905292018-02-07 20:59:33 +0100132 /* If there's both a high-level code and low-level code, dispatch on
133 * the high-level code. */
134 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100135 {
136 case 0:
137 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100138
139 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
140 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
141 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
142 return( PSA_ERROR_NOT_SUPPORTED );
143 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
144 return( PSA_ERROR_HARDWARE_FAILURE );
145
146 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
147 return( PSA_ERROR_HARDWARE_FAILURE );
148
Gilles Peskine9a944802018-06-21 09:35:35 +0200149 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
150 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
151 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
152 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
153 case MBEDTLS_ERR_ASN1_INVALID_DATA:
154 return( PSA_ERROR_INVALID_ARGUMENT );
155 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
156 return( PSA_ERROR_INSUFFICIENT_MEMORY );
157 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
158 return( PSA_ERROR_BUFFER_TOO_SMALL );
159
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000161 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
163 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
164#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100165 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
166 return( PSA_ERROR_NOT_SUPPORTED );
167 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
168 return( PSA_ERROR_HARDWARE_FAILURE );
169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
173 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
180 case MBEDTLS_ERR_CCM_BAD_INPUT:
181 return( PSA_ERROR_INVALID_ARGUMENT );
182 case MBEDTLS_ERR_CCM_AUTH_FAILED:
183 return( PSA_ERROR_INVALID_SIGNATURE );
184 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
Gilles Peskine26869f22019-05-06 15:25:00 +0200187 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
188 return( PSA_ERROR_INVALID_ARGUMENT );
189
190 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
191 return( PSA_ERROR_BAD_STATE );
192 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
193 return( PSA_ERROR_INVALID_SIGNATURE );
194
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
196 return( PSA_ERROR_NOT_SUPPORTED );
197 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
200 return( PSA_ERROR_INSUFFICIENT_MEMORY );
201 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
202 return( PSA_ERROR_INVALID_PADDING );
203 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200204 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100205 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
206 return( PSA_ERROR_INVALID_SIGNATURE );
207 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200208 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
213 return( PSA_ERROR_HARDWARE_FAILURE );
214
215 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
216 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
217 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
218 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
219 return( PSA_ERROR_NOT_SUPPORTED );
220 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
221 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
222
223 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
Gilles Peskinee59236f2018-01-27 23:32:46 +0100228 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
229 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
230 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
233 case MBEDTLS_ERR_GCM_AUTH_FAILED:
234 return( PSA_ERROR_INVALID_SIGNATURE );
235 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200236 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100237 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
238 return( PSA_ERROR_HARDWARE_FAILURE );
239
240 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
245 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
246 return( PSA_ERROR_NOT_SUPPORTED );
247 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
248 return( PSA_ERROR_INVALID_ARGUMENT );
249 case MBEDTLS_ERR_MD_ALLOC_FAILED:
250 return( PSA_ERROR_INSUFFICIENT_MEMORY );
251 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
252 return( PSA_ERROR_STORAGE_FAILURE );
253 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
254 return( PSA_ERROR_HARDWARE_FAILURE );
255
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
257 return( PSA_ERROR_STORAGE_FAILURE );
258 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
263 return( PSA_ERROR_BUFFER_TOO_SMALL );
264 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_ALLOC_FAILED:
274 return( PSA_ERROR_INSUFFICIENT_MEMORY );
275 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
276 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100279 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100280 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
281 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
284 return( PSA_ERROR_NOT_SUPPORTED );
285 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
286 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
287 return( PSA_ERROR_NOT_PERMITTED );
288 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_PK_INVALID_ALG:
291 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
292 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
293 return( PSA_ERROR_NOT_SUPPORTED );
294 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
295 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
300 return( PSA_ERROR_HARDWARE_FAILURE );
301 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
302 return( PSA_ERROR_NOT_SUPPORTED );
303
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
305 return( PSA_ERROR_HARDWARE_FAILURE );
306
307 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_RSA_INVALID_PADDING:
310 return( PSA_ERROR_INVALID_PADDING );
311 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
312 return( PSA_ERROR_HARDWARE_FAILURE );
313 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
314 return( PSA_ERROR_INVALID_ARGUMENT );
315 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
316 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200317 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100318 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
319 return( PSA_ERROR_INVALID_SIGNATURE );
320 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
321 return( PSA_ERROR_BUFFER_TOO_SMALL );
322 case MBEDTLS_ERR_RSA_RNG_FAILED:
323 return( PSA_ERROR_INSUFFICIENT_MEMORY );
324 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
325 return( PSA_ERROR_NOT_SUPPORTED );
326 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
327 return( PSA_ERROR_HARDWARE_FAILURE );
328
329 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
330 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
332 return( PSA_ERROR_HARDWARE_FAILURE );
333
334 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
335 return( PSA_ERROR_INVALID_ARGUMENT );
336 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
337 return( PSA_ERROR_HARDWARE_FAILURE );
338
itayzafrir5c753392018-05-08 11:18:38 +0300339 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300341 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
343 return( PSA_ERROR_BUFFER_TOO_SMALL );
344 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
345 return( PSA_ERROR_NOT_SUPPORTED );
346 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
347 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
348 return( PSA_ERROR_INVALID_SIGNATURE );
349 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
350 return( PSA_ERROR_INSUFFICIENT_MEMORY );
351 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
352 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000353 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
354 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300355
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 default:
David Saadab4ecc272019-02-14 13:48:10 +0200357 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100358 }
359}
360
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200361
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200362
363
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100364/****************************************************************/
365/* Key management */
366/****************************************************************/
367
Gilles Peskine73167e12019-07-12 23:44:37 +0200368#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
369static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
370{
Gilles Peskine8e338702019-07-30 20:06:31 +0200371 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200372}
373#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
374
John Durkop07cc04a2020-11-16 22:08:34 -0800375/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
376 * current test driver in key_management.c is using this function
377 * when accelerators are used for ECC key pair and public key.
378 * Once that dependency is resolved these guards can be removed.
379 */
John Durkop9814fa22020-11-04 12:28:15 -0800380#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800381 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
382 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
383 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100384mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100385 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200386{
387 switch( curve )
388 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100389 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100390 switch( byte_length )
391 {
392 case PSA_BITS_TO_BYTES( 192 ):
393 return( MBEDTLS_ECP_DP_SECP192R1 );
394 case PSA_BITS_TO_BYTES( 224 ):
395 return( MBEDTLS_ECP_DP_SECP224R1 );
396 case PSA_BITS_TO_BYTES( 256 ):
397 return( MBEDTLS_ECP_DP_SECP256R1 );
398 case PSA_BITS_TO_BYTES( 384 ):
399 return( MBEDTLS_ECP_DP_SECP384R1 );
400 case PSA_BITS_TO_BYTES( 521 ):
401 return( MBEDTLS_ECP_DP_SECP521R1 );
402 default:
403 return( MBEDTLS_ECP_DP_NONE );
404 }
405 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100406
Paul Elliott8ff510a2020-06-02 17:19:28 +0100407 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100408 switch( byte_length )
409 {
410 case PSA_BITS_TO_BYTES( 256 ):
411 return( MBEDTLS_ECP_DP_BP256R1 );
412 case PSA_BITS_TO_BYTES( 384 ):
413 return( MBEDTLS_ECP_DP_BP384R1 );
414 case PSA_BITS_TO_BYTES( 512 ):
415 return( MBEDTLS_ECP_DP_BP512R1 );
416 default:
417 return( MBEDTLS_ECP_DP_NONE );
418 }
419 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100420
Paul Elliott8ff510a2020-06-02 17:19:28 +0100421 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100422 switch( byte_length )
423 {
424 case PSA_BITS_TO_BYTES( 255 ):
425 return( MBEDTLS_ECP_DP_CURVE25519 );
426 case PSA_BITS_TO_BYTES( 448 ):
427 return( MBEDTLS_ECP_DP_CURVE448 );
428 default:
429 return( MBEDTLS_ECP_DP_NONE );
430 }
431 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100432
Paul Elliott8ff510a2020-06-02 17:19:28 +0100433 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100434 switch( byte_length )
435 {
436 case PSA_BITS_TO_BYTES( 192 ):
437 return( MBEDTLS_ECP_DP_SECP192K1 );
438 case PSA_BITS_TO_BYTES( 224 ):
439 return( MBEDTLS_ECP_DP_SECP224K1 );
440 case PSA_BITS_TO_BYTES( 256 ):
441 return( MBEDTLS_ECP_DP_SECP256K1 );
442 default:
443 return( MBEDTLS_ECP_DP_NONE );
444 }
445 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100446
Gilles Peskine12313cd2018-06-20 00:20:32 +0200447 default:
448 return( MBEDTLS_ECP_DP_NONE );
449 }
450}
John Durkop9814fa22020-11-04 12:28:15 -0800451#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800452 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
453 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
454 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200455
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200456static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
457 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200458{
459 /* Check that the bit size is acceptable for the key type */
460 switch( type )
461 {
462 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200463 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200464 case PSA_KEY_TYPE_DERIVE:
465 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200466#if defined(MBEDTLS_AES_C)
467 case PSA_KEY_TYPE_AES:
468 if( bits != 128 && bits != 192 && bits != 256 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
470 break;
471#endif
472#if defined(MBEDTLS_CAMELLIA_C)
473 case PSA_KEY_TYPE_CAMELLIA:
474 if( bits != 128 && bits != 192 && bits != 256 )
475 return( PSA_ERROR_INVALID_ARGUMENT );
476 break;
477#endif
478#if defined(MBEDTLS_DES_C)
479 case PSA_KEY_TYPE_DES:
480 if( bits != 64 && bits != 128 && bits != 192 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
482 break;
483#endif
484#if defined(MBEDTLS_ARC4_C)
485 case PSA_KEY_TYPE_ARC4:
486 if( bits < 8 || bits > 2048 )
487 return( PSA_ERROR_INVALID_ARGUMENT );
488 break;
489#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200490#if defined(MBEDTLS_CHACHA20_C)
491 case PSA_KEY_TYPE_CHACHA20:
492 if( bits != 256 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
494 break;
495#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496 default:
497 return( PSA_ERROR_NOT_SUPPORTED );
498 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200499 if( bits % 8 != 0 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200502 return( PSA_SUCCESS );
503}
504
John Durkop0e005192020-10-31 22:06:54 -0700505#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
506 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
507 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
John Durkop9814fa22020-11-04 12:28:15 -0800508 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
509 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
510 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana01795d2020-07-24 22:48:15 +0200511
Gilles Peskine86a440b2018-11-12 18:39:40 +0100512/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
513 * that are not a multiple of 8) well. For example, there is only
514 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
515 * way to return the exact bit size of a key.
516 * To keep things simple, reject non-byte-aligned key sizes. */
517static psa_status_t psa_check_rsa_key_byte_aligned(
518 const mbedtls_rsa_context *rsa )
519{
520 mbedtls_mpi n;
521 psa_status_t status;
522 mbedtls_mpi_init( &n );
523 status = mbedtls_to_psa_error(
524 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
525 if( status == PSA_SUCCESS )
526 {
527 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
528 status = PSA_ERROR_NOT_SUPPORTED;
529 }
530 mbedtls_mpi_free( &n );
531 return( status );
532}
533
Steven Cooreman7f391872020-07-30 14:57:44 +0200534/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200535 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200536 * \param[in] type The type of key contained in \p data.
537 * \param[in] data The buffer from which to load the representation.
538 * \param[in] data_length The size in bytes of \p data.
539 * \param[out] p_rsa Returns a pointer to an RSA context on success.
540 * The caller is responsible for freeing both the
541 * contents of the context and the context itself
542 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200543 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200544static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
545 const uint8_t *data,
546 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200547 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200548{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000549 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200550 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000551 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200552 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000553
554 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200555 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000556 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200557 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200558 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200560 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000561 if( status != PSA_SUCCESS )
562 goto exit;
563
564 /* We have something that the pkparse module recognizes. If it is a
565 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200566 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200567 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000568 status = PSA_ERROR_INVALID_ARGUMENT;
569 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200570 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000571
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000572 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
573 * supports non-byte-aligned key sizes, but not well. For example,
574 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200575 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000576 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
577 {
578 status = PSA_ERROR_NOT_SUPPORTED;
579 goto exit;
580 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200581 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200582 if( status != PSA_SUCCESS )
583 goto exit;
584
Steven Cooremana2371e52020-07-28 14:30:39 +0200585 /* Copy out the pointer to the RSA context, and reset the PK context
586 * such that pk_free doesn't free the RSA context we just grabbed. */
587 *p_rsa = mbedtls_pk_rsa( ctx );
588 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000589
590exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200591 mbedtls_pk_free( &ctx );
592 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +0200593}
594
John Durkop6ba40d12020-11-10 08:50:04 -0800595#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
596 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
597 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
598 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
599 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
600 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
601
602#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
603 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
604
Steven Cooremana01795d2020-07-24 22:48:15 +0200605/** Export an RSA key to export representation
606 *
607 * \param[in] type The type of key (public/private) to export
608 * \param[in] rsa The internal RSA representation from which to export
609 * \param[out] data The buffer to export to
610 * \param[in] data_size The length of the buffer to export to
611 * \param[out] data_length The amount of bytes written to \p data
612 */
613static psa_status_t psa_export_rsa_key( psa_key_type_t type,
614 mbedtls_rsa_context *rsa,
615 uint8_t *data,
616 size_t data_size,
617 size_t *data_length )
618{
619#if defined(MBEDTLS_PK_WRITE_C)
620 int ret;
621 mbedtls_pk_context pk;
622 uint8_t *pos = data + data_size;
623
624 mbedtls_pk_init( &pk );
625 pk.pk_info = &mbedtls_rsa_info;
626 pk.pk_ctx = rsa;
627
628 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200629 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
630 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200631 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
632 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
633 else
634 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
635
636 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200637 {
638 /* Clean up in case pk_write failed halfway through. */
639 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200640 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200641 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200642
643 /* The mbedtls_pk_xxx functions write to the end of the buffer.
644 * Move the data to the beginning and erase remaining data
645 * at the original location. */
646 if( 2 * (size_t) ret <= data_size )
647 {
648 memcpy( data, data + data_size - ret, ret );
649 memset( data + data_size - ret, 0, ret );
650 }
651 else if( (size_t) ret < data_size )
652 {
653 memmove( data, data + data_size - ret, ret );
654 memset( data + ret, 0, data_size - ret );
655 }
656
657 *data_length = ret;
658 return( PSA_SUCCESS );
659#else
660 (void) type;
661 (void) rsa;
662 (void) data;
663 (void) data_size;
664 (void) data_length;
665 return( PSA_ERROR_NOT_SUPPORTED );
666#endif /* MBEDTLS_PK_WRITE_C */
667}
668
669/** Import an RSA key from import representation to a slot
670 *
671 * \param[in,out] slot The slot where to store the export representation to
672 * \param[in] data The buffer containing the import representation
673 * \param[in] data_length The amount of bytes in \p data
674 */
675static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
676 const uint8_t *data,
677 size_t data_length )
678{
679 psa_status_t status;
680 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200681 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200682
Steven Cooremana01795d2020-07-24 22:48:15 +0200683 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200684 status = psa_load_rsa_representation( slot->attr.type,
685 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200686 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200687 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200688 if( status != PSA_SUCCESS )
689 goto exit;
690
691 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200692 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200693
Steven Cooreman75b74362020-07-28 14:30:13 +0200694 /* Re-export the data to PSA export format, such that we can store export
695 * representation in the key slot. Export representation in case of RSA is
696 * the smallest representation that's allowed as input, so a straight-up
697 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200698 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200699 if( output == NULL )
700 {
701 status = PSA_ERROR_INSUFFICIENT_MEMORY;
702 goto exit;
703 }
704
Steven Cooremana01795d2020-07-24 22:48:15 +0200705 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200706 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200707 output,
708 data_length,
709 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200710exit:
711 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200712 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200713 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200714
715 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000716 if( status != PSA_SUCCESS )
717 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200718 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000719 return( status );
720 }
721
Steven Cooremana01795d2020-07-24 22:48:15 +0200722 /* On success, store the allocated export-formatted key. */
723 slot->data.key.data = output;
724 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000725
726 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200727}
John Durkop6ba40d12020-11-10 08:50:04 -0800728
729#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800730 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200731
John Durkop9814fa22020-11-04 12:28:15 -0800732#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800733 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
734 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
735 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
736 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Steven Cooreman7f391872020-07-30 14:57:44 +0200737/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200738 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200739 * \param[in] type The type of key contained in \p data.
740 * \param[in] data The buffer from which to load the representation.
741 * \param[in] data_length The size in bytes of \p data.
742 * \param[out] p_ecp Returns a pointer to an ECP context on success.
743 * The caller is responsible for freeing both the
744 * contents of the context and the context itself
745 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200746 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200747static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
748 const uint8_t *data,
749 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200750 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100751{
752 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200753 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200754 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200755 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100756
Steven Cooreman4fed4552020-08-03 14:46:03 +0200757 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
758 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100759 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200760 /* A Weierstrass public key is represented as:
761 * - The byte 0x04;
762 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
763 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200764 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200765 */
766 if( ( data_length & 1 ) == 0 )
767 return( PSA_ERROR_INVALID_ARGUMENT );
768 curve_size = data_length / 2;
769
770 /* Montgomery public keys are represented in compressed format, meaning
771 * their curve_size is equal to the amount of input. */
772
773 /* Private keys are represented in uncompressed private random integer
774 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100775 }
776
Steven Cooreman6d839f02020-07-30 11:36:45 +0200777 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200778 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200779 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200780 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200781 mbedtls_ecp_keypair_init( ecp );
782
Gilles Peskine4cd32772019-12-02 20:49:42 +0100783 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200784 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
785 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100786 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200787 {
788 status = PSA_ERROR_INVALID_ARGUMENT;
789 goto exit;
790 }
791
Steven Cooremanacda8342020-07-24 23:09:52 +0200792 status = mbedtls_to_psa_error(
793 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
794 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200795 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200796
Steven Cooreman6d839f02020-07-30 11:36:45 +0200797 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200798 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200799 {
800 /* Load the public value. */
801 status = mbedtls_to_psa_error(
802 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200803 data,
804 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200805 if( status != PSA_SUCCESS )
806 goto exit;
807
808 /* Check that the point is on the curve. */
809 status = mbedtls_to_psa_error(
810 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
811 if( status != PSA_SUCCESS )
812 goto exit;
813 }
814 else
815 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200816 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200817 status = mbedtls_to_psa_error(
818 mbedtls_ecp_read_key( ecp->grp.id,
819 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200820 data,
821 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200822 if( status != PSA_SUCCESS )
823 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200824 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200825
826 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200827exit:
828 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200829 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200830 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200831 mbedtls_free( ecp );
832 }
833
Steven Cooreman29149862020-08-05 15:43:42 +0200834 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100835}
John Durkop6ba40d12020-11-10 08:50:04 -0800836#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
837 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
838 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
839 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
840 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000841
John Durkop6ba40d12020-11-10 08:50:04 -0800842#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
843 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200844/** Export an ECP key to export representation
845 *
846 * \param[in] type The type of key (public/private) to export
847 * \param[in] ecp The internal ECP representation from which to export
848 * \param[out] data The buffer to export to
849 * \param[in] data_size The length of the buffer to export to
850 * \param[out] data_length The amount of bytes written to \p data
851 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200852static psa_status_t psa_export_ecp_key( psa_key_type_t type,
853 mbedtls_ecp_keypair *ecp,
854 uint8_t *data,
855 size_t data_size,
856 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200857{
Steven Cooremanacda8342020-07-24 23:09:52 +0200858 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000859
Steven Cooremanacda8342020-07-24 23:09:52 +0200860 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200861 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200862 /* Check whether the public part is loaded */
863 if( mbedtls_ecp_is_zero( &ecp->Q ) )
864 {
865 /* Calculate the public key */
866 status = mbedtls_to_psa_error(
867 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine30524eb2020-11-13 17:02:26 +0100868 mbedtls_psa_get_random, mbedtls_psa_random_state( &global_data.rng ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200869 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200870 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200871 }
872
Steven Cooreman4fed4552020-08-03 14:46:03 +0200873 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200874 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
875 MBEDTLS_ECP_PF_UNCOMPRESSED,
876 data_length,
877 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200878 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200879 if( status != PSA_SUCCESS )
880 memset( data, 0, data_size );
881
Steven Cooreman29149862020-08-05 15:43:42 +0200882 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200883 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200884 else
885 {
Steven Cooreman29149862020-08-05 15:43:42 +0200886 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200887 return( PSA_ERROR_BUFFER_TOO_SMALL );
888
889 status = mbedtls_to_psa_error(
890 mbedtls_ecp_write_key( ecp,
891 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200892 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200893 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200894 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200895 else
896 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200897
898 return( status );
899 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200900}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200901
Steven Cooreman4fed4552020-08-03 14:46:03 +0200902/** Import an ECP key from import representation to a slot
903 *
904 * \param[in,out] slot The slot where to store the export representation to
905 * \param[in] data The buffer containing the import representation
906 * \param[in] data_length The amount of bytes in \p data
907 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200908static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
909 const uint8_t *data,
910 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100911{
Steven Cooremanacda8342020-07-24 23:09:52 +0200912 psa_status_t status;
913 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200914 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100915
Steven Cooremanacda8342020-07-24 23:09:52 +0200916 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200917 status = psa_load_ecp_representation( slot->attr.type,
918 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200919 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200920 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100921 if( status != PSA_SUCCESS )
922 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100923
Steven Cooremanacda8342020-07-24 23:09:52 +0200924 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200925 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200926 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200927 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200928
Steven Cooremanacda8342020-07-24 23:09:52 +0200929 /* Re-export the data to PSA export format. There is currently no support
930 * for other input formats then the export format, so this is a 1-1
931 * copy operation. */
932 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200933 if( output == NULL )
934 {
935 status = PSA_ERROR_INSUFFICIENT_MEMORY;
936 goto exit;
937 }
938
939 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200940 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200941 output,
942 data_length,
943 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100944exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200945 /* Always free the PK object (will also free contained ECP context) */
946 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200947 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200948
949 /* Free the allocated buffer only on error. */
950 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100951 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200952 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200953 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100954 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200955
956 /* On success, store the allocated export-formatted key. */
957 slot->data.key.data = output;
958 slot->data.key.bytes = data_length;
959
960 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100961}
John Durkop9814fa22020-11-04 12:28:15 -0800962#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
963 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100964
Gilles Peskineb46bef22019-07-30 21:32:04 +0200965/** Return the size of the key in the given slot, in bits.
966 *
967 * \param[in] slot A key slot.
968 *
969 * \return The key size in bits, read from the metadata in the slot.
970 */
971static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
972{
973 return( slot->attr.bits );
974}
975
Steven Cooreman75b74362020-07-28 14:30:13 +0200976/** Try to allocate a buffer to an empty key slot.
977 *
978 * \param[in,out] slot Key slot to attach buffer to.
979 * \param[in] buffer_length Requested size of the buffer.
980 *
981 * \retval #PSA_SUCCESS
982 * The buffer has been successfully allocated.
983 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
984 * Not enough memory was available for allocation.
985 * \retval #PSA_ERROR_ALREADY_EXISTS
986 * Trying to allocate a buffer to a non-empty key slot.
987 */
988static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
989 size_t buffer_length )
990{
991 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200992 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200993
994 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
995 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200996 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200997
998 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200999 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001000}
1001
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001002psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
1003 const uint8_t* data,
1004 size_t data_length )
1005{
1006 psa_status_t status = psa_allocate_buffer_to_slot( slot,
1007 data_length );
1008 if( status != PSA_SUCCESS )
1009 return( status );
1010
1011 memcpy( slot->data.key.data, data, data_length );
1012 return( PSA_SUCCESS );
1013}
1014
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001015/** Import key data into a slot.
1016 *
1017 * `slot->type` must have been set previously.
1018 * This function assumes that the slot does not contain any key material yet.
1019 * On failure, the slot content is unchanged.
1020 *
1021 * Persistent storage is not affected.
1022 *
1023 * \param[in,out] slot The key slot to import data into.
1024 * Its `type` field must have previously been set to
1025 * the desired key type.
1026 * It must not contain any key material yet.
1027 * \param[in] data Buffer containing the key material to parse and import.
1028 * \param data_length Size of \p data in bytes.
1029 *
1030 * \retval #PSA_SUCCESS
1031 * \retval #PSA_ERROR_INVALID_ARGUMENT
1032 * \retval #PSA_ERROR_NOT_SUPPORTED
1033 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1034 */
1035static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1036 const uint8_t *data,
1037 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001038{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001039 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001040 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001041
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001042 /* zero-length keys are never supported. */
1043 if( data_length == 0 )
1044 return( PSA_ERROR_NOT_SUPPORTED );
1045
Gilles Peskine8e338702019-07-30 20:06:31 +02001046 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001047 {
Steven Cooreman04524762020-10-13 17:43:44 +02001048 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001049
Steven Cooreman75b74362020-07-28 14:30:13 +02001050 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1051 if( data_length > SIZE_MAX / 8 )
1052 return( PSA_ERROR_NOT_SUPPORTED );
1053
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001054 /* Enforce a size limit, and in particular ensure that the bit
1055 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001056 if( bit_size > PSA_MAX_KEY_BITS )
1057 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001058
1059 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001060 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001061 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001062
1063 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001064 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001065 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001066 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001067
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001068 /* Write the actual key size to the slot.
1069 * psa_start_key_creation() wrote the size declared by the
1070 * caller, which may be 0 (meaning unspecified) or wrong. */
1071 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001072
1073 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001074 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001075 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001076 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001077 /* Try validation through accelerators first. */
1078 bit_size = slot->attr.bits;
1079 psa_key_attributes_t attributes = {
1080 .core = slot->attr
1081 };
1082 status = psa_driver_wrapper_validate_key( &attributes,
1083 data,
1084 data_length,
1085 &bit_size );
1086 if( status == PSA_SUCCESS )
1087 {
1088 /* Key has been validated successfully by an accelerator.
1089 * Copy key material into slot. */
1090 status = psa_copy_key_material_into_slot( slot, data, data_length );
1091 if( status != PSA_SUCCESS )
1092 return( status );
1093
1094 slot->attr.bits = (psa_key_bits_t) bit_size;
1095 return( PSA_SUCCESS );
1096 }
1097 else if( status != PSA_ERROR_NOT_SUPPORTED )
1098 return( status );
1099
1100 /* Key format is not supported by any accelerator, try software fallback
1101 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -08001102#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1103 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001104 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1105 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001106 return( psa_import_ecp_key( slot, data, data_length ) );
1107 }
John Durkop9814fa22020-11-04 12:28:15 -08001108#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1109 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -07001110#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1111 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +01001112 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001113 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001114 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001115 }
John Durkop9814fa22020-11-04 12:28:15 -08001116#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1117 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001118
1119 /* Fell through the fallback as well, so have nothing else to try. */
1120 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001121 }
1122 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001123 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001124 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001125 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001126 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001127}
1128
Gilles Peskinef603c712019-01-19 13:40:11 +01001129/** Calculate the intersection of two algorithm usage policies.
1130 *
1131 * Return 0 (which allows no operation) on incompatibility.
1132 */
1133static psa_algorithm_t psa_key_policy_algorithm_intersection(
1134 psa_algorithm_t alg1,
1135 psa_algorithm_t alg2 )
1136{
Gilles Peskine549ea862019-05-22 11:45:59 +02001137 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001138 if( alg1 == alg2 )
1139 return( alg1 );
1140 /* If the policies are from the same hash-and-sign family, check
1141 * if one is a wildcard. If so the other has the specific algorithm. */
1142 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1143 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1144 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1145 {
1146 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1147 return( alg2 );
1148 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1149 return( alg1 );
1150 }
1151 /* If the policies are incompatible, allow nothing. */
1152 return( 0 );
1153}
1154
Gilles Peskined6f371b2019-05-10 19:33:38 +02001155static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1156 psa_algorithm_t requested_alg )
1157{
Gilles Peskine549ea862019-05-22 11:45:59 +02001158 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001159 if( requested_alg == policy_alg )
1160 return( 1 );
1161 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001162 * and requested_alg is the same hash-and-sign family with any hash,
1163 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001164 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1165 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1166 {
1167 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1168 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1169 }
Steven Cooremance48e852020-10-05 16:02:45 +02001170 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001171 * a key derivation with that key agreement should also be allowed. This
1172 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001173 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1174 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1175 {
1176 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1177 policy_alg );
1178 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001179 /* If it isn't permitted, it's forbidden. */
1180 return( 0 );
1181}
1182
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001183/** Test whether a policy permits an algorithm.
1184 *
1185 * The caller must test usage flags separately.
1186 */
1187static int psa_key_policy_permits( const psa_key_policy_t *policy,
1188 psa_algorithm_t alg )
1189{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001190 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1191 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001192}
1193
Gilles Peskinef603c712019-01-19 13:40:11 +01001194/** Restrict a key policy based on a constraint.
1195 *
1196 * \param[in,out] policy The policy to restrict.
1197 * \param[in] constraint The policy constraint to apply.
1198 *
1199 * \retval #PSA_SUCCESS
1200 * \c *policy contains the intersection of the original value of
1201 * \c *policy and \c *constraint.
1202 * \retval #PSA_ERROR_INVALID_ARGUMENT
1203 * \c *policy and \c *constraint are incompatible.
1204 * \c *policy is unchanged.
1205 */
1206static psa_status_t psa_restrict_key_policy(
1207 psa_key_policy_t *policy,
1208 const psa_key_policy_t *constraint )
1209{
1210 psa_algorithm_t intersection_alg =
1211 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001212 psa_algorithm_t intersection_alg2 =
1213 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001214 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1215 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001216 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1217 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001218 policy->usage &= constraint->usage;
1219 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001220 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001221 return( PSA_SUCCESS );
1222}
1223
Ronald Cron5c522922020-11-14 16:35:34 +01001224/** Get the description of a key given its identifier and policy constraints
1225 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001226 *
Ronald Cron5c522922020-11-14 16:35:34 +01001227 * The key must have allow all the usage flags set in \p usage. If \p alg is
1228 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +01001229 *
Ronald Cron5c522922020-11-14 16:35:34 +01001230 * In case of a persistent key, the function loads the description of the key
1231 * into a key slot if not already done.
1232 *
1233 * On success, the returned key slot is locked. It is the responsibility of
1234 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001235 */
Ronald Cron5c522922020-11-14 16:35:34 +01001236static psa_status_t psa_get_and_lock_key_slot_with_policy(
1237 mbedtls_svc_key_id_t key,
1238 psa_key_slot_t **p_slot,
1239 psa_key_usage_t usage,
1240 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001241{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001242 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1243 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001244
Ronald Cron5c522922020-11-14 16:35:34 +01001245 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001246 if( status != PSA_SUCCESS )
1247 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001248 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001249
1250 /* Enforce that usage policy for the key slot contains all the flags
1251 * required by the usage parameter. There is one exception: public
1252 * keys can always be exported, so we treat public key objects as
1253 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001254 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001255 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001256
1257 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001258 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001259 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001260
1261 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001262 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001263 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001264
Darryl Green06fd18d2018-07-16 11:21:11 +01001265 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001266
1267error:
1268 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001269 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001270
1271 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001272}
Darryl Green940d72c2018-07-13 13:18:51 +01001273
Ronald Cron5c522922020-11-14 16:35:34 +01001274/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001275 *
1276 * A transparent key is a key for which the key material is directly
1277 * available, as opposed to a key in a secure element.
1278 *
Ronald Cron5c522922020-11-14 16:35:34 +01001279 * This is a temporary function to use instead of
1280 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1281 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001282 *
Ronald Cron5c522922020-11-14 16:35:34 +01001283 * On success, the returned key slot is locked. It is the responsibility of the
1284 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001285 */
1286#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001287static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1288 mbedtls_svc_key_id_t key,
1289 psa_key_slot_t **p_slot,
1290 psa_key_usage_t usage,
1291 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001292{
Ronald Cron5c522922020-11-14 16:35:34 +01001293 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1294 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001295 if( status != PSA_SUCCESS )
1296 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001297
Gilles Peskineadad8132019-07-25 11:31:23 +02001298 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001299 {
Ronald Cron5c522922020-11-14 16:35:34 +01001300 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001301 *p_slot = NULL;
1302 return( PSA_ERROR_NOT_SUPPORTED );
1303 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001304
Gilles Peskine28f8f302019-07-24 13:30:31 +02001305 return( PSA_SUCCESS );
1306}
1307#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1308/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001309#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1310 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001311#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1312
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001313/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001314static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001315{
Gilles Peskine73167e12019-07-12 23:44:37 +02001316#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1317 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001318 {
1319 /* No key material to clean. */
1320 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001321 else
1322#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001323 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001324 /* Data pointer will always be either a valid pointer or NULL in an
1325 * initialized slot, so we can just free it. */
1326 if( slot->data.key.data != NULL )
1327 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001328 mbedtls_free( slot->data.key.data );
1329 slot->data.key.data = NULL;
1330 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001331 }
Darryl Green40225ba2018-11-15 14:48:15 +00001332
1333 return( PSA_SUCCESS );
1334}
1335
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001336/** Completely wipe a slot in memory, including its policy.
1337 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001338psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001339{
1340 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001341
1342 /*
1343 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001344 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001345 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1346 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001347 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001348 */
Ronald Cron5c522922020-11-14 16:35:34 +01001349 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001350 {
1351#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001352 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001353#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001354 status = PSA_ERROR_CORRUPTION_DETECTED;
1355 }
1356
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001357 /* Multipart operations may still be using the key. This is safe
1358 * because all multipart operation objects are independent from
1359 * the key slot: if they need to access the key after the setup
1360 * phase, they have a copy of the key. Note that this means that
1361 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001362 /* At this point, key material and other type-specific content has
1363 * been wiped. Clear remaining metadata. We can call memset and not
1364 * zeroize because the metadata is not particularly sensitive. */
1365 memset( slot, 0, sizeof( *slot ) );
1366 return( status );
1367}
1368
Ronald Croncf56a0a2020-08-04 09:51:30 +02001369psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001370{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001371 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001372 psa_status_t status; /* status of the last operation */
1373 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001374#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1375 psa_se_drv_table_entry_t *driver;
1376#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001377
Ronald Croncf56a0a2020-08-04 09:51:30 +02001378 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001379 return( PSA_SUCCESS );
1380
Ronald Cronf2911112020-10-29 17:51:10 +01001381 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001382 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001383 * key, this will load the key description from persistent memory if not
1384 * done yet. We cannot avoid this loading as without it we don't know if
1385 * the key is operated by an SE or not and this information is needed by
1386 * the current implementation.
1387 */
Ronald Cron5c522922020-11-14 16:35:34 +01001388 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001389 if( status != PSA_SUCCESS )
1390 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001391
Ronald Cronf2911112020-10-29 17:51:10 +01001392 /*
1393 * If the key slot containing the key description is under access by the
1394 * library (apart from the present access), the key cannot be destroyed
1395 * yet. For the time being, just return in error. Eventually (to be
1396 * implemented), the key should be destroyed when all accesses have
1397 * stopped.
1398 */
Ronald Cron5c522922020-11-14 16:35:34 +01001399 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001400 {
Ronald Cron5c522922020-11-14 16:35:34 +01001401 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001402 return( PSA_ERROR_GENERIC_ERROR );
1403 }
1404
Gilles Peskine354f7672019-07-12 23:46:38 +02001405#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001406 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001407 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001408 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001409 /* For a key in a secure element, we need to do three things:
1410 * remove the key file in internal storage, destroy the
1411 * key inside the secure element, and update the driver's
1412 * persistent data. Start a transaction that will encompass these
1413 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001414 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001415 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001416 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001417 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001418 status = psa_crypto_save_transaction( );
1419 if( status != PSA_SUCCESS )
1420 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001421 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001422 /* We should still try to destroy the key in the secure
1423 * element and the key metadata in storage. This is especially
1424 * important if the error is that the storage is full.
1425 * But how to do it exactly without risking an inconsistent
1426 * state after a reset?
1427 * https://github.com/ARMmbed/mbed-crypto/issues/215
1428 */
1429 overall_status = status;
1430 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001431 }
1432
Gilles Peskine354f7672019-07-12 23:46:38 +02001433 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001434 if( overall_status == PSA_SUCCESS )
1435 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001436 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001437#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1438
Darryl Greend49a4992018-06-18 17:27:26 +01001439#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001440 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001441 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001442 status = psa_destroy_persistent_key( slot->attr.id );
1443 if( overall_status == PSA_SUCCESS )
1444 overall_status = status;
1445
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001446 /* TODO: other slots may have a copy of the same key. We should
1447 * invalidate them.
1448 * https://github.com/ARMmbed/mbed-crypto/issues/214
1449 */
Darryl Greend49a4992018-06-18 17:27:26 +01001450 }
1451#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001452
Gilles Peskinefc762652019-07-22 19:30:34 +02001453#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1454 if( driver != NULL )
1455 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001456 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001457 if( overall_status == PSA_SUCCESS )
1458 overall_status = status;
1459 status = psa_crypto_stop_transaction( );
1460 if( overall_status == PSA_SUCCESS )
1461 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001462 }
1463#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1464
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001465#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1466exit:
1467#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001468 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001469 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1470 if( overall_status == PSA_SUCCESS )
1471 overall_status = status;
1472 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001473}
1474
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001475void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001476{
Gilles Peskineb699f072019-04-26 16:06:02 +02001477 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001478 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001479}
1480
Gilles Peskineb699f072019-04-26 16:06:02 +02001481psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1482 psa_key_type_t type,
1483 const uint8_t *data,
1484 size_t data_length )
1485{
1486 uint8_t *copy = NULL;
1487
1488 if( data_length != 0 )
1489 {
1490 copy = mbedtls_calloc( 1, data_length );
1491 if( copy == NULL )
1492 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1493 memcpy( copy, data, data_length );
1494 }
1495 /* After this point, this function is guaranteed to succeed, so it
1496 * can start modifying `*attributes`. */
1497
1498 if( attributes->domain_parameters != NULL )
1499 {
1500 mbedtls_free( attributes->domain_parameters );
1501 attributes->domain_parameters = NULL;
1502 attributes->domain_parameters_size = 0;
1503 }
1504
1505 attributes->domain_parameters = copy;
1506 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001507 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001508 return( PSA_SUCCESS );
1509}
1510
1511psa_status_t psa_get_key_domain_parameters(
1512 const psa_key_attributes_t *attributes,
1513 uint8_t *data, size_t data_size, size_t *data_length )
1514{
1515 if( attributes->domain_parameters_size > data_size )
1516 return( PSA_ERROR_BUFFER_TOO_SMALL );
1517 *data_length = attributes->domain_parameters_size;
1518 if( attributes->domain_parameters_size != 0 )
1519 memcpy( data, attributes->domain_parameters,
1520 attributes->domain_parameters_size );
1521 return( PSA_SUCCESS );
1522}
1523
John Durkop07cc04a2020-11-16 22:08:34 -08001524#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001525 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001526static psa_status_t psa_get_rsa_public_exponent(
1527 const mbedtls_rsa_context *rsa,
1528 psa_key_attributes_t *attributes )
1529{
1530 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001532 uint8_t *buffer = NULL;
1533 size_t buflen;
1534 mbedtls_mpi_init( &mpi );
1535
1536 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1537 if( ret != 0 )
1538 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001539 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1540 {
1541 /* It's the default value, which is reported as an empty string,
1542 * so there's nothing to do. */
1543 goto exit;
1544 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001545
1546 buflen = mbedtls_mpi_size( &mpi );
1547 buffer = mbedtls_calloc( 1, buflen );
1548 if( buffer == NULL )
1549 {
1550 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1551 goto exit;
1552 }
1553 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1554 if( ret != 0 )
1555 goto exit;
1556 attributes->domain_parameters = buffer;
1557 attributes->domain_parameters_size = buflen;
1558
1559exit:
1560 mbedtls_mpi_free( &mpi );
1561 if( ret != 0 )
1562 mbedtls_free( buffer );
1563 return( mbedtls_to_psa_error( ret ) );
1564}
John Durkop07cc04a2020-11-16 22:08:34 -08001565#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001566 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001567
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001568/** Retrieve all the publicly-accessible attributes of a key.
1569 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001570psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001571 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001572{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001573 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001574 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001575 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001576
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001577 psa_reset_key_attributes( attributes );
1578
Ronald Cron5c522922020-11-14 16:35:34 +01001579 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001580 if( status != PSA_SUCCESS )
1581 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001582
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001583 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001584 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1585 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1586
1587#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1588 if( psa_key_slot_is_external( slot ) )
1589 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1590#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001591
Gilles Peskine8e338702019-07-30 20:06:31 +02001592 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001593 {
John Durkop07cc04a2020-11-16 22:08:34 -08001594#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001595 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001596 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001597 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001598#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001599 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001600 * is not yet implemented.
1601 * https://github.com/ARMmbed/mbed-crypto/issues/216
1602 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001603 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001604 break;
1605#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001606 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001607 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001608
Steven Cooreman4fed4552020-08-03 14:46:03 +02001609 status = psa_load_rsa_representation( slot->attr.type,
1610 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001611 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001612 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001613 if( status != PSA_SUCCESS )
1614 break;
1615
Steven Cooremana2371e52020-07-28 14:30:39 +02001616 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001617 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001618 mbedtls_rsa_free( rsa );
1619 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001620 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001621 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001622#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001623 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001624 default:
1625 /* Nothing else to do. */
1626 break;
1627 }
1628
1629 if( status != PSA_SUCCESS )
1630 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001631
Ronald Cron5c522922020-11-14 16:35:34 +01001632 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001633
Ronald Cron5c522922020-11-14 16:35:34 +01001634 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001635}
1636
Gilles Peskinec8000c02019-08-02 20:15:51 +02001637#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1638psa_status_t psa_get_key_slot_number(
1639 const psa_key_attributes_t *attributes,
1640 psa_key_slot_number_t *slot_number )
1641{
1642 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1643 {
1644 *slot_number = attributes->slot_number;
1645 return( PSA_SUCCESS );
1646 }
1647 else
1648 return( PSA_ERROR_INVALID_ARGUMENT );
1649}
1650#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1651
Steven Cooremana01795d2020-07-24 22:48:15 +02001652static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1653 uint8_t *data,
1654 size_t data_size,
1655 size_t *data_length )
1656{
1657 if( slot->data.key.bytes > data_size )
1658 return( PSA_ERROR_BUFFER_TOO_SMALL );
1659 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1660 memset( data + slot->data.key.bytes, 0,
1661 data_size - slot->data.key.bytes );
1662 *data_length = slot->data.key.bytes;
1663 return( PSA_SUCCESS );
1664}
1665
Gilles Peskinef603c712019-01-19 13:40:11 +01001666static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1667 uint8_t *data,
1668 size_t data_size,
1669 size_t *data_length,
1670 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001671{
Gilles Peskine5d309672019-07-12 23:47:28 +02001672#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1673 const psa_drv_se_t *drv;
1674 psa_drv_se_context_t *drv_context;
1675#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1676
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001677 *data_length = 0;
1678
Gilles Peskine8e338702019-07-30 20:06:31 +02001679 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001680 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001681
Gilles Peskinef9168942019-09-12 19:20:29 +02001682 /* Reject a zero-length output buffer now, since this can never be a
1683 * valid key representation. This way we know that data must be a valid
1684 * pointer and we can do things like memset(data, ..., data_size). */
1685 if( data_size == 0 )
1686 return( PSA_ERROR_BUFFER_TOO_SMALL );
1687
Gilles Peskine5d309672019-07-12 23:47:28 +02001688#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001689 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001690 {
1691 psa_drv_se_export_key_t method;
1692 if( drv->key_management == NULL )
1693 return( PSA_ERROR_NOT_SUPPORTED );
1694 method = ( export_public_key ?
1695 drv->key_management->p_export_public :
1696 drv->key_management->p_export );
1697 if( method == NULL )
1698 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001699 return( method( drv_context,
1700 slot->data.se.slot_number,
1701 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001702 }
1703#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1704
Gilles Peskine8e338702019-07-30 20:06:31 +02001705 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001706 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001707 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001708 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001709 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1710 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001711 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001712 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001713 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001714 /* Exporting public -> public */
1715 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1716 }
1717 else if( !export_public_key )
1718 {
1719 /* Exporting private -> private */
1720 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1721 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001722
Steven Cooreman560c28a2020-07-24 23:20:24 +02001723 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001724 * so conversion is needed. Try the accelerators first. */
1725 psa_status_t status = psa_driver_wrapper_export_public_key( slot,
1726 data,
1727 data_size,
1728 data_length );
1729
1730 if( status != PSA_ERROR_NOT_SUPPORTED ||
1731 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1732 return( status );
1733
Steven Cooreman560c28a2020-07-24 23:20:24 +02001734 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1735 {
John Durkop0e005192020-10-31 22:06:54 -07001736#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1737 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001738 mbedtls_rsa_context *rsa = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001739 status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001740 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001741 slot->data.key.data,
1742 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001743 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001744 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001745 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001746
Steven Cooreman560c28a2020-07-24 23:20:24 +02001747 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001748 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001749 data,
1750 data_size,
1751 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001752
Steven Cooremana2371e52020-07-28 14:30:39 +02001753 mbedtls_rsa_free( rsa );
1754 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001755
Steven Cooreman560c28a2020-07-24 23:20:24 +02001756 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001757#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001758 /* We don't know how to convert a private RSA key to public. */
1759 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001760#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1761 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001762 }
1763 else
Moran Pekera998bc62018-04-16 18:16:20 +03001764 {
John Durkop6ba40d12020-11-10 08:50:04 -08001765#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1766 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001767 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001768 status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001769 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001770 slot->data.key.data,
1771 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001772 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001773 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001774 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001775
1776 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1777 PSA_KEY_TYPE_ECC_GET_FAMILY(
1778 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001779 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001780 data,
1781 data_size,
1782 data_length );
1783
Steven Cooremana2371e52020-07-28 14:30:39 +02001784 mbedtls_ecp_keypair_free( ecp );
1785 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001786 return( status );
1787#else
1788 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001789 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001790#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1791 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001792 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001793 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001794 else
1795 {
1796 /* This shouldn't happen in the reference implementation, but
1797 it is valid for a special-purpose implementation to omit
1798 support for exporting certain key types. */
1799 return( PSA_ERROR_NOT_SUPPORTED );
1800 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001801}
1802
Ronald Croncf56a0a2020-08-04 09:51:30 +02001803psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001804 uint8_t *data,
1805 size_t data_size,
1806 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001807{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001808 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001809 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001810 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001811
1812 /* Set the key to empty now, so that even when there are errors, we always
1813 * set data_length to a value between 0 and data_size. On error, setting
1814 * the key to empty is a good choice because an empty key representation is
1815 * unlikely to be accepted anywhere. */
1816 *data_length = 0;
1817
1818 /* Export requires the EXPORT flag. There is an exception for public keys,
Ronald Cron5c522922020-11-14 16:35:34 +01001819 * which don't require any flag, but
1820 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1821 */
1822 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1823 PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001824 if( status != PSA_SUCCESS )
1825 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001826
1827 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
Ronald Cron5c522922020-11-14 16:35:34 +01001828 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001829
Ronald Cron5c522922020-11-14 16:35:34 +01001830 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001831}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001832
Ronald Croncf56a0a2020-08-04 09:51:30 +02001833psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001834 uint8_t *data,
1835 size_t data_size,
1836 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001837{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001838 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001839 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001840 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001841
1842 /* Set the key to empty now, so that even when there are errors, we always
1843 * set data_length to a value between 0 and data_size. On error, setting
1844 * the key to empty is a good choice because an empty key representation is
1845 * unlikely to be accepted anywhere. */
1846 *data_length = 0;
1847
1848 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001849 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001850 if( status != PSA_SUCCESS )
1851 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001852
1853 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
Ronald Cron5c522922020-11-14 16:35:34 +01001854 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001855
Ronald Cron5c522922020-11-14 16:35:34 +01001856 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001857}
1858
Gilles Peskine91e8c332019-08-02 19:19:39 +02001859#if defined(static_assert)
1860static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1861 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001862static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001863 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001864static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001865 "One or more key attribute flag is listed as both internal-only and external-only" );
1866#endif
1867
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001868/** Validate that a key policy is internally well-formed.
1869 *
1870 * This function only rejects invalid policies. It does not validate the
1871 * consistency of the policy with respect to other attributes of the key
1872 * such as the key type.
1873 */
1874static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001875{
1876 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001877 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001878 PSA_KEY_USAGE_ENCRYPT |
1879 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001880 PSA_KEY_USAGE_SIGN_HASH |
1881 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001882 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1883 return( PSA_ERROR_INVALID_ARGUMENT );
1884
Gilles Peskine4747d192019-04-17 15:05:45 +02001885 return( PSA_SUCCESS );
1886}
1887
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001888/** Validate the internal consistency of key attributes.
1889 *
1890 * This function only rejects invalid attribute values. If does not
1891 * validate the consistency of the attributes with any key data that may
1892 * be involved in the creation of the key.
1893 *
1894 * Call this function early in the key creation process.
1895 *
1896 * \param[in] attributes Key attributes for the new key.
1897 * \param[out] p_drv On any return, the driver for the key, if any.
1898 * NULL for a transparent key.
1899 *
1900 */
1901static psa_status_t psa_validate_key_attributes(
1902 const psa_key_attributes_t *attributes,
1903 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001904{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001905 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001906 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001907 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001908
Ronald Cron54b90082020-10-29 15:26:43 +01001909 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001910 if( status != PSA_SUCCESS )
1911 return( status );
1912
Ronald Crond2ed4812020-07-17 16:11:30 +02001913 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001914 if( status != PSA_SUCCESS )
1915 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001916
Ronald Cron65f38a32020-10-23 17:11:13 +02001917 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1918 {
1919 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1920 return( PSA_ERROR_INVALID_ARGUMENT );
1921 }
1922 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001923 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001924 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001925 if( status != PSA_SUCCESS )
1926 return( status );
1927 }
1928
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001929 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001930 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001931 return( status );
1932
1933 /* Refuse to create overly large keys.
1934 * Note that this doesn't trigger on import if the attributes don't
1935 * explicitly specify a size (so psa_get_key_bits returns 0), so
1936 * psa_import_key() needs its own checks. */
1937 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1938 return( PSA_ERROR_NOT_SUPPORTED );
1939
Gilles Peskine91e8c332019-08-02 19:19:39 +02001940 /* Reject invalid flags. These should not be reachable through the API. */
1941 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1942 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1943 return( PSA_ERROR_INVALID_ARGUMENT );
1944
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001945 return( PSA_SUCCESS );
1946}
1947
Gilles Peskine4747d192019-04-17 15:05:45 +02001948/** Prepare a key slot to receive key material.
1949 *
1950 * This function allocates a key slot and sets its metadata.
1951 *
1952 * If this function fails, call psa_fail_key_creation().
1953 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001954 * This function is intended to be used as follows:
1955 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001956 * it with the specified attributes, and in case of a volatile key assign it
1957 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001958 * -# Populate the slot with the key material.
1959 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1960 * In case of failure at any step, stop the sequence and call
1961 * psa_fail_key_creation().
1962 *
Ronald Cron5c522922020-11-14 16:35:34 +01001963 * On success, the key slot is locked. It is the responsibility of the caller
1964 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001965 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001966 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001967 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001968 * \param[out] p_slot On success, a pointer to the prepared slot.
1969 * \param[out] p_drv On any return, the driver for the key, if any.
1970 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001971 *
1972 * \retval #PSA_SUCCESS
1973 * The key slot is ready to receive key material.
1974 * \return If this function fails, the key slot is an invalid state.
1975 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001976 */
1977static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001978 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001979 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001980 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001981 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001982{
1983 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001984 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001985 psa_key_slot_t *slot;
1986
Gilles Peskinedf179142019-07-15 22:02:14 +02001987 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001988 *p_drv = NULL;
1989
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001990 status = psa_validate_key_attributes( attributes, p_drv );
1991 if( status != PSA_SUCCESS )
1992 return( status );
1993
Ronald Cronc4d1b512020-07-31 11:26:37 +02001994 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001995 if( status != PSA_SUCCESS )
1996 return( status );
1997 slot = *p_slot;
1998
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001999 /* We're storing the declared bit-size of the key. It's up to each
2000 * creation mechanism to verify that this information is correct.
2001 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02002002 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02002003 * is optional (import, copy). In case of a volatile key, assign it the
2004 * volatile key identifier associated to the slot returned to contain its
2005 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002006
2007 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002008 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
2009 {
2010#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2011 slot->attr.id = volatile_key_id;
2012#else
2013 slot->attr.id.key_id = volatile_key_id;
2014#endif
2015 }
Gilles Peskinec744d992019-07-30 17:26:54 +02002016
Gilles Peskine91e8c332019-08-02 19:19:39 +02002017 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02002018 * external-only flags, query `attributes`. Thanks to the check
2019 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02002020 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02002021 * may have set. */
2022 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02002023
Gilles Peskinecbaff462019-07-12 23:46:04 +02002024#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002025 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002026 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02002027 * create the key file in internal storage, create the
2028 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002029 * persistent data. This is done by starting a transaction that will
2030 * encompass these three actions.
2031 * For registering a volatile key, we just need to find an appropriate
2032 * slot number inside the SE. Since the key is designated volatile, creating
2033 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02002034 /* The first thing to do is to find a slot number for the new key.
2035 * We save the slot number in persistent storage as part of the
2036 * transaction data. It will be needed to recover if the power
2037 * fails during the key creation process, to clean up on the secure
2038 * element side after restarting. Obtaining a slot number from the
2039 * secure element driver updates its persistent state, but we do not yet
2040 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02002041 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002042 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00002043 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02002044 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02002045 &slot->data.se.slot_number );
2046 if( status != PSA_SUCCESS )
2047 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002048
2049 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002050 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002051 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2052 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
2053 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
2054 psa_crypto_transaction.key.id = slot->attr.id;
2055 status = psa_crypto_save_transaction( );
2056 if( status != PSA_SUCCESS )
2057 {
2058 (void) psa_crypto_stop_transaction( );
2059 return( status );
2060 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002061 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002062 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002063
2064 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2065 {
2066 /* Key registration only makes sense with a secure element. */
2067 return( PSA_ERROR_INVALID_ARGUMENT );
2068 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002069#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2070
Ronald Cronc4d1b512020-07-31 11:26:37 +02002071 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002072}
Gilles Peskine4747d192019-04-17 15:05:45 +02002073
2074/** Finalize the creation of a key once its key material has been set.
2075 *
2076 * This entails writing the key to persistent storage.
2077 *
2078 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002079 * See the documentation of psa_start_key_creation() for the intended use
2080 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002081 *
Ronald Cron5c522922020-11-14 16:35:34 +01002082 * If the finalization succeeds, the function unlocks the key slot (it was
2083 * locked by psa_start_key_creation()) and the key slot cannot be accessed
2084 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01002085 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002086 * \param[in,out] slot Pointer to the slot with key material.
2087 * \param[in] driver The secure element driver for the key,
2088 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01002089 * \param[out] key On success, identifier of the key. Note that the
2090 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002091 *
2092 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002093 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002094 * \return If this function fails, the key slot is an invalid state.
2095 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002096 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002097static psa_status_t psa_finish_key_creation(
2098 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002099 psa_se_drv_table_entry_t *driver,
2100 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002101{
2102 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002103 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002104 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002105
2106#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002107 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002108 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002109#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2110 if( driver != NULL )
2111 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002112 psa_se_key_data_storage_t data;
2113#if defined(static_assert)
2114 static_assert( sizeof( slot->data.se.slot_number ) ==
2115 sizeof( data.slot_number ),
2116 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002117#endif
2118 memcpy( &data.slot_number, &slot->data.se.slot_number,
2119 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002120 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002121 (uint8_t*) &data,
2122 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002123 }
2124 else
2125#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2126 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002127 /* Key material is saved in export representation in the slot, so
2128 * just pass the slot buffer for storage. */
2129 status = psa_save_persistent_key( &slot->attr,
2130 slot->data.key.data,
2131 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002132 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002133 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002134#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2135
Gilles Peskinecbaff462019-07-12 23:46:04 +02002136#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002137 /* Finish the transaction for a key creation. This does not
2138 * happen when registering an existing key. Detect this case
2139 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002140 * creation of a persistent key in a secure element requires a transaction,
2141 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002142 if( driver != NULL &&
2143 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002144 {
2145 status = psa_save_se_persistent_data( driver );
2146 if( status != PSA_SUCCESS )
2147 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002148 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002149 return( status );
2150 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002151 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002152 }
2153#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2154
Ronald Cron50972942020-11-14 11:28:25 +01002155 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002156 {
2157 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002158 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002159 if( status != PSA_SUCCESS )
2160 *key = MBEDTLS_SVC_KEY_ID_INIT;
2161 }
Ronald Cron50972942020-11-14 11:28:25 +01002162
Gilles Peskine4747d192019-04-17 15:05:45 +02002163 return( status );
2164}
2165
2166/** Abort the creation of a key.
2167 *
2168 * You may call this function after calling psa_start_key_creation(),
2169 * or after psa_finish_key_creation() fails. In other circumstances, this
2170 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002171 * See the documentation of psa_start_key_creation() for the intended use
2172 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002173 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002174 * \param[in,out] slot Pointer to the slot with key material.
2175 * \param[in] driver The secure element driver for the key,
2176 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002177 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002178static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002179 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002180{
Gilles Peskine011e4282019-06-26 18:34:38 +02002181 (void) driver;
2182
Gilles Peskine4747d192019-04-17 15:05:45 +02002183 if( slot == NULL )
2184 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002185
2186#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002187 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002188 * element, and the failure happened later (when saving metadata
2189 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002190 * element.
2191 * https://github.com/ARMmbed/mbed-crypto/issues/217
2192 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002193
Gilles Peskined7729582019-08-05 15:55:54 +02002194 /* Abort the ongoing transaction if any (there may not be one if
2195 * the creation process failed before starting one, or if the
2196 * key creation is a registration of a key in a secure element).
2197 * Earlier functions must already have done what it takes to undo any
2198 * partial creation. All that's left is to update the transaction data
2199 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002200 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002201#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2202
Gilles Peskine4747d192019-04-17 15:05:45 +02002203 psa_wipe_key_slot( slot );
2204}
2205
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002206/** Validate optional attributes during key creation.
2207 *
2208 * Some key attributes are optional during key creation. If they are
2209 * specified in the attributes structure, check that they are consistent
2210 * with the data in the slot.
2211 *
2212 * This function should be called near the end of key creation, after
2213 * the slot in memory is fully populated but before saving persistent data.
2214 */
2215static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002216 const psa_key_slot_t *slot,
2217 const psa_key_attributes_t *attributes )
2218{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002219 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002220 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002221 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002222 return( PSA_ERROR_INVALID_ARGUMENT );
2223 }
2224
2225 if( attributes->domain_parameters_size != 0 )
2226 {
John Durkop0e005192020-10-31 22:06:54 -07002227#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2228 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002229 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002230 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002231 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002232 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002234
Steven Cooreman7f391872020-07-30 14:57:44 +02002235 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002236 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002237 slot->data.key.data,
2238 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002239 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002240 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002241 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002242
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002243 mbedtls_mpi_init( &actual );
2244 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002245 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002246 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002247 mbedtls_rsa_free( rsa );
2248 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002249 if( ret != 0 )
2250 goto rsa_exit;
2251 ret = mbedtls_mpi_read_binary( &required,
2252 attributes->domain_parameters,
2253 attributes->domain_parameters_size );
2254 if( ret != 0 )
2255 goto rsa_exit;
2256 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2257 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2258 rsa_exit:
2259 mbedtls_mpi_free( &actual );
2260 mbedtls_mpi_free( &required );
2261 if( ret != 0)
2262 return( mbedtls_to_psa_error( ret ) );
2263 }
2264 else
John Durkop9814fa22020-11-04 12:28:15 -08002265#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2266 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002267 {
2268 return( PSA_ERROR_INVALID_ARGUMENT );
2269 }
2270 }
2271
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002272 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002273 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002274 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002275 return( PSA_ERROR_INVALID_ARGUMENT );
2276 }
2277
2278 return( PSA_SUCCESS );
2279}
2280
Gilles Peskine4747d192019-04-17 15:05:45 +02002281psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002282 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002283 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002284 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002285{
2286 psa_status_t status;
2287 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002288 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002289
Ronald Cron81709fc2020-11-14 12:10:32 +01002290 *key = MBEDTLS_SVC_KEY_ID_INIT;
2291
Gilles Peskine0f84d622019-09-12 19:03:13 +02002292 /* Reject zero-length symmetric keys (including raw data key objects).
2293 * This also rejects any key which might be encoded as an empty string,
2294 * which is never valid. */
2295 if( data_length == 0 )
2296 return( PSA_ERROR_INVALID_ARGUMENT );
2297
Gilles Peskinedf179142019-07-15 22:02:14 +02002298 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002299 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002300 if( status != PSA_SUCCESS )
2301 goto exit;
2302
Gilles Peskine5d309672019-07-12 23:47:28 +02002303#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2304 if( driver != NULL )
2305 {
2306 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002307 /* The driver should set the number of key bits, however in
2308 * case it doesn't, we initialize bits to an invalid value. */
2309 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002310 if( drv->key_management == NULL ||
2311 drv->key_management->p_import == NULL )
2312 {
2313 status = PSA_ERROR_NOT_SUPPORTED;
2314 goto exit;
2315 }
2316 status = drv->key_management->p_import(
2317 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002318 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002319 &bits );
2320 if( status != PSA_SUCCESS )
2321 goto exit;
2322 if( bits > PSA_MAX_KEY_BITS )
2323 {
2324 status = PSA_ERROR_NOT_SUPPORTED;
2325 goto exit;
2326 }
2327 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002328 }
2329 else
2330#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2331 {
2332 status = psa_import_key_into_slot( slot, data, data_length );
2333 if( status != PSA_SUCCESS )
2334 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002335 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002336 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002337 if( status != PSA_SUCCESS )
2338 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002339
Ronald Cron81709fc2020-11-14 12:10:32 +01002340 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002341exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002342 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002343 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002344
Gilles Peskine4747d192019-04-17 15:05:45 +02002345 return( status );
2346}
2347
Gilles Peskined7729582019-08-05 15:55:54 +02002348#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2349psa_status_t mbedtls_psa_register_se_key(
2350 const psa_key_attributes_t *attributes )
2351{
2352 psa_status_t status;
2353 psa_key_slot_t *slot = NULL;
2354 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002355 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002356
2357 /* Leaving attributes unspecified is not currently supported.
2358 * It could make sense to query the key type and size from the
2359 * secure element, but not all secure elements support this
2360 * and the driver HAL doesn't currently support it. */
2361 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2362 return( PSA_ERROR_NOT_SUPPORTED );
2363 if( psa_get_key_bits( attributes ) == 0 )
2364 return( PSA_ERROR_NOT_SUPPORTED );
2365
2366 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002367 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002368 if( status != PSA_SUCCESS )
2369 goto exit;
2370
Ronald Cron81709fc2020-11-14 12:10:32 +01002371 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002372
2373exit:
2374 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002375 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002376
Gilles Peskined7729582019-08-05 15:55:54 +02002377 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002378 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002379 return( status );
2380}
2381#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2382
Gilles Peskinef603c712019-01-19 13:40:11 +01002383static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002384 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002385{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002386 psa_status_t status = psa_copy_key_material_into_slot( target,
2387 source->data.key.data,
2388 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002389 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002390 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002391
Steven Cooreman398aee52020-10-13 14:35:45 +02002392 target->attr.type = source->attr.type;
2393 target->attr.bits = source->attr.bits;
2394
2395 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002396}
2397
Ronald Croncf56a0a2020-08-04 09:51:30 +02002398psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002399 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002400 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002401{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002402 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002403 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002404 psa_key_slot_t *source_slot = NULL;
2405 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002406 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002407 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002408
Ronald Cron81709fc2020-11-14 12:10:32 +01002409 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2410
Ronald Cron5c522922020-11-14 16:35:34 +01002411 status = psa_get_and_lock_transparent_key_slot_with_policy(
2412 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002413 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002414 goto exit;
2415
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002416 status = psa_validate_optional_attributes( source_slot,
2417 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002418 if( status != PSA_SUCCESS )
2419 goto exit;
2420
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002421 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002422 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002423 if( status != PSA_SUCCESS )
2424 goto exit;
2425
Ronald Cron81709fc2020-11-14 12:10:32 +01002426 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2427 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002428 if( status != PSA_SUCCESS )
2429 goto exit;
2430
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002431#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2432 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002433 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002434 /* Copying to a secure element is not implemented yet. */
2435 status = PSA_ERROR_NOT_SUPPORTED;
2436 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002437 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002438#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002439
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002440 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002441 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002442 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002443
Ronald Cron81709fc2020-11-14 12:10:32 +01002444 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002445exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002446 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002447 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002448
Ronald Cron5c522922020-11-14 16:35:34 +01002449 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002450
Ronald Cron5c522922020-11-14 16:35:34 +01002451 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002452}
2453
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002454
2455
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002456/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002457/* Message digests */
2458/****************************************************************/
2459
John Durkop07cc04a2020-11-16 22:08:34 -08002460#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002461 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2462 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002463 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002464static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002465{
2466 switch( alg )
2467 {
2468#if defined(MBEDTLS_MD2_C)
2469 case PSA_ALG_MD2:
2470 return( &mbedtls_md2_info );
2471#endif
2472#if defined(MBEDTLS_MD4_C)
2473 case PSA_ALG_MD4:
2474 return( &mbedtls_md4_info );
2475#endif
2476#if defined(MBEDTLS_MD5_C)
2477 case PSA_ALG_MD5:
2478 return( &mbedtls_md5_info );
2479#endif
2480#if defined(MBEDTLS_RIPEMD160_C)
2481 case PSA_ALG_RIPEMD160:
2482 return( &mbedtls_ripemd160_info );
2483#endif
2484#if defined(MBEDTLS_SHA1_C)
2485 case PSA_ALG_SHA_1:
2486 return( &mbedtls_sha1_info );
2487#endif
2488#if defined(MBEDTLS_SHA256_C)
2489 case PSA_ALG_SHA_224:
2490 return( &mbedtls_sha224_info );
2491 case PSA_ALG_SHA_256:
2492 return( &mbedtls_sha256_info );
2493#endif
2494#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002495#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002496 case PSA_ALG_SHA_384:
2497 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002498#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002499 case PSA_ALG_SHA_512:
2500 return( &mbedtls_sha512_info );
2501#endif
2502 default:
2503 return( NULL );
2504 }
2505}
John Durkop07cc04a2020-11-16 22:08:34 -08002506#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002507 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2508 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2509 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002510
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002511psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2512{
2513 switch( operation->alg )
2514 {
Gilles Peskine81736312018-06-26 15:04:31 +02002515 case 0:
2516 /* The object has (apparently) been initialized but it is not
2517 * in use. It's ok to call abort on such an object, and there's
2518 * nothing to do. */
2519 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002520#if defined(MBEDTLS_MD2_C)
2521 case PSA_ALG_MD2:
2522 mbedtls_md2_free( &operation->ctx.md2 );
2523 break;
2524#endif
2525#if defined(MBEDTLS_MD4_C)
2526 case PSA_ALG_MD4:
2527 mbedtls_md4_free( &operation->ctx.md4 );
2528 break;
2529#endif
2530#if defined(MBEDTLS_MD5_C)
2531 case PSA_ALG_MD5:
2532 mbedtls_md5_free( &operation->ctx.md5 );
2533 break;
2534#endif
2535#if defined(MBEDTLS_RIPEMD160_C)
2536 case PSA_ALG_RIPEMD160:
2537 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2538 break;
2539#endif
2540#if defined(MBEDTLS_SHA1_C)
2541 case PSA_ALG_SHA_1:
2542 mbedtls_sha1_free( &operation->ctx.sha1 );
2543 break;
2544#endif
2545#if defined(MBEDTLS_SHA256_C)
2546 case PSA_ALG_SHA_224:
2547 case PSA_ALG_SHA_256:
2548 mbedtls_sha256_free( &operation->ctx.sha256 );
2549 break;
2550#endif
2551#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002552#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002553 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002554#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002555 case PSA_ALG_SHA_512:
2556 mbedtls_sha512_free( &operation->ctx.sha512 );
2557 break;
2558#endif
2559 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002560 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002561 }
2562 operation->alg = 0;
2563 return( PSA_SUCCESS );
2564}
2565
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002566psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002567 psa_algorithm_t alg )
2568{
Janos Follath24eed8d2019-11-22 13:21:35 +00002569 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002570
2571 /* A context must be freshly initialized before it can be set up. */
2572 if( operation->alg != 0 )
2573 {
2574 return( PSA_ERROR_BAD_STATE );
2575 }
2576
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002577 switch( alg )
2578 {
2579#if defined(MBEDTLS_MD2_C)
2580 case PSA_ALG_MD2:
2581 mbedtls_md2_init( &operation->ctx.md2 );
2582 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2583 break;
2584#endif
2585#if defined(MBEDTLS_MD4_C)
2586 case PSA_ALG_MD4:
2587 mbedtls_md4_init( &operation->ctx.md4 );
2588 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2589 break;
2590#endif
2591#if defined(MBEDTLS_MD5_C)
2592 case PSA_ALG_MD5:
2593 mbedtls_md5_init( &operation->ctx.md5 );
2594 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2595 break;
2596#endif
2597#if defined(MBEDTLS_RIPEMD160_C)
2598 case PSA_ALG_RIPEMD160:
2599 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2600 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2601 break;
2602#endif
2603#if defined(MBEDTLS_SHA1_C)
2604 case PSA_ALG_SHA_1:
2605 mbedtls_sha1_init( &operation->ctx.sha1 );
2606 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2607 break;
2608#endif
2609#if defined(MBEDTLS_SHA256_C)
2610 case PSA_ALG_SHA_224:
2611 mbedtls_sha256_init( &operation->ctx.sha256 );
2612 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2613 break;
2614 case PSA_ALG_SHA_256:
2615 mbedtls_sha256_init( &operation->ctx.sha256 );
2616 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2617 break;
2618#endif
2619#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002620#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002621 case PSA_ALG_SHA_384:
2622 mbedtls_sha512_init( &operation->ctx.sha512 );
2623 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2624 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002625#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002626 case PSA_ALG_SHA_512:
2627 mbedtls_sha512_init( &operation->ctx.sha512 );
2628 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2629 break;
2630#endif
2631 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002632 return( PSA_ALG_IS_HASH( alg ) ?
2633 PSA_ERROR_NOT_SUPPORTED :
2634 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002635 }
2636 if( ret == 0 )
2637 operation->alg = alg;
2638 else
2639 psa_hash_abort( operation );
2640 return( mbedtls_to_psa_error( ret ) );
2641}
2642
2643psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2644 const uint8_t *input,
2645 size_t input_length )
2646{
Janos Follath24eed8d2019-11-22 13:21:35 +00002647 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002648
2649 /* Don't require hash implementations to behave correctly on a
2650 * zero-length input, which may have an invalid pointer. */
2651 if( input_length == 0 )
2652 return( PSA_SUCCESS );
2653
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002654 switch( operation->alg )
2655 {
2656#if defined(MBEDTLS_MD2_C)
2657 case PSA_ALG_MD2:
2658 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2659 input, input_length );
2660 break;
2661#endif
2662#if defined(MBEDTLS_MD4_C)
2663 case PSA_ALG_MD4:
2664 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2665 input, input_length );
2666 break;
2667#endif
2668#if defined(MBEDTLS_MD5_C)
2669 case PSA_ALG_MD5:
2670 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2671 input, input_length );
2672 break;
2673#endif
2674#if defined(MBEDTLS_RIPEMD160_C)
2675 case PSA_ALG_RIPEMD160:
2676 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2677 input, input_length );
2678 break;
2679#endif
2680#if defined(MBEDTLS_SHA1_C)
2681 case PSA_ALG_SHA_1:
2682 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2683 input, input_length );
2684 break;
2685#endif
2686#if defined(MBEDTLS_SHA256_C)
2687 case PSA_ALG_SHA_224:
2688 case PSA_ALG_SHA_256:
2689 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2690 input, input_length );
2691 break;
2692#endif
2693#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002694#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002695 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002696#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002697 case PSA_ALG_SHA_512:
2698 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2699 input, input_length );
2700 break;
2701#endif
2702 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002703 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002704 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002705
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002706 if( ret != 0 )
2707 psa_hash_abort( operation );
2708 return( mbedtls_to_psa_error( ret ) );
2709}
2710
2711psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2712 uint8_t *hash,
2713 size_t hash_size,
2714 size_t *hash_length )
2715{
itayzafrir40835d42018-08-02 13:14:17 +03002716 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002718 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002719
2720 /* Fill the output buffer with something that isn't a valid hash
2721 * (barring an attack on the hash and deliberately-crafted input),
2722 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002723 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002724 /* If hash_size is 0 then hash may be NULL and then the
2725 * call to memset would have undefined behavior. */
2726 if( hash_size != 0 )
2727 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002728
2729 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002730 {
2731 status = PSA_ERROR_BUFFER_TOO_SMALL;
2732 goto exit;
2733 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002734
2735 switch( operation->alg )
2736 {
2737#if defined(MBEDTLS_MD2_C)
2738 case PSA_ALG_MD2:
2739 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2740 break;
2741#endif
2742#if defined(MBEDTLS_MD4_C)
2743 case PSA_ALG_MD4:
2744 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2745 break;
2746#endif
2747#if defined(MBEDTLS_MD5_C)
2748 case PSA_ALG_MD5:
2749 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2750 break;
2751#endif
2752#if defined(MBEDTLS_RIPEMD160_C)
2753 case PSA_ALG_RIPEMD160:
2754 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2755 break;
2756#endif
2757#if defined(MBEDTLS_SHA1_C)
2758 case PSA_ALG_SHA_1:
2759 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2760 break;
2761#endif
2762#if defined(MBEDTLS_SHA256_C)
2763 case PSA_ALG_SHA_224:
2764 case PSA_ALG_SHA_256:
2765 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2766 break;
2767#endif
2768#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002769#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002770 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002771#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002772 case PSA_ALG_SHA_512:
2773 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2774 break;
2775#endif
2776 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002777 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002778 }
itayzafrir40835d42018-08-02 13:14:17 +03002779 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002780
itayzafrir40835d42018-08-02 13:14:17 +03002781exit:
2782 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002783 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002784 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002785 return( psa_hash_abort( operation ) );
2786 }
2787 else
2788 {
2789 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002790 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002791 }
2792}
2793
Gilles Peskine2d277862018-06-18 15:41:12 +02002794psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2795 const uint8_t *hash,
2796 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002797{
2798 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2799 size_t actual_hash_length;
2800 psa_status_t status = psa_hash_finish( operation,
2801 actual_hash, sizeof( actual_hash ),
2802 &actual_hash_length );
2803 if( status != PSA_SUCCESS )
2804 return( status );
2805 if( actual_hash_length != hash_length )
2806 return( PSA_ERROR_INVALID_SIGNATURE );
2807 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2808 return( PSA_ERROR_INVALID_SIGNATURE );
2809 return( PSA_SUCCESS );
2810}
2811
Gilles Peskine0a749c82019-11-28 19:33:58 +01002812psa_status_t psa_hash_compute( psa_algorithm_t alg,
2813 const uint8_t *input, size_t input_length,
2814 uint8_t *hash, size_t hash_size,
2815 size_t *hash_length )
2816{
2817 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2818 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2819
2820 *hash_length = hash_size;
2821 status = psa_hash_setup( &operation, alg );
2822 if( status != PSA_SUCCESS )
2823 goto exit;
2824 status = psa_hash_update( &operation, input, input_length );
2825 if( status != PSA_SUCCESS )
2826 goto exit;
2827 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2828 if( status != PSA_SUCCESS )
2829 goto exit;
2830
2831exit:
2832 if( status == PSA_SUCCESS )
2833 status = psa_hash_abort( &operation );
2834 else
2835 psa_hash_abort( &operation );
2836 return( status );
2837}
2838
2839psa_status_t psa_hash_compare( psa_algorithm_t alg,
2840 const uint8_t *input, size_t input_length,
2841 const uint8_t *hash, size_t hash_length )
2842{
2843 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2844 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2845
2846 status = psa_hash_setup( &operation, alg );
2847 if( status != PSA_SUCCESS )
2848 goto exit;
2849 status = psa_hash_update( &operation, input, input_length );
2850 if( status != PSA_SUCCESS )
2851 goto exit;
2852 status = psa_hash_verify( &operation, hash, hash_length );
2853 if( status != PSA_SUCCESS )
2854 goto exit;
2855
2856exit:
2857 if( status == PSA_SUCCESS )
2858 status = psa_hash_abort( &operation );
2859 else
2860 psa_hash_abort( &operation );
2861 return( status );
2862}
2863
Gilles Peskineeb35d782019-01-22 17:56:16 +01002864psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2865 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002866{
2867 if( target_operation->alg != 0 )
2868 return( PSA_ERROR_BAD_STATE );
2869
2870 switch( source_operation->alg )
2871 {
2872 case 0:
2873 return( PSA_ERROR_BAD_STATE );
2874#if defined(MBEDTLS_MD2_C)
2875 case PSA_ALG_MD2:
2876 mbedtls_md2_clone( &target_operation->ctx.md2,
2877 &source_operation->ctx.md2 );
2878 break;
2879#endif
2880#if defined(MBEDTLS_MD4_C)
2881 case PSA_ALG_MD4:
2882 mbedtls_md4_clone( &target_operation->ctx.md4,
2883 &source_operation->ctx.md4 );
2884 break;
2885#endif
2886#if defined(MBEDTLS_MD5_C)
2887 case PSA_ALG_MD5:
2888 mbedtls_md5_clone( &target_operation->ctx.md5,
2889 &source_operation->ctx.md5 );
2890 break;
2891#endif
2892#if defined(MBEDTLS_RIPEMD160_C)
2893 case PSA_ALG_RIPEMD160:
2894 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2895 &source_operation->ctx.ripemd160 );
2896 break;
2897#endif
2898#if defined(MBEDTLS_SHA1_C)
2899 case PSA_ALG_SHA_1:
2900 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2901 &source_operation->ctx.sha1 );
2902 break;
2903#endif
2904#if defined(MBEDTLS_SHA256_C)
2905 case PSA_ALG_SHA_224:
2906 case PSA_ALG_SHA_256:
2907 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2908 &source_operation->ctx.sha256 );
2909 break;
2910#endif
2911#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002912#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002913 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002914#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002915 case PSA_ALG_SHA_512:
2916 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2917 &source_operation->ctx.sha512 );
2918 break;
2919#endif
2920 default:
2921 return( PSA_ERROR_NOT_SUPPORTED );
2922 }
2923
2924 target_operation->alg = source_operation->alg;
2925 return( PSA_SUCCESS );
2926}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002927
2928
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002929/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002930/* MAC */
2931/****************************************************************/
2932
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002933static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002934 psa_algorithm_t alg,
2935 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002936 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002937 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002938{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002939 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002940 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002941
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002942 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002943 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002944
Gilles Peskine8c9def32018-02-08 10:02:12 +01002945 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2946 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002947 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002948 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002949 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002950 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002951 mode = MBEDTLS_MODE_STREAM;
2952 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002953 case PSA_ALG_CTR:
2954 mode = MBEDTLS_MODE_CTR;
2955 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002956 case PSA_ALG_CFB:
2957 mode = MBEDTLS_MODE_CFB;
2958 break;
2959 case PSA_ALG_OFB:
2960 mode = MBEDTLS_MODE_OFB;
2961 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002962 case PSA_ALG_ECB_NO_PADDING:
2963 mode = MBEDTLS_MODE_ECB;
2964 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002965 case PSA_ALG_CBC_NO_PADDING:
2966 mode = MBEDTLS_MODE_CBC;
2967 break;
2968 case PSA_ALG_CBC_PKCS7:
2969 mode = MBEDTLS_MODE_CBC;
2970 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002971 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002972 mode = MBEDTLS_MODE_CCM;
2973 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002974 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002975 mode = MBEDTLS_MODE_GCM;
2976 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002977 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2978 mode = MBEDTLS_MODE_CHACHAPOLY;
2979 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002980 default:
2981 return( NULL );
2982 }
2983 }
2984 else if( alg == PSA_ALG_CMAC )
2985 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002986 else
2987 return( NULL );
2988
2989 switch( key_type )
2990 {
2991 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002992 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002993 break;
2994 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002995 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2996 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002997 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002998 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002999 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003000 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003001 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3002 * but two-key Triple-DES is functionally three-key Triple-DES
3003 * with K1=K3, so that's how we present it to mbedtls. */
3004 if( key_bits == 128 )
3005 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003006 break;
3007 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003008 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003009 break;
3010 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003011 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003012 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003013 case PSA_KEY_TYPE_CHACHA20:
3014 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3015 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003016 default:
3017 return( NULL );
3018 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003019 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003020 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003021
Jaeden Amero23bbb752018-06-26 14:16:54 +01003022 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3023 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003024}
3025
John Durkop6ba40d12020-11-10 08:50:04 -08003026#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003027static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003028{
Gilles Peskine2d277862018-06-18 15:41:12 +02003029 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003030 {
3031 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003032 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003033 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003034 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003035 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003036 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003037 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003038 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003039 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003040 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003041 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003042 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003043 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003044 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003045 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003046 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003047 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003048 return( 128 );
3049 default:
3050 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003051 }
3052}
John Durkop07cc04a2020-11-16 22:08:34 -08003053#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003054
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003055/* Initialize the MAC operation structure. Once this function has been
3056 * called, psa_mac_abort can run and will do the right thing. */
3057static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3058 psa_algorithm_t alg )
3059{
3060 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3061
3062 operation->alg = alg;
3063 operation->key_set = 0;
3064 operation->iv_set = 0;
3065 operation->iv_required = 0;
3066 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003067 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003068
3069#if defined(MBEDTLS_CMAC_C)
3070 if( alg == PSA_ALG_CMAC )
3071 {
3072 operation->iv_required = 0;
3073 mbedtls_cipher_init( &operation->ctx.cmac );
3074 status = PSA_SUCCESS;
3075 }
3076 else
3077#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003078#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003079 if( PSA_ALG_IS_HMAC( operation->alg ) )
3080 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003081 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3082 operation->ctx.hmac.hash_ctx.alg = 0;
3083 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003084 }
3085 else
John Durkopd0321952020-10-29 21:37:36 -07003086#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003087 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003088 if( ! PSA_ALG_IS_MAC( alg ) )
3089 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003090 }
3091
3092 if( status != PSA_SUCCESS )
3093 memset( operation, 0, sizeof( *operation ) );
3094 return( status );
3095}
3096
John Durkop6ba40d12020-11-10 08:50:04 -08003097#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003098static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3099{
Gilles Peskine3f108122018-12-07 18:14:53 +01003100 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003101 return( psa_hash_abort( &hmac->hash_ctx ) );
3102}
John Durkop6ba40d12020-11-10 08:50:04 -08003103#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003104
Gilles Peskine8c9def32018-02-08 10:02:12 +01003105psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3106{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003107 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003108 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003109 /* The object has (apparently) been initialized but it is not
3110 * in use. It's ok to call abort on such an object, and there's
3111 * nothing to do. */
3112 return( PSA_SUCCESS );
3113 }
3114 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003115#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003116 if( operation->alg == PSA_ALG_CMAC )
3117 {
3118 mbedtls_cipher_free( &operation->ctx.cmac );
3119 }
3120 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003121#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003122#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003123 if( PSA_ALG_IS_HMAC( operation->alg ) )
3124 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003125 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003126 }
3127 else
John Durkopd0321952020-10-29 21:37:36 -07003128#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003129 {
3130 /* Sanity check (shouldn't happen: operation->alg should
3131 * always have been initialized to a valid value). */
3132 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003133 }
Moran Peker41deec42018-04-04 15:43:05 +03003134
Gilles Peskine8c9def32018-02-08 10:02:12 +01003135 operation->alg = 0;
3136 operation->key_set = 0;
3137 operation->iv_set = 0;
3138 operation->iv_required = 0;
3139 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003140 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003141
Gilles Peskine8c9def32018-02-08 10:02:12 +01003142 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003143
3144bad_state:
3145 /* If abort is called on an uninitialized object, we can't trust
3146 * anything. Wipe the object in case it contains confidential data.
3147 * This may result in a memory leak if a pointer gets overwritten,
3148 * but it's too late to do anything about this. */
3149 memset( operation, 0, sizeof( *operation ) );
3150 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003151}
3152
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003153#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003154static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003155 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003156 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003157 const mbedtls_cipher_info_t *cipher_info )
3158{
Janos Follath24eed8d2019-11-22 13:21:35 +00003159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003160
3161 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003162
3163 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3164 if( ret != 0 )
3165 return( ret );
3166
3167 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003168 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003169 key_bits );
3170 return( ret );
3171}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003172#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003173
John Durkop6ba40d12020-11-10 08:50:04 -08003174#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003175static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3176 const uint8_t *key,
3177 size_t key_length,
3178 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003179{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003180 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003181 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003182 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003183 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003184 psa_status_t status;
3185
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003186 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3187 * overflow below. This should never trigger if the hash algorithm
3188 * is implemented correctly. */
3189 /* The size checks against the ipad and opad buffers cannot be written
3190 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3191 * because that triggers -Wlogical-op on GCC 7.3. */
3192 if( block_size > sizeof( ipad ) )
3193 return( PSA_ERROR_NOT_SUPPORTED );
3194 if( block_size > sizeof( hmac->opad ) )
3195 return( PSA_ERROR_NOT_SUPPORTED );
3196 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003197 return( PSA_ERROR_NOT_SUPPORTED );
3198
Gilles Peskined223b522018-06-11 18:12:58 +02003199 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003200 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003201 status = psa_hash_compute( hash_alg, key, key_length,
3202 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003203 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003204 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003205 }
Gilles Peskine96889972018-07-12 17:07:03 +02003206 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3207 * but it is permitted. It is common when HMAC is used in HKDF, for
3208 * example. Don't call `memcpy` in the 0-length because `key` could be
3209 * an invalid pointer which would make the behavior undefined. */
3210 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003211 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003212
Gilles Peskined223b522018-06-11 18:12:58 +02003213 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3214 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003215 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003216 ipad[i] ^= 0x36;
3217 memset( ipad + key_length, 0x36, block_size - key_length );
3218
3219 /* Copy the key material from ipad to opad, flipping the requisite bits,
3220 * and filling the rest of opad with the requisite constant. */
3221 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003222 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3223 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003224
Gilles Peskine01126fa2018-07-12 17:04:55 +02003225 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003226 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003227 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003228
Gilles Peskine01126fa2018-07-12 17:04:55 +02003229 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003230
3231cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003232 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003233
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003234 return( status );
3235}
John Durkop6ba40d12020-11-10 08:50:04 -08003236#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003237
Gilles Peskine89167cb2018-07-08 20:12:23 +02003238static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003239 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003240 psa_algorithm_t alg,
3241 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003242{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003243 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003244 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003245 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003246 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003247 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003248 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003249 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003250 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003251
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003252 /* A context must be freshly initialized before it can be set up. */
3253 if( operation->alg != 0 )
3254 {
3255 return( PSA_ERROR_BAD_STATE );
3256 }
3257
Gilles Peskined911eb72018-08-14 15:18:45 +02003258 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003259 if( status != PSA_SUCCESS )
3260 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003261 if( is_sign )
3262 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003263
Ronald Cron5c522922020-11-14 16:35:34 +01003264 status = psa_get_and_lock_transparent_key_slot_with_policy(
3265 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003266 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003267 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003268 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003269
Gilles Peskine8c9def32018-02-08 10:02:12 +01003270#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003271 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003272 {
3273 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003274 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003275 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003276 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003277 if( cipher_info == NULL )
3278 {
3279 status = PSA_ERROR_NOT_SUPPORTED;
3280 goto exit;
3281 }
3282 operation->mac_size = cipher_info->block_size;
3283 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3284 status = mbedtls_to_psa_error( ret );
3285 }
3286 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003287#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003288#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003289 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003290 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003291 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003292 if( hash_alg == 0 )
3293 {
3294 status = PSA_ERROR_NOT_SUPPORTED;
3295 goto exit;
3296 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003297
3298 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3299 /* Sanity check. This shouldn't fail on a valid configuration. */
3300 if( operation->mac_size == 0 ||
3301 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3302 {
3303 status = PSA_ERROR_NOT_SUPPORTED;
3304 goto exit;
3305 }
3306
Gilles Peskine8e338702019-07-30 20:06:31 +02003307 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003308 {
3309 status = PSA_ERROR_INVALID_ARGUMENT;
3310 goto exit;
3311 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003312
Gilles Peskine01126fa2018-07-12 17:04:55 +02003313 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003314 slot->data.key.data,
3315 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003316 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003317 }
3318 else
John Durkopd0321952020-10-29 21:37:36 -07003319#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003320 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003321 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003322 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003323 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003324
Gilles Peskined911eb72018-08-14 15:18:45 +02003325 if( truncated == 0 )
3326 {
3327 /* The "normal" case: untruncated algorithm. Nothing to do. */
3328 }
3329 else if( truncated < 4 )
3330 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003331 /* A very short MAC is too short for security since it can be
3332 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3333 * so we make this our minimum, even though 32 bits is still
3334 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003335 status = PSA_ERROR_NOT_SUPPORTED;
3336 }
3337 else if( truncated > operation->mac_size )
3338 {
3339 /* It's impossible to "truncate" to a larger length. */
3340 status = PSA_ERROR_INVALID_ARGUMENT;
3341 }
3342 else
3343 operation->mac_size = truncated;
3344
Gilles Peskinefbfac682018-07-08 20:51:54 +02003345exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003346 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003347 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003348 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003349 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003350 else
3351 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003352 operation->key_set = 1;
3353 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003354
Ronald Cron5c522922020-11-14 16:35:34 +01003355 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003356
Ronald Cron5c522922020-11-14 16:35:34 +01003357 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003358}
3359
Gilles Peskine89167cb2018-07-08 20:12:23 +02003360psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003361 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003362 psa_algorithm_t alg )
3363{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003364 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003365}
3366
3367psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003368 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003369 psa_algorithm_t alg )
3370{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003371 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003372}
3373
Gilles Peskine8c9def32018-02-08 10:02:12 +01003374psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3375 const uint8_t *input,
3376 size_t input_length )
3377{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003378 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003379 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003380 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003381 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003382 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003383 operation->has_input = 1;
3384
Gilles Peskine8c9def32018-02-08 10:02:12 +01003385#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003386 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003387 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003388 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3389 input, input_length );
3390 status = mbedtls_to_psa_error( ret );
3391 }
3392 else
3393#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003394#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003395 if( PSA_ALG_IS_HMAC( operation->alg ) )
3396 {
3397 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3398 input_length );
3399 }
3400 else
John Durkopd0321952020-10-29 21:37:36 -07003401#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003402 {
3403 /* This shouldn't happen if `operation` was initialized by
3404 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003405 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003406 }
3407
Gilles Peskinefbfac682018-07-08 20:51:54 +02003408 if( status != PSA_SUCCESS )
3409 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003410 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003411}
3412
John Durkop6ba40d12020-11-10 08:50:04 -08003413#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003414static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3415 uint8_t *mac,
3416 size_t mac_size )
3417{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003418 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003419 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3420 size_t hash_size = 0;
3421 size_t block_size = psa_get_hash_block_size( hash_alg );
3422 psa_status_t status;
3423
Gilles Peskine01126fa2018-07-12 17:04:55 +02003424 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3425 if( status != PSA_SUCCESS )
3426 return( status );
3427 /* From here on, tmp needs to be wiped. */
3428
3429 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3430 if( status != PSA_SUCCESS )
3431 goto exit;
3432
3433 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3434 if( status != PSA_SUCCESS )
3435 goto exit;
3436
3437 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3438 if( status != PSA_SUCCESS )
3439 goto exit;
3440
Gilles Peskined911eb72018-08-14 15:18:45 +02003441 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3442 if( status != PSA_SUCCESS )
3443 goto exit;
3444
3445 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003446
3447exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003448 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003449 return( status );
3450}
John Durkop6ba40d12020-11-10 08:50:04 -08003451#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003452
mohammad16036df908f2018-04-02 08:34:15 -07003453static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003454 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003455 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003456{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003457 if( ! operation->key_set )
3458 return( PSA_ERROR_BAD_STATE );
3459 if( operation->iv_required && ! operation->iv_set )
3460 return( PSA_ERROR_BAD_STATE );
3461
Gilles Peskine8c9def32018-02-08 10:02:12 +01003462 if( mac_size < operation->mac_size )
3463 return( PSA_ERROR_BUFFER_TOO_SMALL );
3464
Gilles Peskine8c9def32018-02-08 10:02:12 +01003465#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003466 if( operation->alg == PSA_ALG_CMAC )
3467 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003468 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3469 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3470 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003471 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003472 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003473 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003474 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003475 else
3476#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003477#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003478 if( PSA_ALG_IS_HMAC( operation->alg ) )
3479 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003480 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003481 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003482 }
3483 else
John Durkopd0321952020-10-29 21:37:36 -07003484#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003485 {
3486 /* This shouldn't happen if `operation` was initialized by
3487 * a setup function. */
3488 return( PSA_ERROR_BAD_STATE );
3489 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003490}
3491
Gilles Peskineacd4be32018-07-08 19:56:25 +02003492psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3493 uint8_t *mac,
3494 size_t mac_size,
3495 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003496{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003497 psa_status_t status;
3498
Jaeden Amero252ef282019-02-15 14:05:35 +00003499 if( operation->alg == 0 )
3500 {
3501 return( PSA_ERROR_BAD_STATE );
3502 }
3503
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003504 /* Fill the output buffer with something that isn't a valid mac
3505 * (barring an attack on the mac and deliberately-crafted input),
3506 * in case the caller doesn't check the return status properly. */
3507 *mac_length = mac_size;
3508 /* If mac_size is 0 then mac may be NULL and then the
3509 * call to memset would have undefined behavior. */
3510 if( mac_size != 0 )
3511 memset( mac, '!', mac_size );
3512
Gilles Peskine89167cb2018-07-08 20:12:23 +02003513 if( ! operation->is_sign )
3514 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003515 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003516 }
mohammad16036df908f2018-04-02 08:34:15 -07003517
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003518 status = psa_mac_finish_internal( operation, mac, mac_size );
3519
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003520 if( status == PSA_SUCCESS )
3521 {
3522 status = psa_mac_abort( operation );
3523 if( status == PSA_SUCCESS )
3524 *mac_length = operation->mac_size;
3525 else
3526 memset( mac, '!', mac_size );
3527 }
3528 else
3529 psa_mac_abort( operation );
3530 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003531}
3532
Gilles Peskineacd4be32018-07-08 19:56:25 +02003533psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3534 const uint8_t *mac,
3535 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003536{
Gilles Peskine828ed142018-06-18 23:25:51 +02003537 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003538 psa_status_t status;
3539
Jaeden Amero252ef282019-02-15 14:05:35 +00003540 if( operation->alg == 0 )
3541 {
3542 return( PSA_ERROR_BAD_STATE );
3543 }
3544
Gilles Peskine89167cb2018-07-08 20:12:23 +02003545 if( operation->is_sign )
3546 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003547 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003548 }
3549 if( operation->mac_size != mac_length )
3550 {
3551 status = PSA_ERROR_INVALID_SIGNATURE;
3552 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003553 }
mohammad16036df908f2018-04-02 08:34:15 -07003554
3555 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003556 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003557 if( status != PSA_SUCCESS )
3558 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003559
3560 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3561 status = PSA_ERROR_INVALID_SIGNATURE;
3562
3563cleanup:
3564 if( status == PSA_SUCCESS )
3565 status = psa_mac_abort( operation );
3566 else
3567 psa_mac_abort( operation );
3568
Gilles Peskine3f108122018-12-07 18:14:53 +01003569 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003570
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003571 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003572}
3573
3574
Gilles Peskine20035e32018-02-03 22:44:14 +01003575
Gilles Peskine20035e32018-02-03 22:44:14 +01003576/****************************************************************/
3577/* Asymmetric cryptography */
3578/****************************************************************/
3579
John Durkop6ba40d12020-11-10 08:50:04 -08003580#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3581 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003582/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003583 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003584static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3585 size_t hash_length,
3586 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003587{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003588 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003589 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003590 *md_alg = mbedtls_md_get_type( md_info );
3591
3592 /* The Mbed TLS RSA module uses an unsigned int for hash length
3593 * parameters. Validate that it fits so that we don't risk an
3594 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003595#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003596 if( hash_length > UINT_MAX )
3597 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003598#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003599
John Durkop0e005192020-10-31 22:06:54 -07003600#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003601 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3602 * must be correct. */
3603 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3604 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003605 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003606 if( md_info == NULL )
3607 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003608 if( mbedtls_md_get_size( md_info ) != hash_length )
3609 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003610 }
John Durkop0e005192020-10-31 22:06:54 -07003611#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003612
John Durkop0e005192020-10-31 22:06:54 -07003613#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003614 /* PSS requires a hash internally. */
3615 if( PSA_ALG_IS_RSA_PSS( alg ) )
3616 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003617 if( md_info == NULL )
3618 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003619 }
John Durkop0e005192020-10-31 22:06:54 -07003620#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003621
Gilles Peskine61b91d42018-06-08 16:09:36 +02003622 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003623}
3624
Gilles Peskine2b450e32018-06-27 15:42:46 +02003625static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3626 psa_algorithm_t alg,
3627 const uint8_t *hash,
3628 size_t hash_length,
3629 uint8_t *signature,
3630 size_t signature_size,
3631 size_t *signature_length )
3632{
3633 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003634 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003635 mbedtls_md_type_t md_alg;
3636
3637 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3638 if( status != PSA_SUCCESS )
3639 return( status );
3640
Gilles Peskine630a18a2018-06-29 17:49:35 +02003641 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003642 return( PSA_ERROR_BUFFER_TOO_SMALL );
3643
John Durkop0e005192020-10-31 22:06:54 -07003644#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003645 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3646 {
3647 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3648 MBEDTLS_MD_NONE );
3649 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003650 mbedtls_psa_get_random,
3651 mbedtls_psa_random_state( &global_data.rng ),
Gilles Peskine2b450e32018-06-27 15:42:46 +02003652 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003653 md_alg,
3654 (unsigned int) hash_length,
3655 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003656 signature );
3657 }
3658 else
John Durkop0e005192020-10-31 22:06:54 -07003659#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3660#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003661 if( PSA_ALG_IS_RSA_PSS( alg ) )
3662 {
3663 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3664 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003665 mbedtls_psa_get_random,
3666 mbedtls_psa_random_state( &global_data.rng ),
Gilles Peskine2b450e32018-06-27 15:42:46 +02003667 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003668 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003669 (unsigned int) hash_length,
3670 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003671 signature );
3672 }
3673 else
John Durkop0e005192020-10-31 22:06:54 -07003674#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003675 {
3676 return( PSA_ERROR_INVALID_ARGUMENT );
3677 }
3678
3679 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003680 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003681 return( mbedtls_to_psa_error( ret ) );
3682}
3683
3684static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3685 psa_algorithm_t alg,
3686 const uint8_t *hash,
3687 size_t hash_length,
3688 const uint8_t *signature,
3689 size_t signature_length )
3690{
3691 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003692 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003693 mbedtls_md_type_t md_alg;
3694
3695 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3696 if( status != PSA_SUCCESS )
3697 return( status );
3698
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003699 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3700 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003701
John Durkop0e005192020-10-31 22:06:54 -07003702#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003703 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3704 {
3705 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3706 MBEDTLS_MD_NONE );
3707 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003708 mbedtls_psa_get_random,
3709 mbedtls_psa_random_state( &global_data.rng ),
Gilles Peskine2b450e32018-06-27 15:42:46 +02003710 MBEDTLS_RSA_PUBLIC,
3711 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003712 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003713 hash,
3714 signature );
3715 }
3716 else
John Durkop0e005192020-10-31 22:06:54 -07003717#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3718#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003719 if( PSA_ALG_IS_RSA_PSS( alg ) )
3720 {
3721 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3722 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003723 mbedtls_psa_get_random,
3724 mbedtls_psa_random_state( &global_data.rng ),
Gilles Peskine2b450e32018-06-27 15:42:46 +02003725 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003726 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003727 (unsigned int) hash_length,
3728 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003729 signature );
3730 }
3731 else
John Durkop0e005192020-10-31 22:06:54 -07003732#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003733 {
3734 return( PSA_ERROR_INVALID_ARGUMENT );
3735 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003736
3737 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3738 * the rest of the signature is invalid". This has little use in
3739 * practice and PSA doesn't report this distinction. */
3740 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3741 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003742 return( mbedtls_to_psa_error( ret ) );
3743}
John Durkop6ba40d12020-11-10 08:50:04 -08003744#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3745 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003746
John Durkop6ba40d12020-11-10 08:50:04 -08003747#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3748 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003749/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3750 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3751 * (even though these functions don't modify it). */
3752static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
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 )
3759{
Janos Follath24eed8d2019-11-22 13:21:35 +00003760 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003761 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003762 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003763 mbedtls_mpi_init( &r );
3764 mbedtls_mpi_init( &s );
3765
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003766 if( signature_size < 2 * curve_bytes )
3767 {
3768 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3769 goto cleanup;
3770 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003771
John Durkop0ea39e02020-10-13 19:58:20 -07003772#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003773 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3774 {
3775 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3776 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3777 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003778 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3779 &ecp->d, hash,
3780 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003781 mbedtls_psa_get_random,
3782 mbedtls_psa_random_state( &global_data.rng ) ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003783 }
3784 else
John Durkop0ea39e02020-10-13 19:58:20 -07003785#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003786 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003787 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003788 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3789 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003790 mbedtls_psa_get_random,
3791 mbedtls_psa_random_state( &global_data.rng ) ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003792 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003793
3794 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3795 signature,
3796 curve_bytes ) );
3797 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3798 signature + curve_bytes,
3799 curve_bytes ) );
3800
3801cleanup:
3802 mbedtls_mpi_free( &r );
3803 mbedtls_mpi_free( &s );
3804 if( ret == 0 )
3805 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003806 return( mbedtls_to_psa_error( ret ) );
3807}
3808
3809static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3810 const uint8_t *hash,
3811 size_t hash_length,
3812 const uint8_t *signature,
3813 size_t signature_length )
3814{
Janos Follath24eed8d2019-11-22 13:21:35 +00003815 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003816 mbedtls_mpi r, s;
3817 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3818 mbedtls_mpi_init( &r );
3819 mbedtls_mpi_init( &s );
3820
3821 if( signature_length != 2 * curve_bytes )
3822 return( PSA_ERROR_INVALID_SIGNATURE );
3823
3824 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3825 signature,
3826 curve_bytes ) );
3827 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3828 signature + curve_bytes,
3829 curve_bytes ) );
3830
Steven Cooremanacda8342020-07-24 23:09:52 +02003831 /* Check whether the public part is loaded. If not, load it. */
3832 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3833 {
3834 MBEDTLS_MPI_CHK(
3835 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003836 mbedtls_psa_get_random, mbedtls_psa_random_state( &global_data.rng ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003837 }
3838
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003839 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3840 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003841
3842cleanup:
3843 mbedtls_mpi_free( &r );
3844 mbedtls_mpi_free( &s );
3845 return( mbedtls_to_psa_error( ret ) );
3846}
John Durkop6ba40d12020-11-10 08:50:04 -08003847#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3848 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003849
Ronald Croncf56a0a2020-08-04 09:51:30 +02003850psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003851 psa_algorithm_t alg,
3852 const uint8_t *hash,
3853 size_t hash_length,
3854 uint8_t *signature,
3855 size_t signature_size,
3856 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003857{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003858 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003859 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003860 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003861
3862 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003863 /* Immediately reject a zero-length signature buffer. This guarantees
3864 * that signature must be a valid pointer. (On the other hand, the hash
3865 * buffer can in principle be empty since it doesn't actually have
3866 * to be a hash.) */
3867 if( signature_size == 0 )
3868 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003869
Ronald Cron5c522922020-11-14 16:35:34 +01003870 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3871 PSA_KEY_USAGE_SIGN_HASH,
3872 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003873 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003874 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003875 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003876 {
3877 status = PSA_ERROR_INVALID_ARGUMENT;
3878 goto exit;
3879 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003880
Steven Cooremancd84cb42020-07-16 20:28:36 +02003881 /* Try any of the available accelerators first */
3882 status = psa_driver_wrapper_sign_hash( slot,
3883 alg,
3884 hash,
3885 hash_length,
3886 signature,
3887 signature_size,
3888 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003889 if( status != PSA_ERROR_NOT_SUPPORTED ||
3890 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003891 goto exit;
3892
Steven Cooreman7a250572020-07-17 16:43:05 +02003893 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003894#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3895 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003896 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003897 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003898 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003899
Steven Cooreman4fed4552020-08-03 14:46:03 +02003900 status = psa_load_rsa_representation( slot->attr.type,
3901 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003902 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003903 &rsa );
3904 if( status != PSA_SUCCESS )
3905 goto exit;
3906
Steven Cooremana2371e52020-07-28 14:30:39 +02003907 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003908 alg,
3909 hash, hash_length,
3910 signature, signature_size,
3911 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003912
Steven Cooremana2371e52020-07-28 14:30:39 +02003913 mbedtls_rsa_free( rsa );
3914 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003915 }
3916 else
John Durkop6ba40d12020-11-10 08:50:04 -08003917#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3918 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003919 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003920 {
John Durkop9814fa22020-11-04 12:28:15 -08003921#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3922 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003923 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003924#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003925 PSA_ALG_IS_ECDSA( alg )
3926#else
3927 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3928#endif
3929 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003930 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003931 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003932 status = psa_load_ecp_representation( slot->attr.type,
3933 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003934 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003935 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003936 if( status != PSA_SUCCESS )
3937 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003938 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003939 alg,
3940 hash, hash_length,
3941 signature, signature_size,
3942 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003943 mbedtls_ecp_keypair_free( ecp );
3944 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003945 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003946 else
John Durkop9814fa22020-11-04 12:28:15 -08003947#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3948 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003949 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003950 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003951 }
itayzafrir5c753392018-05-08 11:18:38 +03003952 }
3953 else
itayzafrir5c753392018-05-08 11:18:38 +03003954 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003955 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003956 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003957
3958exit:
3959 /* Fill the unused part of the output buffer (the whole buffer on error,
3960 * the trailing part on success) with something that isn't a valid mac
3961 * (barring an attack on the mac and deliberately-crafted input),
3962 * in case the caller doesn't check the return status properly. */
3963 if( status == PSA_SUCCESS )
3964 memset( signature + *signature_length, '!',
3965 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003966 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003967 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003968 /* If signature_size is 0 then we have nothing to do. We must not call
3969 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003970
Ronald Cron5c522922020-11-14 16:35:34 +01003971 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003972
Ronald Cron5c522922020-11-14 16:35:34 +01003973 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003974}
3975
Ronald Croncf56a0a2020-08-04 09:51:30 +02003976psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003977 psa_algorithm_t alg,
3978 const uint8_t *hash,
3979 size_t hash_length,
3980 const uint8_t *signature,
3981 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003982{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003983 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003984 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003985 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003986
Ronald Cron5c522922020-11-14 16:35:34 +01003987 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3988 PSA_KEY_USAGE_VERIFY_HASH,
3989 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003990 if( status != PSA_SUCCESS )
3991 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003992
Steven Cooreman55ae2172020-07-17 19:46:15 +02003993 /* Try any of the available accelerators first */
3994 status = psa_driver_wrapper_verify_hash( slot,
3995 alg,
3996 hash,
3997 hash_length,
3998 signature,
3999 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004000 if( status != PSA_ERROR_NOT_SUPPORTED ||
4001 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004002 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004003
John Durkop6ba40d12020-11-10 08:50:04 -08004004#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4005 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004006 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004007 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004008 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004009
Steven Cooreman4fed4552020-08-03 14:46:03 +02004010 status = psa_load_rsa_representation( slot->attr.type,
4011 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004012 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004013 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004014 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004015 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004016
Steven Cooremana2371e52020-07-28 14:30:39 +02004017 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004018 alg,
4019 hash, hash_length,
4020 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004021 mbedtls_rsa_free( rsa );
4022 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004023 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004024 }
4025 else
John Durkop6ba40d12020-11-10 08:50:04 -08004026#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4027 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004028 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004029 {
John Durkop9814fa22020-11-04 12:28:15 -08004030#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4031 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004032 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004033 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004034 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004035 status = psa_load_ecp_representation( slot->attr.type,
4036 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004037 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004038 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004039 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004040 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004041 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004042 hash, hash_length,
4043 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004044 mbedtls_ecp_keypair_free( ecp );
4045 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004046 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004047 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004048 else
John Durkop9814fa22020-11-04 12:28:15 -08004049#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4050 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004051 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004052 status = PSA_ERROR_INVALID_ARGUMENT;
4053 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004054 }
itayzafrir5c753392018-05-08 11:18:38 +03004055 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004056 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004057 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004058 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004059 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004060
4061exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004062 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004063
Ronald Cron5c522922020-11-14 16:35:34 +01004064 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004065}
4066
John Durkop0e005192020-10-31 22:06:54 -07004067#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004068static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4069 mbedtls_rsa_context *rsa )
4070{
4071 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4072 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4073 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4074 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4075}
John Durkop0e005192020-10-31 22:06:54 -07004076#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004077
Ronald Croncf56a0a2020-08-04 09:51:30 +02004078psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004079 psa_algorithm_t alg,
4080 const uint8_t *input,
4081 size_t input_length,
4082 const uint8_t *salt,
4083 size_t salt_length,
4084 uint8_t *output,
4085 size_t output_size,
4086 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004087{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004088 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004089 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004090 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004091
Darryl Green5cc689a2018-07-24 15:34:10 +01004092 (void) input;
4093 (void) input_length;
4094 (void) salt;
4095 (void) output;
4096 (void) output_size;
4097
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004098 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004099
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004100 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4101 return( PSA_ERROR_INVALID_ARGUMENT );
4102
Ronald Cron5c522922020-11-14 16:35:34 +01004103 status = psa_get_and_lock_transparent_key_slot_with_policy(
4104 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004105 if( status != PSA_SUCCESS )
4106 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004107 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4108 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004109 {
4110 status = PSA_ERROR_INVALID_ARGUMENT;
4111 goto exit;
4112 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004113
John Durkop6ba40d12020-11-10 08:50:04 -08004114#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4115 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004116 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004117 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004118 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004119 status = psa_load_rsa_representation( slot->attr.type,
4120 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004121 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004122 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004123 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004124 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004125
4126 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004127 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004128 status = PSA_ERROR_BUFFER_TOO_SMALL;
4129 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004130 }
John Durkop0e005192020-10-31 22:06:54 -07004131#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004132 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004133 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004134 status = mbedtls_to_psa_error(
4135 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004136 mbedtls_psa_get_random,
4137 mbedtls_psa_random_state( &global_data.rng ),
Steven Cooreman4fed4552020-08-03 14:46:03 +02004138 MBEDTLS_RSA_PUBLIC,
4139 input_length,
4140 input,
4141 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004142 }
4143 else
John Durkop0e005192020-10-31 22:06:54 -07004144#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4145#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004146 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004147 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004148 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004149 status = mbedtls_to_psa_error(
4150 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004151 mbedtls_psa_get_random,
4152 mbedtls_psa_random_state( &global_data.rng ),
Steven Cooreman4fed4552020-08-03 14:46:03 +02004153 MBEDTLS_RSA_PUBLIC,
4154 salt, salt_length,
4155 input_length,
4156 input,
4157 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004158 }
4159 else
John Durkop0e005192020-10-31 22:06:54 -07004160#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004161 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004162 status = PSA_ERROR_INVALID_ARGUMENT;
4163 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004164 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004165rsa_exit:
4166 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004167 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004168
Steven Cooremana2371e52020-07-28 14:30:39 +02004169 mbedtls_rsa_free( rsa );
4170 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004171 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004172 else
John Durkop9814fa22020-11-04 12:28:15 -08004173#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004174 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004175 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004176 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004177 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004178
4179exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004180 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004181
Ronald Cron5c522922020-11-14 16:35:34 +01004182 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004183}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004184
Ronald Croncf56a0a2020-08-04 09:51:30 +02004185psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004186 psa_algorithm_t alg,
4187 const uint8_t *input,
4188 size_t input_length,
4189 const uint8_t *salt,
4190 size_t salt_length,
4191 uint8_t *output,
4192 size_t output_size,
4193 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004194{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004195 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004196 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004197 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004198
Darryl Green5cc689a2018-07-24 15:34:10 +01004199 (void) input;
4200 (void) input_length;
4201 (void) salt;
4202 (void) output;
4203 (void) output_size;
4204
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004205 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004206
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004207 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4208 return( PSA_ERROR_INVALID_ARGUMENT );
4209
Ronald Cron5c522922020-11-14 16:35:34 +01004210 status = psa_get_and_lock_transparent_key_slot_with_policy(
4211 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004212 if( status != PSA_SUCCESS )
4213 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004214 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004215 {
4216 status = PSA_ERROR_INVALID_ARGUMENT;
4217 goto exit;
4218 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004219
John Durkop6ba40d12020-11-10 08:50:04 -08004220#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4221 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004222 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004223 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004224 mbedtls_rsa_context *rsa = NULL;
4225 status = psa_load_rsa_representation( slot->attr.type,
4226 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004227 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004228 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004229 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004230 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004231
Steven Cooremana2371e52020-07-28 14:30:39 +02004232 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004233 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004234 status = PSA_ERROR_INVALID_ARGUMENT;
4235 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004236 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004237
John Durkop0e005192020-10-31 22:06:54 -07004238#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004239 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004240 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004241 status = mbedtls_to_psa_error(
4242 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004243 mbedtls_psa_get_random,
4244 mbedtls_psa_random_state( &global_data.rng ),
Steven Cooreman4fed4552020-08-03 14:46:03 +02004245 MBEDTLS_RSA_PRIVATE,
4246 output_length,
4247 input,
4248 output,
4249 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004250 }
4251 else
John Durkop0e005192020-10-31 22:06:54 -07004252#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4253#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004254 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004255 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004256 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004257 status = mbedtls_to_psa_error(
4258 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004259 mbedtls_psa_get_random,
4260 mbedtls_psa_random_state( &global_data.rng ),
Steven Cooreman4fed4552020-08-03 14:46:03 +02004261 MBEDTLS_RSA_PRIVATE,
4262 salt, salt_length,
4263 output_length,
4264 input,
4265 output,
4266 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004267 }
4268 else
John Durkop0e005192020-10-31 22:06:54 -07004269#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004270 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004271 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004272 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004273
Steven Cooreman4fed4552020-08-03 14:46:03 +02004274rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004275 mbedtls_rsa_free( rsa );
4276 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004277 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004278 else
John Durkop6ba40d12020-11-10 08:50:04 -08004279#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4280 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004281 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004282 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004283 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004284
4285exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004286 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004287
Ronald Cron5c522922020-11-14 16:35:34 +01004288 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004289}
Gilles Peskine20035e32018-02-03 22:44:14 +01004290
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004291
4292
mohammad1603503973b2018-03-12 15:59:30 +02004293/****************************************************************/
4294/* Symmetric cryptography */
4295/****************************************************************/
4296
Gilles Peskinee553c652018-06-04 16:22:46 +02004297static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004298 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004299 psa_algorithm_t alg,
4300 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004301{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004302 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004303 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004304 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004305 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004306 size_t key_bits;
4307 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004308 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4309 PSA_KEY_USAGE_ENCRYPT :
4310 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004311
Steven Cooremand3feccd2020-09-01 15:56:14 +02004312 /* A context must be freshly initialized before it can be set up. */
4313 if( operation->alg != 0 )
4314 return( PSA_ERROR_BAD_STATE );
4315
Steven Cooremana07b9972020-09-10 14:54:14 +02004316 /* The requested algorithm must be one that can be processed by cipher. */
4317 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004318 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004319
Steven Cooremanef8575e2020-09-11 11:44:50 +02004320 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004321 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004322 if( status != PSA_SUCCESS )
4323 goto exit;
4324
4325 /* Initialize the operation struct members, except for alg. The alg member
4326 * is used to indicate to psa_cipher_abort that there are resources to free,
4327 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004328 operation->key_set = 0;
4329 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004330 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004331 operation->iv_size = 0;
4332 operation->block_size = 0;
4333 if( alg == PSA_ALG_ECB_NO_PADDING )
4334 operation->iv_required = 0;
4335 else
4336 operation->iv_required = 1;
4337
Steven Cooremana07b9972020-09-10 14:54:14 +02004338 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004339 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004340 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004341 slot,
4342 alg );
4343 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004344 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004345 slot,
4346 alg );
4347
Steven Cooremanef8575e2020-09-11 11:44:50 +02004348 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004349 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004350 /* Once the driver context is initialised, it needs to be freed using
4351 * psa_cipher_abort. Indicate this through setting alg. */
4352 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004353 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004354
Steven Cooremand3feccd2020-09-01 15:56:14 +02004355 if( status != PSA_ERROR_NOT_SUPPORTED ||
4356 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4357 goto exit;
4358
Steven Cooremana07b9972020-09-10 14:54:14 +02004359 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004360 * available for the given algorithm & key. */
4361 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004362
Steven Cooremana07b9972020-09-10 14:54:14 +02004363 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004364 * psa_cipher_abort. Indicate there is something to be freed through setting
4365 * alg, and indicate the operation is being done using mbedtls crypto through
4366 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004367 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004368 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004369
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004370 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004371 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004372 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004373 {
4374 status = PSA_ERROR_NOT_SUPPORTED;
4375 goto exit;
4376 }
mohammad1603503973b2018-03-12 15:59:30 +02004377
mohammad1603503973b2018-03-12 15:59:30 +02004378 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004379 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004380 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004381
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004382#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004383 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004384 {
4385 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004386 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004387 memcpy( keys, slot->data.key.data, 16 );
4388 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004389 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4390 keys,
4391 192, cipher_operation );
4392 }
4393 else
4394#endif
4395 {
4396 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004397 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004398 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004399 }
Moran Peker41deec42018-04-04 15:43:05 +03004400 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004401 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004402
mohammad16038481e742018-03-18 13:57:31 +02004403#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004404 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004405 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004406 case PSA_ALG_CBC_NO_PADDING:
4407 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4408 MBEDTLS_PADDING_NONE );
4409 break;
4410 case PSA_ALG_CBC_PKCS7:
4411 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4412 MBEDTLS_PADDING_PKCS7 );
4413 break;
4414 default:
4415 /* The algorithm doesn't involve padding. */
4416 ret = 0;
4417 break;
4418 }
4419 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004420 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004421#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4422
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004423 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004424 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004425 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4426 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004427 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004428 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004429 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004430#if defined(MBEDTLS_CHACHA20_C)
4431 else
4432 if( alg == PSA_ALG_CHACHA20 )
4433 operation->iv_size = 12;
4434#endif
mohammad1603503973b2018-03-12 15:59:30 +02004435
Steven Cooreman7df02922020-09-09 15:28:49 +02004436 status = PSA_SUCCESS;
4437
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004438exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004439 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004440 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004441 if( status == PSA_SUCCESS )
4442 {
4443 /* Update operation flags for both driver and software implementations */
4444 operation->key_set = 1;
4445 }
4446 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004447 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004448
Ronald Cron5c522922020-11-14 16:35:34 +01004449 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004450
Ronald Cron5c522922020-11-14 16:35:34 +01004451 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004452}
4453
Gilles Peskinefe119512018-07-08 21:39:34 +02004454psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004455 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004456 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004457{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004458 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004459}
4460
Gilles Peskinefe119512018-07-08 21:39:34 +02004461psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004462 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004463 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004464{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004465 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004466}
4467
Gilles Peskinefe119512018-07-08 21:39:34 +02004468psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004469 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004470 size_t iv_size,
4471 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004472{
itayzafrir534bd7c2018-08-02 13:56:32 +03004473 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004474 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004475 if( operation->iv_set || ! operation->iv_required )
4476 {
4477 return( PSA_ERROR_BAD_STATE );
4478 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004479
Steven Cooremanef8575e2020-09-11 11:44:50 +02004480 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004481 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004482 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004483 iv,
4484 iv_size,
4485 iv_length );
4486 goto exit;
4487 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004488
Moran Peker41deec42018-04-04 15:43:05 +03004489 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004490 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004491 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004492 goto exit;
4493 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01004494 ret = mbedtls_psa_get_random( mbedtls_psa_random_state( &global_data.rng ),
4495 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004496 if( ret != 0 )
4497 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004498 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004499 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004500 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004501
mohammad16038481e742018-03-18 13:57:31 +02004502 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004503 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004504
Moran Peker395db872018-05-31 14:07:14 +03004505exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004506 if( status == PSA_SUCCESS )
4507 operation->iv_set = 1;
4508 else
Moran Peker395db872018-05-31 14:07:14 +03004509 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004510 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004511}
4512
Gilles Peskinefe119512018-07-08 21:39:34 +02004513psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004514 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004515 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004516{
itayzafrir534bd7c2018-08-02 13:56:32 +03004517 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004519 if( operation->iv_set || ! operation->iv_required )
4520 {
4521 return( PSA_ERROR_BAD_STATE );
4522 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004523
Steven Cooremanef8575e2020-09-11 11:44:50 +02004524 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004525 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004526 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004527 iv,
4528 iv_length );
4529 goto exit;
4530 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004531
Moran Pekera28258c2018-05-29 16:25:04 +03004532 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004533 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004534 status = PSA_ERROR_INVALID_ARGUMENT;
4535 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004536 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004537 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4538 status = mbedtls_to_psa_error( ret );
4539exit:
4540 if( status == PSA_SUCCESS )
4541 operation->iv_set = 1;
4542 else
mohammad1603503973b2018-03-12 15:59:30 +02004543 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004544 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004545}
4546
Steven Cooreman177deba2020-09-07 17:14:14 +02004547/* Process input for which the algorithm is set to ECB mode. This requires
4548 * manual processing, since the PSA API is defined as being able to process
4549 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4550 * underlying mbedtls_cipher_update only takes full blocks. */
4551static psa_status_t psa_cipher_update_ecb_internal(
4552 mbedtls_cipher_context_t *ctx,
4553 const uint8_t *input,
4554 size_t input_length,
4555 uint8_t *output,
4556 size_t output_size,
4557 size_t *output_length )
4558{
4559 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4560 size_t block_size = ctx->cipher_info->block_size;
4561 size_t internal_output_length = 0;
4562 *output_length = 0;
4563
4564 if( input_length == 0 )
4565 {
4566 status = PSA_SUCCESS;
4567 goto exit;
4568 }
4569
4570 if( ctx->unprocessed_len > 0 )
4571 {
4572 /* Fill up to block size, and run the block if there's a full one. */
4573 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4574
4575 if( input_length < bytes_to_copy )
4576 bytes_to_copy = input_length;
4577
4578 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4579 input, bytes_to_copy );
4580 input_length -= bytes_to_copy;
4581 input += bytes_to_copy;
4582 ctx->unprocessed_len += bytes_to_copy;
4583
4584 if( ctx->unprocessed_len == block_size )
4585 {
4586 status = mbedtls_to_psa_error(
4587 mbedtls_cipher_update( ctx,
4588 ctx->unprocessed_data,
4589 block_size,
4590 output, &internal_output_length ) );
4591
4592 if( status != PSA_SUCCESS )
4593 goto exit;
4594
4595 output += internal_output_length;
4596 output_size -= internal_output_length;
4597 *output_length += internal_output_length;
4598 ctx->unprocessed_len = 0;
4599 }
4600 }
4601
4602 while( input_length >= block_size )
4603 {
4604 /* Run all full blocks we have, one by one */
4605 status = mbedtls_to_psa_error(
4606 mbedtls_cipher_update( ctx, input,
4607 block_size,
4608 output, &internal_output_length ) );
4609
4610 if( status != PSA_SUCCESS )
4611 goto exit;
4612
4613 input_length -= block_size;
4614 input += block_size;
4615
4616 output += internal_output_length;
4617 output_size -= internal_output_length;
4618 *output_length += internal_output_length;
4619 }
4620
4621 if( input_length > 0 )
4622 {
4623 /* Save unprocessed bytes for later processing */
4624 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4625 input, input_length );
4626 ctx->unprocessed_len += input_length;
4627 }
4628
4629 status = PSA_SUCCESS;
4630
4631exit:
4632 return( status );
4633}
4634
Gilles Peskinee553c652018-06-04 16:22:46 +02004635psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4636 const uint8_t *input,
4637 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004638 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004639 size_t output_size,
4640 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004641{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004642 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004643 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004644 if( operation->alg == 0 )
4645 {
4646 return( PSA_ERROR_BAD_STATE );
4647 }
4648 if( operation->iv_required && ! operation->iv_set )
4649 {
4650 return( PSA_ERROR_BAD_STATE );
4651 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004652
Steven Cooremanef8575e2020-09-11 11:44:50 +02004653 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004654 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004655 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004656 input,
4657 input_length,
4658 output,
4659 output_size,
4660 output_length );
4661 goto exit;
4662 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004663
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004664 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004665 {
4666 /* Take the unprocessed partial block left over from previous
4667 * update calls, if any, plus the input to this call. Remove
4668 * the last partial block, if any. You get the data that will be
4669 * output in this call. */
4670 expected_output_size =
4671 ( operation->ctx.cipher.unprocessed_len + input_length )
4672 / operation->block_size * operation->block_size;
4673 }
4674 else
4675 {
4676 expected_output_size = input_length;
4677 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004678
Gilles Peskine89d789c2018-06-04 17:17:16 +02004679 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004680 {
4681 status = PSA_ERROR_BUFFER_TOO_SMALL;
4682 goto exit;
4683 }
mohammad160382759612018-03-12 18:16:40 +02004684
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004685 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4686 {
4687 /* mbedtls_cipher_update has an API inconsistency: it will only
4688 * process a single block at a time in ECB mode. Abstract away that
4689 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004690 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4691 input,
4692 input_length,
4693 output,
4694 output_size,
4695 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004696 }
4697 else
4698 {
4699 status = mbedtls_to_psa_error(
4700 mbedtls_cipher_update( &operation->ctx.cipher, input,
4701 input_length, output, output_length ) );
4702 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004703exit:
4704 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004705 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004706 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004707}
4708
Gilles Peskinee553c652018-06-04 16:22:46 +02004709psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4710 uint8_t *output,
4711 size_t output_size,
4712 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004713{
David Saadab4ecc272019-02-14 13:48:10 +02004714 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004715 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004716 if( operation->alg == 0 )
4717 {
4718 return( PSA_ERROR_BAD_STATE );
4719 }
4720 if( operation->iv_required && ! operation->iv_set )
4721 {
4722 return( PSA_ERROR_BAD_STATE );
4723 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004724
Steven Cooremanef8575e2020-09-11 11:44:50 +02004725 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004726 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004727 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004728 output,
4729 output_size,
4730 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004731 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004732 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004733
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004734 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004735 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004736 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004737 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004738 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004739 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004740 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004741 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004742 }
4743
Steven Cooremanef8575e2020-09-11 11:44:50 +02004744 status = mbedtls_to_psa_error(
4745 mbedtls_cipher_finish( &operation->ctx.cipher,
4746 temp_output_buffer,
4747 output_length ) );
4748 if( status != PSA_SUCCESS )
4749 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004750
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004751 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004752 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004753 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004754 memcpy( output, temp_output_buffer, *output_length );
4755 else
Janos Follath315b51c2018-07-09 16:04:51 +01004756 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004757
Steven Cooremanef8575e2020-09-11 11:44:50 +02004758exit:
4759 if( operation->mbedtls_in_use == 1 )
4760 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004761
Steven Cooremanef8575e2020-09-11 11:44:50 +02004762 if( status == PSA_SUCCESS )
4763 return( psa_cipher_abort( operation ) );
4764 else
4765 {
4766 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004767 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004768
Steven Cooremanef8575e2020-09-11 11:44:50 +02004769 return( status );
4770 }
mohammad1603503973b2018-03-12 15:59:30 +02004771}
4772
Gilles Peskinee553c652018-06-04 16:22:46 +02004773psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4774{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004775 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004776 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004777 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004778 * in use. It's ok to call abort on such an object, and there's
4779 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004780 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004781 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004782
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004783 /* Sanity check (shouldn't happen: operation->alg should
4784 * always have been initialized to a valid value). */
4785 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4786 return( PSA_ERROR_BAD_STATE );
4787
Steven Cooremanef8575e2020-09-11 11:44:50 +02004788 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004789 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004790 else
4791 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004792
Moran Peker41deec42018-04-04 15:43:05 +03004793 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004794 operation->key_set = 0;
4795 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004796 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004797 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004798 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004799 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004800
Moran Peker395db872018-05-31 14:07:14 +03004801 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004802}
4803
Gilles Peskinea0655c32018-04-30 17:06:50 +02004804
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004805
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004806
mohammad16035955c982018-04-26 00:53:03 +03004807/****************************************************************/
4808/* AEAD */
4809/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004810
Gilles Peskineedf9a652018-08-17 18:11:56 +02004811typedef struct
4812{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004813 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004814 const mbedtls_cipher_info_t *cipher_info;
4815 union
4816 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004817 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004818#if defined(MBEDTLS_CCM_C)
4819 mbedtls_ccm_context ccm;
4820#endif /* MBEDTLS_CCM_C */
4821#if defined(MBEDTLS_GCM_C)
4822 mbedtls_gcm_context gcm;
4823#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004824#if defined(MBEDTLS_CHACHAPOLY_C)
4825 mbedtls_chachapoly_context chachapoly;
4826#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004827 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004828 psa_algorithm_t core_alg;
4829 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004830 uint8_t tag_length;
4831} aead_operation_t;
4832
Ronald Cronf95a2b12020-10-22 15:24:49 +02004833#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4834
Gilles Peskine30a9e412019-01-14 18:36:12 +01004835static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004836{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004837 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004838 {
4839#if defined(MBEDTLS_CCM_C)
4840 case PSA_ALG_CCM:
4841 mbedtls_ccm_free( &operation->ctx.ccm );
4842 break;
4843#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004844#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004845 case PSA_ALG_GCM:
4846 mbedtls_gcm_free( &operation->ctx.gcm );
4847 break;
4848#endif /* MBEDTLS_GCM_C */
4849 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004850
Ronald Cron5c522922020-11-14 16:35:34 +01004851 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004852}
4853
4854static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004855 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004856 psa_key_usage_t usage,
4857 psa_algorithm_t alg )
4858{
4859 psa_status_t status;
4860 size_t key_bits;
4861 mbedtls_cipher_id_t cipher_id;
4862
Ronald Cron5c522922020-11-14 16:35:34 +01004863 status = psa_get_and_lock_transparent_key_slot_with_policy(
4864 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004865 if( status != PSA_SUCCESS )
4866 return( status );
4867
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004868 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004869
4870 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004871 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004872 &cipher_id );
4873 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004874 {
4875 status = PSA_ERROR_NOT_SUPPORTED;
4876 goto cleanup;
4877 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004878
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004879 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004880 {
4881#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004882 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4883 operation->core_alg = PSA_ALG_CCM;
4884 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004885 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4886 * The call to mbedtls_ccm_encrypt_and_tag or
4887 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004888 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004889 {
4890 status = PSA_ERROR_INVALID_ARGUMENT;
4891 goto cleanup;
4892 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004893 mbedtls_ccm_init( &operation->ctx.ccm );
4894 status = mbedtls_to_psa_error(
4895 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004896 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004897 (unsigned int) key_bits ) );
4898 if( status != 0 )
4899 goto cleanup;
4900 break;
4901#endif /* MBEDTLS_CCM_C */
4902
4903#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004904 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4905 operation->core_alg = PSA_ALG_GCM;
4906 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004907 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4908 * The call to mbedtls_gcm_crypt_and_tag or
4909 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004910 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004911 {
4912 status = PSA_ERROR_INVALID_ARGUMENT;
4913 goto cleanup;
4914 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004915 mbedtls_gcm_init( &operation->ctx.gcm );
4916 status = mbedtls_to_psa_error(
4917 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004918 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004919 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004920 if( status != 0 )
4921 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004922 break;
4923#endif /* MBEDTLS_GCM_C */
4924
Gilles Peskine26869f22019-05-06 15:25:00 +02004925#if defined(MBEDTLS_CHACHAPOLY_C)
4926 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4927 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4928 operation->full_tag_length = 16;
4929 /* We only support the default tag length. */
4930 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004931 {
4932 status = PSA_ERROR_NOT_SUPPORTED;
4933 goto cleanup;
4934 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004935 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4936 status = mbedtls_to_psa_error(
4937 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004938 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004939 if( status != 0 )
4940 goto cleanup;
4941 break;
4942#endif /* MBEDTLS_CHACHAPOLY_C */
4943
Gilles Peskineedf9a652018-08-17 18:11:56 +02004944 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004945 status = PSA_ERROR_NOT_SUPPORTED;
4946 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004947 }
4948
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004949 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4950 {
4951 status = PSA_ERROR_INVALID_ARGUMENT;
4952 goto cleanup;
4953 }
4954 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004955
Gilles Peskineedf9a652018-08-17 18:11:56 +02004956 return( PSA_SUCCESS );
4957
4958cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004959 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004960 return( status );
4961}
4962
Ronald Croncf56a0a2020-08-04 09:51:30 +02004963psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004964 psa_algorithm_t alg,
4965 const uint8_t *nonce,
4966 size_t nonce_length,
4967 const uint8_t *additional_data,
4968 size_t additional_data_length,
4969 const uint8_t *plaintext,
4970 size_t plaintext_length,
4971 uint8_t *ciphertext,
4972 size_t ciphertext_size,
4973 size_t *ciphertext_length )
4974{
mohammad16035955c982018-04-26 00:53:03 +03004975 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004976 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004977 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004978
mohammad1603f08a5502018-06-03 15:05:47 +03004979 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004980
Ronald Croncf56a0a2020-08-04 09:51:30 +02004981 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004982 if( status != PSA_SUCCESS )
4983 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004984
Gilles Peskineedf9a652018-08-17 18:11:56 +02004985 /* For all currently supported modes, the tag is at the end of the
4986 * ciphertext. */
4987 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4988 {
4989 status = PSA_ERROR_BUFFER_TOO_SMALL;
4990 goto exit;
4991 }
4992 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004993
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004994#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004995 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004996 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004997 status = mbedtls_to_psa_error(
4998 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4999 MBEDTLS_GCM_ENCRYPT,
5000 plaintext_length,
5001 nonce, nonce_length,
5002 additional_data, additional_data_length,
5003 plaintext, ciphertext,
5004 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005005 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005006 else
5007#endif /* MBEDTLS_GCM_C */
5008#if defined(MBEDTLS_CCM_C)
5009 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005010 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005011 status = mbedtls_to_psa_error(
5012 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5013 plaintext_length,
5014 nonce, nonce_length,
5015 additional_data,
5016 additional_data_length,
5017 plaintext, ciphertext,
5018 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005019 }
mohammad16035c8845f2018-05-09 05:40:09 -07005020 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005021#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005022#if defined(MBEDTLS_CHACHAPOLY_C)
5023 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5024 {
5025 if( nonce_length != 12 || operation.tag_length != 16 )
5026 {
5027 status = PSA_ERROR_NOT_SUPPORTED;
5028 goto exit;
5029 }
5030 status = mbedtls_to_psa_error(
5031 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5032 plaintext_length,
5033 nonce,
5034 additional_data,
5035 additional_data_length,
5036 plaintext,
5037 ciphertext,
5038 tag ) );
5039 }
5040 else
5041#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005042 {
mohammad1603554faad2018-06-03 15:07:38 +03005043 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005044 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005045
Gilles Peskineedf9a652018-08-17 18:11:56 +02005046 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5047 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005048
Gilles Peskineedf9a652018-08-17 18:11:56 +02005049exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005050 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005051 if( status == PSA_SUCCESS )
5052 *ciphertext_length = plaintext_length + operation.tag_length;
5053 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005054}
5055
Gilles Peskineee652a32018-06-01 19:23:52 +02005056/* Locate the tag in a ciphertext buffer containing the encrypted data
5057 * followed by the tag. Return the length of the part preceding the tag in
5058 * *plaintext_length. This is the size of the plaintext in modes where
5059 * the encrypted data has the same size as the plaintext, such as
5060 * CCM and GCM. */
5061static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5062 const uint8_t *ciphertext,
5063 size_t ciphertext_length,
5064 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005065 const uint8_t **p_tag )
5066{
5067 size_t payload_length;
5068 if( tag_length > ciphertext_length )
5069 return( PSA_ERROR_INVALID_ARGUMENT );
5070 payload_length = ciphertext_length - tag_length;
5071 if( payload_length > plaintext_size )
5072 return( PSA_ERROR_BUFFER_TOO_SMALL );
5073 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005074 return( PSA_SUCCESS );
5075}
5076
Ronald Croncf56a0a2020-08-04 09:51:30 +02005077psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005078 psa_algorithm_t alg,
5079 const uint8_t *nonce,
5080 size_t nonce_length,
5081 const uint8_t *additional_data,
5082 size_t additional_data_length,
5083 const uint8_t *ciphertext,
5084 size_t ciphertext_length,
5085 uint8_t *plaintext,
5086 size_t plaintext_size,
5087 size_t *plaintext_length )
5088{
mohammad16035955c982018-04-26 00:53:03 +03005089 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005090 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005091 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005092
Gilles Peskineee652a32018-06-01 19:23:52 +02005093 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005094
Ronald Croncf56a0a2020-08-04 09:51:30 +02005095 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005096 if( status != PSA_SUCCESS )
5097 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005098
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005099 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5100 ciphertext, ciphertext_length,
5101 plaintext_size, &tag );
5102 if( status != PSA_SUCCESS )
5103 goto exit;
5104
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005105#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005106 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005107 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005108 status = mbedtls_to_psa_error(
5109 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5110 ciphertext_length - operation.tag_length,
5111 nonce, nonce_length,
5112 additional_data,
5113 additional_data_length,
5114 tag, operation.tag_length,
5115 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005116 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005117 else
5118#endif /* MBEDTLS_GCM_C */
5119#if defined(MBEDTLS_CCM_C)
5120 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005121 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005122 status = mbedtls_to_psa_error(
5123 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5124 ciphertext_length - operation.tag_length,
5125 nonce, nonce_length,
5126 additional_data,
5127 additional_data_length,
5128 ciphertext, plaintext,
5129 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005130 }
mohammad160339574652018-06-01 04:39:53 -07005131 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005132#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005133#if defined(MBEDTLS_CHACHAPOLY_C)
5134 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5135 {
5136 if( nonce_length != 12 || operation.tag_length != 16 )
5137 {
5138 status = PSA_ERROR_NOT_SUPPORTED;
5139 goto exit;
5140 }
5141 status = mbedtls_to_psa_error(
5142 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5143 ciphertext_length - operation.tag_length,
5144 nonce,
5145 additional_data,
5146 additional_data_length,
5147 tag,
5148 ciphertext,
5149 plaintext ) );
5150 }
5151 else
5152#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005153 {
mohammad1603554faad2018-06-03 15:07:38 +03005154 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005155 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005156
Gilles Peskineedf9a652018-08-17 18:11:56 +02005157 if( status != PSA_SUCCESS && plaintext_size != 0 )
5158 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005159
Gilles Peskineedf9a652018-08-17 18:11:56 +02005160exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005161 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005162 if( status == PSA_SUCCESS )
5163 *plaintext_length = ciphertext_length - operation.tag_length;
5164 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005165}
5166
Gilles Peskinea0655c32018-04-30 17:06:50 +02005167
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005168
Gilles Peskine20035e32018-02-03 22:44:14 +01005169/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005170/* Generators */
5171/****************************************************************/
5172
John Durkop07cc04a2020-11-16 22:08:34 -08005173#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5174 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5175 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5176#define AT_LEAST_ONE_BUILTIN_KDF
5177#endif
5178
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005179#define HKDF_STATE_INIT 0 /* no input yet */
5180#define HKDF_STATE_STARTED 1 /* got salt */
5181#define HKDF_STATE_KEYED 2 /* got key */
5182#define HKDF_STATE_OUTPUT 3 /* output started */
5183
Gilles Peskinecbe66502019-05-16 16:59:18 +02005184static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005185 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005186{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005187 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5188 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005189 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005190 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005191}
5192
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005193psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005194{
5195 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005196 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005197 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005198 {
5199 /* The object has (apparently) been initialized but it is not
5200 * in use. It's ok to call abort on such an object, and there's
5201 * nothing to do. */
5202 }
5203 else
John Durkopd0321952020-10-29 21:37:36 -07005204#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005205 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005206 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005207 mbedtls_free( operation->ctx.hkdf.info );
5208 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005209 }
John Durkop07cc04a2020-11-16 22:08:34 -08005210 else
5211#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5212#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5213 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5214 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005215 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005216 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005217 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005218 if( operation->ctx.tls12_prf.seed != NULL )
5219 {
5220 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5221 operation->ctx.tls12_prf.seed_length );
5222 mbedtls_free( operation->ctx.tls12_prf.seed );
5223 }
5224
5225 if( operation->ctx.tls12_prf.label != NULL )
5226 {
5227 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5228 operation->ctx.tls12_prf.label_length );
5229 mbedtls_free( operation->ctx.tls12_prf.label );
5230 }
5231
5232 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5233
5234 /* We leave the fields Ai and output_block to be erased safely by the
5235 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005236 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005237 else
John Durkop07cc04a2020-11-16 22:08:34 -08005238#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5239 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005240 {
5241 status = PSA_ERROR_BAD_STATE;
5242 }
Janos Follath083036a2019-06-11 10:22:26 +01005243 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005244 return( status );
5245}
5246
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005247psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005248 size_t *capacity)
5249{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005250 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005251 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005252 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005253 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005254 }
5255
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005256 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005257 return( PSA_SUCCESS );
5258}
5259
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005260psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005261 size_t capacity )
5262{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005263 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005264 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005265 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005266 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005268 return( PSA_SUCCESS );
5269}
5270
John Durkopd0321952020-10-29 21:37:36 -07005271#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005273 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005274static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005275 psa_algorithm_t hash_alg,
5276 uint8_t *output,
5277 size_t output_length )
5278{
5279 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5280 psa_status_t status;
5281
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005282 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5283 return( PSA_ERROR_BAD_STATE );
5284 hkdf->state = HKDF_STATE_OUTPUT;
5285
Gilles Peskinebef7f142018-07-12 17:22:21 +02005286 while( output_length != 0 )
5287 {
5288 /* Copy what remains of the current block */
5289 uint8_t n = hash_length - hkdf->offset_in_block;
5290 if( n > output_length )
5291 n = (uint8_t) output_length;
5292 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5293 output += n;
5294 output_length -= n;
5295 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005296 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005297 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005298 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005299 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005300 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005301 * object was corrupted or if this function is called directly
5302 * inside the library. */
5303 if( hkdf->block_number == 0xff )
5304 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005305
5306 /* We need a new block */
5307 ++hkdf->block_number;
5308 hkdf->offset_in_block = 0;
5309 status = psa_hmac_setup_internal( &hkdf->hmac,
5310 hkdf->prk, hash_length,
5311 hash_alg );
5312 if( status != PSA_SUCCESS )
5313 return( status );
5314 if( hkdf->block_number != 1 )
5315 {
5316 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5317 hkdf->output_block,
5318 hash_length );
5319 if( status != PSA_SUCCESS )
5320 return( status );
5321 }
5322 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5323 hkdf->info,
5324 hkdf->info_length );
5325 if( status != PSA_SUCCESS )
5326 return( status );
5327 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5328 &hkdf->block_number, 1 );
5329 if( status != PSA_SUCCESS )
5330 return( status );
5331 status = psa_hmac_finish_internal( &hkdf->hmac,
5332 hkdf->output_block,
5333 sizeof( hkdf->output_block ) );
5334 if( status != PSA_SUCCESS )
5335 return( status );
5336 }
5337
5338 return( PSA_SUCCESS );
5339}
John Durkop07cc04a2020-11-16 22:08:34 -08005340#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005341
John Durkop07cc04a2020-11-16 22:08:34 -08005342#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5343 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005344static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5345 psa_tls12_prf_key_derivation_t *tls12_prf,
5346 psa_algorithm_t alg )
5347{
5348 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5349 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005350 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5351 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005352
Janos Follath7742fee2019-06-17 12:58:10 +01005353 /* We can't be wanting more output after block 0xff, otherwise
5354 * the capacity check in psa_key_derivation_output_bytes() would have
5355 * prevented this call. It could happen only if the operation
5356 * object was corrupted or if this function is called directly
5357 * inside the library. */
5358 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005359 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005360
5361 /* We need a new block */
5362 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005363 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005364
5365 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5366 *
5367 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5368 *
5369 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5370 * HMAC_hash(secret, A(2) + seed) +
5371 * HMAC_hash(secret, A(3) + seed) + ...
5372 *
5373 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005374 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005375 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005376 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005377 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005378 * is currently extracted as `output_block` and where i is
5379 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005380 */
5381
Janos Follathea29bfb2019-06-19 12:21:20 +01005382 /* Save the hash context before using it, to preserve the hash state with
5383 * only the inner padding in it. We need this, because inner padding depends
5384 * on the key (secret in the RFC's terminology). */
5385 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5386 if( status != PSA_SUCCESS )
5387 goto cleanup;
5388
5389 /* Calculate A(i) where i = tls12_prf->block_number. */
5390 if( tls12_prf->block_number == 1 )
5391 {
5392 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5393 * the variable seed and in this instance means it in the context of the
5394 * P_hash function, where seed = label + seed.) */
5395 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5396 tls12_prf->label, tls12_prf->label_length );
5397 if( status != PSA_SUCCESS )
5398 goto cleanup;
5399 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5400 tls12_prf->seed, tls12_prf->seed_length );
5401 if( status != PSA_SUCCESS )
5402 goto cleanup;
5403 }
5404 else
5405 {
5406 /* A(i) = HMAC_hash(secret, A(i-1)) */
5407 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5408 tls12_prf->Ai, hash_length );
5409 if( status != PSA_SUCCESS )
5410 goto cleanup;
5411 }
5412
5413 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5414 tls12_prf->Ai, hash_length );
5415 if( status != PSA_SUCCESS )
5416 goto cleanup;
5417 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5418 if( status != PSA_SUCCESS )
5419 goto cleanup;
5420
5421 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5422 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5423 tls12_prf->Ai, hash_length );
5424 if( status != PSA_SUCCESS )
5425 goto cleanup;
5426 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5427 tls12_prf->label, tls12_prf->label_length );
5428 if( status != PSA_SUCCESS )
5429 goto cleanup;
5430 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5431 tls12_prf->seed, tls12_prf->seed_length );
5432 if( status != PSA_SUCCESS )
5433 goto cleanup;
5434 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5435 tls12_prf->output_block, hash_length );
5436 if( status != PSA_SUCCESS )
5437 goto cleanup;
5438 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5439 if( status != PSA_SUCCESS )
5440 goto cleanup;
5441
Janos Follath7742fee2019-06-17 12:58:10 +01005442
5443cleanup:
5444
Janos Follathea29bfb2019-06-19 12:21:20 +01005445 cleanup_status = psa_hash_abort( &backup );
5446 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5447 status = cleanup_status;
5448
5449 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005450}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005451
Janos Follath844eb0e2019-06-19 12:10:49 +01005452static psa_status_t psa_key_derivation_tls12_prf_read(
5453 psa_tls12_prf_key_derivation_t *tls12_prf,
5454 psa_algorithm_t alg,
5455 uint8_t *output,
5456 size_t output_length )
5457{
5458 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5459 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5460 psa_status_t status;
5461 uint8_t offset, length;
5462
5463 while( output_length != 0 )
5464 {
5465 /* Check if we have fully processed the current block. */
5466 if( tls12_prf->left_in_block == 0 )
5467 {
5468 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5469 alg );
5470 if( status != PSA_SUCCESS )
5471 return( status );
5472
5473 continue;
5474 }
5475
5476 if( tls12_prf->left_in_block > output_length )
5477 length = (uint8_t) output_length;
5478 else
5479 length = tls12_prf->left_in_block;
5480
5481 offset = hash_length - tls12_prf->left_in_block;
5482 memcpy( output, tls12_prf->output_block + offset, length );
5483 output += length;
5484 output_length -= length;
5485 tls12_prf->left_in_block -= length;
5486 }
5487
5488 return( PSA_SUCCESS );
5489}
John Durkop07cc04a2020-11-16 22:08:34 -08005490#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5491 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005492
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005493psa_status_t psa_key_derivation_output_bytes(
5494 psa_key_derivation_operation_t *operation,
5495 uint8_t *output,
5496 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005497{
5498 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005499 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005500
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005501 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005502 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005503 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005504 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005505 }
5506
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005507 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005508 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005509 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005510 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005511 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005512 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005513 goto exit;
5514 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005515 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005516 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005517 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005518 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005519 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5520 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005521 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005522 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005523 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005524 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005526
John Durkopd0321952020-10-29 21:37:36 -07005527#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005528 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005529 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005530 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005531 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005532 output, output_length );
5533 }
Janos Follathadbec812019-06-14 11:05:39 +01005534 else
John Durkop07cc04a2020-11-16 22:08:34 -08005535#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5536#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5537 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005538 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005539 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005540 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005541 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005542 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005543 output_length );
5544 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005545 else
John Durkop07cc04a2020-11-16 22:08:34 -08005546#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5547 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005548 {
5549 return( PSA_ERROR_BAD_STATE );
5550 }
5551
5552exit:
5553 if( status != PSA_SUCCESS )
5554 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005555 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005556 * This allows us to differentiate between exhausted operations and
5557 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5558 * operations. */
5559 psa_algorithm_t alg = operation->alg;
5560 psa_key_derivation_abort( operation );
5561 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005562 memset( output, '!', output_length );
5563 }
5564 return( status );
5565}
5566
Gilles Peskine08542d82018-07-19 17:05:42 +02005567#if defined(MBEDTLS_DES_C)
5568static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5569{
5570 if( data_size >= 8 )
5571 mbedtls_des_key_set_parity( data );
5572 if( data_size >= 16 )
5573 mbedtls_des_key_set_parity( data + 8 );
5574 if( data_size >= 24 )
5575 mbedtls_des_key_set_parity( data + 16 );
5576}
5577#endif /* MBEDTLS_DES_C */
5578
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005579static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005580 psa_key_slot_t *slot,
5581 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005582 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005583{
5584 uint8_t *data = NULL;
5585 size_t bytes = PSA_BITS_TO_BYTES( bits );
5586 psa_status_t status;
5587
Gilles Peskine8e338702019-07-30 20:06:31 +02005588 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005589 return( PSA_ERROR_INVALID_ARGUMENT );
5590 if( bits % 8 != 0 )
5591 return( PSA_ERROR_INVALID_ARGUMENT );
5592 data = mbedtls_calloc( 1, bytes );
5593 if( data == NULL )
5594 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5595
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005596 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005597 if( status != PSA_SUCCESS )
5598 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005599#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005600 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005601 psa_des_set_key_parity( data, bytes );
5602#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005603 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005604
5605exit:
5606 mbedtls_free( data );
5607 return( status );
5608}
5609
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005610psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005611 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005612 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005613{
5614 psa_status_t status;
5615 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005616 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005617
Ronald Cron81709fc2020-11-14 12:10:32 +01005618 *key = MBEDTLS_SVC_KEY_ID_INIT;
5619
Gilles Peskine0f84d622019-09-12 19:03:13 +02005620 /* Reject any attempt to create a zero-length key so that we don't
5621 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5622 if( psa_get_key_bits( attributes ) == 0 )
5623 return( PSA_ERROR_INVALID_ARGUMENT );
5624
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005625 if( ! operation->can_output_key )
5626 return( PSA_ERROR_NOT_PERMITTED );
5627
Ronald Cron81709fc2020-11-14 12:10:32 +01005628 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5629 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005630#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5631 if( driver != NULL )
5632 {
5633 /* Deriving a key in a secure element is not implemented yet. */
5634 status = PSA_ERROR_NOT_SUPPORTED;
5635 }
5636#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005637 if( status == PSA_SUCCESS )
5638 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005639 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005640 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005641 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005642 }
5643 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005644 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005645 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005646 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005647
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005648 return( status );
5649}
5650
Gilles Peskineeab56e42018-07-12 17:12:33 +02005651
5652
5653/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005654/* Key derivation */
5655/****************************************************************/
5656
John Durkop07cc04a2020-11-16 22:08:34 -08005657#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005658static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005659 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005660 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005661{
John Durkop07cc04a2020-11-16 22:08:34 -08005662 int is_kdf_alg_supported;
5663
Janos Follath5fe19732019-06-20 15:09:30 +01005664 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5665 * initialisers for this union leave some bytes unspecified.) */
5666 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5667
Gilles Peskine969c5d62019-01-16 15:53:06 +01005668 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005669#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005670 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5671 is_kdf_alg_supported = 1;
5672 else
5673#endif
5674#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5675 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5676 is_kdf_alg_supported = 1;
5677 else
5678#endif
5679#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5680 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5681 is_kdf_alg_supported = 1;
5682 else
5683#endif
5684 is_kdf_alg_supported = 0;
5685
5686 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005687 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005688 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005689 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5690 if( hash_size == 0 )
5691 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005692 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5693 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005694 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005695 {
5696 return( PSA_ERROR_NOT_SUPPORTED );
5697 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005698 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005699 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005700 }
John Durkop07cc04a2020-11-16 22:08:34 -08005701
5702 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005703}
John Durkop07cc04a2020-11-16 22:08:34 -08005704#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005705
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005706psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005707 psa_algorithm_t alg )
5708{
5709 psa_status_t status;
5710
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005711 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005712 return( PSA_ERROR_BAD_STATE );
5713
Gilles Peskine6843c292019-01-18 16:44:49 +01005714 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5715 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005716#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005717 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005718 {
5719 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005720 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005721 }
5722 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5723 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005724 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005725 }
John Durkop07cc04a2020-11-16 22:08:34 -08005726#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005727 else
5728 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005729
5730 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005731 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005732 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005733}
5734
John Durkopd0321952020-10-29 21:37:36 -07005735#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005736static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005737 psa_algorithm_t hash_alg,
5738 psa_key_derivation_step_t step,
5739 const uint8_t *data,
5740 size_t data_length )
5741{
5742 psa_status_t status;
5743 switch( step )
5744 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005745 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005746 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005747 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005748 status = psa_hmac_setup_internal( &hkdf->hmac,
5749 data, data_length,
5750 hash_alg );
5751 if( status != PSA_SUCCESS )
5752 return( status );
5753 hkdf->state = HKDF_STATE_STARTED;
5754 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005755 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005756 /* If no salt was provided, use an empty salt. */
5757 if( hkdf->state == HKDF_STATE_INIT )
5758 {
5759 status = psa_hmac_setup_internal( &hkdf->hmac,
5760 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005761 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005762 if( status != PSA_SUCCESS )
5763 return( status );
5764 hkdf->state = HKDF_STATE_STARTED;
5765 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005766 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005767 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005768 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5769 data, data_length );
5770 if( status != PSA_SUCCESS )
5771 return( status );
5772 status = psa_hmac_finish_internal( &hkdf->hmac,
5773 hkdf->prk,
5774 sizeof( hkdf->prk ) );
5775 if( status != PSA_SUCCESS )
5776 return( status );
5777 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5778 hkdf->block_number = 0;
5779 hkdf->state = HKDF_STATE_KEYED;
5780 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005781 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005782 if( hkdf->state == HKDF_STATE_OUTPUT )
5783 return( PSA_ERROR_BAD_STATE );
5784 if( hkdf->info_set )
5785 return( PSA_ERROR_BAD_STATE );
5786 hkdf->info_length = data_length;
5787 if( data_length != 0 )
5788 {
5789 hkdf->info = mbedtls_calloc( 1, data_length );
5790 if( hkdf->info == NULL )
5791 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5792 memcpy( hkdf->info, data, data_length );
5793 }
5794 hkdf->info_set = 1;
5795 return( PSA_SUCCESS );
5796 default:
5797 return( PSA_ERROR_INVALID_ARGUMENT );
5798 }
5799}
John Durkop07cc04a2020-11-16 22:08:34 -08005800#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005801
John Durkop07cc04a2020-11-16 22:08:34 -08005802#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5803 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005804static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5805 const uint8_t *data,
5806 size_t data_length )
5807{
5808 if( prf->state != TLS12_PRF_STATE_INIT )
5809 return( PSA_ERROR_BAD_STATE );
5810
Janos Follathd6dce9f2019-07-04 09:11:38 +01005811 if( data_length != 0 )
5812 {
5813 prf->seed = mbedtls_calloc( 1, data_length );
5814 if( prf->seed == NULL )
5815 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005816
Janos Follathd6dce9f2019-07-04 09:11:38 +01005817 memcpy( prf->seed, data, data_length );
5818 prf->seed_length = data_length;
5819 }
Janos Follathf08e2652019-06-13 09:05:41 +01005820
5821 prf->state = TLS12_PRF_STATE_SEED_SET;
5822
5823 return( PSA_SUCCESS );
5824}
5825
Janos Follath81550542019-06-13 14:26:34 +01005826static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5827 psa_algorithm_t hash_alg,
5828 const uint8_t *data,
5829 size_t data_length )
5830{
5831 psa_status_t status;
5832 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5833 return( PSA_ERROR_BAD_STATE );
5834
5835 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5836 if( status != PSA_SUCCESS )
5837 return( status );
5838
5839 prf->state = TLS12_PRF_STATE_KEY_SET;
5840
5841 return( PSA_SUCCESS );
5842}
5843
Janos Follath63028dd2019-06-13 09:15:47 +01005844static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5845 const uint8_t *data,
5846 size_t data_length )
5847{
5848 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5849 return( PSA_ERROR_BAD_STATE );
5850
Janos Follathd6dce9f2019-07-04 09:11:38 +01005851 if( data_length != 0 )
5852 {
5853 prf->label = mbedtls_calloc( 1, data_length );
5854 if( prf->label == NULL )
5855 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005856
Janos Follathd6dce9f2019-07-04 09:11:38 +01005857 memcpy( prf->label, data, data_length );
5858 prf->label_length = data_length;
5859 }
Janos Follath63028dd2019-06-13 09:15:47 +01005860
5861 prf->state = TLS12_PRF_STATE_LABEL_SET;
5862
5863 return( PSA_SUCCESS );
5864}
5865
Janos Follathb03233e2019-06-11 15:30:30 +01005866static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5867 psa_algorithm_t hash_alg,
5868 psa_key_derivation_step_t step,
5869 const uint8_t *data,
5870 size_t data_length )
5871{
Janos Follathb03233e2019-06-11 15:30:30 +01005872 switch( step )
5873 {
Janos Follathf08e2652019-06-13 09:05:41 +01005874 case PSA_KEY_DERIVATION_INPUT_SEED:
5875 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005876 case PSA_KEY_DERIVATION_INPUT_SECRET:
5877 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005878 case PSA_KEY_DERIVATION_INPUT_LABEL:
5879 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005880 default:
5881 return( PSA_ERROR_INVALID_ARGUMENT );
5882 }
5883}
John Durkop07cc04a2020-11-16 22:08:34 -08005884#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5885 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5886
5887#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5888static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5889 psa_tls12_prf_key_derivation_t *prf,
5890 psa_algorithm_t hash_alg,
5891 const uint8_t *data,
5892 size_t data_length )
5893{
5894 psa_status_t status;
5895 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5896 uint8_t *cur = pms;
5897
5898 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5899 return( PSA_ERROR_INVALID_ARGUMENT );
5900
5901 /* Quoting RFC 4279, Section 2:
5902 *
5903 * The premaster secret is formed as follows: if the PSK is N octets
5904 * long, concatenate a uint16 with the value N, N zero octets, a second
5905 * uint16 with the value N, and the PSK itself.
5906 */
5907
5908 *cur++ = ( data_length >> 8 ) & 0xff;
5909 *cur++ = ( data_length >> 0 ) & 0xff;
5910 memset( cur, 0, data_length );
5911 cur += data_length;
5912 *cur++ = pms[0];
5913 *cur++ = pms[1];
5914 memcpy( cur, data, data_length );
5915 cur += data_length;
5916
5917 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5918
5919 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5920 return( status );
5921}
Janos Follath6660f0e2019-06-17 08:44:03 +01005922
5923static psa_status_t psa_tls12_prf_psk_to_ms_input(
5924 psa_tls12_prf_key_derivation_t *prf,
5925 psa_algorithm_t hash_alg,
5926 psa_key_derivation_step_t step,
5927 const uint8_t *data,
5928 size_t data_length )
5929{
5930 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005931 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005932 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5933 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005934 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005935
5936 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5937}
John Durkop07cc04a2020-11-16 22:08:34 -08005938#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005939
Gilles Peskineb8965192019-09-24 16:21:10 +02005940/** Check whether the given key type is acceptable for the given
5941 * input step of a key derivation.
5942 *
5943 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5944 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5945 * Both secret and non-secret inputs can alternatively have the type
5946 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5947 * that the input was passed as a buffer rather than via a key object.
5948 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005949static int psa_key_derivation_check_input_type(
5950 psa_key_derivation_step_t step,
5951 psa_key_type_t key_type )
5952{
5953 switch( step )
5954 {
5955 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005956 if( key_type == PSA_KEY_TYPE_DERIVE )
5957 return( PSA_SUCCESS );
5958 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005959 return( PSA_SUCCESS );
5960 break;
5961 case PSA_KEY_DERIVATION_INPUT_LABEL:
5962 case PSA_KEY_DERIVATION_INPUT_SALT:
5963 case PSA_KEY_DERIVATION_INPUT_INFO:
5964 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005965 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5966 return( PSA_SUCCESS );
5967 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005968 return( PSA_SUCCESS );
5969 break;
5970 }
5971 return( PSA_ERROR_INVALID_ARGUMENT );
5972}
5973
Janos Follathb80a94e2019-06-12 15:54:46 +01005974static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005975 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005976 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005977 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005978 const uint8_t *data,
5979 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005980{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005981 psa_status_t status;
5982 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5983
5984 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005985 if( status != PSA_SUCCESS )
5986 goto exit;
5987
John Durkopd0321952020-10-29 21:37:36 -07005988#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005989 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005990 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005991 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005992 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005993 step, data, data_length );
5994 }
John Durkop07cc04a2020-11-16 22:08:34 -08005995 else
5996#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5997#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5998 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005999 {
Janos Follathb03233e2019-06-11 15:30:30 +01006000 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6001 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6002 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006003 }
John Durkop07cc04a2020-11-16 22:08:34 -08006004 else
6005#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6006#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6007 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006008 {
6009 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6010 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6011 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006012 }
6013 else
John Durkop07cc04a2020-11-16 22:08:34 -08006014#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006015 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006016 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006017 return( PSA_ERROR_BAD_STATE );
6018 }
6019
Gilles Peskine224b0d62019-09-23 18:13:17 +02006020exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006021 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006022 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006023 return( status );
6024}
6025
Janos Follath51f4a0f2019-06-14 11:35:55 +01006026psa_status_t psa_key_derivation_input_bytes(
6027 psa_key_derivation_operation_t *operation,
6028 psa_key_derivation_step_t step,
6029 const uint8_t *data,
6030 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006031{
Gilles Peskineb8965192019-09-24 16:21:10 +02006032 return( psa_key_derivation_input_internal( operation, step,
6033 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006034 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006035}
6036
Janos Follath51f4a0f2019-06-14 11:35:55 +01006037psa_status_t psa_key_derivation_input_key(
6038 psa_key_derivation_operation_t *operation,
6039 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006040 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006041{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006042 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006043 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006044 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006045
Ronald Cron5c522922020-11-14 16:35:34 +01006046 status = psa_get_and_lock_transparent_key_slot_with_policy(
6047 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006048 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006049 {
6050 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006051 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006052 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006053
6054 /* Passing a key object as a SECRET input unlocks the permission
6055 * to output to a key object. */
6056 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6057 operation->can_output_key = 1;
6058
Ronald Cronf95a2b12020-10-22 15:24:49 +02006059 status = psa_key_derivation_input_internal( operation,
6060 step, slot->attr.type,
6061 slot->data.key.data,
6062 slot->data.key.bytes );
6063
Ronald Cron5c522922020-11-14 16:35:34 +01006064 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006065
Ronald Cron5c522922020-11-14 16:35:34 +01006066 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006067}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006068
6069
6070
6071/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006072/* Key agreement */
6073/****************************************************************/
6074
John Durkop6ba40d12020-11-10 08:50:04 -08006075#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006076static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6077 size_t peer_key_length,
6078 const mbedtls_ecp_keypair *our_key,
6079 uint8_t *shared_secret,
6080 size_t shared_secret_size,
6081 size_t *shared_secret_length )
6082{
Steven Cooremand4867872020-08-05 16:31:39 +02006083 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006084 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006085 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006086 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006087 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006088 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006089
Steven Cooreman4fed4552020-08-03 14:46:03 +02006090 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6091 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006092 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006093 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006094 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006095 goto exit;
6096
Jaeden Amero97271b32019-01-10 19:38:51 +00006097 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006098 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006099 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006100 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006101 status = mbedtls_to_psa_error(
6102 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6103 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006104 goto exit;
6105
Jaeden Amero97271b32019-01-10 19:38:51 +00006106 status = mbedtls_to_psa_error(
6107 mbedtls_ecdh_calc_secret( &ecdh,
6108 shared_secret_length,
6109 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006110 mbedtls_psa_get_random,
6111 mbedtls_psa_random_state( &global_data.rng ) ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006112 if( status != PSA_SUCCESS )
6113 goto exit;
6114 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6115 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006116
6117exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006118 if( status != PSA_SUCCESS )
6119 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006120 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006121 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006122 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006123
Jaeden Amero97271b32019-01-10 19:38:51 +00006124 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006125}
John Durkop6ba40d12020-11-10 08:50:04 -08006126#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006127
Gilles Peskine01d718c2018-09-18 12:01:02 +02006128#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6129
Gilles Peskine0216fe12019-04-11 21:23:21 +02006130static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6131 psa_key_slot_t *private_key,
6132 const uint8_t *peer_key,
6133 size_t peer_key_length,
6134 uint8_t *shared_secret,
6135 size_t shared_secret_size,
6136 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006137{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006138 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006139 {
John Durkop6ba40d12020-11-10 08:50:04 -08006140#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006141 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006142 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006143 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006144 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006145 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006146 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006147 private_key->data.key.data,
6148 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006149 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006150 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006151 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006152 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006153 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006154 shared_secret, shared_secret_size,
6155 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006156 mbedtls_ecp_keypair_free( ecp );
6157 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006158 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006159#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006160 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006161 (void) private_key;
6162 (void) peer_key;
6163 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006164 (void) shared_secret;
6165 (void) shared_secret_size;
6166 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006167 return( PSA_ERROR_NOT_SUPPORTED );
6168 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006169}
6170
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006171/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006172 * to potentially free embedded data structures and wipe confidential data.
6173 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006174static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006175 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006176 psa_key_slot_t *private_key,
6177 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006178 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006179{
6180 psa_status_t status;
6181 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6182 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006183 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006184
6185 /* Step 1: run the secret agreement algorithm to generate the shared
6186 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006187 status = psa_key_agreement_raw_internal( ka_alg,
6188 private_key,
6189 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006190 shared_secret,
6191 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006192 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006193 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006194 goto exit;
6195
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006196 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006197 * the shared secret. A shared secret is permitted wherever a key
6198 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006199 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006200 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006201 shared_secret,
6202 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006203exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006204 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006205 return( status );
6206}
6207
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006208psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006209 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006210 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006211 const uint8_t *peer_key,
6212 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006213{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006214 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006215 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006216 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006217
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006218 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006219 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006220 status = psa_get_and_lock_transparent_key_slot_with_policy(
6221 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006222 if( status != PSA_SUCCESS )
6223 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006224 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006225 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006226 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006227 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006228 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006229 else
6230 {
6231 /* If a private key has been added as SECRET, we allow the derived
6232 * key material to be used as a key in PSA Crypto. */
6233 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6234 operation->can_output_key = 1;
6235 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006236
Ronald Cron5c522922020-11-14 16:35:34 +01006237 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006238
Ronald Cron5c522922020-11-14 16:35:34 +01006239 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006240}
6241
Gilles Peskinebe697d82019-05-16 18:00:41 +02006242psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006243 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006244 const uint8_t *peer_key,
6245 size_t peer_key_length,
6246 uint8_t *output,
6247 size_t output_size,
6248 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006249{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006250 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006251 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006252 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006253
6254 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6255 {
6256 status = PSA_ERROR_INVALID_ARGUMENT;
6257 goto exit;
6258 }
Ronald Cron5c522922020-11-14 16:35:34 +01006259 status = psa_get_and_lock_transparent_key_slot_with_policy(
6260 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006261 if( status != PSA_SUCCESS )
6262 goto exit;
6263
6264 status = psa_key_agreement_raw_internal( alg, slot,
6265 peer_key, peer_key_length,
6266 output, output_size,
6267 output_length );
6268
6269exit:
6270 if( status != PSA_SUCCESS )
6271 {
6272 /* If an error happens and is not handled properly, the output
6273 * may be used as a key to protect sensitive data. Arrange for such
6274 * a key to be random, which is likely to result in decryption or
6275 * verification errors. This is better than filling the buffer with
6276 * some constant data such as zeros, which would result in the data
6277 * being protected with a reproducible, easily knowable key.
6278 */
6279 psa_generate_random( output, output_size );
6280 *output_length = output_size;
6281 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006282
Ronald Cron5c522922020-11-14 16:35:34 +01006283 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006284
Ronald Cron5c522922020-11-14 16:35:34 +01006285 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006286}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006287
6288
Gilles Peskine30524eb2020-11-13 17:02:26 +01006289
Gilles Peskine86a440b2018-11-12 18:39:40 +01006290/****************************************************************/
6291/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006292/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006293
Gilles Peskine30524eb2020-11-13 17:02:26 +01006294/** Initialize the PSA random generator.
6295 */
6296static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6297{
6298 /* Set default configuration if
6299 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6300 if( rng->entropy_init == NULL )
6301 rng->entropy_init = mbedtls_entropy_init;
6302 if( rng->entropy_free == NULL )
6303 rng->entropy_free = mbedtls_entropy_free;
6304
6305 rng->entropy_init( &rng->entropy );
6306#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6307 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6308 /* The PSA entropy injection feature depends on using NV seed as an entropy
6309 * source. Add NV seed as an entropy source for PSA entropy injection. */
6310 mbedtls_entropy_add_source( &rng->entropy,
6311 mbedtls_nv_seed_poll, NULL,
6312 MBEDTLS_ENTROPY_BLOCK_SIZE,
6313 MBEDTLS_ENTROPY_SOURCE_STRONG );
6314#endif
6315
6316 mbedtls_psa_drbg_init( mbedtls_psa_random_state( rng ) );
6317}
6318
6319/** Deinitialize the PSA random generator.
6320 */
6321static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6322{
6323 mbedtls_psa_drbg_free( mbedtls_psa_random_state( rng ) );
6324 rng->entropy_free( &rng->entropy );
6325}
6326
6327/** Seed the PSA random generator.
6328 */
6329static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6330{
6331 const unsigned char drbg_seed[] = "PSA";
6332 int ret = mbedtls_psa_drbg_seed( rng, drbg_seed, sizeof( drbg_seed ) - 1 );
6333 return mbedtls_to_psa_error( ret );
6334}
6335
Gilles Peskinee59236f2018-01-27 23:32:46 +01006336psa_status_t psa_generate_random( uint8_t *output,
6337 size_t output_size )
6338{
Janos Follath24eed8d2019-11-22 13:21:35 +00006339 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006340 GUARD_MODULE_INITIALIZED;
6341
Gilles Peskine30524eb2020-11-13 17:02:26 +01006342 while( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006343 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006344 ret = mbedtls_psa_get_random(
6345 mbedtls_psa_random_state( &global_data.rng ),
6346 output, MBEDTLS_PSA_RANDOM_MAX_REQUEST );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006347 if( ret != 0 )
6348 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006349 output += MBEDTLS_PSA_RANDOM_MAX_REQUEST;
6350 output_size -= MBEDTLS_PSA_RANDOM_MAX_REQUEST;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006351 }
6352
Gilles Peskine30524eb2020-11-13 17:02:26 +01006353 ret = mbedtls_psa_get_random( mbedtls_psa_random_state( &global_data.rng ),
6354 output, output_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006355 return( mbedtls_to_psa_error( ret ) );
6356}
6357
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006358#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6359#include "mbedtls/entropy_poll.h"
6360
Gilles Peskine7228da22019-07-15 11:06:15 +02006361psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006362 size_t seed_size )
6363{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006364 if( global_data.initialized )
6365 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006366
6367 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6368 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6369 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6370 return( PSA_ERROR_INVALID_ARGUMENT );
6371
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006372 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006373}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006374#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006375
John Durkop07cc04a2020-11-16 22:08:34 -08006376#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006377static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6378 size_t domain_parameters_size,
6379 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006380{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006381 size_t i;
6382 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006383
Gilles Peskinee56e8782019-04-26 17:34:02 +02006384 if( domain_parameters_size == 0 )
6385 {
6386 *exponent = 65537;
6387 return( PSA_SUCCESS );
6388 }
6389
6390 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6391 * support values that fit in a 32-bit integer, which is larger than
6392 * int on just about every platform anyway. */
6393 if( domain_parameters_size > sizeof( acc ) )
6394 return( PSA_ERROR_NOT_SUPPORTED );
6395 for( i = 0; i < domain_parameters_size; i++ )
6396 acc = ( acc << 8 ) | domain_parameters[i];
6397 if( acc > INT_MAX )
6398 return( PSA_ERROR_NOT_SUPPORTED );
6399 *exponent = acc;
6400 return( PSA_SUCCESS );
6401}
John Durkop07cc04a2020-11-16 22:08:34 -08006402#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006403
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006404static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006405 psa_key_slot_t *slot, size_t bits,
6406 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006407{
Gilles Peskine8e338702019-07-30 20:06:31 +02006408 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006409
Gilles Peskinee56e8782019-04-26 17:34:02 +02006410 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006411 return( PSA_ERROR_INVALID_ARGUMENT );
6412
Gilles Peskinee59236f2018-01-27 23:32:46 +01006413 if( key_type_is_raw_bytes( type ) )
6414 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006415 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006416
6417 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006418 if( status != PSA_SUCCESS )
6419 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006420
6421 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006422 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6423 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006424 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006425
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006426 status = psa_generate_random( slot->data.key.data,
6427 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006428 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006429 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006430
6431 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006432#if defined(MBEDTLS_DES_C)
6433 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006434 psa_des_set_key_parity( slot->data.key.data,
6435 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006436#endif /* MBEDTLS_DES_C */
6437 }
6438 else
6439
John Durkop07cc04a2020-11-16 22:08:34 -08006440#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006441 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006442 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006443 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006445 int exponent;
6446 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006447 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6448 return( PSA_ERROR_NOT_SUPPORTED );
6449 /* Accept only byte-aligned keys, for the same reasons as
6450 * in psa_import_rsa_key(). */
6451 if( bits % 8 != 0 )
6452 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006453 status = psa_read_rsa_exponent( domain_parameters,
6454 domain_parameters_size,
6455 &exponent );
6456 if( status != PSA_SUCCESS )
6457 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006458 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6459 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006460 mbedtls_psa_get_random,
6461 mbedtls_psa_random_state( &global_data.rng ),
Gilles Peskinee59236f2018-01-27 23:32:46 +01006462 (unsigned int) bits,
6463 exponent );
6464 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006465 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006466
6467 /* Make sure to always have an export representation available */
6468 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6469
Steven Cooreman75b74362020-07-28 14:30:13 +02006470 status = psa_allocate_buffer_to_slot( slot, bytes );
6471 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006472 {
6473 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006474 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006475 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006476
6477 status = psa_export_rsa_key( type,
6478 &rsa,
6479 slot->data.key.data,
6480 bytes,
6481 &slot->data.key.bytes );
6482 mbedtls_rsa_free( &rsa );
6483 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006484 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006485 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006486 }
6487 else
John Durkop07cc04a2020-11-16 22:08:34 -08006488#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006489
John Durkop9814fa22020-11-04 12:28:15 -08006490#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006491 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006492 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006493 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006494 mbedtls_ecp_group_id grp_id =
6495 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006496 const mbedtls_ecp_curve_info *curve_info =
6497 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006498 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006500 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006501 return( PSA_ERROR_NOT_SUPPORTED );
6502 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6503 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006504 mbedtls_ecp_keypair_init( &ecp );
6505 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006506 mbedtls_psa_get_random,
6507 mbedtls_psa_random_state( &global_data.rng ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006508 if( ret != 0 )
6509 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006510 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006511 return( mbedtls_to_psa_error( ret ) );
6512 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006513
6514
6515 /* Make sure to always have an export representation available */
6516 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006517 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6518 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006519 {
6520 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006521 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006522 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006523
6524 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006525 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6526
6527 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006528 if( status != PSA_SUCCESS ) {
6529 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006530 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006531 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006532 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006533 }
6534 else
John Durkop9814fa22020-11-04 12:28:15 -08006535#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006536 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006537 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006538 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006539
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006540 return( PSA_SUCCESS );
6541}
Darryl Green0c6575a2018-11-07 16:05:30 +00006542
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006543psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006544 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006545{
6546 psa_status_t status;
6547 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006548 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006549
Ronald Cron81709fc2020-11-14 12:10:32 +01006550 *key = MBEDTLS_SVC_KEY_ID_INIT;
6551
Gilles Peskine0f84d622019-09-12 19:03:13 +02006552 /* Reject any attempt to create a zero-length key so that we don't
6553 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6554 if( psa_get_key_bits( attributes ) == 0 )
6555 return( PSA_ERROR_INVALID_ARGUMENT );
6556
Ronald Cron81709fc2020-11-14 12:10:32 +01006557 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6558 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006559 if( status != PSA_SUCCESS )
6560 goto exit;
6561
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006562 status = psa_driver_wrapper_generate_key( attributes,
6563 slot );
6564 if( status != PSA_ERROR_NOT_SUPPORTED ||
6565 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6566 goto exit;
6567
6568 status = psa_generate_key_internal(
6569 slot, attributes->core.bits,
6570 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006571
6572exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006573 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006574 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006575 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006576 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006577
Darryl Green0c6575a2018-11-07 16:05:30 +00006578 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006579}
6580
6581
Gilles Peskinee59236f2018-01-27 23:32:46 +01006582
6583/****************************************************************/
6584/* Module setup */
6585/****************************************************************/
6586
Gilles Peskine5e769522018-11-20 21:59:56 +01006587psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6588 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6589 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6590{
6591 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6592 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006593 global_data.rng.entropy_init = entropy_init;
6594 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006595 return( PSA_SUCCESS );
6596}
6597
Gilles Peskinee59236f2018-01-27 23:32:46 +01006598void mbedtls_psa_crypto_free( void )
6599{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006600 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006601 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6602 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006603 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006604 }
6605 /* Wipe all remaining data, including configuration.
6606 * In particular, this sets all state indicator to the value
6607 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006608 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006609#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006610 /* Unregister all secure element drivers, so that we restart from
6611 * a pristine state. */
6612 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006613#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006614}
6615
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006616#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6617/** Recover a transaction that was interrupted by a power failure.
6618 *
6619 * This function is called during initialization, before psa_crypto_init()
6620 * returns. If this function returns a failure status, the initialization
6621 * fails.
6622 */
6623static psa_status_t psa_crypto_recover_transaction(
6624 const psa_crypto_transaction_t *transaction )
6625{
6626 switch( transaction->unknown.type )
6627 {
6628 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6629 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006630 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006631 * is implemented.
6632 * https://github.com/ARMmbed/mbed-crypto/issues/218
6633 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006634 default:
6635 /* We found an unsupported transaction in the storage.
6636 * We don't know what state the storage is in. Give up. */
6637 return( PSA_ERROR_STORAGE_FAILURE );
6638 }
6639}
6640#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6641
Gilles Peskinee59236f2018-01-27 23:32:46 +01006642psa_status_t psa_crypto_init( void )
6643{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006644 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006645
Gilles Peskinec6b69072018-11-20 21:42:52 +01006646 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006647 if( global_data.initialized != 0 )
6648 return( PSA_SUCCESS );
6649
Gilles Peskine30524eb2020-11-13 17:02:26 +01006650 /* Initialize and seed the random generator. */
6651 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006652 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006653 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006654 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006655 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006656 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006657
Gilles Peskine66fb1262018-12-10 16:29:04 +01006658 status = psa_initialize_key_slots( );
6659 if( status != PSA_SUCCESS )
6660 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006661
Gilles Peskined9348f22019-10-01 15:22:29 +02006662#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6663 status = psa_init_all_se_drivers( );
6664 if( status != PSA_SUCCESS )
6665 goto exit;
6666#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6667
Gilles Peskine4b734222019-07-24 15:56:31 +02006668#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006669 status = psa_crypto_load_transaction( );
6670 if( status == PSA_SUCCESS )
6671 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006672 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6673 if( status != PSA_SUCCESS )
6674 goto exit;
6675 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006676 }
6677 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6678 {
6679 /* There's no transaction to complete. It's all good. */
6680 status = PSA_SUCCESS;
6681 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006682#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006683
Gilles Peskinec6b69072018-11-20 21:42:52 +01006684 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006685 global_data.initialized = 1;
6686
6687exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006688 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006689 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006690 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006691}
6692
6693#endif /* MBEDTLS_PSA_CRYPTO_C */