blob: 70ef9be0ded33b24680c200bbc3e6e0411cb4dd2 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043#include <stdlib.h>
44#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020046#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010047#define mbedtls_calloc calloc
48#define mbedtls_free free
49#endif
50
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020052#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000053#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020054#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/blowfish.h"
56#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020057#include "mbedtls/chacha20.h"
58#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/cipher.h"
60#include "mbedtls/ccm.h"
61#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020064#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010065#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010066#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/error.h"
68#include "mbedtls/gcm.h"
69#include "mbedtls/md2.h"
70#include "mbedtls/md4.h"
71#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
73#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010076#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/sha1.h"
80#include "mbedtls/sha256.h"
81#include "mbedtls/sha512.h"
82#include "mbedtls/xtea.h"
83
Gilles Peskine996deb12018-08-01 15:45:45 +020084#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
85
Gilles Peskine9ef733f2018-02-07 21:05:37 +010086/* constant-time buffer comparison */
87static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
88{
89 size_t i;
90 unsigned char diff = 0;
91
92 for( i = 0; i < n; i++ )
93 diff |= a[i] ^ b[i];
94
95 return( diff );
96}
97
98
99
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100100/****************************************************************/
101/* Global data, support functions and library management */
102/****************************************************************/
103
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104static int key_type_is_raw_bytes( psa_key_type_t type )
105{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200106 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200107}
108
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109/* Values for psa_global_data_t::rng_state */
110#define RNG_NOT_INITIALIZED 0
111#define RNG_INITIALIZED 1
112#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100113
Gilles Peskine2d277862018-06-18 15:41:12 +0200114typedef struct
115{
Gilles Peskine5e769522018-11-20 21:59:56 +0100116 void (* entropy_init )( mbedtls_entropy_context *ctx );
117 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118 mbedtls_entropy_context entropy;
119 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100120 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100121 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100122} psa_global_data_t;
123
124static psa_global_data_t global_data;
125
itayzafrir0adf0fc2018-09-06 16:24:41 +0300126#define GUARD_MODULE_INITIALIZED \
127 if( global_data.initialized == 0 ) \
128 return( PSA_ERROR_BAD_STATE );
129
Gilles Peskinee59236f2018-01-27 23:32:46 +0100130static psa_status_t mbedtls_to_psa_error( int ret )
131{
Gilles Peskinea5905292018-02-07 20:59:33 +0100132 /* If there's both a high-level code and low-level code, dispatch on
133 * the high-level code. */
134 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100135 {
136 case 0:
137 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100138
139 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
140 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
141 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
142 return( PSA_ERROR_NOT_SUPPORTED );
143 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
144 return( PSA_ERROR_HARDWARE_FAILURE );
145
146 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
147 return( PSA_ERROR_HARDWARE_FAILURE );
148
Gilles Peskine9a944802018-06-21 09:35:35 +0200149 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
150 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
151 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
152 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
153 case MBEDTLS_ERR_ASN1_INVALID_DATA:
154 return( PSA_ERROR_INVALID_ARGUMENT );
155 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
156 return( PSA_ERROR_INSUFFICIENT_MEMORY );
157 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
158 return( PSA_ERROR_BUFFER_TOO_SMALL );
159
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000161 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100165 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
166 return( PSA_ERROR_NOT_SUPPORTED );
167 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
168 return( PSA_ERROR_HARDWARE_FAILURE );
169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
180 case MBEDTLS_ERR_CCM_BAD_INPUT:
181 return( PSA_ERROR_INVALID_ARGUMENT );
182 case MBEDTLS_ERR_CCM_AUTH_FAILED:
183 return( PSA_ERROR_INVALID_SIGNATURE );
184 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
Gilles Peskine26869f22019-05-06 15:25:00 +0200187 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
188 return( PSA_ERROR_INVALID_ARGUMENT );
189
190 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
191 return( PSA_ERROR_BAD_STATE );
192 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
193 return( PSA_ERROR_INVALID_SIGNATURE );
194
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
196 return( PSA_ERROR_NOT_SUPPORTED );
197 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
200 return( PSA_ERROR_INSUFFICIENT_MEMORY );
201 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
202 return( PSA_ERROR_INVALID_PADDING );
203 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
204 return( PSA_ERROR_BAD_STATE );
205 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
206 return( PSA_ERROR_INVALID_SIGNATURE );
207 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200208 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
213 return( PSA_ERROR_HARDWARE_FAILURE );
214
215 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
216 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
217 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
218 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
219 return( PSA_ERROR_NOT_SUPPORTED );
220 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
221 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
222
223 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
Gilles Peskinee59236f2018-01-27 23:32:46 +0100228 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
229 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
230 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
233 case MBEDTLS_ERR_GCM_AUTH_FAILED:
234 return( PSA_ERROR_INVALID_SIGNATURE );
235 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200236 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100237 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
238 return( PSA_ERROR_HARDWARE_FAILURE );
239
240 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
245 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
246 return( PSA_ERROR_NOT_SUPPORTED );
247 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
248 return( PSA_ERROR_INVALID_ARGUMENT );
249 case MBEDTLS_ERR_MD_ALLOC_FAILED:
250 return( PSA_ERROR_INSUFFICIENT_MEMORY );
251 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
252 return( PSA_ERROR_STORAGE_FAILURE );
253 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
254 return( PSA_ERROR_HARDWARE_FAILURE );
255
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
257 return( PSA_ERROR_STORAGE_FAILURE );
258 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
263 return( PSA_ERROR_BUFFER_TOO_SMALL );
264 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_ALLOC_FAILED:
274 return( PSA_ERROR_INSUFFICIENT_MEMORY );
275 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
276 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100279 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100280 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
281 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
284 return( PSA_ERROR_NOT_SUPPORTED );
285 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
286 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
287 return( PSA_ERROR_NOT_PERMITTED );
288 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_PK_INVALID_ALG:
291 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
292 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
293 return( PSA_ERROR_NOT_SUPPORTED );
294 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
295 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
300 return( PSA_ERROR_HARDWARE_FAILURE );
301 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
302 return( PSA_ERROR_NOT_SUPPORTED );
303
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
305 return( PSA_ERROR_HARDWARE_FAILURE );
306
307 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_RSA_INVALID_PADDING:
310 return( PSA_ERROR_INVALID_PADDING );
311 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
312 return( PSA_ERROR_HARDWARE_FAILURE );
313 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
314 return( PSA_ERROR_INVALID_ARGUMENT );
315 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
316 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200317 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100318 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
319 return( PSA_ERROR_INVALID_SIGNATURE );
320 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
321 return( PSA_ERROR_BUFFER_TOO_SMALL );
322 case MBEDTLS_ERR_RSA_RNG_FAILED:
323 return( PSA_ERROR_INSUFFICIENT_MEMORY );
324 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
325 return( PSA_ERROR_NOT_SUPPORTED );
326 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
327 return( PSA_ERROR_HARDWARE_FAILURE );
328
329 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
330 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
332 return( PSA_ERROR_HARDWARE_FAILURE );
333
334 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
335 return( PSA_ERROR_INVALID_ARGUMENT );
336 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
337 return( PSA_ERROR_HARDWARE_FAILURE );
338
itayzafrir5c753392018-05-08 11:18:38 +0300339 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300341 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
343 return( PSA_ERROR_BUFFER_TOO_SMALL );
344 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
345 return( PSA_ERROR_NOT_SUPPORTED );
346 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
347 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
348 return( PSA_ERROR_INVALID_SIGNATURE );
349 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
350 return( PSA_ERROR_INSUFFICIENT_MEMORY );
351 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
352 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
369 return( psa_key_lifetime_is_external( slot->lifetime ) );
370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200374static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
375{
376 switch( grpid )
377 {
378 case MBEDTLS_ECP_DP_SECP192R1:
379 return( PSA_ECC_CURVE_SECP192R1 );
380 case MBEDTLS_ECP_DP_SECP224R1:
381 return( PSA_ECC_CURVE_SECP224R1 );
382 case MBEDTLS_ECP_DP_SECP256R1:
383 return( PSA_ECC_CURVE_SECP256R1 );
384 case MBEDTLS_ECP_DP_SECP384R1:
385 return( PSA_ECC_CURVE_SECP384R1 );
386 case MBEDTLS_ECP_DP_SECP521R1:
387 return( PSA_ECC_CURVE_SECP521R1 );
388 case MBEDTLS_ECP_DP_BP256R1:
389 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
390 case MBEDTLS_ECP_DP_BP384R1:
391 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
392 case MBEDTLS_ECP_DP_BP512R1:
393 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
394 case MBEDTLS_ECP_DP_CURVE25519:
395 return( PSA_ECC_CURVE_CURVE25519 );
396 case MBEDTLS_ECP_DP_SECP192K1:
397 return( PSA_ECC_CURVE_SECP192K1 );
398 case MBEDTLS_ECP_DP_SECP224K1:
399 return( PSA_ECC_CURVE_SECP224K1 );
400 case MBEDTLS_ECP_DP_SECP256K1:
401 return( PSA_ECC_CURVE_SECP256K1 );
402 case MBEDTLS_ECP_DP_CURVE448:
403 return( PSA_ECC_CURVE_CURVE448 );
404 default:
405 return( 0 );
406 }
407}
408
Gilles Peskine12313cd2018-06-20 00:20:32 +0200409static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
410{
411 switch( curve )
412 {
413 case PSA_ECC_CURVE_SECP192R1:
414 return( MBEDTLS_ECP_DP_SECP192R1 );
415 case PSA_ECC_CURVE_SECP224R1:
416 return( MBEDTLS_ECP_DP_SECP224R1 );
417 case PSA_ECC_CURVE_SECP256R1:
418 return( MBEDTLS_ECP_DP_SECP256R1 );
419 case PSA_ECC_CURVE_SECP384R1:
420 return( MBEDTLS_ECP_DP_SECP384R1 );
421 case PSA_ECC_CURVE_SECP521R1:
422 return( MBEDTLS_ECP_DP_SECP521R1 );
423 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
424 return( MBEDTLS_ECP_DP_BP256R1 );
425 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
426 return( MBEDTLS_ECP_DP_BP384R1 );
427 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
428 return( MBEDTLS_ECP_DP_BP512R1 );
429 case PSA_ECC_CURVE_CURVE25519:
430 return( MBEDTLS_ECP_DP_CURVE25519 );
431 case PSA_ECC_CURVE_SECP192K1:
432 return( MBEDTLS_ECP_DP_SECP192K1 );
433 case PSA_ECC_CURVE_SECP224K1:
434 return( MBEDTLS_ECP_DP_SECP224K1 );
435 case PSA_ECC_CURVE_SECP256K1:
436 return( MBEDTLS_ECP_DP_SECP256K1 );
437 case PSA_ECC_CURVE_CURVE448:
438 return( MBEDTLS_ECP_DP_CURVE448 );
439 default:
440 return( MBEDTLS_ECP_DP_NONE );
441 }
442}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100443#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200444
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
446 size_t bits,
447 struct raw_data *raw )
448{
449 /* Check that the bit size is acceptable for the key type */
450 switch( type )
451 {
452 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200453 if( bits == 0 )
454 {
455 raw->bytes = 0;
456 raw->data = NULL;
457 return( PSA_SUCCESS );
458 }
459 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460#if defined(MBEDTLS_MD_C)
461 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200462#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200463 case PSA_KEY_TYPE_DERIVE:
464 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200465#if defined(MBEDTLS_AES_C)
466 case PSA_KEY_TYPE_AES:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_CAMELLIA_C)
472 case PSA_KEY_TYPE_CAMELLIA:
473 if( bits != 128 && bits != 192 && bits != 256 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_DES_C)
478 case PSA_KEY_TYPE_DES:
479 if( bits != 64 && bits != 128 && bits != 192 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
483#if defined(MBEDTLS_ARC4_C)
484 case PSA_KEY_TYPE_ARC4:
485 if( bits < 8 || bits > 2048 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200489#if defined(MBEDTLS_CHACHA20_C)
490 case PSA_KEY_TYPE_CHACHA20:
491 if( bits != 256 )
492 return( PSA_ERROR_INVALID_ARGUMENT );
493 break;
494#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 default:
496 return( PSA_ERROR_NOT_SUPPORTED );
497 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200498 if( bits % 8 != 0 )
499 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200500
501 /* Allocate memory for the key */
502 raw->bytes = PSA_BITS_TO_BYTES( bits );
503 raw->data = mbedtls_calloc( 1, raw->bytes );
504 if( raw->data == NULL )
505 {
506 raw->bytes = 0;
507 return( PSA_ERROR_INSUFFICIENT_MEMORY );
508 }
509 return( PSA_SUCCESS );
510}
511
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200512#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100513/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
514 * that are not a multiple of 8) well. For example, there is only
515 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
516 * way to return the exact bit size of a key.
517 * To keep things simple, reject non-byte-aligned key sizes. */
518static psa_status_t psa_check_rsa_key_byte_aligned(
519 const mbedtls_rsa_context *rsa )
520{
521 mbedtls_mpi n;
522 psa_status_t status;
523 mbedtls_mpi_init( &n );
524 status = mbedtls_to_psa_error(
525 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
526 if( status == PSA_SUCCESS )
527 {
528 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
529 status = PSA_ERROR_NOT_SUPPORTED;
530 }
531 mbedtls_mpi_free( &n );
532 return( status );
533}
534
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000535static psa_status_t psa_import_rsa_key( psa_key_type_t type,
536 const uint8_t *data,
537 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200538 mbedtls_rsa_context **p_rsa )
539{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000540 psa_status_t status;
541 mbedtls_pk_context pk;
542 mbedtls_rsa_context *rsa;
543 size_t bits;
544
545 mbedtls_pk_init( &pk );
546
547 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200548 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000549 status = mbedtls_to_psa_error(
550 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200551 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000552 status = mbedtls_to_psa_error(
553 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
554 if( status != PSA_SUCCESS )
555 goto exit;
556
557 /* We have something that the pkparse module recognizes. If it is a
558 * valid RSA key, store it. */
559 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000561 status = PSA_ERROR_INVALID_ARGUMENT;
562 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200563 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000564
565 rsa = mbedtls_pk_rsa( pk );
566 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
567 * supports non-byte-aligned key sizes, but not well. For example,
568 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
569 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
570 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
571 {
572 status = PSA_ERROR_NOT_SUPPORTED;
573 goto exit;
574 }
575 status = psa_check_rsa_key_byte_aligned( rsa );
576
577exit:
578 /* Free the content of the pk object only on error. */
579 if( status != PSA_SUCCESS )
580 {
581 mbedtls_pk_free( &pk );
582 return( status );
583 }
584
585 /* On success, store the content of the object in the RSA context. */
586 *p_rsa = rsa;
587
588 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200589}
590#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
591
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000592#if defined(MBEDTLS_ECP_C)
593
594/* Import a public key given as the uncompressed representation defined by SEC1
595 * 2.3.3 as the content of an ECPoint. */
596static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
597 const uint8_t *data,
598 size_t data_length,
599 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200600{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000602 mbedtls_ecp_keypair *ecp = NULL;
603 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
604
605 *p_ecp = NULL;
606 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
607 if( ecp == NULL )
608 return( PSA_ERROR_INSUFFICIENT_MEMORY );
609 mbedtls_ecp_keypair_init( ecp );
610
611 /* Load the group. */
612 status = mbedtls_to_psa_error(
613 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616 /* Load the public value. */
617 status = mbedtls_to_psa_error(
618 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
619 data, data_length ) );
620 if( status != PSA_SUCCESS )
621 goto exit;
622
623 /* Check that the point is on the curve. */
624 status = mbedtls_to_psa_error(
625 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
626 if( status != PSA_SUCCESS )
627 goto exit;
628
629 *p_ecp = ecp;
630 return( PSA_SUCCESS );
631
632exit:
633 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200634 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000635 mbedtls_ecp_keypair_free( ecp );
636 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200637 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000638 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200639}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000640#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200641
Gilles Peskinef76aa772018-10-29 19:24:33 +0100642#if defined(MBEDTLS_ECP_C)
643/* Import a private key given as a byte string which is the private value
644 * in big-endian order. */
645static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
646 const uint8_t *data,
647 size_t data_length,
648 mbedtls_ecp_keypair **p_ecp )
649{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651 mbedtls_ecp_keypair *ecp = NULL;
652 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
653
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200654 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
655 return( PSA_ERROR_INVALID_ARGUMENT );
656
Gilles Peskinef76aa772018-10-29 19:24:33 +0100657 *p_ecp = NULL;
658 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
659 if( ecp == NULL )
660 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000661 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100662
663 /* Load the group. */
664 status = mbedtls_to_psa_error(
665 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
666 if( status != PSA_SUCCESS )
667 goto exit;
668 /* Load the secret value. */
669 status = mbedtls_to_psa_error(
670 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
671 if( status != PSA_SUCCESS )
672 goto exit;
673 /* Validate the private key. */
674 status = mbedtls_to_psa_error(
675 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678 /* Calculate the public key from the private key. */
679 status = mbedtls_to_psa_error(
680 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
681 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
682 if( status != PSA_SUCCESS )
683 goto exit;
684
685 *p_ecp = ecp;
686 return( PSA_SUCCESS );
687
688exit:
689 if( ecp != NULL )
690 {
691 mbedtls_ecp_keypair_free( ecp );
692 mbedtls_free( ecp );
693 }
694 return( status );
695}
696#endif /* defined(MBEDTLS_ECP_C) */
697
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100698/** Import key data into a slot. `slot->type` must have been set
699 * previously. This function assumes that the slot does not contain
700 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100701psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
702 const uint8_t *data,
703 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200705 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706
Darryl Green940d72c2018-07-13 13:18:51 +0100707 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100709 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 if( data_length > SIZE_MAX / 8 )
711 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100712 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200713 PSA_BYTES_TO_BITS( data_length ),
714 &slot->data.raw );
715 if( status != PSA_SUCCESS )
716 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200717 if( data_length != 0 )
718 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100719 }
720 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100721#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200722 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100723 {
Darryl Green940d72c2018-07-13 13:18:51 +0100724 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100725 data, data_length,
726 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100727 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000728 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
729 {
730 status = psa_import_ec_public_key(
731 PSA_KEY_TYPE_GET_CURVE( slot->type ),
732 data, data_length,
733 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000734 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100735 else
736#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000737#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
738 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100739 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000740 status = psa_import_rsa_key( slot->type,
741 data, data_length,
742 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 }
744 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000745#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 {
747 return( PSA_ERROR_NOT_SUPPORTED );
748 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000749 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100750}
751
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100752/** Calculate the intersection of two algorithm usage policies.
753 *
754 * Return 0 (which allows no operation) on incompatibility.
755 */
756static psa_algorithm_t psa_key_policy_algorithm_intersection(
757 psa_algorithm_t alg1,
758 psa_algorithm_t alg2 )
759{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200760 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100761 if( alg1 == alg2 )
762 return( alg1 );
763 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100764 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100765 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
766 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
767 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
768 {
769 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
770 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100771 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100772 return( alg1 );
773 }
774 /* If the policies are incompatible, allow nothing. */
775 return( 0 );
776}
777
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200778static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
779 psa_algorithm_t requested_alg )
780{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200781 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200782 if( requested_alg == policy_alg )
783 return( 1 );
784 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200785 * and requested_alg is the same hash-and-sign family with any hash,
786 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200787 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
788 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
789 {
790 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
791 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
792 }
793 /* If it isn't permitted, it's forbidden. */
794 return( 0 );
795}
796
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100797/** Test whether a policy permits an algorithm.
798 *
799 * The caller must test usage flags separately.
800 */
801static int psa_key_policy_permits( const psa_key_policy_t *policy,
802 psa_algorithm_t alg )
803{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200804 return( psa_key_algorithm_permits( policy->alg, alg ) ||
805 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100806}
807
Gilles Peskinef603c712019-01-19 13:40:11 +0100808/** Restrict a key policy based on a constraint.
809 *
810 * \param[in,out] policy The policy to restrict.
811 * \param[in] constraint The policy constraint to apply.
812 *
813 * \retval #PSA_SUCCESS
814 * \c *policy contains the intersection of the original value of
815 * \c *policy and \c *constraint.
816 * \retval #PSA_ERROR_INVALID_ARGUMENT
817 * \c *policy and \c *constraint are incompatible.
818 * \c *policy is unchanged.
819 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100820static psa_status_t psa_restrict_key_policy(
821 psa_key_policy_t *policy,
822 const psa_key_policy_t *constraint )
823{
824 psa_algorithm_t intersection_alg =
825 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200826 psa_algorithm_t intersection_alg2 =
827 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100828 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
829 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200830 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
831 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100832 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100833 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200834 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100835 return( PSA_SUCCESS );
836}
837
Darryl Green06fd18d2018-07-16 11:21:11 +0100838/** Retrieve a slot which must contain a key. The key must have allow all the
839 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
840 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100841static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100842 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100843 psa_key_usage_t usage,
844 psa_algorithm_t alg )
845{
846 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100847 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100848
849 *p_slot = NULL;
850
Gilles Peskinec5487a82018-12-03 18:08:14 +0100851 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100852 if( status != PSA_SUCCESS )
853 return( status );
854 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200855 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100856
857 /* Enforce that usage policy for the key slot contains all the flags
858 * required by the usage parameter. There is one exception: public
859 * keys can always be exported, so we treat public key objects as
860 * if they had the export flag. */
861 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
862 usage &= ~PSA_KEY_USAGE_EXPORT;
863 if( ( slot->policy.usage & usage ) != usage )
864 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100865
866 /* Enforce that the usage policy permits the requested algortihm. */
867 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100868 return( PSA_ERROR_NOT_PERMITTED );
869
870 *p_slot = slot;
871 return( PSA_SUCCESS );
872}
Darryl Green940d72c2018-07-13 13:18:51 +0100873
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100874/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100875static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000876{
Gilles Peskine73167e12019-07-12 23:44:37 +0200877#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
878 if( psa_key_slot_is_external( slot ) )
879 {
880 /* No key material to clean. */
881 }
882 else
883#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +0000884 if( slot->type == PSA_KEY_TYPE_NONE )
885 {
886 /* No key material to clean. */
887 }
888 else if( key_type_is_raw_bytes( slot->type ) )
889 {
890 mbedtls_free( slot->data.raw.data );
891 }
892 else
893#if defined(MBEDTLS_RSA_C)
894 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
895 {
896 mbedtls_rsa_free( slot->data.rsa );
897 mbedtls_free( slot->data.rsa );
898 }
899 else
900#endif /* defined(MBEDTLS_RSA_C) */
901#if defined(MBEDTLS_ECP_C)
902 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
903 {
904 mbedtls_ecp_keypair_free( slot->data.ecp );
905 mbedtls_free( slot->data.ecp );
906 }
907 else
908#endif /* defined(MBEDTLS_ECP_C) */
909 {
910 /* Shouldn't happen: the key type is not any type that we
911 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200912 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000913 }
914
915 return( PSA_SUCCESS );
916}
917
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100918static void psa_abort_operations_using_key( psa_key_slot_t *slot )
919{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200920 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100921 (void) slot;
922}
923
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100924/** Completely wipe a slot in memory, including its policy.
925 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100926psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100927{
928 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100929 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100930 /* At this point, key material and other type-specific content has
931 * been wiped. Clear remaining metadata. We can call memset and not
932 * zeroize because the metadata is not particularly sensitive. */
933 memset( slot, 0, sizeof( *slot ) );
934 return( status );
935}
936
Gilles Peskinec5487a82018-12-03 18:08:14 +0100937psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100938{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100939 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100940 psa_status_t status = PSA_SUCCESS;
941 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200942#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
943 psa_se_drv_table_entry_t *driver;
944#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945
Gilles Peskinec5487a82018-12-03 18:08:14 +0100946 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200947 if( status != PSA_SUCCESS )
948 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +0200949
950#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
951 driver = psa_get_se_driver_entry( slot->lifetime );
952 if( driver != NULL )
953 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
954#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
955
Darryl Greend49a4992018-06-18 17:27:26 +0100956#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
957 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
958 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100959 storage_status =
960 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100961 }
962#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +0200963
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100964 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100965 if( status != PSA_SUCCESS )
966 return( status );
967 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968}
969
Gilles Peskineb870b182018-07-06 16:02:09 +0200970/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200971static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200972{
973 if( key_type_is_raw_bytes( slot->type ) )
974 return( slot->data.raw.bytes * 8 );
975#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200976 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100977 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200978#endif /* defined(MBEDTLS_RSA_C) */
979#if defined(MBEDTLS_ECP_C)
980 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
981 return( slot->data.ecp->grp.pbits );
982#endif /* defined(MBEDTLS_ECP_C) */
983 /* Shouldn't happen except on an empty slot. */
984 return( 0 );
985}
986
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200987void psa_reset_key_attributes( psa_key_attributes_t *attributes )
988{
Gilles Peskineb699f072019-04-26 16:06:02 +0200989 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200990 memset( attributes, 0, sizeof( *attributes ) );
991}
992
Gilles Peskineb699f072019-04-26 16:06:02 +0200993psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
994 psa_key_type_t type,
995 const uint8_t *data,
996 size_t data_length )
997{
998 uint8_t *copy = NULL;
999
1000 if( data_length != 0 )
1001 {
1002 copy = mbedtls_calloc( 1, data_length );
1003 if( copy == NULL )
1004 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1005 memcpy( copy, data, data_length );
1006 }
1007 /* After this point, this function is guaranteed to succeed, so it
1008 * can start modifying `*attributes`. */
1009
1010 if( attributes->domain_parameters != NULL )
1011 {
1012 mbedtls_free( attributes->domain_parameters );
1013 attributes->domain_parameters = NULL;
1014 attributes->domain_parameters_size = 0;
1015 }
1016
1017 attributes->domain_parameters = copy;
1018 attributes->domain_parameters_size = data_length;
1019 attributes->type = type;
1020 return( PSA_SUCCESS );
1021}
1022
1023psa_status_t psa_get_key_domain_parameters(
1024 const psa_key_attributes_t *attributes,
1025 uint8_t *data, size_t data_size, size_t *data_length )
1026{
1027 if( attributes->domain_parameters_size > data_size )
1028 return( PSA_ERROR_BUFFER_TOO_SMALL );
1029 *data_length = attributes->domain_parameters_size;
1030 if( attributes->domain_parameters_size != 0 )
1031 memcpy( data, attributes->domain_parameters,
1032 attributes->domain_parameters_size );
1033 return( PSA_SUCCESS );
1034}
1035
1036#if defined(MBEDTLS_RSA_C)
1037static psa_status_t psa_get_rsa_public_exponent(
1038 const mbedtls_rsa_context *rsa,
1039 psa_key_attributes_t *attributes )
1040{
1041 mbedtls_mpi mpi;
1042 int ret;
1043 uint8_t *buffer = NULL;
1044 size_t buflen;
1045 mbedtls_mpi_init( &mpi );
1046
1047 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1048 if( ret != 0 )
1049 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001050 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1051 {
1052 /* It's the default value, which is reported as an empty string,
1053 * so there's nothing to do. */
1054 goto exit;
1055 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001056
1057 buflen = mbedtls_mpi_size( &mpi );
1058 buffer = mbedtls_calloc( 1, buflen );
1059 if( buffer == NULL )
1060 {
1061 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1062 goto exit;
1063 }
1064 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1065 if( ret != 0 )
1066 goto exit;
1067 attributes->domain_parameters = buffer;
1068 attributes->domain_parameters_size = buflen;
1069
1070exit:
1071 mbedtls_mpi_free( &mpi );
1072 if( ret != 0 )
1073 mbedtls_free( buffer );
1074 return( mbedtls_to_psa_error( ret ) );
1075}
1076#endif /* MBEDTLS_RSA_C */
1077
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001078psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1079 psa_key_attributes_t *attributes )
1080{
1081 psa_key_slot_t *slot;
1082 psa_status_t status;
1083
1084 psa_reset_key_attributes( attributes );
1085
1086 status = psa_get_key_slot( handle, &slot );
1087 if( status != PSA_SUCCESS )
1088 return( status );
1089
1090 attributes->id = slot->persistent_storage_id;
1091 attributes->lifetime = slot->lifetime;
1092 attributes->policy = slot->policy;
1093 attributes->type = slot->type;
1094 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001095
1096 switch( slot->type )
1097 {
1098#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001099 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001100 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1101 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1102 break;
1103#endif
1104 default:
1105 /* Nothing else to do. */
1106 break;
1107 }
1108
1109 if( status != PSA_SUCCESS )
1110 psa_reset_key_attributes( attributes );
1111 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001112}
1113
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001114#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001115static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1116 unsigned char *buf, size_t size )
1117{
1118 int ret;
1119 unsigned char *c;
1120 size_t len = 0;
1121
1122 c = buf + size;
1123
1124 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1125
1126 return( (int) len );
1127}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001128#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001129
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001130static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1131 uint8_t *data,
1132 size_t data_size,
1133 size_t *data_length,
1134 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001135{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001136 *data_length = 0;
1137
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001138 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001139 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001140
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001141 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001142 {
1143 if( slot->data.raw.bytes > data_size )
1144 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001145 if( data_size != 0 )
1146 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001147 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001148 memset( data + slot->data.raw.bytes, 0,
1149 data_size - slot->data.raw.bytes );
1150 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001151 *data_length = slot->data.raw.bytes;
1152 return( PSA_SUCCESS );
1153 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001154#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001155 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001156 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001157 psa_status_t status;
1158
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001159 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001160 if( bytes > data_size )
1161 return( PSA_ERROR_BUFFER_TOO_SMALL );
1162 status = mbedtls_to_psa_error(
1163 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1164 if( status != PSA_SUCCESS )
1165 return( status );
1166 memset( data + bytes, 0, data_size - bytes );
1167 *data_length = bytes;
1168 return( PSA_SUCCESS );
1169 }
1170#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001171 else
Moran Peker17e36e12018-05-02 12:55:20 +03001172 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001173#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001174 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001175 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001176 {
Moran Pekera998bc62018-04-16 18:16:20 +03001177 mbedtls_pk_context pk;
1178 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001179 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001180 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001181#if defined(MBEDTLS_RSA_C)
1182 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001183 pk.pk_info = &mbedtls_rsa_info;
1184 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001185#else
1186 return( PSA_ERROR_NOT_SUPPORTED );
1187#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001188 }
1189 else
1190 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001191#if defined(MBEDTLS_ECP_C)
1192 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001193 pk.pk_info = &mbedtls_eckey_info;
1194 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001195#else
1196 return( PSA_ERROR_NOT_SUPPORTED );
1197#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001198 }
Moran Pekerd7326592018-05-29 16:56:39 +03001199 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001200 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001201 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001202 }
Moran Peker17e36e12018-05-02 12:55:20 +03001203 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001204 {
Moran Peker17e36e12018-05-02 12:55:20 +03001205 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001206 }
Moran Peker60364322018-04-29 11:34:58 +03001207 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001208 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001209 /* If data_size is 0 then data may be NULL and then the
1210 * call to memset would have undefined behavior. */
1211 if( data_size != 0 )
1212 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001213 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001214 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001215 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1216 * Move the data to the beginning and erase remaining data
1217 * at the original location. */
1218 if( 2 * (size_t) ret <= data_size )
1219 {
1220 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001221 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001222 }
1223 else if( (size_t) ret < data_size )
1224 {
1225 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001226 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001227 }
Moran Pekera998bc62018-04-16 18:16:20 +03001228 *data_length = ret;
1229 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001230 }
1231 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001232#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001233 {
1234 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001235 it is valid for a special-purpose implementation to omit
1236 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001237 return( PSA_ERROR_NOT_SUPPORTED );
1238 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001239 }
1240}
1241
Gilles Peskinec5487a82018-12-03 18:08:14 +01001242psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001243 uint8_t *data,
1244 size_t data_size,
1245 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001246{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001247 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001248 psa_status_t status;
1249
1250 /* Set the key to empty now, so that even when there are errors, we always
1251 * set data_length to a value between 0 and data_size. On error, setting
1252 * the key to empty is a good choice because an empty key representation is
1253 * unlikely to be accepted anywhere. */
1254 *data_length = 0;
1255
1256 /* Export requires the EXPORT flag. There is an exception for public keys,
1257 * which don't require any flag, but psa_get_key_from_slot takes
1258 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001259 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001260 if( status != PSA_SUCCESS )
1261 return( status );
1262 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001263 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001264}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001265
Gilles Peskinec5487a82018-12-03 18:08:14 +01001266psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001267 uint8_t *data,
1268 size_t data_size,
1269 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001270{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001271 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001272 psa_status_t status;
1273
1274 /* Set the key to empty now, so that even when there are errors, we always
1275 * set data_length to a value between 0 and data_size. On error, setting
1276 * the key to empty is a good choice because an empty key representation is
1277 * unlikely to be accepted anywhere. */
1278 *data_length = 0;
1279
1280 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001281 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001282 if( status != PSA_SUCCESS )
1283 return( status );
1284 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001285 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001286}
1287
Gilles Peskine4747d192019-04-17 15:05:45 +02001288static psa_status_t psa_set_key_policy_internal(
1289 psa_key_slot_t *slot,
1290 const psa_key_policy_t *policy )
1291{
1292 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001293 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001294 PSA_KEY_USAGE_ENCRYPT |
1295 PSA_KEY_USAGE_DECRYPT |
1296 PSA_KEY_USAGE_SIGN |
1297 PSA_KEY_USAGE_VERIFY |
1298 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1299 return( PSA_ERROR_INVALID_ARGUMENT );
1300
1301 slot->policy = *policy;
1302 return( PSA_SUCCESS );
1303}
1304
1305/** Prepare a key slot to receive key material.
1306 *
1307 * This function allocates a key slot and sets its metadata.
1308 *
1309 * If this function fails, call psa_fail_key_creation().
1310 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001311 * This function is intended to be used as follows:
1312 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1313 * it with the specified attributes, and assign it a handle.
1314 * -# Populate the slot with the key material.
1315 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1316 * In case of failure at any step, stop the sequence and call
1317 * psa_fail_key_creation().
1318 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001319 * \param[in] attributes Key attributes for the new key.
1320 * \param[out] handle On success, a handle for the allocated slot.
1321 * \param[out] p_slot On success, a pointer to the prepared slot.
1322 * \param[out] p_drv On any return, the driver for the key, if any.
1323 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001324 *
1325 * \retval #PSA_SUCCESS
1326 * The key slot is ready to receive key material.
1327 * \return If this function fails, the key slot is an invalid state.
1328 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001329 */
1330static psa_status_t psa_start_key_creation(
1331 const psa_key_attributes_t *attributes,
1332 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001333 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001334 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001335{
1336 psa_status_t status;
1337 psa_key_slot_t *slot;
1338
Gilles Peskine011e4282019-06-26 18:34:38 +02001339 *p_drv = NULL;
1340
Gilles Peskine267c6562019-05-27 19:01:54 +02001341 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001342 if( status != PSA_SUCCESS )
1343 return( status );
1344 slot = *p_slot;
1345
1346 status = psa_set_key_policy_internal( slot, &attributes->policy );
1347 if( status != PSA_SUCCESS )
1348 return( status );
1349 slot->lifetime = attributes->lifetime;
Gilles Peskine011e4282019-06-26 18:34:38 +02001350
Gilles Peskine4747d192019-04-17 15:05:45 +02001351 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001352 {
1353 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskine011e4282019-06-26 18:34:38 +02001354 attributes->id,
1355 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001356 if( status != PSA_SUCCESS )
1357 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001358 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001359 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001360 slot->type = attributes->type;
1361
Gilles Peskinecbaff462019-07-12 23:46:04 +02001362#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1363 /* Find a slot number. Don't yet mark it as allocated in case
1364 * the key creation fails or there is a power failure. */
1365 if( *p_drv != NULL )
1366 {
1367 status = psa_find_se_slot_for_key( attributes, *p_drv,
1368 &slot->data.se.slot_number );
1369 if( status != PSA_SUCCESS )
1370 return( status );
1371 }
1372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1373
Gilles Peskine4747d192019-04-17 15:05:45 +02001374 return( status );
1375}
1376
1377/** Finalize the creation of a key once its key material has been set.
1378 *
1379 * This entails writing the key to persistent storage.
1380 *
1381 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001382 * See the documentation of psa_start_key_creation() for the intended use
1383 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001384 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001385 * \param[in,out] slot Pointer to the slot with key material.
1386 * \param[in] driver The secure element driver for the key,
1387 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001388 *
1389 * \retval #PSA_SUCCESS
1390 * The key was successfully created. The handle is now valid.
1391 * \return If this function fails, the key slot is an invalid state.
1392 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001393 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001394static psa_status_t psa_finish_key_creation(
1395 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001396 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001397{
1398 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001399 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001400 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001401
1402#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1403 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1404 {
1405 uint8_t *buffer = NULL;
1406 size_t buffer_size = 0;
1407 size_t length;
1408
1409 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001410 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001411 buffer = mbedtls_calloc( 1, buffer_size );
1412 if( buffer == NULL && buffer_size != 0 )
1413 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1414 status = psa_internal_export_key( slot,
1415 buffer, buffer_size, &length,
1416 0 );
1417
1418 if( status == PSA_SUCCESS )
1419 {
1420 status = psa_save_persistent_key( slot->persistent_storage_id,
1421 slot->type, &slot->policy,
1422 buffer, length );
1423 }
1424
1425 if( buffer_size != 0 )
1426 mbedtls_platform_zeroize( buffer, buffer_size );
1427 mbedtls_free( buffer );
1428 }
1429#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1430
Gilles Peskinecbaff462019-07-12 23:46:04 +02001431#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1432 if( driver != NULL )
1433 {
1434 status = psa_save_se_persistent_data( driver );
1435 if( status != PSA_SUCCESS )
1436 {
1437 psa_destroy_persistent_key( slot->persistent_storage_id );
1438 return( status );
1439 }
1440 }
1441#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1442
Gilles Peskine4747d192019-04-17 15:05:45 +02001443 return( status );
1444}
1445
1446/** Abort the creation of a key.
1447 *
1448 * You may call this function after calling psa_start_key_creation(),
1449 * or after psa_finish_key_creation() fails. In other circumstances, this
1450 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001451 * See the documentation of psa_start_key_creation() for the intended use
1452 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001453 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001454 * \param[in,out] slot Pointer to the slot with key material.
1455 * \param[in] driver The secure element driver for the key,
1456 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001457 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001458static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001459 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001460{
Gilles Peskine011e4282019-06-26 18:34:38 +02001461 (void) driver;
1462
Gilles Peskine4747d192019-04-17 15:05:45 +02001463 if( slot == NULL )
1464 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001465
1466#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1467 /* TOnogrepDO: If the key has already been created in the secure
1468 * element, and the failure happened later (when saving metadata
1469 * to internal storage), we need to destroy the key in the secure
1470 * element. */
1471#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1472
Gilles Peskine4747d192019-04-17 15:05:45 +02001473 psa_wipe_key_slot( slot );
1474}
1475
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001476static psa_status_t psa_check_key_slot_attributes(
1477 const psa_key_slot_t *slot,
1478 const psa_key_attributes_t *attributes )
1479{
1480 if( attributes->type != 0 )
1481 {
1482 if( attributes->type != slot->type )
1483 return( PSA_ERROR_INVALID_ARGUMENT );
1484 }
1485
1486 if( attributes->domain_parameters_size != 0 )
1487 {
1488#if defined(MBEDTLS_RSA_C)
1489 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1490 {
1491 mbedtls_mpi actual, required;
1492 int ret;
1493 mbedtls_mpi_init( &actual );
1494 mbedtls_mpi_init( &required );
1495 ret = mbedtls_rsa_export( slot->data.rsa,
1496 NULL, NULL, NULL, NULL, &actual );
1497 if( ret != 0 )
1498 goto rsa_exit;
1499 ret = mbedtls_mpi_read_binary( &required,
1500 attributes->domain_parameters,
1501 attributes->domain_parameters_size );
1502 if( ret != 0 )
1503 goto rsa_exit;
1504 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1505 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1506 rsa_exit:
1507 mbedtls_mpi_free( &actual );
1508 mbedtls_mpi_free( &required );
1509 if( ret != 0)
1510 return( mbedtls_to_psa_error( ret ) );
1511 }
1512 else
1513#endif
1514 {
1515 return( PSA_ERROR_INVALID_ARGUMENT );
1516 }
1517 }
1518
1519 if( attributes->bits != 0 )
1520 {
1521 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1522 return( PSA_ERROR_INVALID_ARGUMENT );
1523 }
1524
1525 return( PSA_SUCCESS );
1526}
1527
Gilles Peskine4747d192019-04-17 15:05:45 +02001528psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001529 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001530 size_t data_length,
1531 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001532{
1533 psa_status_t status;
1534 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001535 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001536
Gilles Peskine011e4282019-06-26 18:34:38 +02001537 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001538 if( status != PSA_SUCCESS )
1539 goto exit;
1540
1541 status = psa_import_key_into_slot( slot, data, data_length );
1542 if( status != PSA_SUCCESS )
1543 goto exit;
1544 status = psa_check_key_slot_attributes( slot, attributes );
1545 if( status != PSA_SUCCESS )
1546 goto exit;
1547
Gilles Peskine011e4282019-06-26 18:34:38 +02001548 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001549exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001550 if( status != PSA_SUCCESS )
1551 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001552 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001553 *handle = 0;
1554 }
1555 return( status );
1556}
1557
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001558static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001559 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001560{
1561 psa_status_t status;
1562 uint8_t *buffer = NULL;
1563 size_t buffer_size = 0;
1564 size_t length;
1565
1566 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001567 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001568 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001569 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001570 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001571 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1572 if( status != PSA_SUCCESS )
1573 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001574 target->type = source->type;
1575 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001576
1577exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001578 if( buffer_size != 0 )
1579 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001580 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001581 return( status );
1582}
1583
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001584psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1585 const psa_key_attributes_t *specified_attributes,
1586 psa_key_handle_t *target_handle )
1587{
1588 psa_status_t status;
1589 psa_key_slot_t *source_slot = NULL;
1590 psa_key_slot_t *target_slot = NULL;
1591 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001592 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001593
Gilles Peskinec160d9e2019-05-14 14:32:03 +02001594 status = psa_get_key_from_slot( source_handle, &source_slot,
1595 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001596 if( status != PSA_SUCCESS )
1597 goto exit;
1598
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001599 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1600 if( status != PSA_SUCCESS )
1601 goto exit;
1602
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001603 status = psa_restrict_key_policy( &actual_attributes.policy,
1604 &source_slot->policy );
1605 if( status != PSA_SUCCESS )
1606 goto exit;
1607
1608 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001609 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001610 if( status != PSA_SUCCESS )
1611 goto exit;
1612
1613 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001614 if( status != PSA_SUCCESS )
1615 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001616
Gilles Peskine011e4282019-06-26 18:34:38 +02001617 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001618exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001619 if( status != PSA_SUCCESS )
1620 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001621 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001622 *target_handle = 0;
1623 }
1624 return( status );
1625}
1626
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001627
1628
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001629/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001630/* Message digests */
1631/****************************************************************/
1632
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001633static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001634{
1635 switch( alg )
1636 {
1637#if defined(MBEDTLS_MD2_C)
1638 case PSA_ALG_MD2:
1639 return( &mbedtls_md2_info );
1640#endif
1641#if defined(MBEDTLS_MD4_C)
1642 case PSA_ALG_MD4:
1643 return( &mbedtls_md4_info );
1644#endif
1645#if defined(MBEDTLS_MD5_C)
1646 case PSA_ALG_MD5:
1647 return( &mbedtls_md5_info );
1648#endif
1649#if defined(MBEDTLS_RIPEMD160_C)
1650 case PSA_ALG_RIPEMD160:
1651 return( &mbedtls_ripemd160_info );
1652#endif
1653#if defined(MBEDTLS_SHA1_C)
1654 case PSA_ALG_SHA_1:
1655 return( &mbedtls_sha1_info );
1656#endif
1657#if defined(MBEDTLS_SHA256_C)
1658 case PSA_ALG_SHA_224:
1659 return( &mbedtls_sha224_info );
1660 case PSA_ALG_SHA_256:
1661 return( &mbedtls_sha256_info );
1662#endif
1663#if defined(MBEDTLS_SHA512_C)
1664 case PSA_ALG_SHA_384:
1665 return( &mbedtls_sha384_info );
1666 case PSA_ALG_SHA_512:
1667 return( &mbedtls_sha512_info );
1668#endif
1669 default:
1670 return( NULL );
1671 }
1672}
1673
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001674psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1675{
1676 switch( operation->alg )
1677 {
Gilles Peskine81736312018-06-26 15:04:31 +02001678 case 0:
1679 /* The object has (apparently) been initialized but it is not
1680 * in use. It's ok to call abort on such an object, and there's
1681 * nothing to do. */
1682 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001683#if defined(MBEDTLS_MD2_C)
1684 case PSA_ALG_MD2:
1685 mbedtls_md2_free( &operation->ctx.md2 );
1686 break;
1687#endif
1688#if defined(MBEDTLS_MD4_C)
1689 case PSA_ALG_MD4:
1690 mbedtls_md4_free( &operation->ctx.md4 );
1691 break;
1692#endif
1693#if defined(MBEDTLS_MD5_C)
1694 case PSA_ALG_MD5:
1695 mbedtls_md5_free( &operation->ctx.md5 );
1696 break;
1697#endif
1698#if defined(MBEDTLS_RIPEMD160_C)
1699 case PSA_ALG_RIPEMD160:
1700 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1701 break;
1702#endif
1703#if defined(MBEDTLS_SHA1_C)
1704 case PSA_ALG_SHA_1:
1705 mbedtls_sha1_free( &operation->ctx.sha1 );
1706 break;
1707#endif
1708#if defined(MBEDTLS_SHA256_C)
1709 case PSA_ALG_SHA_224:
1710 case PSA_ALG_SHA_256:
1711 mbedtls_sha256_free( &operation->ctx.sha256 );
1712 break;
1713#endif
1714#if defined(MBEDTLS_SHA512_C)
1715 case PSA_ALG_SHA_384:
1716 case PSA_ALG_SHA_512:
1717 mbedtls_sha512_free( &operation->ctx.sha512 );
1718 break;
1719#endif
1720 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001721 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001722 }
1723 operation->alg = 0;
1724 return( PSA_SUCCESS );
1725}
1726
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001727psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001728 psa_algorithm_t alg )
1729{
1730 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001731
1732 /* A context must be freshly initialized before it can be set up. */
1733 if( operation->alg != 0 )
1734 {
1735 return( PSA_ERROR_BAD_STATE );
1736 }
1737
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001738 switch( alg )
1739 {
1740#if defined(MBEDTLS_MD2_C)
1741 case PSA_ALG_MD2:
1742 mbedtls_md2_init( &operation->ctx.md2 );
1743 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1744 break;
1745#endif
1746#if defined(MBEDTLS_MD4_C)
1747 case PSA_ALG_MD4:
1748 mbedtls_md4_init( &operation->ctx.md4 );
1749 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1750 break;
1751#endif
1752#if defined(MBEDTLS_MD5_C)
1753 case PSA_ALG_MD5:
1754 mbedtls_md5_init( &operation->ctx.md5 );
1755 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1756 break;
1757#endif
1758#if defined(MBEDTLS_RIPEMD160_C)
1759 case PSA_ALG_RIPEMD160:
1760 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1761 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1762 break;
1763#endif
1764#if defined(MBEDTLS_SHA1_C)
1765 case PSA_ALG_SHA_1:
1766 mbedtls_sha1_init( &operation->ctx.sha1 );
1767 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1768 break;
1769#endif
1770#if defined(MBEDTLS_SHA256_C)
1771 case PSA_ALG_SHA_224:
1772 mbedtls_sha256_init( &operation->ctx.sha256 );
1773 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1774 break;
1775 case PSA_ALG_SHA_256:
1776 mbedtls_sha256_init( &operation->ctx.sha256 );
1777 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1778 break;
1779#endif
1780#if defined(MBEDTLS_SHA512_C)
1781 case PSA_ALG_SHA_384:
1782 mbedtls_sha512_init( &operation->ctx.sha512 );
1783 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1784 break;
1785 case PSA_ALG_SHA_512:
1786 mbedtls_sha512_init( &operation->ctx.sha512 );
1787 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1788 break;
1789#endif
1790 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001791 return( PSA_ALG_IS_HASH( alg ) ?
1792 PSA_ERROR_NOT_SUPPORTED :
1793 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001794 }
1795 if( ret == 0 )
1796 operation->alg = alg;
1797 else
1798 psa_hash_abort( operation );
1799 return( mbedtls_to_psa_error( ret ) );
1800}
1801
1802psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1803 const uint8_t *input,
1804 size_t input_length )
1805{
1806 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001807
1808 /* Don't require hash implementations to behave correctly on a
1809 * zero-length input, which may have an invalid pointer. */
1810 if( input_length == 0 )
1811 return( PSA_SUCCESS );
1812
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001813 switch( operation->alg )
1814 {
1815#if defined(MBEDTLS_MD2_C)
1816 case PSA_ALG_MD2:
1817 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1818 input, input_length );
1819 break;
1820#endif
1821#if defined(MBEDTLS_MD4_C)
1822 case PSA_ALG_MD4:
1823 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1824 input, input_length );
1825 break;
1826#endif
1827#if defined(MBEDTLS_MD5_C)
1828 case PSA_ALG_MD5:
1829 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1830 input, input_length );
1831 break;
1832#endif
1833#if defined(MBEDTLS_RIPEMD160_C)
1834 case PSA_ALG_RIPEMD160:
1835 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1836 input, input_length );
1837 break;
1838#endif
1839#if defined(MBEDTLS_SHA1_C)
1840 case PSA_ALG_SHA_1:
1841 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1842 input, input_length );
1843 break;
1844#endif
1845#if defined(MBEDTLS_SHA256_C)
1846 case PSA_ALG_SHA_224:
1847 case PSA_ALG_SHA_256:
1848 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1849 input, input_length );
1850 break;
1851#endif
1852#if defined(MBEDTLS_SHA512_C)
1853 case PSA_ALG_SHA_384:
1854 case PSA_ALG_SHA_512:
1855 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1856 input, input_length );
1857 break;
1858#endif
1859 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001860 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001861 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001862
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001863 if( ret != 0 )
1864 psa_hash_abort( operation );
1865 return( mbedtls_to_psa_error( ret ) );
1866}
1867
1868psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1869 uint8_t *hash,
1870 size_t hash_size,
1871 size_t *hash_length )
1872{
itayzafrir40835d42018-08-02 13:14:17 +03001873 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001874 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001875 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001876
1877 /* Fill the output buffer with something that isn't a valid hash
1878 * (barring an attack on the hash and deliberately-crafted input),
1879 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001880 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001881 /* If hash_size is 0 then hash may be NULL and then the
1882 * call to memset would have undefined behavior. */
1883 if( hash_size != 0 )
1884 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001885
1886 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001887 {
1888 status = PSA_ERROR_BUFFER_TOO_SMALL;
1889 goto exit;
1890 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001891
1892 switch( operation->alg )
1893 {
1894#if defined(MBEDTLS_MD2_C)
1895 case PSA_ALG_MD2:
1896 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1897 break;
1898#endif
1899#if defined(MBEDTLS_MD4_C)
1900 case PSA_ALG_MD4:
1901 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1902 break;
1903#endif
1904#if defined(MBEDTLS_MD5_C)
1905 case PSA_ALG_MD5:
1906 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1907 break;
1908#endif
1909#if defined(MBEDTLS_RIPEMD160_C)
1910 case PSA_ALG_RIPEMD160:
1911 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1912 break;
1913#endif
1914#if defined(MBEDTLS_SHA1_C)
1915 case PSA_ALG_SHA_1:
1916 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1917 break;
1918#endif
1919#if defined(MBEDTLS_SHA256_C)
1920 case PSA_ALG_SHA_224:
1921 case PSA_ALG_SHA_256:
1922 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1923 break;
1924#endif
1925#if defined(MBEDTLS_SHA512_C)
1926 case PSA_ALG_SHA_384:
1927 case PSA_ALG_SHA_512:
1928 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1929 break;
1930#endif
1931 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001932 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001933 }
itayzafrir40835d42018-08-02 13:14:17 +03001934 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001935
itayzafrir40835d42018-08-02 13:14:17 +03001936exit:
1937 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001938 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001939 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001940 return( psa_hash_abort( operation ) );
1941 }
1942 else
1943 {
1944 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001945 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001946 }
1947}
1948
Gilles Peskine2d277862018-06-18 15:41:12 +02001949psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1950 const uint8_t *hash,
1951 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001952{
1953 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1954 size_t actual_hash_length;
1955 psa_status_t status = psa_hash_finish( operation,
1956 actual_hash, sizeof( actual_hash ),
1957 &actual_hash_length );
1958 if( status != PSA_SUCCESS )
1959 return( status );
1960 if( actual_hash_length != hash_length )
1961 return( PSA_ERROR_INVALID_SIGNATURE );
1962 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1963 return( PSA_ERROR_INVALID_SIGNATURE );
1964 return( PSA_SUCCESS );
1965}
1966
Gilles Peskineeb35d782019-01-22 17:56:16 +01001967psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1968 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001969{
1970 if( target_operation->alg != 0 )
1971 return( PSA_ERROR_BAD_STATE );
1972
1973 switch( source_operation->alg )
1974 {
1975 case 0:
1976 return( PSA_ERROR_BAD_STATE );
1977#if defined(MBEDTLS_MD2_C)
1978 case PSA_ALG_MD2:
1979 mbedtls_md2_clone( &target_operation->ctx.md2,
1980 &source_operation->ctx.md2 );
1981 break;
1982#endif
1983#if defined(MBEDTLS_MD4_C)
1984 case PSA_ALG_MD4:
1985 mbedtls_md4_clone( &target_operation->ctx.md4,
1986 &source_operation->ctx.md4 );
1987 break;
1988#endif
1989#if defined(MBEDTLS_MD5_C)
1990 case PSA_ALG_MD5:
1991 mbedtls_md5_clone( &target_operation->ctx.md5,
1992 &source_operation->ctx.md5 );
1993 break;
1994#endif
1995#if defined(MBEDTLS_RIPEMD160_C)
1996 case PSA_ALG_RIPEMD160:
1997 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1998 &source_operation->ctx.ripemd160 );
1999 break;
2000#endif
2001#if defined(MBEDTLS_SHA1_C)
2002 case PSA_ALG_SHA_1:
2003 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2004 &source_operation->ctx.sha1 );
2005 break;
2006#endif
2007#if defined(MBEDTLS_SHA256_C)
2008 case PSA_ALG_SHA_224:
2009 case PSA_ALG_SHA_256:
2010 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2011 &source_operation->ctx.sha256 );
2012 break;
2013#endif
2014#if defined(MBEDTLS_SHA512_C)
2015 case PSA_ALG_SHA_384:
2016 case PSA_ALG_SHA_512:
2017 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2018 &source_operation->ctx.sha512 );
2019 break;
2020#endif
2021 default:
2022 return( PSA_ERROR_NOT_SUPPORTED );
2023 }
2024
2025 target_operation->alg = source_operation->alg;
2026 return( PSA_SUCCESS );
2027}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002028
2029
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002030/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031/* MAC */
2032/****************************************************************/
2033
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002034static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002035 psa_algorithm_t alg,
2036 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002037 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002038 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002039{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002040 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002041 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002042
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002043 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002044 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002045
Gilles Peskine8c9def32018-02-08 10:02:12 +01002046 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2047 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002048 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002049 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002050 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002051 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002052 mode = MBEDTLS_MODE_STREAM;
2053 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002054 case PSA_ALG_CTR:
2055 mode = MBEDTLS_MODE_CTR;
2056 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002057 case PSA_ALG_CFB:
2058 mode = MBEDTLS_MODE_CFB;
2059 break;
2060 case PSA_ALG_OFB:
2061 mode = MBEDTLS_MODE_OFB;
2062 break;
2063 case PSA_ALG_CBC_NO_PADDING:
2064 mode = MBEDTLS_MODE_CBC;
2065 break;
2066 case PSA_ALG_CBC_PKCS7:
2067 mode = MBEDTLS_MODE_CBC;
2068 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002069 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002070 mode = MBEDTLS_MODE_CCM;
2071 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002072 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002073 mode = MBEDTLS_MODE_GCM;
2074 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002075 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2076 mode = MBEDTLS_MODE_CHACHAPOLY;
2077 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002078 default:
2079 return( NULL );
2080 }
2081 }
2082 else if( alg == PSA_ALG_CMAC )
2083 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002084 else
2085 return( NULL );
2086
2087 switch( key_type )
2088 {
2089 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002090 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002091 break;
2092 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002093 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2094 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002095 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002096 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002097 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002098 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002099 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2100 * but two-key Triple-DES is functionally three-key Triple-DES
2101 * with K1=K3, so that's how we present it to mbedtls. */
2102 if( key_bits == 128 )
2103 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002104 break;
2105 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002106 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002107 break;
2108 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002109 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002110 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002111 case PSA_KEY_TYPE_CHACHA20:
2112 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2113 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002114 default:
2115 return( NULL );
2116 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002117 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002118 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002119
Jaeden Amero23bbb752018-06-26 14:16:54 +01002120 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2121 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002122}
2123
Gilles Peskinea05219c2018-11-16 16:02:56 +01002124#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002125static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002126{
Gilles Peskine2d277862018-06-18 15:41:12 +02002127 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002128 {
2129 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002130 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002131 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002132 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002133 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002134 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002135 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002136 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002137 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002138 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002139 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002140 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002141 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002142 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002143 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002144 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002145 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002146 return( 128 );
2147 default:
2148 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002149 }
2150}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002151#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002152
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002153/* Initialize the MAC operation structure. Once this function has been
2154 * called, psa_mac_abort can run and will do the right thing. */
2155static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2156 psa_algorithm_t alg )
2157{
2158 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2159
2160 operation->alg = alg;
2161 operation->key_set = 0;
2162 operation->iv_set = 0;
2163 operation->iv_required = 0;
2164 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002165 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002166
2167#if defined(MBEDTLS_CMAC_C)
2168 if( alg == PSA_ALG_CMAC )
2169 {
2170 operation->iv_required = 0;
2171 mbedtls_cipher_init( &operation->ctx.cmac );
2172 status = PSA_SUCCESS;
2173 }
2174 else
2175#endif /* MBEDTLS_CMAC_C */
2176#if defined(MBEDTLS_MD_C)
2177 if( PSA_ALG_IS_HMAC( operation->alg ) )
2178 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002179 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2180 operation->ctx.hmac.hash_ctx.alg = 0;
2181 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002182 }
2183 else
2184#endif /* MBEDTLS_MD_C */
2185 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002186 if( ! PSA_ALG_IS_MAC( alg ) )
2187 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002188 }
2189
2190 if( status != PSA_SUCCESS )
2191 memset( operation, 0, sizeof( *operation ) );
2192 return( status );
2193}
2194
Gilles Peskine01126fa2018-07-12 17:04:55 +02002195#if defined(MBEDTLS_MD_C)
2196static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2197{
Gilles Peskine3f108122018-12-07 18:14:53 +01002198 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002199 return( psa_hash_abort( &hmac->hash_ctx ) );
2200}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002201
Janos Follath999f6482019-06-11 12:04:10 +01002202#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002203static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2204{
2205 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2206 memset( hmac, 0, sizeof( *hmac ) );
2207}
Janos Follath999f6482019-06-11 12:04:10 +01002208#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002209#endif /* MBEDTLS_MD_C */
2210
Gilles Peskine8c9def32018-02-08 10:02:12 +01002211psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2212{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002213 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002214 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002215 /* The object has (apparently) been initialized but it is not
2216 * in use. It's ok to call abort on such an object, and there's
2217 * nothing to do. */
2218 return( PSA_SUCCESS );
2219 }
2220 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002221#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002222 if( operation->alg == PSA_ALG_CMAC )
2223 {
2224 mbedtls_cipher_free( &operation->ctx.cmac );
2225 }
2226 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002227#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002228#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002229 if( PSA_ALG_IS_HMAC( operation->alg ) )
2230 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002231 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002232 }
2233 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002234#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002235 {
2236 /* Sanity check (shouldn't happen: operation->alg should
2237 * always have been initialized to a valid value). */
2238 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002239 }
Moran Peker41deec42018-04-04 15:43:05 +03002240
Gilles Peskine8c9def32018-02-08 10:02:12 +01002241 operation->alg = 0;
2242 operation->key_set = 0;
2243 operation->iv_set = 0;
2244 operation->iv_required = 0;
2245 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002246 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002247
Gilles Peskine8c9def32018-02-08 10:02:12 +01002248 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002249
2250bad_state:
2251 /* If abort is called on an uninitialized object, we can't trust
2252 * anything. Wipe the object in case it contains confidential data.
2253 * This may result in a memory leak if a pointer gets overwritten,
2254 * but it's too late to do anything about this. */
2255 memset( operation, 0, sizeof( *operation ) );
2256 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002257}
2258
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002259#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002260static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002261 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002262 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002263 const mbedtls_cipher_info_t *cipher_info )
2264{
2265 int ret;
2266
2267 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002268
2269 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2270 if( ret != 0 )
2271 return( ret );
2272
2273 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2274 slot->data.raw.data,
2275 key_bits );
2276 return( ret );
2277}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002278#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002279
Gilles Peskine248051a2018-06-20 16:09:38 +02002280#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002281static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2282 const uint8_t *key,
2283 size_t key_length,
2284 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002285{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002286 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002287 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002288 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002289 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002290 psa_status_t status;
2291
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002292 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2293 * overflow below. This should never trigger if the hash algorithm
2294 * is implemented correctly. */
2295 /* The size checks against the ipad and opad buffers cannot be written
2296 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2297 * because that triggers -Wlogical-op on GCC 7.3. */
2298 if( block_size > sizeof( ipad ) )
2299 return( PSA_ERROR_NOT_SUPPORTED );
2300 if( block_size > sizeof( hmac->opad ) )
2301 return( PSA_ERROR_NOT_SUPPORTED );
2302 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002303 return( PSA_ERROR_NOT_SUPPORTED );
2304
Gilles Peskined223b522018-06-11 18:12:58 +02002305 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002306 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002307 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002308 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002309 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002310 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002311 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002312 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002313 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002314 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002315 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002316 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002317 }
Gilles Peskine96889972018-07-12 17:07:03 +02002318 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2319 * but it is permitted. It is common when HMAC is used in HKDF, for
2320 * example. Don't call `memcpy` in the 0-length because `key` could be
2321 * an invalid pointer which would make the behavior undefined. */
2322 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002323 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002324
Gilles Peskined223b522018-06-11 18:12:58 +02002325 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2326 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002327 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002328 ipad[i] ^= 0x36;
2329 memset( ipad + key_length, 0x36, block_size - key_length );
2330
2331 /* Copy the key material from ipad to opad, flipping the requisite bits,
2332 * and filling the rest of opad with the requisite constant. */
2333 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002334 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2335 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002336
Gilles Peskine01126fa2018-07-12 17:04:55 +02002337 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002338 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002339 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002340
Gilles Peskine01126fa2018-07-12 17:04:55 +02002341 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002342
2343cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002344 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002345
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002346 return( status );
2347}
Gilles Peskine248051a2018-06-20 16:09:38 +02002348#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002349
Gilles Peskine89167cb2018-07-08 20:12:23 +02002350static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002351 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002352 psa_algorithm_t alg,
2353 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002354{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002355 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002356 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002357 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002358 psa_key_usage_t usage =
2359 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002360 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002361 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002362
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002363 /* A context must be freshly initialized before it can be set up. */
2364 if( operation->alg != 0 )
2365 {
2366 return( PSA_ERROR_BAD_STATE );
2367 }
2368
Gilles Peskined911eb72018-08-14 15:18:45 +02002369 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002370 if( status != PSA_SUCCESS )
2371 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002372 if( is_sign )
2373 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374
Gilles Peskinec5487a82018-12-03 18:08:14 +01002375 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002376 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002377 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002378 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002379
Gilles Peskine8c9def32018-02-08 10:02:12 +01002380#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002381 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002382 {
2383 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002384 mbedtls_cipher_info_from_psa( full_length_alg,
2385 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002386 int ret;
2387 if( cipher_info == NULL )
2388 {
2389 status = PSA_ERROR_NOT_SUPPORTED;
2390 goto exit;
2391 }
2392 operation->mac_size = cipher_info->block_size;
2393 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2394 status = mbedtls_to_psa_error( ret );
2395 }
2396 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002397#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002398#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002399 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002400 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002401 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002402 if( hash_alg == 0 )
2403 {
2404 status = PSA_ERROR_NOT_SUPPORTED;
2405 goto exit;
2406 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002407
2408 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2409 /* Sanity check. This shouldn't fail on a valid configuration. */
2410 if( operation->mac_size == 0 ||
2411 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2412 {
2413 status = PSA_ERROR_NOT_SUPPORTED;
2414 goto exit;
2415 }
2416
Gilles Peskine01126fa2018-07-12 17:04:55 +02002417 if( slot->type != PSA_KEY_TYPE_HMAC )
2418 {
2419 status = PSA_ERROR_INVALID_ARGUMENT;
2420 goto exit;
2421 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002422
Gilles Peskine01126fa2018-07-12 17:04:55 +02002423 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2424 slot->data.raw.data,
2425 slot->data.raw.bytes,
2426 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002427 }
2428 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002429#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002430 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002431 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002432 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002433 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002434
Gilles Peskined911eb72018-08-14 15:18:45 +02002435 if( truncated == 0 )
2436 {
2437 /* The "normal" case: untruncated algorithm. Nothing to do. */
2438 }
2439 else if( truncated < 4 )
2440 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002441 /* A very short MAC is too short for security since it can be
2442 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2443 * so we make this our minimum, even though 32 bits is still
2444 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002445 status = PSA_ERROR_NOT_SUPPORTED;
2446 }
2447 else if( truncated > operation->mac_size )
2448 {
2449 /* It's impossible to "truncate" to a larger length. */
2450 status = PSA_ERROR_INVALID_ARGUMENT;
2451 }
2452 else
2453 operation->mac_size = truncated;
2454
Gilles Peskinefbfac682018-07-08 20:51:54 +02002455exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002456 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002457 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002458 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002460 else
2461 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002462 operation->key_set = 1;
2463 }
2464 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002465}
2466
Gilles Peskine89167cb2018-07-08 20:12:23 +02002467psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002468 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002469 psa_algorithm_t alg )
2470{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002471 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002472}
2473
2474psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002475 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002476 psa_algorithm_t alg )
2477{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002478 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002479}
2480
Gilles Peskine8c9def32018-02-08 10:02:12 +01002481psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2482 const uint8_t *input,
2483 size_t input_length )
2484{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002485 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002487 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002489 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490 operation->has_input = 1;
2491
Gilles Peskine8c9def32018-02-08 10:02:12 +01002492#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002493 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002494 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002495 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2496 input, input_length );
2497 status = mbedtls_to_psa_error( ret );
2498 }
2499 else
2500#endif /* MBEDTLS_CMAC_C */
2501#if defined(MBEDTLS_MD_C)
2502 if( PSA_ALG_IS_HMAC( operation->alg ) )
2503 {
2504 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2505 input_length );
2506 }
2507 else
2508#endif /* MBEDTLS_MD_C */
2509 {
2510 /* This shouldn't happen if `operation` was initialized by
2511 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002512 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002513 }
2514
Gilles Peskinefbfac682018-07-08 20:51:54 +02002515 if( status != PSA_SUCCESS )
2516 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002517 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518}
2519
Gilles Peskine01126fa2018-07-12 17:04:55 +02002520#if defined(MBEDTLS_MD_C)
2521static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2522 uint8_t *mac,
2523 size_t mac_size )
2524{
2525 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2526 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2527 size_t hash_size = 0;
2528 size_t block_size = psa_get_hash_block_size( hash_alg );
2529 psa_status_t status;
2530
Gilles Peskine01126fa2018-07-12 17:04:55 +02002531 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2532 if( status != PSA_SUCCESS )
2533 return( status );
2534 /* From here on, tmp needs to be wiped. */
2535
2536 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2537 if( status != PSA_SUCCESS )
2538 goto exit;
2539
2540 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2541 if( status != PSA_SUCCESS )
2542 goto exit;
2543
2544 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2545 if( status != PSA_SUCCESS )
2546 goto exit;
2547
Gilles Peskined911eb72018-08-14 15:18:45 +02002548 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2549 if( status != PSA_SUCCESS )
2550 goto exit;
2551
2552 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002553
2554exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002555 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002556 return( status );
2557}
2558#endif /* MBEDTLS_MD_C */
2559
mohammad16036df908f2018-04-02 08:34:15 -07002560static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002561 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002562 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002563{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002564 if( ! operation->key_set )
2565 return( PSA_ERROR_BAD_STATE );
2566 if( operation->iv_required && ! operation->iv_set )
2567 return( PSA_ERROR_BAD_STATE );
2568
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569 if( mac_size < operation->mac_size )
2570 return( PSA_ERROR_BUFFER_TOO_SMALL );
2571
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002573 if( operation->alg == PSA_ALG_CMAC )
2574 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002575 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2576 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2577 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002578 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002579 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002580 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002581 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002582 else
2583#endif /* MBEDTLS_CMAC_C */
2584#if defined(MBEDTLS_MD_C)
2585 if( PSA_ALG_IS_HMAC( operation->alg ) )
2586 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002587 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002588 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002589 }
2590 else
2591#endif /* MBEDTLS_MD_C */
2592 {
2593 /* This shouldn't happen if `operation` was initialized by
2594 * a setup function. */
2595 return( PSA_ERROR_BAD_STATE );
2596 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597}
2598
Gilles Peskineacd4be32018-07-08 19:56:25 +02002599psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2600 uint8_t *mac,
2601 size_t mac_size,
2602 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002603{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002604 psa_status_t status;
2605
Jaeden Amero252ef282019-02-15 14:05:35 +00002606 if( operation->alg == 0 )
2607 {
2608 return( PSA_ERROR_BAD_STATE );
2609 }
2610
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002611 /* Fill the output buffer with something that isn't a valid mac
2612 * (barring an attack on the mac and deliberately-crafted input),
2613 * in case the caller doesn't check the return status properly. */
2614 *mac_length = mac_size;
2615 /* If mac_size is 0 then mac may be NULL and then the
2616 * call to memset would have undefined behavior. */
2617 if( mac_size != 0 )
2618 memset( mac, '!', mac_size );
2619
Gilles Peskine89167cb2018-07-08 20:12:23 +02002620 if( ! operation->is_sign )
2621 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002622 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002623 }
mohammad16036df908f2018-04-02 08:34:15 -07002624
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002625 status = psa_mac_finish_internal( operation, mac, mac_size );
2626
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002627 if( status == PSA_SUCCESS )
2628 {
2629 status = psa_mac_abort( operation );
2630 if( status == PSA_SUCCESS )
2631 *mac_length = operation->mac_size;
2632 else
2633 memset( mac, '!', mac_size );
2634 }
2635 else
2636 psa_mac_abort( operation );
2637 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002638}
2639
Gilles Peskineacd4be32018-07-08 19:56:25 +02002640psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2641 const uint8_t *mac,
2642 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002643{
Gilles Peskine828ed142018-06-18 23:25:51 +02002644 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002645 psa_status_t status;
2646
Jaeden Amero252ef282019-02-15 14:05:35 +00002647 if( operation->alg == 0 )
2648 {
2649 return( PSA_ERROR_BAD_STATE );
2650 }
2651
Gilles Peskine89167cb2018-07-08 20:12:23 +02002652 if( operation->is_sign )
2653 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002654 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002655 }
2656 if( operation->mac_size != mac_length )
2657 {
2658 status = PSA_ERROR_INVALID_SIGNATURE;
2659 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002660 }
mohammad16036df908f2018-04-02 08:34:15 -07002661
2662 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002663 actual_mac, sizeof( actual_mac ) );
2664
2665 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2666 status = PSA_ERROR_INVALID_SIGNATURE;
2667
2668cleanup:
2669 if( status == PSA_SUCCESS )
2670 status = psa_mac_abort( operation );
2671 else
2672 psa_mac_abort( operation );
2673
Gilles Peskine3f108122018-12-07 18:14:53 +01002674 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002675
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002676 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002677}
2678
2679
Gilles Peskine20035e32018-02-03 22:44:14 +01002680
Gilles Peskine20035e32018-02-03 22:44:14 +01002681/****************************************************************/
2682/* Asymmetric cryptography */
2683/****************************************************************/
2684
Gilles Peskine2b450e32018-06-27 15:42:46 +02002685#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002686/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002687 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002688static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2689 size_t hash_length,
2690 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002691{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002692 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002693 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002694 *md_alg = mbedtls_md_get_type( md_info );
2695
2696 /* The Mbed TLS RSA module uses an unsigned int for hash length
2697 * parameters. Validate that it fits so that we don't risk an
2698 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002699#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002700 if( hash_length > UINT_MAX )
2701 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002702#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002703
2704#if defined(MBEDTLS_PKCS1_V15)
2705 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2706 * must be correct. */
2707 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2708 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002709 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002710 if( md_info == NULL )
2711 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002712 if( mbedtls_md_get_size( md_info ) != hash_length )
2713 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002714 }
2715#endif /* MBEDTLS_PKCS1_V15 */
2716
2717#if defined(MBEDTLS_PKCS1_V21)
2718 /* PSS requires a hash internally. */
2719 if( PSA_ALG_IS_RSA_PSS( alg ) )
2720 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002721 if( md_info == NULL )
2722 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002723 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002724#endif /* MBEDTLS_PKCS1_V21 */
2725
Gilles Peskine61b91d42018-06-08 16:09:36 +02002726 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002727}
2728
Gilles Peskine2b450e32018-06-27 15:42:46 +02002729static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2730 psa_algorithm_t alg,
2731 const uint8_t *hash,
2732 size_t hash_length,
2733 uint8_t *signature,
2734 size_t signature_size,
2735 size_t *signature_length )
2736{
2737 psa_status_t status;
2738 int ret;
2739 mbedtls_md_type_t md_alg;
2740
2741 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2742 if( status != PSA_SUCCESS )
2743 return( status );
2744
Gilles Peskine630a18a2018-06-29 17:49:35 +02002745 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002746 return( PSA_ERROR_BUFFER_TOO_SMALL );
2747
2748#if defined(MBEDTLS_PKCS1_V15)
2749 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2750 {
2751 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2752 MBEDTLS_MD_NONE );
2753 ret = mbedtls_rsa_pkcs1_sign( rsa,
2754 mbedtls_ctr_drbg_random,
2755 &global_data.ctr_drbg,
2756 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002757 md_alg,
2758 (unsigned int) hash_length,
2759 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002760 signature );
2761 }
2762 else
2763#endif /* MBEDTLS_PKCS1_V15 */
2764#if defined(MBEDTLS_PKCS1_V21)
2765 if( PSA_ALG_IS_RSA_PSS( alg ) )
2766 {
2767 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2768 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2769 mbedtls_ctr_drbg_random,
2770 &global_data.ctr_drbg,
2771 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002772 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002773 (unsigned int) hash_length,
2774 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002775 signature );
2776 }
2777 else
2778#endif /* MBEDTLS_PKCS1_V21 */
2779 {
2780 return( PSA_ERROR_INVALID_ARGUMENT );
2781 }
2782
2783 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002784 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002785 return( mbedtls_to_psa_error( ret ) );
2786}
2787
2788static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2789 psa_algorithm_t alg,
2790 const uint8_t *hash,
2791 size_t hash_length,
2792 const uint8_t *signature,
2793 size_t signature_length )
2794{
2795 psa_status_t status;
2796 int ret;
2797 mbedtls_md_type_t md_alg;
2798
2799 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2800 if( status != PSA_SUCCESS )
2801 return( status );
2802
Gilles Peskine630a18a2018-06-29 17:49:35 +02002803 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002804 return( PSA_ERROR_BUFFER_TOO_SMALL );
2805
2806#if defined(MBEDTLS_PKCS1_V15)
2807 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2808 {
2809 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2810 MBEDTLS_MD_NONE );
2811 ret = mbedtls_rsa_pkcs1_verify( rsa,
2812 mbedtls_ctr_drbg_random,
2813 &global_data.ctr_drbg,
2814 MBEDTLS_RSA_PUBLIC,
2815 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002816 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002817 hash,
2818 signature );
2819 }
2820 else
2821#endif /* MBEDTLS_PKCS1_V15 */
2822#if defined(MBEDTLS_PKCS1_V21)
2823 if( PSA_ALG_IS_RSA_PSS( alg ) )
2824 {
2825 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2826 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2827 mbedtls_ctr_drbg_random,
2828 &global_data.ctr_drbg,
2829 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002830 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002831 (unsigned int) hash_length,
2832 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002833 signature );
2834 }
2835 else
2836#endif /* MBEDTLS_PKCS1_V21 */
2837 {
2838 return( PSA_ERROR_INVALID_ARGUMENT );
2839 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002840
2841 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2842 * the rest of the signature is invalid". This has little use in
2843 * practice and PSA doesn't report this distinction. */
2844 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2845 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002846 return( mbedtls_to_psa_error( ret ) );
2847}
2848#endif /* MBEDTLS_RSA_C */
2849
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002850#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002851/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2852 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2853 * (even though these functions don't modify it). */
2854static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2855 psa_algorithm_t alg,
2856 const uint8_t *hash,
2857 size_t hash_length,
2858 uint8_t *signature,
2859 size_t signature_size,
2860 size_t *signature_length )
2861{
2862 int ret;
2863 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002864 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002865 mbedtls_mpi_init( &r );
2866 mbedtls_mpi_init( &s );
2867
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002868 if( signature_size < 2 * curve_bytes )
2869 {
2870 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2871 goto cleanup;
2872 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002873
Gilles Peskinea05219c2018-11-16 16:02:56 +01002874#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002875 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2876 {
2877 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2878 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2879 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2880 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2881 hash, hash_length,
2882 md_alg ) );
2883 }
2884 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002885#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002886 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002887 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002888 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2889 hash, hash_length,
2890 mbedtls_ctr_drbg_random,
2891 &global_data.ctr_drbg ) );
2892 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002893
2894 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2895 signature,
2896 curve_bytes ) );
2897 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2898 signature + curve_bytes,
2899 curve_bytes ) );
2900
2901cleanup:
2902 mbedtls_mpi_free( &r );
2903 mbedtls_mpi_free( &s );
2904 if( ret == 0 )
2905 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002906 return( mbedtls_to_psa_error( ret ) );
2907}
2908
2909static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2910 const uint8_t *hash,
2911 size_t hash_length,
2912 const uint8_t *signature,
2913 size_t signature_length )
2914{
2915 int ret;
2916 mbedtls_mpi r, s;
2917 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2918 mbedtls_mpi_init( &r );
2919 mbedtls_mpi_init( &s );
2920
2921 if( signature_length != 2 * curve_bytes )
2922 return( PSA_ERROR_INVALID_SIGNATURE );
2923
2924 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2925 signature,
2926 curve_bytes ) );
2927 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2928 signature + curve_bytes,
2929 curve_bytes ) );
2930
2931 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2932 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002933
2934cleanup:
2935 mbedtls_mpi_free( &r );
2936 mbedtls_mpi_free( &s );
2937 return( mbedtls_to_psa_error( ret ) );
2938}
2939#endif /* MBEDTLS_ECDSA_C */
2940
Gilles Peskinec5487a82018-12-03 18:08:14 +01002941psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002942 psa_algorithm_t alg,
2943 const uint8_t *hash,
2944 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002945 uint8_t *signature,
2946 size_t signature_size,
2947 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002948{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002949 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002950 psa_status_t status;
2951
2952 *signature_length = signature_size;
2953
Gilles Peskinec5487a82018-12-03 18:08:14 +01002954 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002955 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002956 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02002957 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002958 {
2959 status = PSA_ERROR_INVALID_ARGUMENT;
2960 goto exit;
2961 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002962
Gilles Peskine20035e32018-02-03 22:44:14 +01002963#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02002964 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01002965 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002966 status = psa_rsa_sign( slot->data.rsa,
2967 alg,
2968 hash, hash_length,
2969 signature, signature_size,
2970 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002971 }
2972 else
2973#endif /* defined(MBEDTLS_RSA_C) */
2974#if defined(MBEDTLS_ECP_C)
2975 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2976 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002977#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002978 if(
2979#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2980 PSA_ALG_IS_ECDSA( alg )
2981#else
2982 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2983#endif
2984 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002985 status = psa_ecdsa_sign( slot->data.ecp,
2986 alg,
2987 hash, hash_length,
2988 signature, signature_size,
2989 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002990 else
2991#endif /* defined(MBEDTLS_ECDSA_C) */
2992 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002993 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002994 }
itayzafrir5c753392018-05-08 11:18:38 +03002995 }
2996 else
2997#endif /* defined(MBEDTLS_ECP_C) */
2998 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002999 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003000 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003001
3002exit:
3003 /* Fill the unused part of the output buffer (the whole buffer on error,
3004 * the trailing part on success) with something that isn't a valid mac
3005 * (barring an attack on the mac and deliberately-crafted input),
3006 * in case the caller doesn't check the return status properly. */
3007 if( status == PSA_SUCCESS )
3008 memset( signature + *signature_length, '!',
3009 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003010 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003011 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003012 /* If signature_size is 0 then we have nothing to do. We must not call
3013 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003014 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003015}
3016
Gilles Peskinec5487a82018-12-03 18:08:14 +01003017psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003018 psa_algorithm_t alg,
3019 const uint8_t *hash,
3020 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003021 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003022 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003023{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003024 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003025 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003026
Gilles Peskinec5487a82018-12-03 18:08:14 +01003027 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003028 if( status != PSA_SUCCESS )
3029 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003030
Gilles Peskine61b91d42018-06-08 16:09:36 +02003031#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003032 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003033 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003034 return( psa_rsa_verify( slot->data.rsa,
3035 alg,
3036 hash, hash_length,
3037 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003038 }
3039 else
3040#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003041#if defined(MBEDTLS_ECP_C)
3042 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3043 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003044#if defined(MBEDTLS_ECDSA_C)
3045 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003046 return( psa_ecdsa_verify( slot->data.ecp,
3047 hash, hash_length,
3048 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003049 else
3050#endif /* defined(MBEDTLS_ECDSA_C) */
3051 {
3052 return( PSA_ERROR_INVALID_ARGUMENT );
3053 }
itayzafrir5c753392018-05-08 11:18:38 +03003054 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003055 else
3056#endif /* defined(MBEDTLS_ECP_C) */
3057 {
3058 return( PSA_ERROR_NOT_SUPPORTED );
3059 }
3060}
3061
Gilles Peskine072ac562018-06-30 00:21:29 +02003062#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3063static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3064 mbedtls_rsa_context *rsa )
3065{
3066 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3067 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3068 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3069 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3070}
3071#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3072
Gilles Peskinec5487a82018-12-03 18:08:14 +01003073psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003074 psa_algorithm_t alg,
3075 const uint8_t *input,
3076 size_t input_length,
3077 const uint8_t *salt,
3078 size_t salt_length,
3079 uint8_t *output,
3080 size_t output_size,
3081 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003082{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003083 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003084 psa_status_t status;
3085
Darryl Green5cc689a2018-07-24 15:34:10 +01003086 (void) input;
3087 (void) input_length;
3088 (void) salt;
3089 (void) output;
3090 (void) output_size;
3091
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003092 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003093
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003094 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3095 return( PSA_ERROR_INVALID_ARGUMENT );
3096
Gilles Peskinec5487a82018-12-03 18:08:14 +01003097 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003098 if( status != PSA_SUCCESS )
3099 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003100 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003101 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003102 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003103
3104#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003105 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003106 {
3107 mbedtls_rsa_context *rsa = slot->data.rsa;
3108 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003109 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003110 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003111#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003112 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003113 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003114 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3115 mbedtls_ctr_drbg_random,
3116 &global_data.ctr_drbg,
3117 MBEDTLS_RSA_PUBLIC,
3118 input_length,
3119 input,
3120 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003121 }
3122 else
3123#endif /* MBEDTLS_PKCS1_V15 */
3124#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003125 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003126 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003127 psa_rsa_oaep_set_padding_mode( alg, rsa );
3128 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3129 mbedtls_ctr_drbg_random,
3130 &global_data.ctr_drbg,
3131 MBEDTLS_RSA_PUBLIC,
3132 salt, salt_length,
3133 input_length,
3134 input,
3135 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003136 }
3137 else
3138#endif /* MBEDTLS_PKCS1_V21 */
3139 {
3140 return( PSA_ERROR_INVALID_ARGUMENT );
3141 }
3142 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003143 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003144 return( mbedtls_to_psa_error( ret ) );
3145 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003146 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003147#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003148 {
3149 return( PSA_ERROR_NOT_SUPPORTED );
3150 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003151}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003152
Gilles Peskinec5487a82018-12-03 18:08:14 +01003153psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003154 psa_algorithm_t alg,
3155 const uint8_t *input,
3156 size_t input_length,
3157 const uint8_t *salt,
3158 size_t salt_length,
3159 uint8_t *output,
3160 size_t output_size,
3161 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003162{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003163 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003164 psa_status_t status;
3165
Darryl Green5cc689a2018-07-24 15:34:10 +01003166 (void) input;
3167 (void) input_length;
3168 (void) salt;
3169 (void) output;
3170 (void) output_size;
3171
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003172 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003173
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003174 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3175 return( PSA_ERROR_INVALID_ARGUMENT );
3176
Gilles Peskinec5487a82018-12-03 18:08:14 +01003177 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003178 if( status != PSA_SUCCESS )
3179 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003180 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003181 return( PSA_ERROR_INVALID_ARGUMENT );
3182
3183#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003184 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003185 {
3186 mbedtls_rsa_context *rsa = slot->data.rsa;
3187 int ret;
3188
Gilles Peskine630a18a2018-06-29 17:49:35 +02003189 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003190 return( PSA_ERROR_INVALID_ARGUMENT );
3191
3192#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003193 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003194 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003195 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3196 mbedtls_ctr_drbg_random,
3197 &global_data.ctr_drbg,
3198 MBEDTLS_RSA_PRIVATE,
3199 output_length,
3200 input,
3201 output,
3202 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003203 }
3204 else
3205#endif /* MBEDTLS_PKCS1_V15 */
3206#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003207 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003208 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003209 psa_rsa_oaep_set_padding_mode( alg, rsa );
3210 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3211 mbedtls_ctr_drbg_random,
3212 &global_data.ctr_drbg,
3213 MBEDTLS_RSA_PRIVATE,
3214 salt, salt_length,
3215 output_length,
3216 input,
3217 output,
3218 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003219 }
3220 else
3221#endif /* MBEDTLS_PKCS1_V21 */
3222 {
3223 return( PSA_ERROR_INVALID_ARGUMENT );
3224 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003225
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003226 return( mbedtls_to_psa_error( ret ) );
3227 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003228 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003229#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003230 {
3231 return( PSA_ERROR_NOT_SUPPORTED );
3232 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003233}
Gilles Peskine20035e32018-02-03 22:44:14 +01003234
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003235
3236
mohammad1603503973b2018-03-12 15:59:30 +02003237/****************************************************************/
3238/* Symmetric cryptography */
3239/****************************************************************/
3240
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003241/* Initialize the cipher operation structure. Once this function has been
3242 * called, psa_cipher_abort can run and will do the right thing. */
3243static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3244 psa_algorithm_t alg )
3245{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003246 if( ! PSA_ALG_IS_CIPHER( alg ) )
3247 {
3248 memset( operation, 0, sizeof( *operation ) );
3249 return( PSA_ERROR_INVALID_ARGUMENT );
3250 }
3251
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003252 operation->alg = alg;
3253 operation->key_set = 0;
3254 operation->iv_set = 0;
3255 operation->iv_required = 1;
3256 operation->iv_size = 0;
3257 operation->block_size = 0;
3258 mbedtls_cipher_init( &operation->ctx.cipher );
3259 return( PSA_SUCCESS );
3260}
3261
Gilles Peskinee553c652018-06-04 16:22:46 +02003262static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003263 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003264 psa_algorithm_t alg,
3265 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003266{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003267 int ret = 0;
3268 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003269 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003270 size_t key_bits;
3271 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003272 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3273 PSA_KEY_USAGE_ENCRYPT :
3274 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003275
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003276 /* A context must be freshly initialized before it can be set up. */
3277 if( operation->alg != 0 )
3278 {
3279 return( PSA_ERROR_BAD_STATE );
3280 }
3281
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003282 status = psa_cipher_init( operation, alg );
3283 if( status != PSA_SUCCESS )
3284 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003285
Gilles Peskinec5487a82018-12-03 18:08:14 +01003286 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003287 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003288 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003289 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003290
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003291 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003292 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003293 {
3294 status = PSA_ERROR_NOT_SUPPORTED;
3295 goto exit;
3296 }
mohammad1603503973b2018-03-12 15:59:30 +02003297
mohammad1603503973b2018-03-12 15:59:30 +02003298 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003299 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003300 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003301
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003302#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003303 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003304 {
3305 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3306 unsigned char keys[24];
3307 memcpy( keys, slot->data.raw.data, 16 );
3308 memcpy( keys + 16, slot->data.raw.data, 8 );
3309 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3310 keys,
3311 192, cipher_operation );
3312 }
3313 else
3314#endif
3315 {
3316 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3317 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003318 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003319 }
Moran Peker41deec42018-04-04 15:43:05 +03003320 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003321 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003322
mohammad16038481e742018-03-18 13:57:31 +02003323#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003324 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003325 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003326 case PSA_ALG_CBC_NO_PADDING:
3327 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3328 MBEDTLS_PADDING_NONE );
3329 break;
3330 case PSA_ALG_CBC_PKCS7:
3331 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3332 MBEDTLS_PADDING_PKCS7 );
3333 break;
3334 default:
3335 /* The algorithm doesn't involve padding. */
3336 ret = 0;
3337 break;
3338 }
3339 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003340 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003341#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3342
mohammad1603503973b2018-03-12 15:59:30 +02003343 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003344 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3345 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3346 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003347 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003348 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003349 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003350#if defined(MBEDTLS_CHACHA20_C)
3351 else
3352 if( alg == PSA_ALG_CHACHA20 )
3353 operation->iv_size = 12;
3354#endif
mohammad1603503973b2018-03-12 15:59:30 +02003355
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003356exit:
3357 if( status == 0 )
3358 status = mbedtls_to_psa_error( ret );
3359 if( status != 0 )
3360 psa_cipher_abort( operation );
3361 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003362}
3363
Gilles Peskinefe119512018-07-08 21:39:34 +02003364psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003365 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003366 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003367{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003368 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003369}
3370
Gilles Peskinefe119512018-07-08 21:39:34 +02003371psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003372 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003373 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003374{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003375 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003376}
3377
Gilles Peskinefe119512018-07-08 21:39:34 +02003378psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3379 unsigned char *iv,
3380 size_t iv_size,
3381 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003382{
itayzafrir534bd7c2018-08-02 13:56:32 +03003383 psa_status_t status;
3384 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003385 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003386 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003387 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003388 }
Moran Peker41deec42018-04-04 15:43:05 +03003389 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003390 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003391 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003392 goto exit;
3393 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003394 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3395 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003396 if( ret != 0 )
3397 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003398 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003399 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003400 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003401
mohammad16038481e742018-03-18 13:57:31 +02003402 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003403 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003404
Moran Peker395db872018-05-31 14:07:14 +03003405exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003406 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003407 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003408 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003409}
3410
Gilles Peskinefe119512018-07-08 21:39:34 +02003411psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3412 const unsigned char *iv,
3413 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003414{
itayzafrir534bd7c2018-08-02 13:56:32 +03003415 psa_status_t status;
3416 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003417 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003418 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003419 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003420 }
Moran Pekera28258c2018-05-29 16:25:04 +03003421 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003422 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003423 status = PSA_ERROR_INVALID_ARGUMENT;
3424 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003425 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003426 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3427 status = mbedtls_to_psa_error( ret );
3428exit:
3429 if( status == PSA_SUCCESS )
3430 operation->iv_set = 1;
3431 else
mohammad1603503973b2018-03-12 15:59:30 +02003432 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003433 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003434}
3435
Gilles Peskinee553c652018-06-04 16:22:46 +02003436psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3437 const uint8_t *input,
3438 size_t input_length,
3439 unsigned char *output,
3440 size_t output_size,
3441 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003442{
itayzafrir534bd7c2018-08-02 13:56:32 +03003443 psa_status_t status;
3444 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003445 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003446
3447 if( operation->alg == 0 )
3448 {
3449 return( PSA_ERROR_BAD_STATE );
3450 }
3451
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003452 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003453 {
3454 /* Take the unprocessed partial block left over from previous
3455 * update calls, if any, plus the input to this call. Remove
3456 * the last partial block, if any. You get the data that will be
3457 * output in this call. */
3458 expected_output_size =
3459 ( operation->ctx.cipher.unprocessed_len + input_length )
3460 / operation->block_size * operation->block_size;
3461 }
3462 else
3463 {
3464 expected_output_size = input_length;
3465 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003466
Gilles Peskine89d789c2018-06-04 17:17:16 +02003467 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003468 {
3469 status = PSA_ERROR_BUFFER_TOO_SMALL;
3470 goto exit;
3471 }
mohammad160382759612018-03-12 18:16:40 +02003472
mohammad1603503973b2018-03-12 15:59:30 +02003473 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003474 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003475 status = mbedtls_to_psa_error( ret );
3476exit:
3477 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003478 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003479 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003480}
3481
Gilles Peskinee553c652018-06-04 16:22:46 +02003482psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3483 uint8_t *output,
3484 size_t output_size,
3485 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003486{
David Saadab4ecc272019-02-14 13:48:10 +02003487 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003488 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003489 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003490
mohammad1603503973b2018-03-12 15:59:30 +02003491 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003492 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003493 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003494 }
3495 if( operation->iv_required && ! operation->iv_set )
3496 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003497 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003498 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003499
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003500 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003501 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3502 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003503 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003504 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003505 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003506 }
3507
Janos Follath315b51c2018-07-09 16:04:51 +01003508 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3509 temp_output_buffer,
3510 output_length );
3511 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003512 {
Janos Follath315b51c2018-07-09 16:04:51 +01003513 status = mbedtls_to_psa_error( cipher_ret );
3514 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003515 }
Janos Follath315b51c2018-07-09 16:04:51 +01003516
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003517 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003518 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003519 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003520 memcpy( output, temp_output_buffer, *output_length );
3521 else
3522 {
Janos Follath315b51c2018-07-09 16:04:51 +01003523 status = PSA_ERROR_BUFFER_TOO_SMALL;
3524 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003525 }
mohammad1603503973b2018-03-12 15:59:30 +02003526
Gilles Peskine3f108122018-12-07 18:14:53 +01003527 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003528 status = psa_cipher_abort( operation );
3529
3530 return( status );
3531
3532error:
3533
3534 *output_length = 0;
3535
Gilles Peskine3f108122018-12-07 18:14:53 +01003536 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003537 (void) psa_cipher_abort( operation );
3538
3539 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003540}
3541
Gilles Peskinee553c652018-06-04 16:22:46 +02003542psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3543{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003544 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003545 {
3546 /* The object has (apparently) been initialized but it is not
3547 * in use. It's ok to call abort on such an object, and there's
3548 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003549 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003550 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003551
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003552 /* Sanity check (shouldn't happen: operation->alg should
3553 * always have been initialized to a valid value). */
3554 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3555 return( PSA_ERROR_BAD_STATE );
3556
mohammad1603503973b2018-03-12 15:59:30 +02003557 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003558
Moran Peker41deec42018-04-04 15:43:05 +03003559 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003560 operation->key_set = 0;
3561 operation->iv_set = 0;
3562 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003563 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003564 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003565
Moran Peker395db872018-05-31 14:07:14 +03003566 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003567}
3568
Gilles Peskinea0655c32018-04-30 17:06:50 +02003569
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003570
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003571
mohammad16035955c982018-04-26 00:53:03 +03003572/****************************************************************/
3573/* AEAD */
3574/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003575
Gilles Peskineedf9a652018-08-17 18:11:56 +02003576typedef struct
3577{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003578 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003579 const mbedtls_cipher_info_t *cipher_info;
3580 union
3581 {
3582#if defined(MBEDTLS_CCM_C)
3583 mbedtls_ccm_context ccm;
3584#endif /* MBEDTLS_CCM_C */
3585#if defined(MBEDTLS_GCM_C)
3586 mbedtls_gcm_context gcm;
3587#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003588#if defined(MBEDTLS_CHACHAPOLY_C)
3589 mbedtls_chachapoly_context chachapoly;
3590#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003591 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003592 psa_algorithm_t core_alg;
3593 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003594 uint8_t tag_length;
3595} aead_operation_t;
3596
Gilles Peskine30a9e412019-01-14 18:36:12 +01003597static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003598{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003599 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003600 {
3601#if defined(MBEDTLS_CCM_C)
3602 case PSA_ALG_CCM:
3603 mbedtls_ccm_free( &operation->ctx.ccm );
3604 break;
3605#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003606#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003607 case PSA_ALG_GCM:
3608 mbedtls_gcm_free( &operation->ctx.gcm );
3609 break;
3610#endif /* MBEDTLS_GCM_C */
3611 }
3612}
3613
3614static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003615 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003616 psa_key_usage_t usage,
3617 psa_algorithm_t alg )
3618{
3619 psa_status_t status;
3620 size_t key_bits;
3621 mbedtls_cipher_id_t cipher_id;
3622
Gilles Peskinec5487a82018-12-03 18:08:14 +01003623 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003624 if( status != PSA_SUCCESS )
3625 return( status );
3626
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003627 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003628
3629 operation->cipher_info =
3630 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3631 &cipher_id );
3632 if( operation->cipher_info == NULL )
3633 return( PSA_ERROR_NOT_SUPPORTED );
3634
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003635 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003636 {
3637#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003638 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3639 operation->core_alg = PSA_ALG_CCM;
3640 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003641 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3642 * The call to mbedtls_ccm_encrypt_and_tag or
3643 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003644 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3645 return( PSA_ERROR_INVALID_ARGUMENT );
3646 mbedtls_ccm_init( &operation->ctx.ccm );
3647 status = mbedtls_to_psa_error(
3648 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3649 operation->slot->data.raw.data,
3650 (unsigned int) key_bits ) );
3651 if( status != 0 )
3652 goto cleanup;
3653 break;
3654#endif /* MBEDTLS_CCM_C */
3655
3656#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003657 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3658 operation->core_alg = PSA_ALG_GCM;
3659 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003660 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3661 * The call to mbedtls_gcm_crypt_and_tag or
3662 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003663 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3664 return( PSA_ERROR_INVALID_ARGUMENT );
3665 mbedtls_gcm_init( &operation->ctx.gcm );
3666 status = mbedtls_to_psa_error(
3667 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3668 operation->slot->data.raw.data,
3669 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003670 if( status != 0 )
3671 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003672 break;
3673#endif /* MBEDTLS_GCM_C */
3674
Gilles Peskine26869f22019-05-06 15:25:00 +02003675#if defined(MBEDTLS_CHACHAPOLY_C)
3676 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3677 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3678 operation->full_tag_length = 16;
3679 /* We only support the default tag length. */
3680 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3681 return( PSA_ERROR_NOT_SUPPORTED );
3682 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3683 status = mbedtls_to_psa_error(
3684 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3685 operation->slot->data.raw.data ) );
3686 if( status != 0 )
3687 goto cleanup;
3688 break;
3689#endif /* MBEDTLS_CHACHAPOLY_C */
3690
Gilles Peskineedf9a652018-08-17 18:11:56 +02003691 default:
3692 return( PSA_ERROR_NOT_SUPPORTED );
3693 }
3694
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003695 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3696 {
3697 status = PSA_ERROR_INVALID_ARGUMENT;
3698 goto cleanup;
3699 }
3700 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003701
Gilles Peskineedf9a652018-08-17 18:11:56 +02003702 return( PSA_SUCCESS );
3703
3704cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003705 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003706 return( status );
3707}
3708
Gilles Peskinec5487a82018-12-03 18:08:14 +01003709psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003710 psa_algorithm_t alg,
3711 const uint8_t *nonce,
3712 size_t nonce_length,
3713 const uint8_t *additional_data,
3714 size_t additional_data_length,
3715 const uint8_t *plaintext,
3716 size_t plaintext_length,
3717 uint8_t *ciphertext,
3718 size_t ciphertext_size,
3719 size_t *ciphertext_length )
3720{
mohammad16035955c982018-04-26 00:53:03 +03003721 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003722 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003723 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003724
mohammad1603f08a5502018-06-03 15:05:47 +03003725 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003726
Gilles Peskinec5487a82018-12-03 18:08:14 +01003727 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003728 if( status != PSA_SUCCESS )
3729 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003730
Gilles Peskineedf9a652018-08-17 18:11:56 +02003731 /* For all currently supported modes, the tag is at the end of the
3732 * ciphertext. */
3733 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3734 {
3735 status = PSA_ERROR_BUFFER_TOO_SMALL;
3736 goto exit;
3737 }
3738 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003739
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003740#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003741 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003742 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003743 status = mbedtls_to_psa_error(
3744 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3745 MBEDTLS_GCM_ENCRYPT,
3746 plaintext_length,
3747 nonce, nonce_length,
3748 additional_data, additional_data_length,
3749 plaintext, ciphertext,
3750 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003751 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003752 else
3753#endif /* MBEDTLS_GCM_C */
3754#if defined(MBEDTLS_CCM_C)
3755 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003756 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003757 status = mbedtls_to_psa_error(
3758 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3759 plaintext_length,
3760 nonce, nonce_length,
3761 additional_data,
3762 additional_data_length,
3763 plaintext, ciphertext,
3764 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003765 }
mohammad16035c8845f2018-05-09 05:40:09 -07003766 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003767#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003768#if defined(MBEDTLS_CHACHAPOLY_C)
3769 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3770 {
3771 if( nonce_length != 12 || operation.tag_length != 16 )
3772 {
3773 status = PSA_ERROR_NOT_SUPPORTED;
3774 goto exit;
3775 }
3776 status = mbedtls_to_psa_error(
3777 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3778 plaintext_length,
3779 nonce,
3780 additional_data,
3781 additional_data_length,
3782 plaintext,
3783 ciphertext,
3784 tag ) );
3785 }
3786 else
3787#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003788 {
mohammad1603554faad2018-06-03 15:07:38 +03003789 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003790 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003791
Gilles Peskineedf9a652018-08-17 18:11:56 +02003792 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3793 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003794
Gilles Peskineedf9a652018-08-17 18:11:56 +02003795exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003796 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003797 if( status == PSA_SUCCESS )
3798 *ciphertext_length = plaintext_length + operation.tag_length;
3799 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003800}
3801
Gilles Peskineee652a32018-06-01 19:23:52 +02003802/* Locate the tag in a ciphertext buffer containing the encrypted data
3803 * followed by the tag. Return the length of the part preceding the tag in
3804 * *plaintext_length. This is the size of the plaintext in modes where
3805 * the encrypted data has the same size as the plaintext, such as
3806 * CCM and GCM. */
3807static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3808 const uint8_t *ciphertext,
3809 size_t ciphertext_length,
3810 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003811 const uint8_t **p_tag )
3812{
3813 size_t payload_length;
3814 if( tag_length > ciphertext_length )
3815 return( PSA_ERROR_INVALID_ARGUMENT );
3816 payload_length = ciphertext_length - tag_length;
3817 if( payload_length > plaintext_size )
3818 return( PSA_ERROR_BUFFER_TOO_SMALL );
3819 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003820 return( PSA_SUCCESS );
3821}
3822
Gilles Peskinec5487a82018-12-03 18:08:14 +01003823psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003824 psa_algorithm_t alg,
3825 const uint8_t *nonce,
3826 size_t nonce_length,
3827 const uint8_t *additional_data,
3828 size_t additional_data_length,
3829 const uint8_t *ciphertext,
3830 size_t ciphertext_length,
3831 uint8_t *plaintext,
3832 size_t plaintext_size,
3833 size_t *plaintext_length )
3834{
mohammad16035955c982018-04-26 00:53:03 +03003835 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003836 aead_operation_t operation;
3837 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003838
Gilles Peskineee652a32018-06-01 19:23:52 +02003839 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003840
Gilles Peskinec5487a82018-12-03 18:08:14 +01003841 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003842 if( status != PSA_SUCCESS )
3843 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003844
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003845 status = psa_aead_unpadded_locate_tag( operation.tag_length,
3846 ciphertext, ciphertext_length,
3847 plaintext_size, &tag );
3848 if( status != PSA_SUCCESS )
3849 goto exit;
3850
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003851#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003852 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003853 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003854 status = mbedtls_to_psa_error(
3855 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3856 ciphertext_length - operation.tag_length,
3857 nonce, nonce_length,
3858 additional_data,
3859 additional_data_length,
3860 tag, operation.tag_length,
3861 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003862 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003863 else
3864#endif /* MBEDTLS_GCM_C */
3865#if defined(MBEDTLS_CCM_C)
3866 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003867 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003868 status = mbedtls_to_psa_error(
3869 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3870 ciphertext_length - operation.tag_length,
3871 nonce, nonce_length,
3872 additional_data,
3873 additional_data_length,
3874 ciphertext, plaintext,
3875 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003876 }
mohammad160339574652018-06-01 04:39:53 -07003877 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003878#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003879#if defined(MBEDTLS_CHACHAPOLY_C)
3880 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3881 {
3882 if( nonce_length != 12 || operation.tag_length != 16 )
3883 {
3884 status = PSA_ERROR_NOT_SUPPORTED;
3885 goto exit;
3886 }
3887 status = mbedtls_to_psa_error(
3888 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
3889 ciphertext_length - operation.tag_length,
3890 nonce,
3891 additional_data,
3892 additional_data_length,
3893 tag,
3894 ciphertext,
3895 plaintext ) );
3896 }
3897 else
3898#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07003899 {
mohammad1603554faad2018-06-03 15:07:38 +03003900 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003901 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003902
Gilles Peskineedf9a652018-08-17 18:11:56 +02003903 if( status != PSA_SUCCESS && plaintext_size != 0 )
3904 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003905
Gilles Peskineedf9a652018-08-17 18:11:56 +02003906exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003907 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003908 if( status == PSA_SUCCESS )
3909 *plaintext_length = ciphertext_length - operation.tag_length;
3910 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003911}
3912
Gilles Peskinea0655c32018-04-30 17:06:50 +02003913
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003914
Gilles Peskine20035e32018-02-03 22:44:14 +01003915/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003916/* Generators */
3917/****************************************************************/
3918
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003919#define HKDF_STATE_INIT 0 /* no input yet */
3920#define HKDF_STATE_STARTED 1 /* got salt */
3921#define HKDF_STATE_KEYED 2 /* got key */
3922#define HKDF_STATE_OUTPUT 3 /* output started */
3923
Gilles Peskinecbe66502019-05-16 16:59:18 +02003924static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003925 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01003926{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003927 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
3928 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003929 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003930 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003931}
3932
3933
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003934psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003935{
3936 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003937 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003938 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003939 {
3940 /* The object has (apparently) been initialized but it is not
3941 * in use. It's ok to call abort on such an object, and there's
3942 * nothing to do. */
3943 }
3944 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003945#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003946 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003947 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003948 mbedtls_free( operation->ctx.hkdf.info );
3949 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003950 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003951 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003952 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003953 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003954 {
Janos Follath6a1d2622019-06-11 10:37:28 +01003955#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003956 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003957 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003958 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
3959 operation->ctx.tls12_prf.key_len );
3960 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003961 }
Hanno Becker580fba12018-11-13 20:50:45 +00003962
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003963 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00003964 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003965 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
3966 operation->ctx.tls12_prf.Ai_with_seed_len );
3967 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00003968 }
Janos Follath6a1d2622019-06-11 10:37:28 +01003969#else
3970 if( operation->ctx.tls12_prf.seed != NULL )
3971 {
3972 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
3973 operation->ctx.tls12_prf.seed_length );
3974 mbedtls_free( operation->ctx.tls12_prf.seed );
3975 }
3976
3977 if( operation->ctx.tls12_prf.label != NULL )
3978 {
3979 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
3980 operation->ctx.tls12_prf.label_length );
3981 mbedtls_free( operation->ctx.tls12_prf.label );
3982 }
3983
3984 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
3985
3986 /* We leave the fields Ai and output_block to be erased safely by the
3987 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01003988#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003989 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003990 else
3991#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003992 {
3993 status = PSA_ERROR_BAD_STATE;
3994 }
Janos Follath083036a2019-06-11 10:22:26 +01003995 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003996 return( status );
3997}
3998
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003999psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004000 size_t *capacity)
4001{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004002 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004003 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004004 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004005 return PSA_ERROR_BAD_STATE;
4006 }
4007
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004008 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004009 return( PSA_SUCCESS );
4010}
4011
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004012psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004013 size_t capacity )
4014{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004015 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004016 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004017 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004018 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004019 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004020 return( PSA_SUCCESS );
4021}
4022
Gilles Peskinebef7f142018-07-12 17:22:21 +02004023#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004024/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004025 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004026static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004027 psa_algorithm_t hash_alg,
4028 uint8_t *output,
4029 size_t output_length )
4030{
4031 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4032 psa_status_t status;
4033
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004034 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4035 return( PSA_ERROR_BAD_STATE );
4036 hkdf->state = HKDF_STATE_OUTPUT;
4037
Gilles Peskinebef7f142018-07-12 17:22:21 +02004038 while( output_length != 0 )
4039 {
4040 /* Copy what remains of the current block */
4041 uint8_t n = hash_length - hkdf->offset_in_block;
4042 if( n > output_length )
4043 n = (uint8_t) output_length;
4044 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4045 output += n;
4046 output_length -= n;
4047 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004048 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004049 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004050 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004051 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004052 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004053 * object was corrupted or if this function is called directly
4054 * inside the library. */
4055 if( hkdf->block_number == 0xff )
4056 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004057
4058 /* We need a new block */
4059 ++hkdf->block_number;
4060 hkdf->offset_in_block = 0;
4061 status = psa_hmac_setup_internal( &hkdf->hmac,
4062 hkdf->prk, hash_length,
4063 hash_alg );
4064 if( status != PSA_SUCCESS )
4065 return( status );
4066 if( hkdf->block_number != 1 )
4067 {
4068 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4069 hkdf->output_block,
4070 hash_length );
4071 if( status != PSA_SUCCESS )
4072 return( status );
4073 }
4074 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4075 hkdf->info,
4076 hkdf->info_length );
4077 if( status != PSA_SUCCESS )
4078 return( status );
4079 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4080 &hkdf->block_number, 1 );
4081 if( status != PSA_SUCCESS )
4082 return( status );
4083 status = psa_hmac_finish_internal( &hkdf->hmac,
4084 hkdf->output_block,
4085 sizeof( hkdf->output_block ) );
4086 if( status != PSA_SUCCESS )
4087 return( status );
4088 }
4089
4090 return( PSA_SUCCESS );
4091}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004092
Janos Follath999f6482019-06-11 12:04:10 +01004093#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004094static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4095 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004096 psa_algorithm_t alg )
4097{
4098 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4099 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4100 psa_hmac_internal_data hmac;
4101 psa_status_t status, cleanup_status;
4102
Hanno Becker3b339e22018-11-13 20:56:14 +00004103 unsigned char *Ai;
4104 size_t Ai_len;
4105
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004106 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004107 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004108 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004109 * object was corrupted or if this function is called directly
4110 * inside the library. */
4111 if( tls12_prf->block_number == 0xff )
4112 return( PSA_ERROR_BAD_STATE );
4113
4114 /* We need a new block */
4115 ++tls12_prf->block_number;
4116 tls12_prf->offset_in_block = 0;
4117
4118 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4119 *
4120 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4121 *
4122 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4123 * HMAC_hash(secret, A(2) + seed) +
4124 * HMAC_hash(secret, A(3) + seed) + ...
4125 *
4126 * A(0) = seed
4127 * A(i) = HMAC_hash( secret, A(i-1) )
4128 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004129 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004130 * `HMAC_hash(secret, A(i) + seed)` from which the output
4131 * is currently extracted as `output_block`, while
4132 * `A(i) + seed` is stored in `Ai_with_seed`.
4133 *
4134 * Generating a new block means recalculating `Ai_with_seed`
4135 * from the A(i)-part of it, and afterwards recalculating
4136 * `output_block`.
4137 *
4138 * A(0) is computed at setup time.
4139 *
4140 */
4141
4142 psa_hmac_init_internal( &hmac );
4143
4144 /* We must distinguish the calculation of A(1) from those
4145 * of A(2) and higher, because A(0)=seed has a different
4146 * length than the other A(i). */
4147 if( tls12_prf->block_number == 1 )
4148 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004149 Ai = tls12_prf->Ai_with_seed + hash_length;
4150 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004151 }
4152 else
4153 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004154 Ai = tls12_prf->Ai_with_seed;
4155 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004156 }
4157
Hanno Becker3b339e22018-11-13 20:56:14 +00004158 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4159 status = psa_hmac_setup_internal( &hmac,
4160 tls12_prf->key,
4161 tls12_prf->key_len,
4162 hash_alg );
4163 if( status != PSA_SUCCESS )
4164 goto cleanup;
4165
4166 status = psa_hash_update( &hmac.hash_ctx,
4167 Ai, Ai_len );
4168 if( status != PSA_SUCCESS )
4169 goto cleanup;
4170
4171 status = psa_hmac_finish_internal( &hmac,
4172 tls12_prf->Ai_with_seed,
4173 hash_length );
4174 if( status != PSA_SUCCESS )
4175 goto cleanup;
4176
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004177 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4178 status = psa_hmac_setup_internal( &hmac,
4179 tls12_prf->key,
4180 tls12_prf->key_len,
4181 hash_alg );
4182 if( status != PSA_SUCCESS )
4183 goto cleanup;
4184
4185 status = psa_hash_update( &hmac.hash_ctx,
4186 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004187 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004188 if( status != PSA_SUCCESS )
4189 goto cleanup;
4190
4191 status = psa_hmac_finish_internal( &hmac,
4192 tls12_prf->output_block,
4193 hash_length );
4194 if( status != PSA_SUCCESS )
4195 goto cleanup;
4196
4197cleanup:
4198
4199 cleanup_status = psa_hmac_abort_internal( &hmac );
4200 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4201 status = cleanup_status;
4202
4203 return( status );
4204}
Janos Follath7742fee2019-06-17 12:58:10 +01004205#else
4206static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4207 psa_tls12_prf_key_derivation_t *tls12_prf,
4208 psa_algorithm_t alg )
4209{
4210 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4211 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004212 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4213 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004214
Janos Follath7742fee2019-06-17 12:58:10 +01004215 /* We can't be wanting more output after block 0xff, otherwise
4216 * the capacity check in psa_key_derivation_output_bytes() would have
4217 * prevented this call. It could happen only if the operation
4218 * object was corrupted or if this function is called directly
4219 * inside the library. */
4220 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004221 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004222
4223 /* We need a new block */
4224 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004225 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004226
4227 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4228 *
4229 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4230 *
4231 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4232 * HMAC_hash(secret, A(2) + seed) +
4233 * HMAC_hash(secret, A(3) + seed) + ...
4234 *
4235 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004236 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004237 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004238 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004239 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004240 * is currently extracted as `output_block` and where i is
4241 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004242 */
4243
Janos Follathea29bfb2019-06-19 12:21:20 +01004244 /* Save the hash context before using it, to preserve the hash state with
4245 * only the inner padding in it. We need this, because inner padding depends
4246 * on the key (secret in the RFC's terminology). */
4247 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4248 if( status != PSA_SUCCESS )
4249 goto cleanup;
4250
4251 /* Calculate A(i) where i = tls12_prf->block_number. */
4252 if( tls12_prf->block_number == 1 )
4253 {
4254 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4255 * the variable seed and in this instance means it in the context of the
4256 * P_hash function, where seed = label + seed.) */
4257 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4258 tls12_prf->label, tls12_prf->label_length );
4259 if( status != PSA_SUCCESS )
4260 goto cleanup;
4261 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4262 tls12_prf->seed, tls12_prf->seed_length );
4263 if( status != PSA_SUCCESS )
4264 goto cleanup;
4265 }
4266 else
4267 {
4268 /* A(i) = HMAC_hash(secret, A(i-1)) */
4269 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4270 tls12_prf->Ai, hash_length );
4271 if( status != PSA_SUCCESS )
4272 goto cleanup;
4273 }
4274
4275 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4276 tls12_prf->Ai, hash_length );
4277 if( status != PSA_SUCCESS )
4278 goto cleanup;
4279 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4280 if( status != PSA_SUCCESS )
4281 goto cleanup;
4282
4283 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4284 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4285 tls12_prf->Ai, hash_length );
4286 if( status != PSA_SUCCESS )
4287 goto cleanup;
4288 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4289 tls12_prf->label, tls12_prf->label_length );
4290 if( status != PSA_SUCCESS )
4291 goto cleanup;
4292 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4293 tls12_prf->seed, tls12_prf->seed_length );
4294 if( status != PSA_SUCCESS )
4295 goto cleanup;
4296 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4297 tls12_prf->output_block, hash_length );
4298 if( status != PSA_SUCCESS )
4299 goto cleanup;
4300 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4301 if( status != PSA_SUCCESS )
4302 goto cleanup;
4303
Janos Follath7742fee2019-06-17 12:58:10 +01004304
4305cleanup:
4306
Janos Follathea29bfb2019-06-19 12:21:20 +01004307 cleanup_status = psa_hash_abort( &backup );
4308 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4309 status = cleanup_status;
4310
4311 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004312}
Janos Follath999f6482019-06-11 12:04:10 +01004313#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004314
Janos Follath999f6482019-06-11 12:04:10 +01004315#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004316/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004317 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004318static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004319 psa_tls12_prf_key_derivation_t *tls12_prf,
4320 psa_algorithm_t alg,
4321 uint8_t *output,
4322 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004323{
4324 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4325 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4326 psa_status_t status;
4327
4328 while( output_length != 0 )
4329 {
4330 /* Copy what remains of the current block */
4331 uint8_t n = hash_length - tls12_prf->offset_in_block;
4332
4333 /* Check if we have fully processed the current block. */
4334 if( n == 0 )
4335 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004336 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004337 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004338 if( status != PSA_SUCCESS )
4339 return( status );
4340
4341 continue;
4342 }
4343
4344 if( n > output_length )
4345 n = (uint8_t) output_length;
4346 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4347 n );
4348 output += n;
4349 output_length -= n;
4350 tls12_prf->offset_in_block += n;
4351 }
4352
4353 return( PSA_SUCCESS );
4354}
Janos Follath844eb0e2019-06-19 12:10:49 +01004355#else
4356static psa_status_t psa_key_derivation_tls12_prf_read(
4357 psa_tls12_prf_key_derivation_t *tls12_prf,
4358 psa_algorithm_t alg,
4359 uint8_t *output,
4360 size_t output_length )
4361{
4362 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4363 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4364 psa_status_t status;
4365 uint8_t offset, length;
4366
4367 while( output_length != 0 )
4368 {
4369 /* Check if we have fully processed the current block. */
4370 if( tls12_prf->left_in_block == 0 )
4371 {
4372 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4373 alg );
4374 if( status != PSA_SUCCESS )
4375 return( status );
4376
4377 continue;
4378 }
4379
4380 if( tls12_prf->left_in_block > output_length )
4381 length = (uint8_t) output_length;
4382 else
4383 length = tls12_prf->left_in_block;
4384
4385 offset = hash_length - tls12_prf->left_in_block;
4386 memcpy( output, tls12_prf->output_block + offset, length );
4387 output += length;
4388 output_length -= length;
4389 tls12_prf->left_in_block -= length;
4390 }
4391
4392 return( PSA_SUCCESS );
4393}
Janos Follath999f6482019-06-11 12:04:10 +01004394#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004395#endif /* MBEDTLS_MD_C */
4396
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004397psa_status_t psa_key_derivation_output_bytes(
4398 psa_key_derivation_operation_t *operation,
4399 uint8_t *output,
4400 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004401{
4402 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004403 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004404
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004405 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004406 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004407 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004408 return PSA_ERROR_BAD_STATE;
4409 }
4410
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004411 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004412 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004413 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004414 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004415 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004416 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004417 goto exit;
4418 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004419 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004420 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004421 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004422 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004423 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4424 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004425 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004426 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004427 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004428 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004429 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004430
Gilles Peskinebef7f142018-07-12 17:22:21 +02004431#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004432 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004433 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004434 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004435 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004436 output, output_length );
4437 }
Janos Follathadbec812019-06-14 11:05:39 +01004438 else
Janos Follathadbec812019-06-14 11:05:39 +01004439 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004440 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004441 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004442 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004443 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004444 output_length );
4445 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004446 else
4447#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004448 {
4449 return( PSA_ERROR_BAD_STATE );
4450 }
4451
4452exit:
4453 if( status != PSA_SUCCESS )
4454 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004455 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004456 * This allows us to differentiate between exhausted operations and
4457 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4458 * operations. */
4459 psa_algorithm_t alg = operation->alg;
4460 psa_key_derivation_abort( operation );
4461 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004462 memset( output, '!', output_length );
4463 }
4464 return( status );
4465}
4466
Gilles Peskine08542d82018-07-19 17:05:42 +02004467#if defined(MBEDTLS_DES_C)
4468static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4469{
4470 if( data_size >= 8 )
4471 mbedtls_des_key_set_parity( data );
4472 if( data_size >= 16 )
4473 mbedtls_des_key_set_parity( data + 8 );
4474 if( data_size >= 24 )
4475 mbedtls_des_key_set_parity( data + 16 );
4476}
4477#endif /* MBEDTLS_DES_C */
4478
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004479static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004480 psa_key_slot_t *slot,
4481 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004482 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004483{
4484 uint8_t *data = NULL;
4485 size_t bytes = PSA_BITS_TO_BYTES( bits );
4486 psa_status_t status;
4487
4488 if( ! key_type_is_raw_bytes( slot->type ) )
4489 return( PSA_ERROR_INVALID_ARGUMENT );
4490 if( bits % 8 != 0 )
4491 return( PSA_ERROR_INVALID_ARGUMENT );
4492 data = mbedtls_calloc( 1, bytes );
4493 if( data == NULL )
4494 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4495
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004496 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004497 if( status != PSA_SUCCESS )
4498 goto exit;
4499#if defined(MBEDTLS_DES_C)
4500 if( slot->type == PSA_KEY_TYPE_DES )
4501 psa_des_set_key_parity( data, bytes );
4502#endif /* MBEDTLS_DES_C */
4503 status = psa_import_key_into_slot( slot, data, bytes );
4504
4505exit:
4506 mbedtls_free( data );
4507 return( status );
4508}
4509
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004510psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004511 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004512 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004513{
4514 psa_status_t status;
4515 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004516 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004517 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004518 if( status == PSA_SUCCESS )
4519 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004520 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004521 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004522 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004523 }
4524 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004525 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004526 if( status != PSA_SUCCESS )
4527 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004528 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004529 *handle = 0;
4530 }
4531 return( status );
4532}
4533
Gilles Peskineeab56e42018-07-12 17:12:33 +02004534
4535
4536/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004537/* Key derivation */
4538/****************************************************************/
4539
Gilles Peskinea05219c2018-11-16 16:02:56 +01004540#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004541#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004542/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004543 * of the HKDF algorithm.
4544 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004545 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004546 * to potentially free embedded data structures and wipe confidential data.
4547 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004548static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004549 const uint8_t *secret,
4550 size_t secret_length,
4551 psa_algorithm_t hash_alg,
4552 const uint8_t *salt,
4553 size_t salt_length,
4554 const uint8_t *label,
4555 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004556{
4557 psa_status_t status;
4558 status = psa_hmac_setup_internal( &hkdf->hmac,
4559 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004560 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004561 if( status != PSA_SUCCESS )
4562 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004563 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004564 if( status != PSA_SUCCESS )
4565 return( status );
4566 status = psa_hmac_finish_internal( &hkdf->hmac,
4567 hkdf->prk,
4568 sizeof( hkdf->prk ) );
4569 if( status != PSA_SUCCESS )
4570 return( status );
4571 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4572 hkdf->block_number = 0;
4573 hkdf->info_length = label_length;
4574 if( label_length != 0 )
4575 {
4576 hkdf->info = mbedtls_calloc( 1, label_length );
4577 if( hkdf->info == NULL )
4578 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4579 memcpy( hkdf->info, label, label_length );
4580 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004581 hkdf->state = HKDF_STATE_KEYED;
4582 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004583 return( PSA_SUCCESS );
4584}
Janos Follath71a4c912019-06-11 09:14:47 +01004585#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004586#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004587
Gilles Peskinea05219c2018-11-16 16:02:56 +01004588#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004589#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004590/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004591 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004592 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004593 * to potentially free embedded data structures and wipe confidential data.
4594 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004595static psa_status_t psa_key_derivation_tls12_prf_setup(
4596 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004597 const unsigned char *key,
4598 size_t key_len,
4599 psa_algorithm_t hash_alg,
4600 const uint8_t *salt,
4601 size_t salt_length,
4602 const uint8_t *label,
4603 size_t label_length )
4604{
4605 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004606 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4607 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004608
4609 tls12_prf->key = mbedtls_calloc( 1, key_len );
4610 if( tls12_prf->key == NULL )
4611 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4612 tls12_prf->key_len = key_len;
4613 memcpy( tls12_prf->key, key, key_len );
4614
Hanno Becker580fba12018-11-13 20:50:45 +00004615 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004616 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004617 if( overflow )
4618 return( PSA_ERROR_INVALID_ARGUMENT );
4619
4620 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4621 if( tls12_prf->Ai_with_seed == NULL )
4622 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4623 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4624
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004625 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4626 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004627 if( label_length != 0 )
4628 {
4629 memcpy( tls12_prf->Ai_with_seed + hash_length,
4630 label, label_length );
4631 }
4632
4633 if( salt_length != 0 )
4634 {
4635 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4636 salt, salt_length );
4637 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004638
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004639 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004640 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004641 tls12_prf->block_number = 0;
4642 tls12_prf->offset_in_block = hash_length;
4643
4644 return( PSA_SUCCESS );
4645}
Janos Follath71a4c912019-06-11 09:14:47 +01004646#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004647
Janos Follath71a4c912019-06-11 09:14:47 +01004648#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004649/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004650static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4651 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004652 const unsigned char *psk,
4653 size_t psk_len,
4654 psa_algorithm_t hash_alg,
4655 const uint8_t *salt,
4656 size_t salt_length,
4657 const uint8_t *label,
4658 size_t label_length )
4659{
4660 psa_status_t status;
4661 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4662
4663 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4664 return( PSA_ERROR_INVALID_ARGUMENT );
4665
4666 /* Quoting RFC 4279, Section 2:
4667 *
4668 * The premaster secret is formed as follows: if the PSK is N octets
4669 * long, concatenate a uint16 with the value N, N zero octets, a second
4670 * uint16 with the value N, and the PSK itself.
4671 */
4672
4673 pms[0] = ( psk_len >> 8 ) & 0xff;
4674 pms[1] = ( psk_len >> 0 ) & 0xff;
4675 memset( pms + 2, 0, psk_len );
4676 pms[2 + psk_len + 0] = pms[0];
4677 pms[2 + psk_len + 1] = pms[1];
4678 memcpy( pms + 4 + psk_len, psk, psk_len );
4679
Gilles Peskinecbe66502019-05-16 16:59:18 +02004680 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004681 pms, 4 + 2 * psk_len,
4682 hash_alg,
4683 salt, salt_length,
4684 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004685
Gilles Peskine3f108122018-12-07 18:14:53 +01004686 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004687 return( status );
4688}
Janos Follath71a4c912019-06-11 09:14:47 +01004689#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004690#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004691
Janos Follath71a4c912019-06-11 09:14:47 +01004692#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004693/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004694 * to potentially free embedded data structures and wipe confidential data.
4695 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004696static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004697 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004698 const uint8_t *secret, size_t secret_length,
4699 psa_algorithm_t alg,
4700 const uint8_t *salt, size_t salt_length,
4701 const uint8_t *label, size_t label_length,
4702 size_t capacity )
4703{
4704 psa_status_t status;
4705 size_t max_capacity;
4706
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004707 /* Set operation->alg even on failure so that abort knows what to do. */
4708 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004709
4710#if defined(MBEDTLS_MD_C)
4711 if( PSA_ALG_IS_HKDF( alg ) )
4712 {
4713 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4714 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4715 if( hash_size == 0 )
4716 return( PSA_ERROR_NOT_SUPPORTED );
4717 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004718 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004719 secret, secret_length,
4720 hash_alg,
4721 salt, salt_length,
4722 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004723 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004724 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4725 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4726 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004727 {
4728 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4729 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4730
4731 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4732 if( hash_alg != PSA_ALG_SHA_256 &&
4733 hash_alg != PSA_ALG_SHA_384 )
4734 {
4735 return( PSA_ERROR_NOT_SUPPORTED );
4736 }
4737
4738 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004739
4740 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4741 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004742 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004743 secret, secret_length,
4744 hash_alg, salt, salt_length,
4745 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004746 }
4747 else
4748 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004749 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004750 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004751 secret, secret_length,
4752 hash_alg, salt, salt_length,
4753 label, label_length );
4754 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004755 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004756 else
4757#endif
4758 {
4759 return( PSA_ERROR_NOT_SUPPORTED );
4760 }
4761
4762 if( status != PSA_SUCCESS )
4763 return( status );
4764
4765 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004766 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004767 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004768 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004769 else
4770 return( PSA_ERROR_INVALID_ARGUMENT );
4771
4772 return( PSA_SUCCESS );
4773}
Janos Follath71a4c912019-06-11 09:14:47 +01004774#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004775
Janos Follath71a4c912019-06-11 09:14:47 +01004776#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004777psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004778 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004779 psa_algorithm_t alg,
4780 const uint8_t *salt,
4781 size_t salt_length,
4782 const uint8_t *label,
4783 size_t label_length,
4784 size_t capacity )
4785{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004786 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004787 psa_status_t status;
4788
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004790 return( PSA_ERROR_BAD_STATE );
4791
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004792 /* Make sure that alg is a key derivation algorithm. This prevents
4793 * key selection algorithms, which psa_key_derivation_internal
4794 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004795 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4796 return( PSA_ERROR_INVALID_ARGUMENT );
4797
Gilles Peskinec5487a82018-12-03 18:08:14 +01004798 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004799 if( status != PSA_SUCCESS )
4800 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004801
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004802 if( slot->type != PSA_KEY_TYPE_DERIVE )
4803 return( PSA_ERROR_INVALID_ARGUMENT );
4804
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004805 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004806 slot->data.raw.data,
4807 slot->data.raw.bytes,
4808 alg,
4809 salt, salt_length,
4810 label, label_length,
4811 capacity );
4812 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004813 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004814 return( status );
4815}
Janos Follath71a4c912019-06-11 09:14:47 +01004816#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004817
Gilles Peskine969c5d62019-01-16 15:53:06 +01004818static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004819 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004820 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004821{
Janos Follath5fe19732019-06-20 15:09:30 +01004822 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4823 * initialisers for this union leave some bytes unspecified.) */
4824 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4825
Gilles Peskine969c5d62019-01-16 15:53:06 +01004826 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004827#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004828 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4829 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4830 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004831 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004832 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004833 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4834 if( hash_size == 0 )
4835 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004836 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4837 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004838 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004839 {
4840 return( PSA_ERROR_NOT_SUPPORTED );
4841 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004842 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004843 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004844 }
4845#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004846 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004847 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004848}
4849
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004850psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004851 psa_algorithm_t alg )
4852{
4853 psa_status_t status;
4854
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004855 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004856 return( PSA_ERROR_BAD_STATE );
4857
Gilles Peskine6843c292019-01-18 16:44:49 +01004858 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4859 return( PSA_ERROR_INVALID_ARGUMENT );
4860 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004861 {
4862 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004863 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004864 }
4865 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4866 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004867 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004868 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004869 else
4870 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004871
4872 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004873 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004874 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004875}
4876
4877#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004878static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004879 psa_algorithm_t hash_alg,
4880 psa_key_derivation_step_t step,
4881 const uint8_t *data,
4882 size_t data_length )
4883{
4884 psa_status_t status;
4885 switch( step )
4886 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004887 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004888 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004889 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004890 status = psa_hmac_setup_internal( &hkdf->hmac,
4891 data, data_length,
4892 hash_alg );
4893 if( status != PSA_SUCCESS )
4894 return( status );
4895 hkdf->state = HKDF_STATE_STARTED;
4896 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004897 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004898 /* If no salt was provided, use an empty salt. */
4899 if( hkdf->state == HKDF_STATE_INIT )
4900 {
4901 status = psa_hmac_setup_internal( &hkdf->hmac,
4902 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004903 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004904 if( status != PSA_SUCCESS )
4905 return( status );
4906 hkdf->state = HKDF_STATE_STARTED;
4907 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004908 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004909 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004910 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4911 data, data_length );
4912 if( status != PSA_SUCCESS )
4913 return( status );
4914 status = psa_hmac_finish_internal( &hkdf->hmac,
4915 hkdf->prk,
4916 sizeof( hkdf->prk ) );
4917 if( status != PSA_SUCCESS )
4918 return( status );
4919 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4920 hkdf->block_number = 0;
4921 hkdf->state = HKDF_STATE_KEYED;
4922 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004923 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004924 if( hkdf->state == HKDF_STATE_OUTPUT )
4925 return( PSA_ERROR_BAD_STATE );
4926 if( hkdf->info_set )
4927 return( PSA_ERROR_BAD_STATE );
4928 hkdf->info_length = data_length;
4929 if( data_length != 0 )
4930 {
4931 hkdf->info = mbedtls_calloc( 1, data_length );
4932 if( hkdf->info == NULL )
4933 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4934 memcpy( hkdf->info, data, data_length );
4935 }
4936 hkdf->info_set = 1;
4937 return( PSA_SUCCESS );
4938 default:
4939 return( PSA_ERROR_INVALID_ARGUMENT );
4940 }
4941}
Janos Follathb03233e2019-06-11 15:30:30 +01004942
4943#if defined(PSA_PRE_1_0_KEY_DERIVATION)
4944static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
4945 psa_algorithm_t hash_alg,
4946 psa_key_derivation_step_t step,
4947 const uint8_t *data,
4948 size_t data_length )
4949{
4950 (void) prf;
4951 (void) hash_alg;
4952 (void) step;
4953 (void) data;
4954 (void) data_length;
4955
4956 return( PSA_ERROR_INVALID_ARGUMENT );
4957}
Janos Follath6660f0e2019-06-17 08:44:03 +01004958
4959static psa_status_t psa_tls12_prf_psk_to_ms_input(
4960 psa_tls12_prf_key_derivation_t *prf,
4961 psa_algorithm_t hash_alg,
4962 psa_key_derivation_step_t step,
4963 const uint8_t *data,
4964 size_t data_length )
4965{
4966 (void) prf;
4967 (void) hash_alg;
4968 (void) step;
4969 (void) data;
4970 (void) data_length;
4971
4972 return( PSA_ERROR_INVALID_ARGUMENT );
4973}
Janos Follathb03233e2019-06-11 15:30:30 +01004974#else
Janos Follathf08e2652019-06-13 09:05:41 +01004975static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4976 const uint8_t *data,
4977 size_t data_length )
4978{
4979 if( prf->state != TLS12_PRF_STATE_INIT )
4980 return( PSA_ERROR_BAD_STATE );
4981
Janos Follathd6dce9f2019-07-04 09:11:38 +01004982 if( data_length != 0 )
4983 {
4984 prf->seed = mbedtls_calloc( 1, data_length );
4985 if( prf->seed == NULL )
4986 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004987
Janos Follathd6dce9f2019-07-04 09:11:38 +01004988 memcpy( prf->seed, data, data_length );
4989 prf->seed_length = data_length;
4990 }
Janos Follathf08e2652019-06-13 09:05:41 +01004991
4992 prf->state = TLS12_PRF_STATE_SEED_SET;
4993
4994 return( PSA_SUCCESS );
4995}
4996
Janos Follath81550542019-06-13 14:26:34 +01004997static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4998 psa_algorithm_t hash_alg,
4999 const uint8_t *data,
5000 size_t data_length )
5001{
5002 psa_status_t status;
5003 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5004 return( PSA_ERROR_BAD_STATE );
5005
5006 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5007 if( status != PSA_SUCCESS )
5008 return( status );
5009
5010 prf->state = TLS12_PRF_STATE_KEY_SET;
5011
5012 return( PSA_SUCCESS );
5013}
5014
Janos Follath6660f0e2019-06-17 08:44:03 +01005015static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5016 psa_tls12_prf_key_derivation_t *prf,
5017 psa_algorithm_t hash_alg,
5018 const uint8_t *data,
5019 size_t data_length )
5020{
5021 psa_status_t status;
5022 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Janos Follath40e13932019-06-26 13:22:29 +01005023 unsigned char* cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005024
5025 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5026 return( PSA_ERROR_INVALID_ARGUMENT );
5027
5028 /* Quoting RFC 4279, Section 2:
5029 *
5030 * The premaster secret is formed as follows: if the PSK is N octets
5031 * long, concatenate a uint16 with the value N, N zero octets, a second
5032 * uint16 with the value N, and the PSK itself.
5033 */
5034
Janos Follath40e13932019-06-26 13:22:29 +01005035 *cur++ = ( data_length >> 8 ) & 0xff;
5036 *cur++ = ( data_length >> 0 ) & 0xff;
5037 memset( cur, 0, data_length );
5038 cur += data_length;
5039 *cur++ = pms[0];
5040 *cur++ = pms[1];
5041 memcpy( cur, data, data_length );
5042 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005043
Janos Follath40e13932019-06-26 13:22:29 +01005044 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005045
5046 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5047 return( status );
5048}
5049
Janos Follath63028dd2019-06-13 09:15:47 +01005050static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5051 const uint8_t *data,
5052 size_t data_length )
5053{
5054 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5055 return( PSA_ERROR_BAD_STATE );
5056
Janos Follathd6dce9f2019-07-04 09:11:38 +01005057 if( data_length != 0 )
5058 {
5059 prf->label = mbedtls_calloc( 1, data_length );
5060 if( prf->label == NULL )
5061 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005062
Janos Follathd6dce9f2019-07-04 09:11:38 +01005063 memcpy( prf->label, data, data_length );
5064 prf->label_length = data_length;
5065 }
Janos Follath63028dd2019-06-13 09:15:47 +01005066
5067 prf->state = TLS12_PRF_STATE_LABEL_SET;
5068
5069 return( PSA_SUCCESS );
5070}
5071
Janos Follathb03233e2019-06-11 15:30:30 +01005072static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5073 psa_algorithm_t hash_alg,
5074 psa_key_derivation_step_t step,
5075 const uint8_t *data,
5076 size_t data_length )
5077{
Janos Follathb03233e2019-06-11 15:30:30 +01005078 switch( step )
5079 {
Janos Follathf08e2652019-06-13 09:05:41 +01005080 case PSA_KEY_DERIVATION_INPUT_SEED:
5081 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005082 case PSA_KEY_DERIVATION_INPUT_SECRET:
5083 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005084 case PSA_KEY_DERIVATION_INPUT_LABEL:
5085 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005086 default:
5087 return( PSA_ERROR_INVALID_ARGUMENT );
5088 }
5089}
Janos Follath6660f0e2019-06-17 08:44:03 +01005090
5091static psa_status_t psa_tls12_prf_psk_to_ms_input(
5092 psa_tls12_prf_key_derivation_t *prf,
5093 psa_algorithm_t hash_alg,
5094 psa_key_derivation_step_t step,
5095 const uint8_t *data,
5096 size_t data_length )
5097{
5098 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005099 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005100 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5101 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005102 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005103
5104 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5105}
Janos Follathb03233e2019-06-11 15:30:30 +01005106#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005107#endif /* MBEDTLS_MD_C */
5108
Janos Follathb80a94e2019-06-12 15:54:46 +01005109static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005111 psa_key_derivation_step_t step,
5112 const uint8_t *data,
5113 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005114{
5115 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005117
5118#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005119 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005120 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005122 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005123 step, data, data_length );
5124 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005125 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005126#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005127#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005128 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005129 {
Janos Follathb03233e2019-06-11 15:30:30 +01005130 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5131 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5132 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005133 }
5134 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5135 {
5136 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5137 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5138 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005139 }
5140 else
5141#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005142 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005143 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005144 return( PSA_ERROR_BAD_STATE );
5145 }
5146
5147 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005148 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005149 return( status );
5150}
5151
Janos Follath51f4a0f2019-06-14 11:35:55 +01005152psa_status_t psa_key_derivation_input_bytes(
5153 psa_key_derivation_operation_t *operation,
5154 psa_key_derivation_step_t step,
5155 const uint8_t *data,
5156 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005157{
Janos Follathc5621512019-06-14 11:27:57 +01005158 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5159 return( PSA_ERROR_INVALID_ARGUMENT );
5160
5161 return( psa_key_derivation_input_internal( operation, step,
5162 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005163}
5164
Janos Follath51f4a0f2019-06-14 11:35:55 +01005165psa_status_t psa_key_derivation_input_key(
5166 psa_key_derivation_operation_t *operation,
5167 psa_key_derivation_step_t step,
5168 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005169{
5170 psa_key_slot_t *slot;
5171 psa_status_t status;
5172 status = psa_get_key_from_slot( handle, &slot,
5173 PSA_KEY_USAGE_DERIVE,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005174 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005175 if( status != PSA_SUCCESS )
5176 return( status );
5177 if( slot->type != PSA_KEY_TYPE_DERIVE )
5178 return( PSA_ERROR_INVALID_ARGUMENT );
5179 /* Don't allow a key to be used as an input that is usually public.
5180 * This is debatable. It's ok from a cryptographic perspective to
5181 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005182 * the material should be dedicated to a particular input step,
5183 * otherwise this may allow the key to be used in an unintended way
5184 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005185 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005186 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005187 return( psa_key_derivation_input_internal( operation,
5188 step,
5189 slot->data.raw.data,
5190 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005191}
5192
Gilles Peskineea0fb492018-07-12 17:17:20 +02005193
5194
5195/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005196/* Key agreement */
5197/****************************************************************/
5198
Gilles Peskinea05219c2018-11-16 16:02:56 +01005199#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005200static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5201 size_t peer_key_length,
5202 const mbedtls_ecp_keypair *our_key,
5203 uint8_t *shared_secret,
5204 size_t shared_secret_size,
5205 size_t *shared_secret_length )
5206{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005207 mbedtls_ecp_keypair *their_key = NULL;
5208 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005209 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005210 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005211
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005212 status = psa_import_ec_public_key(
5213 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5214 peer_key, peer_key_length,
5215 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005216 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005217 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005218
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005219 status = mbedtls_to_psa_error(
5220 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5221 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005222 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005223 status = mbedtls_to_psa_error(
5224 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5225 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005226 goto exit;
5227
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005228 status = mbedtls_to_psa_error(
5229 mbedtls_ecdh_calc_secret( &ecdh,
5230 shared_secret_length,
5231 shared_secret, shared_secret_size,
5232 mbedtls_ctr_drbg_random,
5233 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005234
5235exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005236 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005237 mbedtls_ecp_keypair_free( their_key );
5238 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005239 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005240}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005241#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005242
Gilles Peskine01d718c2018-09-18 12:01:02 +02005243#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5244
Gilles Peskine0216fe12019-04-11 21:23:21 +02005245static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5246 psa_key_slot_t *private_key,
5247 const uint8_t *peer_key,
5248 size_t peer_key_length,
5249 uint8_t *shared_secret,
5250 size_t shared_secret_size,
5251 size_t *shared_secret_length )
5252{
5253 switch( alg )
5254 {
5255#if defined(MBEDTLS_ECDH_C)
5256 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005257 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005258 return( PSA_ERROR_INVALID_ARGUMENT );
5259 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5260 private_key->data.ecp,
5261 shared_secret, shared_secret_size,
5262 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005263#endif /* MBEDTLS_ECDH_C */
5264 default:
5265 (void) private_key;
5266 (void) peer_key;
5267 (void) peer_key_length;
5268 (void) shared_secret;
5269 (void) shared_secret_size;
5270 (void) shared_secret_length;
5271 return( PSA_ERROR_NOT_SUPPORTED );
5272 }
5273}
5274
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005275/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005276 * to potentially free embedded data structures and wipe confidential data.
5277 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005278static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005279 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005280 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005281 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005282 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005283{
5284 psa_status_t status;
5285 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5286 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005288
5289 /* Step 1: run the secret agreement algorithm to generate the shared
5290 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005291 status = psa_key_agreement_raw_internal( ka_alg,
5292 private_key,
5293 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005294 shared_secret,
5295 sizeof( shared_secret ),
5296 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005297 if( status != PSA_SUCCESS )
5298 goto exit;
5299
5300 /* Step 2: set up the key derivation to generate key material from
5301 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005302 status = psa_key_derivation_input_internal( operation, step,
5303 shared_secret,
5304 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005305
Gilles Peskine01d718c2018-09-18 12:01:02 +02005306exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005307 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005308 return( status );
5309}
5310
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005311psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005312 psa_key_derivation_step_t step,
5313 psa_key_handle_t private_key,
5314 const uint8_t *peer_key,
5315 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005316{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005317 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005318 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005319 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005320 return( PSA_ERROR_INVALID_ARGUMENT );
5321 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005322 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005323 if( status != PSA_SUCCESS )
5324 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005325 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005326 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005327 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005328 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005329 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005330 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005331}
5332
Gilles Peskinebe697d82019-05-16 18:00:41 +02005333psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5334 psa_key_handle_t private_key,
5335 const uint8_t *peer_key,
5336 size_t peer_key_length,
5337 uint8_t *output,
5338 size_t output_size,
5339 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005340{
5341 psa_key_slot_t *slot;
5342 psa_status_t status;
5343
5344 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5345 {
5346 status = PSA_ERROR_INVALID_ARGUMENT;
5347 goto exit;
5348 }
5349 status = psa_get_key_from_slot( private_key, &slot,
5350 PSA_KEY_USAGE_DERIVE, alg );
5351 if( status != PSA_SUCCESS )
5352 goto exit;
5353
5354 status = psa_key_agreement_raw_internal( alg, slot,
5355 peer_key, peer_key_length,
5356 output, output_size,
5357 output_length );
5358
5359exit:
5360 if( status != PSA_SUCCESS )
5361 {
5362 /* If an error happens and is not handled properly, the output
5363 * may be used as a key to protect sensitive data. Arrange for such
5364 * a key to be random, which is likely to result in decryption or
5365 * verification errors. This is better than filling the buffer with
5366 * some constant data such as zeros, which would result in the data
5367 * being protected with a reproducible, easily knowable key.
5368 */
5369 psa_generate_random( output, output_size );
5370 *output_length = output_size;
5371 }
5372 return( status );
5373}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005374
5375
5376/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005377/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005378/****************************************************************/
5379
5380psa_status_t psa_generate_random( uint8_t *output,
5381 size_t output_size )
5382{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005383 int ret;
5384 GUARD_MODULE_INITIALIZED;
5385
5386 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005387 return( mbedtls_to_psa_error( ret ) );
5388}
5389
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005390#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5391#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005392
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005393psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5394 size_t seed_size )
5395{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005396 if( global_data.initialized )
5397 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005398
5399 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5400 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5401 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5402 return( PSA_ERROR_INVALID_ARGUMENT );
5403
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005404 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005405}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005406#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005407
Gilles Peskinee56e8782019-04-26 17:34:02 +02005408#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5409static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5410 size_t domain_parameters_size,
5411 int *exponent )
5412{
5413 size_t i;
5414 uint32_t acc = 0;
5415
5416 if( domain_parameters_size == 0 )
5417 {
5418 *exponent = 65537;
5419 return( PSA_SUCCESS );
5420 }
5421
5422 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5423 * support values that fit in a 32-bit integer, which is larger than
5424 * int on just about every platform anyway. */
5425 if( domain_parameters_size > sizeof( acc ) )
5426 return( PSA_ERROR_NOT_SUPPORTED );
5427 for( i = 0; i < domain_parameters_size; i++ )
5428 acc = ( acc << 8 ) | domain_parameters[i];
5429 if( acc > INT_MAX )
5430 return( PSA_ERROR_NOT_SUPPORTED );
5431 *exponent = acc;
5432 return( PSA_SUCCESS );
5433}
5434#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5435
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005436static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005437 psa_key_slot_t *slot, size_t bits,
5438 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005439{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005440 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005441
Gilles Peskinee56e8782019-04-26 17:34:02 +02005442 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005443 return( PSA_ERROR_INVALID_ARGUMENT );
5444
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005445 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005446 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005447 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005448 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005449 if( status != PSA_SUCCESS )
5450 return( status );
5451 status = psa_generate_random( slot->data.raw.data,
5452 slot->data.raw.bytes );
5453 if( status != PSA_SUCCESS )
5454 {
5455 mbedtls_free( slot->data.raw.data );
5456 return( status );
5457 }
5458#if defined(MBEDTLS_DES_C)
5459 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005460 psa_des_set_key_parity( slot->data.raw.data,
5461 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005462#endif /* MBEDTLS_DES_C */
5463 }
5464 else
5465
5466#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005467 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005468 {
5469 mbedtls_rsa_context *rsa;
5470 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005471 int exponent;
5472 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005473 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5474 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005475 /* Accept only byte-aligned keys, for the same reasons as
5476 * in psa_import_rsa_key(). */
5477 if( bits % 8 != 0 )
5478 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005479 status = psa_read_rsa_exponent( domain_parameters,
5480 domain_parameters_size,
5481 &exponent );
5482 if( status != PSA_SUCCESS )
5483 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005484 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5485 if( rsa == NULL )
5486 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5487 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5488 ret = mbedtls_rsa_gen_key( rsa,
5489 mbedtls_ctr_drbg_random,
5490 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005491 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005492 exponent );
5493 if( ret != 0 )
5494 {
5495 mbedtls_rsa_free( rsa );
5496 mbedtls_free( rsa );
5497 return( mbedtls_to_psa_error( ret ) );
5498 }
5499 slot->data.rsa = rsa;
5500 }
5501 else
5502#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5503
5504#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005505 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005506 {
5507 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5508 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5509 const mbedtls_ecp_curve_info *curve_info =
5510 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5511 mbedtls_ecp_keypair *ecp;
5512 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005513 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005514 return( PSA_ERROR_NOT_SUPPORTED );
5515 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5516 return( PSA_ERROR_NOT_SUPPORTED );
5517 if( curve_info->bit_size != bits )
5518 return( PSA_ERROR_INVALID_ARGUMENT );
5519 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5520 if( ecp == NULL )
5521 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5522 mbedtls_ecp_keypair_init( ecp );
5523 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5524 mbedtls_ctr_drbg_random,
5525 &global_data.ctr_drbg );
5526 if( ret != 0 )
5527 {
5528 mbedtls_ecp_keypair_free( ecp );
5529 mbedtls_free( ecp );
5530 return( mbedtls_to_psa_error( ret ) );
5531 }
5532 slot->data.ecp = ecp;
5533 }
5534 else
5535#endif /* MBEDTLS_ECP_C */
5536
5537 return( PSA_ERROR_NOT_SUPPORTED );
5538
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005539 return( PSA_SUCCESS );
5540}
5541
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005542psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005543 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005544{
5545 psa_status_t status;
5546 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005547 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005548 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005549 if( status == PSA_SUCCESS )
5550 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005551 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005552 slot, attributes->bits,
5553 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005554 }
5555 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005556 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005557 if( status != PSA_SUCCESS )
5558 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005559 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005560 *handle = 0;
5561 }
5562 return( status );
5563}
5564
5565
Gilles Peskine05d69892018-06-19 22:00:52 +02005566
5567/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005568/* Module setup */
5569/****************************************************************/
5570
Gilles Peskine5e769522018-11-20 21:59:56 +01005571psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5572 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5573 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5574{
5575 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5576 return( PSA_ERROR_BAD_STATE );
5577 global_data.entropy_init = entropy_init;
5578 global_data.entropy_free = entropy_free;
5579 return( PSA_SUCCESS );
5580}
5581
Gilles Peskinee59236f2018-01-27 23:32:46 +01005582void mbedtls_psa_crypto_free( void )
5583{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005584 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005585 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5586 {
5587 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005588 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005589 }
5590 /* Wipe all remaining data, including configuration.
5591 * In particular, this sets all state indicator to the value
5592 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005593 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005594#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005595 /* Unregister all secure element drivers, so that we restart from
5596 * a pristine state. */
5597 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005598#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005599}
5600
5601psa_status_t psa_crypto_init( void )
5602{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005603 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005604 const unsigned char drbg_seed[] = "PSA";
5605
Gilles Peskinec6b69072018-11-20 21:42:52 +01005606 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005607 if( global_data.initialized != 0 )
5608 return( PSA_SUCCESS );
5609
Gilles Peskine5e769522018-11-20 21:59:56 +01005610 /* Set default configuration if
5611 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5612 if( global_data.entropy_init == NULL )
5613 global_data.entropy_init = mbedtls_entropy_init;
5614 if( global_data.entropy_free == NULL )
5615 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005616
Gilles Peskinec6b69072018-11-20 21:42:52 +01005617 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005618 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005619 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005620 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005621 status = mbedtls_to_psa_error(
5622 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5623 mbedtls_entropy_func,
5624 &global_data.entropy,
5625 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5626 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005627 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005628 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005629
Gilles Peskine66fb1262018-12-10 16:29:04 +01005630 status = psa_initialize_key_slots( );
5631 if( status != PSA_SUCCESS )
5632 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005633
5634 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005635 global_data.initialized = 1;
5636
Gilles Peskinee59236f2018-01-27 23:32:46 +01005637exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005638 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005639 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005640 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005641}
5642
5643#endif /* MBEDTLS_PSA_CRYPTO_C */