blob: fc9161d8e69e75b8285db6723f61a89176ace129 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043#include <stdlib.h>
44#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020046#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010047#define mbedtls_calloc calloc
48#define mbedtls_free free
49#endif
50
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020052#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000053#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020054#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/blowfish.h"
56#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020057#include "mbedtls/chacha20.h"
58#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/cipher.h"
60#include "mbedtls/ccm.h"
61#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020064#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010065#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010066#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/error.h"
68#include "mbedtls/gcm.h"
69#include "mbedtls/md2.h"
70#include "mbedtls/md4.h"
71#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
73#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010076#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/sha1.h"
80#include "mbedtls/sha256.h"
81#include "mbedtls/sha512.h"
82#include "mbedtls/xtea.h"
83
Gilles Peskine996deb12018-08-01 15:45:45 +020084#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
85
Gilles Peskine9ef733f2018-02-07 21:05:37 +010086/* constant-time buffer comparison */
87static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
88{
89 size_t i;
90 unsigned char diff = 0;
91
92 for( i = 0; i < n; i++ )
93 diff |= a[i] ^ b[i];
94
95 return( diff );
96}
97
98
99
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100100/****************************************************************/
101/* Global data, support functions and library management */
102/****************************************************************/
103
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104static int key_type_is_raw_bytes( psa_key_type_t type )
105{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200106 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200107}
108
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109/* Values for psa_global_data_t::rng_state */
110#define RNG_NOT_INITIALIZED 0
111#define RNG_INITIALIZED 1
112#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100113
Gilles Peskine2d277862018-06-18 15:41:12 +0200114typedef struct
115{
Gilles Peskine5e769522018-11-20 21:59:56 +0100116 void (* entropy_init )( mbedtls_entropy_context *ctx );
117 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118 mbedtls_entropy_context entropy;
119 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100120 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100121 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100122} psa_global_data_t;
123
124static psa_global_data_t global_data;
125
itayzafrir0adf0fc2018-09-06 16:24:41 +0300126#define GUARD_MODULE_INITIALIZED \
127 if( global_data.initialized == 0 ) \
128 return( PSA_ERROR_BAD_STATE );
129
Gilles Peskinee59236f2018-01-27 23:32:46 +0100130static psa_status_t mbedtls_to_psa_error( int ret )
131{
Gilles Peskinea5905292018-02-07 20:59:33 +0100132 /* If there's both a high-level code and low-level code, dispatch on
133 * the high-level code. */
134 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100135 {
136 case 0:
137 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100138
139 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
140 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
141 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
142 return( PSA_ERROR_NOT_SUPPORTED );
143 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
144 return( PSA_ERROR_HARDWARE_FAILURE );
145
146 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
147 return( PSA_ERROR_HARDWARE_FAILURE );
148
Gilles Peskine9a944802018-06-21 09:35:35 +0200149 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
150 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
151 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
152 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
153 case MBEDTLS_ERR_ASN1_INVALID_DATA:
154 return( PSA_ERROR_INVALID_ARGUMENT );
155 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
156 return( PSA_ERROR_INSUFFICIENT_MEMORY );
157 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
158 return( PSA_ERROR_BUFFER_TOO_SMALL );
159
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000161 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100165 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
166 return( PSA_ERROR_NOT_SUPPORTED );
167 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
168 return( PSA_ERROR_HARDWARE_FAILURE );
169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
180 case MBEDTLS_ERR_CCM_BAD_INPUT:
181 return( PSA_ERROR_INVALID_ARGUMENT );
182 case MBEDTLS_ERR_CCM_AUTH_FAILED:
183 return( PSA_ERROR_INVALID_SIGNATURE );
184 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
Gilles Peskine26869f22019-05-06 15:25:00 +0200187 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
188 return( PSA_ERROR_INVALID_ARGUMENT );
189
190 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
191 return( PSA_ERROR_BAD_STATE );
192 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
193 return( PSA_ERROR_INVALID_SIGNATURE );
194
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
196 return( PSA_ERROR_NOT_SUPPORTED );
197 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
200 return( PSA_ERROR_INSUFFICIENT_MEMORY );
201 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
202 return( PSA_ERROR_INVALID_PADDING );
203 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
204 return( PSA_ERROR_BAD_STATE );
205 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
206 return( PSA_ERROR_INVALID_SIGNATURE );
207 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200208 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
213 return( PSA_ERROR_HARDWARE_FAILURE );
214
215 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
216 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
217 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
218 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
219 return( PSA_ERROR_NOT_SUPPORTED );
220 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
221 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
222
223 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
Gilles Peskinee59236f2018-01-27 23:32:46 +0100228 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
229 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
230 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
233 case MBEDTLS_ERR_GCM_AUTH_FAILED:
234 return( PSA_ERROR_INVALID_SIGNATURE );
235 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200236 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100237 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
238 return( PSA_ERROR_HARDWARE_FAILURE );
239
240 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
245 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
246 return( PSA_ERROR_NOT_SUPPORTED );
247 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
248 return( PSA_ERROR_INVALID_ARGUMENT );
249 case MBEDTLS_ERR_MD_ALLOC_FAILED:
250 return( PSA_ERROR_INSUFFICIENT_MEMORY );
251 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
252 return( PSA_ERROR_STORAGE_FAILURE );
253 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
254 return( PSA_ERROR_HARDWARE_FAILURE );
255
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
257 return( PSA_ERROR_STORAGE_FAILURE );
258 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
263 return( PSA_ERROR_BUFFER_TOO_SMALL );
264 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_ALLOC_FAILED:
274 return( PSA_ERROR_INSUFFICIENT_MEMORY );
275 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
276 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100279 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100280 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
281 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
284 return( PSA_ERROR_NOT_SUPPORTED );
285 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
286 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
287 return( PSA_ERROR_NOT_PERMITTED );
288 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_PK_INVALID_ALG:
291 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
292 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
293 return( PSA_ERROR_NOT_SUPPORTED );
294 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
295 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
300 return( PSA_ERROR_HARDWARE_FAILURE );
301 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
302 return( PSA_ERROR_NOT_SUPPORTED );
303
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
305 return( PSA_ERROR_HARDWARE_FAILURE );
306
307 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_RSA_INVALID_PADDING:
310 return( PSA_ERROR_INVALID_PADDING );
311 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
312 return( PSA_ERROR_HARDWARE_FAILURE );
313 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
314 return( PSA_ERROR_INVALID_ARGUMENT );
315 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
316 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200317 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100318 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
319 return( PSA_ERROR_INVALID_SIGNATURE );
320 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
321 return( PSA_ERROR_BUFFER_TOO_SMALL );
322 case MBEDTLS_ERR_RSA_RNG_FAILED:
323 return( PSA_ERROR_INSUFFICIENT_MEMORY );
324 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
325 return( PSA_ERROR_NOT_SUPPORTED );
326 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
327 return( PSA_ERROR_HARDWARE_FAILURE );
328
329 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
330 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
332 return( PSA_ERROR_HARDWARE_FAILURE );
333
334 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
335 return( PSA_ERROR_INVALID_ARGUMENT );
336 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
337 return( PSA_ERROR_HARDWARE_FAILURE );
338
itayzafrir5c753392018-05-08 11:18:38 +0300339 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300341 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
343 return( PSA_ERROR_BUFFER_TOO_SMALL );
344 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
345 return( PSA_ERROR_NOT_SUPPORTED );
346 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
347 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
348 return( PSA_ERROR_INVALID_SIGNATURE );
349 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
350 return( PSA_ERROR_INSUFFICIENT_MEMORY );
351 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
352 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
369 return( psa_key_lifetime_is_external( slot->lifetime ) );
370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200374static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
375{
376 switch( grpid )
377 {
378 case MBEDTLS_ECP_DP_SECP192R1:
379 return( PSA_ECC_CURVE_SECP192R1 );
380 case MBEDTLS_ECP_DP_SECP224R1:
381 return( PSA_ECC_CURVE_SECP224R1 );
382 case MBEDTLS_ECP_DP_SECP256R1:
383 return( PSA_ECC_CURVE_SECP256R1 );
384 case MBEDTLS_ECP_DP_SECP384R1:
385 return( PSA_ECC_CURVE_SECP384R1 );
386 case MBEDTLS_ECP_DP_SECP521R1:
387 return( PSA_ECC_CURVE_SECP521R1 );
388 case MBEDTLS_ECP_DP_BP256R1:
389 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
390 case MBEDTLS_ECP_DP_BP384R1:
391 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
392 case MBEDTLS_ECP_DP_BP512R1:
393 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
394 case MBEDTLS_ECP_DP_CURVE25519:
395 return( PSA_ECC_CURVE_CURVE25519 );
396 case MBEDTLS_ECP_DP_SECP192K1:
397 return( PSA_ECC_CURVE_SECP192K1 );
398 case MBEDTLS_ECP_DP_SECP224K1:
399 return( PSA_ECC_CURVE_SECP224K1 );
400 case MBEDTLS_ECP_DP_SECP256K1:
401 return( PSA_ECC_CURVE_SECP256K1 );
402 case MBEDTLS_ECP_DP_CURVE448:
403 return( PSA_ECC_CURVE_CURVE448 );
404 default:
405 return( 0 );
406 }
407}
408
Gilles Peskine12313cd2018-06-20 00:20:32 +0200409static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
410{
411 switch( curve )
412 {
413 case PSA_ECC_CURVE_SECP192R1:
414 return( MBEDTLS_ECP_DP_SECP192R1 );
415 case PSA_ECC_CURVE_SECP224R1:
416 return( MBEDTLS_ECP_DP_SECP224R1 );
417 case PSA_ECC_CURVE_SECP256R1:
418 return( MBEDTLS_ECP_DP_SECP256R1 );
419 case PSA_ECC_CURVE_SECP384R1:
420 return( MBEDTLS_ECP_DP_SECP384R1 );
421 case PSA_ECC_CURVE_SECP521R1:
422 return( MBEDTLS_ECP_DP_SECP521R1 );
423 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
424 return( MBEDTLS_ECP_DP_BP256R1 );
425 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
426 return( MBEDTLS_ECP_DP_BP384R1 );
427 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
428 return( MBEDTLS_ECP_DP_BP512R1 );
429 case PSA_ECC_CURVE_CURVE25519:
430 return( MBEDTLS_ECP_DP_CURVE25519 );
431 case PSA_ECC_CURVE_SECP192K1:
432 return( MBEDTLS_ECP_DP_SECP192K1 );
433 case PSA_ECC_CURVE_SECP224K1:
434 return( MBEDTLS_ECP_DP_SECP224K1 );
435 case PSA_ECC_CURVE_SECP256K1:
436 return( MBEDTLS_ECP_DP_SECP256K1 );
437 case PSA_ECC_CURVE_CURVE448:
438 return( MBEDTLS_ECP_DP_CURVE448 );
439 default:
440 return( MBEDTLS_ECP_DP_NONE );
441 }
442}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100443#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200444
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
446 size_t bits,
447 struct raw_data *raw )
448{
449 /* Check that the bit size is acceptable for the key type */
450 switch( type )
451 {
452 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200453 if( bits == 0 )
454 {
455 raw->bytes = 0;
456 raw->data = NULL;
457 return( PSA_SUCCESS );
458 }
459 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460#if defined(MBEDTLS_MD_C)
461 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200462#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200463 case PSA_KEY_TYPE_DERIVE:
464 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200465#if defined(MBEDTLS_AES_C)
466 case PSA_KEY_TYPE_AES:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_CAMELLIA_C)
472 case PSA_KEY_TYPE_CAMELLIA:
473 if( bits != 128 && bits != 192 && bits != 256 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_DES_C)
478 case PSA_KEY_TYPE_DES:
479 if( bits != 64 && bits != 128 && bits != 192 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
483#if defined(MBEDTLS_ARC4_C)
484 case PSA_KEY_TYPE_ARC4:
485 if( bits < 8 || bits > 2048 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200489#if defined(MBEDTLS_CHACHA20_C)
490 case PSA_KEY_TYPE_CHACHA20:
491 if( bits != 256 )
492 return( PSA_ERROR_INVALID_ARGUMENT );
493 break;
494#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 default:
496 return( PSA_ERROR_NOT_SUPPORTED );
497 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200498 if( bits % 8 != 0 )
499 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200500
501 /* Allocate memory for the key */
502 raw->bytes = PSA_BITS_TO_BYTES( bits );
503 raw->data = mbedtls_calloc( 1, raw->bytes );
504 if( raw->data == NULL )
505 {
506 raw->bytes = 0;
507 return( PSA_ERROR_INSUFFICIENT_MEMORY );
508 }
509 return( PSA_SUCCESS );
510}
511
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200512#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100513/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
514 * that are not a multiple of 8) well. For example, there is only
515 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
516 * way to return the exact bit size of a key.
517 * To keep things simple, reject non-byte-aligned key sizes. */
518static psa_status_t psa_check_rsa_key_byte_aligned(
519 const mbedtls_rsa_context *rsa )
520{
521 mbedtls_mpi n;
522 psa_status_t status;
523 mbedtls_mpi_init( &n );
524 status = mbedtls_to_psa_error(
525 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
526 if( status == PSA_SUCCESS )
527 {
528 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
529 status = PSA_ERROR_NOT_SUPPORTED;
530 }
531 mbedtls_mpi_free( &n );
532 return( status );
533}
534
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000535static psa_status_t psa_import_rsa_key( psa_key_type_t type,
536 const uint8_t *data,
537 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200538 mbedtls_rsa_context **p_rsa )
539{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000540 psa_status_t status;
541 mbedtls_pk_context pk;
542 mbedtls_rsa_context *rsa;
543 size_t bits;
544
545 mbedtls_pk_init( &pk );
546
547 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200548 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000549 status = mbedtls_to_psa_error(
550 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200551 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000552 status = mbedtls_to_psa_error(
553 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
554 if( status != PSA_SUCCESS )
555 goto exit;
556
557 /* We have something that the pkparse module recognizes. If it is a
558 * valid RSA key, store it. */
559 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000561 status = PSA_ERROR_INVALID_ARGUMENT;
562 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200563 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000564
565 rsa = mbedtls_pk_rsa( pk );
566 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
567 * supports non-byte-aligned key sizes, but not well. For example,
568 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
569 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
570 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
571 {
572 status = PSA_ERROR_NOT_SUPPORTED;
573 goto exit;
574 }
575 status = psa_check_rsa_key_byte_aligned( rsa );
576
577exit:
578 /* Free the content of the pk object only on error. */
579 if( status != PSA_SUCCESS )
580 {
581 mbedtls_pk_free( &pk );
582 return( status );
583 }
584
585 /* On success, store the content of the object in the RSA context. */
586 *p_rsa = rsa;
587
588 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200589}
590#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
591
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000592#if defined(MBEDTLS_ECP_C)
593
594/* Import a public key given as the uncompressed representation defined by SEC1
595 * 2.3.3 as the content of an ECPoint. */
596static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
597 const uint8_t *data,
598 size_t data_length,
599 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200600{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000602 mbedtls_ecp_keypair *ecp = NULL;
603 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
604
605 *p_ecp = NULL;
606 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
607 if( ecp == NULL )
608 return( PSA_ERROR_INSUFFICIENT_MEMORY );
609 mbedtls_ecp_keypair_init( ecp );
610
611 /* Load the group. */
612 status = mbedtls_to_psa_error(
613 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616 /* Load the public value. */
617 status = mbedtls_to_psa_error(
618 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
619 data, data_length ) );
620 if( status != PSA_SUCCESS )
621 goto exit;
622
623 /* Check that the point is on the curve. */
624 status = mbedtls_to_psa_error(
625 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
626 if( status != PSA_SUCCESS )
627 goto exit;
628
629 *p_ecp = ecp;
630 return( PSA_SUCCESS );
631
632exit:
633 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200634 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000635 mbedtls_ecp_keypair_free( ecp );
636 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200637 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000638 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200639}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000640#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200641
Gilles Peskinef76aa772018-10-29 19:24:33 +0100642#if defined(MBEDTLS_ECP_C)
643/* Import a private key given as a byte string which is the private value
644 * in big-endian order. */
645static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
646 const uint8_t *data,
647 size_t data_length,
648 mbedtls_ecp_keypair **p_ecp )
649{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651 mbedtls_ecp_keypair *ecp = NULL;
652 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
653
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200654 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
655 return( PSA_ERROR_INVALID_ARGUMENT );
656
Gilles Peskinef76aa772018-10-29 19:24:33 +0100657 *p_ecp = NULL;
658 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
659 if( ecp == NULL )
660 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000661 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100662
663 /* Load the group. */
664 status = mbedtls_to_psa_error(
665 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
666 if( status != PSA_SUCCESS )
667 goto exit;
668 /* Load the secret value. */
669 status = mbedtls_to_psa_error(
670 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
671 if( status != PSA_SUCCESS )
672 goto exit;
673 /* Validate the private key. */
674 status = mbedtls_to_psa_error(
675 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678 /* Calculate the public key from the private key. */
679 status = mbedtls_to_psa_error(
680 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
681 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
682 if( status != PSA_SUCCESS )
683 goto exit;
684
685 *p_ecp = ecp;
686 return( PSA_SUCCESS );
687
688exit:
689 if( ecp != NULL )
690 {
691 mbedtls_ecp_keypair_free( ecp );
692 mbedtls_free( ecp );
693 }
694 return( status );
695}
696#endif /* defined(MBEDTLS_ECP_C) */
697
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100698/** Import key data into a slot. `slot->type` must have been set
699 * previously. This function assumes that the slot does not contain
700 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100701psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
702 const uint8_t *data,
703 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200705 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706
Darryl Green940d72c2018-07-13 13:18:51 +0100707 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100709 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 if( data_length > SIZE_MAX / 8 )
711 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100712 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200713 PSA_BYTES_TO_BITS( data_length ),
714 &slot->data.raw );
715 if( status != PSA_SUCCESS )
716 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200717 if( data_length != 0 )
718 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100719 }
720 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100721#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200722 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100723 {
Darryl Green940d72c2018-07-13 13:18:51 +0100724 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100725 data, data_length,
726 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100727 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000728 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
729 {
730 status = psa_import_ec_public_key(
731 PSA_KEY_TYPE_GET_CURVE( slot->type ),
732 data, data_length,
733 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000734 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100735 else
736#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000737#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
738 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100739 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000740 status = psa_import_rsa_key( slot->type,
741 data, data_length,
742 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 }
744 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000745#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 {
747 return( PSA_ERROR_NOT_SUPPORTED );
748 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000749 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100750}
751
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100752/** Calculate the intersection of two algorithm usage policies.
753 *
754 * Return 0 (which allows no operation) on incompatibility.
755 */
756static psa_algorithm_t psa_key_policy_algorithm_intersection(
757 psa_algorithm_t alg1,
758 psa_algorithm_t alg2 )
759{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200760 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100761 if( alg1 == alg2 )
762 return( alg1 );
763 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100764 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100765 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
766 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
767 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
768 {
769 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
770 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100771 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100772 return( alg1 );
773 }
774 /* If the policies are incompatible, allow nothing. */
775 return( 0 );
776}
777
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200778static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
779 psa_algorithm_t requested_alg )
780{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200781 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200782 if( requested_alg == policy_alg )
783 return( 1 );
784 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200785 * and requested_alg is the same hash-and-sign family with any hash,
786 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200787 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
788 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
789 {
790 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
791 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
792 }
793 /* If it isn't permitted, it's forbidden. */
794 return( 0 );
795}
796
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100797/** Test whether a policy permits an algorithm.
798 *
799 * The caller must test usage flags separately.
800 */
801static int psa_key_policy_permits( const psa_key_policy_t *policy,
802 psa_algorithm_t alg )
803{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200804 return( psa_key_algorithm_permits( policy->alg, alg ) ||
805 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100806}
807
Gilles Peskinef603c712019-01-19 13:40:11 +0100808/** Restrict a key policy based on a constraint.
809 *
810 * \param[in,out] policy The policy to restrict.
811 * \param[in] constraint The policy constraint to apply.
812 *
813 * \retval #PSA_SUCCESS
814 * \c *policy contains the intersection of the original value of
815 * \c *policy and \c *constraint.
816 * \retval #PSA_ERROR_INVALID_ARGUMENT
817 * \c *policy and \c *constraint are incompatible.
818 * \c *policy is unchanged.
819 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100820static psa_status_t psa_restrict_key_policy(
821 psa_key_policy_t *policy,
822 const psa_key_policy_t *constraint )
823{
824 psa_algorithm_t intersection_alg =
825 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200826 psa_algorithm_t intersection_alg2 =
827 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100828 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
829 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200830 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
831 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100832 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100833 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200834 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100835 return( PSA_SUCCESS );
836}
837
Darryl Green06fd18d2018-07-16 11:21:11 +0100838/** Retrieve a slot which must contain a key. The key must have allow all the
839 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
840 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100841static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100842 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100843 psa_key_usage_t usage,
844 psa_algorithm_t alg )
845{
846 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100847 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100848
849 *p_slot = NULL;
850
Gilles Peskinec5487a82018-12-03 18:08:14 +0100851 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100852 if( status != PSA_SUCCESS )
853 return( status );
854 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200855 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100856
857 /* Enforce that usage policy for the key slot contains all the flags
858 * required by the usage parameter. There is one exception: public
859 * keys can always be exported, so we treat public key objects as
860 * if they had the export flag. */
861 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
862 usage &= ~PSA_KEY_USAGE_EXPORT;
863 if( ( slot->policy.usage & usage ) != usage )
864 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100865
866 /* Enforce that the usage policy permits the requested algortihm. */
867 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100868 return( PSA_ERROR_NOT_PERMITTED );
869
870 *p_slot = slot;
871 return( PSA_SUCCESS );
872}
Darryl Green940d72c2018-07-13 13:18:51 +0100873
Gilles Peskine28f8f302019-07-24 13:30:31 +0200874/** Retrieve a slot which must contain a transparent key.
875 *
876 * A transparent key is a key for which the key material is directly
877 * available, as opposed to a key in a secure element.
878 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200879 * This is a temporary function to use instead of psa_get_key_from_slot()
880 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200881 */
882#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
883static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
884 psa_key_slot_t **p_slot,
885 psa_key_usage_t usage,
886 psa_algorithm_t alg )
887{
888 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
889 if( status != PSA_SUCCESS )
890 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200891 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200892 {
893 *p_slot = NULL;
894 return( PSA_ERROR_NOT_SUPPORTED );
895 }
896 return( PSA_SUCCESS );
897}
898#else /* MBEDTLS_PSA_CRYPTO_SE_C */
899/* With no secure element support, all keys are transparent. */
900#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
901 psa_get_key_from_slot( handle, p_slot, usage, alg )
902#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
903
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100904/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100905static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000906{
Gilles Peskine73167e12019-07-12 23:44:37 +0200907#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
908 if( psa_key_slot_is_external( slot ) )
909 {
910 /* No key material to clean. */
911 }
912 else
913#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +0000914 if( slot->type == PSA_KEY_TYPE_NONE )
915 {
916 /* No key material to clean. */
917 }
918 else if( key_type_is_raw_bytes( slot->type ) )
919 {
920 mbedtls_free( slot->data.raw.data );
921 }
922 else
923#if defined(MBEDTLS_RSA_C)
924 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
925 {
926 mbedtls_rsa_free( slot->data.rsa );
927 mbedtls_free( slot->data.rsa );
928 }
929 else
930#endif /* defined(MBEDTLS_RSA_C) */
931#if defined(MBEDTLS_ECP_C)
932 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
933 {
934 mbedtls_ecp_keypair_free( slot->data.ecp );
935 mbedtls_free( slot->data.ecp );
936 }
937 else
938#endif /* defined(MBEDTLS_ECP_C) */
939 {
940 /* Shouldn't happen: the key type is not any type that we
941 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200942 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000943 }
944
945 return( PSA_SUCCESS );
946}
947
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100948static void psa_abort_operations_using_key( psa_key_slot_t *slot )
949{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200950 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100951 (void) slot;
952}
953
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100954/** Completely wipe a slot in memory, including its policy.
955 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100956psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100957{
958 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100959 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100960 /* At this point, key material and other type-specific content has
961 * been wiped. Clear remaining metadata. We can call memset and not
962 * zeroize because the metadata is not particularly sensitive. */
963 memset( slot, 0, sizeof( *slot ) );
964 return( status );
965}
966
Gilles Peskinec5487a82018-12-03 18:08:14 +0100967psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100969 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100970 psa_status_t status = PSA_SUCCESS;
971 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200972#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
973 psa_se_drv_table_entry_t *driver;
974#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975
Gilles Peskinec5487a82018-12-03 18:08:14 +0100976 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200977 if( status != PSA_SUCCESS )
978 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +0200979
980#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
981 driver = psa_get_se_driver_entry( slot->lifetime );
982 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +0200983 {
Gilles Peskine60450a42019-07-25 11:32:45 +0200984 /* For a key in a secure element, we need to do three things:
985 * remove the key file in internal storage, destroy the
986 * key inside the secure element, and update the driver's
987 * persistent data. Start a transaction that will encompass these
988 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +0200989 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
990 psa_crypto_transaction.key.lifetime = slot->lifetime;
991 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
992 psa_crypto_transaction.key.id = slot->persistent_storage_id;
993 status = psa_crypto_save_transaction( );
994 if( status != PSA_SUCCESS )
995 {
Gilles Peskine66be51c2019-07-25 18:02:52 +0200996 (void) psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +0200997 /* TOnogrepDO: destroy what can be destroyed anyway */
998 return( status );
999 }
1000
Gilles Peskine354f7672019-07-12 23:46:38 +02001001 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +02001002 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001003#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1004
Darryl Greend49a4992018-06-18 17:27:26 +01001005#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1006 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1007 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001008 storage_status =
1009 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +01001010 }
1011#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001012
Gilles Peskinefc762652019-07-22 19:30:34 +02001013#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1014 if( driver != NULL )
1015 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001016 psa_status_t status2;
1017 status = psa_save_se_persistent_data( driver );
1018 status2 = psa_crypto_stop_transaction( );
1019 if( status == PSA_SUCCESS )
1020 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001021 if( status != PSA_SUCCESS )
1022 {
1023 /* TOnogrepDO: destroy what can be destroyed anyway */
1024 return( status );
1025 }
1026 }
1027#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1028
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001029 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001030 if( status != PSA_SUCCESS )
1031 return( status );
1032 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033}
1034
Gilles Peskineb870b182018-07-06 16:02:09 +02001035/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001036static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +02001037{
Gilles Peskine424f8942019-07-15 21:59:53 +02001038#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1039 if( psa_get_se_driver( slot->lifetime, NULL, NULL ) )
1040 return( slot->data.se.bits );
1041#endif /* defined(MBEDTLS_PSA_CRYPTO_SE_C) */
1042
Gilles Peskineb870b182018-07-06 16:02:09 +02001043 if( key_type_is_raw_bytes( slot->type ) )
1044 return( slot->data.raw.bytes * 8 );
1045#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001046 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001047 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001048#endif /* defined(MBEDTLS_RSA_C) */
1049#if defined(MBEDTLS_ECP_C)
1050 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1051 return( slot->data.ecp->grp.pbits );
1052#endif /* defined(MBEDTLS_ECP_C) */
1053 /* Shouldn't happen except on an empty slot. */
1054 return( 0 );
1055}
1056
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001057void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1058{
Gilles Peskineb699f072019-04-26 16:06:02 +02001059 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001060 memset( attributes, 0, sizeof( *attributes ) );
1061}
1062
Gilles Peskineb699f072019-04-26 16:06:02 +02001063psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1064 psa_key_type_t type,
1065 const uint8_t *data,
1066 size_t data_length )
1067{
1068 uint8_t *copy = NULL;
1069
1070 if( data_length != 0 )
1071 {
1072 copy = mbedtls_calloc( 1, data_length );
1073 if( copy == NULL )
1074 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1075 memcpy( copy, data, data_length );
1076 }
1077 /* After this point, this function is guaranteed to succeed, so it
1078 * can start modifying `*attributes`. */
1079
1080 if( attributes->domain_parameters != NULL )
1081 {
1082 mbedtls_free( attributes->domain_parameters );
1083 attributes->domain_parameters = NULL;
1084 attributes->domain_parameters_size = 0;
1085 }
1086
1087 attributes->domain_parameters = copy;
1088 attributes->domain_parameters_size = data_length;
1089 attributes->type = type;
1090 return( PSA_SUCCESS );
1091}
1092
1093psa_status_t psa_get_key_domain_parameters(
1094 const psa_key_attributes_t *attributes,
1095 uint8_t *data, size_t data_size, size_t *data_length )
1096{
1097 if( attributes->domain_parameters_size > data_size )
1098 return( PSA_ERROR_BUFFER_TOO_SMALL );
1099 *data_length = attributes->domain_parameters_size;
1100 if( attributes->domain_parameters_size != 0 )
1101 memcpy( data, attributes->domain_parameters,
1102 attributes->domain_parameters_size );
1103 return( PSA_SUCCESS );
1104}
1105
1106#if defined(MBEDTLS_RSA_C)
1107static psa_status_t psa_get_rsa_public_exponent(
1108 const mbedtls_rsa_context *rsa,
1109 psa_key_attributes_t *attributes )
1110{
1111 mbedtls_mpi mpi;
1112 int ret;
1113 uint8_t *buffer = NULL;
1114 size_t buflen;
1115 mbedtls_mpi_init( &mpi );
1116
1117 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1118 if( ret != 0 )
1119 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001120 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1121 {
1122 /* It's the default value, which is reported as an empty string,
1123 * so there's nothing to do. */
1124 goto exit;
1125 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001126
1127 buflen = mbedtls_mpi_size( &mpi );
1128 buffer = mbedtls_calloc( 1, buflen );
1129 if( buffer == NULL )
1130 {
1131 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1132 goto exit;
1133 }
1134 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1135 if( ret != 0 )
1136 goto exit;
1137 attributes->domain_parameters = buffer;
1138 attributes->domain_parameters_size = buflen;
1139
1140exit:
1141 mbedtls_mpi_free( &mpi );
1142 if( ret != 0 )
1143 mbedtls_free( buffer );
1144 return( mbedtls_to_psa_error( ret ) );
1145}
1146#endif /* MBEDTLS_RSA_C */
1147
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001148/** Retrieve the readily-accessible attributes of a key in a slot.
1149 *
1150 * This function does not compute attributes that are not directly
1151 * stored in the slot, such as the bit size of a transparent key.
1152 */
1153static void psa_get_key_slot_attributes( psa_key_slot_t *slot,
1154 psa_key_attributes_t *attributes )
1155{
1156 attributes->id = slot->persistent_storage_id;
1157 attributes->lifetime = slot->lifetime;
1158 attributes->policy = slot->policy;
1159 attributes->type = slot->type;
1160}
1161
1162/** Retrieve all the publicly-accessible attributes of a key.
1163 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001164psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1165 psa_key_attributes_t *attributes )
1166{
1167 psa_key_slot_t *slot;
1168 psa_status_t status;
1169
1170 psa_reset_key_attributes( attributes );
1171
Gilles Peskine28f8f302019-07-24 13:30:31 +02001172 status = psa_get_transparent_key( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001173 if( status != PSA_SUCCESS )
1174 return( status );
1175
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001176 psa_get_key_slot_attributes( slot, attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001177 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001178
1179 switch( slot->type )
1180 {
1181#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001182 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001183 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1184 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1185 break;
1186#endif
1187 default:
1188 /* Nothing else to do. */
1189 break;
1190 }
1191
1192 if( status != PSA_SUCCESS )
1193 psa_reset_key_attributes( attributes );
1194 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001195}
1196
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001197#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001198static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1199 unsigned char *buf, size_t size )
1200{
1201 int ret;
1202 unsigned char *c;
1203 size_t len = 0;
1204
1205 c = buf + size;
1206
1207 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1208
1209 return( (int) len );
1210}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001211#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001212
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001213static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1214 uint8_t *data,
1215 size_t data_size,
1216 size_t *data_length,
1217 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001218{
Gilles Peskine5d309672019-07-12 23:47:28 +02001219#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1220 const psa_drv_se_t *drv;
1221 psa_drv_se_context_t *drv_context;
1222#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1223
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001224 *data_length = 0;
1225
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001226 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001227 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001228
Gilles Peskine5d309672019-07-12 23:47:28 +02001229#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1230 if( psa_get_se_driver( slot->lifetime, &drv, &drv_context ) )
1231 {
1232 psa_drv_se_export_key_t method;
1233 if( drv->key_management == NULL )
1234 return( PSA_ERROR_NOT_SUPPORTED );
1235 method = ( export_public_key ?
1236 drv->key_management->p_export_public :
1237 drv->key_management->p_export );
1238 if( method == NULL )
1239 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001240 return( method( drv_context,
1241 slot->data.se.slot_number,
1242 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001243 }
1244#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1245
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001246 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001247 {
1248 if( slot->data.raw.bytes > data_size )
1249 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001250 if( data_size != 0 )
1251 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001252 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001253 memset( data + slot->data.raw.bytes, 0,
1254 data_size - slot->data.raw.bytes );
1255 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001256 *data_length = slot->data.raw.bytes;
1257 return( PSA_SUCCESS );
1258 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001259#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001260 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001261 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001262 psa_status_t status;
1263
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001264 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001265 if( bytes > data_size )
1266 return( PSA_ERROR_BUFFER_TOO_SMALL );
1267 status = mbedtls_to_psa_error(
1268 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1269 if( status != PSA_SUCCESS )
1270 return( status );
1271 memset( data + bytes, 0, data_size - bytes );
1272 *data_length = bytes;
1273 return( PSA_SUCCESS );
1274 }
1275#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001276 else
Moran Peker17e36e12018-05-02 12:55:20 +03001277 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001278#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001279 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001280 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001281 {
Moran Pekera998bc62018-04-16 18:16:20 +03001282 mbedtls_pk_context pk;
1283 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001284 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001285 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001286#if defined(MBEDTLS_RSA_C)
1287 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001288 pk.pk_info = &mbedtls_rsa_info;
1289 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001290#else
1291 return( PSA_ERROR_NOT_SUPPORTED );
1292#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001293 }
1294 else
1295 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001296#if defined(MBEDTLS_ECP_C)
1297 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001298 pk.pk_info = &mbedtls_eckey_info;
1299 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001300#else
1301 return( PSA_ERROR_NOT_SUPPORTED );
1302#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001303 }
Moran Pekerd7326592018-05-29 16:56:39 +03001304 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001305 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001306 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001307 }
Moran Peker17e36e12018-05-02 12:55:20 +03001308 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001309 {
Moran Peker17e36e12018-05-02 12:55:20 +03001310 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001311 }
Moran Peker60364322018-04-29 11:34:58 +03001312 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001313 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001314 /* If data_size is 0 then data may be NULL and then the
1315 * call to memset would have undefined behavior. */
1316 if( data_size != 0 )
1317 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001318 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001319 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001320 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1321 * Move the data to the beginning and erase remaining data
1322 * at the original location. */
1323 if( 2 * (size_t) ret <= data_size )
1324 {
1325 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001326 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001327 }
1328 else if( (size_t) ret < data_size )
1329 {
1330 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001331 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001332 }
Moran Pekera998bc62018-04-16 18:16:20 +03001333 *data_length = ret;
1334 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001335 }
1336 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001337#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001338 {
1339 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001340 it is valid for a special-purpose implementation to omit
1341 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001342 return( PSA_ERROR_NOT_SUPPORTED );
1343 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001344 }
1345}
1346
Gilles Peskinec5487a82018-12-03 18:08:14 +01001347psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001348 uint8_t *data,
1349 size_t data_size,
1350 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001351{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001352 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001353 psa_status_t status;
1354
1355 /* Set the key to empty now, so that even when there are errors, we always
1356 * set data_length to a value between 0 and data_size. On error, setting
1357 * the key to empty is a good choice because an empty key representation is
1358 * unlikely to be accepted anywhere. */
1359 *data_length = 0;
1360
1361 /* Export requires the EXPORT flag. There is an exception for public keys,
1362 * which don't require any flag, but psa_get_key_from_slot takes
1363 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001364 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001365 if( status != PSA_SUCCESS )
1366 return( status );
1367 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001368 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001369}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001370
Gilles Peskinec5487a82018-12-03 18:08:14 +01001371psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001372 uint8_t *data,
1373 size_t data_size,
1374 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001375{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001376 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001377 psa_status_t status;
1378
1379 /* Set the key to empty now, so that even when there are errors, we always
1380 * set data_length to a value between 0 and data_size. On error, setting
1381 * the key to empty is a good choice because an empty key representation is
1382 * unlikely to be accepted anywhere. */
1383 *data_length = 0;
1384
1385 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001386 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001387 if( status != PSA_SUCCESS )
1388 return( status );
1389 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001390 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001391}
1392
Gilles Peskine4747d192019-04-17 15:05:45 +02001393static psa_status_t psa_set_key_policy_internal(
1394 psa_key_slot_t *slot,
1395 const psa_key_policy_t *policy )
1396{
1397 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001398 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001399 PSA_KEY_USAGE_ENCRYPT |
1400 PSA_KEY_USAGE_DECRYPT |
1401 PSA_KEY_USAGE_SIGN |
1402 PSA_KEY_USAGE_VERIFY |
1403 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1404 return( PSA_ERROR_INVALID_ARGUMENT );
1405
1406 slot->policy = *policy;
1407 return( PSA_SUCCESS );
1408}
1409
1410/** Prepare a key slot to receive key material.
1411 *
1412 * This function allocates a key slot and sets its metadata.
1413 *
1414 * If this function fails, call psa_fail_key_creation().
1415 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001416 * This function is intended to be used as follows:
1417 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1418 * it with the specified attributes, and assign it a handle.
1419 * -# Populate the slot with the key material.
1420 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1421 * In case of failure at any step, stop the sequence and call
1422 * psa_fail_key_creation().
1423 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001424 * \param[in] attributes Key attributes for the new key.
1425 * \param[out] handle On success, a handle for the allocated slot.
1426 * \param[out] p_slot On success, a pointer to the prepared slot.
1427 * \param[out] p_drv On any return, the driver for the key, if any.
1428 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001429 *
1430 * \retval #PSA_SUCCESS
1431 * The key slot is ready to receive key material.
1432 * \return If this function fails, the key slot is an invalid state.
1433 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001434 */
1435static psa_status_t psa_start_key_creation(
1436 const psa_key_attributes_t *attributes,
1437 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001438 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001439 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001440{
1441 psa_status_t status;
1442 psa_key_slot_t *slot;
1443
Gilles Peskine011e4282019-06-26 18:34:38 +02001444 *p_drv = NULL;
1445
Gilles Peskine267c6562019-05-27 19:01:54 +02001446 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001447 if( status != PSA_SUCCESS )
1448 return( status );
1449 slot = *p_slot;
1450
1451 status = psa_set_key_policy_internal( slot, &attributes->policy );
1452 if( status != PSA_SUCCESS )
1453 return( status );
1454 slot->lifetime = attributes->lifetime;
Gilles Peskine011e4282019-06-26 18:34:38 +02001455
Gilles Peskine4747d192019-04-17 15:05:45 +02001456 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001457 {
1458 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskine011e4282019-06-26 18:34:38 +02001459 attributes->id,
1460 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001461 if( status != PSA_SUCCESS )
1462 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001463 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001464 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001465 slot->type = attributes->type;
1466
Gilles Peskinecbaff462019-07-12 23:46:04 +02001467#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine60450a42019-07-25 11:32:45 +02001468 /* For a key in a secure element, we need to do three things:
1469 * create the key file in internal storage, create the
1470 * key inside the secure element, and update the driver's
1471 * persistent data. Start a transaction that will encompass these
1472 * three actions. */
1473 /* The first thing to do is to find a slot number for the new key.
1474 * We save the slot number in persistent storage as part of the
1475 * transaction data. It will be needed to recover if the power
1476 * fails during the key creation process, to clean up on the secure
1477 * element side after restarting. Obtaining a slot number from the
1478 * secure element driver updates its persistent state, but we do not yet
1479 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001480 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001481 if( *p_drv != NULL )
1482 {
1483 status = psa_find_se_slot_for_key( attributes, *p_drv,
1484 &slot->data.se.slot_number );
1485 if( status != PSA_SUCCESS )
1486 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001487 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1488 psa_crypto_transaction.key.lifetime = slot->lifetime;
1489 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1490 psa_crypto_transaction.key.id = slot->persistent_storage_id;
1491 status = psa_crypto_save_transaction( );
1492 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001493 {
1494 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001495 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001496 }
Gilles Peskine424f8942019-07-15 21:59:53 +02001497
1498 /* TOnogrepDO: validate bits. How to do this depends on the key
1499 * creation method, so setting bits might not belong here. */
1500 slot->data.se.bits = psa_get_key_bits( attributes );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001501 }
1502#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1503
Gilles Peskine4747d192019-04-17 15:05:45 +02001504 return( status );
1505}
1506
1507/** Finalize the creation of a key once its key material has been set.
1508 *
1509 * This entails writing the key to persistent storage.
1510 *
1511 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001512 * See the documentation of psa_start_key_creation() for the intended use
1513 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001514 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001515 * \param[in,out] slot Pointer to the slot with key material.
1516 * \param[in] driver The secure element driver for the key,
1517 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001518 *
1519 * \retval #PSA_SUCCESS
1520 * The key was successfully created. The handle is now valid.
1521 * \return If this function fails, the key slot is an invalid state.
1522 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001523 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001524static psa_status_t psa_finish_key_creation(
1525 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001526 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001527{
1528 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001529 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001530 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001531
1532#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1df83d42019-07-23 16:13:14 +02001533 if( slot->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001534 {
1535 uint8_t *buffer = NULL;
1536 size_t buffer_size = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001537 size_t length = 0;
Gilles Peskine4747d192019-04-17 15:05:45 +02001538
Gilles Peskine1df83d42019-07-23 16:13:14 +02001539#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1540 if( driver != NULL )
1541 {
1542 buffer = (uint8_t*) &slot->data.se.slot_number;
1543 length = sizeof( slot->data.se.slot_number );
1544 }
1545 else
1546#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1547 {
1548 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
1549 psa_get_key_slot_bits( slot ) );
1550 buffer = mbedtls_calloc( 1, buffer_size );
1551 if( buffer == NULL && buffer_size != 0 )
1552 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1553 status = psa_internal_export_key( slot,
1554 buffer, buffer_size, &length,
1555 0 );
1556 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001557
1558 if( status == PSA_SUCCESS )
1559 {
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001560 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1561 psa_get_key_slot_attributes( slot, &attributes );
1562 status = psa_save_persistent_key( &attributes, buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001563 }
1564
Gilles Peskine1df83d42019-07-23 16:13:14 +02001565#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1566 if( driver == NULL )
1567#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1568 {
1569 if( buffer_size != 0 )
1570 mbedtls_platform_zeroize( buffer, buffer_size );
1571 mbedtls_free( buffer );
1572 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001573 }
1574#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1575
Gilles Peskinecbaff462019-07-12 23:46:04 +02001576#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1577 if( driver != NULL )
1578 {
1579 status = psa_save_se_persistent_data( driver );
1580 if( status != PSA_SUCCESS )
1581 {
1582 psa_destroy_persistent_key( slot->persistent_storage_id );
1583 return( status );
1584 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001585 status = psa_crypto_stop_transaction( );
1586 if( status != PSA_SUCCESS )
1587 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001588 }
1589#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1590
Gilles Peskine4747d192019-04-17 15:05:45 +02001591 return( status );
1592}
1593
1594/** Abort the creation of a key.
1595 *
1596 * You may call this function after calling psa_start_key_creation(),
1597 * or after psa_finish_key_creation() fails. In other circumstances, this
1598 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001599 * See the documentation of psa_start_key_creation() for the intended use
1600 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001601 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001602 * \param[in,out] slot Pointer to the slot with key material.
1603 * \param[in] driver The secure element driver for the key,
1604 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001605 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001606static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001607 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001608{
Gilles Peskine011e4282019-06-26 18:34:38 +02001609 (void) driver;
1610
Gilles Peskine4747d192019-04-17 15:05:45 +02001611 if( slot == NULL )
1612 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001613
1614#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1615 /* TOnogrepDO: If the key has already been created in the secure
1616 * element, and the failure happened later (when saving metadata
1617 * to internal storage), we need to destroy the key in the secure
1618 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001619
1620 /* Abort the ongoing transaction if any. We already did what it
1621 * takes to undo any partial creation. All that's left is to update
1622 * the transaction data itself. */
1623 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001624#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1625
Gilles Peskine4747d192019-04-17 15:05:45 +02001626 psa_wipe_key_slot( slot );
1627}
1628
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001629static psa_status_t psa_check_key_slot_attributes(
1630 const psa_key_slot_t *slot,
1631 const psa_key_attributes_t *attributes )
1632{
1633 if( attributes->type != 0 )
1634 {
1635 if( attributes->type != slot->type )
1636 return( PSA_ERROR_INVALID_ARGUMENT );
1637 }
1638
1639 if( attributes->domain_parameters_size != 0 )
1640 {
1641#if defined(MBEDTLS_RSA_C)
1642 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1643 {
1644 mbedtls_mpi actual, required;
1645 int ret;
1646 mbedtls_mpi_init( &actual );
1647 mbedtls_mpi_init( &required );
1648 ret = mbedtls_rsa_export( slot->data.rsa,
1649 NULL, NULL, NULL, NULL, &actual );
1650 if( ret != 0 )
1651 goto rsa_exit;
1652 ret = mbedtls_mpi_read_binary( &required,
1653 attributes->domain_parameters,
1654 attributes->domain_parameters_size );
1655 if( ret != 0 )
1656 goto rsa_exit;
1657 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1658 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1659 rsa_exit:
1660 mbedtls_mpi_free( &actual );
1661 mbedtls_mpi_free( &required );
1662 if( ret != 0)
1663 return( mbedtls_to_psa_error( ret ) );
1664 }
1665 else
1666#endif
1667 {
1668 return( PSA_ERROR_INVALID_ARGUMENT );
1669 }
1670 }
1671
1672 if( attributes->bits != 0 )
1673 {
1674 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1675 return( PSA_ERROR_INVALID_ARGUMENT );
1676 }
1677
1678 return( PSA_SUCCESS );
1679}
1680
Gilles Peskine4747d192019-04-17 15:05:45 +02001681psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001682 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001683 size_t data_length,
1684 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001685{
1686 psa_status_t status;
1687 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001688 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001689
Gilles Peskine011e4282019-06-26 18:34:38 +02001690 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001691 if( status != PSA_SUCCESS )
1692 goto exit;
1693
Gilles Peskine5d309672019-07-12 23:47:28 +02001694#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1695 if( driver != NULL )
1696 {
1697 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
1698 if( drv->key_management == NULL ||
1699 drv->key_management->p_import == NULL )
1700 {
1701 status = PSA_ERROR_NOT_SUPPORTED;
1702 goto exit;
1703 }
1704 status = drv->key_management->p_import(
1705 psa_get_se_driver_context( driver ),
1706 slot->data.se.slot_number,
1707 slot->lifetime, slot->type, slot->policy.alg, slot->policy.usage,
1708 data, data_length );
1709 /* TOnogrepDO: psa_check_key_slot_attributes? */
1710 }
1711 else
1712#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1713 {
1714 status = psa_import_key_into_slot( slot, data, data_length );
1715 if( status != PSA_SUCCESS )
1716 goto exit;
1717 status = psa_check_key_slot_attributes( slot, attributes );
1718 if( status != PSA_SUCCESS )
1719 goto exit;
1720 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001721
Gilles Peskine011e4282019-06-26 18:34:38 +02001722 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001723exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001724 if( status != PSA_SUCCESS )
1725 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001726 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001727 *handle = 0;
1728 }
1729 return( status );
1730}
1731
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001732static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001733 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001734{
1735 psa_status_t status;
1736 uint8_t *buffer = NULL;
1737 size_t buffer_size = 0;
1738 size_t length;
1739
1740 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001741 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001742 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001743 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001744 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001745 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1746 if( status != PSA_SUCCESS )
1747 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001748 target->type = source->type;
1749 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001750
1751exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001752 if( buffer_size != 0 )
1753 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001754 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001755 return( status );
1756}
1757
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001758psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1759 const psa_key_attributes_t *specified_attributes,
1760 psa_key_handle_t *target_handle )
1761{
1762 psa_status_t status;
1763 psa_key_slot_t *source_slot = NULL;
1764 psa_key_slot_t *target_slot = NULL;
1765 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001766 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001767
Gilles Peskine28f8f302019-07-24 13:30:31 +02001768 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001769 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001770 if( status != PSA_SUCCESS )
1771 goto exit;
1772
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001773 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1774 if( status != PSA_SUCCESS )
1775 goto exit;
1776
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001777 status = psa_restrict_key_policy( &actual_attributes.policy,
1778 &source_slot->policy );
1779 if( status != PSA_SUCCESS )
1780 goto exit;
1781
1782 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001783 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001784 if( status != PSA_SUCCESS )
1785 goto exit;
1786
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001787#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1788 if( driver != NULL )
1789 {
1790 /* Copying to a secure element is not implemented yet. */
1791 status = PSA_ERROR_NOT_SUPPORTED;
1792 goto exit;
1793 }
1794#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1795
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001796 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001797 if( status != PSA_SUCCESS )
1798 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001799
Gilles Peskine011e4282019-06-26 18:34:38 +02001800 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001801exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001802 if( status != PSA_SUCCESS )
1803 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001804 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001805 *target_handle = 0;
1806 }
1807 return( status );
1808}
1809
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001810
1811
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001812/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001813/* Message digests */
1814/****************************************************************/
1815
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001816static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001817{
1818 switch( alg )
1819 {
1820#if defined(MBEDTLS_MD2_C)
1821 case PSA_ALG_MD2:
1822 return( &mbedtls_md2_info );
1823#endif
1824#if defined(MBEDTLS_MD4_C)
1825 case PSA_ALG_MD4:
1826 return( &mbedtls_md4_info );
1827#endif
1828#if defined(MBEDTLS_MD5_C)
1829 case PSA_ALG_MD5:
1830 return( &mbedtls_md5_info );
1831#endif
1832#if defined(MBEDTLS_RIPEMD160_C)
1833 case PSA_ALG_RIPEMD160:
1834 return( &mbedtls_ripemd160_info );
1835#endif
1836#if defined(MBEDTLS_SHA1_C)
1837 case PSA_ALG_SHA_1:
1838 return( &mbedtls_sha1_info );
1839#endif
1840#if defined(MBEDTLS_SHA256_C)
1841 case PSA_ALG_SHA_224:
1842 return( &mbedtls_sha224_info );
1843 case PSA_ALG_SHA_256:
1844 return( &mbedtls_sha256_info );
1845#endif
1846#if defined(MBEDTLS_SHA512_C)
1847 case PSA_ALG_SHA_384:
1848 return( &mbedtls_sha384_info );
1849 case PSA_ALG_SHA_512:
1850 return( &mbedtls_sha512_info );
1851#endif
1852 default:
1853 return( NULL );
1854 }
1855}
1856
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001857psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1858{
1859 switch( operation->alg )
1860 {
Gilles Peskine81736312018-06-26 15:04:31 +02001861 case 0:
1862 /* The object has (apparently) been initialized but it is not
1863 * in use. It's ok to call abort on such an object, and there's
1864 * nothing to do. */
1865 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001866#if defined(MBEDTLS_MD2_C)
1867 case PSA_ALG_MD2:
1868 mbedtls_md2_free( &operation->ctx.md2 );
1869 break;
1870#endif
1871#if defined(MBEDTLS_MD4_C)
1872 case PSA_ALG_MD4:
1873 mbedtls_md4_free( &operation->ctx.md4 );
1874 break;
1875#endif
1876#if defined(MBEDTLS_MD5_C)
1877 case PSA_ALG_MD5:
1878 mbedtls_md5_free( &operation->ctx.md5 );
1879 break;
1880#endif
1881#if defined(MBEDTLS_RIPEMD160_C)
1882 case PSA_ALG_RIPEMD160:
1883 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1884 break;
1885#endif
1886#if defined(MBEDTLS_SHA1_C)
1887 case PSA_ALG_SHA_1:
1888 mbedtls_sha1_free( &operation->ctx.sha1 );
1889 break;
1890#endif
1891#if defined(MBEDTLS_SHA256_C)
1892 case PSA_ALG_SHA_224:
1893 case PSA_ALG_SHA_256:
1894 mbedtls_sha256_free( &operation->ctx.sha256 );
1895 break;
1896#endif
1897#if defined(MBEDTLS_SHA512_C)
1898 case PSA_ALG_SHA_384:
1899 case PSA_ALG_SHA_512:
1900 mbedtls_sha512_free( &operation->ctx.sha512 );
1901 break;
1902#endif
1903 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001904 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001905 }
1906 operation->alg = 0;
1907 return( PSA_SUCCESS );
1908}
1909
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001910psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001911 psa_algorithm_t alg )
1912{
1913 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001914
1915 /* A context must be freshly initialized before it can be set up. */
1916 if( operation->alg != 0 )
1917 {
1918 return( PSA_ERROR_BAD_STATE );
1919 }
1920
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001921 switch( alg )
1922 {
1923#if defined(MBEDTLS_MD2_C)
1924 case PSA_ALG_MD2:
1925 mbedtls_md2_init( &operation->ctx.md2 );
1926 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1927 break;
1928#endif
1929#if defined(MBEDTLS_MD4_C)
1930 case PSA_ALG_MD4:
1931 mbedtls_md4_init( &operation->ctx.md4 );
1932 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1933 break;
1934#endif
1935#if defined(MBEDTLS_MD5_C)
1936 case PSA_ALG_MD5:
1937 mbedtls_md5_init( &operation->ctx.md5 );
1938 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1939 break;
1940#endif
1941#if defined(MBEDTLS_RIPEMD160_C)
1942 case PSA_ALG_RIPEMD160:
1943 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1944 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1945 break;
1946#endif
1947#if defined(MBEDTLS_SHA1_C)
1948 case PSA_ALG_SHA_1:
1949 mbedtls_sha1_init( &operation->ctx.sha1 );
1950 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1951 break;
1952#endif
1953#if defined(MBEDTLS_SHA256_C)
1954 case PSA_ALG_SHA_224:
1955 mbedtls_sha256_init( &operation->ctx.sha256 );
1956 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1957 break;
1958 case PSA_ALG_SHA_256:
1959 mbedtls_sha256_init( &operation->ctx.sha256 );
1960 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1961 break;
1962#endif
1963#if defined(MBEDTLS_SHA512_C)
1964 case PSA_ALG_SHA_384:
1965 mbedtls_sha512_init( &operation->ctx.sha512 );
1966 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1967 break;
1968 case PSA_ALG_SHA_512:
1969 mbedtls_sha512_init( &operation->ctx.sha512 );
1970 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1971 break;
1972#endif
1973 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001974 return( PSA_ALG_IS_HASH( alg ) ?
1975 PSA_ERROR_NOT_SUPPORTED :
1976 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001977 }
1978 if( ret == 0 )
1979 operation->alg = alg;
1980 else
1981 psa_hash_abort( operation );
1982 return( mbedtls_to_psa_error( ret ) );
1983}
1984
1985psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1986 const uint8_t *input,
1987 size_t input_length )
1988{
1989 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001990
1991 /* Don't require hash implementations to behave correctly on a
1992 * zero-length input, which may have an invalid pointer. */
1993 if( input_length == 0 )
1994 return( PSA_SUCCESS );
1995
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001996 switch( operation->alg )
1997 {
1998#if defined(MBEDTLS_MD2_C)
1999 case PSA_ALG_MD2:
2000 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2001 input, input_length );
2002 break;
2003#endif
2004#if defined(MBEDTLS_MD4_C)
2005 case PSA_ALG_MD4:
2006 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2007 input, input_length );
2008 break;
2009#endif
2010#if defined(MBEDTLS_MD5_C)
2011 case PSA_ALG_MD5:
2012 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2013 input, input_length );
2014 break;
2015#endif
2016#if defined(MBEDTLS_RIPEMD160_C)
2017 case PSA_ALG_RIPEMD160:
2018 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2019 input, input_length );
2020 break;
2021#endif
2022#if defined(MBEDTLS_SHA1_C)
2023 case PSA_ALG_SHA_1:
2024 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2025 input, input_length );
2026 break;
2027#endif
2028#if defined(MBEDTLS_SHA256_C)
2029 case PSA_ALG_SHA_224:
2030 case PSA_ALG_SHA_256:
2031 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2032 input, input_length );
2033 break;
2034#endif
2035#if defined(MBEDTLS_SHA512_C)
2036 case PSA_ALG_SHA_384:
2037 case PSA_ALG_SHA_512:
2038 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2039 input, input_length );
2040 break;
2041#endif
2042 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002043 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002044 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002045
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002046 if( ret != 0 )
2047 psa_hash_abort( operation );
2048 return( mbedtls_to_psa_error( ret ) );
2049}
2050
2051psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2052 uint8_t *hash,
2053 size_t hash_size,
2054 size_t *hash_length )
2055{
itayzafrir40835d42018-08-02 13:14:17 +03002056 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002057 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002058 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002059
2060 /* Fill the output buffer with something that isn't a valid hash
2061 * (barring an attack on the hash and deliberately-crafted input),
2062 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002063 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002064 /* If hash_size is 0 then hash may be NULL and then the
2065 * call to memset would have undefined behavior. */
2066 if( hash_size != 0 )
2067 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002068
2069 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002070 {
2071 status = PSA_ERROR_BUFFER_TOO_SMALL;
2072 goto exit;
2073 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002074
2075 switch( operation->alg )
2076 {
2077#if defined(MBEDTLS_MD2_C)
2078 case PSA_ALG_MD2:
2079 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2080 break;
2081#endif
2082#if defined(MBEDTLS_MD4_C)
2083 case PSA_ALG_MD4:
2084 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2085 break;
2086#endif
2087#if defined(MBEDTLS_MD5_C)
2088 case PSA_ALG_MD5:
2089 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2090 break;
2091#endif
2092#if defined(MBEDTLS_RIPEMD160_C)
2093 case PSA_ALG_RIPEMD160:
2094 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2095 break;
2096#endif
2097#if defined(MBEDTLS_SHA1_C)
2098 case PSA_ALG_SHA_1:
2099 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2100 break;
2101#endif
2102#if defined(MBEDTLS_SHA256_C)
2103 case PSA_ALG_SHA_224:
2104 case PSA_ALG_SHA_256:
2105 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2106 break;
2107#endif
2108#if defined(MBEDTLS_SHA512_C)
2109 case PSA_ALG_SHA_384:
2110 case PSA_ALG_SHA_512:
2111 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2112 break;
2113#endif
2114 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002115 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002116 }
itayzafrir40835d42018-08-02 13:14:17 +03002117 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002118
itayzafrir40835d42018-08-02 13:14:17 +03002119exit:
2120 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002121 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002122 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002123 return( psa_hash_abort( operation ) );
2124 }
2125 else
2126 {
2127 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002128 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002129 }
2130}
2131
Gilles Peskine2d277862018-06-18 15:41:12 +02002132psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2133 const uint8_t *hash,
2134 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002135{
2136 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2137 size_t actual_hash_length;
2138 psa_status_t status = psa_hash_finish( operation,
2139 actual_hash, sizeof( actual_hash ),
2140 &actual_hash_length );
2141 if( status != PSA_SUCCESS )
2142 return( status );
2143 if( actual_hash_length != hash_length )
2144 return( PSA_ERROR_INVALID_SIGNATURE );
2145 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2146 return( PSA_ERROR_INVALID_SIGNATURE );
2147 return( PSA_SUCCESS );
2148}
2149
Gilles Peskineeb35d782019-01-22 17:56:16 +01002150psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2151 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002152{
2153 if( target_operation->alg != 0 )
2154 return( PSA_ERROR_BAD_STATE );
2155
2156 switch( source_operation->alg )
2157 {
2158 case 0:
2159 return( PSA_ERROR_BAD_STATE );
2160#if defined(MBEDTLS_MD2_C)
2161 case PSA_ALG_MD2:
2162 mbedtls_md2_clone( &target_operation->ctx.md2,
2163 &source_operation->ctx.md2 );
2164 break;
2165#endif
2166#if defined(MBEDTLS_MD4_C)
2167 case PSA_ALG_MD4:
2168 mbedtls_md4_clone( &target_operation->ctx.md4,
2169 &source_operation->ctx.md4 );
2170 break;
2171#endif
2172#if defined(MBEDTLS_MD5_C)
2173 case PSA_ALG_MD5:
2174 mbedtls_md5_clone( &target_operation->ctx.md5,
2175 &source_operation->ctx.md5 );
2176 break;
2177#endif
2178#if defined(MBEDTLS_RIPEMD160_C)
2179 case PSA_ALG_RIPEMD160:
2180 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2181 &source_operation->ctx.ripemd160 );
2182 break;
2183#endif
2184#if defined(MBEDTLS_SHA1_C)
2185 case PSA_ALG_SHA_1:
2186 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2187 &source_operation->ctx.sha1 );
2188 break;
2189#endif
2190#if defined(MBEDTLS_SHA256_C)
2191 case PSA_ALG_SHA_224:
2192 case PSA_ALG_SHA_256:
2193 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2194 &source_operation->ctx.sha256 );
2195 break;
2196#endif
2197#if defined(MBEDTLS_SHA512_C)
2198 case PSA_ALG_SHA_384:
2199 case PSA_ALG_SHA_512:
2200 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2201 &source_operation->ctx.sha512 );
2202 break;
2203#endif
2204 default:
2205 return( PSA_ERROR_NOT_SUPPORTED );
2206 }
2207
2208 target_operation->alg = source_operation->alg;
2209 return( PSA_SUCCESS );
2210}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002211
2212
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002213/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002214/* MAC */
2215/****************************************************************/
2216
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002217static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002218 psa_algorithm_t alg,
2219 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002220 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002221 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002222{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002223 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002224 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002225
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002226 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002227 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002228
Gilles Peskine8c9def32018-02-08 10:02:12 +01002229 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2230 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002231 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002232 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002233 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002234 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002235 mode = MBEDTLS_MODE_STREAM;
2236 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002237 case PSA_ALG_CTR:
2238 mode = MBEDTLS_MODE_CTR;
2239 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002240 case PSA_ALG_CFB:
2241 mode = MBEDTLS_MODE_CFB;
2242 break;
2243 case PSA_ALG_OFB:
2244 mode = MBEDTLS_MODE_OFB;
2245 break;
2246 case PSA_ALG_CBC_NO_PADDING:
2247 mode = MBEDTLS_MODE_CBC;
2248 break;
2249 case PSA_ALG_CBC_PKCS7:
2250 mode = MBEDTLS_MODE_CBC;
2251 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002252 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002253 mode = MBEDTLS_MODE_CCM;
2254 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002255 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002256 mode = MBEDTLS_MODE_GCM;
2257 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002258 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2259 mode = MBEDTLS_MODE_CHACHAPOLY;
2260 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002261 default:
2262 return( NULL );
2263 }
2264 }
2265 else if( alg == PSA_ALG_CMAC )
2266 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002267 else
2268 return( NULL );
2269
2270 switch( key_type )
2271 {
2272 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002273 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002274 break;
2275 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002276 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2277 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002278 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002279 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002280 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002281 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002282 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2283 * but two-key Triple-DES is functionally three-key Triple-DES
2284 * with K1=K3, so that's how we present it to mbedtls. */
2285 if( key_bits == 128 )
2286 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002287 break;
2288 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002289 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002290 break;
2291 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002292 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002293 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002294 case PSA_KEY_TYPE_CHACHA20:
2295 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2296 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002297 default:
2298 return( NULL );
2299 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002300 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002301 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002302
Jaeden Amero23bbb752018-06-26 14:16:54 +01002303 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2304 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002305}
2306
Gilles Peskinea05219c2018-11-16 16:02:56 +01002307#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002308static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002309{
Gilles Peskine2d277862018-06-18 15:41:12 +02002310 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002311 {
2312 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002313 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002314 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002315 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002316 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002317 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002318 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002319 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002320 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002321 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002322 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002323 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002324 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002325 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002326 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002327 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002328 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002329 return( 128 );
2330 default:
2331 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002332 }
2333}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002334#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002335
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002336/* Initialize the MAC operation structure. Once this function has been
2337 * called, psa_mac_abort can run and will do the right thing. */
2338static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2339 psa_algorithm_t alg )
2340{
2341 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2342
2343 operation->alg = alg;
2344 operation->key_set = 0;
2345 operation->iv_set = 0;
2346 operation->iv_required = 0;
2347 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002348 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002349
2350#if defined(MBEDTLS_CMAC_C)
2351 if( alg == PSA_ALG_CMAC )
2352 {
2353 operation->iv_required = 0;
2354 mbedtls_cipher_init( &operation->ctx.cmac );
2355 status = PSA_SUCCESS;
2356 }
2357 else
2358#endif /* MBEDTLS_CMAC_C */
2359#if defined(MBEDTLS_MD_C)
2360 if( PSA_ALG_IS_HMAC( operation->alg ) )
2361 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002362 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2363 operation->ctx.hmac.hash_ctx.alg = 0;
2364 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002365 }
2366 else
2367#endif /* MBEDTLS_MD_C */
2368 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002369 if( ! PSA_ALG_IS_MAC( alg ) )
2370 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002371 }
2372
2373 if( status != PSA_SUCCESS )
2374 memset( operation, 0, sizeof( *operation ) );
2375 return( status );
2376}
2377
Gilles Peskine01126fa2018-07-12 17:04:55 +02002378#if defined(MBEDTLS_MD_C)
2379static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2380{
Gilles Peskine3f108122018-12-07 18:14:53 +01002381 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002382 return( psa_hash_abort( &hmac->hash_ctx ) );
2383}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002384
Janos Follath999f6482019-06-11 12:04:10 +01002385#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002386static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2387{
2388 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2389 memset( hmac, 0, sizeof( *hmac ) );
2390}
Janos Follath999f6482019-06-11 12:04:10 +01002391#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002392#endif /* MBEDTLS_MD_C */
2393
Gilles Peskine8c9def32018-02-08 10:02:12 +01002394psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2395{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002396 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002397 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002398 /* The object has (apparently) been initialized but it is not
2399 * in use. It's ok to call abort on such an object, and there's
2400 * nothing to do. */
2401 return( PSA_SUCCESS );
2402 }
2403 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002404#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002405 if( operation->alg == PSA_ALG_CMAC )
2406 {
2407 mbedtls_cipher_free( &operation->ctx.cmac );
2408 }
2409 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002410#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002411#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002412 if( PSA_ALG_IS_HMAC( operation->alg ) )
2413 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002414 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002415 }
2416 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002417#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002418 {
2419 /* Sanity check (shouldn't happen: operation->alg should
2420 * always have been initialized to a valid value). */
2421 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002422 }
Moran Peker41deec42018-04-04 15:43:05 +03002423
Gilles Peskine8c9def32018-02-08 10:02:12 +01002424 operation->alg = 0;
2425 operation->key_set = 0;
2426 operation->iv_set = 0;
2427 operation->iv_required = 0;
2428 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002429 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002430
Gilles Peskine8c9def32018-02-08 10:02:12 +01002431 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002432
2433bad_state:
2434 /* If abort is called on an uninitialized object, we can't trust
2435 * anything. Wipe the object in case it contains confidential data.
2436 * This may result in a memory leak if a pointer gets overwritten,
2437 * but it's too late to do anything about this. */
2438 memset( operation, 0, sizeof( *operation ) );
2439 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002440}
2441
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002442#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002443static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002444 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002445 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002446 const mbedtls_cipher_info_t *cipher_info )
2447{
2448 int ret;
2449
2450 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002451
2452 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2453 if( ret != 0 )
2454 return( ret );
2455
2456 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2457 slot->data.raw.data,
2458 key_bits );
2459 return( ret );
2460}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002461#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002462
Gilles Peskine248051a2018-06-20 16:09:38 +02002463#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002464static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2465 const uint8_t *key,
2466 size_t key_length,
2467 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002468{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002469 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002470 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002471 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002472 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002473 psa_status_t status;
2474
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002475 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2476 * overflow below. This should never trigger if the hash algorithm
2477 * is implemented correctly. */
2478 /* The size checks against the ipad and opad buffers cannot be written
2479 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2480 * because that triggers -Wlogical-op on GCC 7.3. */
2481 if( block_size > sizeof( ipad ) )
2482 return( PSA_ERROR_NOT_SUPPORTED );
2483 if( block_size > sizeof( hmac->opad ) )
2484 return( PSA_ERROR_NOT_SUPPORTED );
2485 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002486 return( PSA_ERROR_NOT_SUPPORTED );
2487
Gilles Peskined223b522018-06-11 18:12:58 +02002488 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002489 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002490 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002491 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002492 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002493 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002494 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002495 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002496 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002497 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002498 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002499 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002500 }
Gilles Peskine96889972018-07-12 17:07:03 +02002501 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2502 * but it is permitted. It is common when HMAC is used in HKDF, for
2503 * example. Don't call `memcpy` in the 0-length because `key` could be
2504 * an invalid pointer which would make the behavior undefined. */
2505 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002506 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002507
Gilles Peskined223b522018-06-11 18:12:58 +02002508 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2509 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002510 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002511 ipad[i] ^= 0x36;
2512 memset( ipad + key_length, 0x36, block_size - key_length );
2513
2514 /* Copy the key material from ipad to opad, flipping the requisite bits,
2515 * and filling the rest of opad with the requisite constant. */
2516 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002517 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2518 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002519
Gilles Peskine01126fa2018-07-12 17:04:55 +02002520 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002521 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002522 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002523
Gilles Peskine01126fa2018-07-12 17:04:55 +02002524 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002525
2526cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002527 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002528
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002529 return( status );
2530}
Gilles Peskine248051a2018-06-20 16:09:38 +02002531#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002532
Gilles Peskine89167cb2018-07-08 20:12:23 +02002533static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002534 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002535 psa_algorithm_t alg,
2536 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002538 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002539 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002540 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002541 psa_key_usage_t usage =
2542 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002543 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002544 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002546 /* A context must be freshly initialized before it can be set up. */
2547 if( operation->alg != 0 )
2548 {
2549 return( PSA_ERROR_BAD_STATE );
2550 }
2551
Gilles Peskined911eb72018-08-14 15:18:45 +02002552 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002553 if( status != PSA_SUCCESS )
2554 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002555 if( is_sign )
2556 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557
Gilles Peskine28f8f302019-07-24 13:30:31 +02002558 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002559 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002560 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002561 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002562
Gilles Peskine8c9def32018-02-08 10:02:12 +01002563#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002564 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002565 {
2566 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002567 mbedtls_cipher_info_from_psa( full_length_alg,
2568 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002569 int ret;
2570 if( cipher_info == NULL )
2571 {
2572 status = PSA_ERROR_NOT_SUPPORTED;
2573 goto exit;
2574 }
2575 operation->mac_size = cipher_info->block_size;
2576 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2577 status = mbedtls_to_psa_error( ret );
2578 }
2579 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002580#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002581#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002582 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002583 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002584 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002585 if( hash_alg == 0 )
2586 {
2587 status = PSA_ERROR_NOT_SUPPORTED;
2588 goto exit;
2589 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002590
2591 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2592 /* Sanity check. This shouldn't fail on a valid configuration. */
2593 if( operation->mac_size == 0 ||
2594 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2595 {
2596 status = PSA_ERROR_NOT_SUPPORTED;
2597 goto exit;
2598 }
2599
Gilles Peskine01126fa2018-07-12 17:04:55 +02002600 if( slot->type != PSA_KEY_TYPE_HMAC )
2601 {
2602 status = PSA_ERROR_INVALID_ARGUMENT;
2603 goto exit;
2604 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002605
Gilles Peskine01126fa2018-07-12 17:04:55 +02002606 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2607 slot->data.raw.data,
2608 slot->data.raw.bytes,
2609 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002610 }
2611 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002613 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002614 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002615 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002616 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002617
Gilles Peskined911eb72018-08-14 15:18:45 +02002618 if( truncated == 0 )
2619 {
2620 /* The "normal" case: untruncated algorithm. Nothing to do. */
2621 }
2622 else if( truncated < 4 )
2623 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002624 /* A very short MAC is too short for security since it can be
2625 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2626 * so we make this our minimum, even though 32 bits is still
2627 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002628 status = PSA_ERROR_NOT_SUPPORTED;
2629 }
2630 else if( truncated > operation->mac_size )
2631 {
2632 /* It's impossible to "truncate" to a larger length. */
2633 status = PSA_ERROR_INVALID_ARGUMENT;
2634 }
2635 else
2636 operation->mac_size = truncated;
2637
Gilles Peskinefbfac682018-07-08 20:51:54 +02002638exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002639 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002640 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002641 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002642 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002643 else
2644 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002645 operation->key_set = 1;
2646 }
2647 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002648}
2649
Gilles Peskine89167cb2018-07-08 20:12:23 +02002650psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002651 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002652 psa_algorithm_t alg )
2653{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002654 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002655}
2656
2657psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002658 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002659 psa_algorithm_t alg )
2660{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002661 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002662}
2663
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2665 const uint8_t *input,
2666 size_t input_length )
2667{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002668 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002669 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002670 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002671 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002672 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002673 operation->has_input = 1;
2674
Gilles Peskine8c9def32018-02-08 10:02:12 +01002675#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002676 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002677 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002678 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2679 input, input_length );
2680 status = mbedtls_to_psa_error( ret );
2681 }
2682 else
2683#endif /* MBEDTLS_CMAC_C */
2684#if defined(MBEDTLS_MD_C)
2685 if( PSA_ALG_IS_HMAC( operation->alg ) )
2686 {
2687 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2688 input_length );
2689 }
2690 else
2691#endif /* MBEDTLS_MD_C */
2692 {
2693 /* This shouldn't happen if `operation` was initialized by
2694 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002695 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002696 }
2697
Gilles Peskinefbfac682018-07-08 20:51:54 +02002698 if( status != PSA_SUCCESS )
2699 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002700 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002701}
2702
Gilles Peskine01126fa2018-07-12 17:04:55 +02002703#if defined(MBEDTLS_MD_C)
2704static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2705 uint8_t *mac,
2706 size_t mac_size )
2707{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002708 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002709 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2710 size_t hash_size = 0;
2711 size_t block_size = psa_get_hash_block_size( hash_alg );
2712 psa_status_t status;
2713
Gilles Peskine01126fa2018-07-12 17:04:55 +02002714 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2715 if( status != PSA_SUCCESS )
2716 return( status );
2717 /* From here on, tmp needs to be wiped. */
2718
2719 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2720 if( status != PSA_SUCCESS )
2721 goto exit;
2722
2723 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2724 if( status != PSA_SUCCESS )
2725 goto exit;
2726
2727 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2728 if( status != PSA_SUCCESS )
2729 goto exit;
2730
Gilles Peskined911eb72018-08-14 15:18:45 +02002731 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2732 if( status != PSA_SUCCESS )
2733 goto exit;
2734
2735 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002736
2737exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002738 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002739 return( status );
2740}
2741#endif /* MBEDTLS_MD_C */
2742
mohammad16036df908f2018-04-02 08:34:15 -07002743static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002744 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002745 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002747 if( ! operation->key_set )
2748 return( PSA_ERROR_BAD_STATE );
2749 if( operation->iv_required && ! operation->iv_set )
2750 return( PSA_ERROR_BAD_STATE );
2751
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752 if( mac_size < operation->mac_size )
2753 return( PSA_ERROR_BUFFER_TOO_SMALL );
2754
Gilles Peskine8c9def32018-02-08 10:02:12 +01002755#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002756 if( operation->alg == PSA_ALG_CMAC )
2757 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002758 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2759 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2760 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002761 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002762 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002763 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002764 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002765 else
2766#endif /* MBEDTLS_CMAC_C */
2767#if defined(MBEDTLS_MD_C)
2768 if( PSA_ALG_IS_HMAC( operation->alg ) )
2769 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002770 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002771 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002772 }
2773 else
2774#endif /* MBEDTLS_MD_C */
2775 {
2776 /* This shouldn't happen if `operation` was initialized by
2777 * a setup function. */
2778 return( PSA_ERROR_BAD_STATE );
2779 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002780}
2781
Gilles Peskineacd4be32018-07-08 19:56:25 +02002782psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2783 uint8_t *mac,
2784 size_t mac_size,
2785 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002786{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002787 psa_status_t status;
2788
Jaeden Amero252ef282019-02-15 14:05:35 +00002789 if( operation->alg == 0 )
2790 {
2791 return( PSA_ERROR_BAD_STATE );
2792 }
2793
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002794 /* Fill the output buffer with something that isn't a valid mac
2795 * (barring an attack on the mac and deliberately-crafted input),
2796 * in case the caller doesn't check the return status properly. */
2797 *mac_length = mac_size;
2798 /* If mac_size is 0 then mac may be NULL and then the
2799 * call to memset would have undefined behavior. */
2800 if( mac_size != 0 )
2801 memset( mac, '!', mac_size );
2802
Gilles Peskine89167cb2018-07-08 20:12:23 +02002803 if( ! operation->is_sign )
2804 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002805 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002806 }
mohammad16036df908f2018-04-02 08:34:15 -07002807
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002808 status = psa_mac_finish_internal( operation, mac, mac_size );
2809
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002810 if( status == PSA_SUCCESS )
2811 {
2812 status = psa_mac_abort( operation );
2813 if( status == PSA_SUCCESS )
2814 *mac_length = operation->mac_size;
2815 else
2816 memset( mac, '!', mac_size );
2817 }
2818 else
2819 psa_mac_abort( operation );
2820 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002821}
2822
Gilles Peskineacd4be32018-07-08 19:56:25 +02002823psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2824 const uint8_t *mac,
2825 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002826{
Gilles Peskine828ed142018-06-18 23:25:51 +02002827 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002828 psa_status_t status;
2829
Jaeden Amero252ef282019-02-15 14:05:35 +00002830 if( operation->alg == 0 )
2831 {
2832 return( PSA_ERROR_BAD_STATE );
2833 }
2834
Gilles Peskine89167cb2018-07-08 20:12:23 +02002835 if( operation->is_sign )
2836 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002837 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002838 }
2839 if( operation->mac_size != mac_length )
2840 {
2841 status = PSA_ERROR_INVALID_SIGNATURE;
2842 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002843 }
mohammad16036df908f2018-04-02 08:34:15 -07002844
2845 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002846 actual_mac, sizeof( actual_mac ) );
2847
2848 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2849 status = PSA_ERROR_INVALID_SIGNATURE;
2850
2851cleanup:
2852 if( status == PSA_SUCCESS )
2853 status = psa_mac_abort( operation );
2854 else
2855 psa_mac_abort( operation );
2856
Gilles Peskine3f108122018-12-07 18:14:53 +01002857 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002858
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002859 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002860}
2861
2862
Gilles Peskine20035e32018-02-03 22:44:14 +01002863
Gilles Peskine20035e32018-02-03 22:44:14 +01002864/****************************************************************/
2865/* Asymmetric cryptography */
2866/****************************************************************/
2867
Gilles Peskine2b450e32018-06-27 15:42:46 +02002868#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002869/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002870 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002871static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2872 size_t hash_length,
2873 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002874{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002875 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002876 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002877 *md_alg = mbedtls_md_get_type( md_info );
2878
2879 /* The Mbed TLS RSA module uses an unsigned int for hash length
2880 * parameters. Validate that it fits so that we don't risk an
2881 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002882#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002883 if( hash_length > UINT_MAX )
2884 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002885#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002886
2887#if defined(MBEDTLS_PKCS1_V15)
2888 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2889 * must be correct. */
2890 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2891 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002892 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002893 if( md_info == NULL )
2894 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002895 if( mbedtls_md_get_size( md_info ) != hash_length )
2896 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002897 }
2898#endif /* MBEDTLS_PKCS1_V15 */
2899
2900#if defined(MBEDTLS_PKCS1_V21)
2901 /* PSS requires a hash internally. */
2902 if( PSA_ALG_IS_RSA_PSS( alg ) )
2903 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002904 if( md_info == NULL )
2905 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002906 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002907#endif /* MBEDTLS_PKCS1_V21 */
2908
Gilles Peskine61b91d42018-06-08 16:09:36 +02002909 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002910}
2911
Gilles Peskine2b450e32018-06-27 15:42:46 +02002912static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2913 psa_algorithm_t alg,
2914 const uint8_t *hash,
2915 size_t hash_length,
2916 uint8_t *signature,
2917 size_t signature_size,
2918 size_t *signature_length )
2919{
2920 psa_status_t status;
2921 int ret;
2922 mbedtls_md_type_t md_alg;
2923
2924 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2925 if( status != PSA_SUCCESS )
2926 return( status );
2927
Gilles Peskine630a18a2018-06-29 17:49:35 +02002928 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002929 return( PSA_ERROR_BUFFER_TOO_SMALL );
2930
2931#if defined(MBEDTLS_PKCS1_V15)
2932 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2933 {
2934 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2935 MBEDTLS_MD_NONE );
2936 ret = mbedtls_rsa_pkcs1_sign( rsa,
2937 mbedtls_ctr_drbg_random,
2938 &global_data.ctr_drbg,
2939 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002940 md_alg,
2941 (unsigned int) hash_length,
2942 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002943 signature );
2944 }
2945 else
2946#endif /* MBEDTLS_PKCS1_V15 */
2947#if defined(MBEDTLS_PKCS1_V21)
2948 if( PSA_ALG_IS_RSA_PSS( alg ) )
2949 {
2950 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2951 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2952 mbedtls_ctr_drbg_random,
2953 &global_data.ctr_drbg,
2954 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002955 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002956 (unsigned int) hash_length,
2957 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002958 signature );
2959 }
2960 else
2961#endif /* MBEDTLS_PKCS1_V21 */
2962 {
2963 return( PSA_ERROR_INVALID_ARGUMENT );
2964 }
2965
2966 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002967 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002968 return( mbedtls_to_psa_error( ret ) );
2969}
2970
2971static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2972 psa_algorithm_t alg,
2973 const uint8_t *hash,
2974 size_t hash_length,
2975 const uint8_t *signature,
2976 size_t signature_length )
2977{
2978 psa_status_t status;
2979 int ret;
2980 mbedtls_md_type_t md_alg;
2981
2982 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2983 if( status != PSA_SUCCESS )
2984 return( status );
2985
Gilles Peskine630a18a2018-06-29 17:49:35 +02002986 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002987 return( PSA_ERROR_BUFFER_TOO_SMALL );
2988
2989#if defined(MBEDTLS_PKCS1_V15)
2990 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2991 {
2992 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2993 MBEDTLS_MD_NONE );
2994 ret = mbedtls_rsa_pkcs1_verify( rsa,
2995 mbedtls_ctr_drbg_random,
2996 &global_data.ctr_drbg,
2997 MBEDTLS_RSA_PUBLIC,
2998 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002999 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003000 hash,
3001 signature );
3002 }
3003 else
3004#endif /* MBEDTLS_PKCS1_V15 */
3005#if defined(MBEDTLS_PKCS1_V21)
3006 if( PSA_ALG_IS_RSA_PSS( alg ) )
3007 {
3008 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3009 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3010 mbedtls_ctr_drbg_random,
3011 &global_data.ctr_drbg,
3012 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003013 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003014 (unsigned int) hash_length,
3015 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003016 signature );
3017 }
3018 else
3019#endif /* MBEDTLS_PKCS1_V21 */
3020 {
3021 return( PSA_ERROR_INVALID_ARGUMENT );
3022 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003023
3024 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3025 * the rest of the signature is invalid". This has little use in
3026 * practice and PSA doesn't report this distinction. */
3027 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3028 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003029 return( mbedtls_to_psa_error( ret ) );
3030}
3031#endif /* MBEDTLS_RSA_C */
3032
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003033#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003034/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3035 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3036 * (even though these functions don't modify it). */
3037static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3038 psa_algorithm_t alg,
3039 const uint8_t *hash,
3040 size_t hash_length,
3041 uint8_t *signature,
3042 size_t signature_size,
3043 size_t *signature_length )
3044{
3045 int ret;
3046 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003047 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003048 mbedtls_mpi_init( &r );
3049 mbedtls_mpi_init( &s );
3050
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003051 if( signature_size < 2 * curve_bytes )
3052 {
3053 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3054 goto cleanup;
3055 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003056
Gilles Peskinea05219c2018-11-16 16:02:56 +01003057#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003058 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3059 {
3060 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3061 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3062 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3063 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3064 hash, hash_length,
3065 md_alg ) );
3066 }
3067 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003068#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003069 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003070 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003071 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3072 hash, hash_length,
3073 mbedtls_ctr_drbg_random,
3074 &global_data.ctr_drbg ) );
3075 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003076
3077 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3078 signature,
3079 curve_bytes ) );
3080 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3081 signature + curve_bytes,
3082 curve_bytes ) );
3083
3084cleanup:
3085 mbedtls_mpi_free( &r );
3086 mbedtls_mpi_free( &s );
3087 if( ret == 0 )
3088 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003089 return( mbedtls_to_psa_error( ret ) );
3090}
3091
3092static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3093 const uint8_t *hash,
3094 size_t hash_length,
3095 const uint8_t *signature,
3096 size_t signature_length )
3097{
3098 int ret;
3099 mbedtls_mpi r, s;
3100 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3101 mbedtls_mpi_init( &r );
3102 mbedtls_mpi_init( &s );
3103
3104 if( signature_length != 2 * curve_bytes )
3105 return( PSA_ERROR_INVALID_SIGNATURE );
3106
3107 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3108 signature,
3109 curve_bytes ) );
3110 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3111 signature + curve_bytes,
3112 curve_bytes ) );
3113
3114 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3115 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003116
3117cleanup:
3118 mbedtls_mpi_free( &r );
3119 mbedtls_mpi_free( &s );
3120 return( mbedtls_to_psa_error( ret ) );
3121}
3122#endif /* MBEDTLS_ECDSA_C */
3123
Gilles Peskinec5487a82018-12-03 18:08:14 +01003124psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003125 psa_algorithm_t alg,
3126 const uint8_t *hash,
3127 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003128 uint8_t *signature,
3129 size_t signature_size,
3130 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003131{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003132 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003133 psa_status_t status;
3134
3135 *signature_length = signature_size;
3136
Gilles Peskine28f8f302019-07-24 13:30:31 +02003137 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003138 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003139 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003140 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003141 {
3142 status = PSA_ERROR_INVALID_ARGUMENT;
3143 goto exit;
3144 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003145
Gilles Peskine20035e32018-02-03 22:44:14 +01003146#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003147 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003148 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003149 status = psa_rsa_sign( slot->data.rsa,
3150 alg,
3151 hash, hash_length,
3152 signature, signature_size,
3153 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003154 }
3155 else
3156#endif /* defined(MBEDTLS_RSA_C) */
3157#if defined(MBEDTLS_ECP_C)
3158 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3159 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003160#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003161 if(
3162#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3163 PSA_ALG_IS_ECDSA( alg )
3164#else
3165 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3166#endif
3167 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003168 status = psa_ecdsa_sign( slot->data.ecp,
3169 alg,
3170 hash, hash_length,
3171 signature, signature_size,
3172 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003173 else
3174#endif /* defined(MBEDTLS_ECDSA_C) */
3175 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003176 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003177 }
itayzafrir5c753392018-05-08 11:18:38 +03003178 }
3179 else
3180#endif /* defined(MBEDTLS_ECP_C) */
3181 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003182 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003183 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003184
3185exit:
3186 /* Fill the unused part of the output buffer (the whole buffer on error,
3187 * the trailing part on success) with something that isn't a valid mac
3188 * (barring an attack on the mac and deliberately-crafted input),
3189 * in case the caller doesn't check the return status properly. */
3190 if( status == PSA_SUCCESS )
3191 memset( signature + *signature_length, '!',
3192 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003193 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003194 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003195 /* If signature_size is 0 then we have nothing to do. We must not call
3196 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003197 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003198}
3199
Gilles Peskinec5487a82018-12-03 18:08:14 +01003200psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003201 psa_algorithm_t alg,
3202 const uint8_t *hash,
3203 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003204 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003205 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003206{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003207 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003208 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003209
Gilles Peskine28f8f302019-07-24 13:30:31 +02003210 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003211 if( status != PSA_SUCCESS )
3212 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003213
Gilles Peskine61b91d42018-06-08 16:09:36 +02003214#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003215 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003216 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003217 return( psa_rsa_verify( slot->data.rsa,
3218 alg,
3219 hash, hash_length,
3220 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003221 }
3222 else
3223#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003224#if defined(MBEDTLS_ECP_C)
3225 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3226 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003227#if defined(MBEDTLS_ECDSA_C)
3228 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003229 return( psa_ecdsa_verify( slot->data.ecp,
3230 hash, hash_length,
3231 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003232 else
3233#endif /* defined(MBEDTLS_ECDSA_C) */
3234 {
3235 return( PSA_ERROR_INVALID_ARGUMENT );
3236 }
itayzafrir5c753392018-05-08 11:18:38 +03003237 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003238 else
3239#endif /* defined(MBEDTLS_ECP_C) */
3240 {
3241 return( PSA_ERROR_NOT_SUPPORTED );
3242 }
3243}
3244
Gilles Peskine072ac562018-06-30 00:21:29 +02003245#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3246static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3247 mbedtls_rsa_context *rsa )
3248{
3249 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3250 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3251 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3252 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3253}
3254#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3255
Gilles Peskinec5487a82018-12-03 18:08:14 +01003256psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003257 psa_algorithm_t alg,
3258 const uint8_t *input,
3259 size_t input_length,
3260 const uint8_t *salt,
3261 size_t salt_length,
3262 uint8_t *output,
3263 size_t output_size,
3264 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003265{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003266 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003267 psa_status_t status;
3268
Darryl Green5cc689a2018-07-24 15:34:10 +01003269 (void) input;
3270 (void) input_length;
3271 (void) salt;
3272 (void) output;
3273 (void) output_size;
3274
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003275 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003276
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003277 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3278 return( PSA_ERROR_INVALID_ARGUMENT );
3279
Gilles Peskine28f8f302019-07-24 13:30:31 +02003280 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003281 if( status != PSA_SUCCESS )
3282 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003283 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003284 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003286
3287#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003288 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003289 {
3290 mbedtls_rsa_context *rsa = slot->data.rsa;
3291 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003292 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003293 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003294#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003295 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003296 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003297 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3298 mbedtls_ctr_drbg_random,
3299 &global_data.ctr_drbg,
3300 MBEDTLS_RSA_PUBLIC,
3301 input_length,
3302 input,
3303 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304 }
3305 else
3306#endif /* MBEDTLS_PKCS1_V15 */
3307#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003308 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003309 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003310 psa_rsa_oaep_set_padding_mode( alg, rsa );
3311 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3312 mbedtls_ctr_drbg_random,
3313 &global_data.ctr_drbg,
3314 MBEDTLS_RSA_PUBLIC,
3315 salt, salt_length,
3316 input_length,
3317 input,
3318 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003319 }
3320 else
3321#endif /* MBEDTLS_PKCS1_V21 */
3322 {
3323 return( PSA_ERROR_INVALID_ARGUMENT );
3324 }
3325 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003326 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003327 return( mbedtls_to_psa_error( ret ) );
3328 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003329 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003330#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003331 {
3332 return( PSA_ERROR_NOT_SUPPORTED );
3333 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003334}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003335
Gilles Peskinec5487a82018-12-03 18:08:14 +01003336psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003337 psa_algorithm_t alg,
3338 const uint8_t *input,
3339 size_t input_length,
3340 const uint8_t *salt,
3341 size_t salt_length,
3342 uint8_t *output,
3343 size_t output_size,
3344 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003345{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003346 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003347 psa_status_t status;
3348
Darryl Green5cc689a2018-07-24 15:34:10 +01003349 (void) input;
3350 (void) input_length;
3351 (void) salt;
3352 (void) output;
3353 (void) output_size;
3354
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003355 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003356
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003357 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3358 return( PSA_ERROR_INVALID_ARGUMENT );
3359
Gilles Peskine28f8f302019-07-24 13:30:31 +02003360 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003361 if( status != PSA_SUCCESS )
3362 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003363 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003364 return( PSA_ERROR_INVALID_ARGUMENT );
3365
3366#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003367 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003368 {
3369 mbedtls_rsa_context *rsa = slot->data.rsa;
3370 int ret;
3371
Gilles Peskine630a18a2018-06-29 17:49:35 +02003372 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003373 return( PSA_ERROR_INVALID_ARGUMENT );
3374
3375#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003376 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003377 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003378 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3379 mbedtls_ctr_drbg_random,
3380 &global_data.ctr_drbg,
3381 MBEDTLS_RSA_PRIVATE,
3382 output_length,
3383 input,
3384 output,
3385 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003386 }
3387 else
3388#endif /* MBEDTLS_PKCS1_V15 */
3389#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003390 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003391 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003392 psa_rsa_oaep_set_padding_mode( alg, rsa );
3393 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3394 mbedtls_ctr_drbg_random,
3395 &global_data.ctr_drbg,
3396 MBEDTLS_RSA_PRIVATE,
3397 salt, salt_length,
3398 output_length,
3399 input,
3400 output,
3401 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003402 }
3403 else
3404#endif /* MBEDTLS_PKCS1_V21 */
3405 {
3406 return( PSA_ERROR_INVALID_ARGUMENT );
3407 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003408
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003409 return( mbedtls_to_psa_error( ret ) );
3410 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003411 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003412#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003413 {
3414 return( PSA_ERROR_NOT_SUPPORTED );
3415 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003416}
Gilles Peskine20035e32018-02-03 22:44:14 +01003417
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003418
3419
mohammad1603503973b2018-03-12 15:59:30 +02003420/****************************************************************/
3421/* Symmetric cryptography */
3422/****************************************************************/
3423
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003424/* Initialize the cipher operation structure. Once this function has been
3425 * called, psa_cipher_abort can run and will do the right thing. */
3426static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3427 psa_algorithm_t alg )
3428{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003429 if( ! PSA_ALG_IS_CIPHER( alg ) )
3430 {
3431 memset( operation, 0, sizeof( *operation ) );
3432 return( PSA_ERROR_INVALID_ARGUMENT );
3433 }
3434
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003435 operation->alg = alg;
3436 operation->key_set = 0;
3437 operation->iv_set = 0;
3438 operation->iv_required = 1;
3439 operation->iv_size = 0;
3440 operation->block_size = 0;
3441 mbedtls_cipher_init( &operation->ctx.cipher );
3442 return( PSA_SUCCESS );
3443}
3444
Gilles Peskinee553c652018-06-04 16:22:46 +02003445static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003446 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003447 psa_algorithm_t alg,
3448 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003449{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003450 int ret = 0;
3451 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003452 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003453 size_t key_bits;
3454 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003455 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3456 PSA_KEY_USAGE_ENCRYPT :
3457 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003458
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003459 /* A context must be freshly initialized before it can be set up. */
3460 if( operation->alg != 0 )
3461 {
3462 return( PSA_ERROR_BAD_STATE );
3463 }
3464
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003465 status = psa_cipher_init( operation, alg );
3466 if( status != PSA_SUCCESS )
3467 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003468
Gilles Peskine28f8f302019-07-24 13:30:31 +02003469 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003470 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003471 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003472 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003473
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003474 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003475 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003476 {
3477 status = PSA_ERROR_NOT_SUPPORTED;
3478 goto exit;
3479 }
mohammad1603503973b2018-03-12 15:59:30 +02003480
mohammad1603503973b2018-03-12 15:59:30 +02003481 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003482 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003483 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003484
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003485#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003486 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003487 {
3488 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003489 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003490 memcpy( keys, slot->data.raw.data, 16 );
3491 memcpy( keys + 16, slot->data.raw.data, 8 );
3492 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3493 keys,
3494 192, cipher_operation );
3495 }
3496 else
3497#endif
3498 {
3499 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3500 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003501 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003502 }
Moran Peker41deec42018-04-04 15:43:05 +03003503 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003504 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003505
mohammad16038481e742018-03-18 13:57:31 +02003506#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003507 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003508 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003509 case PSA_ALG_CBC_NO_PADDING:
3510 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3511 MBEDTLS_PADDING_NONE );
3512 break;
3513 case PSA_ALG_CBC_PKCS7:
3514 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3515 MBEDTLS_PADDING_PKCS7 );
3516 break;
3517 default:
3518 /* The algorithm doesn't involve padding. */
3519 ret = 0;
3520 break;
3521 }
3522 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003523 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003524#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3525
mohammad1603503973b2018-03-12 15:59:30 +02003526 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003527 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3528 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3529 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003530 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003531 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003532 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003533#if defined(MBEDTLS_CHACHA20_C)
3534 else
3535 if( alg == PSA_ALG_CHACHA20 )
3536 operation->iv_size = 12;
3537#endif
mohammad1603503973b2018-03-12 15:59:30 +02003538
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003539exit:
3540 if( status == 0 )
3541 status = mbedtls_to_psa_error( ret );
3542 if( status != 0 )
3543 psa_cipher_abort( operation );
3544 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003545}
3546
Gilles Peskinefe119512018-07-08 21:39:34 +02003547psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003548 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003549 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003550{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003551 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003552}
3553
Gilles Peskinefe119512018-07-08 21:39:34 +02003554psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003555 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003556 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003557{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003558 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003559}
3560
Gilles Peskinefe119512018-07-08 21:39:34 +02003561psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003562 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003563 size_t iv_size,
3564 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003565{
itayzafrir534bd7c2018-08-02 13:56:32 +03003566 psa_status_t status;
3567 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003568 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003569 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003570 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003571 }
Moran Peker41deec42018-04-04 15:43:05 +03003572 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003573 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003574 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003575 goto exit;
3576 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003577 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3578 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003579 if( ret != 0 )
3580 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003581 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003582 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003583 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003584
mohammad16038481e742018-03-18 13:57:31 +02003585 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003586 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003587
Moran Peker395db872018-05-31 14:07:14 +03003588exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003589 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003590 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003591 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003592}
3593
Gilles Peskinefe119512018-07-08 21:39:34 +02003594psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003595 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003596 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003597{
itayzafrir534bd7c2018-08-02 13:56:32 +03003598 psa_status_t status;
3599 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003600 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003601 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003602 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003603 }
Moran Pekera28258c2018-05-29 16:25:04 +03003604 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003605 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003606 status = PSA_ERROR_INVALID_ARGUMENT;
3607 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003608 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003609 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3610 status = mbedtls_to_psa_error( ret );
3611exit:
3612 if( status == PSA_SUCCESS )
3613 operation->iv_set = 1;
3614 else
mohammad1603503973b2018-03-12 15:59:30 +02003615 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003616 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003617}
3618
Gilles Peskinee553c652018-06-04 16:22:46 +02003619psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3620 const uint8_t *input,
3621 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003622 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003623 size_t output_size,
3624 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003625{
itayzafrir534bd7c2018-08-02 13:56:32 +03003626 psa_status_t status;
3627 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003628 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003629
3630 if( operation->alg == 0 )
3631 {
3632 return( PSA_ERROR_BAD_STATE );
3633 }
3634
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003635 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003636 {
3637 /* Take the unprocessed partial block left over from previous
3638 * update calls, if any, plus the input to this call. Remove
3639 * the last partial block, if any. You get the data that will be
3640 * output in this call. */
3641 expected_output_size =
3642 ( operation->ctx.cipher.unprocessed_len + input_length )
3643 / operation->block_size * operation->block_size;
3644 }
3645 else
3646 {
3647 expected_output_size = input_length;
3648 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003649
Gilles Peskine89d789c2018-06-04 17:17:16 +02003650 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003651 {
3652 status = PSA_ERROR_BUFFER_TOO_SMALL;
3653 goto exit;
3654 }
mohammad160382759612018-03-12 18:16:40 +02003655
mohammad1603503973b2018-03-12 15:59:30 +02003656 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003657 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003658 status = mbedtls_to_psa_error( ret );
3659exit:
3660 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003661 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003662 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003663}
3664
Gilles Peskinee553c652018-06-04 16:22:46 +02003665psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3666 uint8_t *output,
3667 size_t output_size,
3668 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003669{
David Saadab4ecc272019-02-14 13:48:10 +02003670 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003671 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003672 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003673
mohammad1603503973b2018-03-12 15:59:30 +02003674 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003675 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003676 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003677 }
3678 if( operation->iv_required && ! operation->iv_set )
3679 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003680 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003681 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003682
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003683 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003684 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3685 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003686 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003687 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003688 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003689 }
3690
Janos Follath315b51c2018-07-09 16:04:51 +01003691 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3692 temp_output_buffer,
3693 output_length );
3694 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003695 {
Janos Follath315b51c2018-07-09 16:04:51 +01003696 status = mbedtls_to_psa_error( cipher_ret );
3697 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003698 }
Janos Follath315b51c2018-07-09 16:04:51 +01003699
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003700 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003701 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003702 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003703 memcpy( output, temp_output_buffer, *output_length );
3704 else
3705 {
Janos Follath315b51c2018-07-09 16:04:51 +01003706 status = PSA_ERROR_BUFFER_TOO_SMALL;
3707 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003708 }
mohammad1603503973b2018-03-12 15:59:30 +02003709
Gilles Peskine3f108122018-12-07 18:14:53 +01003710 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003711 status = psa_cipher_abort( operation );
3712
3713 return( status );
3714
3715error:
3716
3717 *output_length = 0;
3718
Gilles Peskine3f108122018-12-07 18:14:53 +01003719 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003720 (void) psa_cipher_abort( operation );
3721
3722 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003723}
3724
Gilles Peskinee553c652018-06-04 16:22:46 +02003725psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3726{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003727 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003728 {
3729 /* The object has (apparently) been initialized but it is not
3730 * in use. It's ok to call abort on such an object, and there's
3731 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003732 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003733 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003734
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003735 /* Sanity check (shouldn't happen: operation->alg should
3736 * always have been initialized to a valid value). */
3737 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3738 return( PSA_ERROR_BAD_STATE );
3739
mohammad1603503973b2018-03-12 15:59:30 +02003740 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003741
Moran Peker41deec42018-04-04 15:43:05 +03003742 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003743 operation->key_set = 0;
3744 operation->iv_set = 0;
3745 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003746 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003747 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003748
Moran Peker395db872018-05-31 14:07:14 +03003749 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003750}
3751
Gilles Peskinea0655c32018-04-30 17:06:50 +02003752
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003753
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003754
mohammad16035955c982018-04-26 00:53:03 +03003755/****************************************************************/
3756/* AEAD */
3757/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003758
Gilles Peskineedf9a652018-08-17 18:11:56 +02003759typedef struct
3760{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003761 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003762 const mbedtls_cipher_info_t *cipher_info;
3763 union
3764 {
3765#if defined(MBEDTLS_CCM_C)
3766 mbedtls_ccm_context ccm;
3767#endif /* MBEDTLS_CCM_C */
3768#if defined(MBEDTLS_GCM_C)
3769 mbedtls_gcm_context gcm;
3770#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003771#if defined(MBEDTLS_CHACHAPOLY_C)
3772 mbedtls_chachapoly_context chachapoly;
3773#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003774 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003775 psa_algorithm_t core_alg;
3776 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003777 uint8_t tag_length;
3778} aead_operation_t;
3779
Gilles Peskine30a9e412019-01-14 18:36:12 +01003780static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003781{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003782 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003783 {
3784#if defined(MBEDTLS_CCM_C)
3785 case PSA_ALG_CCM:
3786 mbedtls_ccm_free( &operation->ctx.ccm );
3787 break;
3788#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003789#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003790 case PSA_ALG_GCM:
3791 mbedtls_gcm_free( &operation->ctx.gcm );
3792 break;
3793#endif /* MBEDTLS_GCM_C */
3794 }
3795}
3796
3797static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003798 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003799 psa_key_usage_t usage,
3800 psa_algorithm_t alg )
3801{
3802 psa_status_t status;
3803 size_t key_bits;
3804 mbedtls_cipher_id_t cipher_id;
3805
Gilles Peskine28f8f302019-07-24 13:30:31 +02003806 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003807 if( status != PSA_SUCCESS )
3808 return( status );
3809
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003810 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003811
3812 operation->cipher_info =
3813 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3814 &cipher_id );
3815 if( operation->cipher_info == NULL )
3816 return( PSA_ERROR_NOT_SUPPORTED );
3817
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003818 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003819 {
3820#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003821 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3822 operation->core_alg = PSA_ALG_CCM;
3823 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003824 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3825 * The call to mbedtls_ccm_encrypt_and_tag or
3826 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003827 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3828 return( PSA_ERROR_INVALID_ARGUMENT );
3829 mbedtls_ccm_init( &operation->ctx.ccm );
3830 status = mbedtls_to_psa_error(
3831 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3832 operation->slot->data.raw.data,
3833 (unsigned int) key_bits ) );
3834 if( status != 0 )
3835 goto cleanup;
3836 break;
3837#endif /* MBEDTLS_CCM_C */
3838
3839#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003840 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3841 operation->core_alg = PSA_ALG_GCM;
3842 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003843 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3844 * The call to mbedtls_gcm_crypt_and_tag or
3845 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003846 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3847 return( PSA_ERROR_INVALID_ARGUMENT );
3848 mbedtls_gcm_init( &operation->ctx.gcm );
3849 status = mbedtls_to_psa_error(
3850 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3851 operation->slot->data.raw.data,
3852 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003853 if( status != 0 )
3854 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003855 break;
3856#endif /* MBEDTLS_GCM_C */
3857
Gilles Peskine26869f22019-05-06 15:25:00 +02003858#if defined(MBEDTLS_CHACHAPOLY_C)
3859 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3860 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3861 operation->full_tag_length = 16;
3862 /* We only support the default tag length. */
3863 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3864 return( PSA_ERROR_NOT_SUPPORTED );
3865 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3866 status = mbedtls_to_psa_error(
3867 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3868 operation->slot->data.raw.data ) );
3869 if( status != 0 )
3870 goto cleanup;
3871 break;
3872#endif /* MBEDTLS_CHACHAPOLY_C */
3873
Gilles Peskineedf9a652018-08-17 18:11:56 +02003874 default:
3875 return( PSA_ERROR_NOT_SUPPORTED );
3876 }
3877
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003878 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3879 {
3880 status = PSA_ERROR_INVALID_ARGUMENT;
3881 goto cleanup;
3882 }
3883 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003884
Gilles Peskineedf9a652018-08-17 18:11:56 +02003885 return( PSA_SUCCESS );
3886
3887cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003888 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003889 return( status );
3890}
3891
Gilles Peskinec5487a82018-12-03 18:08:14 +01003892psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003893 psa_algorithm_t alg,
3894 const uint8_t *nonce,
3895 size_t nonce_length,
3896 const uint8_t *additional_data,
3897 size_t additional_data_length,
3898 const uint8_t *plaintext,
3899 size_t plaintext_length,
3900 uint8_t *ciphertext,
3901 size_t ciphertext_size,
3902 size_t *ciphertext_length )
3903{
mohammad16035955c982018-04-26 00:53:03 +03003904 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003905 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003906 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003907
mohammad1603f08a5502018-06-03 15:05:47 +03003908 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003909
Gilles Peskinec5487a82018-12-03 18:08:14 +01003910 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003911 if( status != PSA_SUCCESS )
3912 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003913
Gilles Peskineedf9a652018-08-17 18:11:56 +02003914 /* For all currently supported modes, the tag is at the end of the
3915 * ciphertext. */
3916 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3917 {
3918 status = PSA_ERROR_BUFFER_TOO_SMALL;
3919 goto exit;
3920 }
3921 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003922
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003923#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003924 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003925 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003926 status = mbedtls_to_psa_error(
3927 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3928 MBEDTLS_GCM_ENCRYPT,
3929 plaintext_length,
3930 nonce, nonce_length,
3931 additional_data, additional_data_length,
3932 plaintext, ciphertext,
3933 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003934 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003935 else
3936#endif /* MBEDTLS_GCM_C */
3937#if defined(MBEDTLS_CCM_C)
3938 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003939 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003940 status = mbedtls_to_psa_error(
3941 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3942 plaintext_length,
3943 nonce, nonce_length,
3944 additional_data,
3945 additional_data_length,
3946 plaintext, ciphertext,
3947 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003948 }
mohammad16035c8845f2018-05-09 05:40:09 -07003949 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003950#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003951#if defined(MBEDTLS_CHACHAPOLY_C)
3952 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3953 {
3954 if( nonce_length != 12 || operation.tag_length != 16 )
3955 {
3956 status = PSA_ERROR_NOT_SUPPORTED;
3957 goto exit;
3958 }
3959 status = mbedtls_to_psa_error(
3960 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3961 plaintext_length,
3962 nonce,
3963 additional_data,
3964 additional_data_length,
3965 plaintext,
3966 ciphertext,
3967 tag ) );
3968 }
3969 else
3970#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003971 {
mohammad1603554faad2018-06-03 15:07:38 +03003972 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003973 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003974
Gilles Peskineedf9a652018-08-17 18:11:56 +02003975 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3976 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003977
Gilles Peskineedf9a652018-08-17 18:11:56 +02003978exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003979 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003980 if( status == PSA_SUCCESS )
3981 *ciphertext_length = plaintext_length + operation.tag_length;
3982 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003983}
3984
Gilles Peskineee652a32018-06-01 19:23:52 +02003985/* Locate the tag in a ciphertext buffer containing the encrypted data
3986 * followed by the tag. Return the length of the part preceding the tag in
3987 * *plaintext_length. This is the size of the plaintext in modes where
3988 * the encrypted data has the same size as the plaintext, such as
3989 * CCM and GCM. */
3990static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3991 const uint8_t *ciphertext,
3992 size_t ciphertext_length,
3993 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003994 const uint8_t **p_tag )
3995{
3996 size_t payload_length;
3997 if( tag_length > ciphertext_length )
3998 return( PSA_ERROR_INVALID_ARGUMENT );
3999 payload_length = ciphertext_length - tag_length;
4000 if( payload_length > plaintext_size )
4001 return( PSA_ERROR_BUFFER_TOO_SMALL );
4002 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004003 return( PSA_SUCCESS );
4004}
4005
Gilles Peskinec5487a82018-12-03 18:08:14 +01004006psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004007 psa_algorithm_t alg,
4008 const uint8_t *nonce,
4009 size_t nonce_length,
4010 const uint8_t *additional_data,
4011 size_t additional_data_length,
4012 const uint8_t *ciphertext,
4013 size_t ciphertext_length,
4014 uint8_t *plaintext,
4015 size_t plaintext_size,
4016 size_t *plaintext_length )
4017{
mohammad16035955c982018-04-26 00:53:03 +03004018 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004019 aead_operation_t operation;
4020 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004021
Gilles Peskineee652a32018-06-01 19:23:52 +02004022 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004023
Gilles Peskinec5487a82018-12-03 18:08:14 +01004024 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004025 if( status != PSA_SUCCESS )
4026 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004027
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004028 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4029 ciphertext, ciphertext_length,
4030 plaintext_size, &tag );
4031 if( status != PSA_SUCCESS )
4032 goto exit;
4033
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004034#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004035 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004036 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004037 status = mbedtls_to_psa_error(
4038 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4039 ciphertext_length - operation.tag_length,
4040 nonce, nonce_length,
4041 additional_data,
4042 additional_data_length,
4043 tag, operation.tag_length,
4044 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004045 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004046 else
4047#endif /* MBEDTLS_GCM_C */
4048#if defined(MBEDTLS_CCM_C)
4049 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004050 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004051 status = mbedtls_to_psa_error(
4052 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4053 ciphertext_length - operation.tag_length,
4054 nonce, nonce_length,
4055 additional_data,
4056 additional_data_length,
4057 ciphertext, plaintext,
4058 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004059 }
mohammad160339574652018-06-01 04:39:53 -07004060 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004061#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004062#if defined(MBEDTLS_CHACHAPOLY_C)
4063 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4064 {
4065 if( nonce_length != 12 || operation.tag_length != 16 )
4066 {
4067 status = PSA_ERROR_NOT_SUPPORTED;
4068 goto exit;
4069 }
4070 status = mbedtls_to_psa_error(
4071 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4072 ciphertext_length - operation.tag_length,
4073 nonce,
4074 additional_data,
4075 additional_data_length,
4076 tag,
4077 ciphertext,
4078 plaintext ) );
4079 }
4080 else
4081#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004082 {
mohammad1603554faad2018-06-03 15:07:38 +03004083 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004084 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004085
Gilles Peskineedf9a652018-08-17 18:11:56 +02004086 if( status != PSA_SUCCESS && plaintext_size != 0 )
4087 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004088
Gilles Peskineedf9a652018-08-17 18:11:56 +02004089exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004090 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004091 if( status == PSA_SUCCESS )
4092 *plaintext_length = ciphertext_length - operation.tag_length;
4093 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004094}
4095
Gilles Peskinea0655c32018-04-30 17:06:50 +02004096
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004097
Gilles Peskine20035e32018-02-03 22:44:14 +01004098/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004099/* Generators */
4100/****************************************************************/
4101
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004102#define HKDF_STATE_INIT 0 /* no input yet */
4103#define HKDF_STATE_STARTED 1 /* got salt */
4104#define HKDF_STATE_KEYED 2 /* got key */
4105#define HKDF_STATE_OUTPUT 3 /* output started */
4106
Gilles Peskinecbe66502019-05-16 16:59:18 +02004107static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004108 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004109{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004110 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4111 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004112 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004113 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004114}
4115
4116
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004117psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004118{
4119 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004120 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004121 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004122 {
4123 /* The object has (apparently) been initialized but it is not
4124 * in use. It's ok to call abort on such an object, and there's
4125 * nothing to do. */
4126 }
4127 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004128#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004129 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004130 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004131 mbedtls_free( operation->ctx.hkdf.info );
4132 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004133 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004134 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004135 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004136 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004137 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004138#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004139 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004140 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004141 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4142 operation->ctx.tls12_prf.key_len );
4143 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004144 }
Hanno Becker580fba12018-11-13 20:50:45 +00004145
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004146 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004147 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004148 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4149 operation->ctx.tls12_prf.Ai_with_seed_len );
4150 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004151 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004152#else
4153 if( operation->ctx.tls12_prf.seed != NULL )
4154 {
4155 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4156 operation->ctx.tls12_prf.seed_length );
4157 mbedtls_free( operation->ctx.tls12_prf.seed );
4158 }
4159
4160 if( operation->ctx.tls12_prf.label != NULL )
4161 {
4162 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4163 operation->ctx.tls12_prf.label_length );
4164 mbedtls_free( operation->ctx.tls12_prf.label );
4165 }
4166
4167 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4168
4169 /* We leave the fields Ai and output_block to be erased safely by the
4170 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004171#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004172 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004173 else
4174#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004175 {
4176 status = PSA_ERROR_BAD_STATE;
4177 }
Janos Follath083036a2019-06-11 10:22:26 +01004178 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004179 return( status );
4180}
4181
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004182psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004183 size_t *capacity)
4184{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004185 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004186 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004187 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004188 return PSA_ERROR_BAD_STATE;
4189 }
4190
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004191 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004192 return( PSA_SUCCESS );
4193}
4194
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004195psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004196 size_t capacity )
4197{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004198 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004199 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004200 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004201 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004202 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004203 return( PSA_SUCCESS );
4204}
4205
Gilles Peskinebef7f142018-07-12 17:22:21 +02004206#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004207/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004208 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004209static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004210 psa_algorithm_t hash_alg,
4211 uint8_t *output,
4212 size_t output_length )
4213{
4214 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4215 psa_status_t status;
4216
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004217 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4218 return( PSA_ERROR_BAD_STATE );
4219 hkdf->state = HKDF_STATE_OUTPUT;
4220
Gilles Peskinebef7f142018-07-12 17:22:21 +02004221 while( output_length != 0 )
4222 {
4223 /* Copy what remains of the current block */
4224 uint8_t n = hash_length - hkdf->offset_in_block;
4225 if( n > output_length )
4226 n = (uint8_t) output_length;
4227 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4228 output += n;
4229 output_length -= n;
4230 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004231 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004232 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004233 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004234 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004235 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004236 * object was corrupted or if this function is called directly
4237 * inside the library. */
4238 if( hkdf->block_number == 0xff )
4239 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004240
4241 /* We need a new block */
4242 ++hkdf->block_number;
4243 hkdf->offset_in_block = 0;
4244 status = psa_hmac_setup_internal( &hkdf->hmac,
4245 hkdf->prk, hash_length,
4246 hash_alg );
4247 if( status != PSA_SUCCESS )
4248 return( status );
4249 if( hkdf->block_number != 1 )
4250 {
4251 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4252 hkdf->output_block,
4253 hash_length );
4254 if( status != PSA_SUCCESS )
4255 return( status );
4256 }
4257 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4258 hkdf->info,
4259 hkdf->info_length );
4260 if( status != PSA_SUCCESS )
4261 return( status );
4262 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4263 &hkdf->block_number, 1 );
4264 if( status != PSA_SUCCESS )
4265 return( status );
4266 status = psa_hmac_finish_internal( &hkdf->hmac,
4267 hkdf->output_block,
4268 sizeof( hkdf->output_block ) );
4269 if( status != PSA_SUCCESS )
4270 return( status );
4271 }
4272
4273 return( PSA_SUCCESS );
4274}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004275
Janos Follath999f6482019-06-11 12:04:10 +01004276#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004277static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4278 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004279 psa_algorithm_t alg )
4280{
4281 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4282 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4283 psa_hmac_internal_data hmac;
4284 psa_status_t status, cleanup_status;
4285
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004286 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004287 size_t Ai_len;
4288
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004289 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004290 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004291 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004292 * object was corrupted or if this function is called directly
4293 * inside the library. */
4294 if( tls12_prf->block_number == 0xff )
4295 return( PSA_ERROR_BAD_STATE );
4296
4297 /* We need a new block */
4298 ++tls12_prf->block_number;
4299 tls12_prf->offset_in_block = 0;
4300
4301 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4302 *
4303 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4304 *
4305 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4306 * HMAC_hash(secret, A(2) + seed) +
4307 * HMAC_hash(secret, A(3) + seed) + ...
4308 *
4309 * A(0) = seed
4310 * A(i) = HMAC_hash( secret, A(i-1) )
4311 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004312 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004313 * `HMAC_hash(secret, A(i) + seed)` from which the output
4314 * is currently extracted as `output_block`, while
4315 * `A(i) + seed` is stored in `Ai_with_seed`.
4316 *
4317 * Generating a new block means recalculating `Ai_with_seed`
4318 * from the A(i)-part of it, and afterwards recalculating
4319 * `output_block`.
4320 *
4321 * A(0) is computed at setup time.
4322 *
4323 */
4324
4325 psa_hmac_init_internal( &hmac );
4326
4327 /* We must distinguish the calculation of A(1) from those
4328 * of A(2) and higher, because A(0)=seed has a different
4329 * length than the other A(i). */
4330 if( tls12_prf->block_number == 1 )
4331 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004332 Ai = tls12_prf->Ai_with_seed + hash_length;
4333 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004334 }
4335 else
4336 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004337 Ai = tls12_prf->Ai_with_seed;
4338 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004339 }
4340
Hanno Becker3b339e22018-11-13 20:56:14 +00004341 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4342 status = psa_hmac_setup_internal( &hmac,
4343 tls12_prf->key,
4344 tls12_prf->key_len,
4345 hash_alg );
4346 if( status != PSA_SUCCESS )
4347 goto cleanup;
4348
4349 status = psa_hash_update( &hmac.hash_ctx,
4350 Ai, Ai_len );
4351 if( status != PSA_SUCCESS )
4352 goto cleanup;
4353
4354 status = psa_hmac_finish_internal( &hmac,
4355 tls12_prf->Ai_with_seed,
4356 hash_length );
4357 if( status != PSA_SUCCESS )
4358 goto cleanup;
4359
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004360 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4361 status = psa_hmac_setup_internal( &hmac,
4362 tls12_prf->key,
4363 tls12_prf->key_len,
4364 hash_alg );
4365 if( status != PSA_SUCCESS )
4366 goto cleanup;
4367
4368 status = psa_hash_update( &hmac.hash_ctx,
4369 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004370 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004371 if( status != PSA_SUCCESS )
4372 goto cleanup;
4373
4374 status = psa_hmac_finish_internal( &hmac,
4375 tls12_prf->output_block,
4376 hash_length );
4377 if( status != PSA_SUCCESS )
4378 goto cleanup;
4379
4380cleanup:
4381
4382 cleanup_status = psa_hmac_abort_internal( &hmac );
4383 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4384 status = cleanup_status;
4385
4386 return( status );
4387}
Janos Follath7742fee2019-06-17 12:58:10 +01004388#else
4389static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4390 psa_tls12_prf_key_derivation_t *tls12_prf,
4391 psa_algorithm_t alg )
4392{
4393 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4394 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004395 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4396 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004397
Janos Follath7742fee2019-06-17 12:58:10 +01004398 /* We can't be wanting more output after block 0xff, otherwise
4399 * the capacity check in psa_key_derivation_output_bytes() would have
4400 * prevented this call. It could happen only if the operation
4401 * object was corrupted or if this function is called directly
4402 * inside the library. */
4403 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004404 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004405
4406 /* We need a new block */
4407 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004408 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004409
4410 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4411 *
4412 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4413 *
4414 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4415 * HMAC_hash(secret, A(2) + seed) +
4416 * HMAC_hash(secret, A(3) + seed) + ...
4417 *
4418 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004419 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004420 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004421 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004422 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004423 * is currently extracted as `output_block` and where i is
4424 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004425 */
4426
Janos Follathea29bfb2019-06-19 12:21:20 +01004427 /* Save the hash context before using it, to preserve the hash state with
4428 * only the inner padding in it. We need this, because inner padding depends
4429 * on the key (secret in the RFC's terminology). */
4430 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4431 if( status != PSA_SUCCESS )
4432 goto cleanup;
4433
4434 /* Calculate A(i) where i = tls12_prf->block_number. */
4435 if( tls12_prf->block_number == 1 )
4436 {
4437 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4438 * the variable seed and in this instance means it in the context of the
4439 * P_hash function, where seed = label + seed.) */
4440 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4441 tls12_prf->label, tls12_prf->label_length );
4442 if( status != PSA_SUCCESS )
4443 goto cleanup;
4444 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4445 tls12_prf->seed, tls12_prf->seed_length );
4446 if( status != PSA_SUCCESS )
4447 goto cleanup;
4448 }
4449 else
4450 {
4451 /* A(i) = HMAC_hash(secret, A(i-1)) */
4452 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4453 tls12_prf->Ai, hash_length );
4454 if( status != PSA_SUCCESS )
4455 goto cleanup;
4456 }
4457
4458 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4459 tls12_prf->Ai, hash_length );
4460 if( status != PSA_SUCCESS )
4461 goto cleanup;
4462 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4463 if( status != PSA_SUCCESS )
4464 goto cleanup;
4465
4466 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4467 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4468 tls12_prf->Ai, hash_length );
4469 if( status != PSA_SUCCESS )
4470 goto cleanup;
4471 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4472 tls12_prf->label, tls12_prf->label_length );
4473 if( status != PSA_SUCCESS )
4474 goto cleanup;
4475 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4476 tls12_prf->seed, tls12_prf->seed_length );
4477 if( status != PSA_SUCCESS )
4478 goto cleanup;
4479 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4480 tls12_prf->output_block, hash_length );
4481 if( status != PSA_SUCCESS )
4482 goto cleanup;
4483 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4484 if( status != PSA_SUCCESS )
4485 goto cleanup;
4486
Janos Follath7742fee2019-06-17 12:58:10 +01004487
4488cleanup:
4489
Janos Follathea29bfb2019-06-19 12:21:20 +01004490 cleanup_status = psa_hash_abort( &backup );
4491 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4492 status = cleanup_status;
4493
4494 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004495}
Janos Follath999f6482019-06-11 12:04:10 +01004496#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004497
Janos Follath999f6482019-06-11 12:04:10 +01004498#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004499/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004500 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004501static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004502 psa_tls12_prf_key_derivation_t *tls12_prf,
4503 psa_algorithm_t alg,
4504 uint8_t *output,
4505 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004506{
4507 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4508 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4509 psa_status_t status;
4510
4511 while( output_length != 0 )
4512 {
4513 /* Copy what remains of the current block */
4514 uint8_t n = hash_length - tls12_prf->offset_in_block;
4515
4516 /* Check if we have fully processed the current block. */
4517 if( n == 0 )
4518 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004519 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004520 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004521 if( status != PSA_SUCCESS )
4522 return( status );
4523
4524 continue;
4525 }
4526
4527 if( n > output_length )
4528 n = (uint8_t) output_length;
4529 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4530 n );
4531 output += n;
4532 output_length -= n;
4533 tls12_prf->offset_in_block += n;
4534 }
4535
4536 return( PSA_SUCCESS );
4537}
Janos Follath844eb0e2019-06-19 12:10:49 +01004538#else
4539static psa_status_t psa_key_derivation_tls12_prf_read(
4540 psa_tls12_prf_key_derivation_t *tls12_prf,
4541 psa_algorithm_t alg,
4542 uint8_t *output,
4543 size_t output_length )
4544{
4545 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4546 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4547 psa_status_t status;
4548 uint8_t offset, length;
4549
4550 while( output_length != 0 )
4551 {
4552 /* Check if we have fully processed the current block. */
4553 if( tls12_prf->left_in_block == 0 )
4554 {
4555 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4556 alg );
4557 if( status != PSA_SUCCESS )
4558 return( status );
4559
4560 continue;
4561 }
4562
4563 if( tls12_prf->left_in_block > output_length )
4564 length = (uint8_t) output_length;
4565 else
4566 length = tls12_prf->left_in_block;
4567
4568 offset = hash_length - tls12_prf->left_in_block;
4569 memcpy( output, tls12_prf->output_block + offset, length );
4570 output += length;
4571 output_length -= length;
4572 tls12_prf->left_in_block -= length;
4573 }
4574
4575 return( PSA_SUCCESS );
4576}
Janos Follath999f6482019-06-11 12:04:10 +01004577#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004578#endif /* MBEDTLS_MD_C */
4579
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004580psa_status_t psa_key_derivation_output_bytes(
4581 psa_key_derivation_operation_t *operation,
4582 uint8_t *output,
4583 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004584{
4585 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004586 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004587
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004588 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004589 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004590 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004591 return PSA_ERROR_BAD_STATE;
4592 }
4593
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004594 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004595 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004596 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004597 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004598 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004599 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004600 goto exit;
4601 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004602 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004603 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004604 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004605 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004606 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4607 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004608 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004609 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004610 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004611 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004612 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004613
Gilles Peskinebef7f142018-07-12 17:22:21 +02004614#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004615 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004616 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004617 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004618 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004619 output, output_length );
4620 }
Janos Follathadbec812019-06-14 11:05:39 +01004621 else
Janos Follathadbec812019-06-14 11:05:39 +01004622 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004623 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004624 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004625 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004626 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004627 output_length );
4628 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004629 else
4630#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004631 {
4632 return( PSA_ERROR_BAD_STATE );
4633 }
4634
4635exit:
4636 if( status != PSA_SUCCESS )
4637 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004638 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004639 * This allows us to differentiate between exhausted operations and
4640 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4641 * operations. */
4642 psa_algorithm_t alg = operation->alg;
4643 psa_key_derivation_abort( operation );
4644 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004645 memset( output, '!', output_length );
4646 }
4647 return( status );
4648}
4649
Gilles Peskine08542d82018-07-19 17:05:42 +02004650#if defined(MBEDTLS_DES_C)
4651static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4652{
4653 if( data_size >= 8 )
4654 mbedtls_des_key_set_parity( data );
4655 if( data_size >= 16 )
4656 mbedtls_des_key_set_parity( data + 8 );
4657 if( data_size >= 24 )
4658 mbedtls_des_key_set_parity( data + 16 );
4659}
4660#endif /* MBEDTLS_DES_C */
4661
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004662static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004663 psa_key_slot_t *slot,
4664 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004665 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004666{
4667 uint8_t *data = NULL;
4668 size_t bytes = PSA_BITS_TO_BYTES( bits );
4669 psa_status_t status;
4670
4671 if( ! key_type_is_raw_bytes( slot->type ) )
4672 return( PSA_ERROR_INVALID_ARGUMENT );
4673 if( bits % 8 != 0 )
4674 return( PSA_ERROR_INVALID_ARGUMENT );
4675 data = mbedtls_calloc( 1, bytes );
4676 if( data == NULL )
4677 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4678
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004679 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004680 if( status != PSA_SUCCESS )
4681 goto exit;
4682#if defined(MBEDTLS_DES_C)
4683 if( slot->type == PSA_KEY_TYPE_DES )
4684 psa_des_set_key_parity( data, bytes );
4685#endif /* MBEDTLS_DES_C */
4686 status = psa_import_key_into_slot( slot, data, bytes );
4687
4688exit:
4689 mbedtls_free( data );
4690 return( status );
4691}
4692
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004693psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004694 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004695 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004696{
4697 psa_status_t status;
4698 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004699 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004700 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004701#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4702 if( driver != NULL )
4703 {
4704 /* Deriving a key in a secure element is not implemented yet. */
4705 status = PSA_ERROR_NOT_SUPPORTED;
4706 }
4707#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004708 if( status == PSA_SUCCESS )
4709 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004710 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004711 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004712 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004713 }
4714 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004715 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004716 if( status != PSA_SUCCESS )
4717 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004718 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004719 *handle = 0;
4720 }
4721 return( status );
4722}
4723
Gilles Peskineeab56e42018-07-12 17:12:33 +02004724
4725
4726/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004727/* Key derivation */
4728/****************************************************************/
4729
Gilles Peskinea05219c2018-11-16 16:02:56 +01004730#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004731#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004732/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004733 * of the HKDF algorithm.
4734 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004735 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004736 * to potentially free embedded data structures and wipe confidential data.
4737 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004738static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004739 const uint8_t *secret,
4740 size_t secret_length,
4741 psa_algorithm_t hash_alg,
4742 const uint8_t *salt,
4743 size_t salt_length,
4744 const uint8_t *label,
4745 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004746{
4747 psa_status_t status;
4748 status = psa_hmac_setup_internal( &hkdf->hmac,
4749 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004750 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004751 if( status != PSA_SUCCESS )
4752 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004753 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004754 if( status != PSA_SUCCESS )
4755 return( status );
4756 status = psa_hmac_finish_internal( &hkdf->hmac,
4757 hkdf->prk,
4758 sizeof( hkdf->prk ) );
4759 if( status != PSA_SUCCESS )
4760 return( status );
4761 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4762 hkdf->block_number = 0;
4763 hkdf->info_length = label_length;
4764 if( label_length != 0 )
4765 {
4766 hkdf->info = mbedtls_calloc( 1, label_length );
4767 if( hkdf->info == NULL )
4768 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4769 memcpy( hkdf->info, label, label_length );
4770 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004771 hkdf->state = HKDF_STATE_KEYED;
4772 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004773 return( PSA_SUCCESS );
4774}
Janos Follath71a4c912019-06-11 09:14:47 +01004775#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004776#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004777
Gilles Peskinea05219c2018-11-16 16:02:56 +01004778#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004779#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004780/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004781 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004782 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004783 * to potentially free embedded data structures and wipe confidential data.
4784 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004785static psa_status_t psa_key_derivation_tls12_prf_setup(
4786 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004787 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004788 size_t key_len,
4789 psa_algorithm_t hash_alg,
4790 const uint8_t *salt,
4791 size_t salt_length,
4792 const uint8_t *label,
4793 size_t label_length )
4794{
4795 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004796 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4797 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004798
4799 tls12_prf->key = mbedtls_calloc( 1, key_len );
4800 if( tls12_prf->key == NULL )
4801 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4802 tls12_prf->key_len = key_len;
4803 memcpy( tls12_prf->key, key, key_len );
4804
Hanno Becker580fba12018-11-13 20:50:45 +00004805 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004806 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004807 if( overflow )
4808 return( PSA_ERROR_INVALID_ARGUMENT );
4809
4810 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4811 if( tls12_prf->Ai_with_seed == NULL )
4812 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4813 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4814
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004815 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4816 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004817 if( label_length != 0 )
4818 {
4819 memcpy( tls12_prf->Ai_with_seed + hash_length,
4820 label, label_length );
4821 }
4822
4823 if( salt_length != 0 )
4824 {
4825 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4826 salt, salt_length );
4827 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004828
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004829 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004830 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004831 tls12_prf->block_number = 0;
4832 tls12_prf->offset_in_block = hash_length;
4833
4834 return( PSA_SUCCESS );
4835}
Janos Follath71a4c912019-06-11 09:14:47 +01004836#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004837
Janos Follath71a4c912019-06-11 09:14:47 +01004838#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004839/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004840static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4841 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004842 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004843 size_t psk_len,
4844 psa_algorithm_t hash_alg,
4845 const uint8_t *salt,
4846 size_t salt_length,
4847 const uint8_t *label,
4848 size_t label_length )
4849{
4850 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004851 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00004852
4853 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4854 return( PSA_ERROR_INVALID_ARGUMENT );
4855
4856 /* Quoting RFC 4279, Section 2:
4857 *
4858 * The premaster secret is formed as follows: if the PSK is N octets
4859 * long, concatenate a uint16 with the value N, N zero octets, a second
4860 * uint16 with the value N, and the PSK itself.
4861 */
4862
4863 pms[0] = ( psk_len >> 8 ) & 0xff;
4864 pms[1] = ( psk_len >> 0 ) & 0xff;
4865 memset( pms + 2, 0, psk_len );
4866 pms[2 + psk_len + 0] = pms[0];
4867 pms[2 + psk_len + 1] = pms[1];
4868 memcpy( pms + 4 + psk_len, psk, psk_len );
4869
Gilles Peskinecbe66502019-05-16 16:59:18 +02004870 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004871 pms, 4 + 2 * psk_len,
4872 hash_alg,
4873 salt, salt_length,
4874 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004875
Gilles Peskine3f108122018-12-07 18:14:53 +01004876 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004877 return( status );
4878}
Janos Follath71a4c912019-06-11 09:14:47 +01004879#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004880#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004881
Janos Follath71a4c912019-06-11 09:14:47 +01004882#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004883/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004884 * to potentially free embedded data structures and wipe confidential data.
4885 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004886static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004887 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004888 const uint8_t *secret, size_t secret_length,
4889 psa_algorithm_t alg,
4890 const uint8_t *salt, size_t salt_length,
4891 const uint8_t *label, size_t label_length,
4892 size_t capacity )
4893{
4894 psa_status_t status;
4895 size_t max_capacity;
4896
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004897 /* Set operation->alg even on failure so that abort knows what to do. */
4898 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004899
4900#if defined(MBEDTLS_MD_C)
4901 if( PSA_ALG_IS_HKDF( alg ) )
4902 {
4903 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4904 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4905 if( hash_size == 0 )
4906 return( PSA_ERROR_NOT_SUPPORTED );
4907 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004908 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004909 secret, secret_length,
4910 hash_alg,
4911 salt, salt_length,
4912 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004913 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004914 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4915 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4916 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004917 {
4918 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4919 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4920
4921 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4922 if( hash_alg != PSA_ALG_SHA_256 &&
4923 hash_alg != PSA_ALG_SHA_384 )
4924 {
4925 return( PSA_ERROR_NOT_SUPPORTED );
4926 }
4927
4928 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004929
4930 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4931 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004932 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004933 secret, secret_length,
4934 hash_alg, salt, salt_length,
4935 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004936 }
4937 else
4938 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004939 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004940 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004941 secret, secret_length,
4942 hash_alg, salt, salt_length,
4943 label, label_length );
4944 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004945 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004946 else
4947#endif
4948 {
4949 return( PSA_ERROR_NOT_SUPPORTED );
4950 }
4951
4952 if( status != PSA_SUCCESS )
4953 return( status );
4954
4955 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004956 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004957 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004958 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004959 else
4960 return( PSA_ERROR_INVALID_ARGUMENT );
4961
4962 return( PSA_SUCCESS );
4963}
Janos Follath71a4c912019-06-11 09:14:47 +01004964#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004965
Janos Follath71a4c912019-06-11 09:14:47 +01004966#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004967psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004968 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004969 psa_algorithm_t alg,
4970 const uint8_t *salt,
4971 size_t salt_length,
4972 const uint8_t *label,
4973 size_t label_length,
4974 size_t capacity )
4975{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004976 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004977 psa_status_t status;
4978
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004979 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004980 return( PSA_ERROR_BAD_STATE );
4981
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004982 /* Make sure that alg is a key derivation algorithm. This prevents
4983 * key selection algorithms, which psa_key_derivation_internal
4984 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004985 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4986 return( PSA_ERROR_INVALID_ARGUMENT );
4987
Gilles Peskine28f8f302019-07-24 13:30:31 +02004988 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004989 if( status != PSA_SUCCESS )
4990 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004991
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004992 if( slot->type != PSA_KEY_TYPE_DERIVE )
4993 return( PSA_ERROR_INVALID_ARGUMENT );
4994
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004995 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004996 slot->data.raw.data,
4997 slot->data.raw.bytes,
4998 alg,
4999 salt, salt_length,
5000 label, label_length,
5001 capacity );
5002 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005003 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005004 return( status );
5005}
Janos Follath71a4c912019-06-11 09:14:47 +01005006#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005007
Gilles Peskine969c5d62019-01-16 15:53:06 +01005008static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005009 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005010 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005011{
Janos Follath5fe19732019-06-20 15:09:30 +01005012 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5013 * initialisers for this union leave some bytes unspecified.) */
5014 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5015
Gilles Peskine969c5d62019-01-16 15:53:06 +01005016 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005017#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005018 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5019 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5020 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005021 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005022 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005023 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5024 if( hash_size == 0 )
5025 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005026 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5027 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005028 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005029 {
5030 return( PSA_ERROR_NOT_SUPPORTED );
5031 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005033 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005034 }
5035#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005036 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005037 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005038}
5039
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005040psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005041 psa_algorithm_t alg )
5042{
5043 psa_status_t status;
5044
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005045 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005046 return( PSA_ERROR_BAD_STATE );
5047
Gilles Peskine6843c292019-01-18 16:44:49 +01005048 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5049 return( PSA_ERROR_INVALID_ARGUMENT );
5050 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005051 {
5052 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005053 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005054 }
5055 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5056 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005057 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005058 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005059 else
5060 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005061
5062 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005064 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005065}
5066
5067#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005068static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005069 psa_algorithm_t hash_alg,
5070 psa_key_derivation_step_t step,
5071 const uint8_t *data,
5072 size_t data_length )
5073{
5074 psa_status_t status;
5075 switch( step )
5076 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005077 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005078 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005079 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005080 status = psa_hmac_setup_internal( &hkdf->hmac,
5081 data, data_length,
5082 hash_alg );
5083 if( status != PSA_SUCCESS )
5084 return( status );
5085 hkdf->state = HKDF_STATE_STARTED;
5086 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005087 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005088 /* If no salt was provided, use an empty salt. */
5089 if( hkdf->state == HKDF_STATE_INIT )
5090 {
5091 status = psa_hmac_setup_internal( &hkdf->hmac,
5092 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005093 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005094 if( status != PSA_SUCCESS )
5095 return( status );
5096 hkdf->state = HKDF_STATE_STARTED;
5097 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005098 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005099 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005100 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5101 data, data_length );
5102 if( status != PSA_SUCCESS )
5103 return( status );
5104 status = psa_hmac_finish_internal( &hkdf->hmac,
5105 hkdf->prk,
5106 sizeof( hkdf->prk ) );
5107 if( status != PSA_SUCCESS )
5108 return( status );
5109 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5110 hkdf->block_number = 0;
5111 hkdf->state = HKDF_STATE_KEYED;
5112 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005113 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005114 if( hkdf->state == HKDF_STATE_OUTPUT )
5115 return( PSA_ERROR_BAD_STATE );
5116 if( hkdf->info_set )
5117 return( PSA_ERROR_BAD_STATE );
5118 hkdf->info_length = data_length;
5119 if( data_length != 0 )
5120 {
5121 hkdf->info = mbedtls_calloc( 1, data_length );
5122 if( hkdf->info == NULL )
5123 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5124 memcpy( hkdf->info, data, data_length );
5125 }
5126 hkdf->info_set = 1;
5127 return( PSA_SUCCESS );
5128 default:
5129 return( PSA_ERROR_INVALID_ARGUMENT );
5130 }
5131}
Janos Follathb03233e2019-06-11 15:30:30 +01005132
5133#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5134static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5135 psa_algorithm_t hash_alg,
5136 psa_key_derivation_step_t step,
5137 const uint8_t *data,
5138 size_t data_length )
5139{
5140 (void) prf;
5141 (void) hash_alg;
5142 (void) step;
5143 (void) data;
5144 (void) data_length;
5145
5146 return( PSA_ERROR_INVALID_ARGUMENT );
5147}
Janos Follath6660f0e2019-06-17 08:44:03 +01005148
5149static psa_status_t psa_tls12_prf_psk_to_ms_input(
5150 psa_tls12_prf_key_derivation_t *prf,
5151 psa_algorithm_t hash_alg,
5152 psa_key_derivation_step_t step,
5153 const uint8_t *data,
5154 size_t data_length )
5155{
5156 (void) prf;
5157 (void) hash_alg;
5158 (void) step;
5159 (void) data;
5160 (void) data_length;
5161
5162 return( PSA_ERROR_INVALID_ARGUMENT );
5163}
Janos Follathb03233e2019-06-11 15:30:30 +01005164#else
Janos Follathf08e2652019-06-13 09:05:41 +01005165static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5166 const uint8_t *data,
5167 size_t data_length )
5168{
5169 if( prf->state != TLS12_PRF_STATE_INIT )
5170 return( PSA_ERROR_BAD_STATE );
5171
Janos Follathd6dce9f2019-07-04 09:11:38 +01005172 if( data_length != 0 )
5173 {
5174 prf->seed = mbedtls_calloc( 1, data_length );
5175 if( prf->seed == NULL )
5176 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005177
Janos Follathd6dce9f2019-07-04 09:11:38 +01005178 memcpy( prf->seed, data, data_length );
5179 prf->seed_length = data_length;
5180 }
Janos Follathf08e2652019-06-13 09:05:41 +01005181
5182 prf->state = TLS12_PRF_STATE_SEED_SET;
5183
5184 return( PSA_SUCCESS );
5185}
5186
Janos Follath81550542019-06-13 14:26:34 +01005187static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5188 psa_algorithm_t hash_alg,
5189 const uint8_t *data,
5190 size_t data_length )
5191{
5192 psa_status_t status;
5193 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5194 return( PSA_ERROR_BAD_STATE );
5195
5196 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5197 if( status != PSA_SUCCESS )
5198 return( status );
5199
5200 prf->state = TLS12_PRF_STATE_KEY_SET;
5201
5202 return( PSA_SUCCESS );
5203}
5204
Janos Follath6660f0e2019-06-17 08:44:03 +01005205static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5206 psa_tls12_prf_key_derivation_t *prf,
5207 psa_algorithm_t hash_alg,
5208 const uint8_t *data,
5209 size_t data_length )
5210{
5211 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005212 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5213 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005214
5215 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5216 return( PSA_ERROR_INVALID_ARGUMENT );
5217
5218 /* Quoting RFC 4279, Section 2:
5219 *
5220 * The premaster secret is formed as follows: if the PSK is N octets
5221 * long, concatenate a uint16 with the value N, N zero octets, a second
5222 * uint16 with the value N, and the PSK itself.
5223 */
5224
Janos Follath40e13932019-06-26 13:22:29 +01005225 *cur++ = ( data_length >> 8 ) & 0xff;
5226 *cur++ = ( data_length >> 0 ) & 0xff;
5227 memset( cur, 0, data_length );
5228 cur += data_length;
5229 *cur++ = pms[0];
5230 *cur++ = pms[1];
5231 memcpy( cur, data, data_length );
5232 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005233
Janos Follath40e13932019-06-26 13:22:29 +01005234 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005235
5236 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5237 return( status );
5238}
5239
Janos Follath63028dd2019-06-13 09:15:47 +01005240static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5241 const uint8_t *data,
5242 size_t data_length )
5243{
5244 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5245 return( PSA_ERROR_BAD_STATE );
5246
Janos Follathd6dce9f2019-07-04 09:11:38 +01005247 if( data_length != 0 )
5248 {
5249 prf->label = mbedtls_calloc( 1, data_length );
5250 if( prf->label == NULL )
5251 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005252
Janos Follathd6dce9f2019-07-04 09:11:38 +01005253 memcpy( prf->label, data, data_length );
5254 prf->label_length = data_length;
5255 }
Janos Follath63028dd2019-06-13 09:15:47 +01005256
5257 prf->state = TLS12_PRF_STATE_LABEL_SET;
5258
5259 return( PSA_SUCCESS );
5260}
5261
Janos Follathb03233e2019-06-11 15:30:30 +01005262static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5263 psa_algorithm_t hash_alg,
5264 psa_key_derivation_step_t step,
5265 const uint8_t *data,
5266 size_t data_length )
5267{
Janos Follathb03233e2019-06-11 15:30:30 +01005268 switch( step )
5269 {
Janos Follathf08e2652019-06-13 09:05:41 +01005270 case PSA_KEY_DERIVATION_INPUT_SEED:
5271 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005272 case PSA_KEY_DERIVATION_INPUT_SECRET:
5273 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005274 case PSA_KEY_DERIVATION_INPUT_LABEL:
5275 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005276 default:
5277 return( PSA_ERROR_INVALID_ARGUMENT );
5278 }
5279}
Janos Follath6660f0e2019-06-17 08:44:03 +01005280
5281static psa_status_t psa_tls12_prf_psk_to_ms_input(
5282 psa_tls12_prf_key_derivation_t *prf,
5283 psa_algorithm_t hash_alg,
5284 psa_key_derivation_step_t step,
5285 const uint8_t *data,
5286 size_t data_length )
5287{
5288 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005289 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005290 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5291 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005292 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005293
5294 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5295}
Janos Follathb03233e2019-06-11 15:30:30 +01005296#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005297#endif /* MBEDTLS_MD_C */
5298
Janos Follathb80a94e2019-06-12 15:54:46 +01005299static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005300 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005301 psa_key_derivation_step_t step,
5302 const uint8_t *data,
5303 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005304{
5305 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005306 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005307
5308#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005309 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005310 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005311 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005312 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005313 step, data, data_length );
5314 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005315 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005316#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005317#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005318 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005319 {
Janos Follathb03233e2019-06-11 15:30:30 +01005320 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5321 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5322 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005323 }
5324 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5325 {
5326 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5327 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5328 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005329 }
5330 else
5331#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005332 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005333 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005334 return( PSA_ERROR_BAD_STATE );
5335 }
5336
5337 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005338 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005339 return( status );
5340}
5341
Janos Follath51f4a0f2019-06-14 11:35:55 +01005342psa_status_t psa_key_derivation_input_bytes(
5343 psa_key_derivation_operation_t *operation,
5344 psa_key_derivation_step_t step,
5345 const uint8_t *data,
5346 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005347{
Janos Follathc5621512019-06-14 11:27:57 +01005348 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5349 return( PSA_ERROR_INVALID_ARGUMENT );
5350
5351 return( psa_key_derivation_input_internal( operation, step,
5352 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005353}
5354
Janos Follath51f4a0f2019-06-14 11:35:55 +01005355psa_status_t psa_key_derivation_input_key(
5356 psa_key_derivation_operation_t *operation,
5357 psa_key_derivation_step_t step,
5358 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005359{
5360 psa_key_slot_t *slot;
5361 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005362 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005363 PSA_KEY_USAGE_DERIVE,
5364 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005365 if( status != PSA_SUCCESS )
5366 return( status );
5367 if( slot->type != PSA_KEY_TYPE_DERIVE )
5368 return( PSA_ERROR_INVALID_ARGUMENT );
5369 /* Don't allow a key to be used as an input that is usually public.
5370 * This is debatable. It's ok from a cryptographic perspective to
5371 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005372 * the material should be dedicated to a particular input step,
5373 * otherwise this may allow the key to be used in an unintended way
5374 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005375 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005376 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005377 return( psa_key_derivation_input_internal( operation,
5378 step,
5379 slot->data.raw.data,
5380 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005381}
5382
Gilles Peskineea0fb492018-07-12 17:17:20 +02005383
5384
5385/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005386/* Key agreement */
5387/****************************************************************/
5388
Gilles Peskinea05219c2018-11-16 16:02:56 +01005389#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005390static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5391 size_t peer_key_length,
5392 const mbedtls_ecp_keypair *our_key,
5393 uint8_t *shared_secret,
5394 size_t shared_secret_size,
5395 size_t *shared_secret_length )
5396{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005397 mbedtls_ecp_keypair *their_key = NULL;
5398 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005399 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005400 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005401
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005402 status = psa_import_ec_public_key(
5403 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5404 peer_key, peer_key_length,
5405 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005406 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005407 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005408
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005409 status = mbedtls_to_psa_error(
5410 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5411 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005412 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005413 status = mbedtls_to_psa_error(
5414 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5415 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005416 goto exit;
5417
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005418 status = mbedtls_to_psa_error(
5419 mbedtls_ecdh_calc_secret( &ecdh,
5420 shared_secret_length,
5421 shared_secret, shared_secret_size,
5422 mbedtls_ctr_drbg_random,
5423 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005424
5425exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005426 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005427 mbedtls_ecp_keypair_free( their_key );
5428 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005429 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005430}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005431#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005432
Gilles Peskine01d718c2018-09-18 12:01:02 +02005433#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5434
Gilles Peskine0216fe12019-04-11 21:23:21 +02005435static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5436 psa_key_slot_t *private_key,
5437 const uint8_t *peer_key,
5438 size_t peer_key_length,
5439 uint8_t *shared_secret,
5440 size_t shared_secret_size,
5441 size_t *shared_secret_length )
5442{
5443 switch( alg )
5444 {
5445#if defined(MBEDTLS_ECDH_C)
5446 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005447 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005448 return( PSA_ERROR_INVALID_ARGUMENT );
5449 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5450 private_key->data.ecp,
5451 shared_secret, shared_secret_size,
5452 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005453#endif /* MBEDTLS_ECDH_C */
5454 default:
5455 (void) private_key;
5456 (void) peer_key;
5457 (void) peer_key_length;
5458 (void) shared_secret;
5459 (void) shared_secret_size;
5460 (void) shared_secret_length;
5461 return( PSA_ERROR_NOT_SUPPORTED );
5462 }
5463}
5464
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005465/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005466 * to potentially free embedded data structures and wipe confidential data.
5467 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005468static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005469 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005470 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005471 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005472 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005473{
5474 psa_status_t status;
5475 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5476 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005477 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005478
5479 /* Step 1: run the secret agreement algorithm to generate the shared
5480 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005481 status = psa_key_agreement_raw_internal( ka_alg,
5482 private_key,
5483 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005484 shared_secret,
5485 sizeof( shared_secret ),
5486 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005487 if( status != PSA_SUCCESS )
5488 goto exit;
5489
5490 /* Step 2: set up the key derivation to generate key material from
5491 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005492 status = psa_key_derivation_input_internal( operation, step,
5493 shared_secret,
5494 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005495
Gilles Peskine01d718c2018-09-18 12:01:02 +02005496exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005497 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005498 return( status );
5499}
5500
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005501psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005502 psa_key_derivation_step_t step,
5503 psa_key_handle_t private_key,
5504 const uint8_t *peer_key,
5505 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005506{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005507 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005508 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005509 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005510 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005511 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005512 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005513 if( status != PSA_SUCCESS )
5514 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005515 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005516 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005517 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005518 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005519 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005520 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005521}
5522
Gilles Peskinebe697d82019-05-16 18:00:41 +02005523psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5524 psa_key_handle_t private_key,
5525 const uint8_t *peer_key,
5526 size_t peer_key_length,
5527 uint8_t *output,
5528 size_t output_size,
5529 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005530{
5531 psa_key_slot_t *slot;
5532 psa_status_t status;
5533
5534 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5535 {
5536 status = PSA_ERROR_INVALID_ARGUMENT;
5537 goto exit;
5538 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005539 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005540 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005541 if( status != PSA_SUCCESS )
5542 goto exit;
5543
5544 status = psa_key_agreement_raw_internal( alg, slot,
5545 peer_key, peer_key_length,
5546 output, output_size,
5547 output_length );
5548
5549exit:
5550 if( status != PSA_SUCCESS )
5551 {
5552 /* If an error happens and is not handled properly, the output
5553 * may be used as a key to protect sensitive data. Arrange for such
5554 * a key to be random, which is likely to result in decryption or
5555 * verification errors. This is better than filling the buffer with
5556 * some constant data such as zeros, which would result in the data
5557 * being protected with a reproducible, easily knowable key.
5558 */
5559 psa_generate_random( output, output_size );
5560 *output_length = output_size;
5561 }
5562 return( status );
5563}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005564
5565
5566/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005567/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005568/****************************************************************/
5569
5570psa_status_t psa_generate_random( uint8_t *output,
5571 size_t output_size )
5572{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005573 int ret;
5574 GUARD_MODULE_INITIALIZED;
5575
5576 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005577 return( mbedtls_to_psa_error( ret ) );
5578}
5579
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005580#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5581#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005582
Gilles Peskine7228da22019-07-15 11:06:15 +02005583psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005584 size_t seed_size )
5585{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005586 if( global_data.initialized )
5587 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005588
5589 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5590 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5591 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5592 return( PSA_ERROR_INVALID_ARGUMENT );
5593
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005594 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005595}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005596#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005597
Gilles Peskinee56e8782019-04-26 17:34:02 +02005598#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5599static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5600 size_t domain_parameters_size,
5601 int *exponent )
5602{
5603 size_t i;
5604 uint32_t acc = 0;
5605
5606 if( domain_parameters_size == 0 )
5607 {
5608 *exponent = 65537;
5609 return( PSA_SUCCESS );
5610 }
5611
5612 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5613 * support values that fit in a 32-bit integer, which is larger than
5614 * int on just about every platform anyway. */
5615 if( domain_parameters_size > sizeof( acc ) )
5616 return( PSA_ERROR_NOT_SUPPORTED );
5617 for( i = 0; i < domain_parameters_size; i++ )
5618 acc = ( acc << 8 ) | domain_parameters[i];
5619 if( acc > INT_MAX )
5620 return( PSA_ERROR_NOT_SUPPORTED );
5621 *exponent = acc;
5622 return( PSA_SUCCESS );
5623}
5624#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5625
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005626static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005627 psa_key_slot_t *slot, size_t bits,
5628 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005629{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005630 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005631
Gilles Peskinee56e8782019-04-26 17:34:02 +02005632 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005633 return( PSA_ERROR_INVALID_ARGUMENT );
5634
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005635 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005636 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005637 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005638 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005639 if( status != PSA_SUCCESS )
5640 return( status );
5641 status = psa_generate_random( slot->data.raw.data,
5642 slot->data.raw.bytes );
5643 if( status != PSA_SUCCESS )
5644 {
5645 mbedtls_free( slot->data.raw.data );
5646 return( status );
5647 }
5648#if defined(MBEDTLS_DES_C)
5649 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005650 psa_des_set_key_parity( slot->data.raw.data,
5651 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005652#endif /* MBEDTLS_DES_C */
5653 }
5654 else
5655
5656#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005657 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005658 {
5659 mbedtls_rsa_context *rsa;
5660 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005661 int exponent;
5662 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005663 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5664 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005665 /* Accept only byte-aligned keys, for the same reasons as
5666 * in psa_import_rsa_key(). */
5667 if( bits % 8 != 0 )
5668 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005669 status = psa_read_rsa_exponent( domain_parameters,
5670 domain_parameters_size,
5671 &exponent );
5672 if( status != PSA_SUCCESS )
5673 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005674 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5675 if( rsa == NULL )
5676 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5677 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5678 ret = mbedtls_rsa_gen_key( rsa,
5679 mbedtls_ctr_drbg_random,
5680 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005681 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005682 exponent );
5683 if( ret != 0 )
5684 {
5685 mbedtls_rsa_free( rsa );
5686 mbedtls_free( rsa );
5687 return( mbedtls_to_psa_error( ret ) );
5688 }
5689 slot->data.rsa = rsa;
5690 }
5691 else
5692#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5693
5694#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005695 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005696 {
5697 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5698 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5699 const mbedtls_ecp_curve_info *curve_info =
5700 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5701 mbedtls_ecp_keypair *ecp;
5702 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005703 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005704 return( PSA_ERROR_NOT_SUPPORTED );
5705 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5706 return( PSA_ERROR_NOT_SUPPORTED );
5707 if( curve_info->bit_size != bits )
5708 return( PSA_ERROR_INVALID_ARGUMENT );
5709 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5710 if( ecp == NULL )
5711 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5712 mbedtls_ecp_keypair_init( ecp );
5713 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5714 mbedtls_ctr_drbg_random,
5715 &global_data.ctr_drbg );
5716 if( ret != 0 )
5717 {
5718 mbedtls_ecp_keypair_free( ecp );
5719 mbedtls_free( ecp );
5720 return( mbedtls_to_psa_error( ret ) );
5721 }
5722 slot->data.ecp = ecp;
5723 }
5724 else
5725#endif /* MBEDTLS_ECP_C */
5726
5727 return( PSA_ERROR_NOT_SUPPORTED );
5728
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005729 return( PSA_SUCCESS );
5730}
5731
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005732psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005733 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005734{
5735 psa_status_t status;
5736 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005737 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005738 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005739#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5740 if( driver != NULL )
5741 {
5742 /* Generating a key in a secure element is not implemented yet. */
5743 status = PSA_ERROR_NOT_SUPPORTED;
5744 }
5745#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005746 if( status == PSA_SUCCESS )
5747 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005748 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005749 slot, attributes->bits,
5750 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005751 }
5752 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005753 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005754 if( status != PSA_SUCCESS )
5755 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005756 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005757 *handle = 0;
5758 }
5759 return( status );
5760}
5761
5762
Gilles Peskine05d69892018-06-19 22:00:52 +02005763
5764/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005765/* Module setup */
5766/****************************************************************/
5767
Gilles Peskine5e769522018-11-20 21:59:56 +01005768psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5769 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5770 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5771{
5772 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5773 return( PSA_ERROR_BAD_STATE );
5774 global_data.entropy_init = entropy_init;
5775 global_data.entropy_free = entropy_free;
5776 return( PSA_SUCCESS );
5777}
5778
Gilles Peskinee59236f2018-01-27 23:32:46 +01005779void mbedtls_psa_crypto_free( void )
5780{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005781 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005782 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5783 {
5784 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005785 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005786 }
5787 /* Wipe all remaining data, including configuration.
5788 * In particular, this sets all state indicator to the value
5789 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005790 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005791#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005792 /* Unregister all secure element drivers, so that we restart from
5793 * a pristine state. */
5794 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005795#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005796}
5797
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005798#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5799/** Recover a transaction that was interrupted by a power failure.
5800 *
5801 * This function is called during initialization, before psa_crypto_init()
5802 * returns. If this function returns a failure status, the initialization
5803 * fails.
5804 */
5805static psa_status_t psa_crypto_recover_transaction(
5806 const psa_crypto_transaction_t *transaction )
5807{
5808 switch( transaction->unknown.type )
5809 {
5810 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5811 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
5812 /* TOnogrepDO - fall through to the failure case until this
5813 * is implemented */
5814 default:
5815 /* We found an unsupported transaction in the storage.
5816 * We don't know what state the storage is in. Give up. */
5817 return( PSA_ERROR_STORAGE_FAILURE );
5818 }
5819}
5820#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5821
Gilles Peskinee59236f2018-01-27 23:32:46 +01005822psa_status_t psa_crypto_init( void )
5823{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005824 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005825 const unsigned char drbg_seed[] = "PSA";
5826
Gilles Peskinec6b69072018-11-20 21:42:52 +01005827 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005828 if( global_data.initialized != 0 )
5829 return( PSA_SUCCESS );
5830
Gilles Peskine5e769522018-11-20 21:59:56 +01005831 /* Set default configuration if
5832 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5833 if( global_data.entropy_init == NULL )
5834 global_data.entropy_init = mbedtls_entropy_init;
5835 if( global_data.entropy_free == NULL )
5836 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005837
Gilles Peskinec6b69072018-11-20 21:42:52 +01005838 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005839 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005840 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005841 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005842 status = mbedtls_to_psa_error(
5843 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5844 mbedtls_entropy_func,
5845 &global_data.entropy,
5846 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5847 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005848 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005849 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005850
Gilles Peskine66fb1262018-12-10 16:29:04 +01005851 status = psa_initialize_key_slots( );
5852 if( status != PSA_SUCCESS )
5853 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005854
Gilles Peskine4b734222019-07-24 15:56:31 +02005855#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005856 status = psa_crypto_load_transaction( );
5857 if( status == PSA_SUCCESS )
5858 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005859 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5860 if( status != PSA_SUCCESS )
5861 goto exit;
5862 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005863 }
5864 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5865 {
5866 /* There's no transaction to complete. It's all good. */
5867 status = PSA_SUCCESS;
5868 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005869#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005870
Gilles Peskinec6b69072018-11-20 21:42:52 +01005871 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005872 global_data.initialized = 1;
5873
Gilles Peskinee59236f2018-01-27 23:32:46 +01005874exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005875 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005876 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005877 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005878}
5879
5880#endif /* MBEDTLS_PSA_CRYPTO_C */