blob: 93c9ce444859e09a7cee72e68ad166e7494ffb7c [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 Peskine2f9c4dc2018-01-28 13:16:24 +0100942
Gilles Peskinec5487a82018-12-03 18:08:14 +0100943 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200944 if( status != PSA_SUCCESS )
945 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100946#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
947 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
948 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100949 storage_status =
950 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100951 }
952#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100953 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100954 if( status != PSA_SUCCESS )
955 return( status );
956 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100957}
958
Gilles Peskineb870b182018-07-06 16:02:09 +0200959/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200960static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200961{
962 if( key_type_is_raw_bytes( slot->type ) )
963 return( slot->data.raw.bytes * 8 );
964#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200965 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100966 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200967#endif /* defined(MBEDTLS_RSA_C) */
968#if defined(MBEDTLS_ECP_C)
969 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
970 return( slot->data.ecp->grp.pbits );
971#endif /* defined(MBEDTLS_ECP_C) */
972 /* Shouldn't happen except on an empty slot. */
973 return( 0 );
974}
975
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200976void psa_reset_key_attributes( psa_key_attributes_t *attributes )
977{
Gilles Peskineb699f072019-04-26 16:06:02 +0200978 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200979 memset( attributes, 0, sizeof( *attributes ) );
980}
981
Gilles Peskineb699f072019-04-26 16:06:02 +0200982psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
983 psa_key_type_t type,
984 const uint8_t *data,
985 size_t data_length )
986{
987 uint8_t *copy = NULL;
988
989 if( data_length != 0 )
990 {
991 copy = mbedtls_calloc( 1, data_length );
992 if( copy == NULL )
993 return( PSA_ERROR_INSUFFICIENT_MEMORY );
994 memcpy( copy, data, data_length );
995 }
996 /* After this point, this function is guaranteed to succeed, so it
997 * can start modifying `*attributes`. */
998
999 if( attributes->domain_parameters != NULL )
1000 {
1001 mbedtls_free( attributes->domain_parameters );
1002 attributes->domain_parameters = NULL;
1003 attributes->domain_parameters_size = 0;
1004 }
1005
1006 attributes->domain_parameters = copy;
1007 attributes->domain_parameters_size = data_length;
1008 attributes->type = type;
1009 return( PSA_SUCCESS );
1010}
1011
1012psa_status_t psa_get_key_domain_parameters(
1013 const psa_key_attributes_t *attributes,
1014 uint8_t *data, size_t data_size, size_t *data_length )
1015{
1016 if( attributes->domain_parameters_size > data_size )
1017 return( PSA_ERROR_BUFFER_TOO_SMALL );
1018 *data_length = attributes->domain_parameters_size;
1019 if( attributes->domain_parameters_size != 0 )
1020 memcpy( data, attributes->domain_parameters,
1021 attributes->domain_parameters_size );
1022 return( PSA_SUCCESS );
1023}
1024
1025#if defined(MBEDTLS_RSA_C)
1026static psa_status_t psa_get_rsa_public_exponent(
1027 const mbedtls_rsa_context *rsa,
1028 psa_key_attributes_t *attributes )
1029{
1030 mbedtls_mpi mpi;
1031 int ret;
1032 uint8_t *buffer = NULL;
1033 size_t buflen;
1034 mbedtls_mpi_init( &mpi );
1035
1036 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1037 if( ret != 0 )
1038 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001039 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1040 {
1041 /* It's the default value, which is reported as an empty string,
1042 * so there's nothing to do. */
1043 goto exit;
1044 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001045
1046 buflen = mbedtls_mpi_size( &mpi );
1047 buffer = mbedtls_calloc( 1, buflen );
1048 if( buffer == NULL )
1049 {
1050 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1051 goto exit;
1052 }
1053 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1054 if( ret != 0 )
1055 goto exit;
1056 attributes->domain_parameters = buffer;
1057 attributes->domain_parameters_size = buflen;
1058
1059exit:
1060 mbedtls_mpi_free( &mpi );
1061 if( ret != 0 )
1062 mbedtls_free( buffer );
1063 return( mbedtls_to_psa_error( ret ) );
1064}
1065#endif /* MBEDTLS_RSA_C */
1066
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001067psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1068 psa_key_attributes_t *attributes )
1069{
1070 psa_key_slot_t *slot;
1071 psa_status_t status;
1072
1073 psa_reset_key_attributes( attributes );
1074
1075 status = psa_get_key_slot( handle, &slot );
1076 if( status != PSA_SUCCESS )
1077 return( status );
1078
1079 attributes->id = slot->persistent_storage_id;
1080 attributes->lifetime = slot->lifetime;
1081 attributes->policy = slot->policy;
1082 attributes->type = slot->type;
1083 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001084
1085 switch( slot->type )
1086 {
1087#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001088 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001089 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1090 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1091 break;
1092#endif
1093 default:
1094 /* Nothing else to do. */
1095 break;
1096 }
1097
1098 if( status != PSA_SUCCESS )
1099 psa_reset_key_attributes( attributes );
1100 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001101}
1102
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001103#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001104static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1105 unsigned char *buf, size_t size )
1106{
1107 int ret;
1108 unsigned char *c;
1109 size_t len = 0;
1110
1111 c = buf + size;
1112
1113 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1114
1115 return( (int) len );
1116}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001117#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001118
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001119static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1120 uint8_t *data,
1121 size_t data_size,
1122 size_t *data_length,
1123 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001124{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001125 *data_length = 0;
1126
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001127 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001128 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001129
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001130 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001131 {
1132 if( slot->data.raw.bytes > data_size )
1133 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001134 if( data_size != 0 )
1135 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001136 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001137 memset( data + slot->data.raw.bytes, 0,
1138 data_size - slot->data.raw.bytes );
1139 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001140 *data_length = slot->data.raw.bytes;
1141 return( PSA_SUCCESS );
1142 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001143#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001144 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001145 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001146 psa_status_t status;
1147
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001148 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001149 if( bytes > data_size )
1150 return( PSA_ERROR_BUFFER_TOO_SMALL );
1151 status = mbedtls_to_psa_error(
1152 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1153 if( status != PSA_SUCCESS )
1154 return( status );
1155 memset( data + bytes, 0, data_size - bytes );
1156 *data_length = bytes;
1157 return( PSA_SUCCESS );
1158 }
1159#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001160 else
Moran Peker17e36e12018-05-02 12:55:20 +03001161 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001162#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001163 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001164 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001165 {
Moran Pekera998bc62018-04-16 18:16:20 +03001166 mbedtls_pk_context pk;
1167 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001168 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001169 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001170#if defined(MBEDTLS_RSA_C)
1171 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001172 pk.pk_info = &mbedtls_rsa_info;
1173 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001174#else
1175 return( PSA_ERROR_NOT_SUPPORTED );
1176#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001177 }
1178 else
1179 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001180#if defined(MBEDTLS_ECP_C)
1181 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001182 pk.pk_info = &mbedtls_eckey_info;
1183 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001184#else
1185 return( PSA_ERROR_NOT_SUPPORTED );
1186#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001187 }
Moran Pekerd7326592018-05-29 16:56:39 +03001188 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001189 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001190 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001191 }
Moran Peker17e36e12018-05-02 12:55:20 +03001192 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001193 {
Moran Peker17e36e12018-05-02 12:55:20 +03001194 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001195 }
Moran Peker60364322018-04-29 11:34:58 +03001196 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001197 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001198 /* If data_size is 0 then data may be NULL and then the
1199 * call to memset would have undefined behavior. */
1200 if( data_size != 0 )
1201 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001202 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001203 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001204 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1205 * Move the data to the beginning and erase remaining data
1206 * at the original location. */
1207 if( 2 * (size_t) ret <= data_size )
1208 {
1209 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001210 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001211 }
1212 else if( (size_t) ret < data_size )
1213 {
1214 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001215 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001216 }
Moran Pekera998bc62018-04-16 18:16:20 +03001217 *data_length = ret;
1218 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001219 }
1220 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001221#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001222 {
1223 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001224 it is valid for a special-purpose implementation to omit
1225 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001226 return( PSA_ERROR_NOT_SUPPORTED );
1227 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001228 }
1229}
1230
Gilles Peskinec5487a82018-12-03 18:08:14 +01001231psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001232 uint8_t *data,
1233 size_t data_size,
1234 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001235{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001236 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001237 psa_status_t status;
1238
1239 /* Set the key to empty now, so that even when there are errors, we always
1240 * set data_length to a value between 0 and data_size. On error, setting
1241 * the key to empty is a good choice because an empty key representation is
1242 * unlikely to be accepted anywhere. */
1243 *data_length = 0;
1244
1245 /* Export requires the EXPORT flag. There is an exception for public keys,
1246 * which don't require any flag, but psa_get_key_from_slot takes
1247 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001248 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001249 if( status != PSA_SUCCESS )
1250 return( status );
1251 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001252 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001253}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001254
Gilles Peskinec5487a82018-12-03 18:08:14 +01001255psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001256 uint8_t *data,
1257 size_t data_size,
1258 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001259{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001260 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001261 psa_status_t status;
1262
1263 /* Set the key to empty now, so that even when there are errors, we always
1264 * set data_length to a value between 0 and data_size. On error, setting
1265 * the key to empty is a good choice because an empty key representation is
1266 * unlikely to be accepted anywhere. */
1267 *data_length = 0;
1268
1269 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001270 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001271 if( status != PSA_SUCCESS )
1272 return( status );
1273 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001274 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001275}
1276
Gilles Peskine4747d192019-04-17 15:05:45 +02001277static psa_status_t psa_set_key_policy_internal(
1278 psa_key_slot_t *slot,
1279 const psa_key_policy_t *policy )
1280{
1281 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001282 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001283 PSA_KEY_USAGE_ENCRYPT |
1284 PSA_KEY_USAGE_DECRYPT |
1285 PSA_KEY_USAGE_SIGN |
1286 PSA_KEY_USAGE_VERIFY |
1287 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1288 return( PSA_ERROR_INVALID_ARGUMENT );
1289
1290 slot->policy = *policy;
1291 return( PSA_SUCCESS );
1292}
1293
1294/** Prepare a key slot to receive key material.
1295 *
1296 * This function allocates a key slot and sets its metadata.
1297 *
1298 * If this function fails, call psa_fail_key_creation().
1299 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001300 * This function is intended to be used as follows:
1301 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1302 * it with the specified attributes, and assign it a handle.
1303 * -# Populate the slot with the key material.
1304 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1305 * In case of failure at any step, stop the sequence and call
1306 * psa_fail_key_creation().
1307 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001308 * \param[in] attributes Key attributes for the new key.
1309 * \param[out] handle On success, a handle for the allocated slot.
1310 * \param[out] p_slot On success, a pointer to the prepared slot.
1311 * \param[out] p_drv On any return, the driver for the key, if any.
1312 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001313 *
1314 * \retval #PSA_SUCCESS
1315 * The key slot is ready to receive key material.
1316 * \return If this function fails, the key slot is an invalid state.
1317 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001318 */
1319static psa_status_t psa_start_key_creation(
1320 const psa_key_attributes_t *attributes,
1321 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001322 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001323 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001324{
1325 psa_status_t status;
1326 psa_key_slot_t *slot;
1327
Gilles Peskine011e4282019-06-26 18:34:38 +02001328 *p_drv = NULL;
1329
Gilles Peskine267c6562019-05-27 19:01:54 +02001330 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001331 if( status != PSA_SUCCESS )
1332 return( status );
1333 slot = *p_slot;
1334
1335 status = psa_set_key_policy_internal( slot, &attributes->policy );
1336 if( status != PSA_SUCCESS )
1337 return( status );
1338 slot->lifetime = attributes->lifetime;
Gilles Peskine011e4282019-06-26 18:34:38 +02001339
Gilles Peskine4747d192019-04-17 15:05:45 +02001340 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001341 {
1342 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskine011e4282019-06-26 18:34:38 +02001343 attributes->id,
1344 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001345 if( status != PSA_SUCCESS )
1346 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001347 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001348 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001349 slot->type = attributes->type;
1350
Gilles Peskinecbaff462019-07-12 23:46:04 +02001351#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1352 /* Find a slot number. Don't yet mark it as allocated in case
1353 * the key creation fails or there is a power failure. */
1354 if( *p_drv != NULL )
1355 {
1356 status = psa_find_se_slot_for_key( attributes, *p_drv,
1357 &slot->data.se.slot_number );
1358 if( status != PSA_SUCCESS )
1359 return( status );
1360 }
1361#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1362
Gilles Peskine4747d192019-04-17 15:05:45 +02001363 return( status );
1364}
1365
1366/** Finalize the creation of a key once its key material has been set.
1367 *
1368 * This entails writing the key to persistent storage.
1369 *
1370 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001371 * See the documentation of psa_start_key_creation() for the intended use
1372 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001373 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001374 * \param[in,out] slot Pointer to the slot with key material.
1375 * \param[in] driver The secure element driver for the key,
1376 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001377 *
1378 * \retval #PSA_SUCCESS
1379 * The key was successfully created. The handle is now valid.
1380 * \return If this function fails, the key slot is an invalid state.
1381 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001382 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001383static psa_status_t psa_finish_key_creation(
1384 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001385 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001386{
1387 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001388 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001389 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001390
1391#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1392 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1393 {
1394 uint8_t *buffer = NULL;
1395 size_t buffer_size = 0;
1396 size_t length;
1397
1398 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001399 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001400 buffer = mbedtls_calloc( 1, buffer_size );
1401 if( buffer == NULL && buffer_size != 0 )
1402 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1403 status = psa_internal_export_key( slot,
1404 buffer, buffer_size, &length,
1405 0 );
1406
1407 if( status == PSA_SUCCESS )
1408 {
1409 status = psa_save_persistent_key( slot->persistent_storage_id,
1410 slot->type, &slot->policy,
1411 buffer, length );
1412 }
1413
1414 if( buffer_size != 0 )
1415 mbedtls_platform_zeroize( buffer, buffer_size );
1416 mbedtls_free( buffer );
1417 }
1418#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1419
Gilles Peskinecbaff462019-07-12 23:46:04 +02001420#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1421 if( driver != NULL )
1422 {
1423 status = psa_save_se_persistent_data( driver );
1424 if( status != PSA_SUCCESS )
1425 {
1426 psa_destroy_persistent_key( slot->persistent_storage_id );
1427 return( status );
1428 }
1429 }
1430#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1431
Gilles Peskine4747d192019-04-17 15:05:45 +02001432 return( status );
1433}
1434
1435/** Abort the creation of a key.
1436 *
1437 * You may call this function after calling psa_start_key_creation(),
1438 * or after psa_finish_key_creation() fails. In other circumstances, this
1439 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001440 * See the documentation of psa_start_key_creation() for the intended use
1441 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001442 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001443 * \param[in,out] slot Pointer to the slot with key material.
1444 * \param[in] driver The secure element driver for the key,
1445 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001446 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001447static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001448 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001449{
Gilles Peskine011e4282019-06-26 18:34:38 +02001450 (void) driver;
1451
Gilles Peskine4747d192019-04-17 15:05:45 +02001452 if( slot == NULL )
1453 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001454
1455#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1456 /* TOnogrepDO: If the key has already been created in the secure
1457 * element, and the failure happened later (when saving metadata
1458 * to internal storage), we need to destroy the key in the secure
1459 * element. */
1460#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1461
Gilles Peskine4747d192019-04-17 15:05:45 +02001462 psa_wipe_key_slot( slot );
1463}
1464
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001465static psa_status_t psa_check_key_slot_attributes(
1466 const psa_key_slot_t *slot,
1467 const psa_key_attributes_t *attributes )
1468{
1469 if( attributes->type != 0 )
1470 {
1471 if( attributes->type != slot->type )
1472 return( PSA_ERROR_INVALID_ARGUMENT );
1473 }
1474
1475 if( attributes->domain_parameters_size != 0 )
1476 {
1477#if defined(MBEDTLS_RSA_C)
1478 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1479 {
1480 mbedtls_mpi actual, required;
1481 int ret;
1482 mbedtls_mpi_init( &actual );
1483 mbedtls_mpi_init( &required );
1484 ret = mbedtls_rsa_export( slot->data.rsa,
1485 NULL, NULL, NULL, NULL, &actual );
1486 if( ret != 0 )
1487 goto rsa_exit;
1488 ret = mbedtls_mpi_read_binary( &required,
1489 attributes->domain_parameters,
1490 attributes->domain_parameters_size );
1491 if( ret != 0 )
1492 goto rsa_exit;
1493 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1494 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1495 rsa_exit:
1496 mbedtls_mpi_free( &actual );
1497 mbedtls_mpi_free( &required );
1498 if( ret != 0)
1499 return( mbedtls_to_psa_error( ret ) );
1500 }
1501 else
1502#endif
1503 {
1504 return( PSA_ERROR_INVALID_ARGUMENT );
1505 }
1506 }
1507
1508 if( attributes->bits != 0 )
1509 {
1510 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1511 return( PSA_ERROR_INVALID_ARGUMENT );
1512 }
1513
1514 return( PSA_SUCCESS );
1515}
1516
Gilles Peskine4747d192019-04-17 15:05:45 +02001517psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001518 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001519 size_t data_length,
1520 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001521{
1522 psa_status_t status;
1523 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001524 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001525
Gilles Peskine011e4282019-06-26 18:34:38 +02001526 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001527 if( status != PSA_SUCCESS )
1528 goto exit;
1529
1530 status = psa_import_key_into_slot( slot, data, data_length );
1531 if( status != PSA_SUCCESS )
1532 goto exit;
1533 status = psa_check_key_slot_attributes( slot, attributes );
1534 if( status != PSA_SUCCESS )
1535 goto exit;
1536
Gilles Peskine011e4282019-06-26 18:34:38 +02001537 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001538exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001539 if( status != PSA_SUCCESS )
1540 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001541 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001542 *handle = 0;
1543 }
1544 return( status );
1545}
1546
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001547static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001548 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001549{
1550 psa_status_t status;
1551 uint8_t *buffer = NULL;
1552 size_t buffer_size = 0;
1553 size_t length;
1554
1555 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001556 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001557 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001558 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001559 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001560 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1561 if( status != PSA_SUCCESS )
1562 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001563 target->type = source->type;
1564 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001565
1566exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001567 if( buffer_size != 0 )
1568 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001569 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001570 return( status );
1571}
1572
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001573psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1574 const psa_key_attributes_t *specified_attributes,
1575 psa_key_handle_t *target_handle )
1576{
1577 psa_status_t status;
1578 psa_key_slot_t *source_slot = NULL;
1579 psa_key_slot_t *target_slot = NULL;
1580 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001581 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001582
Gilles Peskinec160d9e2019-05-14 14:32:03 +02001583 status = psa_get_key_from_slot( source_handle, &source_slot,
1584 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001585 if( status != PSA_SUCCESS )
1586 goto exit;
1587
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001588 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1589 if( status != PSA_SUCCESS )
1590 goto exit;
1591
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001592 status = psa_restrict_key_policy( &actual_attributes.policy,
1593 &source_slot->policy );
1594 if( status != PSA_SUCCESS )
1595 goto exit;
1596
1597 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001598 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001599 if( status != PSA_SUCCESS )
1600 goto exit;
1601
1602 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001603 if( status != PSA_SUCCESS )
1604 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001605
Gilles Peskine011e4282019-06-26 18:34:38 +02001606 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001607exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001608 if( status != PSA_SUCCESS )
1609 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001610 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001611 *target_handle = 0;
1612 }
1613 return( status );
1614}
1615
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001616
1617
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001618/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001619/* Message digests */
1620/****************************************************************/
1621
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001622static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001623{
1624 switch( alg )
1625 {
1626#if defined(MBEDTLS_MD2_C)
1627 case PSA_ALG_MD2:
1628 return( &mbedtls_md2_info );
1629#endif
1630#if defined(MBEDTLS_MD4_C)
1631 case PSA_ALG_MD4:
1632 return( &mbedtls_md4_info );
1633#endif
1634#if defined(MBEDTLS_MD5_C)
1635 case PSA_ALG_MD5:
1636 return( &mbedtls_md5_info );
1637#endif
1638#if defined(MBEDTLS_RIPEMD160_C)
1639 case PSA_ALG_RIPEMD160:
1640 return( &mbedtls_ripemd160_info );
1641#endif
1642#if defined(MBEDTLS_SHA1_C)
1643 case PSA_ALG_SHA_1:
1644 return( &mbedtls_sha1_info );
1645#endif
1646#if defined(MBEDTLS_SHA256_C)
1647 case PSA_ALG_SHA_224:
1648 return( &mbedtls_sha224_info );
1649 case PSA_ALG_SHA_256:
1650 return( &mbedtls_sha256_info );
1651#endif
1652#if defined(MBEDTLS_SHA512_C)
1653 case PSA_ALG_SHA_384:
1654 return( &mbedtls_sha384_info );
1655 case PSA_ALG_SHA_512:
1656 return( &mbedtls_sha512_info );
1657#endif
1658 default:
1659 return( NULL );
1660 }
1661}
1662
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001663psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1664{
1665 switch( operation->alg )
1666 {
Gilles Peskine81736312018-06-26 15:04:31 +02001667 case 0:
1668 /* The object has (apparently) been initialized but it is not
1669 * in use. It's ok to call abort on such an object, and there's
1670 * nothing to do. */
1671 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001672#if defined(MBEDTLS_MD2_C)
1673 case PSA_ALG_MD2:
1674 mbedtls_md2_free( &operation->ctx.md2 );
1675 break;
1676#endif
1677#if defined(MBEDTLS_MD4_C)
1678 case PSA_ALG_MD4:
1679 mbedtls_md4_free( &operation->ctx.md4 );
1680 break;
1681#endif
1682#if defined(MBEDTLS_MD5_C)
1683 case PSA_ALG_MD5:
1684 mbedtls_md5_free( &operation->ctx.md5 );
1685 break;
1686#endif
1687#if defined(MBEDTLS_RIPEMD160_C)
1688 case PSA_ALG_RIPEMD160:
1689 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1690 break;
1691#endif
1692#if defined(MBEDTLS_SHA1_C)
1693 case PSA_ALG_SHA_1:
1694 mbedtls_sha1_free( &operation->ctx.sha1 );
1695 break;
1696#endif
1697#if defined(MBEDTLS_SHA256_C)
1698 case PSA_ALG_SHA_224:
1699 case PSA_ALG_SHA_256:
1700 mbedtls_sha256_free( &operation->ctx.sha256 );
1701 break;
1702#endif
1703#if defined(MBEDTLS_SHA512_C)
1704 case PSA_ALG_SHA_384:
1705 case PSA_ALG_SHA_512:
1706 mbedtls_sha512_free( &operation->ctx.sha512 );
1707 break;
1708#endif
1709 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001710 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001711 }
1712 operation->alg = 0;
1713 return( PSA_SUCCESS );
1714}
1715
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001716psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001717 psa_algorithm_t alg )
1718{
1719 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001720
1721 /* A context must be freshly initialized before it can be set up. */
1722 if( operation->alg != 0 )
1723 {
1724 return( PSA_ERROR_BAD_STATE );
1725 }
1726
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001727 switch( alg )
1728 {
1729#if defined(MBEDTLS_MD2_C)
1730 case PSA_ALG_MD2:
1731 mbedtls_md2_init( &operation->ctx.md2 );
1732 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1733 break;
1734#endif
1735#if defined(MBEDTLS_MD4_C)
1736 case PSA_ALG_MD4:
1737 mbedtls_md4_init( &operation->ctx.md4 );
1738 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1739 break;
1740#endif
1741#if defined(MBEDTLS_MD5_C)
1742 case PSA_ALG_MD5:
1743 mbedtls_md5_init( &operation->ctx.md5 );
1744 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1745 break;
1746#endif
1747#if defined(MBEDTLS_RIPEMD160_C)
1748 case PSA_ALG_RIPEMD160:
1749 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1750 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1751 break;
1752#endif
1753#if defined(MBEDTLS_SHA1_C)
1754 case PSA_ALG_SHA_1:
1755 mbedtls_sha1_init( &operation->ctx.sha1 );
1756 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1757 break;
1758#endif
1759#if defined(MBEDTLS_SHA256_C)
1760 case PSA_ALG_SHA_224:
1761 mbedtls_sha256_init( &operation->ctx.sha256 );
1762 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1763 break;
1764 case PSA_ALG_SHA_256:
1765 mbedtls_sha256_init( &operation->ctx.sha256 );
1766 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1767 break;
1768#endif
1769#if defined(MBEDTLS_SHA512_C)
1770 case PSA_ALG_SHA_384:
1771 mbedtls_sha512_init( &operation->ctx.sha512 );
1772 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1773 break;
1774 case PSA_ALG_SHA_512:
1775 mbedtls_sha512_init( &operation->ctx.sha512 );
1776 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1777 break;
1778#endif
1779 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001780 return( PSA_ALG_IS_HASH( alg ) ?
1781 PSA_ERROR_NOT_SUPPORTED :
1782 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001783 }
1784 if( ret == 0 )
1785 operation->alg = alg;
1786 else
1787 psa_hash_abort( operation );
1788 return( mbedtls_to_psa_error( ret ) );
1789}
1790
1791psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1792 const uint8_t *input,
1793 size_t input_length )
1794{
1795 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001796
1797 /* Don't require hash implementations to behave correctly on a
1798 * zero-length input, which may have an invalid pointer. */
1799 if( input_length == 0 )
1800 return( PSA_SUCCESS );
1801
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001802 switch( operation->alg )
1803 {
1804#if defined(MBEDTLS_MD2_C)
1805 case PSA_ALG_MD2:
1806 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1807 input, input_length );
1808 break;
1809#endif
1810#if defined(MBEDTLS_MD4_C)
1811 case PSA_ALG_MD4:
1812 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1813 input, input_length );
1814 break;
1815#endif
1816#if defined(MBEDTLS_MD5_C)
1817 case PSA_ALG_MD5:
1818 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1819 input, input_length );
1820 break;
1821#endif
1822#if defined(MBEDTLS_RIPEMD160_C)
1823 case PSA_ALG_RIPEMD160:
1824 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1825 input, input_length );
1826 break;
1827#endif
1828#if defined(MBEDTLS_SHA1_C)
1829 case PSA_ALG_SHA_1:
1830 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1831 input, input_length );
1832 break;
1833#endif
1834#if defined(MBEDTLS_SHA256_C)
1835 case PSA_ALG_SHA_224:
1836 case PSA_ALG_SHA_256:
1837 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1838 input, input_length );
1839 break;
1840#endif
1841#if defined(MBEDTLS_SHA512_C)
1842 case PSA_ALG_SHA_384:
1843 case PSA_ALG_SHA_512:
1844 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1845 input, input_length );
1846 break;
1847#endif
1848 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001849 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001850 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001851
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001852 if( ret != 0 )
1853 psa_hash_abort( operation );
1854 return( mbedtls_to_psa_error( ret ) );
1855}
1856
1857psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1858 uint8_t *hash,
1859 size_t hash_size,
1860 size_t *hash_length )
1861{
itayzafrir40835d42018-08-02 13:14:17 +03001862 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001863 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001864 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001865
1866 /* Fill the output buffer with something that isn't a valid hash
1867 * (barring an attack on the hash and deliberately-crafted input),
1868 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001869 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001870 /* If hash_size is 0 then hash may be NULL and then the
1871 * call to memset would have undefined behavior. */
1872 if( hash_size != 0 )
1873 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001874
1875 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001876 {
1877 status = PSA_ERROR_BUFFER_TOO_SMALL;
1878 goto exit;
1879 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001880
1881 switch( operation->alg )
1882 {
1883#if defined(MBEDTLS_MD2_C)
1884 case PSA_ALG_MD2:
1885 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1886 break;
1887#endif
1888#if defined(MBEDTLS_MD4_C)
1889 case PSA_ALG_MD4:
1890 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1891 break;
1892#endif
1893#if defined(MBEDTLS_MD5_C)
1894 case PSA_ALG_MD5:
1895 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1896 break;
1897#endif
1898#if defined(MBEDTLS_RIPEMD160_C)
1899 case PSA_ALG_RIPEMD160:
1900 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1901 break;
1902#endif
1903#if defined(MBEDTLS_SHA1_C)
1904 case PSA_ALG_SHA_1:
1905 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1906 break;
1907#endif
1908#if defined(MBEDTLS_SHA256_C)
1909 case PSA_ALG_SHA_224:
1910 case PSA_ALG_SHA_256:
1911 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1912 break;
1913#endif
1914#if defined(MBEDTLS_SHA512_C)
1915 case PSA_ALG_SHA_384:
1916 case PSA_ALG_SHA_512:
1917 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1918 break;
1919#endif
1920 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001921 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001922 }
itayzafrir40835d42018-08-02 13:14:17 +03001923 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001924
itayzafrir40835d42018-08-02 13:14:17 +03001925exit:
1926 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001927 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001928 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001929 return( psa_hash_abort( operation ) );
1930 }
1931 else
1932 {
1933 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001934 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001935 }
1936}
1937
Gilles Peskine2d277862018-06-18 15:41:12 +02001938psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1939 const uint8_t *hash,
1940 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001941{
1942 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1943 size_t actual_hash_length;
1944 psa_status_t status = psa_hash_finish( operation,
1945 actual_hash, sizeof( actual_hash ),
1946 &actual_hash_length );
1947 if( status != PSA_SUCCESS )
1948 return( status );
1949 if( actual_hash_length != hash_length )
1950 return( PSA_ERROR_INVALID_SIGNATURE );
1951 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1952 return( PSA_ERROR_INVALID_SIGNATURE );
1953 return( PSA_SUCCESS );
1954}
1955
Gilles Peskineeb35d782019-01-22 17:56:16 +01001956psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1957 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001958{
1959 if( target_operation->alg != 0 )
1960 return( PSA_ERROR_BAD_STATE );
1961
1962 switch( source_operation->alg )
1963 {
1964 case 0:
1965 return( PSA_ERROR_BAD_STATE );
1966#if defined(MBEDTLS_MD2_C)
1967 case PSA_ALG_MD2:
1968 mbedtls_md2_clone( &target_operation->ctx.md2,
1969 &source_operation->ctx.md2 );
1970 break;
1971#endif
1972#if defined(MBEDTLS_MD4_C)
1973 case PSA_ALG_MD4:
1974 mbedtls_md4_clone( &target_operation->ctx.md4,
1975 &source_operation->ctx.md4 );
1976 break;
1977#endif
1978#if defined(MBEDTLS_MD5_C)
1979 case PSA_ALG_MD5:
1980 mbedtls_md5_clone( &target_operation->ctx.md5,
1981 &source_operation->ctx.md5 );
1982 break;
1983#endif
1984#if defined(MBEDTLS_RIPEMD160_C)
1985 case PSA_ALG_RIPEMD160:
1986 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1987 &source_operation->ctx.ripemd160 );
1988 break;
1989#endif
1990#if defined(MBEDTLS_SHA1_C)
1991 case PSA_ALG_SHA_1:
1992 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1993 &source_operation->ctx.sha1 );
1994 break;
1995#endif
1996#if defined(MBEDTLS_SHA256_C)
1997 case PSA_ALG_SHA_224:
1998 case PSA_ALG_SHA_256:
1999 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2000 &source_operation->ctx.sha256 );
2001 break;
2002#endif
2003#if defined(MBEDTLS_SHA512_C)
2004 case PSA_ALG_SHA_384:
2005 case PSA_ALG_SHA_512:
2006 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2007 &source_operation->ctx.sha512 );
2008 break;
2009#endif
2010 default:
2011 return( PSA_ERROR_NOT_SUPPORTED );
2012 }
2013
2014 target_operation->alg = source_operation->alg;
2015 return( PSA_SUCCESS );
2016}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002017
2018
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002019/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002020/* MAC */
2021/****************************************************************/
2022
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002023static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002024 psa_algorithm_t alg,
2025 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002026 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002027 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002028{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002029 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002030 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002032 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002033 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002034
Gilles Peskine8c9def32018-02-08 10:02:12 +01002035 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2036 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002037 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002038 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002039 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002040 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002041 mode = MBEDTLS_MODE_STREAM;
2042 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002043 case PSA_ALG_CTR:
2044 mode = MBEDTLS_MODE_CTR;
2045 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002046 case PSA_ALG_CFB:
2047 mode = MBEDTLS_MODE_CFB;
2048 break;
2049 case PSA_ALG_OFB:
2050 mode = MBEDTLS_MODE_OFB;
2051 break;
2052 case PSA_ALG_CBC_NO_PADDING:
2053 mode = MBEDTLS_MODE_CBC;
2054 break;
2055 case PSA_ALG_CBC_PKCS7:
2056 mode = MBEDTLS_MODE_CBC;
2057 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002058 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002059 mode = MBEDTLS_MODE_CCM;
2060 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002061 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002062 mode = MBEDTLS_MODE_GCM;
2063 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002064 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2065 mode = MBEDTLS_MODE_CHACHAPOLY;
2066 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002067 default:
2068 return( NULL );
2069 }
2070 }
2071 else if( alg == PSA_ALG_CMAC )
2072 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002073 else
2074 return( NULL );
2075
2076 switch( key_type )
2077 {
2078 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002079 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002080 break;
2081 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002082 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2083 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002084 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002085 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002086 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002087 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002088 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2089 * but two-key Triple-DES is functionally three-key Triple-DES
2090 * with K1=K3, so that's how we present it to mbedtls. */
2091 if( key_bits == 128 )
2092 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002093 break;
2094 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002095 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002096 break;
2097 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002098 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002099 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002100 case PSA_KEY_TYPE_CHACHA20:
2101 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2102 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002103 default:
2104 return( NULL );
2105 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002106 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002107 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002108
Jaeden Amero23bbb752018-06-26 14:16:54 +01002109 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2110 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002111}
2112
Gilles Peskinea05219c2018-11-16 16:02:56 +01002113#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002114static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002115{
Gilles Peskine2d277862018-06-18 15:41:12 +02002116 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002117 {
2118 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002119 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002120 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002121 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002122 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002123 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002124 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002125 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002126 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002127 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002128 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002129 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002130 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002131 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002132 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002133 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002134 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002135 return( 128 );
2136 default:
2137 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002138 }
2139}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002140#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002141
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002142/* Initialize the MAC operation structure. Once this function has been
2143 * called, psa_mac_abort can run and will do the right thing. */
2144static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2145 psa_algorithm_t alg )
2146{
2147 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2148
2149 operation->alg = alg;
2150 operation->key_set = 0;
2151 operation->iv_set = 0;
2152 operation->iv_required = 0;
2153 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002154 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002155
2156#if defined(MBEDTLS_CMAC_C)
2157 if( alg == PSA_ALG_CMAC )
2158 {
2159 operation->iv_required = 0;
2160 mbedtls_cipher_init( &operation->ctx.cmac );
2161 status = PSA_SUCCESS;
2162 }
2163 else
2164#endif /* MBEDTLS_CMAC_C */
2165#if defined(MBEDTLS_MD_C)
2166 if( PSA_ALG_IS_HMAC( operation->alg ) )
2167 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002168 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2169 operation->ctx.hmac.hash_ctx.alg = 0;
2170 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002171 }
2172 else
2173#endif /* MBEDTLS_MD_C */
2174 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002175 if( ! PSA_ALG_IS_MAC( alg ) )
2176 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002177 }
2178
2179 if( status != PSA_SUCCESS )
2180 memset( operation, 0, sizeof( *operation ) );
2181 return( status );
2182}
2183
Gilles Peskine01126fa2018-07-12 17:04:55 +02002184#if defined(MBEDTLS_MD_C)
2185static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2186{
Gilles Peskine3f108122018-12-07 18:14:53 +01002187 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002188 return( psa_hash_abort( &hmac->hash_ctx ) );
2189}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002190
Janos Follath999f6482019-06-11 12:04:10 +01002191#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002192static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2193{
2194 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2195 memset( hmac, 0, sizeof( *hmac ) );
2196}
Janos Follath999f6482019-06-11 12:04:10 +01002197#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002198#endif /* MBEDTLS_MD_C */
2199
Gilles Peskine8c9def32018-02-08 10:02:12 +01002200psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2201{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002202 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002203 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002204 /* The object has (apparently) been initialized but it is not
2205 * in use. It's ok to call abort on such an object, and there's
2206 * nothing to do. */
2207 return( PSA_SUCCESS );
2208 }
2209 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002210#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002211 if( operation->alg == PSA_ALG_CMAC )
2212 {
2213 mbedtls_cipher_free( &operation->ctx.cmac );
2214 }
2215 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002216#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002217#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002218 if( PSA_ALG_IS_HMAC( operation->alg ) )
2219 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002220 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002221 }
2222 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002223#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002224 {
2225 /* Sanity check (shouldn't happen: operation->alg should
2226 * always have been initialized to a valid value). */
2227 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002228 }
Moran Peker41deec42018-04-04 15:43:05 +03002229
Gilles Peskine8c9def32018-02-08 10:02:12 +01002230 operation->alg = 0;
2231 operation->key_set = 0;
2232 operation->iv_set = 0;
2233 operation->iv_required = 0;
2234 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002235 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002236
Gilles Peskine8c9def32018-02-08 10:02:12 +01002237 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002238
2239bad_state:
2240 /* If abort is called on an uninitialized object, we can't trust
2241 * anything. Wipe the object in case it contains confidential data.
2242 * This may result in a memory leak if a pointer gets overwritten,
2243 * but it's too late to do anything about this. */
2244 memset( operation, 0, sizeof( *operation ) );
2245 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002246}
2247
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002248#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002249static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002250 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002251 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002252 const mbedtls_cipher_info_t *cipher_info )
2253{
2254 int ret;
2255
2256 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002257
2258 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2259 if( ret != 0 )
2260 return( ret );
2261
2262 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2263 slot->data.raw.data,
2264 key_bits );
2265 return( ret );
2266}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002267#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002268
Gilles Peskine248051a2018-06-20 16:09:38 +02002269#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002270static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2271 const uint8_t *key,
2272 size_t key_length,
2273 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002274{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002275 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002276 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002277 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002278 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002279 psa_status_t status;
2280
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002281 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2282 * overflow below. This should never trigger if the hash algorithm
2283 * is implemented correctly. */
2284 /* The size checks against the ipad and opad buffers cannot be written
2285 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2286 * because that triggers -Wlogical-op on GCC 7.3. */
2287 if( block_size > sizeof( ipad ) )
2288 return( PSA_ERROR_NOT_SUPPORTED );
2289 if( block_size > sizeof( hmac->opad ) )
2290 return( PSA_ERROR_NOT_SUPPORTED );
2291 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002292 return( PSA_ERROR_NOT_SUPPORTED );
2293
Gilles Peskined223b522018-06-11 18:12:58 +02002294 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002295 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002296 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002297 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002298 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002299 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002300 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002301 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002302 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002303 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002304 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002305 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002306 }
Gilles Peskine96889972018-07-12 17:07:03 +02002307 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2308 * but it is permitted. It is common when HMAC is used in HKDF, for
2309 * example. Don't call `memcpy` in the 0-length because `key` could be
2310 * an invalid pointer which would make the behavior undefined. */
2311 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002312 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002313
Gilles Peskined223b522018-06-11 18:12:58 +02002314 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2315 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002316 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002317 ipad[i] ^= 0x36;
2318 memset( ipad + key_length, 0x36, block_size - key_length );
2319
2320 /* Copy the key material from ipad to opad, flipping the requisite bits,
2321 * and filling the rest of opad with the requisite constant. */
2322 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002323 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2324 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002325
Gilles Peskine01126fa2018-07-12 17:04:55 +02002326 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002327 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002328 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002329
Gilles Peskine01126fa2018-07-12 17:04:55 +02002330 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002331
2332cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002333 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002334
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002335 return( status );
2336}
Gilles Peskine248051a2018-06-20 16:09:38 +02002337#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002338
Gilles Peskine89167cb2018-07-08 20:12:23 +02002339static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002340 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002341 psa_algorithm_t alg,
2342 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002343{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002344 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002345 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002346 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002347 psa_key_usage_t usage =
2348 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002349 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002350 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002351
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002352 /* A context must be freshly initialized before it can be set up. */
2353 if( operation->alg != 0 )
2354 {
2355 return( PSA_ERROR_BAD_STATE );
2356 }
2357
Gilles Peskined911eb72018-08-14 15:18:45 +02002358 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002359 if( status != PSA_SUCCESS )
2360 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002361 if( is_sign )
2362 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002363
Gilles Peskinec5487a82018-12-03 18:08:14 +01002364 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002365 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002366 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002367 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002368
Gilles Peskine8c9def32018-02-08 10:02:12 +01002369#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002370 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002371 {
2372 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002373 mbedtls_cipher_info_from_psa( full_length_alg,
2374 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002375 int ret;
2376 if( cipher_info == NULL )
2377 {
2378 status = PSA_ERROR_NOT_SUPPORTED;
2379 goto exit;
2380 }
2381 operation->mac_size = cipher_info->block_size;
2382 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2383 status = mbedtls_to_psa_error( ret );
2384 }
2385 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002386#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002387#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002388 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002389 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002390 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002391 if( hash_alg == 0 )
2392 {
2393 status = PSA_ERROR_NOT_SUPPORTED;
2394 goto exit;
2395 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002396
2397 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2398 /* Sanity check. This shouldn't fail on a valid configuration. */
2399 if( operation->mac_size == 0 ||
2400 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2401 {
2402 status = PSA_ERROR_NOT_SUPPORTED;
2403 goto exit;
2404 }
2405
Gilles Peskine01126fa2018-07-12 17:04:55 +02002406 if( slot->type != PSA_KEY_TYPE_HMAC )
2407 {
2408 status = PSA_ERROR_INVALID_ARGUMENT;
2409 goto exit;
2410 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002411
Gilles Peskine01126fa2018-07-12 17:04:55 +02002412 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2413 slot->data.raw.data,
2414 slot->data.raw.bytes,
2415 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002416 }
2417 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002419 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002420 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002421 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002422 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002423
Gilles Peskined911eb72018-08-14 15:18:45 +02002424 if( truncated == 0 )
2425 {
2426 /* The "normal" case: untruncated algorithm. Nothing to do. */
2427 }
2428 else if( truncated < 4 )
2429 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002430 /* A very short MAC is too short for security since it can be
2431 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2432 * so we make this our minimum, even though 32 bits is still
2433 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002434 status = PSA_ERROR_NOT_SUPPORTED;
2435 }
2436 else if( truncated > operation->mac_size )
2437 {
2438 /* It's impossible to "truncate" to a larger length. */
2439 status = PSA_ERROR_INVALID_ARGUMENT;
2440 }
2441 else
2442 operation->mac_size = truncated;
2443
Gilles Peskinefbfac682018-07-08 20:51:54 +02002444exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002445 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002446 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002447 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002448 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002449 else
2450 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002451 operation->key_set = 1;
2452 }
2453 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002454}
2455
Gilles Peskine89167cb2018-07-08 20:12:23 +02002456psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002457 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002458 psa_algorithm_t alg )
2459{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002460 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002461}
2462
2463psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002464 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002465 psa_algorithm_t alg )
2466{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002467 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002468}
2469
Gilles Peskine8c9def32018-02-08 10:02:12 +01002470psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2471 const uint8_t *input,
2472 size_t input_length )
2473{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002474 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002475 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002476 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002477 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002478 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002479 operation->has_input = 1;
2480
Gilles Peskine8c9def32018-02-08 10:02:12 +01002481#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002482 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002483 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002484 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2485 input, input_length );
2486 status = mbedtls_to_psa_error( ret );
2487 }
2488 else
2489#endif /* MBEDTLS_CMAC_C */
2490#if defined(MBEDTLS_MD_C)
2491 if( PSA_ALG_IS_HMAC( operation->alg ) )
2492 {
2493 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2494 input_length );
2495 }
2496 else
2497#endif /* MBEDTLS_MD_C */
2498 {
2499 /* This shouldn't happen if `operation` was initialized by
2500 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002501 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002502 }
2503
Gilles Peskinefbfac682018-07-08 20:51:54 +02002504 if( status != PSA_SUCCESS )
2505 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002506 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002507}
2508
Gilles Peskine01126fa2018-07-12 17:04:55 +02002509#if defined(MBEDTLS_MD_C)
2510static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2511 uint8_t *mac,
2512 size_t mac_size )
2513{
2514 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2515 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2516 size_t hash_size = 0;
2517 size_t block_size = psa_get_hash_block_size( hash_alg );
2518 psa_status_t status;
2519
Gilles Peskine01126fa2018-07-12 17:04:55 +02002520 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2521 if( status != PSA_SUCCESS )
2522 return( status );
2523 /* From here on, tmp needs to be wiped. */
2524
2525 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2526 if( status != PSA_SUCCESS )
2527 goto exit;
2528
2529 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2530 if( status != PSA_SUCCESS )
2531 goto exit;
2532
2533 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2534 if( status != PSA_SUCCESS )
2535 goto exit;
2536
Gilles Peskined911eb72018-08-14 15:18:45 +02002537 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2538 if( status != PSA_SUCCESS )
2539 goto exit;
2540
2541 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002542
2543exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002544 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002545 return( status );
2546}
2547#endif /* MBEDTLS_MD_C */
2548
mohammad16036df908f2018-04-02 08:34:15 -07002549static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002550 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002551 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002553 if( ! operation->key_set )
2554 return( PSA_ERROR_BAD_STATE );
2555 if( operation->iv_required && ! operation->iv_set )
2556 return( PSA_ERROR_BAD_STATE );
2557
Gilles Peskine8c9def32018-02-08 10:02:12 +01002558 if( mac_size < operation->mac_size )
2559 return( PSA_ERROR_BUFFER_TOO_SMALL );
2560
Gilles Peskine8c9def32018-02-08 10:02:12 +01002561#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002562 if( operation->alg == PSA_ALG_CMAC )
2563 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002564 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2565 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2566 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002567 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002568 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002569 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002570 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002571 else
2572#endif /* MBEDTLS_CMAC_C */
2573#if defined(MBEDTLS_MD_C)
2574 if( PSA_ALG_IS_HMAC( operation->alg ) )
2575 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002576 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002577 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002578 }
2579 else
2580#endif /* MBEDTLS_MD_C */
2581 {
2582 /* This shouldn't happen if `operation` was initialized by
2583 * a setup function. */
2584 return( PSA_ERROR_BAD_STATE );
2585 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002586}
2587
Gilles Peskineacd4be32018-07-08 19:56:25 +02002588psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2589 uint8_t *mac,
2590 size_t mac_size,
2591 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002592{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002593 psa_status_t status;
2594
Jaeden Amero252ef282019-02-15 14:05:35 +00002595 if( operation->alg == 0 )
2596 {
2597 return( PSA_ERROR_BAD_STATE );
2598 }
2599
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002600 /* Fill the output buffer with something that isn't a valid mac
2601 * (barring an attack on the mac and deliberately-crafted input),
2602 * in case the caller doesn't check the return status properly. */
2603 *mac_length = mac_size;
2604 /* If mac_size is 0 then mac may be NULL and then the
2605 * call to memset would have undefined behavior. */
2606 if( mac_size != 0 )
2607 memset( mac, '!', mac_size );
2608
Gilles Peskine89167cb2018-07-08 20:12:23 +02002609 if( ! operation->is_sign )
2610 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002611 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002612 }
mohammad16036df908f2018-04-02 08:34:15 -07002613
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002614 status = psa_mac_finish_internal( operation, mac, mac_size );
2615
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002616 if( status == PSA_SUCCESS )
2617 {
2618 status = psa_mac_abort( operation );
2619 if( status == PSA_SUCCESS )
2620 *mac_length = operation->mac_size;
2621 else
2622 memset( mac, '!', mac_size );
2623 }
2624 else
2625 psa_mac_abort( operation );
2626 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002627}
2628
Gilles Peskineacd4be32018-07-08 19:56:25 +02002629psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2630 const uint8_t *mac,
2631 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002632{
Gilles Peskine828ed142018-06-18 23:25:51 +02002633 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002634 psa_status_t status;
2635
Jaeden Amero252ef282019-02-15 14:05:35 +00002636 if( operation->alg == 0 )
2637 {
2638 return( PSA_ERROR_BAD_STATE );
2639 }
2640
Gilles Peskine89167cb2018-07-08 20:12:23 +02002641 if( operation->is_sign )
2642 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002643 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002644 }
2645 if( operation->mac_size != mac_length )
2646 {
2647 status = PSA_ERROR_INVALID_SIGNATURE;
2648 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002649 }
mohammad16036df908f2018-04-02 08:34:15 -07002650
2651 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002652 actual_mac, sizeof( actual_mac ) );
2653
2654 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2655 status = PSA_ERROR_INVALID_SIGNATURE;
2656
2657cleanup:
2658 if( status == PSA_SUCCESS )
2659 status = psa_mac_abort( operation );
2660 else
2661 psa_mac_abort( operation );
2662
Gilles Peskine3f108122018-12-07 18:14:53 +01002663 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002664
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002665 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002666}
2667
2668
Gilles Peskine20035e32018-02-03 22:44:14 +01002669
Gilles Peskine20035e32018-02-03 22:44:14 +01002670/****************************************************************/
2671/* Asymmetric cryptography */
2672/****************************************************************/
2673
Gilles Peskine2b450e32018-06-27 15:42:46 +02002674#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002675/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002676 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002677static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2678 size_t hash_length,
2679 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002680{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002681 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002682 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002683 *md_alg = mbedtls_md_get_type( md_info );
2684
2685 /* The Mbed TLS RSA module uses an unsigned int for hash length
2686 * parameters. Validate that it fits so that we don't risk an
2687 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002688#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002689 if( hash_length > UINT_MAX )
2690 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002691#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002692
2693#if defined(MBEDTLS_PKCS1_V15)
2694 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2695 * must be correct. */
2696 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2697 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002698 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002699 if( md_info == NULL )
2700 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002701 if( mbedtls_md_get_size( md_info ) != hash_length )
2702 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002703 }
2704#endif /* MBEDTLS_PKCS1_V15 */
2705
2706#if defined(MBEDTLS_PKCS1_V21)
2707 /* PSS requires a hash internally. */
2708 if( PSA_ALG_IS_RSA_PSS( alg ) )
2709 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002710 if( md_info == NULL )
2711 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002712 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002713#endif /* MBEDTLS_PKCS1_V21 */
2714
Gilles Peskine61b91d42018-06-08 16:09:36 +02002715 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002716}
2717
Gilles Peskine2b450e32018-06-27 15:42:46 +02002718static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2719 psa_algorithm_t alg,
2720 const uint8_t *hash,
2721 size_t hash_length,
2722 uint8_t *signature,
2723 size_t signature_size,
2724 size_t *signature_length )
2725{
2726 psa_status_t status;
2727 int ret;
2728 mbedtls_md_type_t md_alg;
2729
2730 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2731 if( status != PSA_SUCCESS )
2732 return( status );
2733
Gilles Peskine630a18a2018-06-29 17:49:35 +02002734 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002735 return( PSA_ERROR_BUFFER_TOO_SMALL );
2736
2737#if defined(MBEDTLS_PKCS1_V15)
2738 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2739 {
2740 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2741 MBEDTLS_MD_NONE );
2742 ret = mbedtls_rsa_pkcs1_sign( rsa,
2743 mbedtls_ctr_drbg_random,
2744 &global_data.ctr_drbg,
2745 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002746 md_alg,
2747 (unsigned int) hash_length,
2748 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002749 signature );
2750 }
2751 else
2752#endif /* MBEDTLS_PKCS1_V15 */
2753#if defined(MBEDTLS_PKCS1_V21)
2754 if( PSA_ALG_IS_RSA_PSS( alg ) )
2755 {
2756 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2757 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2758 mbedtls_ctr_drbg_random,
2759 &global_data.ctr_drbg,
2760 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002761 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002762 (unsigned int) hash_length,
2763 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002764 signature );
2765 }
2766 else
2767#endif /* MBEDTLS_PKCS1_V21 */
2768 {
2769 return( PSA_ERROR_INVALID_ARGUMENT );
2770 }
2771
2772 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002773 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002774 return( mbedtls_to_psa_error( ret ) );
2775}
2776
2777static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2778 psa_algorithm_t alg,
2779 const uint8_t *hash,
2780 size_t hash_length,
2781 const uint8_t *signature,
2782 size_t signature_length )
2783{
2784 psa_status_t status;
2785 int ret;
2786 mbedtls_md_type_t md_alg;
2787
2788 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2789 if( status != PSA_SUCCESS )
2790 return( status );
2791
Gilles Peskine630a18a2018-06-29 17:49:35 +02002792 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002793 return( PSA_ERROR_BUFFER_TOO_SMALL );
2794
2795#if defined(MBEDTLS_PKCS1_V15)
2796 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2797 {
2798 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2799 MBEDTLS_MD_NONE );
2800 ret = mbedtls_rsa_pkcs1_verify( rsa,
2801 mbedtls_ctr_drbg_random,
2802 &global_data.ctr_drbg,
2803 MBEDTLS_RSA_PUBLIC,
2804 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002805 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002806 hash,
2807 signature );
2808 }
2809 else
2810#endif /* MBEDTLS_PKCS1_V15 */
2811#if defined(MBEDTLS_PKCS1_V21)
2812 if( PSA_ALG_IS_RSA_PSS( alg ) )
2813 {
2814 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2815 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2816 mbedtls_ctr_drbg_random,
2817 &global_data.ctr_drbg,
2818 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002819 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002820 (unsigned int) hash_length,
2821 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002822 signature );
2823 }
2824 else
2825#endif /* MBEDTLS_PKCS1_V21 */
2826 {
2827 return( PSA_ERROR_INVALID_ARGUMENT );
2828 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002829
2830 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2831 * the rest of the signature is invalid". This has little use in
2832 * practice and PSA doesn't report this distinction. */
2833 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2834 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002835 return( mbedtls_to_psa_error( ret ) );
2836}
2837#endif /* MBEDTLS_RSA_C */
2838
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002839#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002840/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2841 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2842 * (even though these functions don't modify it). */
2843static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2844 psa_algorithm_t alg,
2845 const uint8_t *hash,
2846 size_t hash_length,
2847 uint8_t *signature,
2848 size_t signature_size,
2849 size_t *signature_length )
2850{
2851 int ret;
2852 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002853 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002854 mbedtls_mpi_init( &r );
2855 mbedtls_mpi_init( &s );
2856
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002857 if( signature_size < 2 * curve_bytes )
2858 {
2859 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2860 goto cleanup;
2861 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002862
Gilles Peskinea05219c2018-11-16 16:02:56 +01002863#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002864 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2865 {
2866 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2867 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2868 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2869 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2870 hash, hash_length,
2871 md_alg ) );
2872 }
2873 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002874#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002875 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002876 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002877 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2878 hash, hash_length,
2879 mbedtls_ctr_drbg_random,
2880 &global_data.ctr_drbg ) );
2881 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002882
2883 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2884 signature,
2885 curve_bytes ) );
2886 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2887 signature + curve_bytes,
2888 curve_bytes ) );
2889
2890cleanup:
2891 mbedtls_mpi_free( &r );
2892 mbedtls_mpi_free( &s );
2893 if( ret == 0 )
2894 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002895 return( mbedtls_to_psa_error( ret ) );
2896}
2897
2898static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2899 const uint8_t *hash,
2900 size_t hash_length,
2901 const uint8_t *signature,
2902 size_t signature_length )
2903{
2904 int ret;
2905 mbedtls_mpi r, s;
2906 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2907 mbedtls_mpi_init( &r );
2908 mbedtls_mpi_init( &s );
2909
2910 if( signature_length != 2 * curve_bytes )
2911 return( PSA_ERROR_INVALID_SIGNATURE );
2912
2913 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2914 signature,
2915 curve_bytes ) );
2916 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2917 signature + curve_bytes,
2918 curve_bytes ) );
2919
2920 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2921 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002922
2923cleanup:
2924 mbedtls_mpi_free( &r );
2925 mbedtls_mpi_free( &s );
2926 return( mbedtls_to_psa_error( ret ) );
2927}
2928#endif /* MBEDTLS_ECDSA_C */
2929
Gilles Peskinec5487a82018-12-03 18:08:14 +01002930psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002931 psa_algorithm_t alg,
2932 const uint8_t *hash,
2933 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002934 uint8_t *signature,
2935 size_t signature_size,
2936 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002937{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002938 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002939 psa_status_t status;
2940
2941 *signature_length = signature_size;
2942
Gilles Peskinec5487a82018-12-03 18:08:14 +01002943 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002944 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002945 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02002946 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002947 {
2948 status = PSA_ERROR_INVALID_ARGUMENT;
2949 goto exit;
2950 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002951
Gilles Peskine20035e32018-02-03 22:44:14 +01002952#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02002953 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01002954 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002955 status = psa_rsa_sign( slot->data.rsa,
2956 alg,
2957 hash, hash_length,
2958 signature, signature_size,
2959 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002960 }
2961 else
2962#endif /* defined(MBEDTLS_RSA_C) */
2963#if defined(MBEDTLS_ECP_C)
2964 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2965 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002966#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002967 if(
2968#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2969 PSA_ALG_IS_ECDSA( alg )
2970#else
2971 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2972#endif
2973 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002974 status = psa_ecdsa_sign( slot->data.ecp,
2975 alg,
2976 hash, hash_length,
2977 signature, signature_size,
2978 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002979 else
2980#endif /* defined(MBEDTLS_ECDSA_C) */
2981 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002982 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002983 }
itayzafrir5c753392018-05-08 11:18:38 +03002984 }
2985 else
2986#endif /* defined(MBEDTLS_ECP_C) */
2987 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002988 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002989 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002990
2991exit:
2992 /* Fill the unused part of the output buffer (the whole buffer on error,
2993 * the trailing part on success) with something that isn't a valid mac
2994 * (barring an attack on the mac and deliberately-crafted input),
2995 * in case the caller doesn't check the return status properly. */
2996 if( status == PSA_SUCCESS )
2997 memset( signature + *signature_length, '!',
2998 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002999 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003000 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003001 /* If signature_size is 0 then we have nothing to do. We must not call
3002 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003003 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003004}
3005
Gilles Peskinec5487a82018-12-03 18:08:14 +01003006psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003007 psa_algorithm_t alg,
3008 const uint8_t *hash,
3009 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003010 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003011 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003012{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003013 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003014 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003015
Gilles Peskinec5487a82018-12-03 18:08:14 +01003016 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003017 if( status != PSA_SUCCESS )
3018 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003019
Gilles Peskine61b91d42018-06-08 16:09:36 +02003020#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003021 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003022 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003023 return( psa_rsa_verify( slot->data.rsa,
3024 alg,
3025 hash, hash_length,
3026 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003027 }
3028 else
3029#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003030#if defined(MBEDTLS_ECP_C)
3031 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3032 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003033#if defined(MBEDTLS_ECDSA_C)
3034 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003035 return( psa_ecdsa_verify( slot->data.ecp,
3036 hash, hash_length,
3037 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003038 else
3039#endif /* defined(MBEDTLS_ECDSA_C) */
3040 {
3041 return( PSA_ERROR_INVALID_ARGUMENT );
3042 }
itayzafrir5c753392018-05-08 11:18:38 +03003043 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003044 else
3045#endif /* defined(MBEDTLS_ECP_C) */
3046 {
3047 return( PSA_ERROR_NOT_SUPPORTED );
3048 }
3049}
3050
Gilles Peskine072ac562018-06-30 00:21:29 +02003051#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3052static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3053 mbedtls_rsa_context *rsa )
3054{
3055 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3056 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3057 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3058 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3059}
3060#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3061
Gilles Peskinec5487a82018-12-03 18:08:14 +01003062psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003063 psa_algorithm_t alg,
3064 const uint8_t *input,
3065 size_t input_length,
3066 const uint8_t *salt,
3067 size_t salt_length,
3068 uint8_t *output,
3069 size_t output_size,
3070 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003071{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003072 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003073 psa_status_t status;
3074
Darryl Green5cc689a2018-07-24 15:34:10 +01003075 (void) input;
3076 (void) input_length;
3077 (void) salt;
3078 (void) output;
3079 (void) output_size;
3080
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003081 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003082
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003083 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3084 return( PSA_ERROR_INVALID_ARGUMENT );
3085
Gilles Peskinec5487a82018-12-03 18:08:14 +01003086 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003087 if( status != PSA_SUCCESS )
3088 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003089 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003090 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003091 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003092
3093#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003094 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003095 {
3096 mbedtls_rsa_context *rsa = slot->data.rsa;
3097 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003098 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003099 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003100#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003101 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003102 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003103 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3104 mbedtls_ctr_drbg_random,
3105 &global_data.ctr_drbg,
3106 MBEDTLS_RSA_PUBLIC,
3107 input_length,
3108 input,
3109 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003110 }
3111 else
3112#endif /* MBEDTLS_PKCS1_V15 */
3113#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003114 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003115 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003116 psa_rsa_oaep_set_padding_mode( alg, rsa );
3117 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3118 mbedtls_ctr_drbg_random,
3119 &global_data.ctr_drbg,
3120 MBEDTLS_RSA_PUBLIC,
3121 salt, salt_length,
3122 input_length,
3123 input,
3124 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003125 }
3126 else
3127#endif /* MBEDTLS_PKCS1_V21 */
3128 {
3129 return( PSA_ERROR_INVALID_ARGUMENT );
3130 }
3131 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003132 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003133 return( mbedtls_to_psa_error( ret ) );
3134 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003135 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003136#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003137 {
3138 return( PSA_ERROR_NOT_SUPPORTED );
3139 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003140}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003141
Gilles Peskinec5487a82018-12-03 18:08:14 +01003142psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003143 psa_algorithm_t alg,
3144 const uint8_t *input,
3145 size_t input_length,
3146 const uint8_t *salt,
3147 size_t salt_length,
3148 uint8_t *output,
3149 size_t output_size,
3150 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003151{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003152 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003153 psa_status_t status;
3154
Darryl Green5cc689a2018-07-24 15:34:10 +01003155 (void) input;
3156 (void) input_length;
3157 (void) salt;
3158 (void) output;
3159 (void) output_size;
3160
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003161 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003162
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003163 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3164 return( PSA_ERROR_INVALID_ARGUMENT );
3165
Gilles Peskinec5487a82018-12-03 18:08:14 +01003166 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003167 if( status != PSA_SUCCESS )
3168 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003169 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003170 return( PSA_ERROR_INVALID_ARGUMENT );
3171
3172#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003173 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003174 {
3175 mbedtls_rsa_context *rsa = slot->data.rsa;
3176 int ret;
3177
Gilles Peskine630a18a2018-06-29 17:49:35 +02003178 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003179 return( PSA_ERROR_INVALID_ARGUMENT );
3180
3181#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003182 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003183 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003184 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3185 mbedtls_ctr_drbg_random,
3186 &global_data.ctr_drbg,
3187 MBEDTLS_RSA_PRIVATE,
3188 output_length,
3189 input,
3190 output,
3191 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003192 }
3193 else
3194#endif /* MBEDTLS_PKCS1_V15 */
3195#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003196 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003197 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003198 psa_rsa_oaep_set_padding_mode( alg, rsa );
3199 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3200 mbedtls_ctr_drbg_random,
3201 &global_data.ctr_drbg,
3202 MBEDTLS_RSA_PRIVATE,
3203 salt, salt_length,
3204 output_length,
3205 input,
3206 output,
3207 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003208 }
3209 else
3210#endif /* MBEDTLS_PKCS1_V21 */
3211 {
3212 return( PSA_ERROR_INVALID_ARGUMENT );
3213 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003214
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003215 return( mbedtls_to_psa_error( ret ) );
3216 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003217 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003218#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003219 {
3220 return( PSA_ERROR_NOT_SUPPORTED );
3221 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003222}
Gilles Peskine20035e32018-02-03 22:44:14 +01003223
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003224
3225
mohammad1603503973b2018-03-12 15:59:30 +02003226/****************************************************************/
3227/* Symmetric cryptography */
3228/****************************************************************/
3229
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003230/* Initialize the cipher operation structure. Once this function has been
3231 * called, psa_cipher_abort can run and will do the right thing. */
3232static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3233 psa_algorithm_t alg )
3234{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003235 if( ! PSA_ALG_IS_CIPHER( alg ) )
3236 {
3237 memset( operation, 0, sizeof( *operation ) );
3238 return( PSA_ERROR_INVALID_ARGUMENT );
3239 }
3240
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003241 operation->alg = alg;
3242 operation->key_set = 0;
3243 operation->iv_set = 0;
3244 operation->iv_required = 1;
3245 operation->iv_size = 0;
3246 operation->block_size = 0;
3247 mbedtls_cipher_init( &operation->ctx.cipher );
3248 return( PSA_SUCCESS );
3249}
3250
Gilles Peskinee553c652018-06-04 16:22:46 +02003251static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003252 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003253 psa_algorithm_t alg,
3254 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003255{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003256 int ret = 0;
3257 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003258 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003259 size_t key_bits;
3260 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003261 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3262 PSA_KEY_USAGE_ENCRYPT :
3263 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003264
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003265 /* A context must be freshly initialized before it can be set up. */
3266 if( operation->alg != 0 )
3267 {
3268 return( PSA_ERROR_BAD_STATE );
3269 }
3270
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003271 status = psa_cipher_init( operation, alg );
3272 if( status != PSA_SUCCESS )
3273 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003274
Gilles Peskinec5487a82018-12-03 18:08:14 +01003275 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003276 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003277 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003278 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003279
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003280 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003281 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003282 {
3283 status = PSA_ERROR_NOT_SUPPORTED;
3284 goto exit;
3285 }
mohammad1603503973b2018-03-12 15:59:30 +02003286
mohammad1603503973b2018-03-12 15:59:30 +02003287 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003288 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003289 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003290
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003291#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003292 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003293 {
3294 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3295 unsigned char keys[24];
3296 memcpy( keys, slot->data.raw.data, 16 );
3297 memcpy( keys + 16, slot->data.raw.data, 8 );
3298 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3299 keys,
3300 192, cipher_operation );
3301 }
3302 else
3303#endif
3304 {
3305 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3306 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003307 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003308 }
Moran Peker41deec42018-04-04 15:43:05 +03003309 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003310 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003311
mohammad16038481e742018-03-18 13:57:31 +02003312#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003313 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003314 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003315 case PSA_ALG_CBC_NO_PADDING:
3316 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3317 MBEDTLS_PADDING_NONE );
3318 break;
3319 case PSA_ALG_CBC_PKCS7:
3320 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3321 MBEDTLS_PADDING_PKCS7 );
3322 break;
3323 default:
3324 /* The algorithm doesn't involve padding. */
3325 ret = 0;
3326 break;
3327 }
3328 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003329 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003330#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3331
mohammad1603503973b2018-03-12 15:59:30 +02003332 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003333 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3334 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3335 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003336 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003337 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003338 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003339#if defined(MBEDTLS_CHACHA20_C)
3340 else
3341 if( alg == PSA_ALG_CHACHA20 )
3342 operation->iv_size = 12;
3343#endif
mohammad1603503973b2018-03-12 15:59:30 +02003344
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003345exit:
3346 if( status == 0 )
3347 status = mbedtls_to_psa_error( ret );
3348 if( status != 0 )
3349 psa_cipher_abort( operation );
3350 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003351}
3352
Gilles Peskinefe119512018-07-08 21:39:34 +02003353psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003354 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003355 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003356{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003357 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003358}
3359
Gilles Peskinefe119512018-07-08 21:39:34 +02003360psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003361 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003362 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003363{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003364 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003365}
3366
Gilles Peskinefe119512018-07-08 21:39:34 +02003367psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3368 unsigned char *iv,
3369 size_t iv_size,
3370 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003371{
itayzafrir534bd7c2018-08-02 13:56:32 +03003372 psa_status_t status;
3373 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003374 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003375 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003376 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003377 }
Moran Peker41deec42018-04-04 15:43:05 +03003378 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003379 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003380 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003381 goto exit;
3382 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003383 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3384 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003385 if( ret != 0 )
3386 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003387 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003388 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003389 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003390
mohammad16038481e742018-03-18 13:57:31 +02003391 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003392 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003393
Moran Peker395db872018-05-31 14:07:14 +03003394exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003395 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003396 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003397 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003398}
3399
Gilles Peskinefe119512018-07-08 21:39:34 +02003400psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3401 const unsigned char *iv,
3402 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003403{
itayzafrir534bd7c2018-08-02 13:56:32 +03003404 psa_status_t status;
3405 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003406 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003407 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003408 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003409 }
Moran Pekera28258c2018-05-29 16:25:04 +03003410 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003411 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003412 status = PSA_ERROR_INVALID_ARGUMENT;
3413 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003414 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003415 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3416 status = mbedtls_to_psa_error( ret );
3417exit:
3418 if( status == PSA_SUCCESS )
3419 operation->iv_set = 1;
3420 else
mohammad1603503973b2018-03-12 15:59:30 +02003421 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003422 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003423}
3424
Gilles Peskinee553c652018-06-04 16:22:46 +02003425psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3426 const uint8_t *input,
3427 size_t input_length,
3428 unsigned char *output,
3429 size_t output_size,
3430 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003431{
itayzafrir534bd7c2018-08-02 13:56:32 +03003432 psa_status_t status;
3433 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003434 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003435
3436 if( operation->alg == 0 )
3437 {
3438 return( PSA_ERROR_BAD_STATE );
3439 }
3440
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003441 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003442 {
3443 /* Take the unprocessed partial block left over from previous
3444 * update calls, if any, plus the input to this call. Remove
3445 * the last partial block, if any. You get the data that will be
3446 * output in this call. */
3447 expected_output_size =
3448 ( operation->ctx.cipher.unprocessed_len + input_length )
3449 / operation->block_size * operation->block_size;
3450 }
3451 else
3452 {
3453 expected_output_size = input_length;
3454 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003455
Gilles Peskine89d789c2018-06-04 17:17:16 +02003456 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003457 {
3458 status = PSA_ERROR_BUFFER_TOO_SMALL;
3459 goto exit;
3460 }
mohammad160382759612018-03-12 18:16:40 +02003461
mohammad1603503973b2018-03-12 15:59:30 +02003462 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003463 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003464 status = mbedtls_to_psa_error( ret );
3465exit:
3466 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003467 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003468 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003469}
3470
Gilles Peskinee553c652018-06-04 16:22:46 +02003471psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3472 uint8_t *output,
3473 size_t output_size,
3474 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003475{
David Saadab4ecc272019-02-14 13:48:10 +02003476 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003477 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003478 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003479
mohammad1603503973b2018-03-12 15:59:30 +02003480 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003481 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003482 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003483 }
3484 if( operation->iv_required && ! operation->iv_set )
3485 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003486 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003487 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003488
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003489 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003490 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3491 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003492 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003493 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003494 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003495 }
3496
Janos Follath315b51c2018-07-09 16:04:51 +01003497 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3498 temp_output_buffer,
3499 output_length );
3500 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003501 {
Janos Follath315b51c2018-07-09 16:04:51 +01003502 status = mbedtls_to_psa_error( cipher_ret );
3503 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003504 }
Janos Follath315b51c2018-07-09 16:04:51 +01003505
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003506 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003507 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003508 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003509 memcpy( output, temp_output_buffer, *output_length );
3510 else
3511 {
Janos Follath315b51c2018-07-09 16:04:51 +01003512 status = PSA_ERROR_BUFFER_TOO_SMALL;
3513 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003514 }
mohammad1603503973b2018-03-12 15:59:30 +02003515
Gilles Peskine3f108122018-12-07 18:14:53 +01003516 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003517 status = psa_cipher_abort( operation );
3518
3519 return( status );
3520
3521error:
3522
3523 *output_length = 0;
3524
Gilles Peskine3f108122018-12-07 18:14:53 +01003525 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003526 (void) psa_cipher_abort( operation );
3527
3528 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003529}
3530
Gilles Peskinee553c652018-06-04 16:22:46 +02003531psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3532{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003533 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003534 {
3535 /* The object has (apparently) been initialized but it is not
3536 * in use. It's ok to call abort on such an object, and there's
3537 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003538 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003539 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003540
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003541 /* Sanity check (shouldn't happen: operation->alg should
3542 * always have been initialized to a valid value). */
3543 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3544 return( PSA_ERROR_BAD_STATE );
3545
mohammad1603503973b2018-03-12 15:59:30 +02003546 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003547
Moran Peker41deec42018-04-04 15:43:05 +03003548 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003549 operation->key_set = 0;
3550 operation->iv_set = 0;
3551 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003552 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003553 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003554
Moran Peker395db872018-05-31 14:07:14 +03003555 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003556}
3557
Gilles Peskinea0655c32018-04-30 17:06:50 +02003558
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003559
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003560
mohammad16035955c982018-04-26 00:53:03 +03003561/****************************************************************/
3562/* AEAD */
3563/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003564
Gilles Peskineedf9a652018-08-17 18:11:56 +02003565typedef struct
3566{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003567 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003568 const mbedtls_cipher_info_t *cipher_info;
3569 union
3570 {
3571#if defined(MBEDTLS_CCM_C)
3572 mbedtls_ccm_context ccm;
3573#endif /* MBEDTLS_CCM_C */
3574#if defined(MBEDTLS_GCM_C)
3575 mbedtls_gcm_context gcm;
3576#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003577#if defined(MBEDTLS_CHACHAPOLY_C)
3578 mbedtls_chachapoly_context chachapoly;
3579#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003580 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003581 psa_algorithm_t core_alg;
3582 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003583 uint8_t tag_length;
3584} aead_operation_t;
3585
Gilles Peskine30a9e412019-01-14 18:36:12 +01003586static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003587{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003588 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003589 {
3590#if defined(MBEDTLS_CCM_C)
3591 case PSA_ALG_CCM:
3592 mbedtls_ccm_free( &operation->ctx.ccm );
3593 break;
3594#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003595#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003596 case PSA_ALG_GCM:
3597 mbedtls_gcm_free( &operation->ctx.gcm );
3598 break;
3599#endif /* MBEDTLS_GCM_C */
3600 }
3601}
3602
3603static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003604 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003605 psa_key_usage_t usage,
3606 psa_algorithm_t alg )
3607{
3608 psa_status_t status;
3609 size_t key_bits;
3610 mbedtls_cipher_id_t cipher_id;
3611
Gilles Peskinec5487a82018-12-03 18:08:14 +01003612 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003613 if( status != PSA_SUCCESS )
3614 return( status );
3615
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003616 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003617
3618 operation->cipher_info =
3619 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3620 &cipher_id );
3621 if( operation->cipher_info == NULL )
3622 return( PSA_ERROR_NOT_SUPPORTED );
3623
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003624 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003625 {
3626#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003627 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3628 operation->core_alg = PSA_ALG_CCM;
3629 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003630 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3631 * The call to mbedtls_ccm_encrypt_and_tag or
3632 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003633 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3634 return( PSA_ERROR_INVALID_ARGUMENT );
3635 mbedtls_ccm_init( &operation->ctx.ccm );
3636 status = mbedtls_to_psa_error(
3637 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3638 operation->slot->data.raw.data,
3639 (unsigned int) key_bits ) );
3640 if( status != 0 )
3641 goto cleanup;
3642 break;
3643#endif /* MBEDTLS_CCM_C */
3644
3645#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003646 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3647 operation->core_alg = PSA_ALG_GCM;
3648 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003649 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3650 * The call to mbedtls_gcm_crypt_and_tag or
3651 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003652 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3653 return( PSA_ERROR_INVALID_ARGUMENT );
3654 mbedtls_gcm_init( &operation->ctx.gcm );
3655 status = mbedtls_to_psa_error(
3656 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3657 operation->slot->data.raw.data,
3658 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003659 if( status != 0 )
3660 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003661 break;
3662#endif /* MBEDTLS_GCM_C */
3663
Gilles Peskine26869f22019-05-06 15:25:00 +02003664#if defined(MBEDTLS_CHACHAPOLY_C)
3665 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3666 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3667 operation->full_tag_length = 16;
3668 /* We only support the default tag length. */
3669 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3670 return( PSA_ERROR_NOT_SUPPORTED );
3671 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3672 status = mbedtls_to_psa_error(
3673 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3674 operation->slot->data.raw.data ) );
3675 if( status != 0 )
3676 goto cleanup;
3677 break;
3678#endif /* MBEDTLS_CHACHAPOLY_C */
3679
Gilles Peskineedf9a652018-08-17 18:11:56 +02003680 default:
3681 return( PSA_ERROR_NOT_SUPPORTED );
3682 }
3683
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003684 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3685 {
3686 status = PSA_ERROR_INVALID_ARGUMENT;
3687 goto cleanup;
3688 }
3689 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003690
Gilles Peskineedf9a652018-08-17 18:11:56 +02003691 return( PSA_SUCCESS );
3692
3693cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003694 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003695 return( status );
3696}
3697
Gilles Peskinec5487a82018-12-03 18:08:14 +01003698psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003699 psa_algorithm_t alg,
3700 const uint8_t *nonce,
3701 size_t nonce_length,
3702 const uint8_t *additional_data,
3703 size_t additional_data_length,
3704 const uint8_t *plaintext,
3705 size_t plaintext_length,
3706 uint8_t *ciphertext,
3707 size_t ciphertext_size,
3708 size_t *ciphertext_length )
3709{
mohammad16035955c982018-04-26 00:53:03 +03003710 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003711 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003712 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003713
mohammad1603f08a5502018-06-03 15:05:47 +03003714 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003715
Gilles Peskinec5487a82018-12-03 18:08:14 +01003716 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003717 if( status != PSA_SUCCESS )
3718 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003719
Gilles Peskineedf9a652018-08-17 18:11:56 +02003720 /* For all currently supported modes, the tag is at the end of the
3721 * ciphertext. */
3722 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3723 {
3724 status = PSA_ERROR_BUFFER_TOO_SMALL;
3725 goto exit;
3726 }
3727 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003728
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003729#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003730 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003731 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003732 status = mbedtls_to_psa_error(
3733 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3734 MBEDTLS_GCM_ENCRYPT,
3735 plaintext_length,
3736 nonce, nonce_length,
3737 additional_data, additional_data_length,
3738 plaintext, ciphertext,
3739 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003740 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003741 else
3742#endif /* MBEDTLS_GCM_C */
3743#if defined(MBEDTLS_CCM_C)
3744 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003745 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003746 status = mbedtls_to_psa_error(
3747 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3748 plaintext_length,
3749 nonce, nonce_length,
3750 additional_data,
3751 additional_data_length,
3752 plaintext, ciphertext,
3753 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003754 }
mohammad16035c8845f2018-05-09 05:40:09 -07003755 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003756#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003757#if defined(MBEDTLS_CHACHAPOLY_C)
3758 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3759 {
3760 if( nonce_length != 12 || operation.tag_length != 16 )
3761 {
3762 status = PSA_ERROR_NOT_SUPPORTED;
3763 goto exit;
3764 }
3765 status = mbedtls_to_psa_error(
3766 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3767 plaintext_length,
3768 nonce,
3769 additional_data,
3770 additional_data_length,
3771 plaintext,
3772 ciphertext,
3773 tag ) );
3774 }
3775 else
3776#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003777 {
mohammad1603554faad2018-06-03 15:07:38 +03003778 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003779 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003780
Gilles Peskineedf9a652018-08-17 18:11:56 +02003781 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3782 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003783
Gilles Peskineedf9a652018-08-17 18:11:56 +02003784exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003785 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003786 if( status == PSA_SUCCESS )
3787 *ciphertext_length = plaintext_length + operation.tag_length;
3788 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003789}
3790
Gilles Peskineee652a32018-06-01 19:23:52 +02003791/* Locate the tag in a ciphertext buffer containing the encrypted data
3792 * followed by the tag. Return the length of the part preceding the tag in
3793 * *plaintext_length. This is the size of the plaintext in modes where
3794 * the encrypted data has the same size as the plaintext, such as
3795 * CCM and GCM. */
3796static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3797 const uint8_t *ciphertext,
3798 size_t ciphertext_length,
3799 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003800 const uint8_t **p_tag )
3801{
3802 size_t payload_length;
3803 if( tag_length > ciphertext_length )
3804 return( PSA_ERROR_INVALID_ARGUMENT );
3805 payload_length = ciphertext_length - tag_length;
3806 if( payload_length > plaintext_size )
3807 return( PSA_ERROR_BUFFER_TOO_SMALL );
3808 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003809 return( PSA_SUCCESS );
3810}
3811
Gilles Peskinec5487a82018-12-03 18:08:14 +01003812psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003813 psa_algorithm_t alg,
3814 const uint8_t *nonce,
3815 size_t nonce_length,
3816 const uint8_t *additional_data,
3817 size_t additional_data_length,
3818 const uint8_t *ciphertext,
3819 size_t ciphertext_length,
3820 uint8_t *plaintext,
3821 size_t plaintext_size,
3822 size_t *plaintext_length )
3823{
mohammad16035955c982018-04-26 00:53:03 +03003824 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003825 aead_operation_t operation;
3826 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003827
Gilles Peskineee652a32018-06-01 19:23:52 +02003828 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003829
Gilles Peskinec5487a82018-12-03 18:08:14 +01003830 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003831 if( status != PSA_SUCCESS )
3832 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003833
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003834 status = psa_aead_unpadded_locate_tag( operation.tag_length,
3835 ciphertext, ciphertext_length,
3836 plaintext_size, &tag );
3837 if( status != PSA_SUCCESS )
3838 goto exit;
3839
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003840#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003841 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003842 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003843 status = mbedtls_to_psa_error(
3844 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3845 ciphertext_length - operation.tag_length,
3846 nonce, nonce_length,
3847 additional_data,
3848 additional_data_length,
3849 tag, operation.tag_length,
3850 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003851 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003852 else
3853#endif /* MBEDTLS_GCM_C */
3854#if defined(MBEDTLS_CCM_C)
3855 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003856 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003857 status = mbedtls_to_psa_error(
3858 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3859 ciphertext_length - operation.tag_length,
3860 nonce, nonce_length,
3861 additional_data,
3862 additional_data_length,
3863 ciphertext, plaintext,
3864 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003865 }
mohammad160339574652018-06-01 04:39:53 -07003866 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003867#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003868#if defined(MBEDTLS_CHACHAPOLY_C)
3869 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3870 {
3871 if( nonce_length != 12 || operation.tag_length != 16 )
3872 {
3873 status = PSA_ERROR_NOT_SUPPORTED;
3874 goto exit;
3875 }
3876 status = mbedtls_to_psa_error(
3877 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
3878 ciphertext_length - operation.tag_length,
3879 nonce,
3880 additional_data,
3881 additional_data_length,
3882 tag,
3883 ciphertext,
3884 plaintext ) );
3885 }
3886 else
3887#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07003888 {
mohammad1603554faad2018-06-03 15:07:38 +03003889 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003890 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003891
Gilles Peskineedf9a652018-08-17 18:11:56 +02003892 if( status != PSA_SUCCESS && plaintext_size != 0 )
3893 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003894
Gilles Peskineedf9a652018-08-17 18:11:56 +02003895exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003896 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003897 if( status == PSA_SUCCESS )
3898 *plaintext_length = ciphertext_length - operation.tag_length;
3899 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003900}
3901
Gilles Peskinea0655c32018-04-30 17:06:50 +02003902
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003903
Gilles Peskine20035e32018-02-03 22:44:14 +01003904/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003905/* Generators */
3906/****************************************************************/
3907
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003908#define HKDF_STATE_INIT 0 /* no input yet */
3909#define HKDF_STATE_STARTED 1 /* got salt */
3910#define HKDF_STATE_KEYED 2 /* got key */
3911#define HKDF_STATE_OUTPUT 3 /* output started */
3912
Gilles Peskinecbe66502019-05-16 16:59:18 +02003913static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003914 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01003915{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003916 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
3917 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003918 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003919 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003920}
3921
3922
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003923psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003924{
3925 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003926 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003927 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003928 {
3929 /* The object has (apparently) been initialized but it is not
3930 * in use. It's ok to call abort on such an object, and there's
3931 * nothing to do. */
3932 }
3933 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003934#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003935 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003936 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003937 mbedtls_free( operation->ctx.hkdf.info );
3938 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003939 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003940 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003941 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003942 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003943 {
Janos Follath6a1d2622019-06-11 10:37:28 +01003944#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003945 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003946 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003947 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
3948 operation->ctx.tls12_prf.key_len );
3949 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003950 }
Hanno Becker580fba12018-11-13 20:50:45 +00003951
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003952 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00003953 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003954 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
3955 operation->ctx.tls12_prf.Ai_with_seed_len );
3956 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00003957 }
Janos Follath6a1d2622019-06-11 10:37:28 +01003958#else
3959 if( operation->ctx.tls12_prf.seed != NULL )
3960 {
3961 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
3962 operation->ctx.tls12_prf.seed_length );
3963 mbedtls_free( operation->ctx.tls12_prf.seed );
3964 }
3965
3966 if( operation->ctx.tls12_prf.label != NULL )
3967 {
3968 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
3969 operation->ctx.tls12_prf.label_length );
3970 mbedtls_free( operation->ctx.tls12_prf.label );
3971 }
3972
3973 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
3974
3975 /* We leave the fields Ai and output_block to be erased safely by the
3976 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01003977#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003978 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003979 else
3980#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003981 {
3982 status = PSA_ERROR_BAD_STATE;
3983 }
Janos Follath083036a2019-06-11 10:22:26 +01003984 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003985 return( status );
3986}
3987
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003988psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003989 size_t *capacity)
3990{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003991 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003992 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003993 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003994 return PSA_ERROR_BAD_STATE;
3995 }
3996
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003997 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003998 return( PSA_SUCCESS );
3999}
4000
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004001psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004002 size_t capacity )
4003{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004004 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004005 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004006 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004007 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004008 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004009 return( PSA_SUCCESS );
4010}
4011
Gilles Peskinebef7f142018-07-12 17:22:21 +02004012#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004013/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004014 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004015static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004016 psa_algorithm_t hash_alg,
4017 uint8_t *output,
4018 size_t output_length )
4019{
4020 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4021 psa_status_t status;
4022
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004023 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4024 return( PSA_ERROR_BAD_STATE );
4025 hkdf->state = HKDF_STATE_OUTPUT;
4026
Gilles Peskinebef7f142018-07-12 17:22:21 +02004027 while( output_length != 0 )
4028 {
4029 /* Copy what remains of the current block */
4030 uint8_t n = hash_length - hkdf->offset_in_block;
4031 if( n > output_length )
4032 n = (uint8_t) output_length;
4033 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4034 output += n;
4035 output_length -= n;
4036 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004037 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004038 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004039 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004040 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004041 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004042 * object was corrupted or if this function is called directly
4043 * inside the library. */
4044 if( hkdf->block_number == 0xff )
4045 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004046
4047 /* We need a new block */
4048 ++hkdf->block_number;
4049 hkdf->offset_in_block = 0;
4050 status = psa_hmac_setup_internal( &hkdf->hmac,
4051 hkdf->prk, hash_length,
4052 hash_alg );
4053 if( status != PSA_SUCCESS )
4054 return( status );
4055 if( hkdf->block_number != 1 )
4056 {
4057 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4058 hkdf->output_block,
4059 hash_length );
4060 if( status != PSA_SUCCESS )
4061 return( status );
4062 }
4063 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4064 hkdf->info,
4065 hkdf->info_length );
4066 if( status != PSA_SUCCESS )
4067 return( status );
4068 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4069 &hkdf->block_number, 1 );
4070 if( status != PSA_SUCCESS )
4071 return( status );
4072 status = psa_hmac_finish_internal( &hkdf->hmac,
4073 hkdf->output_block,
4074 sizeof( hkdf->output_block ) );
4075 if( status != PSA_SUCCESS )
4076 return( status );
4077 }
4078
4079 return( PSA_SUCCESS );
4080}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004081
Janos Follath999f6482019-06-11 12:04:10 +01004082#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004083static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4084 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004085 psa_algorithm_t alg )
4086{
4087 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4088 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4089 psa_hmac_internal_data hmac;
4090 psa_status_t status, cleanup_status;
4091
Hanno Becker3b339e22018-11-13 20:56:14 +00004092 unsigned char *Ai;
4093 size_t Ai_len;
4094
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004095 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004096 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004097 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004098 * object was corrupted or if this function is called directly
4099 * inside the library. */
4100 if( tls12_prf->block_number == 0xff )
4101 return( PSA_ERROR_BAD_STATE );
4102
4103 /* We need a new block */
4104 ++tls12_prf->block_number;
4105 tls12_prf->offset_in_block = 0;
4106
4107 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4108 *
4109 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4110 *
4111 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4112 * HMAC_hash(secret, A(2) + seed) +
4113 * HMAC_hash(secret, A(3) + seed) + ...
4114 *
4115 * A(0) = seed
4116 * A(i) = HMAC_hash( secret, A(i-1) )
4117 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004118 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004119 * `HMAC_hash(secret, A(i) + seed)` from which the output
4120 * is currently extracted as `output_block`, while
4121 * `A(i) + seed` is stored in `Ai_with_seed`.
4122 *
4123 * Generating a new block means recalculating `Ai_with_seed`
4124 * from the A(i)-part of it, and afterwards recalculating
4125 * `output_block`.
4126 *
4127 * A(0) is computed at setup time.
4128 *
4129 */
4130
4131 psa_hmac_init_internal( &hmac );
4132
4133 /* We must distinguish the calculation of A(1) from those
4134 * of A(2) and higher, because A(0)=seed has a different
4135 * length than the other A(i). */
4136 if( tls12_prf->block_number == 1 )
4137 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004138 Ai = tls12_prf->Ai_with_seed + hash_length;
4139 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004140 }
4141 else
4142 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004143 Ai = tls12_prf->Ai_with_seed;
4144 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004145 }
4146
Hanno Becker3b339e22018-11-13 20:56:14 +00004147 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4148 status = psa_hmac_setup_internal( &hmac,
4149 tls12_prf->key,
4150 tls12_prf->key_len,
4151 hash_alg );
4152 if( status != PSA_SUCCESS )
4153 goto cleanup;
4154
4155 status = psa_hash_update( &hmac.hash_ctx,
4156 Ai, Ai_len );
4157 if( status != PSA_SUCCESS )
4158 goto cleanup;
4159
4160 status = psa_hmac_finish_internal( &hmac,
4161 tls12_prf->Ai_with_seed,
4162 hash_length );
4163 if( status != PSA_SUCCESS )
4164 goto cleanup;
4165
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004166 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4167 status = psa_hmac_setup_internal( &hmac,
4168 tls12_prf->key,
4169 tls12_prf->key_len,
4170 hash_alg );
4171 if( status != PSA_SUCCESS )
4172 goto cleanup;
4173
4174 status = psa_hash_update( &hmac.hash_ctx,
4175 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004176 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004177 if( status != PSA_SUCCESS )
4178 goto cleanup;
4179
4180 status = psa_hmac_finish_internal( &hmac,
4181 tls12_prf->output_block,
4182 hash_length );
4183 if( status != PSA_SUCCESS )
4184 goto cleanup;
4185
4186cleanup:
4187
4188 cleanup_status = psa_hmac_abort_internal( &hmac );
4189 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4190 status = cleanup_status;
4191
4192 return( status );
4193}
Janos Follath7742fee2019-06-17 12:58:10 +01004194#else
4195static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4196 psa_tls12_prf_key_derivation_t *tls12_prf,
4197 psa_algorithm_t alg )
4198{
4199 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4200 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004201 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4202 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004203
Janos Follath7742fee2019-06-17 12:58:10 +01004204 /* We can't be wanting more output after block 0xff, otherwise
4205 * the capacity check in psa_key_derivation_output_bytes() would have
4206 * prevented this call. It could happen only if the operation
4207 * object was corrupted or if this function is called directly
4208 * inside the library. */
4209 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004210 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004211
4212 /* We need a new block */
4213 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004214 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004215
4216 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4217 *
4218 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4219 *
4220 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4221 * HMAC_hash(secret, A(2) + seed) +
4222 * HMAC_hash(secret, A(3) + seed) + ...
4223 *
4224 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004225 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004226 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004227 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004228 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004229 * is currently extracted as `output_block` and where i is
4230 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004231 */
4232
Janos Follathea29bfb2019-06-19 12:21:20 +01004233 /* Save the hash context before using it, to preserve the hash state with
4234 * only the inner padding in it. We need this, because inner padding depends
4235 * on the key (secret in the RFC's terminology). */
4236 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4237 if( status != PSA_SUCCESS )
4238 goto cleanup;
4239
4240 /* Calculate A(i) where i = tls12_prf->block_number. */
4241 if( tls12_prf->block_number == 1 )
4242 {
4243 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4244 * the variable seed and in this instance means it in the context of the
4245 * P_hash function, where seed = label + seed.) */
4246 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4247 tls12_prf->label, tls12_prf->label_length );
4248 if( status != PSA_SUCCESS )
4249 goto cleanup;
4250 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4251 tls12_prf->seed, tls12_prf->seed_length );
4252 if( status != PSA_SUCCESS )
4253 goto cleanup;
4254 }
4255 else
4256 {
4257 /* A(i) = HMAC_hash(secret, A(i-1)) */
4258 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4259 tls12_prf->Ai, hash_length );
4260 if( status != PSA_SUCCESS )
4261 goto cleanup;
4262 }
4263
4264 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4265 tls12_prf->Ai, hash_length );
4266 if( status != PSA_SUCCESS )
4267 goto cleanup;
4268 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4269 if( status != PSA_SUCCESS )
4270 goto cleanup;
4271
4272 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4273 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4274 tls12_prf->Ai, hash_length );
4275 if( status != PSA_SUCCESS )
4276 goto cleanup;
4277 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4278 tls12_prf->label, tls12_prf->label_length );
4279 if( status != PSA_SUCCESS )
4280 goto cleanup;
4281 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4282 tls12_prf->seed, tls12_prf->seed_length );
4283 if( status != PSA_SUCCESS )
4284 goto cleanup;
4285 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4286 tls12_prf->output_block, hash_length );
4287 if( status != PSA_SUCCESS )
4288 goto cleanup;
4289 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4290 if( status != PSA_SUCCESS )
4291 goto cleanup;
4292
Janos Follath7742fee2019-06-17 12:58:10 +01004293
4294cleanup:
4295
Janos Follathea29bfb2019-06-19 12:21:20 +01004296 cleanup_status = psa_hash_abort( &backup );
4297 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4298 status = cleanup_status;
4299
4300 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004301}
Janos Follath999f6482019-06-11 12:04:10 +01004302#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004303
Janos Follath999f6482019-06-11 12:04:10 +01004304#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004305/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004306 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004307static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004308 psa_tls12_prf_key_derivation_t *tls12_prf,
4309 psa_algorithm_t alg,
4310 uint8_t *output,
4311 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004312{
4313 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4314 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4315 psa_status_t status;
4316
4317 while( output_length != 0 )
4318 {
4319 /* Copy what remains of the current block */
4320 uint8_t n = hash_length - tls12_prf->offset_in_block;
4321
4322 /* Check if we have fully processed the current block. */
4323 if( n == 0 )
4324 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004325 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004326 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004327 if( status != PSA_SUCCESS )
4328 return( status );
4329
4330 continue;
4331 }
4332
4333 if( n > output_length )
4334 n = (uint8_t) output_length;
4335 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4336 n );
4337 output += n;
4338 output_length -= n;
4339 tls12_prf->offset_in_block += n;
4340 }
4341
4342 return( PSA_SUCCESS );
4343}
Janos Follath844eb0e2019-06-19 12:10:49 +01004344#else
4345static psa_status_t psa_key_derivation_tls12_prf_read(
4346 psa_tls12_prf_key_derivation_t *tls12_prf,
4347 psa_algorithm_t alg,
4348 uint8_t *output,
4349 size_t output_length )
4350{
4351 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4352 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4353 psa_status_t status;
4354 uint8_t offset, length;
4355
4356 while( output_length != 0 )
4357 {
4358 /* Check if we have fully processed the current block. */
4359 if( tls12_prf->left_in_block == 0 )
4360 {
4361 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4362 alg );
4363 if( status != PSA_SUCCESS )
4364 return( status );
4365
4366 continue;
4367 }
4368
4369 if( tls12_prf->left_in_block > output_length )
4370 length = (uint8_t) output_length;
4371 else
4372 length = tls12_prf->left_in_block;
4373
4374 offset = hash_length - tls12_prf->left_in_block;
4375 memcpy( output, tls12_prf->output_block + offset, length );
4376 output += length;
4377 output_length -= length;
4378 tls12_prf->left_in_block -= length;
4379 }
4380
4381 return( PSA_SUCCESS );
4382}
Janos Follath999f6482019-06-11 12:04:10 +01004383#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004384#endif /* MBEDTLS_MD_C */
4385
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004386psa_status_t psa_key_derivation_output_bytes(
4387 psa_key_derivation_operation_t *operation,
4388 uint8_t *output,
4389 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004390{
4391 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004392 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004393
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004394 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004395 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004396 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004397 return PSA_ERROR_BAD_STATE;
4398 }
4399
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004400 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004401 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004402 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004403 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004404 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004405 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004406 goto exit;
4407 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004408 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004409 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004410 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004411 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004412 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4413 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004415 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004416 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004417 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004418 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004419
Gilles Peskinebef7f142018-07-12 17:22:21 +02004420#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004421 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004422 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004423 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004425 output, output_length );
4426 }
Janos Follathadbec812019-06-14 11:05:39 +01004427 else
Janos Follathadbec812019-06-14 11:05:39 +01004428 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004429 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004430 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004431 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004432 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004433 output_length );
4434 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004435 else
4436#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004437 {
4438 return( PSA_ERROR_BAD_STATE );
4439 }
4440
4441exit:
4442 if( status != PSA_SUCCESS )
4443 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004444 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004445 * This allows us to differentiate between exhausted operations and
4446 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4447 * operations. */
4448 psa_algorithm_t alg = operation->alg;
4449 psa_key_derivation_abort( operation );
4450 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004451 memset( output, '!', output_length );
4452 }
4453 return( status );
4454}
4455
Gilles Peskine08542d82018-07-19 17:05:42 +02004456#if defined(MBEDTLS_DES_C)
4457static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4458{
4459 if( data_size >= 8 )
4460 mbedtls_des_key_set_parity( data );
4461 if( data_size >= 16 )
4462 mbedtls_des_key_set_parity( data + 8 );
4463 if( data_size >= 24 )
4464 mbedtls_des_key_set_parity( data + 16 );
4465}
4466#endif /* MBEDTLS_DES_C */
4467
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004468static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004469 psa_key_slot_t *slot,
4470 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004471 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004472{
4473 uint8_t *data = NULL;
4474 size_t bytes = PSA_BITS_TO_BYTES( bits );
4475 psa_status_t status;
4476
4477 if( ! key_type_is_raw_bytes( slot->type ) )
4478 return( PSA_ERROR_INVALID_ARGUMENT );
4479 if( bits % 8 != 0 )
4480 return( PSA_ERROR_INVALID_ARGUMENT );
4481 data = mbedtls_calloc( 1, bytes );
4482 if( data == NULL )
4483 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4484
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004485 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004486 if( status != PSA_SUCCESS )
4487 goto exit;
4488#if defined(MBEDTLS_DES_C)
4489 if( slot->type == PSA_KEY_TYPE_DES )
4490 psa_des_set_key_parity( data, bytes );
4491#endif /* MBEDTLS_DES_C */
4492 status = psa_import_key_into_slot( slot, data, bytes );
4493
4494exit:
4495 mbedtls_free( data );
4496 return( status );
4497}
4498
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004499psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004500 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004501 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004502{
4503 psa_status_t status;
4504 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004505 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004506 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004507 if( status == PSA_SUCCESS )
4508 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004509 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004510 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004511 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004512 }
4513 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004514 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004515 if( status != PSA_SUCCESS )
4516 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004517 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004518 *handle = 0;
4519 }
4520 return( status );
4521}
4522
Gilles Peskineeab56e42018-07-12 17:12:33 +02004523
4524
4525/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004526/* Key derivation */
4527/****************************************************************/
4528
Gilles Peskinea05219c2018-11-16 16:02:56 +01004529#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004530#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004531/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004532 * of the HKDF algorithm.
4533 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004534 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004535 * to potentially free embedded data structures and wipe confidential data.
4536 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004537static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004538 const uint8_t *secret,
4539 size_t secret_length,
4540 psa_algorithm_t hash_alg,
4541 const uint8_t *salt,
4542 size_t salt_length,
4543 const uint8_t *label,
4544 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004545{
4546 psa_status_t status;
4547 status = psa_hmac_setup_internal( &hkdf->hmac,
4548 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004549 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004550 if( status != PSA_SUCCESS )
4551 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004552 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004553 if( status != PSA_SUCCESS )
4554 return( status );
4555 status = psa_hmac_finish_internal( &hkdf->hmac,
4556 hkdf->prk,
4557 sizeof( hkdf->prk ) );
4558 if( status != PSA_SUCCESS )
4559 return( status );
4560 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4561 hkdf->block_number = 0;
4562 hkdf->info_length = label_length;
4563 if( label_length != 0 )
4564 {
4565 hkdf->info = mbedtls_calloc( 1, label_length );
4566 if( hkdf->info == NULL )
4567 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4568 memcpy( hkdf->info, label, label_length );
4569 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004570 hkdf->state = HKDF_STATE_KEYED;
4571 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004572 return( PSA_SUCCESS );
4573}
Janos Follath71a4c912019-06-11 09:14:47 +01004574#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004575#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004576
Gilles Peskinea05219c2018-11-16 16:02:56 +01004577#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004578#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004579/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004580 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004581 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004582 * to potentially free embedded data structures and wipe confidential data.
4583 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004584static psa_status_t psa_key_derivation_tls12_prf_setup(
4585 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004586 const unsigned char *key,
4587 size_t key_len,
4588 psa_algorithm_t hash_alg,
4589 const uint8_t *salt,
4590 size_t salt_length,
4591 const uint8_t *label,
4592 size_t label_length )
4593{
4594 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004595 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4596 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004597
4598 tls12_prf->key = mbedtls_calloc( 1, key_len );
4599 if( tls12_prf->key == NULL )
4600 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4601 tls12_prf->key_len = key_len;
4602 memcpy( tls12_prf->key, key, key_len );
4603
Hanno Becker580fba12018-11-13 20:50:45 +00004604 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004605 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004606 if( overflow )
4607 return( PSA_ERROR_INVALID_ARGUMENT );
4608
4609 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4610 if( tls12_prf->Ai_with_seed == NULL )
4611 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4612 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4613
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004614 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4615 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004616 if( label_length != 0 )
4617 {
4618 memcpy( tls12_prf->Ai_with_seed + hash_length,
4619 label, label_length );
4620 }
4621
4622 if( salt_length != 0 )
4623 {
4624 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4625 salt, salt_length );
4626 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004627
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004628 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004629 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004630 tls12_prf->block_number = 0;
4631 tls12_prf->offset_in_block = hash_length;
4632
4633 return( PSA_SUCCESS );
4634}
Janos Follath71a4c912019-06-11 09:14:47 +01004635#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004636
Janos Follath71a4c912019-06-11 09:14:47 +01004637#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004638/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004639static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4640 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004641 const unsigned char *psk,
4642 size_t psk_len,
4643 psa_algorithm_t hash_alg,
4644 const uint8_t *salt,
4645 size_t salt_length,
4646 const uint8_t *label,
4647 size_t label_length )
4648{
4649 psa_status_t status;
4650 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4651
4652 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4653 return( PSA_ERROR_INVALID_ARGUMENT );
4654
4655 /* Quoting RFC 4279, Section 2:
4656 *
4657 * The premaster secret is formed as follows: if the PSK is N octets
4658 * long, concatenate a uint16 with the value N, N zero octets, a second
4659 * uint16 with the value N, and the PSK itself.
4660 */
4661
4662 pms[0] = ( psk_len >> 8 ) & 0xff;
4663 pms[1] = ( psk_len >> 0 ) & 0xff;
4664 memset( pms + 2, 0, psk_len );
4665 pms[2 + psk_len + 0] = pms[0];
4666 pms[2 + psk_len + 1] = pms[1];
4667 memcpy( pms + 4 + psk_len, psk, psk_len );
4668
Gilles Peskinecbe66502019-05-16 16:59:18 +02004669 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004670 pms, 4 + 2 * psk_len,
4671 hash_alg,
4672 salt, salt_length,
4673 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004674
Gilles Peskine3f108122018-12-07 18:14:53 +01004675 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004676 return( status );
4677}
Janos Follath71a4c912019-06-11 09:14:47 +01004678#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004679#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004680
Janos Follath71a4c912019-06-11 09:14:47 +01004681#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004682/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004683 * to potentially free embedded data structures and wipe confidential data.
4684 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004685static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004686 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004687 const uint8_t *secret, size_t secret_length,
4688 psa_algorithm_t alg,
4689 const uint8_t *salt, size_t salt_length,
4690 const uint8_t *label, size_t label_length,
4691 size_t capacity )
4692{
4693 psa_status_t status;
4694 size_t max_capacity;
4695
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004696 /* Set operation->alg even on failure so that abort knows what to do. */
4697 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004698
4699#if defined(MBEDTLS_MD_C)
4700 if( PSA_ALG_IS_HKDF( alg ) )
4701 {
4702 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4703 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4704 if( hash_size == 0 )
4705 return( PSA_ERROR_NOT_SUPPORTED );
4706 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004707 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004708 secret, secret_length,
4709 hash_alg,
4710 salt, salt_length,
4711 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004712 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004713 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4714 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4715 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004716 {
4717 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4718 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4719
4720 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4721 if( hash_alg != PSA_ALG_SHA_256 &&
4722 hash_alg != PSA_ALG_SHA_384 )
4723 {
4724 return( PSA_ERROR_NOT_SUPPORTED );
4725 }
4726
4727 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004728
4729 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4730 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004731 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004732 secret, secret_length,
4733 hash_alg, salt, salt_length,
4734 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004735 }
4736 else
4737 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004738 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004739 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004740 secret, secret_length,
4741 hash_alg, salt, salt_length,
4742 label, label_length );
4743 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004744 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004745 else
4746#endif
4747 {
4748 return( PSA_ERROR_NOT_SUPPORTED );
4749 }
4750
4751 if( status != PSA_SUCCESS )
4752 return( status );
4753
4754 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004755 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004756 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004757 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004758 else
4759 return( PSA_ERROR_INVALID_ARGUMENT );
4760
4761 return( PSA_SUCCESS );
4762}
Janos Follath71a4c912019-06-11 09:14:47 +01004763#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004764
Janos Follath71a4c912019-06-11 09:14:47 +01004765#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004766psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004767 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004768 psa_algorithm_t alg,
4769 const uint8_t *salt,
4770 size_t salt_length,
4771 const uint8_t *label,
4772 size_t label_length,
4773 size_t capacity )
4774{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004775 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004776 psa_status_t status;
4777
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004778 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004779 return( PSA_ERROR_BAD_STATE );
4780
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004781 /* Make sure that alg is a key derivation algorithm. This prevents
4782 * key selection algorithms, which psa_key_derivation_internal
4783 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004784 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4785 return( PSA_ERROR_INVALID_ARGUMENT );
4786
Gilles Peskinec5487a82018-12-03 18:08:14 +01004787 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004788 if( status != PSA_SUCCESS )
4789 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004790
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004791 if( slot->type != PSA_KEY_TYPE_DERIVE )
4792 return( PSA_ERROR_INVALID_ARGUMENT );
4793
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004794 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004795 slot->data.raw.data,
4796 slot->data.raw.bytes,
4797 alg,
4798 salt, salt_length,
4799 label, label_length,
4800 capacity );
4801 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004802 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004803 return( status );
4804}
Janos Follath71a4c912019-06-11 09:14:47 +01004805#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004806
Gilles Peskine969c5d62019-01-16 15:53:06 +01004807static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004809 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004810{
Janos Follath5fe19732019-06-20 15:09:30 +01004811 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4812 * initialisers for this union leave some bytes unspecified.) */
4813 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4814
Gilles Peskine969c5d62019-01-16 15:53:06 +01004815 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004816#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004817 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4818 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4819 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004820 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004821 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004822 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4823 if( hash_size == 0 )
4824 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004825 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4826 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004827 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004828 {
4829 return( PSA_ERROR_NOT_SUPPORTED );
4830 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004831 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004832 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004833 }
4834#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004835 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004836 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004837}
4838
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004839psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004840 psa_algorithm_t alg )
4841{
4842 psa_status_t status;
4843
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004844 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004845 return( PSA_ERROR_BAD_STATE );
4846
Gilles Peskine6843c292019-01-18 16:44:49 +01004847 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4848 return( PSA_ERROR_INVALID_ARGUMENT );
4849 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004850 {
4851 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004853 }
4854 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4855 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004857 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004858 else
4859 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004860
4861 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004862 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004863 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004864}
4865
4866#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004867static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004868 psa_algorithm_t hash_alg,
4869 psa_key_derivation_step_t step,
4870 const uint8_t *data,
4871 size_t data_length )
4872{
4873 psa_status_t status;
4874 switch( step )
4875 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004876 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004877 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004878 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004879 status = psa_hmac_setup_internal( &hkdf->hmac,
4880 data, data_length,
4881 hash_alg );
4882 if( status != PSA_SUCCESS )
4883 return( status );
4884 hkdf->state = HKDF_STATE_STARTED;
4885 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004886 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004887 /* If no salt was provided, use an empty salt. */
4888 if( hkdf->state == HKDF_STATE_INIT )
4889 {
4890 status = psa_hmac_setup_internal( &hkdf->hmac,
4891 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004892 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004893 if( status != PSA_SUCCESS )
4894 return( status );
4895 hkdf->state = HKDF_STATE_STARTED;
4896 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004897 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004898 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004899 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4900 data, data_length );
4901 if( status != PSA_SUCCESS )
4902 return( status );
4903 status = psa_hmac_finish_internal( &hkdf->hmac,
4904 hkdf->prk,
4905 sizeof( hkdf->prk ) );
4906 if( status != PSA_SUCCESS )
4907 return( status );
4908 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4909 hkdf->block_number = 0;
4910 hkdf->state = HKDF_STATE_KEYED;
4911 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004912 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004913 if( hkdf->state == HKDF_STATE_OUTPUT )
4914 return( PSA_ERROR_BAD_STATE );
4915 if( hkdf->info_set )
4916 return( PSA_ERROR_BAD_STATE );
4917 hkdf->info_length = data_length;
4918 if( data_length != 0 )
4919 {
4920 hkdf->info = mbedtls_calloc( 1, data_length );
4921 if( hkdf->info == NULL )
4922 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4923 memcpy( hkdf->info, data, data_length );
4924 }
4925 hkdf->info_set = 1;
4926 return( PSA_SUCCESS );
4927 default:
4928 return( PSA_ERROR_INVALID_ARGUMENT );
4929 }
4930}
Janos Follathb03233e2019-06-11 15:30:30 +01004931
4932#if defined(PSA_PRE_1_0_KEY_DERIVATION)
4933static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
4934 psa_algorithm_t hash_alg,
4935 psa_key_derivation_step_t step,
4936 const uint8_t *data,
4937 size_t data_length )
4938{
4939 (void) prf;
4940 (void) hash_alg;
4941 (void) step;
4942 (void) data;
4943 (void) data_length;
4944
4945 return( PSA_ERROR_INVALID_ARGUMENT );
4946}
Janos Follath6660f0e2019-06-17 08:44:03 +01004947
4948static psa_status_t psa_tls12_prf_psk_to_ms_input(
4949 psa_tls12_prf_key_derivation_t *prf,
4950 psa_algorithm_t hash_alg,
4951 psa_key_derivation_step_t step,
4952 const uint8_t *data,
4953 size_t data_length )
4954{
4955 (void) prf;
4956 (void) hash_alg;
4957 (void) step;
4958 (void) data;
4959 (void) data_length;
4960
4961 return( PSA_ERROR_INVALID_ARGUMENT );
4962}
Janos Follathb03233e2019-06-11 15:30:30 +01004963#else
Janos Follathf08e2652019-06-13 09:05:41 +01004964static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4965 const uint8_t *data,
4966 size_t data_length )
4967{
4968 if( prf->state != TLS12_PRF_STATE_INIT )
4969 return( PSA_ERROR_BAD_STATE );
4970
Janos Follathd6dce9f2019-07-04 09:11:38 +01004971 if( data_length != 0 )
4972 {
4973 prf->seed = mbedtls_calloc( 1, data_length );
4974 if( prf->seed == NULL )
4975 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004976
Janos Follathd6dce9f2019-07-04 09:11:38 +01004977 memcpy( prf->seed, data, data_length );
4978 prf->seed_length = data_length;
4979 }
Janos Follathf08e2652019-06-13 09:05:41 +01004980
4981 prf->state = TLS12_PRF_STATE_SEED_SET;
4982
4983 return( PSA_SUCCESS );
4984}
4985
Janos Follath81550542019-06-13 14:26:34 +01004986static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4987 psa_algorithm_t hash_alg,
4988 const uint8_t *data,
4989 size_t data_length )
4990{
4991 psa_status_t status;
4992 if( prf->state != TLS12_PRF_STATE_SEED_SET )
4993 return( PSA_ERROR_BAD_STATE );
4994
4995 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4996 if( status != PSA_SUCCESS )
4997 return( status );
4998
4999 prf->state = TLS12_PRF_STATE_KEY_SET;
5000
5001 return( PSA_SUCCESS );
5002}
5003
Janos Follath6660f0e2019-06-17 08:44:03 +01005004static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5005 psa_tls12_prf_key_derivation_t *prf,
5006 psa_algorithm_t hash_alg,
5007 const uint8_t *data,
5008 size_t data_length )
5009{
5010 psa_status_t status;
5011 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Janos Follath40e13932019-06-26 13:22:29 +01005012 unsigned char* cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005013
5014 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5015 return( PSA_ERROR_INVALID_ARGUMENT );
5016
5017 /* Quoting RFC 4279, Section 2:
5018 *
5019 * The premaster secret is formed as follows: if the PSK is N octets
5020 * long, concatenate a uint16 with the value N, N zero octets, a second
5021 * uint16 with the value N, and the PSK itself.
5022 */
5023
Janos Follath40e13932019-06-26 13:22:29 +01005024 *cur++ = ( data_length >> 8 ) & 0xff;
5025 *cur++ = ( data_length >> 0 ) & 0xff;
5026 memset( cur, 0, data_length );
5027 cur += data_length;
5028 *cur++ = pms[0];
5029 *cur++ = pms[1];
5030 memcpy( cur, data, data_length );
5031 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005032
Janos Follath40e13932019-06-26 13:22:29 +01005033 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005034
5035 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5036 return( status );
5037}
5038
Janos Follath63028dd2019-06-13 09:15:47 +01005039static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5040 const uint8_t *data,
5041 size_t data_length )
5042{
5043 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5044 return( PSA_ERROR_BAD_STATE );
5045
Janos Follathd6dce9f2019-07-04 09:11:38 +01005046 if( data_length != 0 )
5047 {
5048 prf->label = mbedtls_calloc( 1, data_length );
5049 if( prf->label == NULL )
5050 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005051
Janos Follathd6dce9f2019-07-04 09:11:38 +01005052 memcpy( prf->label, data, data_length );
5053 prf->label_length = data_length;
5054 }
Janos Follath63028dd2019-06-13 09:15:47 +01005055
5056 prf->state = TLS12_PRF_STATE_LABEL_SET;
5057
5058 return( PSA_SUCCESS );
5059}
5060
Janos Follathb03233e2019-06-11 15:30:30 +01005061static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5062 psa_algorithm_t hash_alg,
5063 psa_key_derivation_step_t step,
5064 const uint8_t *data,
5065 size_t data_length )
5066{
Janos Follathb03233e2019-06-11 15:30:30 +01005067 switch( step )
5068 {
Janos Follathf08e2652019-06-13 09:05:41 +01005069 case PSA_KEY_DERIVATION_INPUT_SEED:
5070 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005071 case PSA_KEY_DERIVATION_INPUT_SECRET:
5072 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005073 case PSA_KEY_DERIVATION_INPUT_LABEL:
5074 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005075 default:
5076 return( PSA_ERROR_INVALID_ARGUMENT );
5077 }
5078}
Janos Follath6660f0e2019-06-17 08:44:03 +01005079
5080static psa_status_t psa_tls12_prf_psk_to_ms_input(
5081 psa_tls12_prf_key_derivation_t *prf,
5082 psa_algorithm_t hash_alg,
5083 psa_key_derivation_step_t step,
5084 const uint8_t *data,
5085 size_t data_length )
5086{
5087 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005088 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005089 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5090 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005091 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005092
5093 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5094}
Janos Follathb03233e2019-06-11 15:30:30 +01005095#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005096#endif /* MBEDTLS_MD_C */
5097
Janos Follathb80a94e2019-06-12 15:54:46 +01005098static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005099 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005100 psa_key_derivation_step_t step,
5101 const uint8_t *data,
5102 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005103{
5104 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005105 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005106
5107#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005108 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005109 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005111 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005112 step, data, data_length );
5113 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005114 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005115#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005116#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005117 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005118 {
Janos Follathb03233e2019-06-11 15:30:30 +01005119 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5120 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5121 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005122 }
5123 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5124 {
5125 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5126 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5127 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005128 }
5129 else
5130#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005131 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005132 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005133 return( PSA_ERROR_BAD_STATE );
5134 }
5135
5136 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005137 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005138 return( status );
5139}
5140
Janos Follath51f4a0f2019-06-14 11:35:55 +01005141psa_status_t psa_key_derivation_input_bytes(
5142 psa_key_derivation_operation_t *operation,
5143 psa_key_derivation_step_t step,
5144 const uint8_t *data,
5145 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005146{
Janos Follathc5621512019-06-14 11:27:57 +01005147 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5148 return( PSA_ERROR_INVALID_ARGUMENT );
5149
5150 return( psa_key_derivation_input_internal( operation, step,
5151 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005152}
5153
Janos Follath51f4a0f2019-06-14 11:35:55 +01005154psa_status_t psa_key_derivation_input_key(
5155 psa_key_derivation_operation_t *operation,
5156 psa_key_derivation_step_t step,
5157 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005158{
5159 psa_key_slot_t *slot;
5160 psa_status_t status;
5161 status = psa_get_key_from_slot( handle, &slot,
5162 PSA_KEY_USAGE_DERIVE,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005163 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005164 if( status != PSA_SUCCESS )
5165 return( status );
5166 if( slot->type != PSA_KEY_TYPE_DERIVE )
5167 return( PSA_ERROR_INVALID_ARGUMENT );
5168 /* Don't allow a key to be used as an input that is usually public.
5169 * This is debatable. It's ok from a cryptographic perspective to
5170 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005171 * the material should be dedicated to a particular input step,
5172 * otherwise this may allow the key to be used in an unintended way
5173 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005174 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005175 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005176 return( psa_key_derivation_input_internal( operation,
5177 step,
5178 slot->data.raw.data,
5179 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005180}
5181
Gilles Peskineea0fb492018-07-12 17:17:20 +02005182
5183
5184/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005185/* Key agreement */
5186/****************************************************************/
5187
Gilles Peskinea05219c2018-11-16 16:02:56 +01005188#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005189static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5190 size_t peer_key_length,
5191 const mbedtls_ecp_keypair *our_key,
5192 uint8_t *shared_secret,
5193 size_t shared_secret_size,
5194 size_t *shared_secret_length )
5195{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005196 mbedtls_ecp_keypair *their_key = NULL;
5197 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005198 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005199 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005200
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005201 status = psa_import_ec_public_key(
5202 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5203 peer_key, peer_key_length,
5204 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005205 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005206 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005207
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005208 status = mbedtls_to_psa_error(
5209 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5210 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005211 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005212 status = mbedtls_to_psa_error(
5213 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5214 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005215 goto exit;
5216
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005217 status = mbedtls_to_psa_error(
5218 mbedtls_ecdh_calc_secret( &ecdh,
5219 shared_secret_length,
5220 shared_secret, shared_secret_size,
5221 mbedtls_ctr_drbg_random,
5222 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005223
5224exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005225 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005226 mbedtls_ecp_keypair_free( their_key );
5227 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005228 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005229}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005230#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005231
Gilles Peskine01d718c2018-09-18 12:01:02 +02005232#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5233
Gilles Peskine0216fe12019-04-11 21:23:21 +02005234static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5235 psa_key_slot_t *private_key,
5236 const uint8_t *peer_key,
5237 size_t peer_key_length,
5238 uint8_t *shared_secret,
5239 size_t shared_secret_size,
5240 size_t *shared_secret_length )
5241{
5242 switch( alg )
5243 {
5244#if defined(MBEDTLS_ECDH_C)
5245 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005246 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005247 return( PSA_ERROR_INVALID_ARGUMENT );
5248 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5249 private_key->data.ecp,
5250 shared_secret, shared_secret_size,
5251 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005252#endif /* MBEDTLS_ECDH_C */
5253 default:
5254 (void) private_key;
5255 (void) peer_key;
5256 (void) peer_key_length;
5257 (void) shared_secret;
5258 (void) shared_secret_size;
5259 (void) shared_secret_length;
5260 return( PSA_ERROR_NOT_SUPPORTED );
5261 }
5262}
5263
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005264/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005265 * to potentially free embedded data structures and wipe confidential data.
5266 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005268 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005269 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005270 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005271 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005272{
5273 psa_status_t status;
5274 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5275 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005276 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005277
5278 /* Step 1: run the secret agreement algorithm to generate the shared
5279 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005280 status = psa_key_agreement_raw_internal( ka_alg,
5281 private_key,
5282 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005283 shared_secret,
5284 sizeof( shared_secret ),
5285 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005286 if( status != PSA_SUCCESS )
5287 goto exit;
5288
5289 /* Step 2: set up the key derivation to generate key material from
5290 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005291 status = psa_key_derivation_input_internal( operation, step,
5292 shared_secret,
5293 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005294
Gilles Peskine01d718c2018-09-18 12:01:02 +02005295exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005296 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005297 return( status );
5298}
5299
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005300psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005301 psa_key_derivation_step_t step,
5302 psa_key_handle_t private_key,
5303 const uint8_t *peer_key,
5304 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005305{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005306 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005307 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005308 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005309 return( PSA_ERROR_INVALID_ARGUMENT );
5310 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005311 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005312 if( status != PSA_SUCCESS )
5313 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005314 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005315 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005316 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005317 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005318 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005319 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005320}
5321
Gilles Peskinebe697d82019-05-16 18:00:41 +02005322psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5323 psa_key_handle_t private_key,
5324 const uint8_t *peer_key,
5325 size_t peer_key_length,
5326 uint8_t *output,
5327 size_t output_size,
5328 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005329{
5330 psa_key_slot_t *slot;
5331 psa_status_t status;
5332
5333 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5334 {
5335 status = PSA_ERROR_INVALID_ARGUMENT;
5336 goto exit;
5337 }
5338 status = psa_get_key_from_slot( private_key, &slot,
5339 PSA_KEY_USAGE_DERIVE, alg );
5340 if( status != PSA_SUCCESS )
5341 goto exit;
5342
5343 status = psa_key_agreement_raw_internal( alg, slot,
5344 peer_key, peer_key_length,
5345 output, output_size,
5346 output_length );
5347
5348exit:
5349 if( status != PSA_SUCCESS )
5350 {
5351 /* If an error happens and is not handled properly, the output
5352 * may be used as a key to protect sensitive data. Arrange for such
5353 * a key to be random, which is likely to result in decryption or
5354 * verification errors. This is better than filling the buffer with
5355 * some constant data such as zeros, which would result in the data
5356 * being protected with a reproducible, easily knowable key.
5357 */
5358 psa_generate_random( output, output_size );
5359 *output_length = output_size;
5360 }
5361 return( status );
5362}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005363
5364
5365/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005366/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005367/****************************************************************/
5368
5369psa_status_t psa_generate_random( uint8_t *output,
5370 size_t output_size )
5371{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005372 int ret;
5373 GUARD_MODULE_INITIALIZED;
5374
5375 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005376 return( mbedtls_to_psa_error( ret ) );
5377}
5378
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005379#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5380#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005381
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005382psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5383 size_t seed_size )
5384{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005385 if( global_data.initialized )
5386 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005387
5388 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5389 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5390 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5391 return( PSA_ERROR_INVALID_ARGUMENT );
5392
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005393 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005394}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005395#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005396
Gilles Peskinee56e8782019-04-26 17:34:02 +02005397#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5398static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5399 size_t domain_parameters_size,
5400 int *exponent )
5401{
5402 size_t i;
5403 uint32_t acc = 0;
5404
5405 if( domain_parameters_size == 0 )
5406 {
5407 *exponent = 65537;
5408 return( PSA_SUCCESS );
5409 }
5410
5411 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5412 * support values that fit in a 32-bit integer, which is larger than
5413 * int on just about every platform anyway. */
5414 if( domain_parameters_size > sizeof( acc ) )
5415 return( PSA_ERROR_NOT_SUPPORTED );
5416 for( i = 0; i < domain_parameters_size; i++ )
5417 acc = ( acc << 8 ) | domain_parameters[i];
5418 if( acc > INT_MAX )
5419 return( PSA_ERROR_NOT_SUPPORTED );
5420 *exponent = acc;
5421 return( PSA_SUCCESS );
5422}
5423#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5424
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005425static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005426 psa_key_slot_t *slot, size_t bits,
5427 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005428{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005429 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005430
Gilles Peskinee56e8782019-04-26 17:34:02 +02005431 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005432 return( PSA_ERROR_INVALID_ARGUMENT );
5433
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005434 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005435 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005436 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005437 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005438 if( status != PSA_SUCCESS )
5439 return( status );
5440 status = psa_generate_random( slot->data.raw.data,
5441 slot->data.raw.bytes );
5442 if( status != PSA_SUCCESS )
5443 {
5444 mbedtls_free( slot->data.raw.data );
5445 return( status );
5446 }
5447#if defined(MBEDTLS_DES_C)
5448 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005449 psa_des_set_key_parity( slot->data.raw.data,
5450 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005451#endif /* MBEDTLS_DES_C */
5452 }
5453 else
5454
5455#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005456 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005457 {
5458 mbedtls_rsa_context *rsa;
5459 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005460 int exponent;
5461 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005462 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5463 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005464 /* Accept only byte-aligned keys, for the same reasons as
5465 * in psa_import_rsa_key(). */
5466 if( bits % 8 != 0 )
5467 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005468 status = psa_read_rsa_exponent( domain_parameters,
5469 domain_parameters_size,
5470 &exponent );
5471 if( status != PSA_SUCCESS )
5472 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005473 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5474 if( rsa == NULL )
5475 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5476 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5477 ret = mbedtls_rsa_gen_key( rsa,
5478 mbedtls_ctr_drbg_random,
5479 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005480 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005481 exponent );
5482 if( ret != 0 )
5483 {
5484 mbedtls_rsa_free( rsa );
5485 mbedtls_free( rsa );
5486 return( mbedtls_to_psa_error( ret ) );
5487 }
5488 slot->data.rsa = rsa;
5489 }
5490 else
5491#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5492
5493#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005494 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005495 {
5496 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5497 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5498 const mbedtls_ecp_curve_info *curve_info =
5499 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5500 mbedtls_ecp_keypair *ecp;
5501 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005502 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005503 return( PSA_ERROR_NOT_SUPPORTED );
5504 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5505 return( PSA_ERROR_NOT_SUPPORTED );
5506 if( curve_info->bit_size != bits )
5507 return( PSA_ERROR_INVALID_ARGUMENT );
5508 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5509 if( ecp == NULL )
5510 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5511 mbedtls_ecp_keypair_init( ecp );
5512 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5513 mbedtls_ctr_drbg_random,
5514 &global_data.ctr_drbg );
5515 if( ret != 0 )
5516 {
5517 mbedtls_ecp_keypair_free( ecp );
5518 mbedtls_free( ecp );
5519 return( mbedtls_to_psa_error( ret ) );
5520 }
5521 slot->data.ecp = ecp;
5522 }
5523 else
5524#endif /* MBEDTLS_ECP_C */
5525
5526 return( PSA_ERROR_NOT_SUPPORTED );
5527
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005528 return( PSA_SUCCESS );
5529}
5530
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005531psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005532 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005533{
5534 psa_status_t status;
5535 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005536 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005537 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005538 if( status == PSA_SUCCESS )
5539 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005540 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005541 slot, attributes->bits,
5542 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005543 }
5544 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005545 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005546 if( status != PSA_SUCCESS )
5547 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005548 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005549 *handle = 0;
5550 }
5551 return( status );
5552}
5553
5554
Gilles Peskine05d69892018-06-19 22:00:52 +02005555
5556/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005557/* Module setup */
5558/****************************************************************/
5559
Gilles Peskine5e769522018-11-20 21:59:56 +01005560psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5561 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5562 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5563{
5564 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5565 return( PSA_ERROR_BAD_STATE );
5566 global_data.entropy_init = entropy_init;
5567 global_data.entropy_free = entropy_free;
5568 return( PSA_SUCCESS );
5569}
5570
Gilles Peskinee59236f2018-01-27 23:32:46 +01005571void mbedtls_psa_crypto_free( void )
5572{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005573 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005574 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5575 {
5576 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005577 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005578 }
5579 /* Wipe all remaining data, including configuration.
5580 * In particular, this sets all state indicator to the value
5581 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005582 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005583#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005584 /* Unregister all secure element drivers, so that we restart from
5585 * a pristine state. */
5586 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005587#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005588}
5589
5590psa_status_t psa_crypto_init( void )
5591{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005592 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005593 const unsigned char drbg_seed[] = "PSA";
5594
Gilles Peskinec6b69072018-11-20 21:42:52 +01005595 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005596 if( global_data.initialized != 0 )
5597 return( PSA_SUCCESS );
5598
Gilles Peskine5e769522018-11-20 21:59:56 +01005599 /* Set default configuration if
5600 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5601 if( global_data.entropy_init == NULL )
5602 global_data.entropy_init = mbedtls_entropy_init;
5603 if( global_data.entropy_free == NULL )
5604 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005605
Gilles Peskinec6b69072018-11-20 21:42:52 +01005606 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005607 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005608 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005609 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005610 status = mbedtls_to_psa_error(
5611 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5612 mbedtls_entropy_func,
5613 &global_data.entropy,
5614 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5615 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005616 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005617 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005618
Gilles Peskine66fb1262018-12-10 16:29:04 +01005619 status = psa_initialize_key_slots( );
5620 if( status != PSA_SUCCESS )
5621 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005622
5623 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005624 global_data.initialized = 1;
5625
Gilles Peskinee59236f2018-01-27 23:32:46 +01005626exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005627 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005628 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005629 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005630}
5631
5632#endif /* MBEDTLS_PSA_CRYPTO_C */