blob: 84b10df3e11376886c78353f4bf758ed4f05b2aa [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
1351 return( status );
1352}
1353
1354/** Finalize the creation of a key once its key material has been set.
1355 *
1356 * This entails writing the key to persistent storage.
1357 *
1358 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001359 * See the documentation of psa_start_key_creation() for the intended use
1360 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001361 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001362 * \param[in,out] slot Pointer to the slot with key material.
1363 * \param[in] driver The secure element driver for the key,
1364 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001365 *
1366 * \retval #PSA_SUCCESS
1367 * The key was successfully created. The handle is now valid.
1368 * \return If this function fails, the key slot is an invalid state.
1369 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001370 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001371static psa_status_t psa_finish_key_creation(
1372 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001373 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001374{
1375 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001376 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001377 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001378
1379#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1380 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1381 {
1382 uint8_t *buffer = NULL;
1383 size_t buffer_size = 0;
1384 size_t length;
1385
1386 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001387 psa_get_key_slot_bits( slot ) );
Gilles Peskine4747d192019-04-17 15:05:45 +02001388 buffer = mbedtls_calloc( 1, buffer_size );
1389 if( buffer == NULL && buffer_size != 0 )
1390 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1391 status = psa_internal_export_key( slot,
1392 buffer, buffer_size, &length,
1393 0 );
1394
1395 if( status == PSA_SUCCESS )
1396 {
1397 status = psa_save_persistent_key( slot->persistent_storage_id,
1398 slot->type, &slot->policy,
1399 buffer, length );
1400 }
1401
1402 if( buffer_size != 0 )
1403 mbedtls_platform_zeroize( buffer, buffer_size );
1404 mbedtls_free( buffer );
1405 }
1406#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1407
1408 return( status );
1409}
1410
1411/** Abort the creation of a key.
1412 *
1413 * You may call this function after calling psa_start_key_creation(),
1414 * or after psa_finish_key_creation() fails. In other circumstances, this
1415 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001416 * See the documentation of psa_start_key_creation() for the intended use
1417 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001418 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001419 * \param[in,out] slot Pointer to the slot with key material.
1420 * \param[in] driver The secure element driver for the key,
1421 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001422 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001423static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001424 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001425{
Gilles Peskine011e4282019-06-26 18:34:38 +02001426 (void) driver;
1427
Gilles Peskine4747d192019-04-17 15:05:45 +02001428 if( slot == NULL )
1429 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001430
1431#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1432 /* TOnogrepDO: If the key has already been created in the secure
1433 * element, and the failure happened later (when saving metadata
1434 * to internal storage), we need to destroy the key in the secure
1435 * element. */
1436#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1437
Gilles Peskine4747d192019-04-17 15:05:45 +02001438 psa_wipe_key_slot( slot );
1439}
1440
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001441static psa_status_t psa_check_key_slot_attributes(
1442 const psa_key_slot_t *slot,
1443 const psa_key_attributes_t *attributes )
1444{
1445 if( attributes->type != 0 )
1446 {
1447 if( attributes->type != slot->type )
1448 return( PSA_ERROR_INVALID_ARGUMENT );
1449 }
1450
1451 if( attributes->domain_parameters_size != 0 )
1452 {
1453#if defined(MBEDTLS_RSA_C)
1454 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1455 {
1456 mbedtls_mpi actual, required;
1457 int ret;
1458 mbedtls_mpi_init( &actual );
1459 mbedtls_mpi_init( &required );
1460 ret = mbedtls_rsa_export( slot->data.rsa,
1461 NULL, NULL, NULL, NULL, &actual );
1462 if( ret != 0 )
1463 goto rsa_exit;
1464 ret = mbedtls_mpi_read_binary( &required,
1465 attributes->domain_parameters,
1466 attributes->domain_parameters_size );
1467 if( ret != 0 )
1468 goto rsa_exit;
1469 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1470 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1471 rsa_exit:
1472 mbedtls_mpi_free( &actual );
1473 mbedtls_mpi_free( &required );
1474 if( ret != 0)
1475 return( mbedtls_to_psa_error( ret ) );
1476 }
1477 else
1478#endif
1479 {
1480 return( PSA_ERROR_INVALID_ARGUMENT );
1481 }
1482 }
1483
1484 if( attributes->bits != 0 )
1485 {
1486 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1487 return( PSA_ERROR_INVALID_ARGUMENT );
1488 }
1489
1490 return( PSA_SUCCESS );
1491}
1492
Gilles Peskine4747d192019-04-17 15:05:45 +02001493psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001494 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001495 size_t data_length,
1496 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001497{
1498 psa_status_t status;
1499 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001500 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001501
Gilles Peskine011e4282019-06-26 18:34:38 +02001502 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001503 if( status != PSA_SUCCESS )
1504 goto exit;
1505
1506 status = psa_import_key_into_slot( slot, data, data_length );
1507 if( status != PSA_SUCCESS )
1508 goto exit;
1509 status = psa_check_key_slot_attributes( slot, attributes );
1510 if( status != PSA_SUCCESS )
1511 goto exit;
1512
Gilles Peskine011e4282019-06-26 18:34:38 +02001513 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001514exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001515 if( status != PSA_SUCCESS )
1516 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001517 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001518 *handle = 0;
1519 }
1520 return( status );
1521}
1522
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001523static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001524 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001525{
1526 psa_status_t status;
1527 uint8_t *buffer = NULL;
1528 size_t buffer_size = 0;
1529 size_t length;
1530
1531 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001532 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001533 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001534 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001535 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001536 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1537 if( status != PSA_SUCCESS )
1538 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001539 target->type = source->type;
1540 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001541
1542exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001543 if( buffer_size != 0 )
1544 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001545 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001546 return( status );
1547}
1548
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001549psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1550 const psa_key_attributes_t *specified_attributes,
1551 psa_key_handle_t *target_handle )
1552{
1553 psa_status_t status;
1554 psa_key_slot_t *source_slot = NULL;
1555 psa_key_slot_t *target_slot = NULL;
1556 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001557 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001558
Gilles Peskinec160d9e2019-05-14 14:32:03 +02001559 status = psa_get_key_from_slot( source_handle, &source_slot,
1560 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001561 if( status != PSA_SUCCESS )
1562 goto exit;
1563
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001564 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1565 if( status != PSA_SUCCESS )
1566 goto exit;
1567
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001568 status = psa_restrict_key_policy( &actual_attributes.policy,
1569 &source_slot->policy );
1570 if( status != PSA_SUCCESS )
1571 goto exit;
1572
1573 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001574 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001575 if( status != PSA_SUCCESS )
1576 goto exit;
1577
1578 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001579 if( status != PSA_SUCCESS )
1580 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001581
Gilles Peskine011e4282019-06-26 18:34:38 +02001582 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001583exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001584 if( status != PSA_SUCCESS )
1585 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001586 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001587 *target_handle = 0;
1588 }
1589 return( status );
1590}
1591
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001592
1593
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001594/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001595/* Message digests */
1596/****************************************************************/
1597
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001598static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001599{
1600 switch( alg )
1601 {
1602#if defined(MBEDTLS_MD2_C)
1603 case PSA_ALG_MD2:
1604 return( &mbedtls_md2_info );
1605#endif
1606#if defined(MBEDTLS_MD4_C)
1607 case PSA_ALG_MD4:
1608 return( &mbedtls_md4_info );
1609#endif
1610#if defined(MBEDTLS_MD5_C)
1611 case PSA_ALG_MD5:
1612 return( &mbedtls_md5_info );
1613#endif
1614#if defined(MBEDTLS_RIPEMD160_C)
1615 case PSA_ALG_RIPEMD160:
1616 return( &mbedtls_ripemd160_info );
1617#endif
1618#if defined(MBEDTLS_SHA1_C)
1619 case PSA_ALG_SHA_1:
1620 return( &mbedtls_sha1_info );
1621#endif
1622#if defined(MBEDTLS_SHA256_C)
1623 case PSA_ALG_SHA_224:
1624 return( &mbedtls_sha224_info );
1625 case PSA_ALG_SHA_256:
1626 return( &mbedtls_sha256_info );
1627#endif
1628#if defined(MBEDTLS_SHA512_C)
1629 case PSA_ALG_SHA_384:
1630 return( &mbedtls_sha384_info );
1631 case PSA_ALG_SHA_512:
1632 return( &mbedtls_sha512_info );
1633#endif
1634 default:
1635 return( NULL );
1636 }
1637}
1638
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001639psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1640{
1641 switch( operation->alg )
1642 {
Gilles Peskine81736312018-06-26 15:04:31 +02001643 case 0:
1644 /* The object has (apparently) been initialized but it is not
1645 * in use. It's ok to call abort on such an object, and there's
1646 * nothing to do. */
1647 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001648#if defined(MBEDTLS_MD2_C)
1649 case PSA_ALG_MD2:
1650 mbedtls_md2_free( &operation->ctx.md2 );
1651 break;
1652#endif
1653#if defined(MBEDTLS_MD4_C)
1654 case PSA_ALG_MD4:
1655 mbedtls_md4_free( &operation->ctx.md4 );
1656 break;
1657#endif
1658#if defined(MBEDTLS_MD5_C)
1659 case PSA_ALG_MD5:
1660 mbedtls_md5_free( &operation->ctx.md5 );
1661 break;
1662#endif
1663#if defined(MBEDTLS_RIPEMD160_C)
1664 case PSA_ALG_RIPEMD160:
1665 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1666 break;
1667#endif
1668#if defined(MBEDTLS_SHA1_C)
1669 case PSA_ALG_SHA_1:
1670 mbedtls_sha1_free( &operation->ctx.sha1 );
1671 break;
1672#endif
1673#if defined(MBEDTLS_SHA256_C)
1674 case PSA_ALG_SHA_224:
1675 case PSA_ALG_SHA_256:
1676 mbedtls_sha256_free( &operation->ctx.sha256 );
1677 break;
1678#endif
1679#if defined(MBEDTLS_SHA512_C)
1680 case PSA_ALG_SHA_384:
1681 case PSA_ALG_SHA_512:
1682 mbedtls_sha512_free( &operation->ctx.sha512 );
1683 break;
1684#endif
1685 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001686 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001687 }
1688 operation->alg = 0;
1689 return( PSA_SUCCESS );
1690}
1691
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001692psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001693 psa_algorithm_t alg )
1694{
1695 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001696
1697 /* A context must be freshly initialized before it can be set up. */
1698 if( operation->alg != 0 )
1699 {
1700 return( PSA_ERROR_BAD_STATE );
1701 }
1702
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001703 switch( alg )
1704 {
1705#if defined(MBEDTLS_MD2_C)
1706 case PSA_ALG_MD2:
1707 mbedtls_md2_init( &operation->ctx.md2 );
1708 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1709 break;
1710#endif
1711#if defined(MBEDTLS_MD4_C)
1712 case PSA_ALG_MD4:
1713 mbedtls_md4_init( &operation->ctx.md4 );
1714 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1715 break;
1716#endif
1717#if defined(MBEDTLS_MD5_C)
1718 case PSA_ALG_MD5:
1719 mbedtls_md5_init( &operation->ctx.md5 );
1720 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1721 break;
1722#endif
1723#if defined(MBEDTLS_RIPEMD160_C)
1724 case PSA_ALG_RIPEMD160:
1725 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1726 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1727 break;
1728#endif
1729#if defined(MBEDTLS_SHA1_C)
1730 case PSA_ALG_SHA_1:
1731 mbedtls_sha1_init( &operation->ctx.sha1 );
1732 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1733 break;
1734#endif
1735#if defined(MBEDTLS_SHA256_C)
1736 case PSA_ALG_SHA_224:
1737 mbedtls_sha256_init( &operation->ctx.sha256 );
1738 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1739 break;
1740 case PSA_ALG_SHA_256:
1741 mbedtls_sha256_init( &operation->ctx.sha256 );
1742 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1743 break;
1744#endif
1745#if defined(MBEDTLS_SHA512_C)
1746 case PSA_ALG_SHA_384:
1747 mbedtls_sha512_init( &operation->ctx.sha512 );
1748 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1749 break;
1750 case PSA_ALG_SHA_512:
1751 mbedtls_sha512_init( &operation->ctx.sha512 );
1752 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1753 break;
1754#endif
1755 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001756 return( PSA_ALG_IS_HASH( alg ) ?
1757 PSA_ERROR_NOT_SUPPORTED :
1758 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001759 }
1760 if( ret == 0 )
1761 operation->alg = alg;
1762 else
1763 psa_hash_abort( operation );
1764 return( mbedtls_to_psa_error( ret ) );
1765}
1766
1767psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1768 const uint8_t *input,
1769 size_t input_length )
1770{
1771 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001772
1773 /* Don't require hash implementations to behave correctly on a
1774 * zero-length input, which may have an invalid pointer. */
1775 if( input_length == 0 )
1776 return( PSA_SUCCESS );
1777
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001778 switch( operation->alg )
1779 {
1780#if defined(MBEDTLS_MD2_C)
1781 case PSA_ALG_MD2:
1782 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1783 input, input_length );
1784 break;
1785#endif
1786#if defined(MBEDTLS_MD4_C)
1787 case PSA_ALG_MD4:
1788 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1789 input, input_length );
1790 break;
1791#endif
1792#if defined(MBEDTLS_MD5_C)
1793 case PSA_ALG_MD5:
1794 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1795 input, input_length );
1796 break;
1797#endif
1798#if defined(MBEDTLS_RIPEMD160_C)
1799 case PSA_ALG_RIPEMD160:
1800 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1801 input, input_length );
1802 break;
1803#endif
1804#if defined(MBEDTLS_SHA1_C)
1805 case PSA_ALG_SHA_1:
1806 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1807 input, input_length );
1808 break;
1809#endif
1810#if defined(MBEDTLS_SHA256_C)
1811 case PSA_ALG_SHA_224:
1812 case PSA_ALG_SHA_256:
1813 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1814 input, input_length );
1815 break;
1816#endif
1817#if defined(MBEDTLS_SHA512_C)
1818 case PSA_ALG_SHA_384:
1819 case PSA_ALG_SHA_512:
1820 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1821 input, input_length );
1822 break;
1823#endif
1824 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001825 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001826 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001827
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001828 if( ret != 0 )
1829 psa_hash_abort( operation );
1830 return( mbedtls_to_psa_error( ret ) );
1831}
1832
1833psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1834 uint8_t *hash,
1835 size_t hash_size,
1836 size_t *hash_length )
1837{
itayzafrir40835d42018-08-02 13:14:17 +03001838 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001839 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001840 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001841
1842 /* Fill the output buffer with something that isn't a valid hash
1843 * (barring an attack on the hash and deliberately-crafted input),
1844 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001845 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001846 /* If hash_size is 0 then hash may be NULL and then the
1847 * call to memset would have undefined behavior. */
1848 if( hash_size != 0 )
1849 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001850
1851 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001852 {
1853 status = PSA_ERROR_BUFFER_TOO_SMALL;
1854 goto exit;
1855 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001856
1857 switch( operation->alg )
1858 {
1859#if defined(MBEDTLS_MD2_C)
1860 case PSA_ALG_MD2:
1861 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1862 break;
1863#endif
1864#if defined(MBEDTLS_MD4_C)
1865 case PSA_ALG_MD4:
1866 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1867 break;
1868#endif
1869#if defined(MBEDTLS_MD5_C)
1870 case PSA_ALG_MD5:
1871 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1872 break;
1873#endif
1874#if defined(MBEDTLS_RIPEMD160_C)
1875 case PSA_ALG_RIPEMD160:
1876 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1877 break;
1878#endif
1879#if defined(MBEDTLS_SHA1_C)
1880 case PSA_ALG_SHA_1:
1881 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1882 break;
1883#endif
1884#if defined(MBEDTLS_SHA256_C)
1885 case PSA_ALG_SHA_224:
1886 case PSA_ALG_SHA_256:
1887 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1888 break;
1889#endif
1890#if defined(MBEDTLS_SHA512_C)
1891 case PSA_ALG_SHA_384:
1892 case PSA_ALG_SHA_512:
1893 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1894 break;
1895#endif
1896 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001897 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001898 }
itayzafrir40835d42018-08-02 13:14:17 +03001899 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001900
itayzafrir40835d42018-08-02 13:14:17 +03001901exit:
1902 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001903 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001904 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001905 return( psa_hash_abort( operation ) );
1906 }
1907 else
1908 {
1909 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001910 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001911 }
1912}
1913
Gilles Peskine2d277862018-06-18 15:41:12 +02001914psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1915 const uint8_t *hash,
1916 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001917{
1918 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1919 size_t actual_hash_length;
1920 psa_status_t status = psa_hash_finish( operation,
1921 actual_hash, sizeof( actual_hash ),
1922 &actual_hash_length );
1923 if( status != PSA_SUCCESS )
1924 return( status );
1925 if( actual_hash_length != hash_length )
1926 return( PSA_ERROR_INVALID_SIGNATURE );
1927 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1928 return( PSA_ERROR_INVALID_SIGNATURE );
1929 return( PSA_SUCCESS );
1930}
1931
Gilles Peskineeb35d782019-01-22 17:56:16 +01001932psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1933 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001934{
1935 if( target_operation->alg != 0 )
1936 return( PSA_ERROR_BAD_STATE );
1937
1938 switch( source_operation->alg )
1939 {
1940 case 0:
1941 return( PSA_ERROR_BAD_STATE );
1942#if defined(MBEDTLS_MD2_C)
1943 case PSA_ALG_MD2:
1944 mbedtls_md2_clone( &target_operation->ctx.md2,
1945 &source_operation->ctx.md2 );
1946 break;
1947#endif
1948#if defined(MBEDTLS_MD4_C)
1949 case PSA_ALG_MD4:
1950 mbedtls_md4_clone( &target_operation->ctx.md4,
1951 &source_operation->ctx.md4 );
1952 break;
1953#endif
1954#if defined(MBEDTLS_MD5_C)
1955 case PSA_ALG_MD5:
1956 mbedtls_md5_clone( &target_operation->ctx.md5,
1957 &source_operation->ctx.md5 );
1958 break;
1959#endif
1960#if defined(MBEDTLS_RIPEMD160_C)
1961 case PSA_ALG_RIPEMD160:
1962 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1963 &source_operation->ctx.ripemd160 );
1964 break;
1965#endif
1966#if defined(MBEDTLS_SHA1_C)
1967 case PSA_ALG_SHA_1:
1968 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1969 &source_operation->ctx.sha1 );
1970 break;
1971#endif
1972#if defined(MBEDTLS_SHA256_C)
1973 case PSA_ALG_SHA_224:
1974 case PSA_ALG_SHA_256:
1975 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1976 &source_operation->ctx.sha256 );
1977 break;
1978#endif
1979#if defined(MBEDTLS_SHA512_C)
1980 case PSA_ALG_SHA_384:
1981 case PSA_ALG_SHA_512:
1982 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1983 &source_operation->ctx.sha512 );
1984 break;
1985#endif
1986 default:
1987 return( PSA_ERROR_NOT_SUPPORTED );
1988 }
1989
1990 target_operation->alg = source_operation->alg;
1991 return( PSA_SUCCESS );
1992}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001993
1994
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001995/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001996/* MAC */
1997/****************************************************************/
1998
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001999static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002000 psa_algorithm_t alg,
2001 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002002 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002003 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002004{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002005 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002006 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002007
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002008 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002009 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002010
Gilles Peskine8c9def32018-02-08 10:02:12 +01002011 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2012 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002013 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002014 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002015 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002016 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002017 mode = MBEDTLS_MODE_STREAM;
2018 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002019 case PSA_ALG_CTR:
2020 mode = MBEDTLS_MODE_CTR;
2021 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002022 case PSA_ALG_CFB:
2023 mode = MBEDTLS_MODE_CFB;
2024 break;
2025 case PSA_ALG_OFB:
2026 mode = MBEDTLS_MODE_OFB;
2027 break;
2028 case PSA_ALG_CBC_NO_PADDING:
2029 mode = MBEDTLS_MODE_CBC;
2030 break;
2031 case PSA_ALG_CBC_PKCS7:
2032 mode = MBEDTLS_MODE_CBC;
2033 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002034 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002035 mode = MBEDTLS_MODE_CCM;
2036 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002037 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002038 mode = MBEDTLS_MODE_GCM;
2039 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002040 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2041 mode = MBEDTLS_MODE_CHACHAPOLY;
2042 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002043 default:
2044 return( NULL );
2045 }
2046 }
2047 else if( alg == PSA_ALG_CMAC )
2048 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002049 else
2050 return( NULL );
2051
2052 switch( key_type )
2053 {
2054 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002055 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002056 break;
2057 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002058 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2059 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002060 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002061 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002062 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002063 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002064 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2065 * but two-key Triple-DES is functionally three-key Triple-DES
2066 * with K1=K3, so that's how we present it to mbedtls. */
2067 if( key_bits == 128 )
2068 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002069 break;
2070 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002071 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002072 break;
2073 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002074 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002075 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002076 case PSA_KEY_TYPE_CHACHA20:
2077 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2078 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002079 default:
2080 return( NULL );
2081 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002082 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002083 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002084
Jaeden Amero23bbb752018-06-26 14:16:54 +01002085 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2086 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002087}
2088
Gilles Peskinea05219c2018-11-16 16:02:56 +01002089#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002090static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002091{
Gilles Peskine2d277862018-06-18 15:41:12 +02002092 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002093 {
2094 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002095 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002096 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002097 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002098 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002099 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002100 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002101 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002102 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002103 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002104 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002105 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002106 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002107 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002108 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002109 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002110 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002111 return( 128 );
2112 default:
2113 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002114 }
2115}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002116#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002117
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002118/* Initialize the MAC operation structure. Once this function has been
2119 * called, psa_mac_abort can run and will do the right thing. */
2120static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2121 psa_algorithm_t alg )
2122{
2123 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2124
2125 operation->alg = alg;
2126 operation->key_set = 0;
2127 operation->iv_set = 0;
2128 operation->iv_required = 0;
2129 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002130 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002131
2132#if defined(MBEDTLS_CMAC_C)
2133 if( alg == PSA_ALG_CMAC )
2134 {
2135 operation->iv_required = 0;
2136 mbedtls_cipher_init( &operation->ctx.cmac );
2137 status = PSA_SUCCESS;
2138 }
2139 else
2140#endif /* MBEDTLS_CMAC_C */
2141#if defined(MBEDTLS_MD_C)
2142 if( PSA_ALG_IS_HMAC( operation->alg ) )
2143 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002144 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2145 operation->ctx.hmac.hash_ctx.alg = 0;
2146 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002147 }
2148 else
2149#endif /* MBEDTLS_MD_C */
2150 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002151 if( ! PSA_ALG_IS_MAC( alg ) )
2152 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002153 }
2154
2155 if( status != PSA_SUCCESS )
2156 memset( operation, 0, sizeof( *operation ) );
2157 return( status );
2158}
2159
Gilles Peskine01126fa2018-07-12 17:04:55 +02002160#if defined(MBEDTLS_MD_C)
2161static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2162{
Gilles Peskine3f108122018-12-07 18:14:53 +01002163 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002164 return( psa_hash_abort( &hmac->hash_ctx ) );
2165}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002166
Janos Follath999f6482019-06-11 12:04:10 +01002167#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002168static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2169{
2170 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2171 memset( hmac, 0, sizeof( *hmac ) );
2172}
Janos Follath999f6482019-06-11 12:04:10 +01002173#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002174#endif /* MBEDTLS_MD_C */
2175
Gilles Peskine8c9def32018-02-08 10:02:12 +01002176psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2177{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002178 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002179 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002180 /* The object has (apparently) been initialized but it is not
2181 * in use. It's ok to call abort on such an object, and there's
2182 * nothing to do. */
2183 return( PSA_SUCCESS );
2184 }
2185 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002186#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002187 if( operation->alg == PSA_ALG_CMAC )
2188 {
2189 mbedtls_cipher_free( &operation->ctx.cmac );
2190 }
2191 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002192#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002193#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002194 if( PSA_ALG_IS_HMAC( operation->alg ) )
2195 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002196 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002197 }
2198 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002199#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002200 {
2201 /* Sanity check (shouldn't happen: operation->alg should
2202 * always have been initialized to a valid value). */
2203 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002204 }
Moran Peker41deec42018-04-04 15:43:05 +03002205
Gilles Peskine8c9def32018-02-08 10:02:12 +01002206 operation->alg = 0;
2207 operation->key_set = 0;
2208 operation->iv_set = 0;
2209 operation->iv_required = 0;
2210 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002211 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002212
Gilles Peskine8c9def32018-02-08 10:02:12 +01002213 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002214
2215bad_state:
2216 /* If abort is called on an uninitialized object, we can't trust
2217 * anything. Wipe the object in case it contains confidential data.
2218 * This may result in a memory leak if a pointer gets overwritten,
2219 * but it's too late to do anything about this. */
2220 memset( operation, 0, sizeof( *operation ) );
2221 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002222}
2223
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002224#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002225static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002226 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002227 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002228 const mbedtls_cipher_info_t *cipher_info )
2229{
2230 int ret;
2231
2232 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002233
2234 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2235 if( ret != 0 )
2236 return( ret );
2237
2238 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2239 slot->data.raw.data,
2240 key_bits );
2241 return( ret );
2242}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002243#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002244
Gilles Peskine248051a2018-06-20 16:09:38 +02002245#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002246static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2247 const uint8_t *key,
2248 size_t key_length,
2249 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002250{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002251 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002252 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002253 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002254 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002255 psa_status_t status;
2256
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002257 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2258 * overflow below. This should never trigger if the hash algorithm
2259 * is implemented correctly. */
2260 /* The size checks against the ipad and opad buffers cannot be written
2261 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2262 * because that triggers -Wlogical-op on GCC 7.3. */
2263 if( block_size > sizeof( ipad ) )
2264 return( PSA_ERROR_NOT_SUPPORTED );
2265 if( block_size > sizeof( hmac->opad ) )
2266 return( PSA_ERROR_NOT_SUPPORTED );
2267 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002268 return( PSA_ERROR_NOT_SUPPORTED );
2269
Gilles Peskined223b522018-06-11 18:12:58 +02002270 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002271 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002272 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002273 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002274 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002275 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002276 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002277 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002278 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002279 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002280 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002281 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002282 }
Gilles Peskine96889972018-07-12 17:07:03 +02002283 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2284 * but it is permitted. It is common when HMAC is used in HKDF, for
2285 * example. Don't call `memcpy` in the 0-length because `key` could be
2286 * an invalid pointer which would make the behavior undefined. */
2287 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002288 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002289
Gilles Peskined223b522018-06-11 18:12:58 +02002290 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2291 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002292 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002293 ipad[i] ^= 0x36;
2294 memset( ipad + key_length, 0x36, block_size - key_length );
2295
2296 /* Copy the key material from ipad to opad, flipping the requisite bits,
2297 * and filling the rest of opad with the requisite constant. */
2298 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002299 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2300 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002301
Gilles Peskine01126fa2018-07-12 17:04:55 +02002302 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002303 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002304 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002305
Gilles Peskine01126fa2018-07-12 17:04:55 +02002306 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002307
2308cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002309 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002310
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002311 return( status );
2312}
Gilles Peskine248051a2018-06-20 16:09:38 +02002313#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002314
Gilles Peskine89167cb2018-07-08 20:12:23 +02002315static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002316 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002317 psa_algorithm_t alg,
2318 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002319{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002320 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002321 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002322 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002323 psa_key_usage_t usage =
2324 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002325 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002326 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002327
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002328 /* A context must be freshly initialized before it can be set up. */
2329 if( operation->alg != 0 )
2330 {
2331 return( PSA_ERROR_BAD_STATE );
2332 }
2333
Gilles Peskined911eb72018-08-14 15:18:45 +02002334 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002335 if( status != PSA_SUCCESS )
2336 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002337 if( is_sign )
2338 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002339
Gilles Peskinec5487a82018-12-03 18:08:14 +01002340 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002341 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002342 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002343 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002344
Gilles Peskine8c9def32018-02-08 10:02:12 +01002345#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002346 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002347 {
2348 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002349 mbedtls_cipher_info_from_psa( full_length_alg,
2350 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002351 int ret;
2352 if( cipher_info == NULL )
2353 {
2354 status = PSA_ERROR_NOT_SUPPORTED;
2355 goto exit;
2356 }
2357 operation->mac_size = cipher_info->block_size;
2358 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2359 status = mbedtls_to_psa_error( ret );
2360 }
2361 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002362#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002363#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002364 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002365 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002366 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002367 if( hash_alg == 0 )
2368 {
2369 status = PSA_ERROR_NOT_SUPPORTED;
2370 goto exit;
2371 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002372
2373 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2374 /* Sanity check. This shouldn't fail on a valid configuration. */
2375 if( operation->mac_size == 0 ||
2376 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2377 {
2378 status = PSA_ERROR_NOT_SUPPORTED;
2379 goto exit;
2380 }
2381
Gilles Peskine01126fa2018-07-12 17:04:55 +02002382 if( slot->type != PSA_KEY_TYPE_HMAC )
2383 {
2384 status = PSA_ERROR_INVALID_ARGUMENT;
2385 goto exit;
2386 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002387
Gilles Peskine01126fa2018-07-12 17:04:55 +02002388 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2389 slot->data.raw.data,
2390 slot->data.raw.bytes,
2391 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002392 }
2393 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002394#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002395 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002396 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002397 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002398 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002399
Gilles Peskined911eb72018-08-14 15:18:45 +02002400 if( truncated == 0 )
2401 {
2402 /* The "normal" case: untruncated algorithm. Nothing to do. */
2403 }
2404 else if( truncated < 4 )
2405 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002406 /* A very short MAC is too short for security since it can be
2407 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2408 * so we make this our minimum, even though 32 bits is still
2409 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002410 status = PSA_ERROR_NOT_SUPPORTED;
2411 }
2412 else if( truncated > operation->mac_size )
2413 {
2414 /* It's impossible to "truncate" to a larger length. */
2415 status = PSA_ERROR_INVALID_ARGUMENT;
2416 }
2417 else
2418 operation->mac_size = truncated;
2419
Gilles Peskinefbfac682018-07-08 20:51:54 +02002420exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002421 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002422 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002423 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002424 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002425 else
2426 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002427 operation->key_set = 1;
2428 }
2429 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430}
2431
Gilles Peskine89167cb2018-07-08 20:12:23 +02002432psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002433 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002434 psa_algorithm_t alg )
2435{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002436 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002437}
2438
2439psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002440 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002441 psa_algorithm_t alg )
2442{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002443 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002444}
2445
Gilles Peskine8c9def32018-02-08 10:02:12 +01002446psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2447 const uint8_t *input,
2448 size_t input_length )
2449{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002450 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002451 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002452 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002454 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002455 operation->has_input = 1;
2456
Gilles Peskine8c9def32018-02-08 10:02:12 +01002457#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002458 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002459 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002460 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2461 input, input_length );
2462 status = mbedtls_to_psa_error( ret );
2463 }
2464 else
2465#endif /* MBEDTLS_CMAC_C */
2466#if defined(MBEDTLS_MD_C)
2467 if( PSA_ALG_IS_HMAC( operation->alg ) )
2468 {
2469 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2470 input_length );
2471 }
2472 else
2473#endif /* MBEDTLS_MD_C */
2474 {
2475 /* This shouldn't happen if `operation` was initialized by
2476 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002477 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002478 }
2479
Gilles Peskinefbfac682018-07-08 20:51:54 +02002480 if( status != PSA_SUCCESS )
2481 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002482 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483}
2484
Gilles Peskine01126fa2018-07-12 17:04:55 +02002485#if defined(MBEDTLS_MD_C)
2486static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2487 uint8_t *mac,
2488 size_t mac_size )
2489{
2490 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2491 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2492 size_t hash_size = 0;
2493 size_t block_size = psa_get_hash_block_size( hash_alg );
2494 psa_status_t status;
2495
Gilles Peskine01126fa2018-07-12 17:04:55 +02002496 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2497 if( status != PSA_SUCCESS )
2498 return( status );
2499 /* From here on, tmp needs to be wiped. */
2500
2501 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2502 if( status != PSA_SUCCESS )
2503 goto exit;
2504
2505 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2506 if( status != PSA_SUCCESS )
2507 goto exit;
2508
2509 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2510 if( status != PSA_SUCCESS )
2511 goto exit;
2512
Gilles Peskined911eb72018-08-14 15:18:45 +02002513 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2514 if( status != PSA_SUCCESS )
2515 goto exit;
2516
2517 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002518
2519exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002520 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002521 return( status );
2522}
2523#endif /* MBEDTLS_MD_C */
2524
mohammad16036df908f2018-04-02 08:34:15 -07002525static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002526 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002527 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002528{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002529 if( ! operation->key_set )
2530 return( PSA_ERROR_BAD_STATE );
2531 if( operation->iv_required && ! operation->iv_set )
2532 return( PSA_ERROR_BAD_STATE );
2533
Gilles Peskine8c9def32018-02-08 10:02:12 +01002534 if( mac_size < operation->mac_size )
2535 return( PSA_ERROR_BUFFER_TOO_SMALL );
2536
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002538 if( operation->alg == PSA_ALG_CMAC )
2539 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002540 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2541 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2542 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002543 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002544 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002545 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002547 else
2548#endif /* MBEDTLS_CMAC_C */
2549#if defined(MBEDTLS_MD_C)
2550 if( PSA_ALG_IS_HMAC( operation->alg ) )
2551 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002552 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002553 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002554 }
2555 else
2556#endif /* MBEDTLS_MD_C */
2557 {
2558 /* This shouldn't happen if `operation` was initialized by
2559 * a setup function. */
2560 return( PSA_ERROR_BAD_STATE );
2561 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002562}
2563
Gilles Peskineacd4be32018-07-08 19:56:25 +02002564psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2565 uint8_t *mac,
2566 size_t mac_size,
2567 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002568{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002569 psa_status_t status;
2570
Jaeden Amero252ef282019-02-15 14:05:35 +00002571 if( operation->alg == 0 )
2572 {
2573 return( PSA_ERROR_BAD_STATE );
2574 }
2575
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002576 /* Fill the output buffer with something that isn't a valid mac
2577 * (barring an attack on the mac and deliberately-crafted input),
2578 * in case the caller doesn't check the return status properly. */
2579 *mac_length = mac_size;
2580 /* If mac_size is 0 then mac may be NULL and then the
2581 * call to memset would have undefined behavior. */
2582 if( mac_size != 0 )
2583 memset( mac, '!', mac_size );
2584
Gilles Peskine89167cb2018-07-08 20:12:23 +02002585 if( ! operation->is_sign )
2586 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002587 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002588 }
mohammad16036df908f2018-04-02 08:34:15 -07002589
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002590 status = psa_mac_finish_internal( operation, mac, mac_size );
2591
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002592 if( status == PSA_SUCCESS )
2593 {
2594 status = psa_mac_abort( operation );
2595 if( status == PSA_SUCCESS )
2596 *mac_length = operation->mac_size;
2597 else
2598 memset( mac, '!', mac_size );
2599 }
2600 else
2601 psa_mac_abort( operation );
2602 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002603}
2604
Gilles Peskineacd4be32018-07-08 19:56:25 +02002605psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2606 const uint8_t *mac,
2607 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608{
Gilles Peskine828ed142018-06-18 23:25:51 +02002609 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002610 psa_status_t status;
2611
Jaeden Amero252ef282019-02-15 14:05:35 +00002612 if( operation->alg == 0 )
2613 {
2614 return( PSA_ERROR_BAD_STATE );
2615 }
2616
Gilles Peskine89167cb2018-07-08 20:12:23 +02002617 if( operation->is_sign )
2618 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002619 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002620 }
2621 if( operation->mac_size != mac_length )
2622 {
2623 status = PSA_ERROR_INVALID_SIGNATURE;
2624 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002625 }
mohammad16036df908f2018-04-02 08:34:15 -07002626
2627 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002628 actual_mac, sizeof( actual_mac ) );
2629
2630 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2631 status = PSA_ERROR_INVALID_SIGNATURE;
2632
2633cleanup:
2634 if( status == PSA_SUCCESS )
2635 status = psa_mac_abort( operation );
2636 else
2637 psa_mac_abort( operation );
2638
Gilles Peskine3f108122018-12-07 18:14:53 +01002639 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002640
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002641 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002642}
2643
2644
Gilles Peskine20035e32018-02-03 22:44:14 +01002645
Gilles Peskine20035e32018-02-03 22:44:14 +01002646/****************************************************************/
2647/* Asymmetric cryptography */
2648/****************************************************************/
2649
Gilles Peskine2b450e32018-06-27 15:42:46 +02002650#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002651/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002652 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002653static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2654 size_t hash_length,
2655 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002656{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002657 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002658 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002659 *md_alg = mbedtls_md_get_type( md_info );
2660
2661 /* The Mbed TLS RSA module uses an unsigned int for hash length
2662 * parameters. Validate that it fits so that we don't risk an
2663 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002664#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002665 if( hash_length > UINT_MAX )
2666 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002667#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002668
2669#if defined(MBEDTLS_PKCS1_V15)
2670 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2671 * must be correct. */
2672 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2673 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002674 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002675 if( md_info == NULL )
2676 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002677 if( mbedtls_md_get_size( md_info ) != hash_length )
2678 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002679 }
2680#endif /* MBEDTLS_PKCS1_V15 */
2681
2682#if defined(MBEDTLS_PKCS1_V21)
2683 /* PSS requires a hash internally. */
2684 if( PSA_ALG_IS_RSA_PSS( alg ) )
2685 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002686 if( md_info == NULL )
2687 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002688 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002689#endif /* MBEDTLS_PKCS1_V21 */
2690
Gilles Peskine61b91d42018-06-08 16:09:36 +02002691 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002692}
2693
Gilles Peskine2b450e32018-06-27 15:42:46 +02002694static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2695 psa_algorithm_t alg,
2696 const uint8_t *hash,
2697 size_t hash_length,
2698 uint8_t *signature,
2699 size_t signature_size,
2700 size_t *signature_length )
2701{
2702 psa_status_t status;
2703 int ret;
2704 mbedtls_md_type_t md_alg;
2705
2706 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2707 if( status != PSA_SUCCESS )
2708 return( status );
2709
Gilles Peskine630a18a2018-06-29 17:49:35 +02002710 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002711 return( PSA_ERROR_BUFFER_TOO_SMALL );
2712
2713#if defined(MBEDTLS_PKCS1_V15)
2714 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2715 {
2716 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2717 MBEDTLS_MD_NONE );
2718 ret = mbedtls_rsa_pkcs1_sign( rsa,
2719 mbedtls_ctr_drbg_random,
2720 &global_data.ctr_drbg,
2721 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002722 md_alg,
2723 (unsigned int) hash_length,
2724 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002725 signature );
2726 }
2727 else
2728#endif /* MBEDTLS_PKCS1_V15 */
2729#if defined(MBEDTLS_PKCS1_V21)
2730 if( PSA_ALG_IS_RSA_PSS( alg ) )
2731 {
2732 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2733 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2734 mbedtls_ctr_drbg_random,
2735 &global_data.ctr_drbg,
2736 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002737 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002738 (unsigned int) hash_length,
2739 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002740 signature );
2741 }
2742 else
2743#endif /* MBEDTLS_PKCS1_V21 */
2744 {
2745 return( PSA_ERROR_INVALID_ARGUMENT );
2746 }
2747
2748 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002749 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002750 return( mbedtls_to_psa_error( ret ) );
2751}
2752
2753static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2754 psa_algorithm_t alg,
2755 const uint8_t *hash,
2756 size_t hash_length,
2757 const uint8_t *signature,
2758 size_t signature_length )
2759{
2760 psa_status_t status;
2761 int ret;
2762 mbedtls_md_type_t md_alg;
2763
2764 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2765 if( status != PSA_SUCCESS )
2766 return( status );
2767
Gilles Peskine630a18a2018-06-29 17:49:35 +02002768 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002769 return( PSA_ERROR_BUFFER_TOO_SMALL );
2770
2771#if defined(MBEDTLS_PKCS1_V15)
2772 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2773 {
2774 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2775 MBEDTLS_MD_NONE );
2776 ret = mbedtls_rsa_pkcs1_verify( rsa,
2777 mbedtls_ctr_drbg_random,
2778 &global_data.ctr_drbg,
2779 MBEDTLS_RSA_PUBLIC,
2780 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002781 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002782 hash,
2783 signature );
2784 }
2785 else
2786#endif /* MBEDTLS_PKCS1_V15 */
2787#if defined(MBEDTLS_PKCS1_V21)
2788 if( PSA_ALG_IS_RSA_PSS( alg ) )
2789 {
2790 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2791 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2792 mbedtls_ctr_drbg_random,
2793 &global_data.ctr_drbg,
2794 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002795 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002796 (unsigned int) hash_length,
2797 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002798 signature );
2799 }
2800 else
2801#endif /* MBEDTLS_PKCS1_V21 */
2802 {
2803 return( PSA_ERROR_INVALID_ARGUMENT );
2804 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002805
2806 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2807 * the rest of the signature is invalid". This has little use in
2808 * practice and PSA doesn't report this distinction. */
2809 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2810 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002811 return( mbedtls_to_psa_error( ret ) );
2812}
2813#endif /* MBEDTLS_RSA_C */
2814
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002815#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002816/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2817 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2818 * (even though these functions don't modify it). */
2819static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2820 psa_algorithm_t alg,
2821 const uint8_t *hash,
2822 size_t hash_length,
2823 uint8_t *signature,
2824 size_t signature_size,
2825 size_t *signature_length )
2826{
2827 int ret;
2828 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002829 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002830 mbedtls_mpi_init( &r );
2831 mbedtls_mpi_init( &s );
2832
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002833 if( signature_size < 2 * curve_bytes )
2834 {
2835 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2836 goto cleanup;
2837 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002838
Gilles Peskinea05219c2018-11-16 16:02:56 +01002839#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002840 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2841 {
2842 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2843 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2844 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2845 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2846 hash, hash_length,
2847 md_alg ) );
2848 }
2849 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002850#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002851 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002852 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002853 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2854 hash, hash_length,
2855 mbedtls_ctr_drbg_random,
2856 &global_data.ctr_drbg ) );
2857 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002858
2859 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2860 signature,
2861 curve_bytes ) );
2862 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2863 signature + curve_bytes,
2864 curve_bytes ) );
2865
2866cleanup:
2867 mbedtls_mpi_free( &r );
2868 mbedtls_mpi_free( &s );
2869 if( ret == 0 )
2870 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002871 return( mbedtls_to_psa_error( ret ) );
2872}
2873
2874static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2875 const uint8_t *hash,
2876 size_t hash_length,
2877 const uint8_t *signature,
2878 size_t signature_length )
2879{
2880 int ret;
2881 mbedtls_mpi r, s;
2882 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2883 mbedtls_mpi_init( &r );
2884 mbedtls_mpi_init( &s );
2885
2886 if( signature_length != 2 * curve_bytes )
2887 return( PSA_ERROR_INVALID_SIGNATURE );
2888
2889 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2890 signature,
2891 curve_bytes ) );
2892 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2893 signature + curve_bytes,
2894 curve_bytes ) );
2895
2896 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2897 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002898
2899cleanup:
2900 mbedtls_mpi_free( &r );
2901 mbedtls_mpi_free( &s );
2902 return( mbedtls_to_psa_error( ret ) );
2903}
2904#endif /* MBEDTLS_ECDSA_C */
2905
Gilles Peskinec5487a82018-12-03 18:08:14 +01002906psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002907 psa_algorithm_t alg,
2908 const uint8_t *hash,
2909 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002910 uint8_t *signature,
2911 size_t signature_size,
2912 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002913{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002914 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002915 psa_status_t status;
2916
2917 *signature_length = signature_size;
2918
Gilles Peskinec5487a82018-12-03 18:08:14 +01002919 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002920 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002921 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02002922 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002923 {
2924 status = PSA_ERROR_INVALID_ARGUMENT;
2925 goto exit;
2926 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002927
Gilles Peskine20035e32018-02-03 22:44:14 +01002928#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02002929 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01002930 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002931 status = psa_rsa_sign( slot->data.rsa,
2932 alg,
2933 hash, hash_length,
2934 signature, signature_size,
2935 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002936 }
2937 else
2938#endif /* defined(MBEDTLS_RSA_C) */
2939#if defined(MBEDTLS_ECP_C)
2940 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2941 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002942#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002943 if(
2944#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2945 PSA_ALG_IS_ECDSA( alg )
2946#else
2947 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2948#endif
2949 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002950 status = psa_ecdsa_sign( slot->data.ecp,
2951 alg,
2952 hash, hash_length,
2953 signature, signature_size,
2954 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002955 else
2956#endif /* defined(MBEDTLS_ECDSA_C) */
2957 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002958 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002959 }
itayzafrir5c753392018-05-08 11:18:38 +03002960 }
2961 else
2962#endif /* defined(MBEDTLS_ECP_C) */
2963 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002964 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002965 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002966
2967exit:
2968 /* Fill the unused part of the output buffer (the whole buffer on error,
2969 * the trailing part on success) with something that isn't a valid mac
2970 * (barring an attack on the mac and deliberately-crafted input),
2971 * in case the caller doesn't check the return status properly. */
2972 if( status == PSA_SUCCESS )
2973 memset( signature + *signature_length, '!',
2974 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002975 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002976 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002977 /* If signature_size is 0 then we have nothing to do. We must not call
2978 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002979 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002980}
2981
Gilles Peskinec5487a82018-12-03 18:08:14 +01002982psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002983 psa_algorithm_t alg,
2984 const uint8_t *hash,
2985 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002986 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002987 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002988{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002989 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002990 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002991
Gilles Peskinec5487a82018-12-03 18:08:14 +01002992 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002993 if( status != PSA_SUCCESS )
2994 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002995
Gilles Peskine61b91d42018-06-08 16:09:36 +02002996#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002997 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002998 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002999 return( psa_rsa_verify( slot->data.rsa,
3000 alg,
3001 hash, hash_length,
3002 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003003 }
3004 else
3005#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003006#if defined(MBEDTLS_ECP_C)
3007 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3008 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003009#if defined(MBEDTLS_ECDSA_C)
3010 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003011 return( psa_ecdsa_verify( slot->data.ecp,
3012 hash, hash_length,
3013 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003014 else
3015#endif /* defined(MBEDTLS_ECDSA_C) */
3016 {
3017 return( PSA_ERROR_INVALID_ARGUMENT );
3018 }
itayzafrir5c753392018-05-08 11:18:38 +03003019 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003020 else
3021#endif /* defined(MBEDTLS_ECP_C) */
3022 {
3023 return( PSA_ERROR_NOT_SUPPORTED );
3024 }
3025}
3026
Gilles Peskine072ac562018-06-30 00:21:29 +02003027#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3028static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3029 mbedtls_rsa_context *rsa )
3030{
3031 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3032 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3033 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3034 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3035}
3036#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3037
Gilles Peskinec5487a82018-12-03 18:08:14 +01003038psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003039 psa_algorithm_t alg,
3040 const uint8_t *input,
3041 size_t input_length,
3042 const uint8_t *salt,
3043 size_t salt_length,
3044 uint8_t *output,
3045 size_t output_size,
3046 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003047{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003048 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003049 psa_status_t status;
3050
Darryl Green5cc689a2018-07-24 15:34:10 +01003051 (void) input;
3052 (void) input_length;
3053 (void) salt;
3054 (void) output;
3055 (void) output_size;
3056
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003057 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003058
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003059 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3060 return( PSA_ERROR_INVALID_ARGUMENT );
3061
Gilles Peskinec5487a82018-12-03 18:08:14 +01003062 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003063 if( status != PSA_SUCCESS )
3064 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003065 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003066 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003067 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003068
3069#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003070 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003071 {
3072 mbedtls_rsa_context *rsa = slot->data.rsa;
3073 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003074 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003075 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003076#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003077 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003078 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003079 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3080 mbedtls_ctr_drbg_random,
3081 &global_data.ctr_drbg,
3082 MBEDTLS_RSA_PUBLIC,
3083 input_length,
3084 input,
3085 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003086 }
3087 else
3088#endif /* MBEDTLS_PKCS1_V15 */
3089#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003090 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003091 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003092 psa_rsa_oaep_set_padding_mode( alg, rsa );
3093 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3094 mbedtls_ctr_drbg_random,
3095 &global_data.ctr_drbg,
3096 MBEDTLS_RSA_PUBLIC,
3097 salt, salt_length,
3098 input_length,
3099 input,
3100 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003101 }
3102 else
3103#endif /* MBEDTLS_PKCS1_V21 */
3104 {
3105 return( PSA_ERROR_INVALID_ARGUMENT );
3106 }
3107 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003108 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003109 return( mbedtls_to_psa_error( ret ) );
3110 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003111 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003112#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003113 {
3114 return( PSA_ERROR_NOT_SUPPORTED );
3115 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003116}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003117
Gilles Peskinec5487a82018-12-03 18:08:14 +01003118psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003119 psa_algorithm_t alg,
3120 const uint8_t *input,
3121 size_t input_length,
3122 const uint8_t *salt,
3123 size_t salt_length,
3124 uint8_t *output,
3125 size_t output_size,
3126 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003127{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003128 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003129 psa_status_t status;
3130
Darryl Green5cc689a2018-07-24 15:34:10 +01003131 (void) input;
3132 (void) input_length;
3133 (void) salt;
3134 (void) output;
3135 (void) output_size;
3136
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003137 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003138
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003139 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3140 return( PSA_ERROR_INVALID_ARGUMENT );
3141
Gilles Peskinec5487a82018-12-03 18:08:14 +01003142 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003143 if( status != PSA_SUCCESS )
3144 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003145 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003146 return( PSA_ERROR_INVALID_ARGUMENT );
3147
3148#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003149 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003150 {
3151 mbedtls_rsa_context *rsa = slot->data.rsa;
3152 int ret;
3153
Gilles Peskine630a18a2018-06-29 17:49:35 +02003154 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003155 return( PSA_ERROR_INVALID_ARGUMENT );
3156
3157#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003158 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003159 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003160 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3161 mbedtls_ctr_drbg_random,
3162 &global_data.ctr_drbg,
3163 MBEDTLS_RSA_PRIVATE,
3164 output_length,
3165 input,
3166 output,
3167 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003168 }
3169 else
3170#endif /* MBEDTLS_PKCS1_V15 */
3171#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003172 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003173 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003174 psa_rsa_oaep_set_padding_mode( alg, rsa );
3175 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3176 mbedtls_ctr_drbg_random,
3177 &global_data.ctr_drbg,
3178 MBEDTLS_RSA_PRIVATE,
3179 salt, salt_length,
3180 output_length,
3181 input,
3182 output,
3183 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003184 }
3185 else
3186#endif /* MBEDTLS_PKCS1_V21 */
3187 {
3188 return( PSA_ERROR_INVALID_ARGUMENT );
3189 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003190
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003191 return( mbedtls_to_psa_error( ret ) );
3192 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003193 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003194#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003195 {
3196 return( PSA_ERROR_NOT_SUPPORTED );
3197 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003198}
Gilles Peskine20035e32018-02-03 22:44:14 +01003199
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003200
3201
mohammad1603503973b2018-03-12 15:59:30 +02003202/****************************************************************/
3203/* Symmetric cryptography */
3204/****************************************************************/
3205
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003206/* Initialize the cipher operation structure. Once this function has been
3207 * called, psa_cipher_abort can run and will do the right thing. */
3208static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3209 psa_algorithm_t alg )
3210{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003211 if( ! PSA_ALG_IS_CIPHER( alg ) )
3212 {
3213 memset( operation, 0, sizeof( *operation ) );
3214 return( PSA_ERROR_INVALID_ARGUMENT );
3215 }
3216
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003217 operation->alg = alg;
3218 operation->key_set = 0;
3219 operation->iv_set = 0;
3220 operation->iv_required = 1;
3221 operation->iv_size = 0;
3222 operation->block_size = 0;
3223 mbedtls_cipher_init( &operation->ctx.cipher );
3224 return( PSA_SUCCESS );
3225}
3226
Gilles Peskinee553c652018-06-04 16:22:46 +02003227static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003228 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003229 psa_algorithm_t alg,
3230 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003231{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003232 int ret = 0;
3233 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003234 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003235 size_t key_bits;
3236 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003237 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3238 PSA_KEY_USAGE_ENCRYPT :
3239 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003240
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003241 /* A context must be freshly initialized before it can be set up. */
3242 if( operation->alg != 0 )
3243 {
3244 return( PSA_ERROR_BAD_STATE );
3245 }
3246
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003247 status = psa_cipher_init( operation, alg );
3248 if( status != PSA_SUCCESS )
3249 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003250
Gilles Peskinec5487a82018-12-03 18:08:14 +01003251 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003252 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003253 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003254 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003255
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003256 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003257 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003258 {
3259 status = PSA_ERROR_NOT_SUPPORTED;
3260 goto exit;
3261 }
mohammad1603503973b2018-03-12 15:59:30 +02003262
mohammad1603503973b2018-03-12 15:59:30 +02003263 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003264 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003265 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003266
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003267#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003268 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003269 {
3270 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3271 unsigned char keys[24];
3272 memcpy( keys, slot->data.raw.data, 16 );
3273 memcpy( keys + 16, slot->data.raw.data, 8 );
3274 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3275 keys,
3276 192, cipher_operation );
3277 }
3278 else
3279#endif
3280 {
3281 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3282 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003283 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003284 }
Moran Peker41deec42018-04-04 15:43:05 +03003285 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003286 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003287
mohammad16038481e742018-03-18 13:57:31 +02003288#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003289 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003290 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003291 case PSA_ALG_CBC_NO_PADDING:
3292 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3293 MBEDTLS_PADDING_NONE );
3294 break;
3295 case PSA_ALG_CBC_PKCS7:
3296 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3297 MBEDTLS_PADDING_PKCS7 );
3298 break;
3299 default:
3300 /* The algorithm doesn't involve padding. */
3301 ret = 0;
3302 break;
3303 }
3304 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003305 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003306#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3307
mohammad1603503973b2018-03-12 15:59:30 +02003308 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003309 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3310 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3311 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003312 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003313 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003314 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003315#if defined(MBEDTLS_CHACHA20_C)
3316 else
3317 if( alg == PSA_ALG_CHACHA20 )
3318 operation->iv_size = 12;
3319#endif
mohammad1603503973b2018-03-12 15:59:30 +02003320
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003321exit:
3322 if( status == 0 )
3323 status = mbedtls_to_psa_error( ret );
3324 if( status != 0 )
3325 psa_cipher_abort( operation );
3326 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003327}
3328
Gilles Peskinefe119512018-07-08 21:39:34 +02003329psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003330 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003331 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003332{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003333 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003334}
3335
Gilles Peskinefe119512018-07-08 21:39:34 +02003336psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003337 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003338 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003339{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003340 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003341}
3342
Gilles Peskinefe119512018-07-08 21:39:34 +02003343psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3344 unsigned char *iv,
3345 size_t iv_size,
3346 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003347{
itayzafrir534bd7c2018-08-02 13:56:32 +03003348 psa_status_t status;
3349 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003350 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003351 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003352 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003353 }
Moran Peker41deec42018-04-04 15:43:05 +03003354 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003355 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003356 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003357 goto exit;
3358 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003359 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3360 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003361 if( ret != 0 )
3362 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003363 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003364 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003365 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003366
mohammad16038481e742018-03-18 13:57:31 +02003367 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003368 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003369
Moran Peker395db872018-05-31 14:07:14 +03003370exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003371 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003372 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003373 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003374}
3375
Gilles Peskinefe119512018-07-08 21:39:34 +02003376psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3377 const unsigned char *iv,
3378 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003379{
itayzafrir534bd7c2018-08-02 13:56:32 +03003380 psa_status_t status;
3381 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003382 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003383 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003384 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003385 }
Moran Pekera28258c2018-05-29 16:25:04 +03003386 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003387 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003388 status = PSA_ERROR_INVALID_ARGUMENT;
3389 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003390 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003391 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3392 status = mbedtls_to_psa_error( ret );
3393exit:
3394 if( status == PSA_SUCCESS )
3395 operation->iv_set = 1;
3396 else
mohammad1603503973b2018-03-12 15:59:30 +02003397 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003398 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003399}
3400
Gilles Peskinee553c652018-06-04 16:22:46 +02003401psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3402 const uint8_t *input,
3403 size_t input_length,
3404 unsigned char *output,
3405 size_t output_size,
3406 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003407{
itayzafrir534bd7c2018-08-02 13:56:32 +03003408 psa_status_t status;
3409 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003410 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003411
3412 if( operation->alg == 0 )
3413 {
3414 return( PSA_ERROR_BAD_STATE );
3415 }
3416
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003417 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003418 {
3419 /* Take the unprocessed partial block left over from previous
3420 * update calls, if any, plus the input to this call. Remove
3421 * the last partial block, if any. You get the data that will be
3422 * output in this call. */
3423 expected_output_size =
3424 ( operation->ctx.cipher.unprocessed_len + input_length )
3425 / operation->block_size * operation->block_size;
3426 }
3427 else
3428 {
3429 expected_output_size = input_length;
3430 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003431
Gilles Peskine89d789c2018-06-04 17:17:16 +02003432 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003433 {
3434 status = PSA_ERROR_BUFFER_TOO_SMALL;
3435 goto exit;
3436 }
mohammad160382759612018-03-12 18:16:40 +02003437
mohammad1603503973b2018-03-12 15:59:30 +02003438 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003439 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003440 status = mbedtls_to_psa_error( ret );
3441exit:
3442 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003443 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003444 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003445}
3446
Gilles Peskinee553c652018-06-04 16:22:46 +02003447psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3448 uint8_t *output,
3449 size_t output_size,
3450 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003451{
David Saadab4ecc272019-02-14 13:48:10 +02003452 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003453 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003454 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003455
mohammad1603503973b2018-03-12 15:59:30 +02003456 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003457 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003458 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003459 }
3460 if( operation->iv_required && ! operation->iv_set )
3461 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003462 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003463 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003464
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003465 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003466 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3467 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003468 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003469 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003470 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003471 }
3472
Janos Follath315b51c2018-07-09 16:04:51 +01003473 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3474 temp_output_buffer,
3475 output_length );
3476 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003477 {
Janos Follath315b51c2018-07-09 16:04:51 +01003478 status = mbedtls_to_psa_error( cipher_ret );
3479 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003480 }
Janos Follath315b51c2018-07-09 16:04:51 +01003481
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003482 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003483 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003484 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003485 memcpy( output, temp_output_buffer, *output_length );
3486 else
3487 {
Janos Follath315b51c2018-07-09 16:04:51 +01003488 status = PSA_ERROR_BUFFER_TOO_SMALL;
3489 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003490 }
mohammad1603503973b2018-03-12 15:59:30 +02003491
Gilles Peskine3f108122018-12-07 18:14:53 +01003492 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003493 status = psa_cipher_abort( operation );
3494
3495 return( status );
3496
3497error:
3498
3499 *output_length = 0;
3500
Gilles Peskine3f108122018-12-07 18:14:53 +01003501 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003502 (void) psa_cipher_abort( operation );
3503
3504 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003505}
3506
Gilles Peskinee553c652018-06-04 16:22:46 +02003507psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3508{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003509 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003510 {
3511 /* The object has (apparently) been initialized but it is not
3512 * in use. It's ok to call abort on such an object, and there's
3513 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003514 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003515 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003516
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003517 /* Sanity check (shouldn't happen: operation->alg should
3518 * always have been initialized to a valid value). */
3519 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3520 return( PSA_ERROR_BAD_STATE );
3521
mohammad1603503973b2018-03-12 15:59:30 +02003522 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003523
Moran Peker41deec42018-04-04 15:43:05 +03003524 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003525 operation->key_set = 0;
3526 operation->iv_set = 0;
3527 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003528 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003529 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003530
Moran Peker395db872018-05-31 14:07:14 +03003531 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003532}
3533
Gilles Peskinea0655c32018-04-30 17:06:50 +02003534
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003535
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003536
mohammad16035955c982018-04-26 00:53:03 +03003537/****************************************************************/
3538/* AEAD */
3539/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003540
Gilles Peskineedf9a652018-08-17 18:11:56 +02003541typedef struct
3542{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003543 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003544 const mbedtls_cipher_info_t *cipher_info;
3545 union
3546 {
3547#if defined(MBEDTLS_CCM_C)
3548 mbedtls_ccm_context ccm;
3549#endif /* MBEDTLS_CCM_C */
3550#if defined(MBEDTLS_GCM_C)
3551 mbedtls_gcm_context gcm;
3552#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003553#if defined(MBEDTLS_CHACHAPOLY_C)
3554 mbedtls_chachapoly_context chachapoly;
3555#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003556 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003557 psa_algorithm_t core_alg;
3558 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003559 uint8_t tag_length;
3560} aead_operation_t;
3561
Gilles Peskine30a9e412019-01-14 18:36:12 +01003562static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003563{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003564 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003565 {
3566#if defined(MBEDTLS_CCM_C)
3567 case PSA_ALG_CCM:
3568 mbedtls_ccm_free( &operation->ctx.ccm );
3569 break;
3570#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003571#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003572 case PSA_ALG_GCM:
3573 mbedtls_gcm_free( &operation->ctx.gcm );
3574 break;
3575#endif /* MBEDTLS_GCM_C */
3576 }
3577}
3578
3579static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003580 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003581 psa_key_usage_t usage,
3582 psa_algorithm_t alg )
3583{
3584 psa_status_t status;
3585 size_t key_bits;
3586 mbedtls_cipher_id_t cipher_id;
3587
Gilles Peskinec5487a82018-12-03 18:08:14 +01003588 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003589 if( status != PSA_SUCCESS )
3590 return( status );
3591
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003592 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003593
3594 operation->cipher_info =
3595 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3596 &cipher_id );
3597 if( operation->cipher_info == NULL )
3598 return( PSA_ERROR_NOT_SUPPORTED );
3599
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003600 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003601 {
3602#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003603 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3604 operation->core_alg = PSA_ALG_CCM;
3605 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003606 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3607 * The call to mbedtls_ccm_encrypt_and_tag or
3608 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003609 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3610 return( PSA_ERROR_INVALID_ARGUMENT );
3611 mbedtls_ccm_init( &operation->ctx.ccm );
3612 status = mbedtls_to_psa_error(
3613 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3614 operation->slot->data.raw.data,
3615 (unsigned int) key_bits ) );
3616 if( status != 0 )
3617 goto cleanup;
3618 break;
3619#endif /* MBEDTLS_CCM_C */
3620
3621#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003622 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3623 operation->core_alg = PSA_ALG_GCM;
3624 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003625 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3626 * The call to mbedtls_gcm_crypt_and_tag or
3627 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003628 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3629 return( PSA_ERROR_INVALID_ARGUMENT );
3630 mbedtls_gcm_init( &operation->ctx.gcm );
3631 status = mbedtls_to_psa_error(
3632 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3633 operation->slot->data.raw.data,
3634 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003635 if( status != 0 )
3636 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003637 break;
3638#endif /* MBEDTLS_GCM_C */
3639
Gilles Peskine26869f22019-05-06 15:25:00 +02003640#if defined(MBEDTLS_CHACHAPOLY_C)
3641 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3642 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3643 operation->full_tag_length = 16;
3644 /* We only support the default tag length. */
3645 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3646 return( PSA_ERROR_NOT_SUPPORTED );
3647 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3648 status = mbedtls_to_psa_error(
3649 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3650 operation->slot->data.raw.data ) );
3651 if( status != 0 )
3652 goto cleanup;
3653 break;
3654#endif /* MBEDTLS_CHACHAPOLY_C */
3655
Gilles Peskineedf9a652018-08-17 18:11:56 +02003656 default:
3657 return( PSA_ERROR_NOT_SUPPORTED );
3658 }
3659
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003660 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3661 {
3662 status = PSA_ERROR_INVALID_ARGUMENT;
3663 goto cleanup;
3664 }
3665 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003666
Gilles Peskineedf9a652018-08-17 18:11:56 +02003667 return( PSA_SUCCESS );
3668
3669cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003670 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003671 return( status );
3672}
3673
Gilles Peskinec5487a82018-12-03 18:08:14 +01003674psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003675 psa_algorithm_t alg,
3676 const uint8_t *nonce,
3677 size_t nonce_length,
3678 const uint8_t *additional_data,
3679 size_t additional_data_length,
3680 const uint8_t *plaintext,
3681 size_t plaintext_length,
3682 uint8_t *ciphertext,
3683 size_t ciphertext_size,
3684 size_t *ciphertext_length )
3685{
mohammad16035955c982018-04-26 00:53:03 +03003686 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003687 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003688 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003689
mohammad1603f08a5502018-06-03 15:05:47 +03003690 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003691
Gilles Peskinec5487a82018-12-03 18:08:14 +01003692 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003693 if( status != PSA_SUCCESS )
3694 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003695
Gilles Peskineedf9a652018-08-17 18:11:56 +02003696 /* For all currently supported modes, the tag is at the end of the
3697 * ciphertext. */
3698 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3699 {
3700 status = PSA_ERROR_BUFFER_TOO_SMALL;
3701 goto exit;
3702 }
3703 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003704
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003705#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003706 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003707 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003708 status = mbedtls_to_psa_error(
3709 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3710 MBEDTLS_GCM_ENCRYPT,
3711 plaintext_length,
3712 nonce, nonce_length,
3713 additional_data, additional_data_length,
3714 plaintext, ciphertext,
3715 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003716 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003717 else
3718#endif /* MBEDTLS_GCM_C */
3719#if defined(MBEDTLS_CCM_C)
3720 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003721 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003722 status = mbedtls_to_psa_error(
3723 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3724 plaintext_length,
3725 nonce, nonce_length,
3726 additional_data,
3727 additional_data_length,
3728 plaintext, ciphertext,
3729 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003730 }
mohammad16035c8845f2018-05-09 05:40:09 -07003731 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003732#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003733#if defined(MBEDTLS_CHACHAPOLY_C)
3734 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3735 {
3736 if( nonce_length != 12 || operation.tag_length != 16 )
3737 {
3738 status = PSA_ERROR_NOT_SUPPORTED;
3739 goto exit;
3740 }
3741 status = mbedtls_to_psa_error(
3742 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3743 plaintext_length,
3744 nonce,
3745 additional_data,
3746 additional_data_length,
3747 plaintext,
3748 ciphertext,
3749 tag ) );
3750 }
3751 else
3752#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003753 {
mohammad1603554faad2018-06-03 15:07:38 +03003754 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003755 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003756
Gilles Peskineedf9a652018-08-17 18:11:56 +02003757 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3758 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003759
Gilles Peskineedf9a652018-08-17 18:11:56 +02003760exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003761 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003762 if( status == PSA_SUCCESS )
3763 *ciphertext_length = plaintext_length + operation.tag_length;
3764 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003765}
3766
Gilles Peskineee652a32018-06-01 19:23:52 +02003767/* Locate the tag in a ciphertext buffer containing the encrypted data
3768 * followed by the tag. Return the length of the part preceding the tag in
3769 * *plaintext_length. This is the size of the plaintext in modes where
3770 * the encrypted data has the same size as the plaintext, such as
3771 * CCM and GCM. */
3772static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3773 const uint8_t *ciphertext,
3774 size_t ciphertext_length,
3775 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003776 const uint8_t **p_tag )
3777{
3778 size_t payload_length;
3779 if( tag_length > ciphertext_length )
3780 return( PSA_ERROR_INVALID_ARGUMENT );
3781 payload_length = ciphertext_length - tag_length;
3782 if( payload_length > plaintext_size )
3783 return( PSA_ERROR_BUFFER_TOO_SMALL );
3784 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003785 return( PSA_SUCCESS );
3786}
3787
Gilles Peskinec5487a82018-12-03 18:08:14 +01003788psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003789 psa_algorithm_t alg,
3790 const uint8_t *nonce,
3791 size_t nonce_length,
3792 const uint8_t *additional_data,
3793 size_t additional_data_length,
3794 const uint8_t *ciphertext,
3795 size_t ciphertext_length,
3796 uint8_t *plaintext,
3797 size_t plaintext_size,
3798 size_t *plaintext_length )
3799{
mohammad16035955c982018-04-26 00:53:03 +03003800 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003801 aead_operation_t operation;
3802 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003803
Gilles Peskineee652a32018-06-01 19:23:52 +02003804 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003805
Gilles Peskinec5487a82018-12-03 18:08:14 +01003806 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003807 if( status != PSA_SUCCESS )
3808 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003809
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003810 status = psa_aead_unpadded_locate_tag( operation.tag_length,
3811 ciphertext, ciphertext_length,
3812 plaintext_size, &tag );
3813 if( status != PSA_SUCCESS )
3814 goto exit;
3815
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003816#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003817 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003818 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003819 status = mbedtls_to_psa_error(
3820 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3821 ciphertext_length - operation.tag_length,
3822 nonce, nonce_length,
3823 additional_data,
3824 additional_data_length,
3825 tag, operation.tag_length,
3826 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003827 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003828 else
3829#endif /* MBEDTLS_GCM_C */
3830#if defined(MBEDTLS_CCM_C)
3831 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003832 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003833 status = mbedtls_to_psa_error(
3834 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3835 ciphertext_length - operation.tag_length,
3836 nonce, nonce_length,
3837 additional_data,
3838 additional_data_length,
3839 ciphertext, plaintext,
3840 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003841 }
mohammad160339574652018-06-01 04:39:53 -07003842 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003843#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003844#if defined(MBEDTLS_CHACHAPOLY_C)
3845 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3846 {
3847 if( nonce_length != 12 || operation.tag_length != 16 )
3848 {
3849 status = PSA_ERROR_NOT_SUPPORTED;
3850 goto exit;
3851 }
3852 status = mbedtls_to_psa_error(
3853 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
3854 ciphertext_length - operation.tag_length,
3855 nonce,
3856 additional_data,
3857 additional_data_length,
3858 tag,
3859 ciphertext,
3860 plaintext ) );
3861 }
3862 else
3863#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07003864 {
mohammad1603554faad2018-06-03 15:07:38 +03003865 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003866 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003867
Gilles Peskineedf9a652018-08-17 18:11:56 +02003868 if( status != PSA_SUCCESS && plaintext_size != 0 )
3869 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003870
Gilles Peskineedf9a652018-08-17 18:11:56 +02003871exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003872 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003873 if( status == PSA_SUCCESS )
3874 *plaintext_length = ciphertext_length - operation.tag_length;
3875 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003876}
3877
Gilles Peskinea0655c32018-04-30 17:06:50 +02003878
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003879
Gilles Peskine20035e32018-02-03 22:44:14 +01003880/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003881/* Generators */
3882/****************************************************************/
3883
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003884#define HKDF_STATE_INIT 0 /* no input yet */
3885#define HKDF_STATE_STARTED 1 /* got salt */
3886#define HKDF_STATE_KEYED 2 /* got key */
3887#define HKDF_STATE_OUTPUT 3 /* output started */
3888
Gilles Peskinecbe66502019-05-16 16:59:18 +02003889static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003890 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01003891{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003892 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
3893 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003894 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003895 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003896}
3897
3898
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003899psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003900{
3901 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003902 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003903 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003904 {
3905 /* The object has (apparently) been initialized but it is not
3906 * in use. It's ok to call abort on such an object, and there's
3907 * nothing to do. */
3908 }
3909 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003910#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003911 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003912 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003913 mbedtls_free( operation->ctx.hkdf.info );
3914 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003915 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003916 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003917 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003918 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003919 {
Janos Follath6a1d2622019-06-11 10:37:28 +01003920#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003921 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003922 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003923 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
3924 operation->ctx.tls12_prf.key_len );
3925 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003926 }
Hanno Becker580fba12018-11-13 20:50:45 +00003927
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003928 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00003929 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003930 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
3931 operation->ctx.tls12_prf.Ai_with_seed_len );
3932 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00003933 }
Janos Follath6a1d2622019-06-11 10:37:28 +01003934#else
3935 if( operation->ctx.tls12_prf.seed != NULL )
3936 {
3937 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
3938 operation->ctx.tls12_prf.seed_length );
3939 mbedtls_free( operation->ctx.tls12_prf.seed );
3940 }
3941
3942 if( operation->ctx.tls12_prf.label != NULL )
3943 {
3944 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
3945 operation->ctx.tls12_prf.label_length );
3946 mbedtls_free( operation->ctx.tls12_prf.label );
3947 }
3948
3949 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
3950
3951 /* We leave the fields Ai and output_block to be erased safely by the
3952 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01003953#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003954 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003955 else
3956#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003957 {
3958 status = PSA_ERROR_BAD_STATE;
3959 }
Janos Follath083036a2019-06-11 10:22:26 +01003960 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003961 return( status );
3962}
3963
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003964psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003965 size_t *capacity)
3966{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003967 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003968 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003969 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003970 return PSA_ERROR_BAD_STATE;
3971 }
3972
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003973 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003974 return( PSA_SUCCESS );
3975}
3976
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003977psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003978 size_t capacity )
3979{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003980 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003981 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003982 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003983 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003984 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003985 return( PSA_SUCCESS );
3986}
3987
Gilles Peskinebef7f142018-07-12 17:22:21 +02003988#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003989/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003990 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003991static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003992 psa_algorithm_t hash_alg,
3993 uint8_t *output,
3994 size_t output_length )
3995{
3996 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3997 psa_status_t status;
3998
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003999 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4000 return( PSA_ERROR_BAD_STATE );
4001 hkdf->state = HKDF_STATE_OUTPUT;
4002
Gilles Peskinebef7f142018-07-12 17:22:21 +02004003 while( output_length != 0 )
4004 {
4005 /* Copy what remains of the current block */
4006 uint8_t n = hash_length - hkdf->offset_in_block;
4007 if( n > output_length )
4008 n = (uint8_t) output_length;
4009 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4010 output += n;
4011 output_length -= n;
4012 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004013 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004014 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004015 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004016 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004017 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004018 * object was corrupted or if this function is called directly
4019 * inside the library. */
4020 if( hkdf->block_number == 0xff )
4021 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004022
4023 /* We need a new block */
4024 ++hkdf->block_number;
4025 hkdf->offset_in_block = 0;
4026 status = psa_hmac_setup_internal( &hkdf->hmac,
4027 hkdf->prk, hash_length,
4028 hash_alg );
4029 if( status != PSA_SUCCESS )
4030 return( status );
4031 if( hkdf->block_number != 1 )
4032 {
4033 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4034 hkdf->output_block,
4035 hash_length );
4036 if( status != PSA_SUCCESS )
4037 return( status );
4038 }
4039 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4040 hkdf->info,
4041 hkdf->info_length );
4042 if( status != PSA_SUCCESS )
4043 return( status );
4044 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4045 &hkdf->block_number, 1 );
4046 if( status != PSA_SUCCESS )
4047 return( status );
4048 status = psa_hmac_finish_internal( &hkdf->hmac,
4049 hkdf->output_block,
4050 sizeof( hkdf->output_block ) );
4051 if( status != PSA_SUCCESS )
4052 return( status );
4053 }
4054
4055 return( PSA_SUCCESS );
4056}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004057
Janos Follath999f6482019-06-11 12:04:10 +01004058#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004059static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4060 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004061 psa_algorithm_t alg )
4062{
4063 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4064 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4065 psa_hmac_internal_data hmac;
4066 psa_status_t status, cleanup_status;
4067
Hanno Becker3b339e22018-11-13 20:56:14 +00004068 unsigned char *Ai;
4069 size_t Ai_len;
4070
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004071 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004072 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004073 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004074 * object was corrupted or if this function is called directly
4075 * inside the library. */
4076 if( tls12_prf->block_number == 0xff )
4077 return( PSA_ERROR_BAD_STATE );
4078
4079 /* We need a new block */
4080 ++tls12_prf->block_number;
4081 tls12_prf->offset_in_block = 0;
4082
4083 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4084 *
4085 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4086 *
4087 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4088 * HMAC_hash(secret, A(2) + seed) +
4089 * HMAC_hash(secret, A(3) + seed) + ...
4090 *
4091 * A(0) = seed
4092 * A(i) = HMAC_hash( secret, A(i-1) )
4093 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004094 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004095 * `HMAC_hash(secret, A(i) + seed)` from which the output
4096 * is currently extracted as `output_block`, while
4097 * `A(i) + seed` is stored in `Ai_with_seed`.
4098 *
4099 * Generating a new block means recalculating `Ai_with_seed`
4100 * from the A(i)-part of it, and afterwards recalculating
4101 * `output_block`.
4102 *
4103 * A(0) is computed at setup time.
4104 *
4105 */
4106
4107 psa_hmac_init_internal( &hmac );
4108
4109 /* We must distinguish the calculation of A(1) from those
4110 * of A(2) and higher, because A(0)=seed has a different
4111 * length than the other A(i). */
4112 if( tls12_prf->block_number == 1 )
4113 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004114 Ai = tls12_prf->Ai_with_seed + hash_length;
4115 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004116 }
4117 else
4118 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004119 Ai = tls12_prf->Ai_with_seed;
4120 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004121 }
4122
Hanno Becker3b339e22018-11-13 20:56:14 +00004123 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4124 status = psa_hmac_setup_internal( &hmac,
4125 tls12_prf->key,
4126 tls12_prf->key_len,
4127 hash_alg );
4128 if( status != PSA_SUCCESS )
4129 goto cleanup;
4130
4131 status = psa_hash_update( &hmac.hash_ctx,
4132 Ai, Ai_len );
4133 if( status != PSA_SUCCESS )
4134 goto cleanup;
4135
4136 status = psa_hmac_finish_internal( &hmac,
4137 tls12_prf->Ai_with_seed,
4138 hash_length );
4139 if( status != PSA_SUCCESS )
4140 goto cleanup;
4141
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004142 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4143 status = psa_hmac_setup_internal( &hmac,
4144 tls12_prf->key,
4145 tls12_prf->key_len,
4146 hash_alg );
4147 if( status != PSA_SUCCESS )
4148 goto cleanup;
4149
4150 status = psa_hash_update( &hmac.hash_ctx,
4151 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004152 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004153 if( status != PSA_SUCCESS )
4154 goto cleanup;
4155
4156 status = psa_hmac_finish_internal( &hmac,
4157 tls12_prf->output_block,
4158 hash_length );
4159 if( status != PSA_SUCCESS )
4160 goto cleanup;
4161
4162cleanup:
4163
4164 cleanup_status = psa_hmac_abort_internal( &hmac );
4165 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4166 status = cleanup_status;
4167
4168 return( status );
4169}
Janos Follath7742fee2019-06-17 12:58:10 +01004170#else
4171static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4172 psa_tls12_prf_key_derivation_t *tls12_prf,
4173 psa_algorithm_t alg )
4174{
4175 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4176 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004177 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4178 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004179
Janos Follath7742fee2019-06-17 12:58:10 +01004180 /* We can't be wanting more output after block 0xff, otherwise
4181 * the capacity check in psa_key_derivation_output_bytes() would have
4182 * prevented this call. It could happen only if the operation
4183 * object was corrupted or if this function is called directly
4184 * inside the library. */
4185 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004186 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004187
4188 /* We need a new block */
4189 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004190 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004191
4192 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4193 *
4194 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4195 *
4196 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4197 * HMAC_hash(secret, A(2) + seed) +
4198 * HMAC_hash(secret, A(3) + seed) + ...
4199 *
4200 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004201 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004202 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004203 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004204 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004205 * is currently extracted as `output_block` and where i is
4206 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004207 */
4208
Janos Follathea29bfb2019-06-19 12:21:20 +01004209 /* Save the hash context before using it, to preserve the hash state with
4210 * only the inner padding in it. We need this, because inner padding depends
4211 * on the key (secret in the RFC's terminology). */
4212 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4213 if( status != PSA_SUCCESS )
4214 goto cleanup;
4215
4216 /* Calculate A(i) where i = tls12_prf->block_number. */
4217 if( tls12_prf->block_number == 1 )
4218 {
4219 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4220 * the variable seed and in this instance means it in the context of the
4221 * P_hash function, where seed = label + seed.) */
4222 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4223 tls12_prf->label, tls12_prf->label_length );
4224 if( status != PSA_SUCCESS )
4225 goto cleanup;
4226 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4227 tls12_prf->seed, tls12_prf->seed_length );
4228 if( status != PSA_SUCCESS )
4229 goto cleanup;
4230 }
4231 else
4232 {
4233 /* A(i) = HMAC_hash(secret, A(i-1)) */
4234 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4235 tls12_prf->Ai, hash_length );
4236 if( status != PSA_SUCCESS )
4237 goto cleanup;
4238 }
4239
4240 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4241 tls12_prf->Ai, hash_length );
4242 if( status != PSA_SUCCESS )
4243 goto cleanup;
4244 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4245 if( status != PSA_SUCCESS )
4246 goto cleanup;
4247
4248 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4249 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4250 tls12_prf->Ai, hash_length );
4251 if( status != PSA_SUCCESS )
4252 goto cleanup;
4253 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4254 tls12_prf->label, tls12_prf->label_length );
4255 if( status != PSA_SUCCESS )
4256 goto cleanup;
4257 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4258 tls12_prf->seed, tls12_prf->seed_length );
4259 if( status != PSA_SUCCESS )
4260 goto cleanup;
4261 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4262 tls12_prf->output_block, hash_length );
4263 if( status != PSA_SUCCESS )
4264 goto cleanup;
4265 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4266 if( status != PSA_SUCCESS )
4267 goto cleanup;
4268
Janos Follath7742fee2019-06-17 12:58:10 +01004269
4270cleanup:
4271
Janos Follathea29bfb2019-06-19 12:21:20 +01004272 cleanup_status = psa_hash_abort( &backup );
4273 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4274 status = cleanup_status;
4275
4276 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004277}
Janos Follath999f6482019-06-11 12:04:10 +01004278#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004279
Janos Follath999f6482019-06-11 12:04:10 +01004280#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004281/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004282 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004283static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004284 psa_tls12_prf_key_derivation_t *tls12_prf,
4285 psa_algorithm_t alg,
4286 uint8_t *output,
4287 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004288{
4289 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4290 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4291 psa_status_t status;
4292
4293 while( output_length != 0 )
4294 {
4295 /* Copy what remains of the current block */
4296 uint8_t n = hash_length - tls12_prf->offset_in_block;
4297
4298 /* Check if we have fully processed the current block. */
4299 if( n == 0 )
4300 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004301 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004302 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004303 if( status != PSA_SUCCESS )
4304 return( status );
4305
4306 continue;
4307 }
4308
4309 if( n > output_length )
4310 n = (uint8_t) output_length;
4311 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4312 n );
4313 output += n;
4314 output_length -= n;
4315 tls12_prf->offset_in_block += n;
4316 }
4317
4318 return( PSA_SUCCESS );
4319}
Janos Follath844eb0e2019-06-19 12:10:49 +01004320#else
4321static psa_status_t psa_key_derivation_tls12_prf_read(
4322 psa_tls12_prf_key_derivation_t *tls12_prf,
4323 psa_algorithm_t alg,
4324 uint8_t *output,
4325 size_t output_length )
4326{
4327 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4328 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4329 psa_status_t status;
4330 uint8_t offset, length;
4331
4332 while( output_length != 0 )
4333 {
4334 /* Check if we have fully processed the current block. */
4335 if( tls12_prf->left_in_block == 0 )
4336 {
4337 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4338 alg );
4339 if( status != PSA_SUCCESS )
4340 return( status );
4341
4342 continue;
4343 }
4344
4345 if( tls12_prf->left_in_block > output_length )
4346 length = (uint8_t) output_length;
4347 else
4348 length = tls12_prf->left_in_block;
4349
4350 offset = hash_length - tls12_prf->left_in_block;
4351 memcpy( output, tls12_prf->output_block + offset, length );
4352 output += length;
4353 output_length -= length;
4354 tls12_prf->left_in_block -= length;
4355 }
4356
4357 return( PSA_SUCCESS );
4358}
Janos Follath999f6482019-06-11 12:04:10 +01004359#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004360#endif /* MBEDTLS_MD_C */
4361
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004362psa_status_t psa_key_derivation_output_bytes(
4363 psa_key_derivation_operation_t *operation,
4364 uint8_t *output,
4365 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004366{
4367 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004368 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004369
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004370 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004371 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004372 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004373 return PSA_ERROR_BAD_STATE;
4374 }
4375
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004376 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004377 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004378 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004379 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004380 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004381 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004382 goto exit;
4383 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004384 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004385 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004386 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004387 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004388 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4389 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004390 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004391 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004392 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004393 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004394 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004395
Gilles Peskinebef7f142018-07-12 17:22:21 +02004396#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004397 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004398 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004399 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004400 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004401 output, output_length );
4402 }
Janos Follathadbec812019-06-14 11:05:39 +01004403 else
Janos Follathadbec812019-06-14 11:05:39 +01004404 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004405 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004406 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004407 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004408 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004409 output_length );
4410 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004411 else
4412#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004413 {
4414 return( PSA_ERROR_BAD_STATE );
4415 }
4416
4417exit:
4418 if( status != PSA_SUCCESS )
4419 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004420 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004421 * This allows us to differentiate between exhausted operations and
4422 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4423 * operations. */
4424 psa_algorithm_t alg = operation->alg;
4425 psa_key_derivation_abort( operation );
4426 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004427 memset( output, '!', output_length );
4428 }
4429 return( status );
4430}
4431
Gilles Peskine08542d82018-07-19 17:05:42 +02004432#if defined(MBEDTLS_DES_C)
4433static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4434{
4435 if( data_size >= 8 )
4436 mbedtls_des_key_set_parity( data );
4437 if( data_size >= 16 )
4438 mbedtls_des_key_set_parity( data + 8 );
4439 if( data_size >= 24 )
4440 mbedtls_des_key_set_parity( data + 16 );
4441}
4442#endif /* MBEDTLS_DES_C */
4443
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004444static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004445 psa_key_slot_t *slot,
4446 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004447 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004448{
4449 uint8_t *data = NULL;
4450 size_t bytes = PSA_BITS_TO_BYTES( bits );
4451 psa_status_t status;
4452
4453 if( ! key_type_is_raw_bytes( slot->type ) )
4454 return( PSA_ERROR_INVALID_ARGUMENT );
4455 if( bits % 8 != 0 )
4456 return( PSA_ERROR_INVALID_ARGUMENT );
4457 data = mbedtls_calloc( 1, bytes );
4458 if( data == NULL )
4459 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4460
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004461 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004462 if( status != PSA_SUCCESS )
4463 goto exit;
4464#if defined(MBEDTLS_DES_C)
4465 if( slot->type == PSA_KEY_TYPE_DES )
4466 psa_des_set_key_parity( data, bytes );
4467#endif /* MBEDTLS_DES_C */
4468 status = psa_import_key_into_slot( slot, data, bytes );
4469
4470exit:
4471 mbedtls_free( data );
4472 return( status );
4473}
4474
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004475psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004476 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004477 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004478{
4479 psa_status_t status;
4480 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004481 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004482 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004483 if( status == PSA_SUCCESS )
4484 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004485 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004486 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004487 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004488 }
4489 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004490 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004491 if( status != PSA_SUCCESS )
4492 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004493 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004494 *handle = 0;
4495 }
4496 return( status );
4497}
4498
Gilles Peskineeab56e42018-07-12 17:12:33 +02004499
4500
4501/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004502/* Key derivation */
4503/****************************************************************/
4504
Gilles Peskinea05219c2018-11-16 16:02:56 +01004505#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004506#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004507/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004508 * of the HKDF algorithm.
4509 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004510 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004511 * to potentially free embedded data structures and wipe confidential data.
4512 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004513static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004514 const uint8_t *secret,
4515 size_t secret_length,
4516 psa_algorithm_t hash_alg,
4517 const uint8_t *salt,
4518 size_t salt_length,
4519 const uint8_t *label,
4520 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004521{
4522 psa_status_t status;
4523 status = psa_hmac_setup_internal( &hkdf->hmac,
4524 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004525 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004526 if( status != PSA_SUCCESS )
4527 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004528 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004529 if( status != PSA_SUCCESS )
4530 return( status );
4531 status = psa_hmac_finish_internal( &hkdf->hmac,
4532 hkdf->prk,
4533 sizeof( hkdf->prk ) );
4534 if( status != PSA_SUCCESS )
4535 return( status );
4536 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4537 hkdf->block_number = 0;
4538 hkdf->info_length = label_length;
4539 if( label_length != 0 )
4540 {
4541 hkdf->info = mbedtls_calloc( 1, label_length );
4542 if( hkdf->info == NULL )
4543 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4544 memcpy( hkdf->info, label, label_length );
4545 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004546 hkdf->state = HKDF_STATE_KEYED;
4547 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004548 return( PSA_SUCCESS );
4549}
Janos Follath71a4c912019-06-11 09:14:47 +01004550#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004551#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004552
Gilles Peskinea05219c2018-11-16 16:02:56 +01004553#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004554#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004555/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004556 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004557 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004558 * to potentially free embedded data structures and wipe confidential data.
4559 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004560static psa_status_t psa_key_derivation_tls12_prf_setup(
4561 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004562 const unsigned char *key,
4563 size_t key_len,
4564 psa_algorithm_t hash_alg,
4565 const uint8_t *salt,
4566 size_t salt_length,
4567 const uint8_t *label,
4568 size_t label_length )
4569{
4570 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004571 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4572 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004573
4574 tls12_prf->key = mbedtls_calloc( 1, key_len );
4575 if( tls12_prf->key == NULL )
4576 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4577 tls12_prf->key_len = key_len;
4578 memcpy( tls12_prf->key, key, key_len );
4579
Hanno Becker580fba12018-11-13 20:50:45 +00004580 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004581 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004582 if( overflow )
4583 return( PSA_ERROR_INVALID_ARGUMENT );
4584
4585 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4586 if( tls12_prf->Ai_with_seed == NULL )
4587 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4588 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4589
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004590 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4591 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004592 if( label_length != 0 )
4593 {
4594 memcpy( tls12_prf->Ai_with_seed + hash_length,
4595 label, label_length );
4596 }
4597
4598 if( salt_length != 0 )
4599 {
4600 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4601 salt, salt_length );
4602 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004603
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004604 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004605 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004606 tls12_prf->block_number = 0;
4607 tls12_prf->offset_in_block = hash_length;
4608
4609 return( PSA_SUCCESS );
4610}
Janos Follath71a4c912019-06-11 09:14:47 +01004611#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004612
Janos Follath71a4c912019-06-11 09:14:47 +01004613#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004614/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004615static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4616 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004617 const unsigned char *psk,
4618 size_t psk_len,
4619 psa_algorithm_t hash_alg,
4620 const uint8_t *salt,
4621 size_t salt_length,
4622 const uint8_t *label,
4623 size_t label_length )
4624{
4625 psa_status_t status;
4626 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4627
4628 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4629 return( PSA_ERROR_INVALID_ARGUMENT );
4630
4631 /* Quoting RFC 4279, Section 2:
4632 *
4633 * The premaster secret is formed as follows: if the PSK is N octets
4634 * long, concatenate a uint16 with the value N, N zero octets, a second
4635 * uint16 with the value N, and the PSK itself.
4636 */
4637
4638 pms[0] = ( psk_len >> 8 ) & 0xff;
4639 pms[1] = ( psk_len >> 0 ) & 0xff;
4640 memset( pms + 2, 0, psk_len );
4641 pms[2 + psk_len + 0] = pms[0];
4642 pms[2 + psk_len + 1] = pms[1];
4643 memcpy( pms + 4 + psk_len, psk, psk_len );
4644
Gilles Peskinecbe66502019-05-16 16:59:18 +02004645 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004646 pms, 4 + 2 * psk_len,
4647 hash_alg,
4648 salt, salt_length,
4649 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004650
Gilles Peskine3f108122018-12-07 18:14:53 +01004651 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004652 return( status );
4653}
Janos Follath71a4c912019-06-11 09:14:47 +01004654#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004655#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004656
Janos Follath71a4c912019-06-11 09:14:47 +01004657#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004658/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004659 * to potentially free embedded data structures and wipe confidential data.
4660 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004661static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004663 const uint8_t *secret, size_t secret_length,
4664 psa_algorithm_t alg,
4665 const uint8_t *salt, size_t salt_length,
4666 const uint8_t *label, size_t label_length,
4667 size_t capacity )
4668{
4669 psa_status_t status;
4670 size_t max_capacity;
4671
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004672 /* Set operation->alg even on failure so that abort knows what to do. */
4673 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004674
4675#if defined(MBEDTLS_MD_C)
4676 if( PSA_ALG_IS_HKDF( alg ) )
4677 {
4678 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4679 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4680 if( hash_size == 0 )
4681 return( PSA_ERROR_NOT_SUPPORTED );
4682 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004683 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004684 secret, secret_length,
4685 hash_alg,
4686 salt, salt_length,
4687 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004688 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004689 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4690 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4691 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004692 {
4693 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4694 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4695
4696 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4697 if( hash_alg != PSA_ALG_SHA_256 &&
4698 hash_alg != PSA_ALG_SHA_384 )
4699 {
4700 return( PSA_ERROR_NOT_SUPPORTED );
4701 }
4702
4703 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004704
4705 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4706 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004707 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004708 secret, secret_length,
4709 hash_alg, salt, salt_length,
4710 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004711 }
4712 else
4713 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004714 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004715 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004716 secret, secret_length,
4717 hash_alg, salt, salt_length,
4718 label, label_length );
4719 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004720 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004721 else
4722#endif
4723 {
4724 return( PSA_ERROR_NOT_SUPPORTED );
4725 }
4726
4727 if( status != PSA_SUCCESS )
4728 return( status );
4729
4730 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004731 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004732 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004733 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004734 else
4735 return( PSA_ERROR_INVALID_ARGUMENT );
4736
4737 return( PSA_SUCCESS );
4738}
Janos Follath71a4c912019-06-11 09:14:47 +01004739#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004740
Janos Follath71a4c912019-06-11 09:14:47 +01004741#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004742psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004743 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004744 psa_algorithm_t alg,
4745 const uint8_t *salt,
4746 size_t salt_length,
4747 const uint8_t *label,
4748 size_t label_length,
4749 size_t capacity )
4750{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004751 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004752 psa_status_t status;
4753
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004754 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004755 return( PSA_ERROR_BAD_STATE );
4756
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004757 /* Make sure that alg is a key derivation algorithm. This prevents
4758 * key selection algorithms, which psa_key_derivation_internal
4759 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004760 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4761 return( PSA_ERROR_INVALID_ARGUMENT );
4762
Gilles Peskinec5487a82018-12-03 18:08:14 +01004763 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004764 if( status != PSA_SUCCESS )
4765 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004766
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004767 if( slot->type != PSA_KEY_TYPE_DERIVE )
4768 return( PSA_ERROR_INVALID_ARGUMENT );
4769
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004770 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004771 slot->data.raw.data,
4772 slot->data.raw.bytes,
4773 alg,
4774 salt, salt_length,
4775 label, label_length,
4776 capacity );
4777 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004778 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004779 return( status );
4780}
Janos Follath71a4c912019-06-11 09:14:47 +01004781#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004782
Gilles Peskine969c5d62019-01-16 15:53:06 +01004783static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004784 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004785 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004786{
Janos Follath5fe19732019-06-20 15:09:30 +01004787 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4788 * initialisers for this union leave some bytes unspecified.) */
4789 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4790
Gilles Peskine969c5d62019-01-16 15:53:06 +01004791 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004792#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004793 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4794 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4795 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004796 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004797 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004798 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4799 if( hash_size == 0 )
4800 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004801 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4802 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004803 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004804 {
4805 return( PSA_ERROR_NOT_SUPPORTED );
4806 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004807 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004808 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004809 }
4810#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004811 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004812 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004813}
4814
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004815psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004816 psa_algorithm_t alg )
4817{
4818 psa_status_t status;
4819
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004820 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004821 return( PSA_ERROR_BAD_STATE );
4822
Gilles Peskine6843c292019-01-18 16:44:49 +01004823 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4824 return( PSA_ERROR_INVALID_ARGUMENT );
4825 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004826 {
4827 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004829 }
4830 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4831 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004832 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004833 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004834 else
4835 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004836
4837 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004838 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004839 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004840}
4841
4842#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004843static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004844 psa_algorithm_t hash_alg,
4845 psa_key_derivation_step_t step,
4846 const uint8_t *data,
4847 size_t data_length )
4848{
4849 psa_status_t status;
4850 switch( step )
4851 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004852 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004853 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004854 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004855 status = psa_hmac_setup_internal( &hkdf->hmac,
4856 data, data_length,
4857 hash_alg );
4858 if( status != PSA_SUCCESS )
4859 return( status );
4860 hkdf->state = HKDF_STATE_STARTED;
4861 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004862 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004863 /* If no salt was provided, use an empty salt. */
4864 if( hkdf->state == HKDF_STATE_INIT )
4865 {
4866 status = psa_hmac_setup_internal( &hkdf->hmac,
4867 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004868 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004869 if( status != PSA_SUCCESS )
4870 return( status );
4871 hkdf->state = HKDF_STATE_STARTED;
4872 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004873 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004874 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004875 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4876 data, data_length );
4877 if( status != PSA_SUCCESS )
4878 return( status );
4879 status = psa_hmac_finish_internal( &hkdf->hmac,
4880 hkdf->prk,
4881 sizeof( hkdf->prk ) );
4882 if( status != PSA_SUCCESS )
4883 return( status );
4884 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4885 hkdf->block_number = 0;
4886 hkdf->state = HKDF_STATE_KEYED;
4887 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004888 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004889 if( hkdf->state == HKDF_STATE_OUTPUT )
4890 return( PSA_ERROR_BAD_STATE );
4891 if( hkdf->info_set )
4892 return( PSA_ERROR_BAD_STATE );
4893 hkdf->info_length = data_length;
4894 if( data_length != 0 )
4895 {
4896 hkdf->info = mbedtls_calloc( 1, data_length );
4897 if( hkdf->info == NULL )
4898 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4899 memcpy( hkdf->info, data, data_length );
4900 }
4901 hkdf->info_set = 1;
4902 return( PSA_SUCCESS );
4903 default:
4904 return( PSA_ERROR_INVALID_ARGUMENT );
4905 }
4906}
Janos Follathb03233e2019-06-11 15:30:30 +01004907
4908#if defined(PSA_PRE_1_0_KEY_DERIVATION)
4909static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
4910 psa_algorithm_t hash_alg,
4911 psa_key_derivation_step_t step,
4912 const uint8_t *data,
4913 size_t data_length )
4914{
4915 (void) prf;
4916 (void) hash_alg;
4917 (void) step;
4918 (void) data;
4919 (void) data_length;
4920
4921 return( PSA_ERROR_INVALID_ARGUMENT );
4922}
Janos Follath6660f0e2019-06-17 08:44:03 +01004923
4924static psa_status_t psa_tls12_prf_psk_to_ms_input(
4925 psa_tls12_prf_key_derivation_t *prf,
4926 psa_algorithm_t hash_alg,
4927 psa_key_derivation_step_t step,
4928 const uint8_t *data,
4929 size_t data_length )
4930{
4931 (void) prf;
4932 (void) hash_alg;
4933 (void) step;
4934 (void) data;
4935 (void) data_length;
4936
4937 return( PSA_ERROR_INVALID_ARGUMENT );
4938}
Janos Follathb03233e2019-06-11 15:30:30 +01004939#else
Janos Follathf08e2652019-06-13 09:05:41 +01004940static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4941 const uint8_t *data,
4942 size_t data_length )
4943{
4944 if( prf->state != TLS12_PRF_STATE_INIT )
4945 return( PSA_ERROR_BAD_STATE );
4946
Janos Follathd6dce9f2019-07-04 09:11:38 +01004947 if( data_length != 0 )
4948 {
4949 prf->seed = mbedtls_calloc( 1, data_length );
4950 if( prf->seed == NULL )
4951 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004952
Janos Follathd6dce9f2019-07-04 09:11:38 +01004953 memcpy( prf->seed, data, data_length );
4954 prf->seed_length = data_length;
4955 }
Janos Follathf08e2652019-06-13 09:05:41 +01004956
4957 prf->state = TLS12_PRF_STATE_SEED_SET;
4958
4959 return( PSA_SUCCESS );
4960}
4961
Janos Follath81550542019-06-13 14:26:34 +01004962static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4963 psa_algorithm_t hash_alg,
4964 const uint8_t *data,
4965 size_t data_length )
4966{
4967 psa_status_t status;
4968 if( prf->state != TLS12_PRF_STATE_SEED_SET )
4969 return( PSA_ERROR_BAD_STATE );
4970
4971 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4972 if( status != PSA_SUCCESS )
4973 return( status );
4974
4975 prf->state = TLS12_PRF_STATE_KEY_SET;
4976
4977 return( PSA_SUCCESS );
4978}
4979
Janos Follath6660f0e2019-06-17 08:44:03 +01004980static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4981 psa_tls12_prf_key_derivation_t *prf,
4982 psa_algorithm_t hash_alg,
4983 const uint8_t *data,
4984 size_t data_length )
4985{
4986 psa_status_t status;
4987 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Janos Follath40e13932019-06-26 13:22:29 +01004988 unsigned char* cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01004989
4990 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4991 return( PSA_ERROR_INVALID_ARGUMENT );
4992
4993 /* Quoting RFC 4279, Section 2:
4994 *
4995 * The premaster secret is formed as follows: if the PSK is N octets
4996 * long, concatenate a uint16 with the value N, N zero octets, a second
4997 * uint16 with the value N, and the PSK itself.
4998 */
4999
Janos Follath40e13932019-06-26 13:22:29 +01005000 *cur++ = ( data_length >> 8 ) & 0xff;
5001 *cur++ = ( data_length >> 0 ) & 0xff;
5002 memset( cur, 0, data_length );
5003 cur += data_length;
5004 *cur++ = pms[0];
5005 *cur++ = pms[1];
5006 memcpy( cur, data, data_length );
5007 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005008
Janos Follath40e13932019-06-26 13:22:29 +01005009 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005010
5011 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5012 return( status );
5013}
5014
Janos Follath63028dd2019-06-13 09:15:47 +01005015static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5016 const uint8_t *data,
5017 size_t data_length )
5018{
5019 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5020 return( PSA_ERROR_BAD_STATE );
5021
Janos Follathd6dce9f2019-07-04 09:11:38 +01005022 if( data_length != 0 )
5023 {
5024 prf->label = mbedtls_calloc( 1, data_length );
5025 if( prf->label == NULL )
5026 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005027
Janos Follathd6dce9f2019-07-04 09:11:38 +01005028 memcpy( prf->label, data, data_length );
5029 prf->label_length = data_length;
5030 }
Janos Follath63028dd2019-06-13 09:15:47 +01005031
5032 prf->state = TLS12_PRF_STATE_LABEL_SET;
5033
5034 return( PSA_SUCCESS );
5035}
5036
Janos Follathb03233e2019-06-11 15:30:30 +01005037static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5038 psa_algorithm_t hash_alg,
5039 psa_key_derivation_step_t step,
5040 const uint8_t *data,
5041 size_t data_length )
5042{
Janos Follathb03233e2019-06-11 15:30:30 +01005043 switch( step )
5044 {
Janos Follathf08e2652019-06-13 09:05:41 +01005045 case PSA_KEY_DERIVATION_INPUT_SEED:
5046 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005047 case PSA_KEY_DERIVATION_INPUT_SECRET:
5048 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005049 case PSA_KEY_DERIVATION_INPUT_LABEL:
5050 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005051 default:
5052 return( PSA_ERROR_INVALID_ARGUMENT );
5053 }
5054}
Janos Follath6660f0e2019-06-17 08:44:03 +01005055
5056static psa_status_t psa_tls12_prf_psk_to_ms_input(
5057 psa_tls12_prf_key_derivation_t *prf,
5058 psa_algorithm_t hash_alg,
5059 psa_key_derivation_step_t step,
5060 const uint8_t *data,
5061 size_t data_length )
5062{
5063 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005064 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005065 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5066 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005067 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005068
5069 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5070}
Janos Follathb03233e2019-06-11 15:30:30 +01005071#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005072#endif /* MBEDTLS_MD_C */
5073
Janos Follathb80a94e2019-06-12 15:54:46 +01005074static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005075 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005076 psa_key_derivation_step_t step,
5077 const uint8_t *data,
5078 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005079{
5080 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005081 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005082
5083#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005084 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005085 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005086 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005087 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005088 step, data, data_length );
5089 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005090 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005091#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005092#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005093 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005094 {
Janos Follathb03233e2019-06-11 15:30:30 +01005095 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5096 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5097 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005098 }
5099 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5100 {
5101 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5102 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5103 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005104 }
5105 else
5106#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005107 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005109 return( PSA_ERROR_BAD_STATE );
5110 }
5111
5112 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005113 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005114 return( status );
5115}
5116
Janos Follath51f4a0f2019-06-14 11:35:55 +01005117psa_status_t psa_key_derivation_input_bytes(
5118 psa_key_derivation_operation_t *operation,
5119 psa_key_derivation_step_t step,
5120 const uint8_t *data,
5121 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005122{
Janos Follathc5621512019-06-14 11:27:57 +01005123 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5124 return( PSA_ERROR_INVALID_ARGUMENT );
5125
5126 return( psa_key_derivation_input_internal( operation, step,
5127 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005128}
5129
Janos Follath51f4a0f2019-06-14 11:35:55 +01005130psa_status_t psa_key_derivation_input_key(
5131 psa_key_derivation_operation_t *operation,
5132 psa_key_derivation_step_t step,
5133 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005134{
5135 psa_key_slot_t *slot;
5136 psa_status_t status;
5137 status = psa_get_key_from_slot( handle, &slot,
5138 PSA_KEY_USAGE_DERIVE,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005139 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005140 if( status != PSA_SUCCESS )
5141 return( status );
5142 if( slot->type != PSA_KEY_TYPE_DERIVE )
5143 return( PSA_ERROR_INVALID_ARGUMENT );
5144 /* Don't allow a key to be used as an input that is usually public.
5145 * This is debatable. It's ok from a cryptographic perspective to
5146 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005147 * the material should be dedicated to a particular input step,
5148 * otherwise this may allow the key to be used in an unintended way
5149 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005150 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005151 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005152 return( psa_key_derivation_input_internal( operation,
5153 step,
5154 slot->data.raw.data,
5155 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005156}
5157
Gilles Peskineea0fb492018-07-12 17:17:20 +02005158
5159
5160/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005161/* Key agreement */
5162/****************************************************************/
5163
Gilles Peskinea05219c2018-11-16 16:02:56 +01005164#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005165static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5166 size_t peer_key_length,
5167 const mbedtls_ecp_keypair *our_key,
5168 uint8_t *shared_secret,
5169 size_t shared_secret_size,
5170 size_t *shared_secret_length )
5171{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005172 mbedtls_ecp_keypair *their_key = NULL;
5173 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005174 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005175 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005176
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005177 status = psa_import_ec_public_key(
5178 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5179 peer_key, peer_key_length,
5180 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005181 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005182 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005183
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005184 status = mbedtls_to_psa_error(
5185 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5186 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005187 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005188 status = mbedtls_to_psa_error(
5189 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5190 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005191 goto exit;
5192
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005193 status = mbedtls_to_psa_error(
5194 mbedtls_ecdh_calc_secret( &ecdh,
5195 shared_secret_length,
5196 shared_secret, shared_secret_size,
5197 mbedtls_ctr_drbg_random,
5198 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005199
5200exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005201 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005202 mbedtls_ecp_keypair_free( their_key );
5203 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005204 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005205}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005206#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005207
Gilles Peskine01d718c2018-09-18 12:01:02 +02005208#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5209
Gilles Peskine0216fe12019-04-11 21:23:21 +02005210static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5211 psa_key_slot_t *private_key,
5212 const uint8_t *peer_key,
5213 size_t peer_key_length,
5214 uint8_t *shared_secret,
5215 size_t shared_secret_size,
5216 size_t *shared_secret_length )
5217{
5218 switch( alg )
5219 {
5220#if defined(MBEDTLS_ECDH_C)
5221 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005222 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005223 return( PSA_ERROR_INVALID_ARGUMENT );
5224 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5225 private_key->data.ecp,
5226 shared_secret, shared_secret_size,
5227 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005228#endif /* MBEDTLS_ECDH_C */
5229 default:
5230 (void) private_key;
5231 (void) peer_key;
5232 (void) peer_key_length;
5233 (void) shared_secret;
5234 (void) shared_secret_size;
5235 (void) shared_secret_length;
5236 return( PSA_ERROR_NOT_SUPPORTED );
5237 }
5238}
5239
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005240/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005241 * to potentially free embedded data structures and wipe confidential data.
5242 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005243static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005244 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005245 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005246 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005247 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005248{
5249 psa_status_t status;
5250 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5251 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005252 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005253
5254 /* Step 1: run the secret agreement algorithm to generate the shared
5255 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005256 status = psa_key_agreement_raw_internal( ka_alg,
5257 private_key,
5258 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005259 shared_secret,
5260 sizeof( shared_secret ),
5261 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005262 if( status != PSA_SUCCESS )
5263 goto exit;
5264
5265 /* Step 2: set up the key derivation to generate key material from
5266 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005267 status = psa_key_derivation_input_internal( operation, step,
5268 shared_secret,
5269 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005270
Gilles Peskine01d718c2018-09-18 12:01:02 +02005271exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005272 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005273 return( status );
5274}
5275
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005276psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005277 psa_key_derivation_step_t step,
5278 psa_key_handle_t private_key,
5279 const uint8_t *peer_key,
5280 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005281{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005282 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005283 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005284 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005285 return( PSA_ERROR_INVALID_ARGUMENT );
5286 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005288 if( status != PSA_SUCCESS )
5289 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005290 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005291 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005292 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005293 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005294 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005295 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005296}
5297
Gilles Peskinebe697d82019-05-16 18:00:41 +02005298psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5299 psa_key_handle_t private_key,
5300 const uint8_t *peer_key,
5301 size_t peer_key_length,
5302 uint8_t *output,
5303 size_t output_size,
5304 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005305{
5306 psa_key_slot_t *slot;
5307 psa_status_t status;
5308
5309 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5310 {
5311 status = PSA_ERROR_INVALID_ARGUMENT;
5312 goto exit;
5313 }
5314 status = psa_get_key_from_slot( private_key, &slot,
5315 PSA_KEY_USAGE_DERIVE, alg );
5316 if( status != PSA_SUCCESS )
5317 goto exit;
5318
5319 status = psa_key_agreement_raw_internal( alg, slot,
5320 peer_key, peer_key_length,
5321 output, output_size,
5322 output_length );
5323
5324exit:
5325 if( status != PSA_SUCCESS )
5326 {
5327 /* If an error happens and is not handled properly, the output
5328 * may be used as a key to protect sensitive data. Arrange for such
5329 * a key to be random, which is likely to result in decryption or
5330 * verification errors. This is better than filling the buffer with
5331 * some constant data such as zeros, which would result in the data
5332 * being protected with a reproducible, easily knowable key.
5333 */
5334 psa_generate_random( output, output_size );
5335 *output_length = output_size;
5336 }
5337 return( status );
5338}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005339
5340
5341/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005342/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005343/****************************************************************/
5344
5345psa_status_t psa_generate_random( uint8_t *output,
5346 size_t output_size )
5347{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005348 int ret;
5349 GUARD_MODULE_INITIALIZED;
5350
5351 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005352 return( mbedtls_to_psa_error( ret ) );
5353}
5354
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005355#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5356#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005357
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005358psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5359 size_t seed_size )
5360{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005361 if( global_data.initialized )
5362 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005363
5364 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5365 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5366 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5367 return( PSA_ERROR_INVALID_ARGUMENT );
5368
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005369 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005370}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005371#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005372
Gilles Peskinee56e8782019-04-26 17:34:02 +02005373#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5374static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5375 size_t domain_parameters_size,
5376 int *exponent )
5377{
5378 size_t i;
5379 uint32_t acc = 0;
5380
5381 if( domain_parameters_size == 0 )
5382 {
5383 *exponent = 65537;
5384 return( PSA_SUCCESS );
5385 }
5386
5387 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5388 * support values that fit in a 32-bit integer, which is larger than
5389 * int on just about every platform anyway. */
5390 if( domain_parameters_size > sizeof( acc ) )
5391 return( PSA_ERROR_NOT_SUPPORTED );
5392 for( i = 0; i < domain_parameters_size; i++ )
5393 acc = ( acc << 8 ) | domain_parameters[i];
5394 if( acc > INT_MAX )
5395 return( PSA_ERROR_NOT_SUPPORTED );
5396 *exponent = acc;
5397 return( PSA_SUCCESS );
5398}
5399#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5400
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005401static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005402 psa_key_slot_t *slot, size_t bits,
5403 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005404{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005405 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005406
Gilles Peskinee56e8782019-04-26 17:34:02 +02005407 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005408 return( PSA_ERROR_INVALID_ARGUMENT );
5409
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005410 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005411 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005412 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005413 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005414 if( status != PSA_SUCCESS )
5415 return( status );
5416 status = psa_generate_random( slot->data.raw.data,
5417 slot->data.raw.bytes );
5418 if( status != PSA_SUCCESS )
5419 {
5420 mbedtls_free( slot->data.raw.data );
5421 return( status );
5422 }
5423#if defined(MBEDTLS_DES_C)
5424 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005425 psa_des_set_key_parity( slot->data.raw.data,
5426 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005427#endif /* MBEDTLS_DES_C */
5428 }
5429 else
5430
5431#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005432 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005433 {
5434 mbedtls_rsa_context *rsa;
5435 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005436 int exponent;
5437 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005438 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5439 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005440 /* Accept only byte-aligned keys, for the same reasons as
5441 * in psa_import_rsa_key(). */
5442 if( bits % 8 != 0 )
5443 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005444 status = psa_read_rsa_exponent( domain_parameters,
5445 domain_parameters_size,
5446 &exponent );
5447 if( status != PSA_SUCCESS )
5448 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005449 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5450 if( rsa == NULL )
5451 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5452 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5453 ret = mbedtls_rsa_gen_key( rsa,
5454 mbedtls_ctr_drbg_random,
5455 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005456 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005457 exponent );
5458 if( ret != 0 )
5459 {
5460 mbedtls_rsa_free( rsa );
5461 mbedtls_free( rsa );
5462 return( mbedtls_to_psa_error( ret ) );
5463 }
5464 slot->data.rsa = rsa;
5465 }
5466 else
5467#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5468
5469#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005470 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005471 {
5472 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5473 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5474 const mbedtls_ecp_curve_info *curve_info =
5475 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5476 mbedtls_ecp_keypair *ecp;
5477 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005478 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005479 return( PSA_ERROR_NOT_SUPPORTED );
5480 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5481 return( PSA_ERROR_NOT_SUPPORTED );
5482 if( curve_info->bit_size != bits )
5483 return( PSA_ERROR_INVALID_ARGUMENT );
5484 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5485 if( ecp == NULL )
5486 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5487 mbedtls_ecp_keypair_init( ecp );
5488 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5489 mbedtls_ctr_drbg_random,
5490 &global_data.ctr_drbg );
5491 if( ret != 0 )
5492 {
5493 mbedtls_ecp_keypair_free( ecp );
5494 mbedtls_free( ecp );
5495 return( mbedtls_to_psa_error( ret ) );
5496 }
5497 slot->data.ecp = ecp;
5498 }
5499 else
5500#endif /* MBEDTLS_ECP_C */
5501
5502 return( PSA_ERROR_NOT_SUPPORTED );
5503
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005504 return( PSA_SUCCESS );
5505}
5506
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005507psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005508 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005509{
5510 psa_status_t status;
5511 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005512 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005513 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005514 if( status == PSA_SUCCESS )
5515 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005516 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005517 slot, attributes->bits,
5518 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005519 }
5520 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005521 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005522 if( status != PSA_SUCCESS )
5523 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005524 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005525 *handle = 0;
5526 }
5527 return( status );
5528}
5529
5530
Gilles Peskine05d69892018-06-19 22:00:52 +02005531
5532/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005533/* Module setup */
5534/****************************************************************/
5535
Gilles Peskine5e769522018-11-20 21:59:56 +01005536psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5537 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5538 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5539{
5540 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5541 return( PSA_ERROR_BAD_STATE );
5542 global_data.entropy_init = entropy_init;
5543 global_data.entropy_free = entropy_free;
5544 return( PSA_SUCCESS );
5545}
5546
Gilles Peskinee59236f2018-01-27 23:32:46 +01005547void mbedtls_psa_crypto_free( void )
5548{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005549 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005550 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5551 {
5552 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005553 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005554 }
5555 /* Wipe all remaining data, including configuration.
5556 * In particular, this sets all state indicator to the value
5557 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005558 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005559#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005560 /* Unregister all secure element drivers, so that we restart from
5561 * a pristine state. */
5562 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005563#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005564}
5565
5566psa_status_t psa_crypto_init( void )
5567{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005568 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005569 const unsigned char drbg_seed[] = "PSA";
5570
Gilles Peskinec6b69072018-11-20 21:42:52 +01005571 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005572 if( global_data.initialized != 0 )
5573 return( PSA_SUCCESS );
5574
Gilles Peskine5e769522018-11-20 21:59:56 +01005575 /* Set default configuration if
5576 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5577 if( global_data.entropy_init == NULL )
5578 global_data.entropy_init = mbedtls_entropy_init;
5579 if( global_data.entropy_free == NULL )
5580 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005581
Gilles Peskinec6b69072018-11-20 21:42:52 +01005582 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005583 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005584 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005585 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005586 status = mbedtls_to_psa_error(
5587 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5588 mbedtls_entropy_func,
5589 &global_data.entropy,
5590 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5591 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005592 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005593 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005594
Gilles Peskine66fb1262018-12-10 16:29:04 +01005595 status = psa_initialize_key_slots( );
5596 if( status != PSA_SUCCESS )
5597 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005598
5599 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005600 global_data.initialized = 1;
5601
Gilles Peskinee59236f2018-01-27 23:32:46 +01005602exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005603 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005604 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005605 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005606}
5607
5608#endif /* MBEDTLS_PSA_CRYPTO_C */