blob: 5fcf0ac866e695c8b91d34c28b26f05be3adf3a4 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.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 Peskine2f9c4dc2018-01-28 13:16:24 +010043#include <stdlib.h>
44#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020046#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010047#define mbedtls_calloc calloc
48#define mbedtls_free free
49#endif
50
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020052#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000053#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020054#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/blowfish.h"
56#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020057#include "mbedtls/chacha20.h"
58#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/cipher.h"
60#include "mbedtls/ccm.h"
61#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020064#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010065#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010066#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/error.h"
68#include "mbedtls/gcm.h"
69#include "mbedtls/md2.h"
70#include "mbedtls/md4.h"
71#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
73#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010076#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/sha1.h"
80#include "mbedtls/sha256.h"
81#include "mbedtls/sha512.h"
82#include "mbedtls/xtea.h"
83
Gilles Peskine996deb12018-08-01 15:45:45 +020084#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
85
Gilles Peskine9ef733f2018-02-07 21:05:37 +010086/* constant-time buffer comparison */
87static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
88{
89 size_t i;
90 unsigned char diff = 0;
91
92 for( i = 0; i < n; i++ )
93 diff |= a[i] ^ b[i];
94
95 return( diff );
96}
97
98
99
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100100/****************************************************************/
101/* Global data, support functions and library management */
102/****************************************************************/
103
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104static int key_type_is_raw_bytes( psa_key_type_t type )
105{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200106 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200107}
108
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109/* Values for psa_global_data_t::rng_state */
110#define RNG_NOT_INITIALIZED 0
111#define RNG_INITIALIZED 1
112#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100113
Gilles Peskine2d277862018-06-18 15:41:12 +0200114typedef struct
115{
Gilles Peskine5e769522018-11-20 21:59:56 +0100116 void (* entropy_init )( mbedtls_entropy_context *ctx );
117 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118 mbedtls_entropy_context entropy;
119 mbedtls_ctr_drbg_context ctr_drbg;
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
Gilles Peskinee59236f2018-01-27 23:32:46 +0100130static psa_status_t mbedtls_to_psa_error( int ret )
131{
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)
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#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)
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#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:
204 return( PSA_ERROR_BAD_STATE );
205 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 );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
369 return( psa_key_lifetime_is_external( slot->lifetime ) );
370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200374static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
375{
376 switch( grpid )
377 {
378 case MBEDTLS_ECP_DP_SECP192R1:
379 return( PSA_ECC_CURVE_SECP192R1 );
380 case MBEDTLS_ECP_DP_SECP224R1:
381 return( PSA_ECC_CURVE_SECP224R1 );
382 case MBEDTLS_ECP_DP_SECP256R1:
383 return( PSA_ECC_CURVE_SECP256R1 );
384 case MBEDTLS_ECP_DP_SECP384R1:
385 return( PSA_ECC_CURVE_SECP384R1 );
386 case MBEDTLS_ECP_DP_SECP521R1:
387 return( PSA_ECC_CURVE_SECP521R1 );
388 case MBEDTLS_ECP_DP_BP256R1:
389 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
390 case MBEDTLS_ECP_DP_BP384R1:
391 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
392 case MBEDTLS_ECP_DP_BP512R1:
393 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
394 case MBEDTLS_ECP_DP_CURVE25519:
395 return( PSA_ECC_CURVE_CURVE25519 );
396 case MBEDTLS_ECP_DP_SECP192K1:
397 return( PSA_ECC_CURVE_SECP192K1 );
398 case MBEDTLS_ECP_DP_SECP224K1:
399 return( PSA_ECC_CURVE_SECP224K1 );
400 case MBEDTLS_ECP_DP_SECP256K1:
401 return( PSA_ECC_CURVE_SECP256K1 );
402 case MBEDTLS_ECP_DP_CURVE448:
403 return( PSA_ECC_CURVE_CURVE448 );
404 default:
405 return( 0 );
406 }
407}
408
Gilles Peskine12313cd2018-06-20 00:20:32 +0200409static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
410{
411 switch( curve )
412 {
413 case PSA_ECC_CURVE_SECP192R1:
414 return( MBEDTLS_ECP_DP_SECP192R1 );
415 case PSA_ECC_CURVE_SECP224R1:
416 return( MBEDTLS_ECP_DP_SECP224R1 );
417 case PSA_ECC_CURVE_SECP256R1:
418 return( MBEDTLS_ECP_DP_SECP256R1 );
419 case PSA_ECC_CURVE_SECP384R1:
420 return( MBEDTLS_ECP_DP_SECP384R1 );
421 case PSA_ECC_CURVE_SECP521R1:
422 return( MBEDTLS_ECP_DP_SECP521R1 );
423 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
424 return( MBEDTLS_ECP_DP_BP256R1 );
425 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
426 return( MBEDTLS_ECP_DP_BP384R1 );
427 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
428 return( MBEDTLS_ECP_DP_BP512R1 );
429 case PSA_ECC_CURVE_CURVE25519:
430 return( MBEDTLS_ECP_DP_CURVE25519 );
431 case PSA_ECC_CURVE_SECP192K1:
432 return( MBEDTLS_ECP_DP_SECP192K1 );
433 case PSA_ECC_CURVE_SECP224K1:
434 return( MBEDTLS_ECP_DP_SECP224K1 );
435 case PSA_ECC_CURVE_SECP256K1:
436 return( MBEDTLS_ECP_DP_SECP256K1 );
437 case PSA_ECC_CURVE_CURVE448:
438 return( MBEDTLS_ECP_DP_CURVE448 );
439 default:
440 return( MBEDTLS_ECP_DP_NONE );
441 }
442}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100443#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200444
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
446 size_t bits,
447 struct raw_data *raw )
448{
449 /* Check that the bit size is acceptable for the key type */
450 switch( type )
451 {
452 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200453 if( bits == 0 )
454 {
455 raw->bytes = 0;
456 raw->data = NULL;
457 return( PSA_SUCCESS );
458 }
459 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460#if defined(MBEDTLS_MD_C)
461 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200462#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200463 case PSA_KEY_TYPE_DERIVE:
464 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200465#if defined(MBEDTLS_AES_C)
466 case PSA_KEY_TYPE_AES:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_CAMELLIA_C)
472 case PSA_KEY_TYPE_CAMELLIA:
473 if( bits != 128 && bits != 192 && bits != 256 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_DES_C)
478 case PSA_KEY_TYPE_DES:
479 if( bits != 64 && bits != 128 && bits != 192 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
483#if defined(MBEDTLS_ARC4_C)
484 case PSA_KEY_TYPE_ARC4:
485 if( bits < 8 || bits > 2048 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200489#if defined(MBEDTLS_CHACHA20_C)
490 case PSA_KEY_TYPE_CHACHA20:
491 if( bits != 256 )
492 return( PSA_ERROR_INVALID_ARGUMENT );
493 break;
494#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 default:
496 return( PSA_ERROR_NOT_SUPPORTED );
497 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200498 if( bits % 8 != 0 )
499 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200500
501 /* Allocate memory for the key */
502 raw->bytes = PSA_BITS_TO_BYTES( bits );
503 raw->data = mbedtls_calloc( 1, raw->bytes );
504 if( raw->data == NULL )
505 {
506 raw->bytes = 0;
507 return( PSA_ERROR_INSUFFICIENT_MEMORY );
508 }
509 return( PSA_SUCCESS );
510}
511
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200512#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100513/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
514 * that are not a multiple of 8) well. For example, there is only
515 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
516 * way to return the exact bit size of a key.
517 * To keep things simple, reject non-byte-aligned key sizes. */
518static psa_status_t psa_check_rsa_key_byte_aligned(
519 const mbedtls_rsa_context *rsa )
520{
521 mbedtls_mpi n;
522 psa_status_t status;
523 mbedtls_mpi_init( &n );
524 status = mbedtls_to_psa_error(
525 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
526 if( status == PSA_SUCCESS )
527 {
528 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
529 status = PSA_ERROR_NOT_SUPPORTED;
530 }
531 mbedtls_mpi_free( &n );
532 return( status );
533}
534
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000535static psa_status_t psa_import_rsa_key( psa_key_type_t type,
536 const uint8_t *data,
537 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200538 mbedtls_rsa_context **p_rsa )
539{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000540 psa_status_t status;
541 mbedtls_pk_context pk;
542 mbedtls_rsa_context *rsa;
543 size_t bits;
544
545 mbedtls_pk_init( &pk );
546
547 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200548 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000549 status = mbedtls_to_psa_error(
550 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200551 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000552 status = mbedtls_to_psa_error(
553 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
554 if( status != PSA_SUCCESS )
555 goto exit;
556
557 /* We have something that the pkparse module recognizes. If it is a
558 * valid RSA key, store it. */
559 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000561 status = PSA_ERROR_INVALID_ARGUMENT;
562 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200563 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000564
565 rsa = mbedtls_pk_rsa( pk );
566 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
567 * supports non-byte-aligned key sizes, but not well. For example,
568 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
569 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
570 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
571 {
572 status = PSA_ERROR_NOT_SUPPORTED;
573 goto exit;
574 }
575 status = psa_check_rsa_key_byte_aligned( rsa );
576
577exit:
578 /* Free the content of the pk object only on error. */
579 if( status != PSA_SUCCESS )
580 {
581 mbedtls_pk_free( &pk );
582 return( status );
583 }
584
585 /* On success, store the content of the object in the RSA context. */
586 *p_rsa = rsa;
587
588 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200589}
590#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
591
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000592#if defined(MBEDTLS_ECP_C)
593
594/* Import a public key given as the uncompressed representation defined by SEC1
595 * 2.3.3 as the content of an ECPoint. */
596static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
597 const uint8_t *data,
598 size_t data_length,
599 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200600{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000602 mbedtls_ecp_keypair *ecp = NULL;
603 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
604
605 *p_ecp = NULL;
606 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
607 if( ecp == NULL )
608 return( PSA_ERROR_INSUFFICIENT_MEMORY );
609 mbedtls_ecp_keypair_init( ecp );
610
611 /* Load the group. */
612 status = mbedtls_to_psa_error(
613 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616 /* Load the public value. */
617 status = mbedtls_to_psa_error(
618 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
619 data, data_length ) );
620 if( status != PSA_SUCCESS )
621 goto exit;
622
623 /* Check that the point is on the curve. */
624 status = mbedtls_to_psa_error(
625 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
626 if( status != PSA_SUCCESS )
627 goto exit;
628
629 *p_ecp = ecp;
630 return( PSA_SUCCESS );
631
632exit:
633 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200634 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000635 mbedtls_ecp_keypair_free( ecp );
636 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200637 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000638 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200639}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000640#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200641
Gilles Peskinef76aa772018-10-29 19:24:33 +0100642#if defined(MBEDTLS_ECP_C)
643/* Import a private key given as a byte string which is the private value
644 * in big-endian order. */
645static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
646 const uint8_t *data,
647 size_t data_length,
648 mbedtls_ecp_keypair **p_ecp )
649{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651 mbedtls_ecp_keypair *ecp = NULL;
652 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
653
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200654 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
655 return( PSA_ERROR_INVALID_ARGUMENT );
656
Gilles Peskinef76aa772018-10-29 19:24:33 +0100657 *p_ecp = NULL;
658 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
659 if( ecp == NULL )
660 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000661 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100662
663 /* Load the group. */
664 status = mbedtls_to_psa_error(
665 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
666 if( status != PSA_SUCCESS )
667 goto exit;
668 /* Load the secret value. */
669 status = mbedtls_to_psa_error(
670 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
671 if( status != PSA_SUCCESS )
672 goto exit;
673 /* Validate the private key. */
674 status = mbedtls_to_psa_error(
675 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678 /* Calculate the public key from the private key. */
679 status = mbedtls_to_psa_error(
680 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
681 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
682 if( status != PSA_SUCCESS )
683 goto exit;
684
685 *p_ecp = ecp;
686 return( PSA_SUCCESS );
687
688exit:
689 if( ecp != NULL )
690 {
691 mbedtls_ecp_keypair_free( ecp );
692 mbedtls_free( ecp );
693 }
694 return( status );
695}
696#endif /* defined(MBEDTLS_ECP_C) */
697
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100698/** Import key data into a slot. `slot->type` must have been set
699 * previously. This function assumes that the slot does not contain
700 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100701psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
702 const uint8_t *data,
703 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200705 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706
Darryl Green940d72c2018-07-13 13:18:51 +0100707 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100709 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 if( data_length > SIZE_MAX / 8 )
711 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100712 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200713 PSA_BYTES_TO_BITS( data_length ),
714 &slot->data.raw );
715 if( status != PSA_SUCCESS )
716 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200717 if( data_length != 0 )
718 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100719 }
720 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100721#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200722 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100723 {
Darryl Green940d72c2018-07-13 13:18:51 +0100724 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100725 data, data_length,
726 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100727 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000728 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
729 {
730 status = psa_import_ec_public_key(
731 PSA_KEY_TYPE_GET_CURVE( slot->type ),
732 data, data_length,
733 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000734 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100735 else
736#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000737#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
738 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100739 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000740 status = psa_import_rsa_key( slot->type,
741 data, data_length,
742 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 }
744 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000745#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 {
747 return( PSA_ERROR_NOT_SUPPORTED );
748 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000749 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100750}
751
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100752/** Calculate the intersection of two algorithm usage policies.
753 *
754 * Return 0 (which allows no operation) on incompatibility.
755 */
756static psa_algorithm_t psa_key_policy_algorithm_intersection(
757 psa_algorithm_t alg1,
758 psa_algorithm_t alg2 )
759{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200760 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100761 if( alg1 == alg2 )
762 return( alg1 );
763 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100764 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100765 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
766 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
767 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
768 {
769 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
770 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100771 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100772 return( alg1 );
773 }
774 /* If the policies are incompatible, allow nothing. */
775 return( 0 );
776}
777
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200778static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
779 psa_algorithm_t requested_alg )
780{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200781 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200782 if( requested_alg == policy_alg )
783 return( 1 );
784 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200785 * and requested_alg is the same hash-and-sign family with any hash,
786 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200787 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
788 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
789 {
790 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
791 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
792 }
793 /* If it isn't permitted, it's forbidden. */
794 return( 0 );
795}
796
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100797/** Test whether a policy permits an algorithm.
798 *
799 * The caller must test usage flags separately.
800 */
801static int psa_key_policy_permits( const psa_key_policy_t *policy,
802 psa_algorithm_t alg )
803{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200804 return( psa_key_algorithm_permits( policy->alg, alg ) ||
805 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100806}
807
Gilles Peskinef603c712019-01-19 13:40:11 +0100808/** Restrict a key policy based on a constraint.
809 *
810 * \param[in,out] policy The policy to restrict.
811 * \param[in] constraint The policy constraint to apply.
812 *
813 * \retval #PSA_SUCCESS
814 * \c *policy contains the intersection of the original value of
815 * \c *policy and \c *constraint.
816 * \retval #PSA_ERROR_INVALID_ARGUMENT
817 * \c *policy and \c *constraint are incompatible.
818 * \c *policy is unchanged.
819 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100820static psa_status_t psa_restrict_key_policy(
821 psa_key_policy_t *policy,
822 const psa_key_policy_t *constraint )
823{
824 psa_algorithm_t intersection_alg =
825 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200826 psa_algorithm_t intersection_alg2 =
827 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100828 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
829 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200830 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
831 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100832 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100833 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200834 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100835 return( PSA_SUCCESS );
836}
837
Darryl Green06fd18d2018-07-16 11:21:11 +0100838/** Retrieve a slot which must contain a key. The key must have allow all the
839 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
840 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100841static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100842 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100843 psa_key_usage_t usage,
844 psa_algorithm_t alg )
845{
846 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100847 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100848
849 *p_slot = NULL;
850
Gilles Peskinec5487a82018-12-03 18:08:14 +0100851 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100852 if( status != PSA_SUCCESS )
853 return( status );
854 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200855 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100856
857 /* Enforce that usage policy for the key slot contains all the flags
858 * required by the usage parameter. There is one exception: public
859 * keys can always be exported, so we treat public key objects as
860 * if they had the export flag. */
861 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
862 usage &= ~PSA_KEY_USAGE_EXPORT;
863 if( ( slot->policy.usage & usage ) != usage )
864 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100865
866 /* Enforce that the usage policy permits the requested algortihm. */
867 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100868 return( PSA_ERROR_NOT_PERMITTED );
869
870 *p_slot = slot;
871 return( PSA_SUCCESS );
872}
Darryl Green940d72c2018-07-13 13:18:51 +0100873
Gilles Peskine28f8f302019-07-24 13:30:31 +0200874/** Retrieve a slot which must contain a transparent key.
875 *
876 * A transparent key is a key for which the key material is directly
877 * available, as opposed to a key in a secure element.
878 *
879 * This is a temporary function until secure element support is
880 * fully implemented.
881 */
882#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
883static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
884 psa_key_slot_t **p_slot,
885 psa_key_usage_t usage,
886 psa_algorithm_t alg )
887{
888 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
889 if( status != PSA_SUCCESS )
890 return( status );
891 /* Use a simple, cheap test to check whether the key is transparent.
892 * This check assumes that there are no persistent lifetimes other than
893 * PSA_KEY_LIFETIME_PERSISTENT. */
894 if( ( *p_slot )->lifetime > PSA_KEY_LIFETIME_PERSISTENT )
895 {
896 *p_slot = NULL;
897 return( PSA_ERROR_NOT_SUPPORTED );
898 }
899 return( PSA_SUCCESS );
900}
901#else /* MBEDTLS_PSA_CRYPTO_SE_C */
902/* With no secure element support, all keys are transparent. */
903#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
904 psa_get_key_from_slot( handle, p_slot, usage, alg )
905#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
906
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100907/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100908static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000909{
Gilles Peskine73167e12019-07-12 23:44:37 +0200910#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
911 if( psa_key_slot_is_external( slot ) )
912 {
913 /* No key material to clean. */
914 }
915 else
916#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +0000917 if( slot->type == PSA_KEY_TYPE_NONE )
918 {
919 /* No key material to clean. */
920 }
921 else if( key_type_is_raw_bytes( slot->type ) )
922 {
923 mbedtls_free( slot->data.raw.data );
924 }
925 else
926#if defined(MBEDTLS_RSA_C)
927 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
928 {
929 mbedtls_rsa_free( slot->data.rsa );
930 mbedtls_free( slot->data.rsa );
931 }
932 else
933#endif /* defined(MBEDTLS_RSA_C) */
934#if defined(MBEDTLS_ECP_C)
935 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
936 {
937 mbedtls_ecp_keypair_free( slot->data.ecp );
938 mbedtls_free( slot->data.ecp );
939 }
940 else
941#endif /* defined(MBEDTLS_ECP_C) */
942 {
943 /* Shouldn't happen: the key type is not any type that we
944 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200945 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000946 }
947
948 return( PSA_SUCCESS );
949}
950
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100951static void psa_abort_operations_using_key( psa_key_slot_t *slot )
952{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200953 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100954 (void) slot;
955}
956
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100957/** Completely wipe a slot in memory, including its policy.
958 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100959psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100960{
961 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100962 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100963 /* At this point, key material and other type-specific content has
964 * been wiped. Clear remaining metadata. We can call memset and not
965 * zeroize because the metadata is not particularly sensitive. */
966 memset( slot, 0, sizeof( *slot ) );
967 return( status );
968}
969
Gilles Peskinec5487a82018-12-03 18:08:14 +0100970psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100971{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100972 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100973 psa_status_t status = PSA_SUCCESS;
974 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200975#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
976 psa_se_drv_table_entry_t *driver;
977#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100978
Gilles Peskinec5487a82018-12-03 18:08:14 +0100979 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200980 if( status != PSA_SUCCESS )
981 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +0200982
983#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
984 driver = psa_get_se_driver_entry( slot->lifetime );
985 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +0200986 {
987 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
988 psa_crypto_transaction.key.lifetime = slot->lifetime;
989 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
990 psa_crypto_transaction.key.id = slot->persistent_storage_id;
991 status = psa_crypto_save_transaction( );
992 if( status != PSA_SUCCESS )
993 {
994 /* TOnogrepDO: destroy what can be destroyed anyway */
995 return( status );
996 }
997
Gilles Peskine354f7672019-07-12 23:46:38 +0200998 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +0200999 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001000#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1001
Darryl Greend49a4992018-06-18 17:27:26 +01001002#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1003 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1004 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001005 storage_status =
1006 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +01001007 }
1008#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001009
Gilles Peskinefc762652019-07-22 19:30:34 +02001010#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1011 if( driver != NULL )
1012 {
1013 status = psa_crypto_stop_transaction( );
1014 if( status != PSA_SUCCESS )
1015 {
1016 /* TOnogrepDO: destroy what can be destroyed anyway */
1017 return( status );
1018 }
1019 }
1020#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1021
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001022 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001023 if( status != PSA_SUCCESS )
1024 return( status );
1025 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001026}
1027
Gilles Peskineb870b182018-07-06 16:02:09 +02001028/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001029static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +02001030{
1031 if( key_type_is_raw_bytes( slot->type ) )
1032 return( slot->data.raw.bytes * 8 );
1033#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001034 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001035 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001036#endif /* defined(MBEDTLS_RSA_C) */
1037#if defined(MBEDTLS_ECP_C)
1038 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1039 return( slot->data.ecp->grp.pbits );
1040#endif /* defined(MBEDTLS_ECP_C) */
1041 /* Shouldn't happen except on an empty slot. */
1042 return( 0 );
1043}
1044
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001045void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1046{
Gilles Peskineb699f072019-04-26 16:06:02 +02001047 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001048 memset( attributes, 0, sizeof( *attributes ) );
1049}
1050
Gilles Peskineb699f072019-04-26 16:06:02 +02001051psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1052 psa_key_type_t type,
1053 const uint8_t *data,
1054 size_t data_length )
1055{
1056 uint8_t *copy = NULL;
1057
1058 if( data_length != 0 )
1059 {
1060 copy = mbedtls_calloc( 1, data_length );
1061 if( copy == NULL )
1062 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1063 memcpy( copy, data, data_length );
1064 }
1065 /* After this point, this function is guaranteed to succeed, so it
1066 * can start modifying `*attributes`. */
1067
1068 if( attributes->domain_parameters != NULL )
1069 {
1070 mbedtls_free( attributes->domain_parameters );
1071 attributes->domain_parameters = NULL;
1072 attributes->domain_parameters_size = 0;
1073 }
1074
1075 attributes->domain_parameters = copy;
1076 attributes->domain_parameters_size = data_length;
1077 attributes->type = type;
1078 return( PSA_SUCCESS );
1079}
1080
1081psa_status_t psa_get_key_domain_parameters(
1082 const psa_key_attributes_t *attributes,
1083 uint8_t *data, size_t data_size, size_t *data_length )
1084{
1085 if( attributes->domain_parameters_size > data_size )
1086 return( PSA_ERROR_BUFFER_TOO_SMALL );
1087 *data_length = attributes->domain_parameters_size;
1088 if( attributes->domain_parameters_size != 0 )
1089 memcpy( data, attributes->domain_parameters,
1090 attributes->domain_parameters_size );
1091 return( PSA_SUCCESS );
1092}
1093
1094#if defined(MBEDTLS_RSA_C)
1095static psa_status_t psa_get_rsa_public_exponent(
1096 const mbedtls_rsa_context *rsa,
1097 psa_key_attributes_t *attributes )
1098{
1099 mbedtls_mpi mpi;
1100 int ret;
1101 uint8_t *buffer = NULL;
1102 size_t buflen;
1103 mbedtls_mpi_init( &mpi );
1104
1105 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1106 if( ret != 0 )
1107 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001108 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1109 {
1110 /* It's the default value, which is reported as an empty string,
1111 * so there's nothing to do. */
1112 goto exit;
1113 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001114
1115 buflen = mbedtls_mpi_size( &mpi );
1116 buffer = mbedtls_calloc( 1, buflen );
1117 if( buffer == NULL )
1118 {
1119 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1120 goto exit;
1121 }
1122 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1123 if( ret != 0 )
1124 goto exit;
1125 attributes->domain_parameters = buffer;
1126 attributes->domain_parameters_size = buflen;
1127
1128exit:
1129 mbedtls_mpi_free( &mpi );
1130 if( ret != 0 )
1131 mbedtls_free( buffer );
1132 return( mbedtls_to_psa_error( ret ) );
1133}
1134#endif /* MBEDTLS_RSA_C */
1135
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001136/** Retrieve the readily-accessible attributes of a key in a slot.
1137 *
1138 * This function does not compute attributes that are not directly
1139 * stored in the slot, such as the bit size of a transparent key.
1140 */
1141static void psa_get_key_slot_attributes( psa_key_slot_t *slot,
1142 psa_key_attributes_t *attributes )
1143{
1144 attributes->id = slot->persistent_storage_id;
1145 attributes->lifetime = slot->lifetime;
1146 attributes->policy = slot->policy;
1147 attributes->type = slot->type;
1148}
1149
1150/** Retrieve all the publicly-accessible attributes of a key.
1151 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001152psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1153 psa_key_attributes_t *attributes )
1154{
1155 psa_key_slot_t *slot;
1156 psa_status_t status;
1157
1158 psa_reset_key_attributes( attributes );
1159
Gilles Peskine28f8f302019-07-24 13:30:31 +02001160 status = psa_get_transparent_key( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001161 if( status != PSA_SUCCESS )
1162 return( status );
1163
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001164 psa_get_key_slot_attributes( slot, attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001165 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001166
1167 switch( slot->type )
1168 {
1169#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001170 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001171 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1172 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1173 break;
1174#endif
1175 default:
1176 /* Nothing else to do. */
1177 break;
1178 }
1179
1180 if( status != PSA_SUCCESS )
1181 psa_reset_key_attributes( attributes );
1182 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001183}
1184
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001185#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001186static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1187 unsigned char *buf, size_t size )
1188{
1189 int ret;
1190 unsigned char *c;
1191 size_t len = 0;
1192
1193 c = buf + size;
1194
1195 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1196
1197 return( (int) len );
1198}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001199#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001200
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001201static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1202 uint8_t *data,
1203 size_t data_size,
1204 size_t *data_length,
1205 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001206{
Gilles Peskine5d309672019-07-12 23:47:28 +02001207#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1208 const psa_drv_se_t *drv;
1209 psa_drv_se_context_t *drv_context;
1210#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1211
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001212 *data_length = 0;
1213
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001214 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001215 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001216
Gilles Peskine5d309672019-07-12 23:47:28 +02001217#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1218 if( psa_get_se_driver( slot->lifetime, &drv, &drv_context ) )
1219 {
1220 psa_drv_se_export_key_t method;
1221 if( drv->key_management == NULL )
1222 return( PSA_ERROR_NOT_SUPPORTED );
1223 method = ( export_public_key ?
1224 drv->key_management->p_export_public :
1225 drv->key_management->p_export );
1226 if( method == NULL )
1227 return( PSA_ERROR_NOT_SUPPORTED );
1228 return( ( *method )( drv_context,
1229 slot->data.se.slot_number,
1230 data, data_size, data_length ) );
1231 }
1232#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1233
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001234 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001235 {
1236 if( slot->data.raw.bytes > data_size )
1237 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001238 if( data_size != 0 )
1239 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001240 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001241 memset( data + slot->data.raw.bytes, 0,
1242 data_size - slot->data.raw.bytes );
1243 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001244 *data_length = slot->data.raw.bytes;
1245 return( PSA_SUCCESS );
1246 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001247#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001248 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001249 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001250 psa_status_t status;
1251
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001252 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001253 if( bytes > data_size )
1254 return( PSA_ERROR_BUFFER_TOO_SMALL );
1255 status = mbedtls_to_psa_error(
1256 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1257 if( status != PSA_SUCCESS )
1258 return( status );
1259 memset( data + bytes, 0, data_size - bytes );
1260 *data_length = bytes;
1261 return( PSA_SUCCESS );
1262 }
1263#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001264 else
Moran Peker17e36e12018-05-02 12:55:20 +03001265 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001266#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001267 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001268 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001269 {
Moran Pekera998bc62018-04-16 18:16:20 +03001270 mbedtls_pk_context pk;
1271 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001272 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001273 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001274#if defined(MBEDTLS_RSA_C)
1275 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001276 pk.pk_info = &mbedtls_rsa_info;
1277 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001278#else
1279 return( PSA_ERROR_NOT_SUPPORTED );
1280#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001281 }
1282 else
1283 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001284#if defined(MBEDTLS_ECP_C)
1285 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001286 pk.pk_info = &mbedtls_eckey_info;
1287 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001288#else
1289 return( PSA_ERROR_NOT_SUPPORTED );
1290#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001291 }
Moran Pekerd7326592018-05-29 16:56:39 +03001292 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001293 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001294 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001295 }
Moran Peker17e36e12018-05-02 12:55:20 +03001296 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001297 {
Moran Peker17e36e12018-05-02 12:55:20 +03001298 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001299 }
Moran Peker60364322018-04-29 11:34:58 +03001300 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001301 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001302 /* If data_size is 0 then data may be NULL and then the
1303 * call to memset would have undefined behavior. */
1304 if( data_size != 0 )
1305 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001306 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001307 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001308 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1309 * Move the data to the beginning and erase remaining data
1310 * at the original location. */
1311 if( 2 * (size_t) ret <= data_size )
1312 {
1313 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001314 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001315 }
1316 else if( (size_t) ret < data_size )
1317 {
1318 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001319 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001320 }
Moran Pekera998bc62018-04-16 18:16:20 +03001321 *data_length = ret;
1322 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001323 }
1324 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001325#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001326 {
1327 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001328 it is valid for a special-purpose implementation to omit
1329 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001330 return( PSA_ERROR_NOT_SUPPORTED );
1331 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001332 }
1333}
1334
Gilles Peskinec5487a82018-12-03 18:08:14 +01001335psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001336 uint8_t *data,
1337 size_t data_size,
1338 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001339{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001340 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001341 psa_status_t status;
1342
1343 /* Set the key to empty now, so that even when there are errors, we always
1344 * set data_length to a value between 0 and data_size. On error, setting
1345 * the key to empty is a good choice because an empty key representation is
1346 * unlikely to be accepted anywhere. */
1347 *data_length = 0;
1348
1349 /* Export requires the EXPORT flag. There is an exception for public keys,
1350 * which don't require any flag, but psa_get_key_from_slot takes
1351 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001352 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001353 if( status != PSA_SUCCESS )
1354 return( status );
1355 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001356 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001357}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001358
Gilles Peskinec5487a82018-12-03 18:08:14 +01001359psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001360 uint8_t *data,
1361 size_t data_size,
1362 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001363{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001364 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001365 psa_status_t status;
1366
1367 /* Set the key to empty now, so that even when there are errors, we always
1368 * set data_length to a value between 0 and data_size. On error, setting
1369 * the key to empty is a good choice because an empty key representation is
1370 * unlikely to be accepted anywhere. */
1371 *data_length = 0;
1372
1373 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001374 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001375 if( status != PSA_SUCCESS )
1376 return( status );
1377 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001378 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001379}
1380
Gilles Peskine4747d192019-04-17 15:05:45 +02001381static psa_status_t psa_set_key_policy_internal(
1382 psa_key_slot_t *slot,
1383 const psa_key_policy_t *policy )
1384{
1385 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001386 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001387 PSA_KEY_USAGE_ENCRYPT |
1388 PSA_KEY_USAGE_DECRYPT |
1389 PSA_KEY_USAGE_SIGN |
1390 PSA_KEY_USAGE_VERIFY |
1391 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1392 return( PSA_ERROR_INVALID_ARGUMENT );
1393
1394 slot->policy = *policy;
1395 return( PSA_SUCCESS );
1396}
1397
1398/** Prepare a key slot to receive key material.
1399 *
1400 * This function allocates a key slot and sets its metadata.
1401 *
1402 * If this function fails, call psa_fail_key_creation().
1403 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001404 * This function is intended to be used as follows:
1405 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1406 * it with the specified attributes, and assign it a handle.
1407 * -# Populate the slot with the key material.
1408 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1409 * In case of failure at any step, stop the sequence and call
1410 * psa_fail_key_creation().
1411 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001412 * \param[in] attributes Key attributes for the new key.
1413 * \param[out] handle On success, a handle for the allocated slot.
1414 * \param[out] p_slot On success, a pointer to the prepared slot.
1415 * \param[out] p_drv On any return, the driver for the key, if any.
1416 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001417 *
1418 * \retval #PSA_SUCCESS
1419 * The key slot is ready to receive key material.
1420 * \return If this function fails, the key slot is an invalid state.
1421 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001422 */
1423static psa_status_t psa_start_key_creation(
1424 const psa_key_attributes_t *attributes,
1425 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001426 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001427 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001428{
1429 psa_status_t status;
1430 psa_key_slot_t *slot;
1431
Gilles Peskine011e4282019-06-26 18:34:38 +02001432 *p_drv = NULL;
1433
Gilles Peskine267c6562019-05-27 19:01:54 +02001434 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001435 if( status != PSA_SUCCESS )
1436 return( status );
1437 slot = *p_slot;
1438
1439 status = psa_set_key_policy_internal( slot, &attributes->policy );
1440 if( status != PSA_SUCCESS )
1441 return( status );
1442 slot->lifetime = attributes->lifetime;
Gilles Peskine011e4282019-06-26 18:34:38 +02001443
Gilles Peskine4747d192019-04-17 15:05:45 +02001444 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001445 {
1446 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskine011e4282019-06-26 18:34:38 +02001447 attributes->id,
1448 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001449 if( status != PSA_SUCCESS )
1450 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001451 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001452 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001453 slot->type = attributes->type;
1454
Gilles Peskinecbaff462019-07-12 23:46:04 +02001455#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskinefc762652019-07-22 19:30:34 +02001456 /* Find a slot number for the new key. Save the slot number in
1457 * persistent storage, but do not yet save the driver's persistent
1458 * state, so that if the power fails during the key creation process,
1459 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001460 if( *p_drv != NULL )
1461 {
1462 status = psa_find_se_slot_for_key( attributes, *p_drv,
1463 &slot->data.se.slot_number );
1464 if( status != PSA_SUCCESS )
1465 return( status );
1466 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001467 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1468 psa_crypto_transaction.key.lifetime = slot->lifetime;
1469 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1470 psa_crypto_transaction.key.id = slot->persistent_storage_id;
1471 status = psa_crypto_save_transaction( );
1472 if( status != PSA_SUCCESS )
1473 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001474#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1475
Gilles Peskine4747d192019-04-17 15:05:45 +02001476 return( status );
1477}
1478
1479/** Finalize the creation of a key once its key material has been set.
1480 *
1481 * This entails writing the key to persistent storage.
1482 *
1483 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001484 * See the documentation of psa_start_key_creation() for the intended use
1485 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001486 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001487 * \param[in,out] slot Pointer to the slot with key material.
1488 * \param[in] driver The secure element driver for the key,
1489 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001490 *
1491 * \retval #PSA_SUCCESS
1492 * The key was successfully created. The handle is now valid.
1493 * \return If this function fails, the key slot is an invalid state.
1494 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001495 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001496static psa_status_t psa_finish_key_creation(
1497 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001498 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001499{
1500 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001501 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001502 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001503
1504#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1df83d42019-07-23 16:13:14 +02001505 if( slot->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001506 {
1507 uint8_t *buffer = NULL;
1508 size_t buffer_size = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001509 size_t length = 0;
Gilles Peskine4747d192019-04-17 15:05:45 +02001510
Gilles Peskine1df83d42019-07-23 16:13:14 +02001511#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1512 if( driver != NULL )
1513 {
1514 buffer = (uint8_t*) &slot->data.se.slot_number;
1515 length = sizeof( slot->data.se.slot_number );
1516 }
1517 else
1518#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1519 {
1520 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
1521 psa_get_key_slot_bits( slot ) );
1522 buffer = mbedtls_calloc( 1, buffer_size );
1523 if( buffer == NULL && buffer_size != 0 )
1524 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1525 status = psa_internal_export_key( slot,
1526 buffer, buffer_size, &length,
1527 0 );
1528 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001529
1530 if( status == PSA_SUCCESS )
1531 {
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001532 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1533 psa_get_key_slot_attributes( slot, &attributes );
1534 status = psa_save_persistent_key( &attributes, buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001535 }
1536
Gilles Peskine1df83d42019-07-23 16:13:14 +02001537#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1538 if( driver == NULL )
1539#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1540 {
1541 if( buffer_size != 0 )
1542 mbedtls_platform_zeroize( buffer, buffer_size );
1543 mbedtls_free( buffer );
1544 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001545 }
1546#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1547
Gilles Peskinecbaff462019-07-12 23:46:04 +02001548#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1549 if( driver != NULL )
1550 {
1551 status = psa_save_se_persistent_data( driver );
1552 if( status != PSA_SUCCESS )
1553 {
1554 psa_destroy_persistent_key( slot->persistent_storage_id );
1555 return( status );
1556 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001557 status = psa_crypto_stop_transaction( );
1558 if( status != PSA_SUCCESS )
1559 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001560 }
1561#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1562
Gilles Peskine4747d192019-04-17 15:05:45 +02001563 return( status );
1564}
1565
1566/** Abort the creation of a key.
1567 *
1568 * You may call this function after calling psa_start_key_creation(),
1569 * or after psa_finish_key_creation() fails. In other circumstances, this
1570 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001571 * See the documentation of psa_start_key_creation() for the intended use
1572 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001573 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001574 * \param[in,out] slot Pointer to the slot with key material.
1575 * \param[in] driver The secure element driver for the key,
1576 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001577 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001578static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001579 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001580{
Gilles Peskine011e4282019-06-26 18:34:38 +02001581 (void) driver;
1582
Gilles Peskine4747d192019-04-17 15:05:45 +02001583 if( slot == NULL )
1584 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001585
1586#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1587 /* TOnogrepDO: If the key has already been created in the secure
1588 * element, and the failure happened later (when saving metadata
1589 * to internal storage), we need to destroy the key in the secure
1590 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001591
1592 /* Abort the ongoing transaction if any. We already did what it
1593 * takes to undo any partial creation. All that's left is to update
1594 * the transaction data itself. */
1595 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001596#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1597
Gilles Peskine4747d192019-04-17 15:05:45 +02001598 psa_wipe_key_slot( slot );
1599}
1600
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001601static psa_status_t psa_check_key_slot_attributes(
1602 const psa_key_slot_t *slot,
1603 const psa_key_attributes_t *attributes )
1604{
1605 if( attributes->type != 0 )
1606 {
1607 if( attributes->type != slot->type )
1608 return( PSA_ERROR_INVALID_ARGUMENT );
1609 }
1610
1611 if( attributes->domain_parameters_size != 0 )
1612 {
1613#if defined(MBEDTLS_RSA_C)
1614 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1615 {
1616 mbedtls_mpi actual, required;
1617 int ret;
1618 mbedtls_mpi_init( &actual );
1619 mbedtls_mpi_init( &required );
1620 ret = mbedtls_rsa_export( slot->data.rsa,
1621 NULL, NULL, NULL, NULL, &actual );
1622 if( ret != 0 )
1623 goto rsa_exit;
1624 ret = mbedtls_mpi_read_binary( &required,
1625 attributes->domain_parameters,
1626 attributes->domain_parameters_size );
1627 if( ret != 0 )
1628 goto rsa_exit;
1629 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1630 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1631 rsa_exit:
1632 mbedtls_mpi_free( &actual );
1633 mbedtls_mpi_free( &required );
1634 if( ret != 0)
1635 return( mbedtls_to_psa_error( ret ) );
1636 }
1637 else
1638#endif
1639 {
1640 return( PSA_ERROR_INVALID_ARGUMENT );
1641 }
1642 }
1643
1644 if( attributes->bits != 0 )
1645 {
1646 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1647 return( PSA_ERROR_INVALID_ARGUMENT );
1648 }
1649
1650 return( PSA_SUCCESS );
1651}
1652
Gilles Peskine4747d192019-04-17 15:05:45 +02001653psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001654 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001655 size_t data_length,
1656 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001657{
1658 psa_status_t status;
1659 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001660 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001661
Gilles Peskine011e4282019-06-26 18:34:38 +02001662 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001663 if( status != PSA_SUCCESS )
1664 goto exit;
1665
Gilles Peskine5d309672019-07-12 23:47:28 +02001666#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1667 if( driver != NULL )
1668 {
1669 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
1670 if( drv->key_management == NULL ||
1671 drv->key_management->p_import == NULL )
1672 {
1673 status = PSA_ERROR_NOT_SUPPORTED;
1674 goto exit;
1675 }
1676 status = drv->key_management->p_import(
1677 psa_get_se_driver_context( driver ),
1678 slot->data.se.slot_number,
1679 slot->lifetime, slot->type, slot->policy.alg, slot->policy.usage,
1680 data, data_length );
1681 /* TOnogrepDO: psa_check_key_slot_attributes? */
1682 }
1683 else
1684#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1685 {
1686 status = psa_import_key_into_slot( slot, data, data_length );
1687 if( status != PSA_SUCCESS )
1688 goto exit;
1689 status = psa_check_key_slot_attributes( slot, attributes );
1690 if( status != PSA_SUCCESS )
1691 goto exit;
1692 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001693
Gilles Peskine011e4282019-06-26 18:34:38 +02001694 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001695exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001696 if( status != PSA_SUCCESS )
1697 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001698 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001699 *handle = 0;
1700 }
1701 return( status );
1702}
1703
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001704static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001705 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001706{
1707 psa_status_t status;
1708 uint8_t *buffer = NULL;
1709 size_t buffer_size = 0;
1710 size_t length;
1711
1712 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001713 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001714 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001715 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001716 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001717 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1718 if( status != PSA_SUCCESS )
1719 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001720 target->type = source->type;
1721 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001722
1723exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001724 if( buffer_size != 0 )
1725 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001726 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001727 return( status );
1728}
1729
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001730psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1731 const psa_key_attributes_t *specified_attributes,
1732 psa_key_handle_t *target_handle )
1733{
1734 psa_status_t status;
1735 psa_key_slot_t *source_slot = NULL;
1736 psa_key_slot_t *target_slot = NULL;
1737 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001738 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001739
Gilles Peskine28f8f302019-07-24 13:30:31 +02001740 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinec160d9e2019-05-14 14:32:03 +02001741 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001742 if( status != PSA_SUCCESS )
1743 goto exit;
1744
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001745 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1746 if( status != PSA_SUCCESS )
1747 goto exit;
1748
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001749 status = psa_restrict_key_policy( &actual_attributes.policy,
1750 &source_slot->policy );
1751 if( status != PSA_SUCCESS )
1752 goto exit;
1753
1754 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001755 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001756 if( status != PSA_SUCCESS )
1757 goto exit;
1758
1759 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001760 if( status != PSA_SUCCESS )
1761 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001762
Gilles Peskine011e4282019-06-26 18:34:38 +02001763 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001764exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001765 if( status != PSA_SUCCESS )
1766 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001767 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001768 *target_handle = 0;
1769 }
1770 return( status );
1771}
1772
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001773
1774
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001775/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001776/* Message digests */
1777/****************************************************************/
1778
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001779static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001780{
1781 switch( alg )
1782 {
1783#if defined(MBEDTLS_MD2_C)
1784 case PSA_ALG_MD2:
1785 return( &mbedtls_md2_info );
1786#endif
1787#if defined(MBEDTLS_MD4_C)
1788 case PSA_ALG_MD4:
1789 return( &mbedtls_md4_info );
1790#endif
1791#if defined(MBEDTLS_MD5_C)
1792 case PSA_ALG_MD5:
1793 return( &mbedtls_md5_info );
1794#endif
1795#if defined(MBEDTLS_RIPEMD160_C)
1796 case PSA_ALG_RIPEMD160:
1797 return( &mbedtls_ripemd160_info );
1798#endif
1799#if defined(MBEDTLS_SHA1_C)
1800 case PSA_ALG_SHA_1:
1801 return( &mbedtls_sha1_info );
1802#endif
1803#if defined(MBEDTLS_SHA256_C)
1804 case PSA_ALG_SHA_224:
1805 return( &mbedtls_sha224_info );
1806 case PSA_ALG_SHA_256:
1807 return( &mbedtls_sha256_info );
1808#endif
1809#if defined(MBEDTLS_SHA512_C)
1810 case PSA_ALG_SHA_384:
1811 return( &mbedtls_sha384_info );
1812 case PSA_ALG_SHA_512:
1813 return( &mbedtls_sha512_info );
1814#endif
1815 default:
1816 return( NULL );
1817 }
1818}
1819
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001820psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1821{
1822 switch( operation->alg )
1823 {
Gilles Peskine81736312018-06-26 15:04:31 +02001824 case 0:
1825 /* The object has (apparently) been initialized but it is not
1826 * in use. It's ok to call abort on such an object, and there's
1827 * nothing to do. */
1828 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001829#if defined(MBEDTLS_MD2_C)
1830 case PSA_ALG_MD2:
1831 mbedtls_md2_free( &operation->ctx.md2 );
1832 break;
1833#endif
1834#if defined(MBEDTLS_MD4_C)
1835 case PSA_ALG_MD4:
1836 mbedtls_md4_free( &operation->ctx.md4 );
1837 break;
1838#endif
1839#if defined(MBEDTLS_MD5_C)
1840 case PSA_ALG_MD5:
1841 mbedtls_md5_free( &operation->ctx.md5 );
1842 break;
1843#endif
1844#if defined(MBEDTLS_RIPEMD160_C)
1845 case PSA_ALG_RIPEMD160:
1846 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1847 break;
1848#endif
1849#if defined(MBEDTLS_SHA1_C)
1850 case PSA_ALG_SHA_1:
1851 mbedtls_sha1_free( &operation->ctx.sha1 );
1852 break;
1853#endif
1854#if defined(MBEDTLS_SHA256_C)
1855 case PSA_ALG_SHA_224:
1856 case PSA_ALG_SHA_256:
1857 mbedtls_sha256_free( &operation->ctx.sha256 );
1858 break;
1859#endif
1860#if defined(MBEDTLS_SHA512_C)
1861 case PSA_ALG_SHA_384:
1862 case PSA_ALG_SHA_512:
1863 mbedtls_sha512_free( &operation->ctx.sha512 );
1864 break;
1865#endif
1866 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001867 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001868 }
1869 operation->alg = 0;
1870 return( PSA_SUCCESS );
1871}
1872
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001873psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001874 psa_algorithm_t alg )
1875{
1876 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001877
1878 /* A context must be freshly initialized before it can be set up. */
1879 if( operation->alg != 0 )
1880 {
1881 return( PSA_ERROR_BAD_STATE );
1882 }
1883
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001884 switch( alg )
1885 {
1886#if defined(MBEDTLS_MD2_C)
1887 case PSA_ALG_MD2:
1888 mbedtls_md2_init( &operation->ctx.md2 );
1889 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1890 break;
1891#endif
1892#if defined(MBEDTLS_MD4_C)
1893 case PSA_ALG_MD4:
1894 mbedtls_md4_init( &operation->ctx.md4 );
1895 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1896 break;
1897#endif
1898#if defined(MBEDTLS_MD5_C)
1899 case PSA_ALG_MD5:
1900 mbedtls_md5_init( &operation->ctx.md5 );
1901 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1902 break;
1903#endif
1904#if defined(MBEDTLS_RIPEMD160_C)
1905 case PSA_ALG_RIPEMD160:
1906 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1907 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1908 break;
1909#endif
1910#if defined(MBEDTLS_SHA1_C)
1911 case PSA_ALG_SHA_1:
1912 mbedtls_sha1_init( &operation->ctx.sha1 );
1913 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1914 break;
1915#endif
1916#if defined(MBEDTLS_SHA256_C)
1917 case PSA_ALG_SHA_224:
1918 mbedtls_sha256_init( &operation->ctx.sha256 );
1919 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1920 break;
1921 case PSA_ALG_SHA_256:
1922 mbedtls_sha256_init( &operation->ctx.sha256 );
1923 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1924 break;
1925#endif
1926#if defined(MBEDTLS_SHA512_C)
1927 case PSA_ALG_SHA_384:
1928 mbedtls_sha512_init( &operation->ctx.sha512 );
1929 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1930 break;
1931 case PSA_ALG_SHA_512:
1932 mbedtls_sha512_init( &operation->ctx.sha512 );
1933 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1934 break;
1935#endif
1936 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001937 return( PSA_ALG_IS_HASH( alg ) ?
1938 PSA_ERROR_NOT_SUPPORTED :
1939 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001940 }
1941 if( ret == 0 )
1942 operation->alg = alg;
1943 else
1944 psa_hash_abort( operation );
1945 return( mbedtls_to_psa_error( ret ) );
1946}
1947
1948psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1949 const uint8_t *input,
1950 size_t input_length )
1951{
1952 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001953
1954 /* Don't require hash implementations to behave correctly on a
1955 * zero-length input, which may have an invalid pointer. */
1956 if( input_length == 0 )
1957 return( PSA_SUCCESS );
1958
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001959 switch( operation->alg )
1960 {
1961#if defined(MBEDTLS_MD2_C)
1962 case PSA_ALG_MD2:
1963 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1964 input, input_length );
1965 break;
1966#endif
1967#if defined(MBEDTLS_MD4_C)
1968 case PSA_ALG_MD4:
1969 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1970 input, input_length );
1971 break;
1972#endif
1973#if defined(MBEDTLS_MD5_C)
1974 case PSA_ALG_MD5:
1975 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1976 input, input_length );
1977 break;
1978#endif
1979#if defined(MBEDTLS_RIPEMD160_C)
1980 case PSA_ALG_RIPEMD160:
1981 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1982 input, input_length );
1983 break;
1984#endif
1985#if defined(MBEDTLS_SHA1_C)
1986 case PSA_ALG_SHA_1:
1987 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1988 input, input_length );
1989 break;
1990#endif
1991#if defined(MBEDTLS_SHA256_C)
1992 case PSA_ALG_SHA_224:
1993 case PSA_ALG_SHA_256:
1994 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1995 input, input_length );
1996 break;
1997#endif
1998#if defined(MBEDTLS_SHA512_C)
1999 case PSA_ALG_SHA_384:
2000 case PSA_ALG_SHA_512:
2001 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2002 input, input_length );
2003 break;
2004#endif
2005 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002006 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002007 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002008
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002009 if( ret != 0 )
2010 psa_hash_abort( operation );
2011 return( mbedtls_to_psa_error( ret ) );
2012}
2013
2014psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2015 uint8_t *hash,
2016 size_t hash_size,
2017 size_t *hash_length )
2018{
itayzafrir40835d42018-08-02 13:14:17 +03002019 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002020 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002021 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002022
2023 /* Fill the output buffer with something that isn't a valid hash
2024 * (barring an attack on the hash and deliberately-crafted input),
2025 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002026 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002027 /* If hash_size is 0 then hash may be NULL and then the
2028 * call to memset would have undefined behavior. */
2029 if( hash_size != 0 )
2030 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002031
2032 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002033 {
2034 status = PSA_ERROR_BUFFER_TOO_SMALL;
2035 goto exit;
2036 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002037
2038 switch( operation->alg )
2039 {
2040#if defined(MBEDTLS_MD2_C)
2041 case PSA_ALG_MD2:
2042 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2043 break;
2044#endif
2045#if defined(MBEDTLS_MD4_C)
2046 case PSA_ALG_MD4:
2047 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2048 break;
2049#endif
2050#if defined(MBEDTLS_MD5_C)
2051 case PSA_ALG_MD5:
2052 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2053 break;
2054#endif
2055#if defined(MBEDTLS_RIPEMD160_C)
2056 case PSA_ALG_RIPEMD160:
2057 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2058 break;
2059#endif
2060#if defined(MBEDTLS_SHA1_C)
2061 case PSA_ALG_SHA_1:
2062 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2063 break;
2064#endif
2065#if defined(MBEDTLS_SHA256_C)
2066 case PSA_ALG_SHA_224:
2067 case PSA_ALG_SHA_256:
2068 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2069 break;
2070#endif
2071#if defined(MBEDTLS_SHA512_C)
2072 case PSA_ALG_SHA_384:
2073 case PSA_ALG_SHA_512:
2074 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2075 break;
2076#endif
2077 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002078 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002079 }
itayzafrir40835d42018-08-02 13:14:17 +03002080 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002081
itayzafrir40835d42018-08-02 13:14:17 +03002082exit:
2083 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002084 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002085 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002086 return( psa_hash_abort( operation ) );
2087 }
2088 else
2089 {
2090 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002091 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002092 }
2093}
2094
Gilles Peskine2d277862018-06-18 15:41:12 +02002095psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2096 const uint8_t *hash,
2097 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002098{
2099 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2100 size_t actual_hash_length;
2101 psa_status_t status = psa_hash_finish( operation,
2102 actual_hash, sizeof( actual_hash ),
2103 &actual_hash_length );
2104 if( status != PSA_SUCCESS )
2105 return( status );
2106 if( actual_hash_length != hash_length )
2107 return( PSA_ERROR_INVALID_SIGNATURE );
2108 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2109 return( PSA_ERROR_INVALID_SIGNATURE );
2110 return( PSA_SUCCESS );
2111}
2112
Gilles Peskineeb35d782019-01-22 17:56:16 +01002113psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2114 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002115{
2116 if( target_operation->alg != 0 )
2117 return( PSA_ERROR_BAD_STATE );
2118
2119 switch( source_operation->alg )
2120 {
2121 case 0:
2122 return( PSA_ERROR_BAD_STATE );
2123#if defined(MBEDTLS_MD2_C)
2124 case PSA_ALG_MD2:
2125 mbedtls_md2_clone( &target_operation->ctx.md2,
2126 &source_operation->ctx.md2 );
2127 break;
2128#endif
2129#if defined(MBEDTLS_MD4_C)
2130 case PSA_ALG_MD4:
2131 mbedtls_md4_clone( &target_operation->ctx.md4,
2132 &source_operation->ctx.md4 );
2133 break;
2134#endif
2135#if defined(MBEDTLS_MD5_C)
2136 case PSA_ALG_MD5:
2137 mbedtls_md5_clone( &target_operation->ctx.md5,
2138 &source_operation->ctx.md5 );
2139 break;
2140#endif
2141#if defined(MBEDTLS_RIPEMD160_C)
2142 case PSA_ALG_RIPEMD160:
2143 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2144 &source_operation->ctx.ripemd160 );
2145 break;
2146#endif
2147#if defined(MBEDTLS_SHA1_C)
2148 case PSA_ALG_SHA_1:
2149 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2150 &source_operation->ctx.sha1 );
2151 break;
2152#endif
2153#if defined(MBEDTLS_SHA256_C)
2154 case PSA_ALG_SHA_224:
2155 case PSA_ALG_SHA_256:
2156 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2157 &source_operation->ctx.sha256 );
2158 break;
2159#endif
2160#if defined(MBEDTLS_SHA512_C)
2161 case PSA_ALG_SHA_384:
2162 case PSA_ALG_SHA_512:
2163 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2164 &source_operation->ctx.sha512 );
2165 break;
2166#endif
2167 default:
2168 return( PSA_ERROR_NOT_SUPPORTED );
2169 }
2170
2171 target_operation->alg = source_operation->alg;
2172 return( PSA_SUCCESS );
2173}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002174
2175
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002176/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002177/* MAC */
2178/****************************************************************/
2179
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002180static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002181 psa_algorithm_t alg,
2182 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002183 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002184 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002185{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002186 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002187 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002188
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002189 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002190 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002191
Gilles Peskine8c9def32018-02-08 10:02:12 +01002192 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2193 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002194 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002195 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002196 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002197 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002198 mode = MBEDTLS_MODE_STREAM;
2199 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002200 case PSA_ALG_CTR:
2201 mode = MBEDTLS_MODE_CTR;
2202 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002203 case PSA_ALG_CFB:
2204 mode = MBEDTLS_MODE_CFB;
2205 break;
2206 case PSA_ALG_OFB:
2207 mode = MBEDTLS_MODE_OFB;
2208 break;
2209 case PSA_ALG_CBC_NO_PADDING:
2210 mode = MBEDTLS_MODE_CBC;
2211 break;
2212 case PSA_ALG_CBC_PKCS7:
2213 mode = MBEDTLS_MODE_CBC;
2214 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002215 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002216 mode = MBEDTLS_MODE_CCM;
2217 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002218 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002219 mode = MBEDTLS_MODE_GCM;
2220 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002221 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2222 mode = MBEDTLS_MODE_CHACHAPOLY;
2223 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002224 default:
2225 return( NULL );
2226 }
2227 }
2228 else if( alg == PSA_ALG_CMAC )
2229 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002230 else
2231 return( NULL );
2232
2233 switch( key_type )
2234 {
2235 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002236 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002237 break;
2238 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002239 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2240 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002241 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002242 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002243 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002244 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002245 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2246 * but two-key Triple-DES is functionally three-key Triple-DES
2247 * with K1=K3, so that's how we present it to mbedtls. */
2248 if( key_bits == 128 )
2249 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002250 break;
2251 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002252 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002253 break;
2254 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002255 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002256 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002257 case PSA_KEY_TYPE_CHACHA20:
2258 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2259 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002260 default:
2261 return( NULL );
2262 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002263 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002264 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002265
Jaeden Amero23bbb752018-06-26 14:16:54 +01002266 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2267 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002268}
2269
Gilles Peskinea05219c2018-11-16 16:02:56 +01002270#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002271static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002272{
Gilles Peskine2d277862018-06-18 15:41:12 +02002273 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002274 {
2275 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002276 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002277 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002278 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002279 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002280 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002281 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002282 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002283 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002284 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002285 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002286 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002287 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002288 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002289 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002290 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002291 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002292 return( 128 );
2293 default:
2294 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002295 }
2296}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002297#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002298
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002299/* Initialize the MAC operation structure. Once this function has been
2300 * called, psa_mac_abort can run and will do the right thing. */
2301static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2302 psa_algorithm_t alg )
2303{
2304 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2305
2306 operation->alg = alg;
2307 operation->key_set = 0;
2308 operation->iv_set = 0;
2309 operation->iv_required = 0;
2310 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002311 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002312
2313#if defined(MBEDTLS_CMAC_C)
2314 if( alg == PSA_ALG_CMAC )
2315 {
2316 operation->iv_required = 0;
2317 mbedtls_cipher_init( &operation->ctx.cmac );
2318 status = PSA_SUCCESS;
2319 }
2320 else
2321#endif /* MBEDTLS_CMAC_C */
2322#if defined(MBEDTLS_MD_C)
2323 if( PSA_ALG_IS_HMAC( operation->alg ) )
2324 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002325 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2326 operation->ctx.hmac.hash_ctx.alg = 0;
2327 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002328 }
2329 else
2330#endif /* MBEDTLS_MD_C */
2331 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002332 if( ! PSA_ALG_IS_MAC( alg ) )
2333 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002334 }
2335
2336 if( status != PSA_SUCCESS )
2337 memset( operation, 0, sizeof( *operation ) );
2338 return( status );
2339}
2340
Gilles Peskine01126fa2018-07-12 17:04:55 +02002341#if defined(MBEDTLS_MD_C)
2342static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2343{
Gilles Peskine3f108122018-12-07 18:14:53 +01002344 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002345 return( psa_hash_abort( &hmac->hash_ctx ) );
2346}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002347
Janos Follath999f6482019-06-11 12:04:10 +01002348#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002349static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2350{
2351 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2352 memset( hmac, 0, sizeof( *hmac ) );
2353}
Janos Follath999f6482019-06-11 12:04:10 +01002354#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002355#endif /* MBEDTLS_MD_C */
2356
Gilles Peskine8c9def32018-02-08 10:02:12 +01002357psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2358{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002359 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002360 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002361 /* The object has (apparently) been initialized but it is not
2362 * in use. It's ok to call abort on such an object, and there's
2363 * nothing to do. */
2364 return( PSA_SUCCESS );
2365 }
2366 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002367#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002368 if( operation->alg == PSA_ALG_CMAC )
2369 {
2370 mbedtls_cipher_free( &operation->ctx.cmac );
2371 }
2372 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002373#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002375 if( PSA_ALG_IS_HMAC( operation->alg ) )
2376 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002377 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002378 }
2379 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002380#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002381 {
2382 /* Sanity check (shouldn't happen: operation->alg should
2383 * always have been initialized to a valid value). */
2384 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002385 }
Moran Peker41deec42018-04-04 15:43:05 +03002386
Gilles Peskine8c9def32018-02-08 10:02:12 +01002387 operation->alg = 0;
2388 operation->key_set = 0;
2389 operation->iv_set = 0;
2390 operation->iv_required = 0;
2391 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002392 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002393
Gilles Peskine8c9def32018-02-08 10:02:12 +01002394 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002395
2396bad_state:
2397 /* If abort is called on an uninitialized object, we can't trust
2398 * anything. Wipe the object in case it contains confidential data.
2399 * This may result in a memory leak if a pointer gets overwritten,
2400 * but it's too late to do anything about this. */
2401 memset( operation, 0, sizeof( *operation ) );
2402 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002403}
2404
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002405#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002406static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002407 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002408 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002409 const mbedtls_cipher_info_t *cipher_info )
2410{
2411 int ret;
2412
2413 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002414
2415 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2416 if( ret != 0 )
2417 return( ret );
2418
2419 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2420 slot->data.raw.data,
2421 key_bits );
2422 return( ret );
2423}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002424#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002425
Gilles Peskine248051a2018-06-20 16:09:38 +02002426#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002427static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2428 const uint8_t *key,
2429 size_t key_length,
2430 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002431{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002432 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002433 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002434 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002435 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002436 psa_status_t status;
2437
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002438 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2439 * overflow below. This should never trigger if the hash algorithm
2440 * is implemented correctly. */
2441 /* The size checks against the ipad and opad buffers cannot be written
2442 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2443 * because that triggers -Wlogical-op on GCC 7.3. */
2444 if( block_size > sizeof( ipad ) )
2445 return( PSA_ERROR_NOT_SUPPORTED );
2446 if( block_size > sizeof( hmac->opad ) )
2447 return( PSA_ERROR_NOT_SUPPORTED );
2448 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002449 return( PSA_ERROR_NOT_SUPPORTED );
2450
Gilles Peskined223b522018-06-11 18:12:58 +02002451 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002452 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002453 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002454 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002455 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002456 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002457 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002458 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002459 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002460 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002461 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002462 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002463 }
Gilles Peskine96889972018-07-12 17:07:03 +02002464 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2465 * but it is permitted. It is common when HMAC is used in HKDF, for
2466 * example. Don't call `memcpy` in the 0-length because `key` could be
2467 * an invalid pointer which would make the behavior undefined. */
2468 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002469 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002470
Gilles Peskined223b522018-06-11 18:12:58 +02002471 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2472 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002473 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002474 ipad[i] ^= 0x36;
2475 memset( ipad + key_length, 0x36, block_size - key_length );
2476
2477 /* Copy the key material from ipad to opad, flipping the requisite bits,
2478 * and filling the rest of opad with the requisite constant. */
2479 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002480 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2481 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002482
Gilles Peskine01126fa2018-07-12 17:04:55 +02002483 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002484 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002485 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002486
Gilles Peskine01126fa2018-07-12 17:04:55 +02002487 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002488
2489cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002490 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002491
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002492 return( status );
2493}
Gilles Peskine248051a2018-06-20 16:09:38 +02002494#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002495
Gilles Peskine89167cb2018-07-08 20:12:23 +02002496static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002497 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002498 psa_algorithm_t alg,
2499 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002501 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002502 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002504 psa_key_usage_t usage =
2505 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002506 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002507 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002508
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002509 /* A context must be freshly initialized before it can be set up. */
2510 if( operation->alg != 0 )
2511 {
2512 return( PSA_ERROR_BAD_STATE );
2513 }
2514
Gilles Peskined911eb72018-08-14 15:18:45 +02002515 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002516 if( status != PSA_SUCCESS )
2517 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002518 if( is_sign )
2519 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520
Gilles Peskine28f8f302019-07-24 13:30:31 +02002521 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002522 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002523 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002524 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002525
Gilles Peskine8c9def32018-02-08 10:02:12 +01002526#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002527 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002528 {
2529 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002530 mbedtls_cipher_info_from_psa( full_length_alg,
2531 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002532 int ret;
2533 if( cipher_info == NULL )
2534 {
2535 status = PSA_ERROR_NOT_SUPPORTED;
2536 goto exit;
2537 }
2538 operation->mac_size = cipher_info->block_size;
2539 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2540 status = mbedtls_to_psa_error( ret );
2541 }
2542 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002543#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002544#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002545 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002546 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002547 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002548 if( hash_alg == 0 )
2549 {
2550 status = PSA_ERROR_NOT_SUPPORTED;
2551 goto exit;
2552 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002553
2554 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2555 /* Sanity check. This shouldn't fail on a valid configuration. */
2556 if( operation->mac_size == 0 ||
2557 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2558 {
2559 status = PSA_ERROR_NOT_SUPPORTED;
2560 goto exit;
2561 }
2562
Gilles Peskine01126fa2018-07-12 17:04:55 +02002563 if( slot->type != PSA_KEY_TYPE_HMAC )
2564 {
2565 status = PSA_ERROR_INVALID_ARGUMENT;
2566 goto exit;
2567 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002568
Gilles Peskine01126fa2018-07-12 17:04:55 +02002569 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2570 slot->data.raw.data,
2571 slot->data.raw.bytes,
2572 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002573 }
2574 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002576 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002577 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002578 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002579 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002580
Gilles Peskined911eb72018-08-14 15:18:45 +02002581 if( truncated == 0 )
2582 {
2583 /* The "normal" case: untruncated algorithm. Nothing to do. */
2584 }
2585 else if( truncated < 4 )
2586 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002587 /* A very short MAC is too short for security since it can be
2588 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2589 * so we make this our minimum, even though 32 bits is still
2590 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002591 status = PSA_ERROR_NOT_SUPPORTED;
2592 }
2593 else if( truncated > operation->mac_size )
2594 {
2595 /* It's impossible to "truncate" to a larger length. */
2596 status = PSA_ERROR_INVALID_ARGUMENT;
2597 }
2598 else
2599 operation->mac_size = truncated;
2600
Gilles Peskinefbfac682018-07-08 20:51:54 +02002601exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002602 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002604 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002606 else
2607 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002608 operation->key_set = 1;
2609 }
2610 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611}
2612
Gilles Peskine89167cb2018-07-08 20:12:23 +02002613psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002614 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002615 psa_algorithm_t alg )
2616{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002617 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002618}
2619
2620psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002621 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002622 psa_algorithm_t alg )
2623{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002624 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002625}
2626
Gilles Peskine8c9def32018-02-08 10:02:12 +01002627psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2628 const uint8_t *input,
2629 size_t input_length )
2630{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002631 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002632 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002633 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002634 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002635 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002636 operation->has_input = 1;
2637
Gilles Peskine8c9def32018-02-08 10:02:12 +01002638#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002639 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002640 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002641 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2642 input, input_length );
2643 status = mbedtls_to_psa_error( ret );
2644 }
2645 else
2646#endif /* MBEDTLS_CMAC_C */
2647#if defined(MBEDTLS_MD_C)
2648 if( PSA_ALG_IS_HMAC( operation->alg ) )
2649 {
2650 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2651 input_length );
2652 }
2653 else
2654#endif /* MBEDTLS_MD_C */
2655 {
2656 /* This shouldn't happen if `operation` was initialized by
2657 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002658 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002659 }
2660
Gilles Peskinefbfac682018-07-08 20:51:54 +02002661 if( status != PSA_SUCCESS )
2662 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002663 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664}
2665
Gilles Peskine01126fa2018-07-12 17:04:55 +02002666#if defined(MBEDTLS_MD_C)
2667static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2668 uint8_t *mac,
2669 size_t mac_size )
2670{
2671 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2672 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2673 size_t hash_size = 0;
2674 size_t block_size = psa_get_hash_block_size( hash_alg );
2675 psa_status_t status;
2676
Gilles Peskine01126fa2018-07-12 17:04:55 +02002677 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2678 if( status != PSA_SUCCESS )
2679 return( status );
2680 /* From here on, tmp needs to be wiped. */
2681
2682 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2683 if( status != PSA_SUCCESS )
2684 goto exit;
2685
2686 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2687 if( status != PSA_SUCCESS )
2688 goto exit;
2689
2690 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2691 if( status != PSA_SUCCESS )
2692 goto exit;
2693
Gilles Peskined911eb72018-08-14 15:18:45 +02002694 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2695 if( status != PSA_SUCCESS )
2696 goto exit;
2697
2698 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002699
2700exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002701 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002702 return( status );
2703}
2704#endif /* MBEDTLS_MD_C */
2705
mohammad16036df908f2018-04-02 08:34:15 -07002706static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002707 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002708 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002709{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002710 if( ! operation->key_set )
2711 return( PSA_ERROR_BAD_STATE );
2712 if( operation->iv_required && ! operation->iv_set )
2713 return( PSA_ERROR_BAD_STATE );
2714
Gilles Peskine8c9def32018-02-08 10:02:12 +01002715 if( mac_size < operation->mac_size )
2716 return( PSA_ERROR_BUFFER_TOO_SMALL );
2717
Gilles Peskine8c9def32018-02-08 10:02:12 +01002718#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002719 if( operation->alg == PSA_ALG_CMAC )
2720 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002721 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2722 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2723 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002724 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002725 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002726 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002728 else
2729#endif /* MBEDTLS_CMAC_C */
2730#if defined(MBEDTLS_MD_C)
2731 if( PSA_ALG_IS_HMAC( operation->alg ) )
2732 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002733 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002734 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002735 }
2736 else
2737#endif /* MBEDTLS_MD_C */
2738 {
2739 /* This shouldn't happen if `operation` was initialized by
2740 * a setup function. */
2741 return( PSA_ERROR_BAD_STATE );
2742 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002743}
2744
Gilles Peskineacd4be32018-07-08 19:56:25 +02002745psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2746 uint8_t *mac,
2747 size_t mac_size,
2748 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002749{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002750 psa_status_t status;
2751
Jaeden Amero252ef282019-02-15 14:05:35 +00002752 if( operation->alg == 0 )
2753 {
2754 return( PSA_ERROR_BAD_STATE );
2755 }
2756
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002757 /* Fill the output buffer with something that isn't a valid mac
2758 * (barring an attack on the mac and deliberately-crafted input),
2759 * in case the caller doesn't check the return status properly. */
2760 *mac_length = mac_size;
2761 /* If mac_size is 0 then mac may be NULL and then the
2762 * call to memset would have undefined behavior. */
2763 if( mac_size != 0 )
2764 memset( mac, '!', mac_size );
2765
Gilles Peskine89167cb2018-07-08 20:12:23 +02002766 if( ! operation->is_sign )
2767 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002768 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002769 }
mohammad16036df908f2018-04-02 08:34:15 -07002770
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002771 status = psa_mac_finish_internal( operation, mac, mac_size );
2772
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002773 if( status == PSA_SUCCESS )
2774 {
2775 status = psa_mac_abort( operation );
2776 if( status == PSA_SUCCESS )
2777 *mac_length = operation->mac_size;
2778 else
2779 memset( mac, '!', mac_size );
2780 }
2781 else
2782 psa_mac_abort( operation );
2783 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002784}
2785
Gilles Peskineacd4be32018-07-08 19:56:25 +02002786psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2787 const uint8_t *mac,
2788 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002789{
Gilles Peskine828ed142018-06-18 23:25:51 +02002790 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002791 psa_status_t status;
2792
Jaeden Amero252ef282019-02-15 14:05:35 +00002793 if( operation->alg == 0 )
2794 {
2795 return( PSA_ERROR_BAD_STATE );
2796 }
2797
Gilles Peskine89167cb2018-07-08 20:12:23 +02002798 if( operation->is_sign )
2799 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002800 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002801 }
2802 if( operation->mac_size != mac_length )
2803 {
2804 status = PSA_ERROR_INVALID_SIGNATURE;
2805 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002806 }
mohammad16036df908f2018-04-02 08:34:15 -07002807
2808 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002809 actual_mac, sizeof( actual_mac ) );
2810
2811 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2812 status = PSA_ERROR_INVALID_SIGNATURE;
2813
2814cleanup:
2815 if( status == PSA_SUCCESS )
2816 status = psa_mac_abort( operation );
2817 else
2818 psa_mac_abort( operation );
2819
Gilles Peskine3f108122018-12-07 18:14:53 +01002820 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002821
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002822 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002823}
2824
2825
Gilles Peskine20035e32018-02-03 22:44:14 +01002826
Gilles Peskine20035e32018-02-03 22:44:14 +01002827/****************************************************************/
2828/* Asymmetric cryptography */
2829/****************************************************************/
2830
Gilles Peskine2b450e32018-06-27 15:42:46 +02002831#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002832/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002833 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002834static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2835 size_t hash_length,
2836 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002837{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002838 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002839 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002840 *md_alg = mbedtls_md_get_type( md_info );
2841
2842 /* The Mbed TLS RSA module uses an unsigned int for hash length
2843 * parameters. Validate that it fits so that we don't risk an
2844 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002845#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002846 if( hash_length > UINT_MAX )
2847 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002848#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002849
2850#if defined(MBEDTLS_PKCS1_V15)
2851 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2852 * must be correct. */
2853 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2854 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002855 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002856 if( md_info == NULL )
2857 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002858 if( mbedtls_md_get_size( md_info ) != hash_length )
2859 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002860 }
2861#endif /* MBEDTLS_PKCS1_V15 */
2862
2863#if defined(MBEDTLS_PKCS1_V21)
2864 /* PSS requires a hash internally. */
2865 if( PSA_ALG_IS_RSA_PSS( alg ) )
2866 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002867 if( md_info == NULL )
2868 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002869 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002870#endif /* MBEDTLS_PKCS1_V21 */
2871
Gilles Peskine61b91d42018-06-08 16:09:36 +02002872 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002873}
2874
Gilles Peskine2b450e32018-06-27 15:42:46 +02002875static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2876 psa_algorithm_t alg,
2877 const uint8_t *hash,
2878 size_t hash_length,
2879 uint8_t *signature,
2880 size_t signature_size,
2881 size_t *signature_length )
2882{
2883 psa_status_t status;
2884 int ret;
2885 mbedtls_md_type_t md_alg;
2886
2887 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2888 if( status != PSA_SUCCESS )
2889 return( status );
2890
Gilles Peskine630a18a2018-06-29 17:49:35 +02002891 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002892 return( PSA_ERROR_BUFFER_TOO_SMALL );
2893
2894#if defined(MBEDTLS_PKCS1_V15)
2895 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2896 {
2897 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2898 MBEDTLS_MD_NONE );
2899 ret = mbedtls_rsa_pkcs1_sign( rsa,
2900 mbedtls_ctr_drbg_random,
2901 &global_data.ctr_drbg,
2902 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002903 md_alg,
2904 (unsigned int) hash_length,
2905 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002906 signature );
2907 }
2908 else
2909#endif /* MBEDTLS_PKCS1_V15 */
2910#if defined(MBEDTLS_PKCS1_V21)
2911 if( PSA_ALG_IS_RSA_PSS( alg ) )
2912 {
2913 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2914 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2915 mbedtls_ctr_drbg_random,
2916 &global_data.ctr_drbg,
2917 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002918 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002919 (unsigned int) hash_length,
2920 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002921 signature );
2922 }
2923 else
2924#endif /* MBEDTLS_PKCS1_V21 */
2925 {
2926 return( PSA_ERROR_INVALID_ARGUMENT );
2927 }
2928
2929 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002930 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002931 return( mbedtls_to_psa_error( ret ) );
2932}
2933
2934static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2935 psa_algorithm_t alg,
2936 const uint8_t *hash,
2937 size_t hash_length,
2938 const uint8_t *signature,
2939 size_t signature_length )
2940{
2941 psa_status_t status;
2942 int ret;
2943 mbedtls_md_type_t md_alg;
2944
2945 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2946 if( status != PSA_SUCCESS )
2947 return( status );
2948
Gilles Peskine630a18a2018-06-29 17:49:35 +02002949 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002950 return( PSA_ERROR_BUFFER_TOO_SMALL );
2951
2952#if defined(MBEDTLS_PKCS1_V15)
2953 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2954 {
2955 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2956 MBEDTLS_MD_NONE );
2957 ret = mbedtls_rsa_pkcs1_verify( rsa,
2958 mbedtls_ctr_drbg_random,
2959 &global_data.ctr_drbg,
2960 MBEDTLS_RSA_PUBLIC,
2961 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002962 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002963 hash,
2964 signature );
2965 }
2966 else
2967#endif /* MBEDTLS_PKCS1_V15 */
2968#if defined(MBEDTLS_PKCS1_V21)
2969 if( PSA_ALG_IS_RSA_PSS( alg ) )
2970 {
2971 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2972 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2973 mbedtls_ctr_drbg_random,
2974 &global_data.ctr_drbg,
2975 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002976 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002977 (unsigned int) hash_length,
2978 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002979 signature );
2980 }
2981 else
2982#endif /* MBEDTLS_PKCS1_V21 */
2983 {
2984 return( PSA_ERROR_INVALID_ARGUMENT );
2985 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002986
2987 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2988 * the rest of the signature is invalid". This has little use in
2989 * practice and PSA doesn't report this distinction. */
2990 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2991 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002992 return( mbedtls_to_psa_error( ret ) );
2993}
2994#endif /* MBEDTLS_RSA_C */
2995
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002996#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002997/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2998 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2999 * (even though these functions don't modify it). */
3000static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3001 psa_algorithm_t alg,
3002 const uint8_t *hash,
3003 size_t hash_length,
3004 uint8_t *signature,
3005 size_t signature_size,
3006 size_t *signature_length )
3007{
3008 int ret;
3009 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003010 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003011 mbedtls_mpi_init( &r );
3012 mbedtls_mpi_init( &s );
3013
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003014 if( signature_size < 2 * curve_bytes )
3015 {
3016 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3017 goto cleanup;
3018 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003019
Gilles Peskinea05219c2018-11-16 16:02:56 +01003020#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003021 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3022 {
3023 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3024 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3025 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3026 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3027 hash, hash_length,
3028 md_alg ) );
3029 }
3030 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003031#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003032 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003033 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003034 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3035 hash, hash_length,
3036 mbedtls_ctr_drbg_random,
3037 &global_data.ctr_drbg ) );
3038 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003039
3040 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3041 signature,
3042 curve_bytes ) );
3043 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3044 signature + curve_bytes,
3045 curve_bytes ) );
3046
3047cleanup:
3048 mbedtls_mpi_free( &r );
3049 mbedtls_mpi_free( &s );
3050 if( ret == 0 )
3051 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003052 return( mbedtls_to_psa_error( ret ) );
3053}
3054
3055static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3056 const uint8_t *hash,
3057 size_t hash_length,
3058 const uint8_t *signature,
3059 size_t signature_length )
3060{
3061 int ret;
3062 mbedtls_mpi r, s;
3063 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3064 mbedtls_mpi_init( &r );
3065 mbedtls_mpi_init( &s );
3066
3067 if( signature_length != 2 * curve_bytes )
3068 return( PSA_ERROR_INVALID_SIGNATURE );
3069
3070 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3071 signature,
3072 curve_bytes ) );
3073 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3074 signature + curve_bytes,
3075 curve_bytes ) );
3076
3077 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3078 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003079
3080cleanup:
3081 mbedtls_mpi_free( &r );
3082 mbedtls_mpi_free( &s );
3083 return( mbedtls_to_psa_error( ret ) );
3084}
3085#endif /* MBEDTLS_ECDSA_C */
3086
Gilles Peskinec5487a82018-12-03 18:08:14 +01003087psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003088 psa_algorithm_t alg,
3089 const uint8_t *hash,
3090 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003091 uint8_t *signature,
3092 size_t signature_size,
3093 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003094{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003095 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003096 psa_status_t status;
3097
3098 *signature_length = signature_size;
3099
Gilles Peskine28f8f302019-07-24 13:30:31 +02003100 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003101 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003102 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003103 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003104 {
3105 status = PSA_ERROR_INVALID_ARGUMENT;
3106 goto exit;
3107 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003108
Gilles Peskine20035e32018-02-03 22:44:14 +01003109#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003110 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003111 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003112 status = psa_rsa_sign( slot->data.rsa,
3113 alg,
3114 hash, hash_length,
3115 signature, signature_size,
3116 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003117 }
3118 else
3119#endif /* defined(MBEDTLS_RSA_C) */
3120#if defined(MBEDTLS_ECP_C)
3121 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3122 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003123#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003124 if(
3125#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3126 PSA_ALG_IS_ECDSA( alg )
3127#else
3128 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3129#endif
3130 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003131 status = psa_ecdsa_sign( slot->data.ecp,
3132 alg,
3133 hash, hash_length,
3134 signature, signature_size,
3135 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003136 else
3137#endif /* defined(MBEDTLS_ECDSA_C) */
3138 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003139 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003140 }
itayzafrir5c753392018-05-08 11:18:38 +03003141 }
3142 else
3143#endif /* defined(MBEDTLS_ECP_C) */
3144 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003145 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003146 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003147
3148exit:
3149 /* Fill the unused part of the output buffer (the whole buffer on error,
3150 * the trailing part on success) with something that isn't a valid mac
3151 * (barring an attack on the mac and deliberately-crafted input),
3152 * in case the caller doesn't check the return status properly. */
3153 if( status == PSA_SUCCESS )
3154 memset( signature + *signature_length, '!',
3155 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003156 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003157 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003158 /* If signature_size is 0 then we have nothing to do. We must not call
3159 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003160 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003161}
3162
Gilles Peskinec5487a82018-12-03 18:08:14 +01003163psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003164 psa_algorithm_t alg,
3165 const uint8_t *hash,
3166 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003167 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003168 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003169{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003170 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003171 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003172
Gilles Peskine28f8f302019-07-24 13:30:31 +02003173 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003174 if( status != PSA_SUCCESS )
3175 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003176
Gilles Peskine61b91d42018-06-08 16:09:36 +02003177#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003178 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003179 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003180 return( psa_rsa_verify( slot->data.rsa,
3181 alg,
3182 hash, hash_length,
3183 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003184 }
3185 else
3186#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003187#if defined(MBEDTLS_ECP_C)
3188 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3189 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003190#if defined(MBEDTLS_ECDSA_C)
3191 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003192 return( psa_ecdsa_verify( slot->data.ecp,
3193 hash, hash_length,
3194 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003195 else
3196#endif /* defined(MBEDTLS_ECDSA_C) */
3197 {
3198 return( PSA_ERROR_INVALID_ARGUMENT );
3199 }
itayzafrir5c753392018-05-08 11:18:38 +03003200 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003201 else
3202#endif /* defined(MBEDTLS_ECP_C) */
3203 {
3204 return( PSA_ERROR_NOT_SUPPORTED );
3205 }
3206}
3207
Gilles Peskine072ac562018-06-30 00:21:29 +02003208#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3209static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3210 mbedtls_rsa_context *rsa )
3211{
3212 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3213 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3214 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3215 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3216}
3217#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3218
Gilles Peskinec5487a82018-12-03 18:08:14 +01003219psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003220 psa_algorithm_t alg,
3221 const uint8_t *input,
3222 size_t input_length,
3223 const uint8_t *salt,
3224 size_t salt_length,
3225 uint8_t *output,
3226 size_t output_size,
3227 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003228{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003229 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003230 psa_status_t status;
3231
Darryl Green5cc689a2018-07-24 15:34:10 +01003232 (void) input;
3233 (void) input_length;
3234 (void) salt;
3235 (void) output;
3236 (void) output_size;
3237
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003238 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003239
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003240 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3241 return( PSA_ERROR_INVALID_ARGUMENT );
3242
Gilles Peskine28f8f302019-07-24 13:30:31 +02003243 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003244 if( status != PSA_SUCCESS )
3245 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003246 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003247 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003248 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003249
3250#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003251 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003252 {
3253 mbedtls_rsa_context *rsa = slot->data.rsa;
3254 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003255 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003256 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003257#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003258 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003259 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003260 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3261 mbedtls_ctr_drbg_random,
3262 &global_data.ctr_drbg,
3263 MBEDTLS_RSA_PUBLIC,
3264 input_length,
3265 input,
3266 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003267 }
3268 else
3269#endif /* MBEDTLS_PKCS1_V15 */
3270#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003271 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003272 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003273 psa_rsa_oaep_set_padding_mode( alg, rsa );
3274 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3275 mbedtls_ctr_drbg_random,
3276 &global_data.ctr_drbg,
3277 MBEDTLS_RSA_PUBLIC,
3278 salt, salt_length,
3279 input_length,
3280 input,
3281 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003282 }
3283 else
3284#endif /* MBEDTLS_PKCS1_V21 */
3285 {
3286 return( PSA_ERROR_INVALID_ARGUMENT );
3287 }
3288 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003289 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003290 return( mbedtls_to_psa_error( ret ) );
3291 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003292 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003293#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003294 {
3295 return( PSA_ERROR_NOT_SUPPORTED );
3296 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003297}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003298
Gilles Peskinec5487a82018-12-03 18:08:14 +01003299psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003300 psa_algorithm_t alg,
3301 const uint8_t *input,
3302 size_t input_length,
3303 const uint8_t *salt,
3304 size_t salt_length,
3305 uint8_t *output,
3306 size_t output_size,
3307 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003308{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003309 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003310 psa_status_t status;
3311
Darryl Green5cc689a2018-07-24 15:34:10 +01003312 (void) input;
3313 (void) input_length;
3314 (void) salt;
3315 (void) output;
3316 (void) output_size;
3317
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003318 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003319
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003320 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3321 return( PSA_ERROR_INVALID_ARGUMENT );
3322
Gilles Peskine28f8f302019-07-24 13:30:31 +02003323 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003324 if( status != PSA_SUCCESS )
3325 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003326 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003327 return( PSA_ERROR_INVALID_ARGUMENT );
3328
3329#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003330 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003331 {
3332 mbedtls_rsa_context *rsa = slot->data.rsa;
3333 int ret;
3334
Gilles Peskine630a18a2018-06-29 17:49:35 +02003335 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003336 return( PSA_ERROR_INVALID_ARGUMENT );
3337
3338#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003339 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003340 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003341 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3342 mbedtls_ctr_drbg_random,
3343 &global_data.ctr_drbg,
3344 MBEDTLS_RSA_PRIVATE,
3345 output_length,
3346 input,
3347 output,
3348 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003349 }
3350 else
3351#endif /* MBEDTLS_PKCS1_V15 */
3352#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003353 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003354 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003355 psa_rsa_oaep_set_padding_mode( alg, rsa );
3356 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3357 mbedtls_ctr_drbg_random,
3358 &global_data.ctr_drbg,
3359 MBEDTLS_RSA_PRIVATE,
3360 salt, salt_length,
3361 output_length,
3362 input,
3363 output,
3364 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003365 }
3366 else
3367#endif /* MBEDTLS_PKCS1_V21 */
3368 {
3369 return( PSA_ERROR_INVALID_ARGUMENT );
3370 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003371
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003372 return( mbedtls_to_psa_error( ret ) );
3373 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003374 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003375#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003376 {
3377 return( PSA_ERROR_NOT_SUPPORTED );
3378 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003379}
Gilles Peskine20035e32018-02-03 22:44:14 +01003380
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003381
3382
mohammad1603503973b2018-03-12 15:59:30 +02003383/****************************************************************/
3384/* Symmetric cryptography */
3385/****************************************************************/
3386
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003387/* Initialize the cipher operation structure. Once this function has been
3388 * called, psa_cipher_abort can run and will do the right thing. */
3389static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3390 psa_algorithm_t alg )
3391{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003392 if( ! PSA_ALG_IS_CIPHER( alg ) )
3393 {
3394 memset( operation, 0, sizeof( *operation ) );
3395 return( PSA_ERROR_INVALID_ARGUMENT );
3396 }
3397
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003398 operation->alg = alg;
3399 operation->key_set = 0;
3400 operation->iv_set = 0;
3401 operation->iv_required = 1;
3402 operation->iv_size = 0;
3403 operation->block_size = 0;
3404 mbedtls_cipher_init( &operation->ctx.cipher );
3405 return( PSA_SUCCESS );
3406}
3407
Gilles Peskinee553c652018-06-04 16:22:46 +02003408static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003409 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003410 psa_algorithm_t alg,
3411 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003412{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003413 int ret = 0;
3414 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003415 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003416 size_t key_bits;
3417 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003418 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3419 PSA_KEY_USAGE_ENCRYPT :
3420 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003421
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003422 /* A context must be freshly initialized before it can be set up. */
3423 if( operation->alg != 0 )
3424 {
3425 return( PSA_ERROR_BAD_STATE );
3426 }
3427
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003428 status = psa_cipher_init( operation, alg );
3429 if( status != PSA_SUCCESS )
3430 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003431
Gilles Peskine28f8f302019-07-24 13:30:31 +02003432 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003433 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003434 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003435 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003436
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003437 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003438 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003439 {
3440 status = PSA_ERROR_NOT_SUPPORTED;
3441 goto exit;
3442 }
mohammad1603503973b2018-03-12 15:59:30 +02003443
mohammad1603503973b2018-03-12 15:59:30 +02003444 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003445 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003446 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003447
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003448#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003449 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003450 {
3451 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3452 unsigned char keys[24];
3453 memcpy( keys, slot->data.raw.data, 16 );
3454 memcpy( keys + 16, slot->data.raw.data, 8 );
3455 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3456 keys,
3457 192, cipher_operation );
3458 }
3459 else
3460#endif
3461 {
3462 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3463 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003464 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003465 }
Moran Peker41deec42018-04-04 15:43:05 +03003466 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003467 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003468
mohammad16038481e742018-03-18 13:57:31 +02003469#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003470 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003471 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003472 case PSA_ALG_CBC_NO_PADDING:
3473 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3474 MBEDTLS_PADDING_NONE );
3475 break;
3476 case PSA_ALG_CBC_PKCS7:
3477 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3478 MBEDTLS_PADDING_PKCS7 );
3479 break;
3480 default:
3481 /* The algorithm doesn't involve padding. */
3482 ret = 0;
3483 break;
3484 }
3485 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003486 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003487#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3488
mohammad1603503973b2018-03-12 15:59:30 +02003489 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003490 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3491 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3492 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003493 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003494 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003495 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003496#if defined(MBEDTLS_CHACHA20_C)
3497 else
3498 if( alg == PSA_ALG_CHACHA20 )
3499 operation->iv_size = 12;
3500#endif
mohammad1603503973b2018-03-12 15:59:30 +02003501
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003502exit:
3503 if( status == 0 )
3504 status = mbedtls_to_psa_error( ret );
3505 if( status != 0 )
3506 psa_cipher_abort( operation );
3507 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003508}
3509
Gilles Peskinefe119512018-07-08 21:39:34 +02003510psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003511 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003512 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003513{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003514 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003515}
3516
Gilles Peskinefe119512018-07-08 21:39:34 +02003517psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003518 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003519 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003520{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003521 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003522}
3523
Gilles Peskinefe119512018-07-08 21:39:34 +02003524psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3525 unsigned char *iv,
3526 size_t iv_size,
3527 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003528{
itayzafrir534bd7c2018-08-02 13:56:32 +03003529 psa_status_t status;
3530 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003531 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003532 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003533 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003534 }
Moran Peker41deec42018-04-04 15:43:05 +03003535 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003536 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003537 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003538 goto exit;
3539 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003540 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3541 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003542 if( ret != 0 )
3543 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003544 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003545 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003546 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003547
mohammad16038481e742018-03-18 13:57:31 +02003548 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003549 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003550
Moran Peker395db872018-05-31 14:07:14 +03003551exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003552 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003553 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003554 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003555}
3556
Gilles Peskinefe119512018-07-08 21:39:34 +02003557psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3558 const unsigned char *iv,
3559 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003560{
itayzafrir534bd7c2018-08-02 13:56:32 +03003561 psa_status_t status;
3562 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003563 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003564 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003565 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003566 }
Moran Pekera28258c2018-05-29 16:25:04 +03003567 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003568 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003569 status = PSA_ERROR_INVALID_ARGUMENT;
3570 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003571 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003572 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3573 status = mbedtls_to_psa_error( ret );
3574exit:
3575 if( status == PSA_SUCCESS )
3576 operation->iv_set = 1;
3577 else
mohammad1603503973b2018-03-12 15:59:30 +02003578 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003579 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003580}
3581
Gilles Peskinee553c652018-06-04 16:22:46 +02003582psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3583 const uint8_t *input,
3584 size_t input_length,
3585 unsigned char *output,
3586 size_t output_size,
3587 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003588{
itayzafrir534bd7c2018-08-02 13:56:32 +03003589 psa_status_t status;
3590 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003591 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003592
3593 if( operation->alg == 0 )
3594 {
3595 return( PSA_ERROR_BAD_STATE );
3596 }
3597
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003598 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003599 {
3600 /* Take the unprocessed partial block left over from previous
3601 * update calls, if any, plus the input to this call. Remove
3602 * the last partial block, if any. You get the data that will be
3603 * output in this call. */
3604 expected_output_size =
3605 ( operation->ctx.cipher.unprocessed_len + input_length )
3606 / operation->block_size * operation->block_size;
3607 }
3608 else
3609 {
3610 expected_output_size = input_length;
3611 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003612
Gilles Peskine89d789c2018-06-04 17:17:16 +02003613 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003614 {
3615 status = PSA_ERROR_BUFFER_TOO_SMALL;
3616 goto exit;
3617 }
mohammad160382759612018-03-12 18:16:40 +02003618
mohammad1603503973b2018-03-12 15:59:30 +02003619 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003620 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003621 status = mbedtls_to_psa_error( ret );
3622exit:
3623 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003624 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003625 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003626}
3627
Gilles Peskinee553c652018-06-04 16:22:46 +02003628psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3629 uint8_t *output,
3630 size_t output_size,
3631 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003632{
David Saadab4ecc272019-02-14 13:48:10 +02003633 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003634 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003635 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003636
mohammad1603503973b2018-03-12 15:59:30 +02003637 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003638 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003639 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003640 }
3641 if( operation->iv_required && ! operation->iv_set )
3642 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003643 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003644 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003645
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003646 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003647 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3648 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003649 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003650 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003651 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003652 }
3653
Janos Follath315b51c2018-07-09 16:04:51 +01003654 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3655 temp_output_buffer,
3656 output_length );
3657 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003658 {
Janos Follath315b51c2018-07-09 16:04:51 +01003659 status = mbedtls_to_psa_error( cipher_ret );
3660 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003661 }
Janos Follath315b51c2018-07-09 16:04:51 +01003662
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003663 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003664 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003665 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003666 memcpy( output, temp_output_buffer, *output_length );
3667 else
3668 {
Janos Follath315b51c2018-07-09 16:04:51 +01003669 status = PSA_ERROR_BUFFER_TOO_SMALL;
3670 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003671 }
mohammad1603503973b2018-03-12 15:59:30 +02003672
Gilles Peskine3f108122018-12-07 18:14:53 +01003673 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003674 status = psa_cipher_abort( operation );
3675
3676 return( status );
3677
3678error:
3679
3680 *output_length = 0;
3681
Gilles Peskine3f108122018-12-07 18:14:53 +01003682 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003683 (void) psa_cipher_abort( operation );
3684
3685 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003686}
3687
Gilles Peskinee553c652018-06-04 16:22:46 +02003688psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3689{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003690 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003691 {
3692 /* The object has (apparently) been initialized but it is not
3693 * in use. It's ok to call abort on such an object, and there's
3694 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003695 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003696 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003697
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003698 /* Sanity check (shouldn't happen: operation->alg should
3699 * always have been initialized to a valid value). */
3700 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3701 return( PSA_ERROR_BAD_STATE );
3702
mohammad1603503973b2018-03-12 15:59:30 +02003703 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003704
Moran Peker41deec42018-04-04 15:43:05 +03003705 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003706 operation->key_set = 0;
3707 operation->iv_set = 0;
3708 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003709 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003710 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003711
Moran Peker395db872018-05-31 14:07:14 +03003712 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003713}
3714
Gilles Peskinea0655c32018-04-30 17:06:50 +02003715
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003716
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003717
mohammad16035955c982018-04-26 00:53:03 +03003718/****************************************************************/
3719/* AEAD */
3720/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003721
Gilles Peskineedf9a652018-08-17 18:11:56 +02003722typedef struct
3723{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003724 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003725 const mbedtls_cipher_info_t *cipher_info;
3726 union
3727 {
3728#if defined(MBEDTLS_CCM_C)
3729 mbedtls_ccm_context ccm;
3730#endif /* MBEDTLS_CCM_C */
3731#if defined(MBEDTLS_GCM_C)
3732 mbedtls_gcm_context gcm;
3733#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003734#if defined(MBEDTLS_CHACHAPOLY_C)
3735 mbedtls_chachapoly_context chachapoly;
3736#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003737 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003738 psa_algorithm_t core_alg;
3739 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003740 uint8_t tag_length;
3741} aead_operation_t;
3742
Gilles Peskine30a9e412019-01-14 18:36:12 +01003743static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003744{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003745 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003746 {
3747#if defined(MBEDTLS_CCM_C)
3748 case PSA_ALG_CCM:
3749 mbedtls_ccm_free( &operation->ctx.ccm );
3750 break;
3751#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003752#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003753 case PSA_ALG_GCM:
3754 mbedtls_gcm_free( &operation->ctx.gcm );
3755 break;
3756#endif /* MBEDTLS_GCM_C */
3757 }
3758}
3759
3760static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003761 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003762 psa_key_usage_t usage,
3763 psa_algorithm_t alg )
3764{
3765 psa_status_t status;
3766 size_t key_bits;
3767 mbedtls_cipher_id_t cipher_id;
3768
Gilles Peskine28f8f302019-07-24 13:30:31 +02003769 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003770 if( status != PSA_SUCCESS )
3771 return( status );
3772
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003773 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003774
3775 operation->cipher_info =
3776 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3777 &cipher_id );
3778 if( operation->cipher_info == NULL )
3779 return( PSA_ERROR_NOT_SUPPORTED );
3780
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003781 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003782 {
3783#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003784 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3785 operation->core_alg = PSA_ALG_CCM;
3786 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003787 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3788 * The call to mbedtls_ccm_encrypt_and_tag or
3789 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003790 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3791 return( PSA_ERROR_INVALID_ARGUMENT );
3792 mbedtls_ccm_init( &operation->ctx.ccm );
3793 status = mbedtls_to_psa_error(
3794 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3795 operation->slot->data.raw.data,
3796 (unsigned int) key_bits ) );
3797 if( status != 0 )
3798 goto cleanup;
3799 break;
3800#endif /* MBEDTLS_CCM_C */
3801
3802#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003803 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3804 operation->core_alg = PSA_ALG_GCM;
3805 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003806 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3807 * The call to mbedtls_gcm_crypt_and_tag or
3808 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003809 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3810 return( PSA_ERROR_INVALID_ARGUMENT );
3811 mbedtls_gcm_init( &operation->ctx.gcm );
3812 status = mbedtls_to_psa_error(
3813 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3814 operation->slot->data.raw.data,
3815 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003816 if( status != 0 )
3817 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003818 break;
3819#endif /* MBEDTLS_GCM_C */
3820
Gilles Peskine26869f22019-05-06 15:25:00 +02003821#if defined(MBEDTLS_CHACHAPOLY_C)
3822 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3823 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3824 operation->full_tag_length = 16;
3825 /* We only support the default tag length. */
3826 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3827 return( PSA_ERROR_NOT_SUPPORTED );
3828 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3829 status = mbedtls_to_psa_error(
3830 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3831 operation->slot->data.raw.data ) );
3832 if( status != 0 )
3833 goto cleanup;
3834 break;
3835#endif /* MBEDTLS_CHACHAPOLY_C */
3836
Gilles Peskineedf9a652018-08-17 18:11:56 +02003837 default:
3838 return( PSA_ERROR_NOT_SUPPORTED );
3839 }
3840
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003841 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3842 {
3843 status = PSA_ERROR_INVALID_ARGUMENT;
3844 goto cleanup;
3845 }
3846 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003847
Gilles Peskineedf9a652018-08-17 18:11:56 +02003848 return( PSA_SUCCESS );
3849
3850cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003851 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003852 return( status );
3853}
3854
Gilles Peskinec5487a82018-12-03 18:08:14 +01003855psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003856 psa_algorithm_t alg,
3857 const uint8_t *nonce,
3858 size_t nonce_length,
3859 const uint8_t *additional_data,
3860 size_t additional_data_length,
3861 const uint8_t *plaintext,
3862 size_t plaintext_length,
3863 uint8_t *ciphertext,
3864 size_t ciphertext_size,
3865 size_t *ciphertext_length )
3866{
mohammad16035955c982018-04-26 00:53:03 +03003867 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003868 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003869 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003870
mohammad1603f08a5502018-06-03 15:05:47 +03003871 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003872
Gilles Peskinec5487a82018-12-03 18:08:14 +01003873 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003874 if( status != PSA_SUCCESS )
3875 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003876
Gilles Peskineedf9a652018-08-17 18:11:56 +02003877 /* For all currently supported modes, the tag is at the end of the
3878 * ciphertext. */
3879 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3880 {
3881 status = PSA_ERROR_BUFFER_TOO_SMALL;
3882 goto exit;
3883 }
3884 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003885
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003886#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003887 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003888 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003889 status = mbedtls_to_psa_error(
3890 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3891 MBEDTLS_GCM_ENCRYPT,
3892 plaintext_length,
3893 nonce, nonce_length,
3894 additional_data, additional_data_length,
3895 plaintext, ciphertext,
3896 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003897 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003898 else
3899#endif /* MBEDTLS_GCM_C */
3900#if defined(MBEDTLS_CCM_C)
3901 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003902 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003903 status = mbedtls_to_psa_error(
3904 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3905 plaintext_length,
3906 nonce, nonce_length,
3907 additional_data,
3908 additional_data_length,
3909 plaintext, ciphertext,
3910 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003911 }
mohammad16035c8845f2018-05-09 05:40:09 -07003912 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003913#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003914#if defined(MBEDTLS_CHACHAPOLY_C)
3915 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3916 {
3917 if( nonce_length != 12 || operation.tag_length != 16 )
3918 {
3919 status = PSA_ERROR_NOT_SUPPORTED;
3920 goto exit;
3921 }
3922 status = mbedtls_to_psa_error(
3923 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3924 plaintext_length,
3925 nonce,
3926 additional_data,
3927 additional_data_length,
3928 plaintext,
3929 ciphertext,
3930 tag ) );
3931 }
3932 else
3933#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003934 {
mohammad1603554faad2018-06-03 15:07:38 +03003935 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003936 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003937
Gilles Peskineedf9a652018-08-17 18:11:56 +02003938 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3939 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003940
Gilles Peskineedf9a652018-08-17 18:11:56 +02003941exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003942 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003943 if( status == PSA_SUCCESS )
3944 *ciphertext_length = plaintext_length + operation.tag_length;
3945 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003946}
3947
Gilles Peskineee652a32018-06-01 19:23:52 +02003948/* Locate the tag in a ciphertext buffer containing the encrypted data
3949 * followed by the tag. Return the length of the part preceding the tag in
3950 * *plaintext_length. This is the size of the plaintext in modes where
3951 * the encrypted data has the same size as the plaintext, such as
3952 * CCM and GCM. */
3953static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3954 const uint8_t *ciphertext,
3955 size_t ciphertext_length,
3956 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003957 const uint8_t **p_tag )
3958{
3959 size_t payload_length;
3960 if( tag_length > ciphertext_length )
3961 return( PSA_ERROR_INVALID_ARGUMENT );
3962 payload_length = ciphertext_length - tag_length;
3963 if( payload_length > plaintext_size )
3964 return( PSA_ERROR_BUFFER_TOO_SMALL );
3965 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003966 return( PSA_SUCCESS );
3967}
3968
Gilles Peskinec5487a82018-12-03 18:08:14 +01003969psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003970 psa_algorithm_t alg,
3971 const uint8_t *nonce,
3972 size_t nonce_length,
3973 const uint8_t *additional_data,
3974 size_t additional_data_length,
3975 const uint8_t *ciphertext,
3976 size_t ciphertext_length,
3977 uint8_t *plaintext,
3978 size_t plaintext_size,
3979 size_t *plaintext_length )
3980{
mohammad16035955c982018-04-26 00:53:03 +03003981 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003982 aead_operation_t operation;
3983 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003984
Gilles Peskineee652a32018-06-01 19:23:52 +02003985 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003986
Gilles Peskinec5487a82018-12-03 18:08:14 +01003987 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003988 if( status != PSA_SUCCESS )
3989 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003990
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003991 status = psa_aead_unpadded_locate_tag( operation.tag_length,
3992 ciphertext, ciphertext_length,
3993 plaintext_size, &tag );
3994 if( status != PSA_SUCCESS )
3995 goto exit;
3996
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003997#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003998 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003999 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004000 status = mbedtls_to_psa_error(
4001 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4002 ciphertext_length - operation.tag_length,
4003 nonce, nonce_length,
4004 additional_data,
4005 additional_data_length,
4006 tag, operation.tag_length,
4007 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004008 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004009 else
4010#endif /* MBEDTLS_GCM_C */
4011#if defined(MBEDTLS_CCM_C)
4012 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004013 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004014 status = mbedtls_to_psa_error(
4015 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4016 ciphertext_length - operation.tag_length,
4017 nonce, nonce_length,
4018 additional_data,
4019 additional_data_length,
4020 ciphertext, plaintext,
4021 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004022 }
mohammad160339574652018-06-01 04:39:53 -07004023 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004024#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004025#if defined(MBEDTLS_CHACHAPOLY_C)
4026 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4027 {
4028 if( nonce_length != 12 || operation.tag_length != 16 )
4029 {
4030 status = PSA_ERROR_NOT_SUPPORTED;
4031 goto exit;
4032 }
4033 status = mbedtls_to_psa_error(
4034 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4035 ciphertext_length - operation.tag_length,
4036 nonce,
4037 additional_data,
4038 additional_data_length,
4039 tag,
4040 ciphertext,
4041 plaintext ) );
4042 }
4043 else
4044#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004045 {
mohammad1603554faad2018-06-03 15:07:38 +03004046 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004047 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004048
Gilles Peskineedf9a652018-08-17 18:11:56 +02004049 if( status != PSA_SUCCESS && plaintext_size != 0 )
4050 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004051
Gilles Peskineedf9a652018-08-17 18:11:56 +02004052exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004053 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004054 if( status == PSA_SUCCESS )
4055 *plaintext_length = ciphertext_length - operation.tag_length;
4056 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004057}
4058
Gilles Peskinea0655c32018-04-30 17:06:50 +02004059
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004060
Gilles Peskine20035e32018-02-03 22:44:14 +01004061/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004062/* Generators */
4063/****************************************************************/
4064
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004065#define HKDF_STATE_INIT 0 /* no input yet */
4066#define HKDF_STATE_STARTED 1 /* got salt */
4067#define HKDF_STATE_KEYED 2 /* got key */
4068#define HKDF_STATE_OUTPUT 3 /* output started */
4069
Gilles Peskinecbe66502019-05-16 16:59:18 +02004070static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004071 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004072{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004073 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4074 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004075 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004076 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004077}
4078
4079
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004080psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004081{
4082 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004083 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004084 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004085 {
4086 /* The object has (apparently) been initialized but it is not
4087 * in use. It's ok to call abort on such an object, and there's
4088 * nothing to do. */
4089 }
4090 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004091#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004092 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004093 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004094 mbedtls_free( operation->ctx.hkdf.info );
4095 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004096 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004097 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004098 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004099 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004100 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004101#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004102 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004103 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004104 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4105 operation->ctx.tls12_prf.key_len );
4106 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004107 }
Hanno Becker580fba12018-11-13 20:50:45 +00004108
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004109 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004110 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004111 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4112 operation->ctx.tls12_prf.Ai_with_seed_len );
4113 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004114 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004115#else
4116 if( operation->ctx.tls12_prf.seed != NULL )
4117 {
4118 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4119 operation->ctx.tls12_prf.seed_length );
4120 mbedtls_free( operation->ctx.tls12_prf.seed );
4121 }
4122
4123 if( operation->ctx.tls12_prf.label != NULL )
4124 {
4125 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4126 operation->ctx.tls12_prf.label_length );
4127 mbedtls_free( operation->ctx.tls12_prf.label );
4128 }
4129
4130 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4131
4132 /* We leave the fields Ai and output_block to be erased safely by the
4133 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004134#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004135 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004136 else
4137#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004138 {
4139 status = PSA_ERROR_BAD_STATE;
4140 }
Janos Follath083036a2019-06-11 10:22:26 +01004141 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004142 return( status );
4143}
4144
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004145psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004146 size_t *capacity)
4147{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004148 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004149 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004150 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004151 return PSA_ERROR_BAD_STATE;
4152 }
4153
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004154 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004155 return( PSA_SUCCESS );
4156}
4157
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004158psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004159 size_t capacity )
4160{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004161 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004162 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004163 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004164 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004165 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004166 return( PSA_SUCCESS );
4167}
4168
Gilles Peskinebef7f142018-07-12 17:22:21 +02004169#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004170/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004171 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004172static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004173 psa_algorithm_t hash_alg,
4174 uint8_t *output,
4175 size_t output_length )
4176{
4177 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4178 psa_status_t status;
4179
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004180 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4181 return( PSA_ERROR_BAD_STATE );
4182 hkdf->state = HKDF_STATE_OUTPUT;
4183
Gilles Peskinebef7f142018-07-12 17:22:21 +02004184 while( output_length != 0 )
4185 {
4186 /* Copy what remains of the current block */
4187 uint8_t n = hash_length - hkdf->offset_in_block;
4188 if( n > output_length )
4189 n = (uint8_t) output_length;
4190 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4191 output += n;
4192 output_length -= n;
4193 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004194 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004195 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004196 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004197 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004198 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004199 * object was corrupted or if this function is called directly
4200 * inside the library. */
4201 if( hkdf->block_number == 0xff )
4202 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004203
4204 /* We need a new block */
4205 ++hkdf->block_number;
4206 hkdf->offset_in_block = 0;
4207 status = psa_hmac_setup_internal( &hkdf->hmac,
4208 hkdf->prk, hash_length,
4209 hash_alg );
4210 if( status != PSA_SUCCESS )
4211 return( status );
4212 if( hkdf->block_number != 1 )
4213 {
4214 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4215 hkdf->output_block,
4216 hash_length );
4217 if( status != PSA_SUCCESS )
4218 return( status );
4219 }
4220 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4221 hkdf->info,
4222 hkdf->info_length );
4223 if( status != PSA_SUCCESS )
4224 return( status );
4225 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4226 &hkdf->block_number, 1 );
4227 if( status != PSA_SUCCESS )
4228 return( status );
4229 status = psa_hmac_finish_internal( &hkdf->hmac,
4230 hkdf->output_block,
4231 sizeof( hkdf->output_block ) );
4232 if( status != PSA_SUCCESS )
4233 return( status );
4234 }
4235
4236 return( PSA_SUCCESS );
4237}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004238
Janos Follath999f6482019-06-11 12:04:10 +01004239#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004240static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4241 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004242 psa_algorithm_t alg )
4243{
4244 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4245 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4246 psa_hmac_internal_data hmac;
4247 psa_status_t status, cleanup_status;
4248
Hanno Becker3b339e22018-11-13 20:56:14 +00004249 unsigned char *Ai;
4250 size_t Ai_len;
4251
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004252 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004253 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004254 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004255 * object was corrupted or if this function is called directly
4256 * inside the library. */
4257 if( tls12_prf->block_number == 0xff )
4258 return( PSA_ERROR_BAD_STATE );
4259
4260 /* We need a new block */
4261 ++tls12_prf->block_number;
4262 tls12_prf->offset_in_block = 0;
4263
4264 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4265 *
4266 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4267 *
4268 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4269 * HMAC_hash(secret, A(2) + seed) +
4270 * HMAC_hash(secret, A(3) + seed) + ...
4271 *
4272 * A(0) = seed
4273 * A(i) = HMAC_hash( secret, A(i-1) )
4274 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004275 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004276 * `HMAC_hash(secret, A(i) + seed)` from which the output
4277 * is currently extracted as `output_block`, while
4278 * `A(i) + seed` is stored in `Ai_with_seed`.
4279 *
4280 * Generating a new block means recalculating `Ai_with_seed`
4281 * from the A(i)-part of it, and afterwards recalculating
4282 * `output_block`.
4283 *
4284 * A(0) is computed at setup time.
4285 *
4286 */
4287
4288 psa_hmac_init_internal( &hmac );
4289
4290 /* We must distinguish the calculation of A(1) from those
4291 * of A(2) and higher, because A(0)=seed has a different
4292 * length than the other A(i). */
4293 if( tls12_prf->block_number == 1 )
4294 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004295 Ai = tls12_prf->Ai_with_seed + hash_length;
4296 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004297 }
4298 else
4299 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004300 Ai = tls12_prf->Ai_with_seed;
4301 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004302 }
4303
Hanno Becker3b339e22018-11-13 20:56:14 +00004304 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4305 status = psa_hmac_setup_internal( &hmac,
4306 tls12_prf->key,
4307 tls12_prf->key_len,
4308 hash_alg );
4309 if( status != PSA_SUCCESS )
4310 goto cleanup;
4311
4312 status = psa_hash_update( &hmac.hash_ctx,
4313 Ai, Ai_len );
4314 if( status != PSA_SUCCESS )
4315 goto cleanup;
4316
4317 status = psa_hmac_finish_internal( &hmac,
4318 tls12_prf->Ai_with_seed,
4319 hash_length );
4320 if( status != PSA_SUCCESS )
4321 goto cleanup;
4322
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004323 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4324 status = psa_hmac_setup_internal( &hmac,
4325 tls12_prf->key,
4326 tls12_prf->key_len,
4327 hash_alg );
4328 if( status != PSA_SUCCESS )
4329 goto cleanup;
4330
4331 status = psa_hash_update( &hmac.hash_ctx,
4332 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004333 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004334 if( status != PSA_SUCCESS )
4335 goto cleanup;
4336
4337 status = psa_hmac_finish_internal( &hmac,
4338 tls12_prf->output_block,
4339 hash_length );
4340 if( status != PSA_SUCCESS )
4341 goto cleanup;
4342
4343cleanup:
4344
4345 cleanup_status = psa_hmac_abort_internal( &hmac );
4346 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4347 status = cleanup_status;
4348
4349 return( status );
4350}
Janos Follath7742fee2019-06-17 12:58:10 +01004351#else
4352static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4353 psa_tls12_prf_key_derivation_t *tls12_prf,
4354 psa_algorithm_t alg )
4355{
4356 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4357 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004358 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4359 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004360
Janos Follath7742fee2019-06-17 12:58:10 +01004361 /* We can't be wanting more output after block 0xff, otherwise
4362 * the capacity check in psa_key_derivation_output_bytes() would have
4363 * prevented this call. It could happen only if the operation
4364 * object was corrupted or if this function is called directly
4365 * inside the library. */
4366 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004367 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004368
4369 /* We need a new block */
4370 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004371 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004372
4373 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4374 *
4375 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4376 *
4377 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4378 * HMAC_hash(secret, A(2) + seed) +
4379 * HMAC_hash(secret, A(3) + seed) + ...
4380 *
4381 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004382 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004383 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004384 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004385 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004386 * is currently extracted as `output_block` and where i is
4387 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004388 */
4389
Janos Follathea29bfb2019-06-19 12:21:20 +01004390 /* Save the hash context before using it, to preserve the hash state with
4391 * only the inner padding in it. We need this, because inner padding depends
4392 * on the key (secret in the RFC's terminology). */
4393 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4394 if( status != PSA_SUCCESS )
4395 goto cleanup;
4396
4397 /* Calculate A(i) where i = tls12_prf->block_number. */
4398 if( tls12_prf->block_number == 1 )
4399 {
4400 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4401 * the variable seed and in this instance means it in the context of the
4402 * P_hash function, where seed = label + seed.) */
4403 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4404 tls12_prf->label, tls12_prf->label_length );
4405 if( status != PSA_SUCCESS )
4406 goto cleanup;
4407 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4408 tls12_prf->seed, tls12_prf->seed_length );
4409 if( status != PSA_SUCCESS )
4410 goto cleanup;
4411 }
4412 else
4413 {
4414 /* A(i) = HMAC_hash(secret, A(i-1)) */
4415 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4416 tls12_prf->Ai, hash_length );
4417 if( status != PSA_SUCCESS )
4418 goto cleanup;
4419 }
4420
4421 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4422 tls12_prf->Ai, hash_length );
4423 if( status != PSA_SUCCESS )
4424 goto cleanup;
4425 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4426 if( status != PSA_SUCCESS )
4427 goto cleanup;
4428
4429 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4430 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4431 tls12_prf->Ai, hash_length );
4432 if( status != PSA_SUCCESS )
4433 goto cleanup;
4434 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4435 tls12_prf->label, tls12_prf->label_length );
4436 if( status != PSA_SUCCESS )
4437 goto cleanup;
4438 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4439 tls12_prf->seed, tls12_prf->seed_length );
4440 if( status != PSA_SUCCESS )
4441 goto cleanup;
4442 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4443 tls12_prf->output_block, hash_length );
4444 if( status != PSA_SUCCESS )
4445 goto cleanup;
4446 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4447 if( status != PSA_SUCCESS )
4448 goto cleanup;
4449
Janos Follath7742fee2019-06-17 12:58:10 +01004450
4451cleanup:
4452
Janos Follathea29bfb2019-06-19 12:21:20 +01004453 cleanup_status = psa_hash_abort( &backup );
4454 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4455 status = cleanup_status;
4456
4457 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004458}
Janos Follath999f6482019-06-11 12:04:10 +01004459#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004460
Janos Follath999f6482019-06-11 12:04:10 +01004461#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004462/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004463 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004464static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004465 psa_tls12_prf_key_derivation_t *tls12_prf,
4466 psa_algorithm_t alg,
4467 uint8_t *output,
4468 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004469{
4470 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4471 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4472 psa_status_t status;
4473
4474 while( output_length != 0 )
4475 {
4476 /* Copy what remains of the current block */
4477 uint8_t n = hash_length - tls12_prf->offset_in_block;
4478
4479 /* Check if we have fully processed the current block. */
4480 if( n == 0 )
4481 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004482 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004483 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004484 if( status != PSA_SUCCESS )
4485 return( status );
4486
4487 continue;
4488 }
4489
4490 if( n > output_length )
4491 n = (uint8_t) output_length;
4492 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4493 n );
4494 output += n;
4495 output_length -= n;
4496 tls12_prf->offset_in_block += n;
4497 }
4498
4499 return( PSA_SUCCESS );
4500}
Janos Follath844eb0e2019-06-19 12:10:49 +01004501#else
4502static psa_status_t psa_key_derivation_tls12_prf_read(
4503 psa_tls12_prf_key_derivation_t *tls12_prf,
4504 psa_algorithm_t alg,
4505 uint8_t *output,
4506 size_t output_length )
4507{
4508 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4509 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4510 psa_status_t status;
4511 uint8_t offset, length;
4512
4513 while( output_length != 0 )
4514 {
4515 /* Check if we have fully processed the current block. */
4516 if( tls12_prf->left_in_block == 0 )
4517 {
4518 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4519 alg );
4520 if( status != PSA_SUCCESS )
4521 return( status );
4522
4523 continue;
4524 }
4525
4526 if( tls12_prf->left_in_block > output_length )
4527 length = (uint8_t) output_length;
4528 else
4529 length = tls12_prf->left_in_block;
4530
4531 offset = hash_length - tls12_prf->left_in_block;
4532 memcpy( output, tls12_prf->output_block + offset, length );
4533 output += length;
4534 output_length -= length;
4535 tls12_prf->left_in_block -= length;
4536 }
4537
4538 return( PSA_SUCCESS );
4539}
Janos Follath999f6482019-06-11 12:04:10 +01004540#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004541#endif /* MBEDTLS_MD_C */
4542
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004543psa_status_t psa_key_derivation_output_bytes(
4544 psa_key_derivation_operation_t *operation,
4545 uint8_t *output,
4546 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004547{
4548 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004549 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004550
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004551 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004552 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004553 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004554 return PSA_ERROR_BAD_STATE;
4555 }
4556
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004557 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004558 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004559 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004560 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004561 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004562 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004563 goto exit;
4564 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004565 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004566 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004567 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004568 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004569 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4570 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004571 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004572 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004573 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004574 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004575 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004576
Gilles Peskinebef7f142018-07-12 17:22:21 +02004577#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004578 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004579 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004580 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004581 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004582 output, output_length );
4583 }
Janos Follathadbec812019-06-14 11:05:39 +01004584 else
Janos Follathadbec812019-06-14 11:05:39 +01004585 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004586 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004587 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004588 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004589 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004590 output_length );
4591 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004592 else
4593#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004594 {
4595 return( PSA_ERROR_BAD_STATE );
4596 }
4597
4598exit:
4599 if( status != PSA_SUCCESS )
4600 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004601 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004602 * This allows us to differentiate between exhausted operations and
4603 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4604 * operations. */
4605 psa_algorithm_t alg = operation->alg;
4606 psa_key_derivation_abort( operation );
4607 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004608 memset( output, '!', output_length );
4609 }
4610 return( status );
4611}
4612
Gilles Peskine08542d82018-07-19 17:05:42 +02004613#if defined(MBEDTLS_DES_C)
4614static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4615{
4616 if( data_size >= 8 )
4617 mbedtls_des_key_set_parity( data );
4618 if( data_size >= 16 )
4619 mbedtls_des_key_set_parity( data + 8 );
4620 if( data_size >= 24 )
4621 mbedtls_des_key_set_parity( data + 16 );
4622}
4623#endif /* MBEDTLS_DES_C */
4624
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004625static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004626 psa_key_slot_t *slot,
4627 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004628 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004629{
4630 uint8_t *data = NULL;
4631 size_t bytes = PSA_BITS_TO_BYTES( bits );
4632 psa_status_t status;
4633
4634 if( ! key_type_is_raw_bytes( slot->type ) )
4635 return( PSA_ERROR_INVALID_ARGUMENT );
4636 if( bits % 8 != 0 )
4637 return( PSA_ERROR_INVALID_ARGUMENT );
4638 data = mbedtls_calloc( 1, bytes );
4639 if( data == NULL )
4640 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4641
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004642 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004643 if( status != PSA_SUCCESS )
4644 goto exit;
4645#if defined(MBEDTLS_DES_C)
4646 if( slot->type == PSA_KEY_TYPE_DES )
4647 psa_des_set_key_parity( data, bytes );
4648#endif /* MBEDTLS_DES_C */
4649 status = psa_import_key_into_slot( slot, data, bytes );
4650
4651exit:
4652 mbedtls_free( data );
4653 return( status );
4654}
4655
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004656psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004657 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004658 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004659{
4660 psa_status_t status;
4661 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004662 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004663 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004664 if( status == PSA_SUCCESS )
4665 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004666 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004667 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004668 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004669 }
4670 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004671 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004672 if( status != PSA_SUCCESS )
4673 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004674 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004675 *handle = 0;
4676 }
4677 return( status );
4678}
4679
Gilles Peskineeab56e42018-07-12 17:12:33 +02004680
4681
4682/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004683/* Key derivation */
4684/****************************************************************/
4685
Gilles Peskinea05219c2018-11-16 16:02:56 +01004686#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004687#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004688/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004689 * of the HKDF algorithm.
4690 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004691 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004692 * to potentially free embedded data structures and wipe confidential data.
4693 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004694static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004695 const uint8_t *secret,
4696 size_t secret_length,
4697 psa_algorithm_t hash_alg,
4698 const uint8_t *salt,
4699 size_t salt_length,
4700 const uint8_t *label,
4701 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004702{
4703 psa_status_t status;
4704 status = psa_hmac_setup_internal( &hkdf->hmac,
4705 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004706 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004707 if( status != PSA_SUCCESS )
4708 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004709 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004710 if( status != PSA_SUCCESS )
4711 return( status );
4712 status = psa_hmac_finish_internal( &hkdf->hmac,
4713 hkdf->prk,
4714 sizeof( hkdf->prk ) );
4715 if( status != PSA_SUCCESS )
4716 return( status );
4717 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4718 hkdf->block_number = 0;
4719 hkdf->info_length = label_length;
4720 if( label_length != 0 )
4721 {
4722 hkdf->info = mbedtls_calloc( 1, label_length );
4723 if( hkdf->info == NULL )
4724 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4725 memcpy( hkdf->info, label, label_length );
4726 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004727 hkdf->state = HKDF_STATE_KEYED;
4728 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004729 return( PSA_SUCCESS );
4730}
Janos Follath71a4c912019-06-11 09:14:47 +01004731#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004732#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004733
Gilles Peskinea05219c2018-11-16 16:02:56 +01004734#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004735#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004737 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004738 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004739 * to potentially free embedded data structures and wipe confidential data.
4740 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004741static psa_status_t psa_key_derivation_tls12_prf_setup(
4742 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004743 const unsigned char *key,
4744 size_t key_len,
4745 psa_algorithm_t hash_alg,
4746 const uint8_t *salt,
4747 size_t salt_length,
4748 const uint8_t *label,
4749 size_t label_length )
4750{
4751 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004752 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4753 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004754
4755 tls12_prf->key = mbedtls_calloc( 1, key_len );
4756 if( tls12_prf->key == NULL )
4757 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4758 tls12_prf->key_len = key_len;
4759 memcpy( tls12_prf->key, key, key_len );
4760
Hanno Becker580fba12018-11-13 20:50:45 +00004761 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004762 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004763 if( overflow )
4764 return( PSA_ERROR_INVALID_ARGUMENT );
4765
4766 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4767 if( tls12_prf->Ai_with_seed == NULL )
4768 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4769 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4770
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004771 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4772 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004773 if( label_length != 0 )
4774 {
4775 memcpy( tls12_prf->Ai_with_seed + hash_length,
4776 label, label_length );
4777 }
4778
4779 if( salt_length != 0 )
4780 {
4781 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4782 salt, salt_length );
4783 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004784
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004785 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004786 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004787 tls12_prf->block_number = 0;
4788 tls12_prf->offset_in_block = hash_length;
4789
4790 return( PSA_SUCCESS );
4791}
Janos Follath71a4c912019-06-11 09:14:47 +01004792#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004793
Janos Follath71a4c912019-06-11 09:14:47 +01004794#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004795/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004796static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4797 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004798 const unsigned char *psk,
4799 size_t psk_len,
4800 psa_algorithm_t hash_alg,
4801 const uint8_t *salt,
4802 size_t salt_length,
4803 const uint8_t *label,
4804 size_t label_length )
4805{
4806 psa_status_t status;
4807 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4808
4809 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4810 return( PSA_ERROR_INVALID_ARGUMENT );
4811
4812 /* Quoting RFC 4279, Section 2:
4813 *
4814 * The premaster secret is formed as follows: if the PSK is N octets
4815 * long, concatenate a uint16 with the value N, N zero octets, a second
4816 * uint16 with the value N, and the PSK itself.
4817 */
4818
4819 pms[0] = ( psk_len >> 8 ) & 0xff;
4820 pms[1] = ( psk_len >> 0 ) & 0xff;
4821 memset( pms + 2, 0, psk_len );
4822 pms[2 + psk_len + 0] = pms[0];
4823 pms[2 + psk_len + 1] = pms[1];
4824 memcpy( pms + 4 + psk_len, psk, psk_len );
4825
Gilles Peskinecbe66502019-05-16 16:59:18 +02004826 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004827 pms, 4 + 2 * psk_len,
4828 hash_alg,
4829 salt, salt_length,
4830 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004831
Gilles Peskine3f108122018-12-07 18:14:53 +01004832 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004833 return( status );
4834}
Janos Follath71a4c912019-06-11 09:14:47 +01004835#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004836#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004837
Janos Follath71a4c912019-06-11 09:14:47 +01004838#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004839/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004840 * to potentially free embedded data structures and wipe confidential data.
4841 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004842static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004843 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004844 const uint8_t *secret, size_t secret_length,
4845 psa_algorithm_t alg,
4846 const uint8_t *salt, size_t salt_length,
4847 const uint8_t *label, size_t label_length,
4848 size_t capacity )
4849{
4850 psa_status_t status;
4851 size_t max_capacity;
4852
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004853 /* Set operation->alg even on failure so that abort knows what to do. */
4854 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004855
4856#if defined(MBEDTLS_MD_C)
4857 if( PSA_ALG_IS_HKDF( alg ) )
4858 {
4859 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4860 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4861 if( hash_size == 0 )
4862 return( PSA_ERROR_NOT_SUPPORTED );
4863 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004864 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004865 secret, secret_length,
4866 hash_alg,
4867 salt, salt_length,
4868 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004869 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004870 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4871 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4872 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004873 {
4874 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4875 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4876
4877 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4878 if( hash_alg != PSA_ALG_SHA_256 &&
4879 hash_alg != PSA_ALG_SHA_384 )
4880 {
4881 return( PSA_ERROR_NOT_SUPPORTED );
4882 }
4883
4884 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004885
4886 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4887 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004888 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004889 secret, secret_length,
4890 hash_alg, salt, salt_length,
4891 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004892 }
4893 else
4894 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004895 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004896 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004897 secret, secret_length,
4898 hash_alg, salt, salt_length,
4899 label, label_length );
4900 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004901 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004902 else
4903#endif
4904 {
4905 return( PSA_ERROR_NOT_SUPPORTED );
4906 }
4907
4908 if( status != PSA_SUCCESS )
4909 return( status );
4910
4911 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004912 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004913 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004914 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004915 else
4916 return( PSA_ERROR_INVALID_ARGUMENT );
4917
4918 return( PSA_SUCCESS );
4919}
Janos Follath71a4c912019-06-11 09:14:47 +01004920#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004921
Janos Follath71a4c912019-06-11 09:14:47 +01004922#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004923psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004924 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004925 psa_algorithm_t alg,
4926 const uint8_t *salt,
4927 size_t salt_length,
4928 const uint8_t *label,
4929 size_t label_length,
4930 size_t capacity )
4931{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004932 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004933 psa_status_t status;
4934
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004935 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004936 return( PSA_ERROR_BAD_STATE );
4937
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004938 /* Make sure that alg is a key derivation algorithm. This prevents
4939 * key selection algorithms, which psa_key_derivation_internal
4940 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004941 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4942 return( PSA_ERROR_INVALID_ARGUMENT );
4943
Gilles Peskine28f8f302019-07-24 13:30:31 +02004944 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004945 if( status != PSA_SUCCESS )
4946 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004947
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004948 if( slot->type != PSA_KEY_TYPE_DERIVE )
4949 return( PSA_ERROR_INVALID_ARGUMENT );
4950
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004951 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004952 slot->data.raw.data,
4953 slot->data.raw.bytes,
4954 alg,
4955 salt, salt_length,
4956 label, label_length,
4957 capacity );
4958 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004959 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004960 return( status );
4961}
Janos Follath71a4c912019-06-11 09:14:47 +01004962#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004963
Gilles Peskine969c5d62019-01-16 15:53:06 +01004964static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004965 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004966 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004967{
Janos Follath5fe19732019-06-20 15:09:30 +01004968 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4969 * initialisers for this union leave some bytes unspecified.) */
4970 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4971
Gilles Peskine969c5d62019-01-16 15:53:06 +01004972 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004973#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004974 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4975 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4976 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004977 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004978 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004979 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4980 if( hash_size == 0 )
4981 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004982 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4983 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004984 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004985 {
4986 return( PSA_ERROR_NOT_SUPPORTED );
4987 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004988 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004989 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004990 }
4991#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004992 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004993 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004994}
4995
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004996psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004997 psa_algorithm_t alg )
4998{
4999 psa_status_t status;
5000
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005001 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005002 return( PSA_ERROR_BAD_STATE );
5003
Gilles Peskine6843c292019-01-18 16:44:49 +01005004 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5005 return( PSA_ERROR_INVALID_ARGUMENT );
5006 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005007 {
5008 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005009 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005010 }
5011 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5012 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005013 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005014 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005015 else
5016 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005017
5018 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005019 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005020 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005021}
5022
5023#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005024static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005025 psa_algorithm_t hash_alg,
5026 psa_key_derivation_step_t step,
5027 const uint8_t *data,
5028 size_t data_length )
5029{
5030 psa_status_t status;
5031 switch( step )
5032 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005033 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005034 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005035 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005036 status = psa_hmac_setup_internal( &hkdf->hmac,
5037 data, data_length,
5038 hash_alg );
5039 if( status != PSA_SUCCESS )
5040 return( status );
5041 hkdf->state = HKDF_STATE_STARTED;
5042 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005043 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005044 /* If no salt was provided, use an empty salt. */
5045 if( hkdf->state == HKDF_STATE_INIT )
5046 {
5047 status = psa_hmac_setup_internal( &hkdf->hmac,
5048 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005049 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005050 if( status != PSA_SUCCESS )
5051 return( status );
5052 hkdf->state = HKDF_STATE_STARTED;
5053 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005054 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005055 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005056 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5057 data, data_length );
5058 if( status != PSA_SUCCESS )
5059 return( status );
5060 status = psa_hmac_finish_internal( &hkdf->hmac,
5061 hkdf->prk,
5062 sizeof( hkdf->prk ) );
5063 if( status != PSA_SUCCESS )
5064 return( status );
5065 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5066 hkdf->block_number = 0;
5067 hkdf->state = HKDF_STATE_KEYED;
5068 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005069 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005070 if( hkdf->state == HKDF_STATE_OUTPUT )
5071 return( PSA_ERROR_BAD_STATE );
5072 if( hkdf->info_set )
5073 return( PSA_ERROR_BAD_STATE );
5074 hkdf->info_length = data_length;
5075 if( data_length != 0 )
5076 {
5077 hkdf->info = mbedtls_calloc( 1, data_length );
5078 if( hkdf->info == NULL )
5079 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5080 memcpy( hkdf->info, data, data_length );
5081 }
5082 hkdf->info_set = 1;
5083 return( PSA_SUCCESS );
5084 default:
5085 return( PSA_ERROR_INVALID_ARGUMENT );
5086 }
5087}
Janos Follathb03233e2019-06-11 15:30:30 +01005088
5089#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5090static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5091 psa_algorithm_t hash_alg,
5092 psa_key_derivation_step_t step,
5093 const uint8_t *data,
5094 size_t data_length )
5095{
5096 (void) prf;
5097 (void) hash_alg;
5098 (void) step;
5099 (void) data;
5100 (void) data_length;
5101
5102 return( PSA_ERROR_INVALID_ARGUMENT );
5103}
Janos Follath6660f0e2019-06-17 08:44:03 +01005104
5105static psa_status_t psa_tls12_prf_psk_to_ms_input(
5106 psa_tls12_prf_key_derivation_t *prf,
5107 psa_algorithm_t hash_alg,
5108 psa_key_derivation_step_t step,
5109 const uint8_t *data,
5110 size_t data_length )
5111{
5112 (void) prf;
5113 (void) hash_alg;
5114 (void) step;
5115 (void) data;
5116 (void) data_length;
5117
5118 return( PSA_ERROR_INVALID_ARGUMENT );
5119}
Janos Follathb03233e2019-06-11 15:30:30 +01005120#else
Janos Follathf08e2652019-06-13 09:05:41 +01005121static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5122 const uint8_t *data,
5123 size_t data_length )
5124{
5125 if( prf->state != TLS12_PRF_STATE_INIT )
5126 return( PSA_ERROR_BAD_STATE );
5127
Janos Follathd6dce9f2019-07-04 09:11:38 +01005128 if( data_length != 0 )
5129 {
5130 prf->seed = mbedtls_calloc( 1, data_length );
5131 if( prf->seed == NULL )
5132 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005133
Janos Follathd6dce9f2019-07-04 09:11:38 +01005134 memcpy( prf->seed, data, data_length );
5135 prf->seed_length = data_length;
5136 }
Janos Follathf08e2652019-06-13 09:05:41 +01005137
5138 prf->state = TLS12_PRF_STATE_SEED_SET;
5139
5140 return( PSA_SUCCESS );
5141}
5142
Janos Follath81550542019-06-13 14:26:34 +01005143static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5144 psa_algorithm_t hash_alg,
5145 const uint8_t *data,
5146 size_t data_length )
5147{
5148 psa_status_t status;
5149 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5150 return( PSA_ERROR_BAD_STATE );
5151
5152 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5153 if( status != PSA_SUCCESS )
5154 return( status );
5155
5156 prf->state = TLS12_PRF_STATE_KEY_SET;
5157
5158 return( PSA_SUCCESS );
5159}
5160
Janos Follath6660f0e2019-06-17 08:44:03 +01005161static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5162 psa_tls12_prf_key_derivation_t *prf,
5163 psa_algorithm_t hash_alg,
5164 const uint8_t *data,
5165 size_t data_length )
5166{
5167 psa_status_t status;
5168 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Janos Follath40e13932019-06-26 13:22:29 +01005169 unsigned char* cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005170
5171 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5172 return( PSA_ERROR_INVALID_ARGUMENT );
5173
5174 /* Quoting RFC 4279, Section 2:
5175 *
5176 * The premaster secret is formed as follows: if the PSK is N octets
5177 * long, concatenate a uint16 with the value N, N zero octets, a second
5178 * uint16 with the value N, and the PSK itself.
5179 */
5180
Janos Follath40e13932019-06-26 13:22:29 +01005181 *cur++ = ( data_length >> 8 ) & 0xff;
5182 *cur++ = ( data_length >> 0 ) & 0xff;
5183 memset( cur, 0, data_length );
5184 cur += data_length;
5185 *cur++ = pms[0];
5186 *cur++ = pms[1];
5187 memcpy( cur, data, data_length );
5188 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005189
Janos Follath40e13932019-06-26 13:22:29 +01005190 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005191
5192 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5193 return( status );
5194}
5195
Janos Follath63028dd2019-06-13 09:15:47 +01005196static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5197 const uint8_t *data,
5198 size_t data_length )
5199{
5200 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5201 return( PSA_ERROR_BAD_STATE );
5202
Janos Follathd6dce9f2019-07-04 09:11:38 +01005203 if( data_length != 0 )
5204 {
5205 prf->label = mbedtls_calloc( 1, data_length );
5206 if( prf->label == NULL )
5207 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005208
Janos Follathd6dce9f2019-07-04 09:11:38 +01005209 memcpy( prf->label, data, data_length );
5210 prf->label_length = data_length;
5211 }
Janos Follath63028dd2019-06-13 09:15:47 +01005212
5213 prf->state = TLS12_PRF_STATE_LABEL_SET;
5214
5215 return( PSA_SUCCESS );
5216}
5217
Janos Follathb03233e2019-06-11 15:30:30 +01005218static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5219 psa_algorithm_t hash_alg,
5220 psa_key_derivation_step_t step,
5221 const uint8_t *data,
5222 size_t data_length )
5223{
Janos Follathb03233e2019-06-11 15:30:30 +01005224 switch( step )
5225 {
Janos Follathf08e2652019-06-13 09:05:41 +01005226 case PSA_KEY_DERIVATION_INPUT_SEED:
5227 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005228 case PSA_KEY_DERIVATION_INPUT_SECRET:
5229 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005230 case PSA_KEY_DERIVATION_INPUT_LABEL:
5231 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005232 default:
5233 return( PSA_ERROR_INVALID_ARGUMENT );
5234 }
5235}
Janos Follath6660f0e2019-06-17 08:44:03 +01005236
5237static psa_status_t psa_tls12_prf_psk_to_ms_input(
5238 psa_tls12_prf_key_derivation_t *prf,
5239 psa_algorithm_t hash_alg,
5240 psa_key_derivation_step_t step,
5241 const uint8_t *data,
5242 size_t data_length )
5243{
5244 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005245 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005246 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5247 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005248 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005249
5250 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5251}
Janos Follathb03233e2019-06-11 15:30:30 +01005252#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005253#endif /* MBEDTLS_MD_C */
5254
Janos Follathb80a94e2019-06-12 15:54:46 +01005255static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005256 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005257 psa_key_derivation_step_t step,
5258 const uint8_t *data,
5259 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005260{
5261 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005262 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005263
5264#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005265 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005266 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005268 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005269 step, data, data_length );
5270 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005271 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005272#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005273#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005274 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005275 {
Janos Follathb03233e2019-06-11 15:30:30 +01005276 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5277 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5278 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005279 }
5280 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5281 {
5282 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5283 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5284 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005285 }
5286 else
5287#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005288 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005289 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005290 return( PSA_ERROR_BAD_STATE );
5291 }
5292
5293 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005294 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005295 return( status );
5296}
5297
Janos Follath51f4a0f2019-06-14 11:35:55 +01005298psa_status_t psa_key_derivation_input_bytes(
5299 psa_key_derivation_operation_t *operation,
5300 psa_key_derivation_step_t step,
5301 const uint8_t *data,
5302 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005303{
Janos Follathc5621512019-06-14 11:27:57 +01005304 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5305 return( PSA_ERROR_INVALID_ARGUMENT );
5306
5307 return( psa_key_derivation_input_internal( operation, step,
5308 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005309}
5310
Janos Follath51f4a0f2019-06-14 11:35:55 +01005311psa_status_t psa_key_derivation_input_key(
5312 psa_key_derivation_operation_t *operation,
5313 psa_key_derivation_step_t step,
5314 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005315{
5316 psa_key_slot_t *slot;
5317 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005318 status = psa_get_transparent_key( handle, &slot,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005319 PSA_KEY_USAGE_DERIVE,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005320 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005321 if( status != PSA_SUCCESS )
5322 return( status );
5323 if( slot->type != PSA_KEY_TYPE_DERIVE )
5324 return( PSA_ERROR_INVALID_ARGUMENT );
5325 /* Don't allow a key to be used as an input that is usually public.
5326 * This is debatable. It's ok from a cryptographic perspective to
5327 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005328 * the material should be dedicated to a particular input step,
5329 * otherwise this may allow the key to be used in an unintended way
5330 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005331 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005332 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005333 return( psa_key_derivation_input_internal( operation,
5334 step,
5335 slot->data.raw.data,
5336 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005337}
5338
Gilles Peskineea0fb492018-07-12 17:17:20 +02005339
5340
5341/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005342/* Key agreement */
5343/****************************************************************/
5344
Gilles Peskinea05219c2018-11-16 16:02:56 +01005345#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005346static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5347 size_t peer_key_length,
5348 const mbedtls_ecp_keypair *our_key,
5349 uint8_t *shared_secret,
5350 size_t shared_secret_size,
5351 size_t *shared_secret_length )
5352{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005353 mbedtls_ecp_keypair *their_key = NULL;
5354 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005355 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005356 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005357
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005358 status = psa_import_ec_public_key(
5359 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5360 peer_key, peer_key_length,
5361 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005362 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005363 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005364
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005365 status = mbedtls_to_psa_error(
5366 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5367 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005368 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005369 status = mbedtls_to_psa_error(
5370 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5371 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005372 goto exit;
5373
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005374 status = mbedtls_to_psa_error(
5375 mbedtls_ecdh_calc_secret( &ecdh,
5376 shared_secret_length,
5377 shared_secret, shared_secret_size,
5378 mbedtls_ctr_drbg_random,
5379 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005380
5381exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005382 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005383 mbedtls_ecp_keypair_free( their_key );
5384 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005385 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005386}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005387#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005388
Gilles Peskine01d718c2018-09-18 12:01:02 +02005389#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5390
Gilles Peskine0216fe12019-04-11 21:23:21 +02005391static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5392 psa_key_slot_t *private_key,
5393 const uint8_t *peer_key,
5394 size_t peer_key_length,
5395 uint8_t *shared_secret,
5396 size_t shared_secret_size,
5397 size_t *shared_secret_length )
5398{
5399 switch( alg )
5400 {
5401#if defined(MBEDTLS_ECDH_C)
5402 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005403 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005404 return( PSA_ERROR_INVALID_ARGUMENT );
5405 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5406 private_key->data.ecp,
5407 shared_secret, shared_secret_size,
5408 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005409#endif /* MBEDTLS_ECDH_C */
5410 default:
5411 (void) private_key;
5412 (void) peer_key;
5413 (void) peer_key_length;
5414 (void) shared_secret;
5415 (void) shared_secret_size;
5416 (void) shared_secret_length;
5417 return( PSA_ERROR_NOT_SUPPORTED );
5418 }
5419}
5420
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005421/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005422 * to potentially free embedded data structures and wipe confidential data.
5423 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005424static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005425 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005426 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005427 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005428 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005429{
5430 psa_status_t status;
5431 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5432 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005433 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005434
5435 /* Step 1: run the secret agreement algorithm to generate the shared
5436 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005437 status = psa_key_agreement_raw_internal( ka_alg,
5438 private_key,
5439 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005440 shared_secret,
5441 sizeof( shared_secret ),
5442 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005443 if( status != PSA_SUCCESS )
5444 goto exit;
5445
5446 /* Step 2: set up the key derivation to generate key material from
5447 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005448 status = psa_key_derivation_input_internal( operation, step,
5449 shared_secret,
5450 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005451
Gilles Peskine01d718c2018-09-18 12:01:02 +02005452exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005453 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005454 return( status );
5455}
5456
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005457psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005458 psa_key_derivation_step_t step,
5459 psa_key_handle_t private_key,
5460 const uint8_t *peer_key,
5461 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005462{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005463 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005464 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005465 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005466 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005467 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005468 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005469 if( status != PSA_SUCCESS )
5470 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005471 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005472 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005473 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005474 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005475 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005476 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005477}
5478
Gilles Peskinebe697d82019-05-16 18:00:41 +02005479psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5480 psa_key_handle_t private_key,
5481 const uint8_t *peer_key,
5482 size_t peer_key_length,
5483 uint8_t *output,
5484 size_t output_size,
5485 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005486{
5487 psa_key_slot_t *slot;
5488 psa_status_t status;
5489
5490 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5491 {
5492 status = PSA_ERROR_INVALID_ARGUMENT;
5493 goto exit;
5494 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005495 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskine0216fe12019-04-11 21:23:21 +02005496 PSA_KEY_USAGE_DERIVE, alg );
5497 if( status != PSA_SUCCESS )
5498 goto exit;
5499
5500 status = psa_key_agreement_raw_internal( alg, slot,
5501 peer_key, peer_key_length,
5502 output, output_size,
5503 output_length );
5504
5505exit:
5506 if( status != PSA_SUCCESS )
5507 {
5508 /* If an error happens and is not handled properly, the output
5509 * may be used as a key to protect sensitive data. Arrange for such
5510 * a key to be random, which is likely to result in decryption or
5511 * verification errors. This is better than filling the buffer with
5512 * some constant data such as zeros, which would result in the data
5513 * being protected with a reproducible, easily knowable key.
5514 */
5515 psa_generate_random( output, output_size );
5516 *output_length = output_size;
5517 }
5518 return( status );
5519}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005520
5521
5522/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005523/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005524/****************************************************************/
5525
5526psa_status_t psa_generate_random( uint8_t *output,
5527 size_t output_size )
5528{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005529 int ret;
5530 GUARD_MODULE_INITIALIZED;
5531
5532 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005533 return( mbedtls_to_psa_error( ret ) );
5534}
5535
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005536#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5537#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005538
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005539psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5540 size_t seed_size )
5541{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005542 if( global_data.initialized )
5543 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005544
5545 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5546 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5547 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5548 return( PSA_ERROR_INVALID_ARGUMENT );
5549
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005550 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005551}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005552#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005553
Gilles Peskinee56e8782019-04-26 17:34:02 +02005554#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5555static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5556 size_t domain_parameters_size,
5557 int *exponent )
5558{
5559 size_t i;
5560 uint32_t acc = 0;
5561
5562 if( domain_parameters_size == 0 )
5563 {
5564 *exponent = 65537;
5565 return( PSA_SUCCESS );
5566 }
5567
5568 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5569 * support values that fit in a 32-bit integer, which is larger than
5570 * int on just about every platform anyway. */
5571 if( domain_parameters_size > sizeof( acc ) )
5572 return( PSA_ERROR_NOT_SUPPORTED );
5573 for( i = 0; i < domain_parameters_size; i++ )
5574 acc = ( acc << 8 ) | domain_parameters[i];
5575 if( acc > INT_MAX )
5576 return( PSA_ERROR_NOT_SUPPORTED );
5577 *exponent = acc;
5578 return( PSA_SUCCESS );
5579}
5580#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5581
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005582static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005583 psa_key_slot_t *slot, size_t bits,
5584 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005585{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005586 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005587
Gilles Peskinee56e8782019-04-26 17:34:02 +02005588 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005589 return( PSA_ERROR_INVALID_ARGUMENT );
5590
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005591 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005592 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005593 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005594 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005595 if( status != PSA_SUCCESS )
5596 return( status );
5597 status = psa_generate_random( slot->data.raw.data,
5598 slot->data.raw.bytes );
5599 if( status != PSA_SUCCESS )
5600 {
5601 mbedtls_free( slot->data.raw.data );
5602 return( status );
5603 }
5604#if defined(MBEDTLS_DES_C)
5605 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005606 psa_des_set_key_parity( slot->data.raw.data,
5607 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005608#endif /* MBEDTLS_DES_C */
5609 }
5610 else
5611
5612#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005613 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005614 {
5615 mbedtls_rsa_context *rsa;
5616 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005617 int exponent;
5618 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005619 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5620 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005621 /* Accept only byte-aligned keys, for the same reasons as
5622 * in psa_import_rsa_key(). */
5623 if( bits % 8 != 0 )
5624 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005625 status = psa_read_rsa_exponent( domain_parameters,
5626 domain_parameters_size,
5627 &exponent );
5628 if( status != PSA_SUCCESS )
5629 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005630 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5631 if( rsa == NULL )
5632 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5633 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5634 ret = mbedtls_rsa_gen_key( rsa,
5635 mbedtls_ctr_drbg_random,
5636 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005637 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005638 exponent );
5639 if( ret != 0 )
5640 {
5641 mbedtls_rsa_free( rsa );
5642 mbedtls_free( rsa );
5643 return( mbedtls_to_psa_error( ret ) );
5644 }
5645 slot->data.rsa = rsa;
5646 }
5647 else
5648#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5649
5650#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005651 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005652 {
5653 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5654 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5655 const mbedtls_ecp_curve_info *curve_info =
5656 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5657 mbedtls_ecp_keypair *ecp;
5658 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005659 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005660 return( PSA_ERROR_NOT_SUPPORTED );
5661 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5662 return( PSA_ERROR_NOT_SUPPORTED );
5663 if( curve_info->bit_size != bits )
5664 return( PSA_ERROR_INVALID_ARGUMENT );
5665 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5666 if( ecp == NULL )
5667 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5668 mbedtls_ecp_keypair_init( ecp );
5669 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5670 mbedtls_ctr_drbg_random,
5671 &global_data.ctr_drbg );
5672 if( ret != 0 )
5673 {
5674 mbedtls_ecp_keypair_free( ecp );
5675 mbedtls_free( ecp );
5676 return( mbedtls_to_psa_error( ret ) );
5677 }
5678 slot->data.ecp = ecp;
5679 }
5680 else
5681#endif /* MBEDTLS_ECP_C */
5682
5683 return( PSA_ERROR_NOT_SUPPORTED );
5684
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005685 return( PSA_SUCCESS );
5686}
5687
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005688psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005689 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005690{
5691 psa_status_t status;
5692 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005693 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005694 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005695 if( status == PSA_SUCCESS )
5696 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005697 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005698 slot, attributes->bits,
5699 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005700 }
5701 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005702 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005703 if( status != PSA_SUCCESS )
5704 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005705 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005706 *handle = 0;
5707 }
5708 return( status );
5709}
5710
5711
Gilles Peskine05d69892018-06-19 22:00:52 +02005712
5713/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005714/* Module setup */
5715/****************************************************************/
5716
Gilles Peskine5e769522018-11-20 21:59:56 +01005717psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5718 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5719 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5720{
5721 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5722 return( PSA_ERROR_BAD_STATE );
5723 global_data.entropy_init = entropy_init;
5724 global_data.entropy_free = entropy_free;
5725 return( PSA_SUCCESS );
5726}
5727
Gilles Peskinee59236f2018-01-27 23:32:46 +01005728void mbedtls_psa_crypto_free( void )
5729{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005730 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005731 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5732 {
5733 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005734 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005735 }
5736 /* Wipe all remaining data, including configuration.
5737 * In particular, this sets all state indicator to the value
5738 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005739 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005740#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005741 /* Unregister all secure element drivers, so that we restart from
5742 * a pristine state. */
5743 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005744#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005745}
5746
5747psa_status_t psa_crypto_init( void )
5748{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005749 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005750 const unsigned char drbg_seed[] = "PSA";
5751
Gilles Peskinec6b69072018-11-20 21:42:52 +01005752 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005753 if( global_data.initialized != 0 )
5754 return( PSA_SUCCESS );
5755
Gilles Peskine5e769522018-11-20 21:59:56 +01005756 /* Set default configuration if
5757 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5758 if( global_data.entropy_init == NULL )
5759 global_data.entropy_init = mbedtls_entropy_init;
5760 if( global_data.entropy_free == NULL )
5761 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005762
Gilles Peskinec6b69072018-11-20 21:42:52 +01005763 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005764 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005765 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005766 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005767 status = mbedtls_to_psa_error(
5768 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5769 mbedtls_entropy_func,
5770 &global_data.entropy,
5771 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5772 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005773 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005774 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005775
Gilles Peskine66fb1262018-12-10 16:29:04 +01005776 status = psa_initialize_key_slots( );
5777 if( status != PSA_SUCCESS )
5778 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005779
Gilles Peskinefc762652019-07-22 19:30:34 +02005780#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5781 status = psa_crypto_load_transaction( );
5782 if( status == PSA_SUCCESS )
5783 {
5784 /*TOnogrepDO: complete or abort the transaction*/
5785 }
5786 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5787 {
5788 /* There's no transaction to complete. It's all good. */
5789 status = PSA_SUCCESS;
5790 }
5791#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5792
Gilles Peskinec6b69072018-11-20 21:42:52 +01005793 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005794 global_data.initialized = 1;
5795
Gilles Peskinee59236f2018-01-27 23:32:46 +01005796exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005797 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005798 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005799 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005800}
5801
5802#endif /* MBEDTLS_PSA_CRYPTO_C */