blob: c482747b75bf229241c53b1bc6990aff3b4e44d5 [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 Peskinef77ed1f2018-12-03 11:58:46 +0100874/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100875static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000876{
Gilles Peskine73167e12019-07-12 23:44:37 +0200877#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
878 if( psa_key_slot_is_external( slot ) )
879 {
880 /* No key material to clean. */
881 }
882 else
883#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +0000884 if( slot->type == PSA_KEY_TYPE_NONE )
885 {
886 /* No key material to clean. */
887 }
888 else if( key_type_is_raw_bytes( slot->type ) )
889 {
890 mbedtls_free( slot->data.raw.data );
891 }
892 else
893#if defined(MBEDTLS_RSA_C)
894 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
895 {
896 mbedtls_rsa_free( slot->data.rsa );
897 mbedtls_free( slot->data.rsa );
898 }
899 else
900#endif /* defined(MBEDTLS_RSA_C) */
901#if defined(MBEDTLS_ECP_C)
902 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
903 {
904 mbedtls_ecp_keypair_free( slot->data.ecp );
905 mbedtls_free( slot->data.ecp );
906 }
907 else
908#endif /* defined(MBEDTLS_ECP_C) */
909 {
910 /* Shouldn't happen: the key type is not any type that we
911 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200912 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000913 }
914
915 return( PSA_SUCCESS );
916}
917
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100918static void psa_abort_operations_using_key( psa_key_slot_t *slot )
919{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200920 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100921 (void) slot;
922}
923
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100924/** Completely wipe a slot in memory, including its policy.
925 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100926psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100927{
928 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100929 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100930 /* At this point, key material and other type-specific content has
931 * been wiped. Clear remaining metadata. We can call memset and not
932 * zeroize because the metadata is not particularly sensitive. */
933 memset( slot, 0, sizeof( *slot ) );
934 return( status );
935}
936
Gilles Peskinec5487a82018-12-03 18:08:14 +0100937psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100938{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100939 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100940 psa_status_t status = PSA_SUCCESS;
941 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200942#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
943 psa_se_drv_table_entry_t *driver;
944#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945
Gilles Peskinec5487a82018-12-03 18:08:14 +0100946 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200947 if( status != PSA_SUCCESS )
948 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +0200949
950#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
951 driver = psa_get_se_driver_entry( slot->lifetime );
952 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +0200953 {
954 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
955 psa_crypto_transaction.key.lifetime = slot->lifetime;
956 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
957 psa_crypto_transaction.key.id = slot->persistent_storage_id;
958 status = psa_crypto_save_transaction( );
959 if( status != PSA_SUCCESS )
960 {
961 /* TOnogrepDO: destroy what can be destroyed anyway */
962 return( status );
963 }
964
Gilles Peskine354f7672019-07-12 23:46:38 +0200965 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +0200966 }
Gilles Peskine354f7672019-07-12 23:46:38 +0200967#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
968
Darryl Greend49a4992018-06-18 17:27:26 +0100969#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
970 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
971 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100972 storage_status =
973 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100974 }
975#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +0200976
Gilles Peskinefc762652019-07-22 19:30:34 +0200977#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
978 if( driver != NULL )
979 {
980 status = psa_crypto_stop_transaction( );
981 if( status != PSA_SUCCESS )
982 {
983 /* TOnogrepDO: destroy what can be destroyed anyway */
984 return( status );
985 }
986 }
987#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
988
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100989 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100990 if( status != PSA_SUCCESS )
991 return( status );
992 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993}
994
Gilles Peskineb870b182018-07-06 16:02:09 +0200995/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200996static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200997{
998 if( key_type_is_raw_bytes( slot->type ) )
999 return( slot->data.raw.bytes * 8 );
1000#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001001 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001002 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001003#endif /* defined(MBEDTLS_RSA_C) */
1004#if defined(MBEDTLS_ECP_C)
1005 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1006 return( slot->data.ecp->grp.pbits );
1007#endif /* defined(MBEDTLS_ECP_C) */
1008 /* Shouldn't happen except on an empty slot. */
1009 return( 0 );
1010}
1011
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001012void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1013{
Gilles Peskineb699f072019-04-26 16:06:02 +02001014 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001015 memset( attributes, 0, sizeof( *attributes ) );
1016}
1017
Gilles Peskineb699f072019-04-26 16:06:02 +02001018psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1019 psa_key_type_t type,
1020 const uint8_t *data,
1021 size_t data_length )
1022{
1023 uint8_t *copy = NULL;
1024
1025 if( data_length != 0 )
1026 {
1027 copy = mbedtls_calloc( 1, data_length );
1028 if( copy == NULL )
1029 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1030 memcpy( copy, data, data_length );
1031 }
1032 /* After this point, this function is guaranteed to succeed, so it
1033 * can start modifying `*attributes`. */
1034
1035 if( attributes->domain_parameters != NULL )
1036 {
1037 mbedtls_free( attributes->domain_parameters );
1038 attributes->domain_parameters = NULL;
1039 attributes->domain_parameters_size = 0;
1040 }
1041
1042 attributes->domain_parameters = copy;
1043 attributes->domain_parameters_size = data_length;
1044 attributes->type = type;
1045 return( PSA_SUCCESS );
1046}
1047
1048psa_status_t psa_get_key_domain_parameters(
1049 const psa_key_attributes_t *attributes,
1050 uint8_t *data, size_t data_size, size_t *data_length )
1051{
1052 if( attributes->domain_parameters_size > data_size )
1053 return( PSA_ERROR_BUFFER_TOO_SMALL );
1054 *data_length = attributes->domain_parameters_size;
1055 if( attributes->domain_parameters_size != 0 )
1056 memcpy( data, attributes->domain_parameters,
1057 attributes->domain_parameters_size );
1058 return( PSA_SUCCESS );
1059}
1060
1061#if defined(MBEDTLS_RSA_C)
1062static psa_status_t psa_get_rsa_public_exponent(
1063 const mbedtls_rsa_context *rsa,
1064 psa_key_attributes_t *attributes )
1065{
1066 mbedtls_mpi mpi;
1067 int ret;
1068 uint8_t *buffer = NULL;
1069 size_t buflen;
1070 mbedtls_mpi_init( &mpi );
1071
1072 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1073 if( ret != 0 )
1074 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001075 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1076 {
1077 /* It's the default value, which is reported as an empty string,
1078 * so there's nothing to do. */
1079 goto exit;
1080 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001081
1082 buflen = mbedtls_mpi_size( &mpi );
1083 buffer = mbedtls_calloc( 1, buflen );
1084 if( buffer == NULL )
1085 {
1086 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1087 goto exit;
1088 }
1089 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1090 if( ret != 0 )
1091 goto exit;
1092 attributes->domain_parameters = buffer;
1093 attributes->domain_parameters_size = buflen;
1094
1095exit:
1096 mbedtls_mpi_free( &mpi );
1097 if( ret != 0 )
1098 mbedtls_free( buffer );
1099 return( mbedtls_to_psa_error( ret ) );
1100}
1101#endif /* MBEDTLS_RSA_C */
1102
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001103psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1104 psa_key_attributes_t *attributes )
1105{
1106 psa_key_slot_t *slot;
1107 psa_status_t status;
1108
1109 psa_reset_key_attributes( attributes );
1110
1111 status = psa_get_key_slot( handle, &slot );
1112 if( status != PSA_SUCCESS )
1113 return( status );
1114
1115 attributes->id = slot->persistent_storage_id;
1116 attributes->lifetime = slot->lifetime;
1117 attributes->policy = slot->policy;
1118 attributes->type = slot->type;
1119 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001120
1121 switch( slot->type )
1122 {
1123#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001124 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001125 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1126 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1127 break;
1128#endif
1129 default:
1130 /* Nothing else to do. */
1131 break;
1132 }
1133
1134 if( status != PSA_SUCCESS )
1135 psa_reset_key_attributes( attributes );
1136 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001137}
1138
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001139#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001140static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1141 unsigned char *buf, size_t size )
1142{
1143 int ret;
1144 unsigned char *c;
1145 size_t len = 0;
1146
1147 c = buf + size;
1148
1149 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1150
1151 return( (int) len );
1152}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001153#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001154
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001155static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1156 uint8_t *data,
1157 size_t data_size,
1158 size_t *data_length,
1159 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001160{
Gilles Peskine5d309672019-07-12 23:47:28 +02001161#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1162 const psa_drv_se_t *drv;
1163 psa_drv_se_context_t *drv_context;
1164#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1165
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001166 *data_length = 0;
1167
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001168 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001169 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001170
Gilles Peskine5d309672019-07-12 23:47:28 +02001171#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1172 if( psa_get_se_driver( slot->lifetime, &drv, &drv_context ) )
1173 {
1174 psa_drv_se_export_key_t method;
1175 if( drv->key_management == NULL )
1176 return( PSA_ERROR_NOT_SUPPORTED );
1177 method = ( export_public_key ?
1178 drv->key_management->p_export_public :
1179 drv->key_management->p_export );
1180 if( method == NULL )
1181 return( PSA_ERROR_NOT_SUPPORTED );
1182 return( ( *method )( drv_context,
1183 slot->data.se.slot_number,
1184 data, data_size, data_length ) );
1185 }
1186#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1187
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001188 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001189 {
1190 if( slot->data.raw.bytes > data_size )
1191 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001192 if( data_size != 0 )
1193 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001194 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001195 memset( data + slot->data.raw.bytes, 0,
1196 data_size - slot->data.raw.bytes );
1197 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001198 *data_length = slot->data.raw.bytes;
1199 return( PSA_SUCCESS );
1200 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001201#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001202 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001203 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001204 psa_status_t status;
1205
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001206 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001207 if( bytes > data_size )
1208 return( PSA_ERROR_BUFFER_TOO_SMALL );
1209 status = mbedtls_to_psa_error(
1210 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1211 if( status != PSA_SUCCESS )
1212 return( status );
1213 memset( data + bytes, 0, data_size - bytes );
1214 *data_length = bytes;
1215 return( PSA_SUCCESS );
1216 }
1217#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001218 else
Moran Peker17e36e12018-05-02 12:55:20 +03001219 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001220#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001221 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001222 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001223 {
Moran Pekera998bc62018-04-16 18:16:20 +03001224 mbedtls_pk_context pk;
1225 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001226 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001227 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001228#if defined(MBEDTLS_RSA_C)
1229 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001230 pk.pk_info = &mbedtls_rsa_info;
1231 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001232#else
1233 return( PSA_ERROR_NOT_SUPPORTED );
1234#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001235 }
1236 else
1237 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001238#if defined(MBEDTLS_ECP_C)
1239 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001240 pk.pk_info = &mbedtls_eckey_info;
1241 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001242#else
1243 return( PSA_ERROR_NOT_SUPPORTED );
1244#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001245 }
Moran Pekerd7326592018-05-29 16:56:39 +03001246 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001247 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001248 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001249 }
Moran Peker17e36e12018-05-02 12:55:20 +03001250 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001251 {
Moran Peker17e36e12018-05-02 12:55:20 +03001252 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001253 }
Moran Peker60364322018-04-29 11:34:58 +03001254 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001255 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001256 /* If data_size is 0 then data may be NULL and then the
1257 * call to memset would have undefined behavior. */
1258 if( data_size != 0 )
1259 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001260 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001261 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001262 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1263 * Move the data to the beginning and erase remaining data
1264 * at the original location. */
1265 if( 2 * (size_t) ret <= data_size )
1266 {
1267 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001268 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001269 }
1270 else if( (size_t) ret < data_size )
1271 {
1272 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001273 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001274 }
Moran Pekera998bc62018-04-16 18:16:20 +03001275 *data_length = ret;
1276 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001277 }
1278 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001279#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001280 {
1281 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001282 it is valid for a special-purpose implementation to omit
1283 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001284 return( PSA_ERROR_NOT_SUPPORTED );
1285 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001286 }
1287}
1288
Gilles Peskinec5487a82018-12-03 18:08:14 +01001289psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001290 uint8_t *data,
1291 size_t data_size,
1292 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001293{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001294 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001295 psa_status_t status;
1296
1297 /* Set the key to empty now, so that even when there are errors, we always
1298 * set data_length to a value between 0 and data_size. On error, setting
1299 * the key to empty is a good choice because an empty key representation is
1300 * unlikely to be accepted anywhere. */
1301 *data_length = 0;
1302
1303 /* Export requires the EXPORT flag. There is an exception for public keys,
1304 * which don't require any flag, but psa_get_key_from_slot takes
1305 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001306 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001307 if( status != PSA_SUCCESS )
1308 return( status );
1309 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001310 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001311}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001312
Gilles Peskinec5487a82018-12-03 18:08:14 +01001313psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001314 uint8_t *data,
1315 size_t data_size,
1316 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001317{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001318 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001319 psa_status_t status;
1320
1321 /* Set the key to empty now, so that even when there are errors, we always
1322 * set data_length to a value between 0 and data_size. On error, setting
1323 * the key to empty is a good choice because an empty key representation is
1324 * unlikely to be accepted anywhere. */
1325 *data_length = 0;
1326
1327 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001328 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001329 if( status != PSA_SUCCESS )
1330 return( status );
1331 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001332 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001333}
1334
Gilles Peskine4747d192019-04-17 15:05:45 +02001335static psa_status_t psa_set_key_policy_internal(
1336 psa_key_slot_t *slot,
1337 const psa_key_policy_t *policy )
1338{
1339 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001340 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001341 PSA_KEY_USAGE_ENCRYPT |
1342 PSA_KEY_USAGE_DECRYPT |
1343 PSA_KEY_USAGE_SIGN |
1344 PSA_KEY_USAGE_VERIFY |
1345 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1346 return( PSA_ERROR_INVALID_ARGUMENT );
1347
1348 slot->policy = *policy;
1349 return( PSA_SUCCESS );
1350}
1351
1352/** Prepare a key slot to receive key material.
1353 *
1354 * This function allocates a key slot and sets its metadata.
1355 *
1356 * If this function fails, call psa_fail_key_creation().
1357 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001358 * This function is intended to be used as follows:
1359 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1360 * it with the specified attributes, and assign it a handle.
1361 * -# Populate the slot with the key material.
1362 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1363 * In case of failure at any step, stop the sequence and call
1364 * psa_fail_key_creation().
1365 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001366 * \param[in] attributes Key attributes for the new key.
1367 * \param[out] handle On success, a handle for the allocated slot.
1368 * \param[out] p_slot On success, a pointer to the prepared slot.
1369 * \param[out] p_drv On any return, the driver for the key, if any.
1370 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001371 *
1372 * \retval #PSA_SUCCESS
1373 * The key slot is ready to receive key material.
1374 * \return If this function fails, the key slot is an invalid state.
1375 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001376 */
1377static psa_status_t psa_start_key_creation(
1378 const psa_key_attributes_t *attributes,
1379 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001380 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001381 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001382{
1383 psa_status_t status;
1384 psa_key_slot_t *slot;
1385
Gilles Peskine011e4282019-06-26 18:34:38 +02001386 *p_drv = NULL;
1387
Gilles Peskine267c6562019-05-27 19:01:54 +02001388 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001389 if( status != PSA_SUCCESS )
1390 return( status );
1391 slot = *p_slot;
1392
1393 status = psa_set_key_policy_internal( slot, &attributes->policy );
1394 if( status != PSA_SUCCESS )
1395 return( status );
1396 slot->lifetime = attributes->lifetime;
Gilles Peskine011e4282019-06-26 18:34:38 +02001397
Gilles Peskine4747d192019-04-17 15:05:45 +02001398 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001399 {
1400 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskine011e4282019-06-26 18:34:38 +02001401 attributes->id,
1402 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001403 if( status != PSA_SUCCESS )
1404 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001405 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001406 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001407 slot->type = attributes->type;
1408
Gilles Peskinecbaff462019-07-12 23:46:04 +02001409#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskinefc762652019-07-22 19:30:34 +02001410 /* Find a slot number for the new key. Save the slot number in
1411 * persistent storage, but do not yet save the driver's persistent
1412 * state, so that if the power fails during the key creation process,
1413 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001414 if( *p_drv != NULL )
1415 {
1416 status = psa_find_se_slot_for_key( attributes, *p_drv,
1417 &slot->data.se.slot_number );
1418 if( status != PSA_SUCCESS )
1419 return( status );
1420 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001421 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1422 psa_crypto_transaction.key.lifetime = slot->lifetime;
1423 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1424 psa_crypto_transaction.key.id = slot->persistent_storage_id;
1425 status = psa_crypto_save_transaction( );
1426 if( status != PSA_SUCCESS )
1427 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001428#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1429
Gilles Peskine4747d192019-04-17 15:05:45 +02001430 return( status );
1431}
1432
1433/** Finalize the creation of a key once its key material has been set.
1434 *
1435 * This entails writing the key to persistent storage.
1436 *
1437 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001438 * See the documentation of psa_start_key_creation() for the intended use
1439 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001440 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001441 * \param[in,out] slot Pointer to the slot with key material.
1442 * \param[in] driver The secure element driver for the key,
1443 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001444 *
1445 * \retval #PSA_SUCCESS
1446 * The key was successfully created. The handle is now valid.
1447 * \return If this function fails, the key slot is an invalid state.
1448 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001449 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001450static psa_status_t psa_finish_key_creation(
1451 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001452 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001453{
1454 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001455 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001456 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001457
1458#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1459 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1460 {
1461 uint8_t *buffer = NULL;
1462 size_t buffer_size = 0;
1463 size_t length;
1464
1465 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001466 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001467 buffer = mbedtls_calloc( 1, buffer_size );
1468 if( buffer == NULL && buffer_size != 0 )
1469 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1470 status = psa_internal_export_key( slot,
1471 buffer, buffer_size, &length,
1472 0 );
1473
1474 if( status == PSA_SUCCESS )
1475 {
1476 status = psa_save_persistent_key( slot->persistent_storage_id,
1477 slot->type, &slot->policy,
1478 buffer, length );
1479 }
1480
1481 if( buffer_size != 0 )
1482 mbedtls_platform_zeroize( buffer, buffer_size );
1483 mbedtls_free( buffer );
1484 }
1485#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1486
Gilles Peskinecbaff462019-07-12 23:46:04 +02001487#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1488 if( driver != NULL )
1489 {
1490 status = psa_save_se_persistent_data( driver );
1491 if( status != PSA_SUCCESS )
1492 {
1493 psa_destroy_persistent_key( slot->persistent_storage_id );
1494 return( status );
1495 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001496 status = psa_crypto_stop_transaction( );
1497 if( status != PSA_SUCCESS )
1498 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001499 }
1500#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1501
Gilles Peskine4747d192019-04-17 15:05:45 +02001502 return( status );
1503}
1504
1505/** Abort the creation of a key.
1506 *
1507 * You may call this function after calling psa_start_key_creation(),
1508 * or after psa_finish_key_creation() fails. In other circumstances, this
1509 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001510 * See the documentation of psa_start_key_creation() for the intended use
1511 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001512 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001513 * \param[in,out] slot Pointer to the slot with key material.
1514 * \param[in] driver The secure element driver for the key,
1515 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001516 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001517static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001518 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001519{
Gilles Peskine011e4282019-06-26 18:34:38 +02001520 (void) driver;
1521
Gilles Peskine4747d192019-04-17 15:05:45 +02001522 if( slot == NULL )
1523 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001524
1525#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1526 /* TOnogrepDO: If the key has already been created in the secure
1527 * element, and the failure happened later (when saving metadata
1528 * to internal storage), we need to destroy the key in the secure
1529 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001530
1531 /* Abort the ongoing transaction if any. We already did what it
1532 * takes to undo any partial creation. All that's left is to update
1533 * the transaction data itself. */
1534 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001535#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1536
Gilles Peskine4747d192019-04-17 15:05:45 +02001537 psa_wipe_key_slot( slot );
1538}
1539
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001540static psa_status_t psa_check_key_slot_attributes(
1541 const psa_key_slot_t *slot,
1542 const psa_key_attributes_t *attributes )
1543{
1544 if( attributes->type != 0 )
1545 {
1546 if( attributes->type != slot->type )
1547 return( PSA_ERROR_INVALID_ARGUMENT );
1548 }
1549
1550 if( attributes->domain_parameters_size != 0 )
1551 {
1552#if defined(MBEDTLS_RSA_C)
1553 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1554 {
1555 mbedtls_mpi actual, required;
1556 int ret;
1557 mbedtls_mpi_init( &actual );
1558 mbedtls_mpi_init( &required );
1559 ret = mbedtls_rsa_export( slot->data.rsa,
1560 NULL, NULL, NULL, NULL, &actual );
1561 if( ret != 0 )
1562 goto rsa_exit;
1563 ret = mbedtls_mpi_read_binary( &required,
1564 attributes->domain_parameters,
1565 attributes->domain_parameters_size );
1566 if( ret != 0 )
1567 goto rsa_exit;
1568 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1569 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1570 rsa_exit:
1571 mbedtls_mpi_free( &actual );
1572 mbedtls_mpi_free( &required );
1573 if( ret != 0)
1574 return( mbedtls_to_psa_error( ret ) );
1575 }
1576 else
1577#endif
1578 {
1579 return( PSA_ERROR_INVALID_ARGUMENT );
1580 }
1581 }
1582
1583 if( attributes->bits != 0 )
1584 {
1585 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1586 return( PSA_ERROR_INVALID_ARGUMENT );
1587 }
1588
1589 return( PSA_SUCCESS );
1590}
1591
Gilles Peskine4747d192019-04-17 15:05:45 +02001592psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001593 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001594 size_t data_length,
1595 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001596{
1597 psa_status_t status;
1598 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001599 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001600
Gilles Peskine011e4282019-06-26 18:34:38 +02001601 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001602 if( status != PSA_SUCCESS )
1603 goto exit;
1604
Gilles Peskine5d309672019-07-12 23:47:28 +02001605#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1606 if( driver != NULL )
1607 {
1608 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
1609 if( drv->key_management == NULL ||
1610 drv->key_management->p_import == NULL )
1611 {
1612 status = PSA_ERROR_NOT_SUPPORTED;
1613 goto exit;
1614 }
1615 status = drv->key_management->p_import(
1616 psa_get_se_driver_context( driver ),
1617 slot->data.se.slot_number,
1618 slot->lifetime, slot->type, slot->policy.alg, slot->policy.usage,
1619 data, data_length );
1620 /* TOnogrepDO: psa_check_key_slot_attributes? */
1621 }
1622 else
1623#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1624 {
1625 status = psa_import_key_into_slot( slot, data, data_length );
1626 if( status != PSA_SUCCESS )
1627 goto exit;
1628 status = psa_check_key_slot_attributes( slot, attributes );
1629 if( status != PSA_SUCCESS )
1630 goto exit;
1631 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001632
Gilles Peskine011e4282019-06-26 18:34:38 +02001633 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001634exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001635 if( status != PSA_SUCCESS )
1636 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001637 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001638 *handle = 0;
1639 }
1640 return( status );
1641}
1642
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001643static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001644 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001645{
1646 psa_status_t status;
1647 uint8_t *buffer = NULL;
1648 size_t buffer_size = 0;
1649 size_t length;
1650
1651 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001652 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001653 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001654 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001655 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001656 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1657 if( status != PSA_SUCCESS )
1658 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001659 target->type = source->type;
1660 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001661
1662exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001663 if( buffer_size != 0 )
1664 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001665 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001666 return( status );
1667}
1668
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001669psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1670 const psa_key_attributes_t *specified_attributes,
1671 psa_key_handle_t *target_handle )
1672{
1673 psa_status_t status;
1674 psa_key_slot_t *source_slot = NULL;
1675 psa_key_slot_t *target_slot = NULL;
1676 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001677 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001678
Gilles Peskinec160d9e2019-05-14 14:32:03 +02001679 status = psa_get_key_from_slot( source_handle, &source_slot,
1680 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001681 if( status != PSA_SUCCESS )
1682 goto exit;
1683
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001684 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1685 if( status != PSA_SUCCESS )
1686 goto exit;
1687
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001688 status = psa_restrict_key_policy( &actual_attributes.policy,
1689 &source_slot->policy );
1690 if( status != PSA_SUCCESS )
1691 goto exit;
1692
1693 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001694 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001695 if( status != PSA_SUCCESS )
1696 goto exit;
1697
1698 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001699 if( status != PSA_SUCCESS )
1700 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001701
Gilles Peskine011e4282019-06-26 18:34:38 +02001702 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001703exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001704 if( status != PSA_SUCCESS )
1705 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001706 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001707 *target_handle = 0;
1708 }
1709 return( status );
1710}
1711
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001712
1713
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001714/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001715/* Message digests */
1716/****************************************************************/
1717
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001718static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001719{
1720 switch( alg )
1721 {
1722#if defined(MBEDTLS_MD2_C)
1723 case PSA_ALG_MD2:
1724 return( &mbedtls_md2_info );
1725#endif
1726#if defined(MBEDTLS_MD4_C)
1727 case PSA_ALG_MD4:
1728 return( &mbedtls_md4_info );
1729#endif
1730#if defined(MBEDTLS_MD5_C)
1731 case PSA_ALG_MD5:
1732 return( &mbedtls_md5_info );
1733#endif
1734#if defined(MBEDTLS_RIPEMD160_C)
1735 case PSA_ALG_RIPEMD160:
1736 return( &mbedtls_ripemd160_info );
1737#endif
1738#if defined(MBEDTLS_SHA1_C)
1739 case PSA_ALG_SHA_1:
1740 return( &mbedtls_sha1_info );
1741#endif
1742#if defined(MBEDTLS_SHA256_C)
1743 case PSA_ALG_SHA_224:
1744 return( &mbedtls_sha224_info );
1745 case PSA_ALG_SHA_256:
1746 return( &mbedtls_sha256_info );
1747#endif
1748#if defined(MBEDTLS_SHA512_C)
1749 case PSA_ALG_SHA_384:
1750 return( &mbedtls_sha384_info );
1751 case PSA_ALG_SHA_512:
1752 return( &mbedtls_sha512_info );
1753#endif
1754 default:
1755 return( NULL );
1756 }
1757}
1758
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001759psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1760{
1761 switch( operation->alg )
1762 {
Gilles Peskine81736312018-06-26 15:04:31 +02001763 case 0:
1764 /* The object has (apparently) been initialized but it is not
1765 * in use. It's ok to call abort on such an object, and there's
1766 * nothing to do. */
1767 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001768#if defined(MBEDTLS_MD2_C)
1769 case PSA_ALG_MD2:
1770 mbedtls_md2_free( &operation->ctx.md2 );
1771 break;
1772#endif
1773#if defined(MBEDTLS_MD4_C)
1774 case PSA_ALG_MD4:
1775 mbedtls_md4_free( &operation->ctx.md4 );
1776 break;
1777#endif
1778#if defined(MBEDTLS_MD5_C)
1779 case PSA_ALG_MD5:
1780 mbedtls_md5_free( &operation->ctx.md5 );
1781 break;
1782#endif
1783#if defined(MBEDTLS_RIPEMD160_C)
1784 case PSA_ALG_RIPEMD160:
1785 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1786 break;
1787#endif
1788#if defined(MBEDTLS_SHA1_C)
1789 case PSA_ALG_SHA_1:
1790 mbedtls_sha1_free( &operation->ctx.sha1 );
1791 break;
1792#endif
1793#if defined(MBEDTLS_SHA256_C)
1794 case PSA_ALG_SHA_224:
1795 case PSA_ALG_SHA_256:
1796 mbedtls_sha256_free( &operation->ctx.sha256 );
1797 break;
1798#endif
1799#if defined(MBEDTLS_SHA512_C)
1800 case PSA_ALG_SHA_384:
1801 case PSA_ALG_SHA_512:
1802 mbedtls_sha512_free( &operation->ctx.sha512 );
1803 break;
1804#endif
1805 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001806 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001807 }
1808 operation->alg = 0;
1809 return( PSA_SUCCESS );
1810}
1811
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001812psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001813 psa_algorithm_t alg )
1814{
1815 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001816
1817 /* A context must be freshly initialized before it can be set up. */
1818 if( operation->alg != 0 )
1819 {
1820 return( PSA_ERROR_BAD_STATE );
1821 }
1822
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001823 switch( alg )
1824 {
1825#if defined(MBEDTLS_MD2_C)
1826 case PSA_ALG_MD2:
1827 mbedtls_md2_init( &operation->ctx.md2 );
1828 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1829 break;
1830#endif
1831#if defined(MBEDTLS_MD4_C)
1832 case PSA_ALG_MD4:
1833 mbedtls_md4_init( &operation->ctx.md4 );
1834 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1835 break;
1836#endif
1837#if defined(MBEDTLS_MD5_C)
1838 case PSA_ALG_MD5:
1839 mbedtls_md5_init( &operation->ctx.md5 );
1840 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1841 break;
1842#endif
1843#if defined(MBEDTLS_RIPEMD160_C)
1844 case PSA_ALG_RIPEMD160:
1845 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1846 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1847 break;
1848#endif
1849#if defined(MBEDTLS_SHA1_C)
1850 case PSA_ALG_SHA_1:
1851 mbedtls_sha1_init( &operation->ctx.sha1 );
1852 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1853 break;
1854#endif
1855#if defined(MBEDTLS_SHA256_C)
1856 case PSA_ALG_SHA_224:
1857 mbedtls_sha256_init( &operation->ctx.sha256 );
1858 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1859 break;
1860 case PSA_ALG_SHA_256:
1861 mbedtls_sha256_init( &operation->ctx.sha256 );
1862 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1863 break;
1864#endif
1865#if defined(MBEDTLS_SHA512_C)
1866 case PSA_ALG_SHA_384:
1867 mbedtls_sha512_init( &operation->ctx.sha512 );
1868 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1869 break;
1870 case PSA_ALG_SHA_512:
1871 mbedtls_sha512_init( &operation->ctx.sha512 );
1872 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1873 break;
1874#endif
1875 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001876 return( PSA_ALG_IS_HASH( alg ) ?
1877 PSA_ERROR_NOT_SUPPORTED :
1878 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001879 }
1880 if( ret == 0 )
1881 operation->alg = alg;
1882 else
1883 psa_hash_abort( operation );
1884 return( mbedtls_to_psa_error( ret ) );
1885}
1886
1887psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1888 const uint8_t *input,
1889 size_t input_length )
1890{
1891 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001892
1893 /* Don't require hash implementations to behave correctly on a
1894 * zero-length input, which may have an invalid pointer. */
1895 if( input_length == 0 )
1896 return( PSA_SUCCESS );
1897
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001898 switch( operation->alg )
1899 {
1900#if defined(MBEDTLS_MD2_C)
1901 case PSA_ALG_MD2:
1902 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1903 input, input_length );
1904 break;
1905#endif
1906#if defined(MBEDTLS_MD4_C)
1907 case PSA_ALG_MD4:
1908 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1909 input, input_length );
1910 break;
1911#endif
1912#if defined(MBEDTLS_MD5_C)
1913 case PSA_ALG_MD5:
1914 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1915 input, input_length );
1916 break;
1917#endif
1918#if defined(MBEDTLS_RIPEMD160_C)
1919 case PSA_ALG_RIPEMD160:
1920 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1921 input, input_length );
1922 break;
1923#endif
1924#if defined(MBEDTLS_SHA1_C)
1925 case PSA_ALG_SHA_1:
1926 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1927 input, input_length );
1928 break;
1929#endif
1930#if defined(MBEDTLS_SHA256_C)
1931 case PSA_ALG_SHA_224:
1932 case PSA_ALG_SHA_256:
1933 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1934 input, input_length );
1935 break;
1936#endif
1937#if defined(MBEDTLS_SHA512_C)
1938 case PSA_ALG_SHA_384:
1939 case PSA_ALG_SHA_512:
1940 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1941 input, input_length );
1942 break;
1943#endif
1944 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001945 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001946 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001947
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001948 if( ret != 0 )
1949 psa_hash_abort( operation );
1950 return( mbedtls_to_psa_error( ret ) );
1951}
1952
1953psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1954 uint8_t *hash,
1955 size_t hash_size,
1956 size_t *hash_length )
1957{
itayzafrir40835d42018-08-02 13:14:17 +03001958 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001959 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001960 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001961
1962 /* Fill the output buffer with something that isn't a valid hash
1963 * (barring an attack on the hash and deliberately-crafted input),
1964 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001965 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001966 /* If hash_size is 0 then hash may be NULL and then the
1967 * call to memset would have undefined behavior. */
1968 if( hash_size != 0 )
1969 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001970
1971 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001972 {
1973 status = PSA_ERROR_BUFFER_TOO_SMALL;
1974 goto exit;
1975 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001976
1977 switch( operation->alg )
1978 {
1979#if defined(MBEDTLS_MD2_C)
1980 case PSA_ALG_MD2:
1981 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1982 break;
1983#endif
1984#if defined(MBEDTLS_MD4_C)
1985 case PSA_ALG_MD4:
1986 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1987 break;
1988#endif
1989#if defined(MBEDTLS_MD5_C)
1990 case PSA_ALG_MD5:
1991 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1992 break;
1993#endif
1994#if defined(MBEDTLS_RIPEMD160_C)
1995 case PSA_ALG_RIPEMD160:
1996 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1997 break;
1998#endif
1999#if defined(MBEDTLS_SHA1_C)
2000 case PSA_ALG_SHA_1:
2001 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2002 break;
2003#endif
2004#if defined(MBEDTLS_SHA256_C)
2005 case PSA_ALG_SHA_224:
2006 case PSA_ALG_SHA_256:
2007 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2008 break;
2009#endif
2010#if defined(MBEDTLS_SHA512_C)
2011 case PSA_ALG_SHA_384:
2012 case PSA_ALG_SHA_512:
2013 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2014 break;
2015#endif
2016 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002017 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002018 }
itayzafrir40835d42018-08-02 13:14:17 +03002019 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002020
itayzafrir40835d42018-08-02 13:14:17 +03002021exit:
2022 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002023 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002024 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002025 return( psa_hash_abort( operation ) );
2026 }
2027 else
2028 {
2029 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002030 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002031 }
2032}
2033
Gilles Peskine2d277862018-06-18 15:41:12 +02002034psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2035 const uint8_t *hash,
2036 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002037{
2038 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2039 size_t actual_hash_length;
2040 psa_status_t status = psa_hash_finish( operation,
2041 actual_hash, sizeof( actual_hash ),
2042 &actual_hash_length );
2043 if( status != PSA_SUCCESS )
2044 return( status );
2045 if( actual_hash_length != hash_length )
2046 return( PSA_ERROR_INVALID_SIGNATURE );
2047 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2048 return( PSA_ERROR_INVALID_SIGNATURE );
2049 return( PSA_SUCCESS );
2050}
2051
Gilles Peskineeb35d782019-01-22 17:56:16 +01002052psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2053 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002054{
2055 if( target_operation->alg != 0 )
2056 return( PSA_ERROR_BAD_STATE );
2057
2058 switch( source_operation->alg )
2059 {
2060 case 0:
2061 return( PSA_ERROR_BAD_STATE );
2062#if defined(MBEDTLS_MD2_C)
2063 case PSA_ALG_MD2:
2064 mbedtls_md2_clone( &target_operation->ctx.md2,
2065 &source_operation->ctx.md2 );
2066 break;
2067#endif
2068#if defined(MBEDTLS_MD4_C)
2069 case PSA_ALG_MD4:
2070 mbedtls_md4_clone( &target_operation->ctx.md4,
2071 &source_operation->ctx.md4 );
2072 break;
2073#endif
2074#if defined(MBEDTLS_MD5_C)
2075 case PSA_ALG_MD5:
2076 mbedtls_md5_clone( &target_operation->ctx.md5,
2077 &source_operation->ctx.md5 );
2078 break;
2079#endif
2080#if defined(MBEDTLS_RIPEMD160_C)
2081 case PSA_ALG_RIPEMD160:
2082 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2083 &source_operation->ctx.ripemd160 );
2084 break;
2085#endif
2086#if defined(MBEDTLS_SHA1_C)
2087 case PSA_ALG_SHA_1:
2088 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2089 &source_operation->ctx.sha1 );
2090 break;
2091#endif
2092#if defined(MBEDTLS_SHA256_C)
2093 case PSA_ALG_SHA_224:
2094 case PSA_ALG_SHA_256:
2095 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2096 &source_operation->ctx.sha256 );
2097 break;
2098#endif
2099#if defined(MBEDTLS_SHA512_C)
2100 case PSA_ALG_SHA_384:
2101 case PSA_ALG_SHA_512:
2102 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2103 &source_operation->ctx.sha512 );
2104 break;
2105#endif
2106 default:
2107 return( PSA_ERROR_NOT_SUPPORTED );
2108 }
2109
2110 target_operation->alg = source_operation->alg;
2111 return( PSA_SUCCESS );
2112}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002113
2114
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002115/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002116/* MAC */
2117/****************************************************************/
2118
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002119static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002120 psa_algorithm_t alg,
2121 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002122 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002123 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002124{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002125 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002126 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002127
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002128 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002129 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002130
Gilles Peskine8c9def32018-02-08 10:02:12 +01002131 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2132 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002133 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002134 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002135 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002136 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002137 mode = MBEDTLS_MODE_STREAM;
2138 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002139 case PSA_ALG_CTR:
2140 mode = MBEDTLS_MODE_CTR;
2141 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002142 case PSA_ALG_CFB:
2143 mode = MBEDTLS_MODE_CFB;
2144 break;
2145 case PSA_ALG_OFB:
2146 mode = MBEDTLS_MODE_OFB;
2147 break;
2148 case PSA_ALG_CBC_NO_PADDING:
2149 mode = MBEDTLS_MODE_CBC;
2150 break;
2151 case PSA_ALG_CBC_PKCS7:
2152 mode = MBEDTLS_MODE_CBC;
2153 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002154 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002155 mode = MBEDTLS_MODE_CCM;
2156 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002157 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002158 mode = MBEDTLS_MODE_GCM;
2159 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002160 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2161 mode = MBEDTLS_MODE_CHACHAPOLY;
2162 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002163 default:
2164 return( NULL );
2165 }
2166 }
2167 else if( alg == PSA_ALG_CMAC )
2168 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002169 else
2170 return( NULL );
2171
2172 switch( key_type )
2173 {
2174 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002175 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002176 break;
2177 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002178 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2179 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002180 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002181 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002182 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002183 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002184 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2185 * but two-key Triple-DES is functionally three-key Triple-DES
2186 * with K1=K3, so that's how we present it to mbedtls. */
2187 if( key_bits == 128 )
2188 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002189 break;
2190 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002191 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002192 break;
2193 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002194 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002195 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002196 case PSA_KEY_TYPE_CHACHA20:
2197 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2198 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002199 default:
2200 return( NULL );
2201 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002202 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002203 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002204
Jaeden Amero23bbb752018-06-26 14:16:54 +01002205 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2206 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002207}
2208
Gilles Peskinea05219c2018-11-16 16:02:56 +01002209#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002210static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002211{
Gilles Peskine2d277862018-06-18 15:41:12 +02002212 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002213 {
2214 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002215 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002216 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002217 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002218 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002219 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002220 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002221 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002222 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002223 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002224 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002225 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002226 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002227 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002228 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002229 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002230 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002231 return( 128 );
2232 default:
2233 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002234 }
2235}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002236#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002237
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002238/* Initialize the MAC operation structure. Once this function has been
2239 * called, psa_mac_abort can run and will do the right thing. */
2240static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2241 psa_algorithm_t alg )
2242{
2243 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2244
2245 operation->alg = alg;
2246 operation->key_set = 0;
2247 operation->iv_set = 0;
2248 operation->iv_required = 0;
2249 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002250 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002251
2252#if defined(MBEDTLS_CMAC_C)
2253 if( alg == PSA_ALG_CMAC )
2254 {
2255 operation->iv_required = 0;
2256 mbedtls_cipher_init( &operation->ctx.cmac );
2257 status = PSA_SUCCESS;
2258 }
2259 else
2260#endif /* MBEDTLS_CMAC_C */
2261#if defined(MBEDTLS_MD_C)
2262 if( PSA_ALG_IS_HMAC( operation->alg ) )
2263 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002264 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2265 operation->ctx.hmac.hash_ctx.alg = 0;
2266 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002267 }
2268 else
2269#endif /* MBEDTLS_MD_C */
2270 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002271 if( ! PSA_ALG_IS_MAC( alg ) )
2272 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002273 }
2274
2275 if( status != PSA_SUCCESS )
2276 memset( operation, 0, sizeof( *operation ) );
2277 return( status );
2278}
2279
Gilles Peskine01126fa2018-07-12 17:04:55 +02002280#if defined(MBEDTLS_MD_C)
2281static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2282{
Gilles Peskine3f108122018-12-07 18:14:53 +01002283 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002284 return( psa_hash_abort( &hmac->hash_ctx ) );
2285}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002286
Janos Follath999f6482019-06-11 12:04:10 +01002287#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002288static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2289{
2290 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2291 memset( hmac, 0, sizeof( *hmac ) );
2292}
Janos Follath999f6482019-06-11 12:04:10 +01002293#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002294#endif /* MBEDTLS_MD_C */
2295
Gilles Peskine8c9def32018-02-08 10:02:12 +01002296psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2297{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002298 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002299 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002300 /* The object has (apparently) been initialized but it is not
2301 * in use. It's ok to call abort on such an object, and there's
2302 * nothing to do. */
2303 return( PSA_SUCCESS );
2304 }
2305 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002306#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002307 if( operation->alg == PSA_ALG_CMAC )
2308 {
2309 mbedtls_cipher_free( &operation->ctx.cmac );
2310 }
2311 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002312#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002313#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002314 if( PSA_ALG_IS_HMAC( operation->alg ) )
2315 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002316 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002317 }
2318 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002319#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002320 {
2321 /* Sanity check (shouldn't happen: operation->alg should
2322 * always have been initialized to a valid value). */
2323 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002324 }
Moran Peker41deec42018-04-04 15:43:05 +03002325
Gilles Peskine8c9def32018-02-08 10:02:12 +01002326 operation->alg = 0;
2327 operation->key_set = 0;
2328 operation->iv_set = 0;
2329 operation->iv_required = 0;
2330 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002331 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002332
Gilles Peskine8c9def32018-02-08 10:02:12 +01002333 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002334
2335bad_state:
2336 /* If abort is called on an uninitialized object, we can't trust
2337 * anything. Wipe the object in case it contains confidential data.
2338 * This may result in a memory leak if a pointer gets overwritten,
2339 * but it's too late to do anything about this. */
2340 memset( operation, 0, sizeof( *operation ) );
2341 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002342}
2343
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002344#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002345static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002346 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002347 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002348 const mbedtls_cipher_info_t *cipher_info )
2349{
2350 int ret;
2351
2352 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002353
2354 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2355 if( ret != 0 )
2356 return( ret );
2357
2358 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2359 slot->data.raw.data,
2360 key_bits );
2361 return( ret );
2362}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002363#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002364
Gilles Peskine248051a2018-06-20 16:09:38 +02002365#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002366static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2367 const uint8_t *key,
2368 size_t key_length,
2369 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002370{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002371 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002372 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002373 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002374 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002375 psa_status_t status;
2376
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002377 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2378 * overflow below. This should never trigger if the hash algorithm
2379 * is implemented correctly. */
2380 /* The size checks against the ipad and opad buffers cannot be written
2381 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2382 * because that triggers -Wlogical-op on GCC 7.3. */
2383 if( block_size > sizeof( ipad ) )
2384 return( PSA_ERROR_NOT_SUPPORTED );
2385 if( block_size > sizeof( hmac->opad ) )
2386 return( PSA_ERROR_NOT_SUPPORTED );
2387 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002388 return( PSA_ERROR_NOT_SUPPORTED );
2389
Gilles Peskined223b522018-06-11 18:12:58 +02002390 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002391 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002392 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002393 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002394 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002395 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002396 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002397 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002398 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002399 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002400 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002401 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002402 }
Gilles Peskine96889972018-07-12 17:07:03 +02002403 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2404 * but it is permitted. It is common when HMAC is used in HKDF, for
2405 * example. Don't call `memcpy` in the 0-length because `key` could be
2406 * an invalid pointer which would make the behavior undefined. */
2407 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002408 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002409
Gilles Peskined223b522018-06-11 18:12:58 +02002410 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2411 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002412 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002413 ipad[i] ^= 0x36;
2414 memset( ipad + key_length, 0x36, block_size - key_length );
2415
2416 /* Copy the key material from ipad to opad, flipping the requisite bits,
2417 * and filling the rest of opad with the requisite constant. */
2418 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002419 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2420 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002421
Gilles Peskine01126fa2018-07-12 17:04:55 +02002422 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002423 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002424 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002425
Gilles Peskine01126fa2018-07-12 17:04:55 +02002426 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002427
2428cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002429 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002430
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002431 return( status );
2432}
Gilles Peskine248051a2018-06-20 16:09:38 +02002433#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002434
Gilles Peskine89167cb2018-07-08 20:12:23 +02002435static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002436 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002437 psa_algorithm_t alg,
2438 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002439{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002440 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002441 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002442 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002443 psa_key_usage_t usage =
2444 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002445 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002446 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002447
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002448 /* A context must be freshly initialized before it can be set up. */
2449 if( operation->alg != 0 )
2450 {
2451 return( PSA_ERROR_BAD_STATE );
2452 }
2453
Gilles Peskined911eb72018-08-14 15:18:45 +02002454 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002455 if( status != PSA_SUCCESS )
2456 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002457 if( is_sign )
2458 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459
Gilles Peskinec5487a82018-12-03 18:08:14 +01002460 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002461 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002462 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002463 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002464
Gilles Peskine8c9def32018-02-08 10:02:12 +01002465#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002466 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002467 {
2468 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002469 mbedtls_cipher_info_from_psa( full_length_alg,
2470 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002471 int ret;
2472 if( cipher_info == NULL )
2473 {
2474 status = PSA_ERROR_NOT_SUPPORTED;
2475 goto exit;
2476 }
2477 operation->mac_size = cipher_info->block_size;
2478 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2479 status = mbedtls_to_psa_error( ret );
2480 }
2481 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002482#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002484 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002485 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002486 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002487 if( hash_alg == 0 )
2488 {
2489 status = PSA_ERROR_NOT_SUPPORTED;
2490 goto exit;
2491 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002492
2493 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2494 /* Sanity check. This shouldn't fail on a valid configuration. */
2495 if( operation->mac_size == 0 ||
2496 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2497 {
2498 status = PSA_ERROR_NOT_SUPPORTED;
2499 goto exit;
2500 }
2501
Gilles Peskine01126fa2018-07-12 17:04:55 +02002502 if( slot->type != PSA_KEY_TYPE_HMAC )
2503 {
2504 status = PSA_ERROR_INVALID_ARGUMENT;
2505 goto exit;
2506 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002507
Gilles Peskine01126fa2018-07-12 17:04:55 +02002508 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2509 slot->data.raw.data,
2510 slot->data.raw.bytes,
2511 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002512 }
2513 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002514#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002515 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002516 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002517 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002519
Gilles Peskined911eb72018-08-14 15:18:45 +02002520 if( truncated == 0 )
2521 {
2522 /* The "normal" case: untruncated algorithm. Nothing to do. */
2523 }
2524 else if( truncated < 4 )
2525 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002526 /* A very short MAC is too short for security since it can be
2527 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2528 * so we make this our minimum, even though 32 bits is still
2529 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002530 status = PSA_ERROR_NOT_SUPPORTED;
2531 }
2532 else if( truncated > operation->mac_size )
2533 {
2534 /* It's impossible to "truncate" to a larger length. */
2535 status = PSA_ERROR_INVALID_ARGUMENT;
2536 }
2537 else
2538 operation->mac_size = truncated;
2539
Gilles Peskinefbfac682018-07-08 20:51:54 +02002540exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002541 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002543 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002544 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002545 else
2546 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002547 operation->key_set = 1;
2548 }
2549 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550}
2551
Gilles Peskine89167cb2018-07-08 20:12:23 +02002552psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002553 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002554 psa_algorithm_t alg )
2555{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002556 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002557}
2558
2559psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002560 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002561 psa_algorithm_t alg )
2562{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002563 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002564}
2565
Gilles Peskine8c9def32018-02-08 10:02:12 +01002566psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2567 const uint8_t *input,
2568 size_t input_length )
2569{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002570 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002571 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002572 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002573 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002574 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575 operation->has_input = 1;
2576
Gilles Peskine8c9def32018-02-08 10:02:12 +01002577#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002578 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002579 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002580 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2581 input, input_length );
2582 status = mbedtls_to_psa_error( ret );
2583 }
2584 else
2585#endif /* MBEDTLS_CMAC_C */
2586#if defined(MBEDTLS_MD_C)
2587 if( PSA_ALG_IS_HMAC( operation->alg ) )
2588 {
2589 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2590 input_length );
2591 }
2592 else
2593#endif /* MBEDTLS_MD_C */
2594 {
2595 /* This shouldn't happen if `operation` was initialized by
2596 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002597 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002598 }
2599
Gilles Peskinefbfac682018-07-08 20:51:54 +02002600 if( status != PSA_SUCCESS )
2601 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002602 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603}
2604
Gilles Peskine01126fa2018-07-12 17:04:55 +02002605#if defined(MBEDTLS_MD_C)
2606static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2607 uint8_t *mac,
2608 size_t mac_size )
2609{
2610 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2611 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2612 size_t hash_size = 0;
2613 size_t block_size = psa_get_hash_block_size( hash_alg );
2614 psa_status_t status;
2615
Gilles Peskine01126fa2018-07-12 17:04:55 +02002616 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2617 if( status != PSA_SUCCESS )
2618 return( status );
2619 /* From here on, tmp needs to be wiped. */
2620
2621 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2622 if( status != PSA_SUCCESS )
2623 goto exit;
2624
2625 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2626 if( status != PSA_SUCCESS )
2627 goto exit;
2628
2629 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2630 if( status != PSA_SUCCESS )
2631 goto exit;
2632
Gilles Peskined911eb72018-08-14 15:18:45 +02002633 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2634 if( status != PSA_SUCCESS )
2635 goto exit;
2636
2637 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002638
2639exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002640 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002641 return( status );
2642}
2643#endif /* MBEDTLS_MD_C */
2644
mohammad16036df908f2018-04-02 08:34:15 -07002645static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002646 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002647 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002648{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002649 if( ! operation->key_set )
2650 return( PSA_ERROR_BAD_STATE );
2651 if( operation->iv_required && ! operation->iv_set )
2652 return( PSA_ERROR_BAD_STATE );
2653
Gilles Peskine8c9def32018-02-08 10:02:12 +01002654 if( mac_size < operation->mac_size )
2655 return( PSA_ERROR_BUFFER_TOO_SMALL );
2656
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002658 if( operation->alg == PSA_ALG_CMAC )
2659 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002660 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2661 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2662 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002663 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002664 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002665 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002666 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002667 else
2668#endif /* MBEDTLS_CMAC_C */
2669#if defined(MBEDTLS_MD_C)
2670 if( PSA_ALG_IS_HMAC( operation->alg ) )
2671 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002672 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002673 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002674 }
2675 else
2676#endif /* MBEDTLS_MD_C */
2677 {
2678 /* This shouldn't happen if `operation` was initialized by
2679 * a setup function. */
2680 return( PSA_ERROR_BAD_STATE );
2681 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002682}
2683
Gilles Peskineacd4be32018-07-08 19:56:25 +02002684psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2685 uint8_t *mac,
2686 size_t mac_size,
2687 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002688{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002689 psa_status_t status;
2690
Jaeden Amero252ef282019-02-15 14:05:35 +00002691 if( operation->alg == 0 )
2692 {
2693 return( PSA_ERROR_BAD_STATE );
2694 }
2695
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002696 /* Fill the output buffer with something that isn't a valid mac
2697 * (barring an attack on the mac and deliberately-crafted input),
2698 * in case the caller doesn't check the return status properly. */
2699 *mac_length = mac_size;
2700 /* If mac_size is 0 then mac may be NULL and then the
2701 * call to memset would have undefined behavior. */
2702 if( mac_size != 0 )
2703 memset( mac, '!', mac_size );
2704
Gilles Peskine89167cb2018-07-08 20:12:23 +02002705 if( ! operation->is_sign )
2706 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002707 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002708 }
mohammad16036df908f2018-04-02 08:34:15 -07002709
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002710 status = psa_mac_finish_internal( operation, mac, mac_size );
2711
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002712 if( status == PSA_SUCCESS )
2713 {
2714 status = psa_mac_abort( operation );
2715 if( status == PSA_SUCCESS )
2716 *mac_length = operation->mac_size;
2717 else
2718 memset( mac, '!', mac_size );
2719 }
2720 else
2721 psa_mac_abort( operation );
2722 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002723}
2724
Gilles Peskineacd4be32018-07-08 19:56:25 +02002725psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2726 const uint8_t *mac,
2727 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002728{
Gilles Peskine828ed142018-06-18 23:25:51 +02002729 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002730 psa_status_t status;
2731
Jaeden Amero252ef282019-02-15 14:05:35 +00002732 if( operation->alg == 0 )
2733 {
2734 return( PSA_ERROR_BAD_STATE );
2735 }
2736
Gilles Peskine89167cb2018-07-08 20:12:23 +02002737 if( operation->is_sign )
2738 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002739 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002740 }
2741 if( operation->mac_size != mac_length )
2742 {
2743 status = PSA_ERROR_INVALID_SIGNATURE;
2744 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002745 }
mohammad16036df908f2018-04-02 08:34:15 -07002746
2747 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002748 actual_mac, sizeof( actual_mac ) );
2749
2750 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2751 status = PSA_ERROR_INVALID_SIGNATURE;
2752
2753cleanup:
2754 if( status == PSA_SUCCESS )
2755 status = psa_mac_abort( operation );
2756 else
2757 psa_mac_abort( operation );
2758
Gilles Peskine3f108122018-12-07 18:14:53 +01002759 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002760
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002761 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002762}
2763
2764
Gilles Peskine20035e32018-02-03 22:44:14 +01002765
Gilles Peskine20035e32018-02-03 22:44:14 +01002766/****************************************************************/
2767/* Asymmetric cryptography */
2768/****************************************************************/
2769
Gilles Peskine2b450e32018-06-27 15:42:46 +02002770#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002771/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002772 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002773static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2774 size_t hash_length,
2775 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002776{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002777 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002778 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002779 *md_alg = mbedtls_md_get_type( md_info );
2780
2781 /* The Mbed TLS RSA module uses an unsigned int for hash length
2782 * parameters. Validate that it fits so that we don't risk an
2783 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002784#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002785 if( hash_length > UINT_MAX )
2786 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002787#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002788
2789#if defined(MBEDTLS_PKCS1_V15)
2790 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2791 * must be correct. */
2792 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2793 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002794 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002795 if( md_info == NULL )
2796 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002797 if( mbedtls_md_get_size( md_info ) != hash_length )
2798 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002799 }
2800#endif /* MBEDTLS_PKCS1_V15 */
2801
2802#if defined(MBEDTLS_PKCS1_V21)
2803 /* PSS requires a hash internally. */
2804 if( PSA_ALG_IS_RSA_PSS( alg ) )
2805 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002806 if( md_info == NULL )
2807 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002808 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002809#endif /* MBEDTLS_PKCS1_V21 */
2810
Gilles Peskine61b91d42018-06-08 16:09:36 +02002811 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002812}
2813
Gilles Peskine2b450e32018-06-27 15:42:46 +02002814static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2815 psa_algorithm_t alg,
2816 const uint8_t *hash,
2817 size_t hash_length,
2818 uint8_t *signature,
2819 size_t signature_size,
2820 size_t *signature_length )
2821{
2822 psa_status_t status;
2823 int ret;
2824 mbedtls_md_type_t md_alg;
2825
2826 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2827 if( status != PSA_SUCCESS )
2828 return( status );
2829
Gilles Peskine630a18a2018-06-29 17:49:35 +02002830 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002831 return( PSA_ERROR_BUFFER_TOO_SMALL );
2832
2833#if defined(MBEDTLS_PKCS1_V15)
2834 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2835 {
2836 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2837 MBEDTLS_MD_NONE );
2838 ret = mbedtls_rsa_pkcs1_sign( rsa,
2839 mbedtls_ctr_drbg_random,
2840 &global_data.ctr_drbg,
2841 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002842 md_alg,
2843 (unsigned int) hash_length,
2844 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002845 signature );
2846 }
2847 else
2848#endif /* MBEDTLS_PKCS1_V15 */
2849#if defined(MBEDTLS_PKCS1_V21)
2850 if( PSA_ALG_IS_RSA_PSS( alg ) )
2851 {
2852 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2853 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2854 mbedtls_ctr_drbg_random,
2855 &global_data.ctr_drbg,
2856 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002857 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002858 (unsigned int) hash_length,
2859 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002860 signature );
2861 }
2862 else
2863#endif /* MBEDTLS_PKCS1_V21 */
2864 {
2865 return( PSA_ERROR_INVALID_ARGUMENT );
2866 }
2867
2868 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002869 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002870 return( mbedtls_to_psa_error( ret ) );
2871}
2872
2873static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2874 psa_algorithm_t alg,
2875 const uint8_t *hash,
2876 size_t hash_length,
2877 const uint8_t *signature,
2878 size_t signature_length )
2879{
2880 psa_status_t status;
2881 int ret;
2882 mbedtls_md_type_t md_alg;
2883
2884 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2885 if( status != PSA_SUCCESS )
2886 return( status );
2887
Gilles Peskine630a18a2018-06-29 17:49:35 +02002888 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002889 return( PSA_ERROR_BUFFER_TOO_SMALL );
2890
2891#if defined(MBEDTLS_PKCS1_V15)
2892 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2893 {
2894 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2895 MBEDTLS_MD_NONE );
2896 ret = mbedtls_rsa_pkcs1_verify( rsa,
2897 mbedtls_ctr_drbg_random,
2898 &global_data.ctr_drbg,
2899 MBEDTLS_RSA_PUBLIC,
2900 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002901 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002902 hash,
2903 signature );
2904 }
2905 else
2906#endif /* MBEDTLS_PKCS1_V15 */
2907#if defined(MBEDTLS_PKCS1_V21)
2908 if( PSA_ALG_IS_RSA_PSS( alg ) )
2909 {
2910 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2911 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2912 mbedtls_ctr_drbg_random,
2913 &global_data.ctr_drbg,
2914 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002915 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002916 (unsigned int) hash_length,
2917 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002918 signature );
2919 }
2920 else
2921#endif /* MBEDTLS_PKCS1_V21 */
2922 {
2923 return( PSA_ERROR_INVALID_ARGUMENT );
2924 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002925
2926 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2927 * the rest of the signature is invalid". This has little use in
2928 * practice and PSA doesn't report this distinction. */
2929 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2930 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002931 return( mbedtls_to_psa_error( ret ) );
2932}
2933#endif /* MBEDTLS_RSA_C */
2934
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002935#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002936/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2937 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2938 * (even though these functions don't modify it). */
2939static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2940 psa_algorithm_t alg,
2941 const uint8_t *hash,
2942 size_t hash_length,
2943 uint8_t *signature,
2944 size_t signature_size,
2945 size_t *signature_length )
2946{
2947 int ret;
2948 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002949 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002950 mbedtls_mpi_init( &r );
2951 mbedtls_mpi_init( &s );
2952
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002953 if( signature_size < 2 * curve_bytes )
2954 {
2955 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2956 goto cleanup;
2957 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002958
Gilles Peskinea05219c2018-11-16 16:02:56 +01002959#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002960 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2961 {
2962 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2963 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2964 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2965 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2966 hash, hash_length,
2967 md_alg ) );
2968 }
2969 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002970#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002971 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002972 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002973 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2974 hash, hash_length,
2975 mbedtls_ctr_drbg_random,
2976 &global_data.ctr_drbg ) );
2977 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002978
2979 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2980 signature,
2981 curve_bytes ) );
2982 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2983 signature + curve_bytes,
2984 curve_bytes ) );
2985
2986cleanup:
2987 mbedtls_mpi_free( &r );
2988 mbedtls_mpi_free( &s );
2989 if( ret == 0 )
2990 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002991 return( mbedtls_to_psa_error( ret ) );
2992}
2993
2994static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2995 const uint8_t *hash,
2996 size_t hash_length,
2997 const uint8_t *signature,
2998 size_t signature_length )
2999{
3000 int ret;
3001 mbedtls_mpi r, s;
3002 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3003 mbedtls_mpi_init( &r );
3004 mbedtls_mpi_init( &s );
3005
3006 if( signature_length != 2 * curve_bytes )
3007 return( PSA_ERROR_INVALID_SIGNATURE );
3008
3009 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3010 signature,
3011 curve_bytes ) );
3012 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3013 signature + curve_bytes,
3014 curve_bytes ) );
3015
3016 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3017 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003018
3019cleanup:
3020 mbedtls_mpi_free( &r );
3021 mbedtls_mpi_free( &s );
3022 return( mbedtls_to_psa_error( ret ) );
3023}
3024#endif /* MBEDTLS_ECDSA_C */
3025
Gilles Peskinec5487a82018-12-03 18:08:14 +01003026psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003027 psa_algorithm_t alg,
3028 const uint8_t *hash,
3029 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003030 uint8_t *signature,
3031 size_t signature_size,
3032 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003033{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003034 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003035 psa_status_t status;
3036
3037 *signature_length = signature_size;
3038
Gilles Peskinec5487a82018-12-03 18:08:14 +01003039 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003040 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003041 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003042 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003043 {
3044 status = PSA_ERROR_INVALID_ARGUMENT;
3045 goto exit;
3046 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003047
Gilles Peskine20035e32018-02-03 22:44:14 +01003048#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003049 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003050 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003051 status = psa_rsa_sign( slot->data.rsa,
3052 alg,
3053 hash, hash_length,
3054 signature, signature_size,
3055 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003056 }
3057 else
3058#endif /* defined(MBEDTLS_RSA_C) */
3059#if defined(MBEDTLS_ECP_C)
3060 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3061 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003062#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003063 if(
3064#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3065 PSA_ALG_IS_ECDSA( alg )
3066#else
3067 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3068#endif
3069 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003070 status = psa_ecdsa_sign( slot->data.ecp,
3071 alg,
3072 hash, hash_length,
3073 signature, signature_size,
3074 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003075 else
3076#endif /* defined(MBEDTLS_ECDSA_C) */
3077 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003078 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003079 }
itayzafrir5c753392018-05-08 11:18:38 +03003080 }
3081 else
3082#endif /* defined(MBEDTLS_ECP_C) */
3083 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003084 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003085 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003086
3087exit:
3088 /* Fill the unused part of the output buffer (the whole buffer on error,
3089 * the trailing part on success) with something that isn't a valid mac
3090 * (barring an attack on the mac and deliberately-crafted input),
3091 * in case the caller doesn't check the return status properly. */
3092 if( status == PSA_SUCCESS )
3093 memset( signature + *signature_length, '!',
3094 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003095 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003096 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003097 /* If signature_size is 0 then we have nothing to do. We must not call
3098 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003099 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003100}
3101
Gilles Peskinec5487a82018-12-03 18:08:14 +01003102psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003103 psa_algorithm_t alg,
3104 const uint8_t *hash,
3105 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003106 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003107 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003108{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003109 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003110 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003111
Gilles Peskinec5487a82018-12-03 18:08:14 +01003112 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003113 if( status != PSA_SUCCESS )
3114 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003115
Gilles Peskine61b91d42018-06-08 16:09:36 +02003116#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003117 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003118 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003119 return( psa_rsa_verify( slot->data.rsa,
3120 alg,
3121 hash, hash_length,
3122 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003123 }
3124 else
3125#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003126#if defined(MBEDTLS_ECP_C)
3127 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3128 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003129#if defined(MBEDTLS_ECDSA_C)
3130 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003131 return( psa_ecdsa_verify( slot->data.ecp,
3132 hash, hash_length,
3133 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003134 else
3135#endif /* defined(MBEDTLS_ECDSA_C) */
3136 {
3137 return( PSA_ERROR_INVALID_ARGUMENT );
3138 }
itayzafrir5c753392018-05-08 11:18:38 +03003139 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003140 else
3141#endif /* defined(MBEDTLS_ECP_C) */
3142 {
3143 return( PSA_ERROR_NOT_SUPPORTED );
3144 }
3145}
3146
Gilles Peskine072ac562018-06-30 00:21:29 +02003147#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3148static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3149 mbedtls_rsa_context *rsa )
3150{
3151 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3152 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3153 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3154 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3155}
3156#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3157
Gilles Peskinec5487a82018-12-03 18:08:14 +01003158psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003159 psa_algorithm_t alg,
3160 const uint8_t *input,
3161 size_t input_length,
3162 const uint8_t *salt,
3163 size_t salt_length,
3164 uint8_t *output,
3165 size_t output_size,
3166 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003167{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003168 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003169 psa_status_t status;
3170
Darryl Green5cc689a2018-07-24 15:34:10 +01003171 (void) input;
3172 (void) input_length;
3173 (void) salt;
3174 (void) output;
3175 (void) output_size;
3176
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003177 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003179 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3180 return( PSA_ERROR_INVALID_ARGUMENT );
3181
Gilles Peskinec5487a82018-12-03 18:08:14 +01003182 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003183 if( status != PSA_SUCCESS )
3184 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003185 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003186 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003187 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003188
3189#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003190 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003191 {
3192 mbedtls_rsa_context *rsa = slot->data.rsa;
3193 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003194 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003195 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003196#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003197 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003198 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003199 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3200 mbedtls_ctr_drbg_random,
3201 &global_data.ctr_drbg,
3202 MBEDTLS_RSA_PUBLIC,
3203 input_length,
3204 input,
3205 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003206 }
3207 else
3208#endif /* MBEDTLS_PKCS1_V15 */
3209#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003210 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003211 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003212 psa_rsa_oaep_set_padding_mode( alg, rsa );
3213 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3214 mbedtls_ctr_drbg_random,
3215 &global_data.ctr_drbg,
3216 MBEDTLS_RSA_PUBLIC,
3217 salt, salt_length,
3218 input_length,
3219 input,
3220 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003221 }
3222 else
3223#endif /* MBEDTLS_PKCS1_V21 */
3224 {
3225 return( PSA_ERROR_INVALID_ARGUMENT );
3226 }
3227 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003228 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003229 return( mbedtls_to_psa_error( ret ) );
3230 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003231 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003232#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003233 {
3234 return( PSA_ERROR_NOT_SUPPORTED );
3235 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003236}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237
Gilles Peskinec5487a82018-12-03 18:08:14 +01003238psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003239 psa_algorithm_t alg,
3240 const uint8_t *input,
3241 size_t input_length,
3242 const uint8_t *salt,
3243 size_t salt_length,
3244 uint8_t *output,
3245 size_t output_size,
3246 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003247{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003248 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003249 psa_status_t status;
3250
Darryl Green5cc689a2018-07-24 15:34:10 +01003251 (void) input;
3252 (void) input_length;
3253 (void) salt;
3254 (void) output;
3255 (void) output_size;
3256
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003257 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003258
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003259 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3260 return( PSA_ERROR_INVALID_ARGUMENT );
3261
Gilles Peskinec5487a82018-12-03 18:08:14 +01003262 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003263 if( status != PSA_SUCCESS )
3264 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003265 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003266 return( PSA_ERROR_INVALID_ARGUMENT );
3267
3268#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003269 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003270 {
3271 mbedtls_rsa_context *rsa = slot->data.rsa;
3272 int ret;
3273
Gilles Peskine630a18a2018-06-29 17:49:35 +02003274 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003275 return( PSA_ERROR_INVALID_ARGUMENT );
3276
3277#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003278 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003279 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003280 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3281 mbedtls_ctr_drbg_random,
3282 &global_data.ctr_drbg,
3283 MBEDTLS_RSA_PRIVATE,
3284 output_length,
3285 input,
3286 output,
3287 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003288 }
3289 else
3290#endif /* MBEDTLS_PKCS1_V15 */
3291#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003292 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003293 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003294 psa_rsa_oaep_set_padding_mode( alg, rsa );
3295 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3296 mbedtls_ctr_drbg_random,
3297 &global_data.ctr_drbg,
3298 MBEDTLS_RSA_PRIVATE,
3299 salt, salt_length,
3300 output_length,
3301 input,
3302 output,
3303 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304 }
3305 else
3306#endif /* MBEDTLS_PKCS1_V21 */
3307 {
3308 return( PSA_ERROR_INVALID_ARGUMENT );
3309 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003310
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003311 return( mbedtls_to_psa_error( ret ) );
3312 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003313 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003314#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003315 {
3316 return( PSA_ERROR_NOT_SUPPORTED );
3317 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003318}
Gilles Peskine20035e32018-02-03 22:44:14 +01003319
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003320
3321
mohammad1603503973b2018-03-12 15:59:30 +02003322/****************************************************************/
3323/* Symmetric cryptography */
3324/****************************************************************/
3325
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003326/* Initialize the cipher operation structure. Once this function has been
3327 * called, psa_cipher_abort can run and will do the right thing. */
3328static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3329 psa_algorithm_t alg )
3330{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003331 if( ! PSA_ALG_IS_CIPHER( alg ) )
3332 {
3333 memset( operation, 0, sizeof( *operation ) );
3334 return( PSA_ERROR_INVALID_ARGUMENT );
3335 }
3336
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003337 operation->alg = alg;
3338 operation->key_set = 0;
3339 operation->iv_set = 0;
3340 operation->iv_required = 1;
3341 operation->iv_size = 0;
3342 operation->block_size = 0;
3343 mbedtls_cipher_init( &operation->ctx.cipher );
3344 return( PSA_SUCCESS );
3345}
3346
Gilles Peskinee553c652018-06-04 16:22:46 +02003347static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003348 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003349 psa_algorithm_t alg,
3350 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003351{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003352 int ret = 0;
3353 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003354 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003355 size_t key_bits;
3356 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003357 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3358 PSA_KEY_USAGE_ENCRYPT :
3359 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003360
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003361 /* A context must be freshly initialized before it can be set up. */
3362 if( operation->alg != 0 )
3363 {
3364 return( PSA_ERROR_BAD_STATE );
3365 }
3366
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003367 status = psa_cipher_init( operation, alg );
3368 if( status != PSA_SUCCESS )
3369 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003370
Gilles Peskinec5487a82018-12-03 18:08:14 +01003371 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003372 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003373 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003374 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003375
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003376 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003377 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003378 {
3379 status = PSA_ERROR_NOT_SUPPORTED;
3380 goto exit;
3381 }
mohammad1603503973b2018-03-12 15:59:30 +02003382
mohammad1603503973b2018-03-12 15:59:30 +02003383 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003384 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003385 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003386
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003387#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003388 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003389 {
3390 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3391 unsigned char keys[24];
3392 memcpy( keys, slot->data.raw.data, 16 );
3393 memcpy( keys + 16, slot->data.raw.data, 8 );
3394 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3395 keys,
3396 192, cipher_operation );
3397 }
3398 else
3399#endif
3400 {
3401 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3402 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003403 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003404 }
Moran Peker41deec42018-04-04 15:43:05 +03003405 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003406 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003407
mohammad16038481e742018-03-18 13:57:31 +02003408#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003409 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003410 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003411 case PSA_ALG_CBC_NO_PADDING:
3412 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3413 MBEDTLS_PADDING_NONE );
3414 break;
3415 case PSA_ALG_CBC_PKCS7:
3416 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3417 MBEDTLS_PADDING_PKCS7 );
3418 break;
3419 default:
3420 /* The algorithm doesn't involve padding. */
3421 ret = 0;
3422 break;
3423 }
3424 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003425 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003426#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3427
mohammad1603503973b2018-03-12 15:59:30 +02003428 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003429 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3430 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3431 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003432 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003433 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003434 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003435#if defined(MBEDTLS_CHACHA20_C)
3436 else
3437 if( alg == PSA_ALG_CHACHA20 )
3438 operation->iv_size = 12;
3439#endif
mohammad1603503973b2018-03-12 15:59:30 +02003440
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003441exit:
3442 if( status == 0 )
3443 status = mbedtls_to_psa_error( ret );
3444 if( status != 0 )
3445 psa_cipher_abort( operation );
3446 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003447}
3448
Gilles Peskinefe119512018-07-08 21:39:34 +02003449psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003450 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003451 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003452{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003453 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003454}
3455
Gilles Peskinefe119512018-07-08 21:39:34 +02003456psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003457 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003458 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003459{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003460 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003461}
3462
Gilles Peskinefe119512018-07-08 21:39:34 +02003463psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3464 unsigned char *iv,
3465 size_t iv_size,
3466 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003467{
itayzafrir534bd7c2018-08-02 13:56:32 +03003468 psa_status_t status;
3469 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003470 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003471 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003472 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003473 }
Moran Peker41deec42018-04-04 15:43:05 +03003474 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003475 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003476 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003477 goto exit;
3478 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003479 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3480 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003481 if( ret != 0 )
3482 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003483 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003484 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003485 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003486
mohammad16038481e742018-03-18 13:57:31 +02003487 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003488 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003489
Moran Peker395db872018-05-31 14:07:14 +03003490exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003491 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003492 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003493 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003494}
3495
Gilles Peskinefe119512018-07-08 21:39:34 +02003496psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3497 const unsigned char *iv,
3498 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003499{
itayzafrir534bd7c2018-08-02 13:56:32 +03003500 psa_status_t status;
3501 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003502 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003503 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003504 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003505 }
Moran Pekera28258c2018-05-29 16:25:04 +03003506 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003507 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003508 status = PSA_ERROR_INVALID_ARGUMENT;
3509 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003510 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003511 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3512 status = mbedtls_to_psa_error( ret );
3513exit:
3514 if( status == PSA_SUCCESS )
3515 operation->iv_set = 1;
3516 else
mohammad1603503973b2018-03-12 15:59:30 +02003517 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003518 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003519}
3520
Gilles Peskinee553c652018-06-04 16:22:46 +02003521psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3522 const uint8_t *input,
3523 size_t input_length,
3524 unsigned char *output,
3525 size_t output_size,
3526 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003527{
itayzafrir534bd7c2018-08-02 13:56:32 +03003528 psa_status_t status;
3529 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003530 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003531
3532 if( operation->alg == 0 )
3533 {
3534 return( PSA_ERROR_BAD_STATE );
3535 }
3536
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003537 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003538 {
3539 /* Take the unprocessed partial block left over from previous
3540 * update calls, if any, plus the input to this call. Remove
3541 * the last partial block, if any. You get the data that will be
3542 * output in this call. */
3543 expected_output_size =
3544 ( operation->ctx.cipher.unprocessed_len + input_length )
3545 / operation->block_size * operation->block_size;
3546 }
3547 else
3548 {
3549 expected_output_size = input_length;
3550 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003551
Gilles Peskine89d789c2018-06-04 17:17:16 +02003552 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003553 {
3554 status = PSA_ERROR_BUFFER_TOO_SMALL;
3555 goto exit;
3556 }
mohammad160382759612018-03-12 18:16:40 +02003557
mohammad1603503973b2018-03-12 15:59:30 +02003558 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003559 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003560 status = mbedtls_to_psa_error( ret );
3561exit:
3562 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003563 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003564 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003565}
3566
Gilles Peskinee553c652018-06-04 16:22:46 +02003567psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3568 uint8_t *output,
3569 size_t output_size,
3570 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003571{
David Saadab4ecc272019-02-14 13:48:10 +02003572 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003573 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003574 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003575
mohammad1603503973b2018-03-12 15:59:30 +02003576 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003577 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003578 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003579 }
3580 if( operation->iv_required && ! operation->iv_set )
3581 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003582 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003583 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003584
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003585 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003586 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3587 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003588 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003589 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003590 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003591 }
3592
Janos Follath315b51c2018-07-09 16:04:51 +01003593 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3594 temp_output_buffer,
3595 output_length );
3596 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003597 {
Janos Follath315b51c2018-07-09 16:04:51 +01003598 status = mbedtls_to_psa_error( cipher_ret );
3599 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003600 }
Janos Follath315b51c2018-07-09 16:04:51 +01003601
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003602 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003603 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003604 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003605 memcpy( output, temp_output_buffer, *output_length );
3606 else
3607 {
Janos Follath315b51c2018-07-09 16:04:51 +01003608 status = PSA_ERROR_BUFFER_TOO_SMALL;
3609 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003610 }
mohammad1603503973b2018-03-12 15:59:30 +02003611
Gilles Peskine3f108122018-12-07 18:14:53 +01003612 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003613 status = psa_cipher_abort( operation );
3614
3615 return( status );
3616
3617error:
3618
3619 *output_length = 0;
3620
Gilles Peskine3f108122018-12-07 18:14:53 +01003621 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003622 (void) psa_cipher_abort( operation );
3623
3624 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003625}
3626
Gilles Peskinee553c652018-06-04 16:22:46 +02003627psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3628{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003629 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003630 {
3631 /* The object has (apparently) been initialized but it is not
3632 * in use. It's ok to call abort on such an object, and there's
3633 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003634 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003635 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003636
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003637 /* Sanity check (shouldn't happen: operation->alg should
3638 * always have been initialized to a valid value). */
3639 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3640 return( PSA_ERROR_BAD_STATE );
3641
mohammad1603503973b2018-03-12 15:59:30 +02003642 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003643
Moran Peker41deec42018-04-04 15:43:05 +03003644 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003645 operation->key_set = 0;
3646 operation->iv_set = 0;
3647 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003648 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003649 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003650
Moran Peker395db872018-05-31 14:07:14 +03003651 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003652}
3653
Gilles Peskinea0655c32018-04-30 17:06:50 +02003654
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003655
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003656
mohammad16035955c982018-04-26 00:53:03 +03003657/****************************************************************/
3658/* AEAD */
3659/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003660
Gilles Peskineedf9a652018-08-17 18:11:56 +02003661typedef struct
3662{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003663 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003664 const mbedtls_cipher_info_t *cipher_info;
3665 union
3666 {
3667#if defined(MBEDTLS_CCM_C)
3668 mbedtls_ccm_context ccm;
3669#endif /* MBEDTLS_CCM_C */
3670#if defined(MBEDTLS_GCM_C)
3671 mbedtls_gcm_context gcm;
3672#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003673#if defined(MBEDTLS_CHACHAPOLY_C)
3674 mbedtls_chachapoly_context chachapoly;
3675#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003676 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003677 psa_algorithm_t core_alg;
3678 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003679 uint8_t tag_length;
3680} aead_operation_t;
3681
Gilles Peskine30a9e412019-01-14 18:36:12 +01003682static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003683{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003684 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003685 {
3686#if defined(MBEDTLS_CCM_C)
3687 case PSA_ALG_CCM:
3688 mbedtls_ccm_free( &operation->ctx.ccm );
3689 break;
3690#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003691#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003692 case PSA_ALG_GCM:
3693 mbedtls_gcm_free( &operation->ctx.gcm );
3694 break;
3695#endif /* MBEDTLS_GCM_C */
3696 }
3697}
3698
3699static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003700 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003701 psa_key_usage_t usage,
3702 psa_algorithm_t alg )
3703{
3704 psa_status_t status;
3705 size_t key_bits;
3706 mbedtls_cipher_id_t cipher_id;
3707
Gilles Peskinec5487a82018-12-03 18:08:14 +01003708 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003709 if( status != PSA_SUCCESS )
3710 return( status );
3711
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003712 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003713
3714 operation->cipher_info =
3715 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3716 &cipher_id );
3717 if( operation->cipher_info == NULL )
3718 return( PSA_ERROR_NOT_SUPPORTED );
3719
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003720 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003721 {
3722#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003723 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3724 operation->core_alg = PSA_ALG_CCM;
3725 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003726 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3727 * The call to mbedtls_ccm_encrypt_and_tag or
3728 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003729 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3730 return( PSA_ERROR_INVALID_ARGUMENT );
3731 mbedtls_ccm_init( &operation->ctx.ccm );
3732 status = mbedtls_to_psa_error(
3733 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3734 operation->slot->data.raw.data,
3735 (unsigned int) key_bits ) );
3736 if( status != 0 )
3737 goto cleanup;
3738 break;
3739#endif /* MBEDTLS_CCM_C */
3740
3741#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003742 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3743 operation->core_alg = PSA_ALG_GCM;
3744 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003745 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3746 * The call to mbedtls_gcm_crypt_and_tag or
3747 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003748 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3749 return( PSA_ERROR_INVALID_ARGUMENT );
3750 mbedtls_gcm_init( &operation->ctx.gcm );
3751 status = mbedtls_to_psa_error(
3752 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3753 operation->slot->data.raw.data,
3754 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003755 if( status != 0 )
3756 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003757 break;
3758#endif /* MBEDTLS_GCM_C */
3759
Gilles Peskine26869f22019-05-06 15:25:00 +02003760#if defined(MBEDTLS_CHACHAPOLY_C)
3761 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3762 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3763 operation->full_tag_length = 16;
3764 /* We only support the default tag length. */
3765 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3766 return( PSA_ERROR_NOT_SUPPORTED );
3767 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3768 status = mbedtls_to_psa_error(
3769 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3770 operation->slot->data.raw.data ) );
3771 if( status != 0 )
3772 goto cleanup;
3773 break;
3774#endif /* MBEDTLS_CHACHAPOLY_C */
3775
Gilles Peskineedf9a652018-08-17 18:11:56 +02003776 default:
3777 return( PSA_ERROR_NOT_SUPPORTED );
3778 }
3779
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003780 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3781 {
3782 status = PSA_ERROR_INVALID_ARGUMENT;
3783 goto cleanup;
3784 }
3785 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003786
Gilles Peskineedf9a652018-08-17 18:11:56 +02003787 return( PSA_SUCCESS );
3788
3789cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003790 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003791 return( status );
3792}
3793
Gilles Peskinec5487a82018-12-03 18:08:14 +01003794psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003795 psa_algorithm_t alg,
3796 const uint8_t *nonce,
3797 size_t nonce_length,
3798 const uint8_t *additional_data,
3799 size_t additional_data_length,
3800 const uint8_t *plaintext,
3801 size_t plaintext_length,
3802 uint8_t *ciphertext,
3803 size_t ciphertext_size,
3804 size_t *ciphertext_length )
3805{
mohammad16035955c982018-04-26 00:53:03 +03003806 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003807 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003808 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003809
mohammad1603f08a5502018-06-03 15:05:47 +03003810 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003811
Gilles Peskinec5487a82018-12-03 18:08:14 +01003812 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003813 if( status != PSA_SUCCESS )
3814 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003815
Gilles Peskineedf9a652018-08-17 18:11:56 +02003816 /* For all currently supported modes, the tag is at the end of the
3817 * ciphertext. */
3818 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3819 {
3820 status = PSA_ERROR_BUFFER_TOO_SMALL;
3821 goto exit;
3822 }
3823 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003824
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003825#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003826 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003827 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003828 status = mbedtls_to_psa_error(
3829 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3830 MBEDTLS_GCM_ENCRYPT,
3831 plaintext_length,
3832 nonce, nonce_length,
3833 additional_data, additional_data_length,
3834 plaintext, ciphertext,
3835 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003836 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003837 else
3838#endif /* MBEDTLS_GCM_C */
3839#if defined(MBEDTLS_CCM_C)
3840 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003841 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003842 status = mbedtls_to_psa_error(
3843 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3844 plaintext_length,
3845 nonce, nonce_length,
3846 additional_data,
3847 additional_data_length,
3848 plaintext, ciphertext,
3849 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003850 }
mohammad16035c8845f2018-05-09 05:40:09 -07003851 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003852#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003853#if defined(MBEDTLS_CHACHAPOLY_C)
3854 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3855 {
3856 if( nonce_length != 12 || operation.tag_length != 16 )
3857 {
3858 status = PSA_ERROR_NOT_SUPPORTED;
3859 goto exit;
3860 }
3861 status = mbedtls_to_psa_error(
3862 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3863 plaintext_length,
3864 nonce,
3865 additional_data,
3866 additional_data_length,
3867 plaintext,
3868 ciphertext,
3869 tag ) );
3870 }
3871 else
3872#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003873 {
mohammad1603554faad2018-06-03 15:07:38 +03003874 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003875 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003876
Gilles Peskineedf9a652018-08-17 18:11:56 +02003877 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3878 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003879
Gilles Peskineedf9a652018-08-17 18:11:56 +02003880exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003881 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003882 if( status == PSA_SUCCESS )
3883 *ciphertext_length = plaintext_length + operation.tag_length;
3884 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003885}
3886
Gilles Peskineee652a32018-06-01 19:23:52 +02003887/* Locate the tag in a ciphertext buffer containing the encrypted data
3888 * followed by the tag. Return the length of the part preceding the tag in
3889 * *plaintext_length. This is the size of the plaintext in modes where
3890 * the encrypted data has the same size as the plaintext, such as
3891 * CCM and GCM. */
3892static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3893 const uint8_t *ciphertext,
3894 size_t ciphertext_length,
3895 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003896 const uint8_t **p_tag )
3897{
3898 size_t payload_length;
3899 if( tag_length > ciphertext_length )
3900 return( PSA_ERROR_INVALID_ARGUMENT );
3901 payload_length = ciphertext_length - tag_length;
3902 if( payload_length > plaintext_size )
3903 return( PSA_ERROR_BUFFER_TOO_SMALL );
3904 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003905 return( PSA_SUCCESS );
3906}
3907
Gilles Peskinec5487a82018-12-03 18:08:14 +01003908psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003909 psa_algorithm_t alg,
3910 const uint8_t *nonce,
3911 size_t nonce_length,
3912 const uint8_t *additional_data,
3913 size_t additional_data_length,
3914 const uint8_t *ciphertext,
3915 size_t ciphertext_length,
3916 uint8_t *plaintext,
3917 size_t plaintext_size,
3918 size_t *plaintext_length )
3919{
mohammad16035955c982018-04-26 00:53:03 +03003920 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003921 aead_operation_t operation;
3922 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003923
Gilles Peskineee652a32018-06-01 19:23:52 +02003924 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003925
Gilles Peskinec5487a82018-12-03 18:08:14 +01003926 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003927 if( status != PSA_SUCCESS )
3928 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003929
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003930 status = psa_aead_unpadded_locate_tag( operation.tag_length,
3931 ciphertext, ciphertext_length,
3932 plaintext_size, &tag );
3933 if( status != PSA_SUCCESS )
3934 goto exit;
3935
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003936#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003937 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003938 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003939 status = mbedtls_to_psa_error(
3940 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3941 ciphertext_length - operation.tag_length,
3942 nonce, nonce_length,
3943 additional_data,
3944 additional_data_length,
3945 tag, operation.tag_length,
3946 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003947 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003948 else
3949#endif /* MBEDTLS_GCM_C */
3950#if defined(MBEDTLS_CCM_C)
3951 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003952 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003953 status = mbedtls_to_psa_error(
3954 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3955 ciphertext_length - operation.tag_length,
3956 nonce, nonce_length,
3957 additional_data,
3958 additional_data_length,
3959 ciphertext, plaintext,
3960 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003961 }
mohammad160339574652018-06-01 04:39:53 -07003962 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003963#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003964#if defined(MBEDTLS_CHACHAPOLY_C)
3965 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3966 {
3967 if( nonce_length != 12 || operation.tag_length != 16 )
3968 {
3969 status = PSA_ERROR_NOT_SUPPORTED;
3970 goto exit;
3971 }
3972 status = mbedtls_to_psa_error(
3973 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
3974 ciphertext_length - operation.tag_length,
3975 nonce,
3976 additional_data,
3977 additional_data_length,
3978 tag,
3979 ciphertext,
3980 plaintext ) );
3981 }
3982 else
3983#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07003984 {
mohammad1603554faad2018-06-03 15:07:38 +03003985 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003986 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003987
Gilles Peskineedf9a652018-08-17 18:11:56 +02003988 if( status != PSA_SUCCESS && plaintext_size != 0 )
3989 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003990
Gilles Peskineedf9a652018-08-17 18:11:56 +02003991exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003992 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003993 if( status == PSA_SUCCESS )
3994 *plaintext_length = ciphertext_length - operation.tag_length;
3995 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003996}
3997
Gilles Peskinea0655c32018-04-30 17:06:50 +02003998
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003999
Gilles Peskine20035e32018-02-03 22:44:14 +01004000/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004001/* Generators */
4002/****************************************************************/
4003
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004004#define HKDF_STATE_INIT 0 /* no input yet */
4005#define HKDF_STATE_STARTED 1 /* got salt */
4006#define HKDF_STATE_KEYED 2 /* got key */
4007#define HKDF_STATE_OUTPUT 3 /* output started */
4008
Gilles Peskinecbe66502019-05-16 16:59:18 +02004009static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004010 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004011{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004012 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4013 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004014 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004015 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004016}
4017
4018
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004019psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004020{
4021 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004022 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004023 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004024 {
4025 /* The object has (apparently) been initialized but it is not
4026 * in use. It's ok to call abort on such an object, and there's
4027 * nothing to do. */
4028 }
4029 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004030#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004031 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004032 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004033 mbedtls_free( operation->ctx.hkdf.info );
4034 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004035 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004036 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004037 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004038 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004039 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004040#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004041 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004042 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004043 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4044 operation->ctx.tls12_prf.key_len );
4045 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004046 }
Hanno Becker580fba12018-11-13 20:50:45 +00004047
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004048 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004049 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004050 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4051 operation->ctx.tls12_prf.Ai_with_seed_len );
4052 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004053 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004054#else
4055 if( operation->ctx.tls12_prf.seed != NULL )
4056 {
4057 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4058 operation->ctx.tls12_prf.seed_length );
4059 mbedtls_free( operation->ctx.tls12_prf.seed );
4060 }
4061
4062 if( operation->ctx.tls12_prf.label != NULL )
4063 {
4064 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4065 operation->ctx.tls12_prf.label_length );
4066 mbedtls_free( operation->ctx.tls12_prf.label );
4067 }
4068
4069 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4070
4071 /* We leave the fields Ai and output_block to be erased safely by the
4072 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004073#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004074 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004075 else
4076#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004077 {
4078 status = PSA_ERROR_BAD_STATE;
4079 }
Janos Follath083036a2019-06-11 10:22:26 +01004080 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004081 return( status );
4082}
4083
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004084psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004085 size_t *capacity)
4086{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004087 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004088 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004089 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004090 return PSA_ERROR_BAD_STATE;
4091 }
4092
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004093 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004094 return( PSA_SUCCESS );
4095}
4096
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004097psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004098 size_t capacity )
4099{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004100 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004101 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004102 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004103 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004104 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004105 return( PSA_SUCCESS );
4106}
4107
Gilles Peskinebef7f142018-07-12 17:22:21 +02004108#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004109/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004110 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004111static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004112 psa_algorithm_t hash_alg,
4113 uint8_t *output,
4114 size_t output_length )
4115{
4116 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4117 psa_status_t status;
4118
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004119 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4120 return( PSA_ERROR_BAD_STATE );
4121 hkdf->state = HKDF_STATE_OUTPUT;
4122
Gilles Peskinebef7f142018-07-12 17:22:21 +02004123 while( output_length != 0 )
4124 {
4125 /* Copy what remains of the current block */
4126 uint8_t n = hash_length - hkdf->offset_in_block;
4127 if( n > output_length )
4128 n = (uint8_t) output_length;
4129 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4130 output += n;
4131 output_length -= n;
4132 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004133 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004134 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004135 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004136 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004137 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004138 * object was corrupted or if this function is called directly
4139 * inside the library. */
4140 if( hkdf->block_number == 0xff )
4141 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004142
4143 /* We need a new block */
4144 ++hkdf->block_number;
4145 hkdf->offset_in_block = 0;
4146 status = psa_hmac_setup_internal( &hkdf->hmac,
4147 hkdf->prk, hash_length,
4148 hash_alg );
4149 if( status != PSA_SUCCESS )
4150 return( status );
4151 if( hkdf->block_number != 1 )
4152 {
4153 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4154 hkdf->output_block,
4155 hash_length );
4156 if( status != PSA_SUCCESS )
4157 return( status );
4158 }
4159 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4160 hkdf->info,
4161 hkdf->info_length );
4162 if( status != PSA_SUCCESS )
4163 return( status );
4164 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4165 &hkdf->block_number, 1 );
4166 if( status != PSA_SUCCESS )
4167 return( status );
4168 status = psa_hmac_finish_internal( &hkdf->hmac,
4169 hkdf->output_block,
4170 sizeof( hkdf->output_block ) );
4171 if( status != PSA_SUCCESS )
4172 return( status );
4173 }
4174
4175 return( PSA_SUCCESS );
4176}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004177
Janos Follath999f6482019-06-11 12:04:10 +01004178#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004179static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4180 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004181 psa_algorithm_t alg )
4182{
4183 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4184 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4185 psa_hmac_internal_data hmac;
4186 psa_status_t status, cleanup_status;
4187
Hanno Becker3b339e22018-11-13 20:56:14 +00004188 unsigned char *Ai;
4189 size_t Ai_len;
4190
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004191 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004192 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004193 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004194 * object was corrupted or if this function is called directly
4195 * inside the library. */
4196 if( tls12_prf->block_number == 0xff )
4197 return( PSA_ERROR_BAD_STATE );
4198
4199 /* We need a new block */
4200 ++tls12_prf->block_number;
4201 tls12_prf->offset_in_block = 0;
4202
4203 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4204 *
4205 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4206 *
4207 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4208 * HMAC_hash(secret, A(2) + seed) +
4209 * HMAC_hash(secret, A(3) + seed) + ...
4210 *
4211 * A(0) = seed
4212 * A(i) = HMAC_hash( secret, A(i-1) )
4213 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004214 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004215 * `HMAC_hash(secret, A(i) + seed)` from which the output
4216 * is currently extracted as `output_block`, while
4217 * `A(i) + seed` is stored in `Ai_with_seed`.
4218 *
4219 * Generating a new block means recalculating `Ai_with_seed`
4220 * from the A(i)-part of it, and afterwards recalculating
4221 * `output_block`.
4222 *
4223 * A(0) is computed at setup time.
4224 *
4225 */
4226
4227 psa_hmac_init_internal( &hmac );
4228
4229 /* We must distinguish the calculation of A(1) from those
4230 * of A(2) and higher, because A(0)=seed has a different
4231 * length than the other A(i). */
4232 if( tls12_prf->block_number == 1 )
4233 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004234 Ai = tls12_prf->Ai_with_seed + hash_length;
4235 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004236 }
4237 else
4238 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004239 Ai = tls12_prf->Ai_with_seed;
4240 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004241 }
4242
Hanno Becker3b339e22018-11-13 20:56:14 +00004243 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4244 status = psa_hmac_setup_internal( &hmac,
4245 tls12_prf->key,
4246 tls12_prf->key_len,
4247 hash_alg );
4248 if( status != PSA_SUCCESS )
4249 goto cleanup;
4250
4251 status = psa_hash_update( &hmac.hash_ctx,
4252 Ai, Ai_len );
4253 if( status != PSA_SUCCESS )
4254 goto cleanup;
4255
4256 status = psa_hmac_finish_internal( &hmac,
4257 tls12_prf->Ai_with_seed,
4258 hash_length );
4259 if( status != PSA_SUCCESS )
4260 goto cleanup;
4261
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004262 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4263 status = psa_hmac_setup_internal( &hmac,
4264 tls12_prf->key,
4265 tls12_prf->key_len,
4266 hash_alg );
4267 if( status != PSA_SUCCESS )
4268 goto cleanup;
4269
4270 status = psa_hash_update( &hmac.hash_ctx,
4271 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004272 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004273 if( status != PSA_SUCCESS )
4274 goto cleanup;
4275
4276 status = psa_hmac_finish_internal( &hmac,
4277 tls12_prf->output_block,
4278 hash_length );
4279 if( status != PSA_SUCCESS )
4280 goto cleanup;
4281
4282cleanup:
4283
4284 cleanup_status = psa_hmac_abort_internal( &hmac );
4285 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4286 status = cleanup_status;
4287
4288 return( status );
4289}
Janos Follath7742fee2019-06-17 12:58:10 +01004290#else
4291static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4292 psa_tls12_prf_key_derivation_t *tls12_prf,
4293 psa_algorithm_t alg )
4294{
4295 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4296 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004297 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4298 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004299
Janos Follath7742fee2019-06-17 12:58:10 +01004300 /* We can't be wanting more output after block 0xff, otherwise
4301 * the capacity check in psa_key_derivation_output_bytes() would have
4302 * prevented this call. It could happen only if the operation
4303 * object was corrupted or if this function is called directly
4304 * inside the library. */
4305 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004306 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004307
4308 /* We need a new block */
4309 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004310 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004311
4312 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4313 *
4314 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4315 *
4316 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4317 * HMAC_hash(secret, A(2) + seed) +
4318 * HMAC_hash(secret, A(3) + seed) + ...
4319 *
4320 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004321 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004322 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004323 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004324 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004325 * is currently extracted as `output_block` and where i is
4326 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004327 */
4328
Janos Follathea29bfb2019-06-19 12:21:20 +01004329 /* Save the hash context before using it, to preserve the hash state with
4330 * only the inner padding in it. We need this, because inner padding depends
4331 * on the key (secret in the RFC's terminology). */
4332 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4333 if( status != PSA_SUCCESS )
4334 goto cleanup;
4335
4336 /* Calculate A(i) where i = tls12_prf->block_number. */
4337 if( tls12_prf->block_number == 1 )
4338 {
4339 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4340 * the variable seed and in this instance means it in the context of the
4341 * P_hash function, where seed = label + seed.) */
4342 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4343 tls12_prf->label, tls12_prf->label_length );
4344 if( status != PSA_SUCCESS )
4345 goto cleanup;
4346 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4347 tls12_prf->seed, tls12_prf->seed_length );
4348 if( status != PSA_SUCCESS )
4349 goto cleanup;
4350 }
4351 else
4352 {
4353 /* A(i) = HMAC_hash(secret, A(i-1)) */
4354 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4355 tls12_prf->Ai, hash_length );
4356 if( status != PSA_SUCCESS )
4357 goto cleanup;
4358 }
4359
4360 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4361 tls12_prf->Ai, hash_length );
4362 if( status != PSA_SUCCESS )
4363 goto cleanup;
4364 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4365 if( status != PSA_SUCCESS )
4366 goto cleanup;
4367
4368 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4369 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4370 tls12_prf->Ai, hash_length );
4371 if( status != PSA_SUCCESS )
4372 goto cleanup;
4373 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4374 tls12_prf->label, tls12_prf->label_length );
4375 if( status != PSA_SUCCESS )
4376 goto cleanup;
4377 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4378 tls12_prf->seed, tls12_prf->seed_length );
4379 if( status != PSA_SUCCESS )
4380 goto cleanup;
4381 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4382 tls12_prf->output_block, hash_length );
4383 if( status != PSA_SUCCESS )
4384 goto cleanup;
4385 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4386 if( status != PSA_SUCCESS )
4387 goto cleanup;
4388
Janos Follath7742fee2019-06-17 12:58:10 +01004389
4390cleanup:
4391
Janos Follathea29bfb2019-06-19 12:21:20 +01004392 cleanup_status = psa_hash_abort( &backup );
4393 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4394 status = cleanup_status;
4395
4396 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004397}
Janos Follath999f6482019-06-11 12:04:10 +01004398#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004399
Janos Follath999f6482019-06-11 12:04:10 +01004400#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004401/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004402 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004403static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004404 psa_tls12_prf_key_derivation_t *tls12_prf,
4405 psa_algorithm_t alg,
4406 uint8_t *output,
4407 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004408{
4409 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4410 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4411 psa_status_t status;
4412
4413 while( output_length != 0 )
4414 {
4415 /* Copy what remains of the current block */
4416 uint8_t n = hash_length - tls12_prf->offset_in_block;
4417
4418 /* Check if we have fully processed the current block. */
4419 if( n == 0 )
4420 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004421 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004422 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004423 if( status != PSA_SUCCESS )
4424 return( status );
4425
4426 continue;
4427 }
4428
4429 if( n > output_length )
4430 n = (uint8_t) output_length;
4431 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4432 n );
4433 output += n;
4434 output_length -= n;
4435 tls12_prf->offset_in_block += n;
4436 }
4437
4438 return( PSA_SUCCESS );
4439}
Janos Follath844eb0e2019-06-19 12:10:49 +01004440#else
4441static psa_status_t psa_key_derivation_tls12_prf_read(
4442 psa_tls12_prf_key_derivation_t *tls12_prf,
4443 psa_algorithm_t alg,
4444 uint8_t *output,
4445 size_t output_length )
4446{
4447 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4448 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4449 psa_status_t status;
4450 uint8_t offset, length;
4451
4452 while( output_length != 0 )
4453 {
4454 /* Check if we have fully processed the current block. */
4455 if( tls12_prf->left_in_block == 0 )
4456 {
4457 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4458 alg );
4459 if( status != PSA_SUCCESS )
4460 return( status );
4461
4462 continue;
4463 }
4464
4465 if( tls12_prf->left_in_block > output_length )
4466 length = (uint8_t) output_length;
4467 else
4468 length = tls12_prf->left_in_block;
4469
4470 offset = hash_length - tls12_prf->left_in_block;
4471 memcpy( output, tls12_prf->output_block + offset, length );
4472 output += length;
4473 output_length -= length;
4474 tls12_prf->left_in_block -= length;
4475 }
4476
4477 return( PSA_SUCCESS );
4478}
Janos Follath999f6482019-06-11 12:04:10 +01004479#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004480#endif /* MBEDTLS_MD_C */
4481
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004482psa_status_t psa_key_derivation_output_bytes(
4483 psa_key_derivation_operation_t *operation,
4484 uint8_t *output,
4485 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004486{
4487 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004488 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004489
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004491 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004492 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004493 return PSA_ERROR_BAD_STATE;
4494 }
4495
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004496 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004497 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004498 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004499 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004500 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004501 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004502 goto exit;
4503 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004504 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004505 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004506 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004507 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004508 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4509 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004510 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004511 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004512 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004513 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004514 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004515
Gilles Peskinebef7f142018-07-12 17:22:21 +02004516#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004517 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004518 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004519 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004520 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004521 output, output_length );
4522 }
Janos Follathadbec812019-06-14 11:05:39 +01004523 else
Janos Follathadbec812019-06-14 11:05:39 +01004524 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004525 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004526 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004527 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004528 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004529 output_length );
4530 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004531 else
4532#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004533 {
4534 return( PSA_ERROR_BAD_STATE );
4535 }
4536
4537exit:
4538 if( status != PSA_SUCCESS )
4539 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004540 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004541 * This allows us to differentiate between exhausted operations and
4542 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4543 * operations. */
4544 psa_algorithm_t alg = operation->alg;
4545 psa_key_derivation_abort( operation );
4546 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004547 memset( output, '!', output_length );
4548 }
4549 return( status );
4550}
4551
Gilles Peskine08542d82018-07-19 17:05:42 +02004552#if defined(MBEDTLS_DES_C)
4553static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4554{
4555 if( data_size >= 8 )
4556 mbedtls_des_key_set_parity( data );
4557 if( data_size >= 16 )
4558 mbedtls_des_key_set_parity( data + 8 );
4559 if( data_size >= 24 )
4560 mbedtls_des_key_set_parity( data + 16 );
4561}
4562#endif /* MBEDTLS_DES_C */
4563
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004564static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004565 psa_key_slot_t *slot,
4566 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004567 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004568{
4569 uint8_t *data = NULL;
4570 size_t bytes = PSA_BITS_TO_BYTES( bits );
4571 psa_status_t status;
4572
4573 if( ! key_type_is_raw_bytes( slot->type ) )
4574 return( PSA_ERROR_INVALID_ARGUMENT );
4575 if( bits % 8 != 0 )
4576 return( PSA_ERROR_INVALID_ARGUMENT );
4577 data = mbedtls_calloc( 1, bytes );
4578 if( data == NULL )
4579 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4580
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004581 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004582 if( status != PSA_SUCCESS )
4583 goto exit;
4584#if defined(MBEDTLS_DES_C)
4585 if( slot->type == PSA_KEY_TYPE_DES )
4586 psa_des_set_key_parity( data, bytes );
4587#endif /* MBEDTLS_DES_C */
4588 status = psa_import_key_into_slot( slot, data, bytes );
4589
4590exit:
4591 mbedtls_free( data );
4592 return( status );
4593}
4594
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004595psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004596 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004597 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004598{
4599 psa_status_t status;
4600 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004601 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004602 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004603 if( status == PSA_SUCCESS )
4604 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004605 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004606 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004607 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004608 }
4609 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004610 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004611 if( status != PSA_SUCCESS )
4612 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004613 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004614 *handle = 0;
4615 }
4616 return( status );
4617}
4618
Gilles Peskineeab56e42018-07-12 17:12:33 +02004619
4620
4621/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004622/* Key derivation */
4623/****************************************************************/
4624
Gilles Peskinea05219c2018-11-16 16:02:56 +01004625#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004626#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004627/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004628 * of the HKDF algorithm.
4629 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004630 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004631 * to potentially free embedded data structures and wipe confidential data.
4632 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004633static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004634 const uint8_t *secret,
4635 size_t secret_length,
4636 psa_algorithm_t hash_alg,
4637 const uint8_t *salt,
4638 size_t salt_length,
4639 const uint8_t *label,
4640 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004641{
4642 psa_status_t status;
4643 status = psa_hmac_setup_internal( &hkdf->hmac,
4644 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004645 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004646 if( status != PSA_SUCCESS )
4647 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004648 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004649 if( status != PSA_SUCCESS )
4650 return( status );
4651 status = psa_hmac_finish_internal( &hkdf->hmac,
4652 hkdf->prk,
4653 sizeof( hkdf->prk ) );
4654 if( status != PSA_SUCCESS )
4655 return( status );
4656 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4657 hkdf->block_number = 0;
4658 hkdf->info_length = label_length;
4659 if( label_length != 0 )
4660 {
4661 hkdf->info = mbedtls_calloc( 1, label_length );
4662 if( hkdf->info == NULL )
4663 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4664 memcpy( hkdf->info, label, label_length );
4665 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004666 hkdf->state = HKDF_STATE_KEYED;
4667 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004668 return( PSA_SUCCESS );
4669}
Janos Follath71a4c912019-06-11 09:14:47 +01004670#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004671#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004672
Gilles Peskinea05219c2018-11-16 16:02:56 +01004673#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004674#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004676 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004677 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004678 * to potentially free embedded data structures and wipe confidential data.
4679 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004680static psa_status_t psa_key_derivation_tls12_prf_setup(
4681 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004682 const unsigned char *key,
4683 size_t key_len,
4684 psa_algorithm_t hash_alg,
4685 const uint8_t *salt,
4686 size_t salt_length,
4687 const uint8_t *label,
4688 size_t label_length )
4689{
4690 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004691 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4692 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004693
4694 tls12_prf->key = mbedtls_calloc( 1, key_len );
4695 if( tls12_prf->key == NULL )
4696 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4697 tls12_prf->key_len = key_len;
4698 memcpy( tls12_prf->key, key, key_len );
4699
Hanno Becker580fba12018-11-13 20:50:45 +00004700 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004701 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004702 if( overflow )
4703 return( PSA_ERROR_INVALID_ARGUMENT );
4704
4705 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4706 if( tls12_prf->Ai_with_seed == NULL )
4707 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4708 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4709
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004710 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4711 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004712 if( label_length != 0 )
4713 {
4714 memcpy( tls12_prf->Ai_with_seed + hash_length,
4715 label, label_length );
4716 }
4717
4718 if( salt_length != 0 )
4719 {
4720 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4721 salt, salt_length );
4722 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004723
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004724 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004725 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004726 tls12_prf->block_number = 0;
4727 tls12_prf->offset_in_block = hash_length;
4728
4729 return( PSA_SUCCESS );
4730}
Janos Follath71a4c912019-06-11 09:14:47 +01004731#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004732
Janos Follath71a4c912019-06-11 09:14:47 +01004733#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004735static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4736 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004737 const unsigned char *psk,
4738 size_t psk_len,
4739 psa_algorithm_t hash_alg,
4740 const uint8_t *salt,
4741 size_t salt_length,
4742 const uint8_t *label,
4743 size_t label_length )
4744{
4745 psa_status_t status;
4746 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4747
4748 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4749 return( PSA_ERROR_INVALID_ARGUMENT );
4750
4751 /* Quoting RFC 4279, Section 2:
4752 *
4753 * The premaster secret is formed as follows: if the PSK is N octets
4754 * long, concatenate a uint16 with the value N, N zero octets, a second
4755 * uint16 with the value N, and the PSK itself.
4756 */
4757
4758 pms[0] = ( psk_len >> 8 ) & 0xff;
4759 pms[1] = ( psk_len >> 0 ) & 0xff;
4760 memset( pms + 2, 0, psk_len );
4761 pms[2 + psk_len + 0] = pms[0];
4762 pms[2 + psk_len + 1] = pms[1];
4763 memcpy( pms + 4 + psk_len, psk, psk_len );
4764
Gilles Peskinecbe66502019-05-16 16:59:18 +02004765 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004766 pms, 4 + 2 * psk_len,
4767 hash_alg,
4768 salt, salt_length,
4769 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004770
Gilles Peskine3f108122018-12-07 18:14:53 +01004771 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004772 return( status );
4773}
Janos Follath71a4c912019-06-11 09:14:47 +01004774#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004775#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004776
Janos Follath71a4c912019-06-11 09:14:47 +01004777#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004778/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004779 * to potentially free embedded data structures and wipe confidential data.
4780 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004781static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004782 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004783 const uint8_t *secret, size_t secret_length,
4784 psa_algorithm_t alg,
4785 const uint8_t *salt, size_t salt_length,
4786 const uint8_t *label, size_t label_length,
4787 size_t capacity )
4788{
4789 psa_status_t status;
4790 size_t max_capacity;
4791
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004792 /* Set operation->alg even on failure so that abort knows what to do. */
4793 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004794
4795#if defined(MBEDTLS_MD_C)
4796 if( PSA_ALG_IS_HKDF( alg ) )
4797 {
4798 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4799 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4800 if( hash_size == 0 )
4801 return( PSA_ERROR_NOT_SUPPORTED );
4802 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004803 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004804 secret, secret_length,
4805 hash_alg,
4806 salt, salt_length,
4807 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004808 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004809 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4810 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4811 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004812 {
4813 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4814 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4815
4816 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4817 if( hash_alg != PSA_ALG_SHA_256 &&
4818 hash_alg != PSA_ALG_SHA_384 )
4819 {
4820 return( PSA_ERROR_NOT_SUPPORTED );
4821 }
4822
4823 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004824
4825 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4826 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004827 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004828 secret, secret_length,
4829 hash_alg, salt, salt_length,
4830 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004831 }
4832 else
4833 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004834 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004835 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004836 secret, secret_length,
4837 hash_alg, salt, salt_length,
4838 label, label_length );
4839 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004840 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004841 else
4842#endif
4843 {
4844 return( PSA_ERROR_NOT_SUPPORTED );
4845 }
4846
4847 if( status != PSA_SUCCESS )
4848 return( status );
4849
4850 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004851 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004852 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004853 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004854 else
4855 return( PSA_ERROR_INVALID_ARGUMENT );
4856
4857 return( PSA_SUCCESS );
4858}
Janos Follath71a4c912019-06-11 09:14:47 +01004859#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004860
Janos Follath71a4c912019-06-11 09:14:47 +01004861#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004862psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004863 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004864 psa_algorithm_t alg,
4865 const uint8_t *salt,
4866 size_t salt_length,
4867 const uint8_t *label,
4868 size_t label_length,
4869 size_t capacity )
4870{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004871 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004872 psa_status_t status;
4873
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004874 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004875 return( PSA_ERROR_BAD_STATE );
4876
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004877 /* Make sure that alg is a key derivation algorithm. This prevents
4878 * key selection algorithms, which psa_key_derivation_internal
4879 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004880 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4881 return( PSA_ERROR_INVALID_ARGUMENT );
4882
Gilles Peskinec5487a82018-12-03 18:08:14 +01004883 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004884 if( status != PSA_SUCCESS )
4885 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004886
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004887 if( slot->type != PSA_KEY_TYPE_DERIVE )
4888 return( PSA_ERROR_INVALID_ARGUMENT );
4889
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004890 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004891 slot->data.raw.data,
4892 slot->data.raw.bytes,
4893 alg,
4894 salt, salt_length,
4895 label, label_length,
4896 capacity );
4897 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004898 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004899 return( status );
4900}
Janos Follath71a4c912019-06-11 09:14:47 +01004901#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004902
Gilles Peskine969c5d62019-01-16 15:53:06 +01004903static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004904 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004905 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004906{
Janos Follath5fe19732019-06-20 15:09:30 +01004907 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4908 * initialisers for this union leave some bytes unspecified.) */
4909 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4910
Gilles Peskine969c5d62019-01-16 15:53:06 +01004911 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004912#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004913 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4914 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4915 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004916 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004917 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004918 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4919 if( hash_size == 0 )
4920 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004921 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4922 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004923 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004924 {
4925 return( PSA_ERROR_NOT_SUPPORTED );
4926 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004927 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004928 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004929 }
4930#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004931 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004932 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004933}
4934
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004935psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004936 psa_algorithm_t alg )
4937{
4938 psa_status_t status;
4939
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004940 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004941 return( PSA_ERROR_BAD_STATE );
4942
Gilles Peskine6843c292019-01-18 16:44:49 +01004943 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4944 return( PSA_ERROR_INVALID_ARGUMENT );
4945 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004946 {
4947 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004948 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004949 }
4950 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4951 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004952 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004953 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004954 else
4955 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004956
4957 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004958 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004959 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004960}
4961
4962#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004963static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004964 psa_algorithm_t hash_alg,
4965 psa_key_derivation_step_t step,
4966 const uint8_t *data,
4967 size_t data_length )
4968{
4969 psa_status_t status;
4970 switch( step )
4971 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004972 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004973 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004974 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004975 status = psa_hmac_setup_internal( &hkdf->hmac,
4976 data, data_length,
4977 hash_alg );
4978 if( status != PSA_SUCCESS )
4979 return( status );
4980 hkdf->state = HKDF_STATE_STARTED;
4981 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004982 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004983 /* If no salt was provided, use an empty salt. */
4984 if( hkdf->state == HKDF_STATE_INIT )
4985 {
4986 status = psa_hmac_setup_internal( &hkdf->hmac,
4987 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004988 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004989 if( status != PSA_SUCCESS )
4990 return( status );
4991 hkdf->state = HKDF_STATE_STARTED;
4992 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004993 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004994 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004995 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4996 data, data_length );
4997 if( status != PSA_SUCCESS )
4998 return( status );
4999 status = psa_hmac_finish_internal( &hkdf->hmac,
5000 hkdf->prk,
5001 sizeof( hkdf->prk ) );
5002 if( status != PSA_SUCCESS )
5003 return( status );
5004 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5005 hkdf->block_number = 0;
5006 hkdf->state = HKDF_STATE_KEYED;
5007 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005008 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005009 if( hkdf->state == HKDF_STATE_OUTPUT )
5010 return( PSA_ERROR_BAD_STATE );
5011 if( hkdf->info_set )
5012 return( PSA_ERROR_BAD_STATE );
5013 hkdf->info_length = data_length;
5014 if( data_length != 0 )
5015 {
5016 hkdf->info = mbedtls_calloc( 1, data_length );
5017 if( hkdf->info == NULL )
5018 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5019 memcpy( hkdf->info, data, data_length );
5020 }
5021 hkdf->info_set = 1;
5022 return( PSA_SUCCESS );
5023 default:
5024 return( PSA_ERROR_INVALID_ARGUMENT );
5025 }
5026}
Janos Follathb03233e2019-06-11 15:30:30 +01005027
5028#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5029static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5030 psa_algorithm_t hash_alg,
5031 psa_key_derivation_step_t step,
5032 const uint8_t *data,
5033 size_t data_length )
5034{
5035 (void) prf;
5036 (void) hash_alg;
5037 (void) step;
5038 (void) data;
5039 (void) data_length;
5040
5041 return( PSA_ERROR_INVALID_ARGUMENT );
5042}
Janos Follath6660f0e2019-06-17 08:44:03 +01005043
5044static psa_status_t psa_tls12_prf_psk_to_ms_input(
5045 psa_tls12_prf_key_derivation_t *prf,
5046 psa_algorithm_t hash_alg,
5047 psa_key_derivation_step_t step,
5048 const uint8_t *data,
5049 size_t data_length )
5050{
5051 (void) prf;
5052 (void) hash_alg;
5053 (void) step;
5054 (void) data;
5055 (void) data_length;
5056
5057 return( PSA_ERROR_INVALID_ARGUMENT );
5058}
Janos Follathb03233e2019-06-11 15:30:30 +01005059#else
Janos Follathf08e2652019-06-13 09:05:41 +01005060static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5061 const uint8_t *data,
5062 size_t data_length )
5063{
5064 if( prf->state != TLS12_PRF_STATE_INIT )
5065 return( PSA_ERROR_BAD_STATE );
5066
Janos Follathd6dce9f2019-07-04 09:11:38 +01005067 if( data_length != 0 )
5068 {
5069 prf->seed = mbedtls_calloc( 1, data_length );
5070 if( prf->seed == NULL )
5071 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005072
Janos Follathd6dce9f2019-07-04 09:11:38 +01005073 memcpy( prf->seed, data, data_length );
5074 prf->seed_length = data_length;
5075 }
Janos Follathf08e2652019-06-13 09:05:41 +01005076
5077 prf->state = TLS12_PRF_STATE_SEED_SET;
5078
5079 return( PSA_SUCCESS );
5080}
5081
Janos Follath81550542019-06-13 14:26:34 +01005082static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5083 psa_algorithm_t hash_alg,
5084 const uint8_t *data,
5085 size_t data_length )
5086{
5087 psa_status_t status;
5088 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5089 return( PSA_ERROR_BAD_STATE );
5090
5091 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5092 if( status != PSA_SUCCESS )
5093 return( status );
5094
5095 prf->state = TLS12_PRF_STATE_KEY_SET;
5096
5097 return( PSA_SUCCESS );
5098}
5099
Janos Follath6660f0e2019-06-17 08:44:03 +01005100static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5101 psa_tls12_prf_key_derivation_t *prf,
5102 psa_algorithm_t hash_alg,
5103 const uint8_t *data,
5104 size_t data_length )
5105{
5106 psa_status_t status;
5107 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Janos Follath40e13932019-06-26 13:22:29 +01005108 unsigned char* cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005109
5110 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5111 return( PSA_ERROR_INVALID_ARGUMENT );
5112
5113 /* Quoting RFC 4279, Section 2:
5114 *
5115 * The premaster secret is formed as follows: if the PSK is N octets
5116 * long, concatenate a uint16 with the value N, N zero octets, a second
5117 * uint16 with the value N, and the PSK itself.
5118 */
5119
Janos Follath40e13932019-06-26 13:22:29 +01005120 *cur++ = ( data_length >> 8 ) & 0xff;
5121 *cur++ = ( data_length >> 0 ) & 0xff;
5122 memset( cur, 0, data_length );
5123 cur += data_length;
5124 *cur++ = pms[0];
5125 *cur++ = pms[1];
5126 memcpy( cur, data, data_length );
5127 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005128
Janos Follath40e13932019-06-26 13:22:29 +01005129 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005130
5131 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5132 return( status );
5133}
5134
Janos Follath63028dd2019-06-13 09:15:47 +01005135static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5136 const uint8_t *data,
5137 size_t data_length )
5138{
5139 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5140 return( PSA_ERROR_BAD_STATE );
5141
Janos Follathd6dce9f2019-07-04 09:11:38 +01005142 if( data_length != 0 )
5143 {
5144 prf->label = mbedtls_calloc( 1, data_length );
5145 if( prf->label == NULL )
5146 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005147
Janos Follathd6dce9f2019-07-04 09:11:38 +01005148 memcpy( prf->label, data, data_length );
5149 prf->label_length = data_length;
5150 }
Janos Follath63028dd2019-06-13 09:15:47 +01005151
5152 prf->state = TLS12_PRF_STATE_LABEL_SET;
5153
5154 return( PSA_SUCCESS );
5155}
5156
Janos Follathb03233e2019-06-11 15:30:30 +01005157static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5158 psa_algorithm_t hash_alg,
5159 psa_key_derivation_step_t step,
5160 const uint8_t *data,
5161 size_t data_length )
5162{
Janos Follathb03233e2019-06-11 15:30:30 +01005163 switch( step )
5164 {
Janos Follathf08e2652019-06-13 09:05:41 +01005165 case PSA_KEY_DERIVATION_INPUT_SEED:
5166 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005167 case PSA_KEY_DERIVATION_INPUT_SECRET:
5168 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005169 case PSA_KEY_DERIVATION_INPUT_LABEL:
5170 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005171 default:
5172 return( PSA_ERROR_INVALID_ARGUMENT );
5173 }
5174}
Janos Follath6660f0e2019-06-17 08:44:03 +01005175
5176static psa_status_t psa_tls12_prf_psk_to_ms_input(
5177 psa_tls12_prf_key_derivation_t *prf,
5178 psa_algorithm_t hash_alg,
5179 psa_key_derivation_step_t step,
5180 const uint8_t *data,
5181 size_t data_length )
5182{
5183 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005184 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005185 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5186 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005187 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005188
5189 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5190}
Janos Follathb03233e2019-06-11 15:30:30 +01005191#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005192#endif /* MBEDTLS_MD_C */
5193
Janos Follathb80a94e2019-06-12 15:54:46 +01005194static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005195 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005196 psa_key_derivation_step_t step,
5197 const uint8_t *data,
5198 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005199{
5200 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005201 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005202
5203#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005204 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005205 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005206 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005207 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005208 step, data, data_length );
5209 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005210 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005211#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005212#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005213 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005214 {
Janos Follathb03233e2019-06-11 15:30:30 +01005215 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5216 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5217 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005218 }
5219 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5220 {
5221 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5222 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5223 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005224 }
5225 else
5226#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005227 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005228 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005229 return( PSA_ERROR_BAD_STATE );
5230 }
5231
5232 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005233 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005234 return( status );
5235}
5236
Janos Follath51f4a0f2019-06-14 11:35:55 +01005237psa_status_t psa_key_derivation_input_bytes(
5238 psa_key_derivation_operation_t *operation,
5239 psa_key_derivation_step_t step,
5240 const uint8_t *data,
5241 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005242{
Janos Follathc5621512019-06-14 11:27:57 +01005243 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5244 return( PSA_ERROR_INVALID_ARGUMENT );
5245
5246 return( psa_key_derivation_input_internal( operation, step,
5247 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005248}
5249
Janos Follath51f4a0f2019-06-14 11:35:55 +01005250psa_status_t psa_key_derivation_input_key(
5251 psa_key_derivation_operation_t *operation,
5252 psa_key_derivation_step_t step,
5253 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005254{
5255 psa_key_slot_t *slot;
5256 psa_status_t status;
5257 status = psa_get_key_from_slot( handle, &slot,
5258 PSA_KEY_USAGE_DERIVE,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005259 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005260 if( status != PSA_SUCCESS )
5261 return( status );
5262 if( slot->type != PSA_KEY_TYPE_DERIVE )
5263 return( PSA_ERROR_INVALID_ARGUMENT );
5264 /* Don't allow a key to be used as an input that is usually public.
5265 * This is debatable. It's ok from a cryptographic perspective to
5266 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005267 * the material should be dedicated to a particular input step,
5268 * otherwise this may allow the key to be used in an unintended way
5269 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005270 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005271 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005272 return( psa_key_derivation_input_internal( operation,
5273 step,
5274 slot->data.raw.data,
5275 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005276}
5277
Gilles Peskineea0fb492018-07-12 17:17:20 +02005278
5279
5280/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005281/* Key agreement */
5282/****************************************************************/
5283
Gilles Peskinea05219c2018-11-16 16:02:56 +01005284#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005285static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5286 size_t peer_key_length,
5287 const mbedtls_ecp_keypair *our_key,
5288 uint8_t *shared_secret,
5289 size_t shared_secret_size,
5290 size_t *shared_secret_length )
5291{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005292 mbedtls_ecp_keypair *their_key = NULL;
5293 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005294 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005295 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005296
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005297 status = psa_import_ec_public_key(
5298 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5299 peer_key, peer_key_length,
5300 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005301 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005302 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005303
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005304 status = mbedtls_to_psa_error(
5305 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5306 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005307 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005308 status = mbedtls_to_psa_error(
5309 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5310 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005311 goto exit;
5312
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005313 status = mbedtls_to_psa_error(
5314 mbedtls_ecdh_calc_secret( &ecdh,
5315 shared_secret_length,
5316 shared_secret, shared_secret_size,
5317 mbedtls_ctr_drbg_random,
5318 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005319
5320exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005321 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005322 mbedtls_ecp_keypair_free( their_key );
5323 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005324 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005325}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005326#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005327
Gilles Peskine01d718c2018-09-18 12:01:02 +02005328#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5329
Gilles Peskine0216fe12019-04-11 21:23:21 +02005330static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5331 psa_key_slot_t *private_key,
5332 const uint8_t *peer_key,
5333 size_t peer_key_length,
5334 uint8_t *shared_secret,
5335 size_t shared_secret_size,
5336 size_t *shared_secret_length )
5337{
5338 switch( alg )
5339 {
5340#if defined(MBEDTLS_ECDH_C)
5341 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005342 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005343 return( PSA_ERROR_INVALID_ARGUMENT );
5344 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5345 private_key->data.ecp,
5346 shared_secret, shared_secret_size,
5347 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005348#endif /* MBEDTLS_ECDH_C */
5349 default:
5350 (void) private_key;
5351 (void) peer_key;
5352 (void) peer_key_length;
5353 (void) shared_secret;
5354 (void) shared_secret_size;
5355 (void) shared_secret_length;
5356 return( PSA_ERROR_NOT_SUPPORTED );
5357 }
5358}
5359
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005360/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005361 * to potentially free embedded data structures and wipe confidential data.
5362 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005363static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005364 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005365 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005366 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005367 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005368{
5369 psa_status_t status;
5370 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5371 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005372 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005373
5374 /* Step 1: run the secret agreement algorithm to generate the shared
5375 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005376 status = psa_key_agreement_raw_internal( ka_alg,
5377 private_key,
5378 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005379 shared_secret,
5380 sizeof( shared_secret ),
5381 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005382 if( status != PSA_SUCCESS )
5383 goto exit;
5384
5385 /* Step 2: set up the key derivation to generate key material from
5386 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005387 status = psa_key_derivation_input_internal( operation, step,
5388 shared_secret,
5389 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005390
Gilles Peskine01d718c2018-09-18 12:01:02 +02005391exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005392 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005393 return( status );
5394}
5395
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005396psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005397 psa_key_derivation_step_t step,
5398 psa_key_handle_t private_key,
5399 const uint8_t *peer_key,
5400 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005401{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005402 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005403 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005404 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005405 return( PSA_ERROR_INVALID_ARGUMENT );
5406 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005407 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005408 if( status != PSA_SUCCESS )
5409 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005410 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005411 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005412 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005413 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005414 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005415 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005416}
5417
Gilles Peskinebe697d82019-05-16 18:00:41 +02005418psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5419 psa_key_handle_t private_key,
5420 const uint8_t *peer_key,
5421 size_t peer_key_length,
5422 uint8_t *output,
5423 size_t output_size,
5424 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005425{
5426 psa_key_slot_t *slot;
5427 psa_status_t status;
5428
5429 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5430 {
5431 status = PSA_ERROR_INVALID_ARGUMENT;
5432 goto exit;
5433 }
5434 status = psa_get_key_from_slot( private_key, &slot,
5435 PSA_KEY_USAGE_DERIVE, alg );
5436 if( status != PSA_SUCCESS )
5437 goto exit;
5438
5439 status = psa_key_agreement_raw_internal( alg, slot,
5440 peer_key, peer_key_length,
5441 output, output_size,
5442 output_length );
5443
5444exit:
5445 if( status != PSA_SUCCESS )
5446 {
5447 /* If an error happens and is not handled properly, the output
5448 * may be used as a key to protect sensitive data. Arrange for such
5449 * a key to be random, which is likely to result in decryption or
5450 * verification errors. This is better than filling the buffer with
5451 * some constant data such as zeros, which would result in the data
5452 * being protected with a reproducible, easily knowable key.
5453 */
5454 psa_generate_random( output, output_size );
5455 *output_length = output_size;
5456 }
5457 return( status );
5458}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005459
5460
5461/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005462/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005463/****************************************************************/
5464
5465psa_status_t psa_generate_random( uint8_t *output,
5466 size_t output_size )
5467{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005468 int ret;
5469 GUARD_MODULE_INITIALIZED;
5470
5471 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005472 return( mbedtls_to_psa_error( ret ) );
5473}
5474
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005475#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5476#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005477
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005478psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5479 size_t seed_size )
5480{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005481 if( global_data.initialized )
5482 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005483
5484 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5485 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5486 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5487 return( PSA_ERROR_INVALID_ARGUMENT );
5488
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005489 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005490}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005491#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005492
Gilles Peskinee56e8782019-04-26 17:34:02 +02005493#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5494static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5495 size_t domain_parameters_size,
5496 int *exponent )
5497{
5498 size_t i;
5499 uint32_t acc = 0;
5500
5501 if( domain_parameters_size == 0 )
5502 {
5503 *exponent = 65537;
5504 return( PSA_SUCCESS );
5505 }
5506
5507 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5508 * support values that fit in a 32-bit integer, which is larger than
5509 * int on just about every platform anyway. */
5510 if( domain_parameters_size > sizeof( acc ) )
5511 return( PSA_ERROR_NOT_SUPPORTED );
5512 for( i = 0; i < domain_parameters_size; i++ )
5513 acc = ( acc << 8 ) | domain_parameters[i];
5514 if( acc > INT_MAX )
5515 return( PSA_ERROR_NOT_SUPPORTED );
5516 *exponent = acc;
5517 return( PSA_SUCCESS );
5518}
5519#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5520
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005521static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005522 psa_key_slot_t *slot, size_t bits,
5523 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005524{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005525 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005526
Gilles Peskinee56e8782019-04-26 17:34:02 +02005527 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005528 return( PSA_ERROR_INVALID_ARGUMENT );
5529
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005530 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005531 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005532 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005533 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005534 if( status != PSA_SUCCESS )
5535 return( status );
5536 status = psa_generate_random( slot->data.raw.data,
5537 slot->data.raw.bytes );
5538 if( status != PSA_SUCCESS )
5539 {
5540 mbedtls_free( slot->data.raw.data );
5541 return( status );
5542 }
5543#if defined(MBEDTLS_DES_C)
5544 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005545 psa_des_set_key_parity( slot->data.raw.data,
5546 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005547#endif /* MBEDTLS_DES_C */
5548 }
5549 else
5550
5551#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005552 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005553 {
5554 mbedtls_rsa_context *rsa;
5555 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005556 int exponent;
5557 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005558 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5559 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005560 /* Accept only byte-aligned keys, for the same reasons as
5561 * in psa_import_rsa_key(). */
5562 if( bits % 8 != 0 )
5563 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005564 status = psa_read_rsa_exponent( domain_parameters,
5565 domain_parameters_size,
5566 &exponent );
5567 if( status != PSA_SUCCESS )
5568 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005569 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5570 if( rsa == NULL )
5571 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5572 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5573 ret = mbedtls_rsa_gen_key( rsa,
5574 mbedtls_ctr_drbg_random,
5575 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005576 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005577 exponent );
5578 if( ret != 0 )
5579 {
5580 mbedtls_rsa_free( rsa );
5581 mbedtls_free( rsa );
5582 return( mbedtls_to_psa_error( ret ) );
5583 }
5584 slot->data.rsa = rsa;
5585 }
5586 else
5587#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5588
5589#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005590 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005591 {
5592 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5593 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5594 const mbedtls_ecp_curve_info *curve_info =
5595 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5596 mbedtls_ecp_keypair *ecp;
5597 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005598 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005599 return( PSA_ERROR_NOT_SUPPORTED );
5600 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5601 return( PSA_ERROR_NOT_SUPPORTED );
5602 if( curve_info->bit_size != bits )
5603 return( PSA_ERROR_INVALID_ARGUMENT );
5604 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5605 if( ecp == NULL )
5606 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5607 mbedtls_ecp_keypair_init( ecp );
5608 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5609 mbedtls_ctr_drbg_random,
5610 &global_data.ctr_drbg );
5611 if( ret != 0 )
5612 {
5613 mbedtls_ecp_keypair_free( ecp );
5614 mbedtls_free( ecp );
5615 return( mbedtls_to_psa_error( ret ) );
5616 }
5617 slot->data.ecp = ecp;
5618 }
5619 else
5620#endif /* MBEDTLS_ECP_C */
5621
5622 return( PSA_ERROR_NOT_SUPPORTED );
5623
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005624 return( PSA_SUCCESS );
5625}
5626
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005627psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005628 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005629{
5630 psa_status_t status;
5631 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005632 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005633 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005634 if( status == PSA_SUCCESS )
5635 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005636 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005637 slot, attributes->bits,
5638 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005639 }
5640 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005641 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005642 if( status != PSA_SUCCESS )
5643 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005644 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005645 *handle = 0;
5646 }
5647 return( status );
5648}
5649
5650
Gilles Peskine05d69892018-06-19 22:00:52 +02005651
5652/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005653/* Module setup */
5654/****************************************************************/
5655
Gilles Peskine5e769522018-11-20 21:59:56 +01005656psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5657 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5658 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5659{
5660 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5661 return( PSA_ERROR_BAD_STATE );
5662 global_data.entropy_init = entropy_init;
5663 global_data.entropy_free = entropy_free;
5664 return( PSA_SUCCESS );
5665}
5666
Gilles Peskinee59236f2018-01-27 23:32:46 +01005667void mbedtls_psa_crypto_free( void )
5668{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005669 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005670 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5671 {
5672 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005673 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005674 }
5675 /* Wipe all remaining data, including configuration.
5676 * In particular, this sets all state indicator to the value
5677 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005678 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005679#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005680 /* Unregister all secure element drivers, so that we restart from
5681 * a pristine state. */
5682 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005683#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005684}
5685
5686psa_status_t psa_crypto_init( void )
5687{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005688 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005689 const unsigned char drbg_seed[] = "PSA";
5690
Gilles Peskinec6b69072018-11-20 21:42:52 +01005691 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005692 if( global_data.initialized != 0 )
5693 return( PSA_SUCCESS );
5694
Gilles Peskine5e769522018-11-20 21:59:56 +01005695 /* Set default configuration if
5696 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5697 if( global_data.entropy_init == NULL )
5698 global_data.entropy_init = mbedtls_entropy_init;
5699 if( global_data.entropy_free == NULL )
5700 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005701
Gilles Peskinec6b69072018-11-20 21:42:52 +01005702 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005703 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005704 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005705 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005706 status = mbedtls_to_psa_error(
5707 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5708 mbedtls_entropy_func,
5709 &global_data.entropy,
5710 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5711 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005712 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005713 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005714
Gilles Peskine66fb1262018-12-10 16:29:04 +01005715 status = psa_initialize_key_slots( );
5716 if( status != PSA_SUCCESS )
5717 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005718
Gilles Peskinefc762652019-07-22 19:30:34 +02005719#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5720 status = psa_crypto_load_transaction( );
5721 if( status == PSA_SUCCESS )
5722 {
5723 /*TOnogrepDO: complete or abort the transaction*/
5724 }
5725 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5726 {
5727 /* There's no transaction to complete. It's all good. */
5728 status = PSA_SUCCESS;
5729 }
5730#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5731
Gilles Peskinec6b69072018-11-20 21:42:52 +01005732 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005733 global_data.initialized = 1;
5734
Gilles Peskinee59236f2018-01-27 23:32:46 +01005735exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005736 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005737 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005738 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005739}
5740
5741#endif /* MBEDTLS_PSA_CRYPTO_C */