blob: 84b691196d238fbb2903078249681f5123f0760b [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043#include <stdlib.h>
44#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020046#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010047#define mbedtls_calloc calloc
48#define mbedtls_free free
49#endif
50
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020052#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000053#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020054#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/blowfish.h"
56#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020057#include "mbedtls/chacha20.h"
58#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/cipher.h"
60#include "mbedtls/ccm.h"
61#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020064#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010065#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010066#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/error.h"
68#include "mbedtls/gcm.h"
69#include "mbedtls/md2.h"
70#include "mbedtls/md4.h"
71#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
73#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010076#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/sha1.h"
80#include "mbedtls/sha256.h"
81#include "mbedtls/sha512.h"
82#include "mbedtls/xtea.h"
83
Gilles Peskine996deb12018-08-01 15:45:45 +020084#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
85
Gilles Peskine9ef733f2018-02-07 21:05:37 +010086/* constant-time buffer comparison */
87static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
88{
89 size_t i;
90 unsigned char diff = 0;
91
92 for( i = 0; i < n; i++ )
93 diff |= a[i] ^ b[i];
94
95 return( diff );
96}
97
98
99
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100100/****************************************************************/
101/* Global data, support functions and library management */
102/****************************************************************/
103
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104static int key_type_is_raw_bytes( psa_key_type_t type )
105{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200106 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200107}
108
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109/* Values for psa_global_data_t::rng_state */
110#define RNG_NOT_INITIALIZED 0
111#define RNG_INITIALIZED 1
112#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100113
Gilles Peskine2d277862018-06-18 15:41:12 +0200114typedef struct
115{
Gilles Peskine5e769522018-11-20 21:59:56 +0100116 void (* entropy_init )( mbedtls_entropy_context *ctx );
117 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118 mbedtls_entropy_context entropy;
119 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100120 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100121 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100122} psa_global_data_t;
123
124static psa_global_data_t global_data;
125
itayzafrir0adf0fc2018-09-06 16:24:41 +0300126#define GUARD_MODULE_INITIALIZED \
127 if( global_data.initialized == 0 ) \
128 return( PSA_ERROR_BAD_STATE );
129
Gilles Peskinee59236f2018-01-27 23:32:46 +0100130static psa_status_t mbedtls_to_psa_error( int ret )
131{
Gilles Peskinea5905292018-02-07 20:59:33 +0100132 /* If there's both a high-level code and low-level code, dispatch on
133 * the high-level code. */
134 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100135 {
136 case 0:
137 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100138
139 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
140 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
141 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
142 return( PSA_ERROR_NOT_SUPPORTED );
143 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
144 return( PSA_ERROR_HARDWARE_FAILURE );
145
146 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
147 return( PSA_ERROR_HARDWARE_FAILURE );
148
Gilles Peskine9a944802018-06-21 09:35:35 +0200149 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
150 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
151 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
152 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
153 case MBEDTLS_ERR_ASN1_INVALID_DATA:
154 return( PSA_ERROR_INVALID_ARGUMENT );
155 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
156 return( PSA_ERROR_INSUFFICIENT_MEMORY );
157 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
158 return( PSA_ERROR_BUFFER_TOO_SMALL );
159
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000161 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100165 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
166 return( PSA_ERROR_NOT_SUPPORTED );
167 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
168 return( PSA_ERROR_HARDWARE_FAILURE );
169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
180 case MBEDTLS_ERR_CCM_BAD_INPUT:
181 return( PSA_ERROR_INVALID_ARGUMENT );
182 case MBEDTLS_ERR_CCM_AUTH_FAILED:
183 return( PSA_ERROR_INVALID_SIGNATURE );
184 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
Gilles Peskine26869f22019-05-06 15:25:00 +0200187 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
188 return( PSA_ERROR_INVALID_ARGUMENT );
189
190 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
191 return( PSA_ERROR_BAD_STATE );
192 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
193 return( PSA_ERROR_INVALID_SIGNATURE );
194
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
196 return( PSA_ERROR_NOT_SUPPORTED );
197 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
200 return( PSA_ERROR_INSUFFICIENT_MEMORY );
201 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
202 return( PSA_ERROR_INVALID_PADDING );
203 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
204 return( PSA_ERROR_BAD_STATE );
205 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
206 return( PSA_ERROR_INVALID_SIGNATURE );
207 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200208 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
213 return( PSA_ERROR_HARDWARE_FAILURE );
214
215 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
216 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
217 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
218 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
219 return( PSA_ERROR_NOT_SUPPORTED );
220 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
221 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
222
223 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
Gilles Peskinee59236f2018-01-27 23:32:46 +0100228 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
229 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
230 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
233 case MBEDTLS_ERR_GCM_AUTH_FAILED:
234 return( PSA_ERROR_INVALID_SIGNATURE );
235 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200236 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100237 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
238 return( PSA_ERROR_HARDWARE_FAILURE );
239
240 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
245 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
246 return( PSA_ERROR_NOT_SUPPORTED );
247 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
248 return( PSA_ERROR_INVALID_ARGUMENT );
249 case MBEDTLS_ERR_MD_ALLOC_FAILED:
250 return( PSA_ERROR_INSUFFICIENT_MEMORY );
251 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
252 return( PSA_ERROR_STORAGE_FAILURE );
253 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
254 return( PSA_ERROR_HARDWARE_FAILURE );
255
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
257 return( PSA_ERROR_STORAGE_FAILURE );
258 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
263 return( PSA_ERROR_BUFFER_TOO_SMALL );
264 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_ALLOC_FAILED:
274 return( PSA_ERROR_INSUFFICIENT_MEMORY );
275 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
276 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100279 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100280 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
281 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
284 return( PSA_ERROR_NOT_SUPPORTED );
285 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
286 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
287 return( PSA_ERROR_NOT_PERMITTED );
288 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_PK_INVALID_ALG:
291 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
292 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
293 return( PSA_ERROR_NOT_SUPPORTED );
294 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
295 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
300 return( PSA_ERROR_HARDWARE_FAILURE );
301 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
302 return( PSA_ERROR_NOT_SUPPORTED );
303
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
305 return( PSA_ERROR_HARDWARE_FAILURE );
306
307 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_RSA_INVALID_PADDING:
310 return( PSA_ERROR_INVALID_PADDING );
311 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
312 return( PSA_ERROR_HARDWARE_FAILURE );
313 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
314 return( PSA_ERROR_INVALID_ARGUMENT );
315 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
316 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200317 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100318 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
319 return( PSA_ERROR_INVALID_SIGNATURE );
320 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
321 return( PSA_ERROR_BUFFER_TOO_SMALL );
322 case MBEDTLS_ERR_RSA_RNG_FAILED:
323 return( PSA_ERROR_INSUFFICIENT_MEMORY );
324 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
325 return( PSA_ERROR_NOT_SUPPORTED );
326 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
327 return( PSA_ERROR_HARDWARE_FAILURE );
328
329 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
330 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
332 return( PSA_ERROR_HARDWARE_FAILURE );
333
334 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
335 return( PSA_ERROR_INVALID_ARGUMENT );
336 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
337 return( PSA_ERROR_HARDWARE_FAILURE );
338
itayzafrir5c753392018-05-08 11:18:38 +0300339 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300341 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
343 return( PSA_ERROR_BUFFER_TOO_SMALL );
344 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
345 return( PSA_ERROR_NOT_SUPPORTED );
346 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
347 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
348 return( PSA_ERROR_INVALID_SIGNATURE );
349 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
350 return( PSA_ERROR_INSUFFICIENT_MEMORY );
351 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
352 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
369 return( psa_key_lifetime_is_external( slot->lifetime ) );
370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200374static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
375{
376 switch( grpid )
377 {
378 case MBEDTLS_ECP_DP_SECP192R1:
379 return( PSA_ECC_CURVE_SECP192R1 );
380 case MBEDTLS_ECP_DP_SECP224R1:
381 return( PSA_ECC_CURVE_SECP224R1 );
382 case MBEDTLS_ECP_DP_SECP256R1:
383 return( PSA_ECC_CURVE_SECP256R1 );
384 case MBEDTLS_ECP_DP_SECP384R1:
385 return( PSA_ECC_CURVE_SECP384R1 );
386 case MBEDTLS_ECP_DP_SECP521R1:
387 return( PSA_ECC_CURVE_SECP521R1 );
388 case MBEDTLS_ECP_DP_BP256R1:
389 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
390 case MBEDTLS_ECP_DP_BP384R1:
391 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
392 case MBEDTLS_ECP_DP_BP512R1:
393 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
394 case MBEDTLS_ECP_DP_CURVE25519:
395 return( PSA_ECC_CURVE_CURVE25519 );
396 case MBEDTLS_ECP_DP_SECP192K1:
397 return( PSA_ECC_CURVE_SECP192K1 );
398 case MBEDTLS_ECP_DP_SECP224K1:
399 return( PSA_ECC_CURVE_SECP224K1 );
400 case MBEDTLS_ECP_DP_SECP256K1:
401 return( PSA_ECC_CURVE_SECP256K1 );
402 case MBEDTLS_ECP_DP_CURVE448:
403 return( PSA_ECC_CURVE_CURVE448 );
404 default:
405 return( 0 );
406 }
407}
408
Gilles Peskine12313cd2018-06-20 00:20:32 +0200409static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
410{
411 switch( curve )
412 {
413 case PSA_ECC_CURVE_SECP192R1:
414 return( MBEDTLS_ECP_DP_SECP192R1 );
415 case PSA_ECC_CURVE_SECP224R1:
416 return( MBEDTLS_ECP_DP_SECP224R1 );
417 case PSA_ECC_CURVE_SECP256R1:
418 return( MBEDTLS_ECP_DP_SECP256R1 );
419 case PSA_ECC_CURVE_SECP384R1:
420 return( MBEDTLS_ECP_DP_SECP384R1 );
421 case PSA_ECC_CURVE_SECP521R1:
422 return( MBEDTLS_ECP_DP_SECP521R1 );
423 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
424 return( MBEDTLS_ECP_DP_BP256R1 );
425 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
426 return( MBEDTLS_ECP_DP_BP384R1 );
427 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
428 return( MBEDTLS_ECP_DP_BP512R1 );
429 case PSA_ECC_CURVE_CURVE25519:
430 return( MBEDTLS_ECP_DP_CURVE25519 );
431 case PSA_ECC_CURVE_SECP192K1:
432 return( MBEDTLS_ECP_DP_SECP192K1 );
433 case PSA_ECC_CURVE_SECP224K1:
434 return( MBEDTLS_ECP_DP_SECP224K1 );
435 case PSA_ECC_CURVE_SECP256K1:
436 return( MBEDTLS_ECP_DP_SECP256K1 );
437 case PSA_ECC_CURVE_CURVE448:
438 return( MBEDTLS_ECP_DP_CURVE448 );
439 default:
440 return( MBEDTLS_ECP_DP_NONE );
441 }
442}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100443#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200444
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
446 size_t bits,
447 struct raw_data *raw )
448{
449 /* Check that the bit size is acceptable for the key type */
450 switch( type )
451 {
452 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200453 if( bits == 0 )
454 {
455 raw->bytes = 0;
456 raw->data = NULL;
457 return( PSA_SUCCESS );
458 }
459 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460#if defined(MBEDTLS_MD_C)
461 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200462#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200463 case PSA_KEY_TYPE_DERIVE:
464 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200465#if defined(MBEDTLS_AES_C)
466 case PSA_KEY_TYPE_AES:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_CAMELLIA_C)
472 case PSA_KEY_TYPE_CAMELLIA:
473 if( bits != 128 && bits != 192 && bits != 256 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_DES_C)
478 case PSA_KEY_TYPE_DES:
479 if( bits != 64 && bits != 128 && bits != 192 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
483#if defined(MBEDTLS_ARC4_C)
484 case PSA_KEY_TYPE_ARC4:
485 if( bits < 8 || bits > 2048 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200489#if defined(MBEDTLS_CHACHA20_C)
490 case PSA_KEY_TYPE_CHACHA20:
491 if( bits != 256 )
492 return( PSA_ERROR_INVALID_ARGUMENT );
493 break;
494#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 default:
496 return( PSA_ERROR_NOT_SUPPORTED );
497 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200498 if( bits % 8 != 0 )
499 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200500
501 /* Allocate memory for the key */
502 raw->bytes = PSA_BITS_TO_BYTES( bits );
503 raw->data = mbedtls_calloc( 1, raw->bytes );
504 if( raw->data == NULL )
505 {
506 raw->bytes = 0;
507 return( PSA_ERROR_INSUFFICIENT_MEMORY );
508 }
509 return( PSA_SUCCESS );
510}
511
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200512#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100513/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
514 * that are not a multiple of 8) well. For example, there is only
515 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
516 * way to return the exact bit size of a key.
517 * To keep things simple, reject non-byte-aligned key sizes. */
518static psa_status_t psa_check_rsa_key_byte_aligned(
519 const mbedtls_rsa_context *rsa )
520{
521 mbedtls_mpi n;
522 psa_status_t status;
523 mbedtls_mpi_init( &n );
524 status = mbedtls_to_psa_error(
525 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
526 if( status == PSA_SUCCESS )
527 {
528 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
529 status = PSA_ERROR_NOT_SUPPORTED;
530 }
531 mbedtls_mpi_free( &n );
532 return( status );
533}
534
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000535static psa_status_t psa_import_rsa_key( psa_key_type_t type,
536 const uint8_t *data,
537 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200538 mbedtls_rsa_context **p_rsa )
539{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000540 psa_status_t status;
541 mbedtls_pk_context pk;
542 mbedtls_rsa_context *rsa;
543 size_t bits;
544
545 mbedtls_pk_init( &pk );
546
547 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200548 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000549 status = mbedtls_to_psa_error(
550 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200551 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000552 status = mbedtls_to_psa_error(
553 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
554 if( status != PSA_SUCCESS )
555 goto exit;
556
557 /* We have something that the pkparse module recognizes. If it is a
558 * valid RSA key, store it. */
559 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000561 status = PSA_ERROR_INVALID_ARGUMENT;
562 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200563 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000564
565 rsa = mbedtls_pk_rsa( pk );
566 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
567 * supports non-byte-aligned key sizes, but not well. For example,
568 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
569 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
570 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
571 {
572 status = PSA_ERROR_NOT_SUPPORTED;
573 goto exit;
574 }
575 status = psa_check_rsa_key_byte_aligned( rsa );
576
577exit:
578 /* Free the content of the pk object only on error. */
579 if( status != PSA_SUCCESS )
580 {
581 mbedtls_pk_free( &pk );
582 return( status );
583 }
584
585 /* On success, store the content of the object in the RSA context. */
586 *p_rsa = rsa;
587
588 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200589}
590#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
591
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000592#if defined(MBEDTLS_ECP_C)
593
594/* Import a public key given as the uncompressed representation defined by SEC1
595 * 2.3.3 as the content of an ECPoint. */
596static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
597 const uint8_t *data,
598 size_t data_length,
599 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200600{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000602 mbedtls_ecp_keypair *ecp = NULL;
603 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
604
605 *p_ecp = NULL;
606 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
607 if( ecp == NULL )
608 return( PSA_ERROR_INSUFFICIENT_MEMORY );
609 mbedtls_ecp_keypair_init( ecp );
610
611 /* Load the group. */
612 status = mbedtls_to_psa_error(
613 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616 /* Load the public value. */
617 status = mbedtls_to_psa_error(
618 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
619 data, data_length ) );
620 if( status != PSA_SUCCESS )
621 goto exit;
622
623 /* Check that the point is on the curve. */
624 status = mbedtls_to_psa_error(
625 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
626 if( status != PSA_SUCCESS )
627 goto exit;
628
629 *p_ecp = ecp;
630 return( PSA_SUCCESS );
631
632exit:
633 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200634 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000635 mbedtls_ecp_keypair_free( ecp );
636 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200637 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000638 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200639}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000640#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200641
Gilles Peskinef76aa772018-10-29 19:24:33 +0100642#if defined(MBEDTLS_ECP_C)
643/* Import a private key given as a byte string which is the private value
644 * in big-endian order. */
645static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
646 const uint8_t *data,
647 size_t data_length,
648 mbedtls_ecp_keypair **p_ecp )
649{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651 mbedtls_ecp_keypair *ecp = NULL;
652 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
653
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200654 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
655 return( PSA_ERROR_INVALID_ARGUMENT );
656
Gilles Peskinef76aa772018-10-29 19:24:33 +0100657 *p_ecp = NULL;
658 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
659 if( ecp == NULL )
660 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000661 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100662
663 /* Load the group. */
664 status = mbedtls_to_psa_error(
665 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
666 if( status != PSA_SUCCESS )
667 goto exit;
668 /* Load the secret value. */
669 status = mbedtls_to_psa_error(
670 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
671 if( status != PSA_SUCCESS )
672 goto exit;
673 /* Validate the private key. */
674 status = mbedtls_to_psa_error(
675 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678 /* Calculate the public key from the private key. */
679 status = mbedtls_to_psa_error(
680 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
681 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
682 if( status != PSA_SUCCESS )
683 goto exit;
684
685 *p_ecp = ecp;
686 return( PSA_SUCCESS );
687
688exit:
689 if( ecp != NULL )
690 {
691 mbedtls_ecp_keypair_free( ecp );
692 mbedtls_free( ecp );
693 }
694 return( status );
695}
696#endif /* defined(MBEDTLS_ECP_C) */
697
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100698/** Import key data into a slot. `slot->type` must have been set
699 * previously. This function assumes that the slot does not contain
700 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100701psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
702 const uint8_t *data,
703 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200705 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706
Darryl Green940d72c2018-07-13 13:18:51 +0100707 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100709 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 if( data_length > SIZE_MAX / 8 )
711 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100712 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200713 PSA_BYTES_TO_BITS( data_length ),
714 &slot->data.raw );
715 if( status != PSA_SUCCESS )
716 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200717 if( data_length != 0 )
718 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100719 }
720 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100721#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200722 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100723 {
Darryl Green940d72c2018-07-13 13:18:51 +0100724 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100725 data, data_length,
726 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100727 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000728 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
729 {
730 status = psa_import_ec_public_key(
731 PSA_KEY_TYPE_GET_CURVE( slot->type ),
732 data, data_length,
733 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000734 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100735 else
736#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000737#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
738 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100739 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000740 status = psa_import_rsa_key( slot->type,
741 data, data_length,
742 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 }
744 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000745#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 {
747 return( PSA_ERROR_NOT_SUPPORTED );
748 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000749 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100750}
751
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100752/** Calculate the intersection of two algorithm usage policies.
753 *
754 * Return 0 (which allows no operation) on incompatibility.
755 */
756static psa_algorithm_t psa_key_policy_algorithm_intersection(
757 psa_algorithm_t alg1,
758 psa_algorithm_t alg2 )
759{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200760 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100761 if( alg1 == alg2 )
762 return( alg1 );
763 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100764 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100765 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
766 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
767 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
768 {
769 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
770 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100771 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100772 return( alg1 );
773 }
774 /* If the policies are incompatible, allow nothing. */
775 return( 0 );
776}
777
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200778static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
779 psa_algorithm_t requested_alg )
780{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200781 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200782 if( requested_alg == policy_alg )
783 return( 1 );
784 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200785 * and requested_alg is the same hash-and-sign family with any hash,
786 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200787 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
788 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
789 {
790 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
791 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
792 }
793 /* If it isn't permitted, it's forbidden. */
794 return( 0 );
795}
796
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100797/** Test whether a policy permits an algorithm.
798 *
799 * The caller must test usage flags separately.
800 */
801static int psa_key_policy_permits( const psa_key_policy_t *policy,
802 psa_algorithm_t alg )
803{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200804 return( psa_key_algorithm_permits( policy->alg, alg ) ||
805 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100806}
807
Gilles Peskinef603c712019-01-19 13:40:11 +0100808/** Restrict a key policy based on a constraint.
809 *
810 * \param[in,out] policy The policy to restrict.
811 * \param[in] constraint The policy constraint to apply.
812 *
813 * \retval #PSA_SUCCESS
814 * \c *policy contains the intersection of the original value of
815 * \c *policy and \c *constraint.
816 * \retval #PSA_ERROR_INVALID_ARGUMENT
817 * \c *policy and \c *constraint are incompatible.
818 * \c *policy is unchanged.
819 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100820static psa_status_t psa_restrict_key_policy(
821 psa_key_policy_t *policy,
822 const psa_key_policy_t *constraint )
823{
824 psa_algorithm_t intersection_alg =
825 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200826 psa_algorithm_t intersection_alg2 =
827 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100828 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
829 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200830 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
831 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100832 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100833 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200834 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100835 return( PSA_SUCCESS );
836}
837
Darryl Green06fd18d2018-07-16 11:21:11 +0100838/** Retrieve a slot which must contain a key. The key must have allow all the
839 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
840 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100841static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100842 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100843 psa_key_usage_t usage,
844 psa_algorithm_t alg )
845{
846 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100847 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100848
849 *p_slot = NULL;
850
Gilles Peskinec5487a82018-12-03 18:08:14 +0100851 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100852 if( status != PSA_SUCCESS )
853 return( status );
854 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200855 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100856
857 /* Enforce that usage policy for the key slot contains all the flags
858 * required by the usage parameter. There is one exception: public
859 * keys can always be exported, so we treat public key objects as
860 * if they had the export flag. */
861 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
862 usage &= ~PSA_KEY_USAGE_EXPORT;
863 if( ( slot->policy.usage & usage ) != usage )
864 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100865
866 /* Enforce that the usage policy permits the requested algortihm. */
867 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100868 return( PSA_ERROR_NOT_PERMITTED );
869
870 *p_slot = slot;
871 return( PSA_SUCCESS );
872}
Darryl Green940d72c2018-07-13 13:18:51 +0100873
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100874/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100875static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000876{
Gilles Peskine73167e12019-07-12 23:44:37 +0200877#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
878 if( psa_key_slot_is_external( slot ) )
879 {
880 /* No key material to clean. */
881 }
882 else
883#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +0000884 if( slot->type == PSA_KEY_TYPE_NONE )
885 {
886 /* No key material to clean. */
887 }
888 else if( key_type_is_raw_bytes( slot->type ) )
889 {
890 mbedtls_free( slot->data.raw.data );
891 }
892 else
893#if defined(MBEDTLS_RSA_C)
894 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
895 {
896 mbedtls_rsa_free( slot->data.rsa );
897 mbedtls_free( slot->data.rsa );
898 }
899 else
900#endif /* defined(MBEDTLS_RSA_C) */
901#if defined(MBEDTLS_ECP_C)
902 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
903 {
904 mbedtls_ecp_keypair_free( slot->data.ecp );
905 mbedtls_free( slot->data.ecp );
906 }
907 else
908#endif /* defined(MBEDTLS_ECP_C) */
909 {
910 /* Shouldn't happen: the key type is not any type that we
911 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200912 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000913 }
914
915 return( PSA_SUCCESS );
916}
917
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100918static void psa_abort_operations_using_key( psa_key_slot_t *slot )
919{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200920 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100921 (void) slot;
922}
923
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100924/** Completely wipe a slot in memory, including its policy.
925 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100926psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100927{
928 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100929 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100930 /* At this point, key material and other type-specific content has
931 * been wiped. Clear remaining metadata. We can call memset and not
932 * zeroize because the metadata is not particularly sensitive. */
933 memset( slot, 0, sizeof( *slot ) );
934 return( status );
935}
936
Gilles Peskinec5487a82018-12-03 18:08:14 +0100937psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100938{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100939 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100940 psa_status_t status = PSA_SUCCESS;
941 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200942#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
943 psa_se_drv_table_entry_t *driver;
944#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945
Gilles Peskinec5487a82018-12-03 18:08:14 +0100946 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200947 if( status != PSA_SUCCESS )
948 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +0200949
950#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
951 driver = psa_get_se_driver_entry( slot->lifetime );
952 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +0200953 {
954 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
955 psa_crypto_transaction.key.lifetime = slot->lifetime;
956 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
957 psa_crypto_transaction.key.id = slot->persistent_storage_id;
958 status = psa_crypto_save_transaction( );
959 if( status != PSA_SUCCESS )
960 {
961 /* TOnogrepDO: destroy what can be destroyed anyway */
962 return( status );
963 }
964
Gilles Peskine354f7672019-07-12 23:46:38 +0200965 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +0200966 }
Gilles Peskine354f7672019-07-12 23:46:38 +0200967#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
968
Darryl Greend49a4992018-06-18 17:27:26 +0100969#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
970 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
971 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100972 storage_status =
973 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100974 }
975#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +0200976
Gilles Peskinefc762652019-07-22 19:30:34 +0200977#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
978 if( driver != NULL )
979 {
980 status = psa_crypto_stop_transaction( );
981 if( status != PSA_SUCCESS )
982 {
983 /* TOnogrepDO: destroy what can be destroyed anyway */
984 return( status );
985 }
986 }
987#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
988
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100989 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100990 if( status != PSA_SUCCESS )
991 return( status );
992 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993}
994
Gilles Peskineb870b182018-07-06 16:02:09 +0200995/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200996static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200997{
998 if( key_type_is_raw_bytes( slot->type ) )
999 return( slot->data.raw.bytes * 8 );
1000#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001001 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001002 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001003#endif /* defined(MBEDTLS_RSA_C) */
1004#if defined(MBEDTLS_ECP_C)
1005 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1006 return( slot->data.ecp->grp.pbits );
1007#endif /* defined(MBEDTLS_ECP_C) */
1008 /* Shouldn't happen except on an empty slot. */
1009 return( 0 );
1010}
1011
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001012void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1013{
Gilles Peskineb699f072019-04-26 16:06:02 +02001014 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001015 memset( attributes, 0, sizeof( *attributes ) );
1016}
1017
Gilles Peskineb699f072019-04-26 16:06:02 +02001018psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1019 psa_key_type_t type,
1020 const uint8_t *data,
1021 size_t data_length )
1022{
1023 uint8_t *copy = NULL;
1024
1025 if( data_length != 0 )
1026 {
1027 copy = mbedtls_calloc( 1, data_length );
1028 if( copy == NULL )
1029 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1030 memcpy( copy, data, data_length );
1031 }
1032 /* After this point, this function is guaranteed to succeed, so it
1033 * can start modifying `*attributes`. */
1034
1035 if( attributes->domain_parameters != NULL )
1036 {
1037 mbedtls_free( attributes->domain_parameters );
1038 attributes->domain_parameters = NULL;
1039 attributes->domain_parameters_size = 0;
1040 }
1041
1042 attributes->domain_parameters = copy;
1043 attributes->domain_parameters_size = data_length;
1044 attributes->type = type;
1045 return( PSA_SUCCESS );
1046}
1047
1048psa_status_t psa_get_key_domain_parameters(
1049 const psa_key_attributes_t *attributes,
1050 uint8_t *data, size_t data_size, size_t *data_length )
1051{
1052 if( attributes->domain_parameters_size > data_size )
1053 return( PSA_ERROR_BUFFER_TOO_SMALL );
1054 *data_length = attributes->domain_parameters_size;
1055 if( attributes->domain_parameters_size != 0 )
1056 memcpy( data, attributes->domain_parameters,
1057 attributes->domain_parameters_size );
1058 return( PSA_SUCCESS );
1059}
1060
1061#if defined(MBEDTLS_RSA_C)
1062static psa_status_t psa_get_rsa_public_exponent(
1063 const mbedtls_rsa_context *rsa,
1064 psa_key_attributes_t *attributes )
1065{
1066 mbedtls_mpi mpi;
1067 int ret;
1068 uint8_t *buffer = NULL;
1069 size_t buflen;
1070 mbedtls_mpi_init( &mpi );
1071
1072 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1073 if( ret != 0 )
1074 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001075 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1076 {
1077 /* It's the default value, which is reported as an empty string,
1078 * so there's nothing to do. */
1079 goto exit;
1080 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001081
1082 buflen = mbedtls_mpi_size( &mpi );
1083 buffer = mbedtls_calloc( 1, buflen );
1084 if( buffer == NULL )
1085 {
1086 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1087 goto exit;
1088 }
1089 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1090 if( ret != 0 )
1091 goto exit;
1092 attributes->domain_parameters = buffer;
1093 attributes->domain_parameters_size = buflen;
1094
1095exit:
1096 mbedtls_mpi_free( &mpi );
1097 if( ret != 0 )
1098 mbedtls_free( buffer );
1099 return( mbedtls_to_psa_error( ret ) );
1100}
1101#endif /* MBEDTLS_RSA_C */
1102
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001103/** Retrieve the readily-accessible attributes of a key in a slot.
1104 *
1105 * This function does not compute attributes that are not directly
1106 * stored in the slot, such as the bit size of a transparent key.
1107 */
1108static void psa_get_key_slot_attributes( psa_key_slot_t *slot,
1109 psa_key_attributes_t *attributes )
1110{
1111 attributes->id = slot->persistent_storage_id;
1112 attributes->lifetime = slot->lifetime;
1113 attributes->policy = slot->policy;
1114 attributes->type = slot->type;
1115}
1116
1117/** Retrieve all the publicly-accessible attributes of a key.
1118 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001119psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1120 psa_key_attributes_t *attributes )
1121{
1122 psa_key_slot_t *slot;
1123 psa_status_t status;
1124
1125 psa_reset_key_attributes( attributes );
1126
1127 status = psa_get_key_slot( handle, &slot );
1128 if( status != PSA_SUCCESS )
1129 return( status );
1130
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001131 psa_get_key_slot_attributes( slot, attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001132 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001133
1134 switch( slot->type )
1135 {
1136#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001137 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001138 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1139 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1140 break;
1141#endif
1142 default:
1143 /* Nothing else to do. */
1144 break;
1145 }
1146
1147 if( status != PSA_SUCCESS )
1148 psa_reset_key_attributes( attributes );
1149 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001150}
1151
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001152#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001153static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1154 unsigned char *buf, size_t size )
1155{
1156 int ret;
1157 unsigned char *c;
1158 size_t len = 0;
1159
1160 c = buf + size;
1161
1162 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1163
1164 return( (int) len );
1165}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001166#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001167
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001168static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1169 uint8_t *data,
1170 size_t data_size,
1171 size_t *data_length,
1172 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001173{
Gilles Peskine5d309672019-07-12 23:47:28 +02001174#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1175 const psa_drv_se_t *drv;
1176 psa_drv_se_context_t *drv_context;
1177#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1178
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001179 *data_length = 0;
1180
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001181 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001182 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001183
Gilles Peskine5d309672019-07-12 23:47:28 +02001184#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1185 if( psa_get_se_driver( slot->lifetime, &drv, &drv_context ) )
1186 {
1187 psa_drv_se_export_key_t method;
1188 if( drv->key_management == NULL )
1189 return( PSA_ERROR_NOT_SUPPORTED );
1190 method = ( export_public_key ?
1191 drv->key_management->p_export_public :
1192 drv->key_management->p_export );
1193 if( method == NULL )
1194 return( PSA_ERROR_NOT_SUPPORTED );
1195 return( ( *method )( drv_context,
1196 slot->data.se.slot_number,
1197 data, data_size, data_length ) );
1198 }
1199#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1200
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001201 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001202 {
1203 if( slot->data.raw.bytes > data_size )
1204 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001205 if( data_size != 0 )
1206 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001207 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001208 memset( data + slot->data.raw.bytes, 0,
1209 data_size - slot->data.raw.bytes );
1210 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001211 *data_length = slot->data.raw.bytes;
1212 return( PSA_SUCCESS );
1213 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001214#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001215 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001216 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001217 psa_status_t status;
1218
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001219 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001220 if( bytes > data_size )
1221 return( PSA_ERROR_BUFFER_TOO_SMALL );
1222 status = mbedtls_to_psa_error(
1223 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1224 if( status != PSA_SUCCESS )
1225 return( status );
1226 memset( data + bytes, 0, data_size - bytes );
1227 *data_length = bytes;
1228 return( PSA_SUCCESS );
1229 }
1230#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001231 else
Moran Peker17e36e12018-05-02 12:55:20 +03001232 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001233#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001234 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001235 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001236 {
Moran Pekera998bc62018-04-16 18:16:20 +03001237 mbedtls_pk_context pk;
1238 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001239 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001240 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001241#if defined(MBEDTLS_RSA_C)
1242 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001243 pk.pk_info = &mbedtls_rsa_info;
1244 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001245#else
1246 return( PSA_ERROR_NOT_SUPPORTED );
1247#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001248 }
1249 else
1250 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001251#if defined(MBEDTLS_ECP_C)
1252 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001253 pk.pk_info = &mbedtls_eckey_info;
1254 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001255#else
1256 return( PSA_ERROR_NOT_SUPPORTED );
1257#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001258 }
Moran Pekerd7326592018-05-29 16:56:39 +03001259 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001260 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001261 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001262 }
Moran Peker17e36e12018-05-02 12:55:20 +03001263 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001264 {
Moran Peker17e36e12018-05-02 12:55:20 +03001265 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001266 }
Moran Peker60364322018-04-29 11:34:58 +03001267 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001268 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001269 /* If data_size is 0 then data may be NULL and then the
1270 * call to memset would have undefined behavior. */
1271 if( data_size != 0 )
1272 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001273 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001274 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001275 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1276 * Move the data to the beginning and erase remaining data
1277 * at the original location. */
1278 if( 2 * (size_t) ret <= data_size )
1279 {
1280 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001281 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001282 }
1283 else if( (size_t) ret < data_size )
1284 {
1285 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001286 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001287 }
Moran Pekera998bc62018-04-16 18:16:20 +03001288 *data_length = ret;
1289 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001290 }
1291 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001292#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001293 {
1294 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001295 it is valid for a special-purpose implementation to omit
1296 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001297 return( PSA_ERROR_NOT_SUPPORTED );
1298 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001299 }
1300}
1301
Gilles Peskinec5487a82018-12-03 18:08:14 +01001302psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001303 uint8_t *data,
1304 size_t data_size,
1305 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001306{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001307 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001308 psa_status_t status;
1309
1310 /* Set the key to empty now, so that even when there are errors, we always
1311 * set data_length to a value between 0 and data_size. On error, setting
1312 * the key to empty is a good choice because an empty key representation is
1313 * unlikely to be accepted anywhere. */
1314 *data_length = 0;
1315
1316 /* Export requires the EXPORT flag. There is an exception for public keys,
1317 * which don't require any flag, but psa_get_key_from_slot takes
1318 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001319 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001320 if( status != PSA_SUCCESS )
1321 return( status );
1322 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001323 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001324}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001325
Gilles Peskinec5487a82018-12-03 18:08:14 +01001326psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001327 uint8_t *data,
1328 size_t data_size,
1329 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001330{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001331 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001332 psa_status_t status;
1333
1334 /* Set the key to empty now, so that even when there are errors, we always
1335 * set data_length to a value between 0 and data_size. On error, setting
1336 * the key to empty is a good choice because an empty key representation is
1337 * unlikely to be accepted anywhere. */
1338 *data_length = 0;
1339
1340 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001341 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001342 if( status != PSA_SUCCESS )
1343 return( status );
1344 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001345 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001346}
1347
Gilles Peskine4747d192019-04-17 15:05:45 +02001348static psa_status_t psa_set_key_policy_internal(
1349 psa_key_slot_t *slot,
1350 const psa_key_policy_t *policy )
1351{
1352 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001353 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001354 PSA_KEY_USAGE_ENCRYPT |
1355 PSA_KEY_USAGE_DECRYPT |
1356 PSA_KEY_USAGE_SIGN |
1357 PSA_KEY_USAGE_VERIFY |
1358 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1359 return( PSA_ERROR_INVALID_ARGUMENT );
1360
1361 slot->policy = *policy;
1362 return( PSA_SUCCESS );
1363}
1364
1365/** Prepare a key slot to receive key material.
1366 *
1367 * This function allocates a key slot and sets its metadata.
1368 *
1369 * If this function fails, call psa_fail_key_creation().
1370 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001371 * This function is intended to be used as follows:
1372 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1373 * it with the specified attributes, and assign it a handle.
1374 * -# Populate the slot with the key material.
1375 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1376 * In case of failure at any step, stop the sequence and call
1377 * psa_fail_key_creation().
1378 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001379 * \param[in] attributes Key attributes for the new key.
1380 * \param[out] handle On success, a handle for the allocated slot.
1381 * \param[out] p_slot On success, a pointer to the prepared slot.
1382 * \param[out] p_drv On any return, the driver for the key, if any.
1383 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001384 *
1385 * \retval #PSA_SUCCESS
1386 * The key slot is ready to receive key material.
1387 * \return If this function fails, the key slot is an invalid state.
1388 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001389 */
1390static psa_status_t psa_start_key_creation(
1391 const psa_key_attributes_t *attributes,
1392 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001393 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001394 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001395{
1396 psa_status_t status;
1397 psa_key_slot_t *slot;
1398
Gilles Peskine011e4282019-06-26 18:34:38 +02001399 *p_drv = NULL;
1400
Gilles Peskine267c6562019-05-27 19:01:54 +02001401 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001402 if( status != PSA_SUCCESS )
1403 return( status );
1404 slot = *p_slot;
1405
1406 status = psa_set_key_policy_internal( slot, &attributes->policy );
1407 if( status != PSA_SUCCESS )
1408 return( status );
1409 slot->lifetime = attributes->lifetime;
Gilles Peskine011e4282019-06-26 18:34:38 +02001410
Gilles Peskine4747d192019-04-17 15:05:45 +02001411 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001412 {
1413 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskine011e4282019-06-26 18:34:38 +02001414 attributes->id,
1415 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001416 if( status != PSA_SUCCESS )
1417 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001418 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001419 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001420 slot->type = attributes->type;
1421
Gilles Peskinecbaff462019-07-12 23:46:04 +02001422#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskinefc762652019-07-22 19:30:34 +02001423 /* Find a slot number for the new key. Save the slot number in
1424 * persistent storage, but do not yet save the driver's persistent
1425 * state, so that if the power fails during the key creation process,
1426 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001427 if( *p_drv != NULL )
1428 {
1429 status = psa_find_se_slot_for_key( attributes, *p_drv,
1430 &slot->data.se.slot_number );
1431 if( status != PSA_SUCCESS )
1432 return( status );
1433 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001434 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1435 psa_crypto_transaction.key.lifetime = slot->lifetime;
1436 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1437 psa_crypto_transaction.key.id = slot->persistent_storage_id;
1438 status = psa_crypto_save_transaction( );
1439 if( status != PSA_SUCCESS )
1440 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001441#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1442
Gilles Peskine4747d192019-04-17 15:05:45 +02001443 return( status );
1444}
1445
1446/** Finalize the creation of a key once its key material has been set.
1447 *
1448 * This entails writing the key to persistent storage.
1449 *
1450 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001451 * See the documentation of psa_start_key_creation() for the intended use
1452 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001453 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001454 * \param[in,out] slot Pointer to the slot with key material.
1455 * \param[in] driver The secure element driver for the key,
1456 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001457 *
1458 * \retval #PSA_SUCCESS
1459 * The key was successfully created. The handle is now valid.
1460 * \return If this function fails, the key slot is an invalid state.
1461 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001462 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001463static psa_status_t psa_finish_key_creation(
1464 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001465 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001466{
1467 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001468 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001469 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001470
1471#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1df83d42019-07-23 16:13:14 +02001472 if( slot->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001473 {
1474 uint8_t *buffer = NULL;
1475 size_t buffer_size = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001476 size_t length = 0;
Gilles Peskine4747d192019-04-17 15:05:45 +02001477
Gilles Peskine1df83d42019-07-23 16:13:14 +02001478#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1479 if( driver != NULL )
1480 {
1481 buffer = (uint8_t*) &slot->data.se.slot_number;
1482 length = sizeof( slot->data.se.slot_number );
1483 }
1484 else
1485#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1486 {
1487 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
1488 psa_get_key_slot_bits( slot ) );
1489 buffer = mbedtls_calloc( 1, buffer_size );
1490 if( buffer == NULL && buffer_size != 0 )
1491 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1492 status = psa_internal_export_key( slot,
1493 buffer, buffer_size, &length,
1494 0 );
1495 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001496
1497 if( status == PSA_SUCCESS )
1498 {
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001499 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1500 psa_get_key_slot_attributes( slot, &attributes );
1501 status = psa_save_persistent_key( &attributes, buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001502 }
1503
Gilles Peskine1df83d42019-07-23 16:13:14 +02001504#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1505 if( driver == NULL )
1506#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1507 {
1508 if( buffer_size != 0 )
1509 mbedtls_platform_zeroize( buffer, buffer_size );
1510 mbedtls_free( buffer );
1511 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001512 }
1513#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1514
Gilles Peskinecbaff462019-07-12 23:46:04 +02001515#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1516 if( driver != NULL )
1517 {
1518 status = psa_save_se_persistent_data( driver );
1519 if( status != PSA_SUCCESS )
1520 {
1521 psa_destroy_persistent_key( slot->persistent_storage_id );
1522 return( status );
1523 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001524 status = psa_crypto_stop_transaction( );
1525 if( status != PSA_SUCCESS )
1526 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001527 }
1528#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1529
Gilles Peskine4747d192019-04-17 15:05:45 +02001530 return( status );
1531}
1532
1533/** Abort the creation of a key.
1534 *
1535 * You may call this function after calling psa_start_key_creation(),
1536 * or after psa_finish_key_creation() fails. In other circumstances, this
1537 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001538 * See the documentation of psa_start_key_creation() for the intended use
1539 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001540 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001541 * \param[in,out] slot Pointer to the slot with key material.
1542 * \param[in] driver The secure element driver for the key,
1543 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001544 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001545static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001546 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001547{
Gilles Peskine011e4282019-06-26 18:34:38 +02001548 (void) driver;
1549
Gilles Peskine4747d192019-04-17 15:05:45 +02001550 if( slot == NULL )
1551 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001552
1553#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1554 /* TOnogrepDO: If the key has already been created in the secure
1555 * element, and the failure happened later (when saving metadata
1556 * to internal storage), we need to destroy the key in the secure
1557 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001558
1559 /* Abort the ongoing transaction if any. We already did what it
1560 * takes to undo any partial creation. All that's left is to update
1561 * the transaction data itself. */
1562 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001563#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1564
Gilles Peskine4747d192019-04-17 15:05:45 +02001565 psa_wipe_key_slot( slot );
1566}
1567
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001568static psa_status_t psa_check_key_slot_attributes(
1569 const psa_key_slot_t *slot,
1570 const psa_key_attributes_t *attributes )
1571{
1572 if( attributes->type != 0 )
1573 {
1574 if( attributes->type != slot->type )
1575 return( PSA_ERROR_INVALID_ARGUMENT );
1576 }
1577
1578 if( attributes->domain_parameters_size != 0 )
1579 {
1580#if defined(MBEDTLS_RSA_C)
1581 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1582 {
1583 mbedtls_mpi actual, required;
1584 int ret;
1585 mbedtls_mpi_init( &actual );
1586 mbedtls_mpi_init( &required );
1587 ret = mbedtls_rsa_export( slot->data.rsa,
1588 NULL, NULL, NULL, NULL, &actual );
1589 if( ret != 0 )
1590 goto rsa_exit;
1591 ret = mbedtls_mpi_read_binary( &required,
1592 attributes->domain_parameters,
1593 attributes->domain_parameters_size );
1594 if( ret != 0 )
1595 goto rsa_exit;
1596 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1597 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1598 rsa_exit:
1599 mbedtls_mpi_free( &actual );
1600 mbedtls_mpi_free( &required );
1601 if( ret != 0)
1602 return( mbedtls_to_psa_error( ret ) );
1603 }
1604 else
1605#endif
1606 {
1607 return( PSA_ERROR_INVALID_ARGUMENT );
1608 }
1609 }
1610
1611 if( attributes->bits != 0 )
1612 {
1613 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1614 return( PSA_ERROR_INVALID_ARGUMENT );
1615 }
1616
1617 return( PSA_SUCCESS );
1618}
1619
Gilles Peskine4747d192019-04-17 15:05:45 +02001620psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001621 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001622 size_t data_length,
1623 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001624{
1625 psa_status_t status;
1626 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001627 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001628
Gilles Peskine011e4282019-06-26 18:34:38 +02001629 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001630 if( status != PSA_SUCCESS )
1631 goto exit;
1632
Gilles Peskine5d309672019-07-12 23:47:28 +02001633#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1634 if( driver != NULL )
1635 {
1636 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
1637 if( drv->key_management == NULL ||
1638 drv->key_management->p_import == NULL )
1639 {
1640 status = PSA_ERROR_NOT_SUPPORTED;
1641 goto exit;
1642 }
1643 status = drv->key_management->p_import(
1644 psa_get_se_driver_context( driver ),
1645 slot->data.se.slot_number,
1646 slot->lifetime, slot->type, slot->policy.alg, slot->policy.usage,
1647 data, data_length );
1648 /* TOnogrepDO: psa_check_key_slot_attributes? */
1649 }
1650 else
1651#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1652 {
1653 status = psa_import_key_into_slot( slot, data, data_length );
1654 if( status != PSA_SUCCESS )
1655 goto exit;
1656 status = psa_check_key_slot_attributes( slot, attributes );
1657 if( status != PSA_SUCCESS )
1658 goto exit;
1659 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001660
Gilles Peskine011e4282019-06-26 18:34:38 +02001661 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001662exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001663 if( status != PSA_SUCCESS )
1664 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001665 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001666 *handle = 0;
1667 }
1668 return( status );
1669}
1670
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001671static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001672 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001673{
1674 psa_status_t status;
1675 uint8_t *buffer = NULL;
1676 size_t buffer_size = 0;
1677 size_t length;
1678
1679 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001680 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001681 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001682 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001683 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001684 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1685 if( status != PSA_SUCCESS )
1686 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001687 target->type = source->type;
1688 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001689
1690exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001691 if( buffer_size != 0 )
1692 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001693 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001694 return( status );
1695}
1696
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001697psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1698 const psa_key_attributes_t *specified_attributes,
1699 psa_key_handle_t *target_handle )
1700{
1701 psa_status_t status;
1702 psa_key_slot_t *source_slot = NULL;
1703 psa_key_slot_t *target_slot = NULL;
1704 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001705 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001706
Gilles Peskinec160d9e2019-05-14 14:32:03 +02001707 status = psa_get_key_from_slot( source_handle, &source_slot,
1708 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001709 if( status != PSA_SUCCESS )
1710 goto exit;
1711
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001712 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1713 if( status != PSA_SUCCESS )
1714 goto exit;
1715
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001716 status = psa_restrict_key_policy( &actual_attributes.policy,
1717 &source_slot->policy );
1718 if( status != PSA_SUCCESS )
1719 goto exit;
1720
1721 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001722 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001723 if( status != PSA_SUCCESS )
1724 goto exit;
1725
1726 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001727 if( status != PSA_SUCCESS )
1728 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001729
Gilles Peskine011e4282019-06-26 18:34:38 +02001730 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001731exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001732 if( status != PSA_SUCCESS )
1733 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001734 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001735 *target_handle = 0;
1736 }
1737 return( status );
1738}
1739
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001740
1741
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001742/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001743/* Message digests */
1744/****************************************************************/
1745
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001746static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001747{
1748 switch( alg )
1749 {
1750#if defined(MBEDTLS_MD2_C)
1751 case PSA_ALG_MD2:
1752 return( &mbedtls_md2_info );
1753#endif
1754#if defined(MBEDTLS_MD4_C)
1755 case PSA_ALG_MD4:
1756 return( &mbedtls_md4_info );
1757#endif
1758#if defined(MBEDTLS_MD5_C)
1759 case PSA_ALG_MD5:
1760 return( &mbedtls_md5_info );
1761#endif
1762#if defined(MBEDTLS_RIPEMD160_C)
1763 case PSA_ALG_RIPEMD160:
1764 return( &mbedtls_ripemd160_info );
1765#endif
1766#if defined(MBEDTLS_SHA1_C)
1767 case PSA_ALG_SHA_1:
1768 return( &mbedtls_sha1_info );
1769#endif
1770#if defined(MBEDTLS_SHA256_C)
1771 case PSA_ALG_SHA_224:
1772 return( &mbedtls_sha224_info );
1773 case PSA_ALG_SHA_256:
1774 return( &mbedtls_sha256_info );
1775#endif
1776#if defined(MBEDTLS_SHA512_C)
1777 case PSA_ALG_SHA_384:
1778 return( &mbedtls_sha384_info );
1779 case PSA_ALG_SHA_512:
1780 return( &mbedtls_sha512_info );
1781#endif
1782 default:
1783 return( NULL );
1784 }
1785}
1786
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001787psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1788{
1789 switch( operation->alg )
1790 {
Gilles Peskine81736312018-06-26 15:04:31 +02001791 case 0:
1792 /* The object has (apparently) been initialized but it is not
1793 * in use. It's ok to call abort on such an object, and there's
1794 * nothing to do. */
1795 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001796#if defined(MBEDTLS_MD2_C)
1797 case PSA_ALG_MD2:
1798 mbedtls_md2_free( &operation->ctx.md2 );
1799 break;
1800#endif
1801#if defined(MBEDTLS_MD4_C)
1802 case PSA_ALG_MD4:
1803 mbedtls_md4_free( &operation->ctx.md4 );
1804 break;
1805#endif
1806#if defined(MBEDTLS_MD5_C)
1807 case PSA_ALG_MD5:
1808 mbedtls_md5_free( &operation->ctx.md5 );
1809 break;
1810#endif
1811#if defined(MBEDTLS_RIPEMD160_C)
1812 case PSA_ALG_RIPEMD160:
1813 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1814 break;
1815#endif
1816#if defined(MBEDTLS_SHA1_C)
1817 case PSA_ALG_SHA_1:
1818 mbedtls_sha1_free( &operation->ctx.sha1 );
1819 break;
1820#endif
1821#if defined(MBEDTLS_SHA256_C)
1822 case PSA_ALG_SHA_224:
1823 case PSA_ALG_SHA_256:
1824 mbedtls_sha256_free( &operation->ctx.sha256 );
1825 break;
1826#endif
1827#if defined(MBEDTLS_SHA512_C)
1828 case PSA_ALG_SHA_384:
1829 case PSA_ALG_SHA_512:
1830 mbedtls_sha512_free( &operation->ctx.sha512 );
1831 break;
1832#endif
1833 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001834 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001835 }
1836 operation->alg = 0;
1837 return( PSA_SUCCESS );
1838}
1839
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001840psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001841 psa_algorithm_t alg )
1842{
1843 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001844
1845 /* A context must be freshly initialized before it can be set up. */
1846 if( operation->alg != 0 )
1847 {
1848 return( PSA_ERROR_BAD_STATE );
1849 }
1850
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001851 switch( alg )
1852 {
1853#if defined(MBEDTLS_MD2_C)
1854 case PSA_ALG_MD2:
1855 mbedtls_md2_init( &operation->ctx.md2 );
1856 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1857 break;
1858#endif
1859#if defined(MBEDTLS_MD4_C)
1860 case PSA_ALG_MD4:
1861 mbedtls_md4_init( &operation->ctx.md4 );
1862 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1863 break;
1864#endif
1865#if defined(MBEDTLS_MD5_C)
1866 case PSA_ALG_MD5:
1867 mbedtls_md5_init( &operation->ctx.md5 );
1868 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1869 break;
1870#endif
1871#if defined(MBEDTLS_RIPEMD160_C)
1872 case PSA_ALG_RIPEMD160:
1873 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1874 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1875 break;
1876#endif
1877#if defined(MBEDTLS_SHA1_C)
1878 case PSA_ALG_SHA_1:
1879 mbedtls_sha1_init( &operation->ctx.sha1 );
1880 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1881 break;
1882#endif
1883#if defined(MBEDTLS_SHA256_C)
1884 case PSA_ALG_SHA_224:
1885 mbedtls_sha256_init( &operation->ctx.sha256 );
1886 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1887 break;
1888 case PSA_ALG_SHA_256:
1889 mbedtls_sha256_init( &operation->ctx.sha256 );
1890 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1891 break;
1892#endif
1893#if defined(MBEDTLS_SHA512_C)
1894 case PSA_ALG_SHA_384:
1895 mbedtls_sha512_init( &operation->ctx.sha512 );
1896 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1897 break;
1898 case PSA_ALG_SHA_512:
1899 mbedtls_sha512_init( &operation->ctx.sha512 );
1900 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1901 break;
1902#endif
1903 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001904 return( PSA_ALG_IS_HASH( alg ) ?
1905 PSA_ERROR_NOT_SUPPORTED :
1906 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001907 }
1908 if( ret == 0 )
1909 operation->alg = alg;
1910 else
1911 psa_hash_abort( operation );
1912 return( mbedtls_to_psa_error( ret ) );
1913}
1914
1915psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1916 const uint8_t *input,
1917 size_t input_length )
1918{
1919 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001920
1921 /* Don't require hash implementations to behave correctly on a
1922 * zero-length input, which may have an invalid pointer. */
1923 if( input_length == 0 )
1924 return( PSA_SUCCESS );
1925
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001926 switch( operation->alg )
1927 {
1928#if defined(MBEDTLS_MD2_C)
1929 case PSA_ALG_MD2:
1930 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1931 input, input_length );
1932 break;
1933#endif
1934#if defined(MBEDTLS_MD4_C)
1935 case PSA_ALG_MD4:
1936 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1937 input, input_length );
1938 break;
1939#endif
1940#if defined(MBEDTLS_MD5_C)
1941 case PSA_ALG_MD5:
1942 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1943 input, input_length );
1944 break;
1945#endif
1946#if defined(MBEDTLS_RIPEMD160_C)
1947 case PSA_ALG_RIPEMD160:
1948 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1949 input, input_length );
1950 break;
1951#endif
1952#if defined(MBEDTLS_SHA1_C)
1953 case PSA_ALG_SHA_1:
1954 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1955 input, input_length );
1956 break;
1957#endif
1958#if defined(MBEDTLS_SHA256_C)
1959 case PSA_ALG_SHA_224:
1960 case PSA_ALG_SHA_256:
1961 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1962 input, input_length );
1963 break;
1964#endif
1965#if defined(MBEDTLS_SHA512_C)
1966 case PSA_ALG_SHA_384:
1967 case PSA_ALG_SHA_512:
1968 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1969 input, input_length );
1970 break;
1971#endif
1972 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001973 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001974 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001975
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001976 if( ret != 0 )
1977 psa_hash_abort( operation );
1978 return( mbedtls_to_psa_error( ret ) );
1979}
1980
1981psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1982 uint8_t *hash,
1983 size_t hash_size,
1984 size_t *hash_length )
1985{
itayzafrir40835d42018-08-02 13:14:17 +03001986 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001987 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001988 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001989
1990 /* Fill the output buffer with something that isn't a valid hash
1991 * (barring an attack on the hash and deliberately-crafted input),
1992 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001993 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001994 /* If hash_size is 0 then hash may be NULL and then the
1995 * call to memset would have undefined behavior. */
1996 if( hash_size != 0 )
1997 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001998
1999 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002000 {
2001 status = PSA_ERROR_BUFFER_TOO_SMALL;
2002 goto exit;
2003 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002004
2005 switch( operation->alg )
2006 {
2007#if defined(MBEDTLS_MD2_C)
2008 case PSA_ALG_MD2:
2009 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2010 break;
2011#endif
2012#if defined(MBEDTLS_MD4_C)
2013 case PSA_ALG_MD4:
2014 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2015 break;
2016#endif
2017#if defined(MBEDTLS_MD5_C)
2018 case PSA_ALG_MD5:
2019 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2020 break;
2021#endif
2022#if defined(MBEDTLS_RIPEMD160_C)
2023 case PSA_ALG_RIPEMD160:
2024 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2025 break;
2026#endif
2027#if defined(MBEDTLS_SHA1_C)
2028 case PSA_ALG_SHA_1:
2029 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2030 break;
2031#endif
2032#if defined(MBEDTLS_SHA256_C)
2033 case PSA_ALG_SHA_224:
2034 case PSA_ALG_SHA_256:
2035 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2036 break;
2037#endif
2038#if defined(MBEDTLS_SHA512_C)
2039 case PSA_ALG_SHA_384:
2040 case PSA_ALG_SHA_512:
2041 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2042 break;
2043#endif
2044 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002045 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002046 }
itayzafrir40835d42018-08-02 13:14:17 +03002047 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002048
itayzafrir40835d42018-08-02 13:14:17 +03002049exit:
2050 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002051 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002052 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002053 return( psa_hash_abort( operation ) );
2054 }
2055 else
2056 {
2057 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002058 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002059 }
2060}
2061
Gilles Peskine2d277862018-06-18 15:41:12 +02002062psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2063 const uint8_t *hash,
2064 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002065{
2066 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2067 size_t actual_hash_length;
2068 psa_status_t status = psa_hash_finish( operation,
2069 actual_hash, sizeof( actual_hash ),
2070 &actual_hash_length );
2071 if( status != PSA_SUCCESS )
2072 return( status );
2073 if( actual_hash_length != hash_length )
2074 return( PSA_ERROR_INVALID_SIGNATURE );
2075 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2076 return( PSA_ERROR_INVALID_SIGNATURE );
2077 return( PSA_SUCCESS );
2078}
2079
Gilles Peskineeb35d782019-01-22 17:56:16 +01002080psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2081 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002082{
2083 if( target_operation->alg != 0 )
2084 return( PSA_ERROR_BAD_STATE );
2085
2086 switch( source_operation->alg )
2087 {
2088 case 0:
2089 return( PSA_ERROR_BAD_STATE );
2090#if defined(MBEDTLS_MD2_C)
2091 case PSA_ALG_MD2:
2092 mbedtls_md2_clone( &target_operation->ctx.md2,
2093 &source_operation->ctx.md2 );
2094 break;
2095#endif
2096#if defined(MBEDTLS_MD4_C)
2097 case PSA_ALG_MD4:
2098 mbedtls_md4_clone( &target_operation->ctx.md4,
2099 &source_operation->ctx.md4 );
2100 break;
2101#endif
2102#if defined(MBEDTLS_MD5_C)
2103 case PSA_ALG_MD5:
2104 mbedtls_md5_clone( &target_operation->ctx.md5,
2105 &source_operation->ctx.md5 );
2106 break;
2107#endif
2108#if defined(MBEDTLS_RIPEMD160_C)
2109 case PSA_ALG_RIPEMD160:
2110 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2111 &source_operation->ctx.ripemd160 );
2112 break;
2113#endif
2114#if defined(MBEDTLS_SHA1_C)
2115 case PSA_ALG_SHA_1:
2116 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2117 &source_operation->ctx.sha1 );
2118 break;
2119#endif
2120#if defined(MBEDTLS_SHA256_C)
2121 case PSA_ALG_SHA_224:
2122 case PSA_ALG_SHA_256:
2123 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2124 &source_operation->ctx.sha256 );
2125 break;
2126#endif
2127#if defined(MBEDTLS_SHA512_C)
2128 case PSA_ALG_SHA_384:
2129 case PSA_ALG_SHA_512:
2130 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2131 &source_operation->ctx.sha512 );
2132 break;
2133#endif
2134 default:
2135 return( PSA_ERROR_NOT_SUPPORTED );
2136 }
2137
2138 target_operation->alg = source_operation->alg;
2139 return( PSA_SUCCESS );
2140}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002141
2142
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002143/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002144/* MAC */
2145/****************************************************************/
2146
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002147static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002148 psa_algorithm_t alg,
2149 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002150 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002151 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002152{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002153 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002154 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002155
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002156 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002157 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002158
Gilles Peskine8c9def32018-02-08 10:02:12 +01002159 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2160 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002161 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002162 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002163 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002164 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002165 mode = MBEDTLS_MODE_STREAM;
2166 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002167 case PSA_ALG_CTR:
2168 mode = MBEDTLS_MODE_CTR;
2169 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002170 case PSA_ALG_CFB:
2171 mode = MBEDTLS_MODE_CFB;
2172 break;
2173 case PSA_ALG_OFB:
2174 mode = MBEDTLS_MODE_OFB;
2175 break;
2176 case PSA_ALG_CBC_NO_PADDING:
2177 mode = MBEDTLS_MODE_CBC;
2178 break;
2179 case PSA_ALG_CBC_PKCS7:
2180 mode = MBEDTLS_MODE_CBC;
2181 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002182 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002183 mode = MBEDTLS_MODE_CCM;
2184 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002185 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002186 mode = MBEDTLS_MODE_GCM;
2187 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002188 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2189 mode = MBEDTLS_MODE_CHACHAPOLY;
2190 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002191 default:
2192 return( NULL );
2193 }
2194 }
2195 else if( alg == PSA_ALG_CMAC )
2196 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002197 else
2198 return( NULL );
2199
2200 switch( key_type )
2201 {
2202 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002203 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002204 break;
2205 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002206 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2207 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002208 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002209 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002210 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002211 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002212 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2213 * but two-key Triple-DES is functionally three-key Triple-DES
2214 * with K1=K3, so that's how we present it to mbedtls. */
2215 if( key_bits == 128 )
2216 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002217 break;
2218 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002219 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002220 break;
2221 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002222 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002223 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002224 case PSA_KEY_TYPE_CHACHA20:
2225 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2226 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002227 default:
2228 return( NULL );
2229 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002230 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002231 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002232
Jaeden Amero23bbb752018-06-26 14:16:54 +01002233 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2234 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002235}
2236
Gilles Peskinea05219c2018-11-16 16:02:56 +01002237#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002238static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002239{
Gilles Peskine2d277862018-06-18 15:41:12 +02002240 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002241 {
2242 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002243 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002244 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002245 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002246 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002247 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002248 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002249 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002250 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002251 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002252 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002253 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002254 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002255 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002256 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002257 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002258 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002259 return( 128 );
2260 default:
2261 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002262 }
2263}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002264#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002265
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002266/* Initialize the MAC operation structure. Once this function has been
2267 * called, psa_mac_abort can run and will do the right thing. */
2268static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2269 psa_algorithm_t alg )
2270{
2271 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2272
2273 operation->alg = alg;
2274 operation->key_set = 0;
2275 operation->iv_set = 0;
2276 operation->iv_required = 0;
2277 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002278 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002279
2280#if defined(MBEDTLS_CMAC_C)
2281 if( alg == PSA_ALG_CMAC )
2282 {
2283 operation->iv_required = 0;
2284 mbedtls_cipher_init( &operation->ctx.cmac );
2285 status = PSA_SUCCESS;
2286 }
2287 else
2288#endif /* MBEDTLS_CMAC_C */
2289#if defined(MBEDTLS_MD_C)
2290 if( PSA_ALG_IS_HMAC( operation->alg ) )
2291 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002292 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2293 operation->ctx.hmac.hash_ctx.alg = 0;
2294 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002295 }
2296 else
2297#endif /* MBEDTLS_MD_C */
2298 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002299 if( ! PSA_ALG_IS_MAC( alg ) )
2300 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002301 }
2302
2303 if( status != PSA_SUCCESS )
2304 memset( operation, 0, sizeof( *operation ) );
2305 return( status );
2306}
2307
Gilles Peskine01126fa2018-07-12 17:04:55 +02002308#if defined(MBEDTLS_MD_C)
2309static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2310{
Gilles Peskine3f108122018-12-07 18:14:53 +01002311 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002312 return( psa_hash_abort( &hmac->hash_ctx ) );
2313}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002314
Janos Follath999f6482019-06-11 12:04:10 +01002315#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002316static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2317{
2318 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2319 memset( hmac, 0, sizeof( *hmac ) );
2320}
Janos Follath999f6482019-06-11 12:04:10 +01002321#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002322#endif /* MBEDTLS_MD_C */
2323
Gilles Peskine8c9def32018-02-08 10:02:12 +01002324psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2325{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002326 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002327 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002328 /* The object has (apparently) been initialized but it is not
2329 * in use. It's ok to call abort on such an object, and there's
2330 * nothing to do. */
2331 return( PSA_SUCCESS );
2332 }
2333 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002334#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002335 if( operation->alg == PSA_ALG_CMAC )
2336 {
2337 mbedtls_cipher_free( &operation->ctx.cmac );
2338 }
2339 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002340#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002341#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002342 if( PSA_ALG_IS_HMAC( operation->alg ) )
2343 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002344 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002345 }
2346 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002347#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002348 {
2349 /* Sanity check (shouldn't happen: operation->alg should
2350 * always have been initialized to a valid value). */
2351 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002352 }
Moran Peker41deec42018-04-04 15:43:05 +03002353
Gilles Peskine8c9def32018-02-08 10:02:12 +01002354 operation->alg = 0;
2355 operation->key_set = 0;
2356 operation->iv_set = 0;
2357 operation->iv_required = 0;
2358 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002359 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002360
Gilles Peskine8c9def32018-02-08 10:02:12 +01002361 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002362
2363bad_state:
2364 /* If abort is called on an uninitialized object, we can't trust
2365 * anything. Wipe the object in case it contains confidential data.
2366 * This may result in a memory leak if a pointer gets overwritten,
2367 * but it's too late to do anything about this. */
2368 memset( operation, 0, sizeof( *operation ) );
2369 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002370}
2371
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002372#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002373static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002374 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002375 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002376 const mbedtls_cipher_info_t *cipher_info )
2377{
2378 int ret;
2379
2380 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002381
2382 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2383 if( ret != 0 )
2384 return( ret );
2385
2386 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2387 slot->data.raw.data,
2388 key_bits );
2389 return( ret );
2390}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002391#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002392
Gilles Peskine248051a2018-06-20 16:09:38 +02002393#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002394static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2395 const uint8_t *key,
2396 size_t key_length,
2397 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002398{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002399 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002400 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002401 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002402 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002403 psa_status_t status;
2404
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002405 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2406 * overflow below. This should never trigger if the hash algorithm
2407 * is implemented correctly. */
2408 /* The size checks against the ipad and opad buffers cannot be written
2409 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2410 * because that triggers -Wlogical-op on GCC 7.3. */
2411 if( block_size > sizeof( ipad ) )
2412 return( PSA_ERROR_NOT_SUPPORTED );
2413 if( block_size > sizeof( hmac->opad ) )
2414 return( PSA_ERROR_NOT_SUPPORTED );
2415 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002416 return( PSA_ERROR_NOT_SUPPORTED );
2417
Gilles Peskined223b522018-06-11 18:12:58 +02002418 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002419 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002420 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002421 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002422 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002423 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002424 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002425 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002426 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002427 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002428 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002429 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002430 }
Gilles Peskine96889972018-07-12 17:07:03 +02002431 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2432 * but it is permitted. It is common when HMAC is used in HKDF, for
2433 * example. Don't call `memcpy` in the 0-length because `key` could be
2434 * an invalid pointer which would make the behavior undefined. */
2435 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002436 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002437
Gilles Peskined223b522018-06-11 18:12:58 +02002438 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2439 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002440 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002441 ipad[i] ^= 0x36;
2442 memset( ipad + key_length, 0x36, block_size - key_length );
2443
2444 /* Copy the key material from ipad to opad, flipping the requisite bits,
2445 * and filling the rest of opad with the requisite constant. */
2446 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002447 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2448 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002449
Gilles Peskine01126fa2018-07-12 17:04:55 +02002450 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002451 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002452 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002453
Gilles Peskine01126fa2018-07-12 17:04:55 +02002454 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002455
2456cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002457 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002458
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002459 return( status );
2460}
Gilles Peskine248051a2018-06-20 16:09:38 +02002461#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002462
Gilles Peskine89167cb2018-07-08 20:12:23 +02002463static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002464 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002465 psa_algorithm_t alg,
2466 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002467{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002468 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002469 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002470 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002471 psa_key_usage_t usage =
2472 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002473 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002474 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002475
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002476 /* A context must be freshly initialized before it can be set up. */
2477 if( operation->alg != 0 )
2478 {
2479 return( PSA_ERROR_BAD_STATE );
2480 }
2481
Gilles Peskined911eb72018-08-14 15:18:45 +02002482 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002483 if( status != PSA_SUCCESS )
2484 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002485 if( is_sign )
2486 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002487
Gilles Peskinec5487a82018-12-03 18:08:14 +01002488 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002489 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002490 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002491 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002492
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002494 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002495 {
2496 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002497 mbedtls_cipher_info_from_psa( full_length_alg,
2498 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002499 int ret;
2500 if( cipher_info == NULL )
2501 {
2502 status = PSA_ERROR_NOT_SUPPORTED;
2503 goto exit;
2504 }
2505 operation->mac_size = cipher_info->block_size;
2506 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2507 status = mbedtls_to_psa_error( ret );
2508 }
2509 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002510#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002511#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002512 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002513 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002514 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002515 if( hash_alg == 0 )
2516 {
2517 status = PSA_ERROR_NOT_SUPPORTED;
2518 goto exit;
2519 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002520
2521 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2522 /* Sanity check. This shouldn't fail on a valid configuration. */
2523 if( operation->mac_size == 0 ||
2524 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2525 {
2526 status = PSA_ERROR_NOT_SUPPORTED;
2527 goto exit;
2528 }
2529
Gilles Peskine01126fa2018-07-12 17:04:55 +02002530 if( slot->type != PSA_KEY_TYPE_HMAC )
2531 {
2532 status = PSA_ERROR_INVALID_ARGUMENT;
2533 goto exit;
2534 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002535
Gilles Peskine01126fa2018-07-12 17:04:55 +02002536 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2537 slot->data.raw.data,
2538 slot->data.raw.bytes,
2539 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002540 }
2541 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002543 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002544 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002545 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002547
Gilles Peskined911eb72018-08-14 15:18:45 +02002548 if( truncated == 0 )
2549 {
2550 /* The "normal" case: untruncated algorithm. Nothing to do. */
2551 }
2552 else if( truncated < 4 )
2553 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002554 /* A very short MAC is too short for security since it can be
2555 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2556 * so we make this our minimum, even though 32 bits is still
2557 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002558 status = PSA_ERROR_NOT_SUPPORTED;
2559 }
2560 else if( truncated > operation->mac_size )
2561 {
2562 /* It's impossible to "truncate" to a larger length. */
2563 status = PSA_ERROR_INVALID_ARGUMENT;
2564 }
2565 else
2566 operation->mac_size = truncated;
2567
Gilles Peskinefbfac682018-07-08 20:51:54 +02002568exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002569 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002570 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002571 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002573 else
2574 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002575 operation->key_set = 1;
2576 }
2577 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002578}
2579
Gilles Peskine89167cb2018-07-08 20:12:23 +02002580psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002581 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002582 psa_algorithm_t alg )
2583{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002584 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002585}
2586
2587psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002588 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002589 psa_algorithm_t alg )
2590{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002591 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002592}
2593
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2595 const uint8_t *input,
2596 size_t input_length )
2597{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002598 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002599 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002600 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002601 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002602 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603 operation->has_input = 1;
2604
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002606 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002607 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002608 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2609 input, input_length );
2610 status = mbedtls_to_psa_error( ret );
2611 }
2612 else
2613#endif /* MBEDTLS_CMAC_C */
2614#if defined(MBEDTLS_MD_C)
2615 if( PSA_ALG_IS_HMAC( operation->alg ) )
2616 {
2617 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2618 input_length );
2619 }
2620 else
2621#endif /* MBEDTLS_MD_C */
2622 {
2623 /* This shouldn't happen if `operation` was initialized by
2624 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002625 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002626 }
2627
Gilles Peskinefbfac682018-07-08 20:51:54 +02002628 if( status != PSA_SUCCESS )
2629 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002630 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002631}
2632
Gilles Peskine01126fa2018-07-12 17:04:55 +02002633#if defined(MBEDTLS_MD_C)
2634static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2635 uint8_t *mac,
2636 size_t mac_size )
2637{
2638 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2639 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2640 size_t hash_size = 0;
2641 size_t block_size = psa_get_hash_block_size( hash_alg );
2642 psa_status_t status;
2643
Gilles Peskine01126fa2018-07-12 17:04:55 +02002644 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2645 if( status != PSA_SUCCESS )
2646 return( status );
2647 /* From here on, tmp needs to be wiped. */
2648
2649 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2650 if( status != PSA_SUCCESS )
2651 goto exit;
2652
2653 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2654 if( status != PSA_SUCCESS )
2655 goto exit;
2656
2657 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2658 if( status != PSA_SUCCESS )
2659 goto exit;
2660
Gilles Peskined911eb72018-08-14 15:18:45 +02002661 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2662 if( status != PSA_SUCCESS )
2663 goto exit;
2664
2665 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002666
2667exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002668 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002669 return( status );
2670}
2671#endif /* MBEDTLS_MD_C */
2672
mohammad16036df908f2018-04-02 08:34:15 -07002673static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002674 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002675 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002676{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002677 if( ! operation->key_set )
2678 return( PSA_ERROR_BAD_STATE );
2679 if( operation->iv_required && ! operation->iv_set )
2680 return( PSA_ERROR_BAD_STATE );
2681
Gilles Peskine8c9def32018-02-08 10:02:12 +01002682 if( mac_size < operation->mac_size )
2683 return( PSA_ERROR_BUFFER_TOO_SMALL );
2684
Gilles Peskine8c9def32018-02-08 10:02:12 +01002685#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002686 if( operation->alg == PSA_ALG_CMAC )
2687 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002688 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2689 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2690 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002691 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002692 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002693 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002694 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002695 else
2696#endif /* MBEDTLS_CMAC_C */
2697#if defined(MBEDTLS_MD_C)
2698 if( PSA_ALG_IS_HMAC( operation->alg ) )
2699 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002700 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002701 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002702 }
2703 else
2704#endif /* MBEDTLS_MD_C */
2705 {
2706 /* This shouldn't happen if `operation` was initialized by
2707 * a setup function. */
2708 return( PSA_ERROR_BAD_STATE );
2709 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710}
2711
Gilles Peskineacd4be32018-07-08 19:56:25 +02002712psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2713 uint8_t *mac,
2714 size_t mac_size,
2715 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002716{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002717 psa_status_t status;
2718
Jaeden Amero252ef282019-02-15 14:05:35 +00002719 if( operation->alg == 0 )
2720 {
2721 return( PSA_ERROR_BAD_STATE );
2722 }
2723
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002724 /* Fill the output buffer with something that isn't a valid mac
2725 * (barring an attack on the mac and deliberately-crafted input),
2726 * in case the caller doesn't check the return status properly. */
2727 *mac_length = mac_size;
2728 /* If mac_size is 0 then mac may be NULL and then the
2729 * call to memset would have undefined behavior. */
2730 if( mac_size != 0 )
2731 memset( mac, '!', mac_size );
2732
Gilles Peskine89167cb2018-07-08 20:12:23 +02002733 if( ! operation->is_sign )
2734 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002735 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002736 }
mohammad16036df908f2018-04-02 08:34:15 -07002737
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002738 status = psa_mac_finish_internal( operation, mac, mac_size );
2739
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002740 if( status == PSA_SUCCESS )
2741 {
2742 status = psa_mac_abort( operation );
2743 if( status == PSA_SUCCESS )
2744 *mac_length = operation->mac_size;
2745 else
2746 memset( mac, '!', mac_size );
2747 }
2748 else
2749 psa_mac_abort( operation );
2750 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002751}
2752
Gilles Peskineacd4be32018-07-08 19:56:25 +02002753psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2754 const uint8_t *mac,
2755 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002756{
Gilles Peskine828ed142018-06-18 23:25:51 +02002757 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002758 psa_status_t status;
2759
Jaeden Amero252ef282019-02-15 14:05:35 +00002760 if( operation->alg == 0 )
2761 {
2762 return( PSA_ERROR_BAD_STATE );
2763 }
2764
Gilles Peskine89167cb2018-07-08 20:12:23 +02002765 if( operation->is_sign )
2766 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002767 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002768 }
2769 if( operation->mac_size != mac_length )
2770 {
2771 status = PSA_ERROR_INVALID_SIGNATURE;
2772 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002773 }
mohammad16036df908f2018-04-02 08:34:15 -07002774
2775 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002776 actual_mac, sizeof( actual_mac ) );
2777
2778 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2779 status = PSA_ERROR_INVALID_SIGNATURE;
2780
2781cleanup:
2782 if( status == PSA_SUCCESS )
2783 status = psa_mac_abort( operation );
2784 else
2785 psa_mac_abort( operation );
2786
Gilles Peskine3f108122018-12-07 18:14:53 +01002787 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002788
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002789 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002790}
2791
2792
Gilles Peskine20035e32018-02-03 22:44:14 +01002793
Gilles Peskine20035e32018-02-03 22:44:14 +01002794/****************************************************************/
2795/* Asymmetric cryptography */
2796/****************************************************************/
2797
Gilles Peskine2b450e32018-06-27 15:42:46 +02002798#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002799/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002800 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002801static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2802 size_t hash_length,
2803 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002804{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002805 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002806 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002807 *md_alg = mbedtls_md_get_type( md_info );
2808
2809 /* The Mbed TLS RSA module uses an unsigned int for hash length
2810 * parameters. Validate that it fits so that we don't risk an
2811 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002812#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002813 if( hash_length > UINT_MAX )
2814 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002815#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002816
2817#if defined(MBEDTLS_PKCS1_V15)
2818 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2819 * must be correct. */
2820 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2821 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002822 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002823 if( md_info == NULL )
2824 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002825 if( mbedtls_md_get_size( md_info ) != hash_length )
2826 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002827 }
2828#endif /* MBEDTLS_PKCS1_V15 */
2829
2830#if defined(MBEDTLS_PKCS1_V21)
2831 /* PSS requires a hash internally. */
2832 if( PSA_ALG_IS_RSA_PSS( alg ) )
2833 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002834 if( md_info == NULL )
2835 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002836 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002837#endif /* MBEDTLS_PKCS1_V21 */
2838
Gilles Peskine61b91d42018-06-08 16:09:36 +02002839 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002840}
2841
Gilles Peskine2b450e32018-06-27 15:42:46 +02002842static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2843 psa_algorithm_t alg,
2844 const uint8_t *hash,
2845 size_t hash_length,
2846 uint8_t *signature,
2847 size_t signature_size,
2848 size_t *signature_length )
2849{
2850 psa_status_t status;
2851 int ret;
2852 mbedtls_md_type_t md_alg;
2853
2854 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2855 if( status != PSA_SUCCESS )
2856 return( status );
2857
Gilles Peskine630a18a2018-06-29 17:49:35 +02002858 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002859 return( PSA_ERROR_BUFFER_TOO_SMALL );
2860
2861#if defined(MBEDTLS_PKCS1_V15)
2862 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2863 {
2864 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2865 MBEDTLS_MD_NONE );
2866 ret = mbedtls_rsa_pkcs1_sign( rsa,
2867 mbedtls_ctr_drbg_random,
2868 &global_data.ctr_drbg,
2869 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002870 md_alg,
2871 (unsigned int) hash_length,
2872 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002873 signature );
2874 }
2875 else
2876#endif /* MBEDTLS_PKCS1_V15 */
2877#if defined(MBEDTLS_PKCS1_V21)
2878 if( PSA_ALG_IS_RSA_PSS( alg ) )
2879 {
2880 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2881 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2882 mbedtls_ctr_drbg_random,
2883 &global_data.ctr_drbg,
2884 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002885 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002886 (unsigned int) hash_length,
2887 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002888 signature );
2889 }
2890 else
2891#endif /* MBEDTLS_PKCS1_V21 */
2892 {
2893 return( PSA_ERROR_INVALID_ARGUMENT );
2894 }
2895
2896 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002897 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002898 return( mbedtls_to_psa_error( ret ) );
2899}
2900
2901static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2902 psa_algorithm_t alg,
2903 const uint8_t *hash,
2904 size_t hash_length,
2905 const uint8_t *signature,
2906 size_t signature_length )
2907{
2908 psa_status_t status;
2909 int ret;
2910 mbedtls_md_type_t md_alg;
2911
2912 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2913 if( status != PSA_SUCCESS )
2914 return( status );
2915
Gilles Peskine630a18a2018-06-29 17:49:35 +02002916 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002917 return( PSA_ERROR_BUFFER_TOO_SMALL );
2918
2919#if defined(MBEDTLS_PKCS1_V15)
2920 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2921 {
2922 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2923 MBEDTLS_MD_NONE );
2924 ret = mbedtls_rsa_pkcs1_verify( rsa,
2925 mbedtls_ctr_drbg_random,
2926 &global_data.ctr_drbg,
2927 MBEDTLS_RSA_PUBLIC,
2928 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002929 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002930 hash,
2931 signature );
2932 }
2933 else
2934#endif /* MBEDTLS_PKCS1_V15 */
2935#if defined(MBEDTLS_PKCS1_V21)
2936 if( PSA_ALG_IS_RSA_PSS( alg ) )
2937 {
2938 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2939 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2940 mbedtls_ctr_drbg_random,
2941 &global_data.ctr_drbg,
2942 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002943 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002944 (unsigned int) hash_length,
2945 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002946 signature );
2947 }
2948 else
2949#endif /* MBEDTLS_PKCS1_V21 */
2950 {
2951 return( PSA_ERROR_INVALID_ARGUMENT );
2952 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002953
2954 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2955 * the rest of the signature is invalid". This has little use in
2956 * practice and PSA doesn't report this distinction. */
2957 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2958 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002959 return( mbedtls_to_psa_error( ret ) );
2960}
2961#endif /* MBEDTLS_RSA_C */
2962
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002963#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002964/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2965 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2966 * (even though these functions don't modify it). */
2967static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2968 psa_algorithm_t alg,
2969 const uint8_t *hash,
2970 size_t hash_length,
2971 uint8_t *signature,
2972 size_t signature_size,
2973 size_t *signature_length )
2974{
2975 int ret;
2976 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002977 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002978 mbedtls_mpi_init( &r );
2979 mbedtls_mpi_init( &s );
2980
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002981 if( signature_size < 2 * curve_bytes )
2982 {
2983 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2984 goto cleanup;
2985 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002986
Gilles Peskinea05219c2018-11-16 16:02:56 +01002987#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002988 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2989 {
2990 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2991 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2992 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2993 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2994 hash, hash_length,
2995 md_alg ) );
2996 }
2997 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002998#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002999 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003000 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003001 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3002 hash, hash_length,
3003 mbedtls_ctr_drbg_random,
3004 &global_data.ctr_drbg ) );
3005 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003006
3007 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3008 signature,
3009 curve_bytes ) );
3010 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3011 signature + curve_bytes,
3012 curve_bytes ) );
3013
3014cleanup:
3015 mbedtls_mpi_free( &r );
3016 mbedtls_mpi_free( &s );
3017 if( ret == 0 )
3018 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003019 return( mbedtls_to_psa_error( ret ) );
3020}
3021
3022static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3023 const uint8_t *hash,
3024 size_t hash_length,
3025 const uint8_t *signature,
3026 size_t signature_length )
3027{
3028 int ret;
3029 mbedtls_mpi r, s;
3030 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3031 mbedtls_mpi_init( &r );
3032 mbedtls_mpi_init( &s );
3033
3034 if( signature_length != 2 * curve_bytes )
3035 return( PSA_ERROR_INVALID_SIGNATURE );
3036
3037 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3038 signature,
3039 curve_bytes ) );
3040 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3041 signature + curve_bytes,
3042 curve_bytes ) );
3043
3044 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3045 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003046
3047cleanup:
3048 mbedtls_mpi_free( &r );
3049 mbedtls_mpi_free( &s );
3050 return( mbedtls_to_psa_error( ret ) );
3051}
3052#endif /* MBEDTLS_ECDSA_C */
3053
Gilles Peskinec5487a82018-12-03 18:08:14 +01003054psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003055 psa_algorithm_t alg,
3056 const uint8_t *hash,
3057 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003058 uint8_t *signature,
3059 size_t signature_size,
3060 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003061{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003062 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003063 psa_status_t status;
3064
3065 *signature_length = signature_size;
3066
Gilles Peskinec5487a82018-12-03 18:08:14 +01003067 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003068 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003069 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003070 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003071 {
3072 status = PSA_ERROR_INVALID_ARGUMENT;
3073 goto exit;
3074 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003075
Gilles Peskine20035e32018-02-03 22:44:14 +01003076#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003077 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003078 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003079 status = psa_rsa_sign( slot->data.rsa,
3080 alg,
3081 hash, hash_length,
3082 signature, signature_size,
3083 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003084 }
3085 else
3086#endif /* defined(MBEDTLS_RSA_C) */
3087#if defined(MBEDTLS_ECP_C)
3088 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3089 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003090#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003091 if(
3092#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3093 PSA_ALG_IS_ECDSA( alg )
3094#else
3095 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3096#endif
3097 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003098 status = psa_ecdsa_sign( slot->data.ecp,
3099 alg,
3100 hash, hash_length,
3101 signature, signature_size,
3102 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003103 else
3104#endif /* defined(MBEDTLS_ECDSA_C) */
3105 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003106 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003107 }
itayzafrir5c753392018-05-08 11:18:38 +03003108 }
3109 else
3110#endif /* defined(MBEDTLS_ECP_C) */
3111 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003112 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003113 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003114
3115exit:
3116 /* Fill the unused part of the output buffer (the whole buffer on error,
3117 * the trailing part on success) with something that isn't a valid mac
3118 * (barring an attack on the mac and deliberately-crafted input),
3119 * in case the caller doesn't check the return status properly. */
3120 if( status == PSA_SUCCESS )
3121 memset( signature + *signature_length, '!',
3122 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003123 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003124 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003125 /* If signature_size is 0 then we have nothing to do. We must not call
3126 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003127 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003128}
3129
Gilles Peskinec5487a82018-12-03 18:08:14 +01003130psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003131 psa_algorithm_t alg,
3132 const uint8_t *hash,
3133 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003134 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003135 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003136{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003137 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003138 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003139
Gilles Peskinec5487a82018-12-03 18:08:14 +01003140 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003141 if( status != PSA_SUCCESS )
3142 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003143
Gilles Peskine61b91d42018-06-08 16:09:36 +02003144#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003145 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003146 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003147 return( psa_rsa_verify( slot->data.rsa,
3148 alg,
3149 hash, hash_length,
3150 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003151 }
3152 else
3153#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003154#if defined(MBEDTLS_ECP_C)
3155 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3156 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003157#if defined(MBEDTLS_ECDSA_C)
3158 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003159 return( psa_ecdsa_verify( slot->data.ecp,
3160 hash, hash_length,
3161 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003162 else
3163#endif /* defined(MBEDTLS_ECDSA_C) */
3164 {
3165 return( PSA_ERROR_INVALID_ARGUMENT );
3166 }
itayzafrir5c753392018-05-08 11:18:38 +03003167 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003168 else
3169#endif /* defined(MBEDTLS_ECP_C) */
3170 {
3171 return( PSA_ERROR_NOT_SUPPORTED );
3172 }
3173}
3174
Gilles Peskine072ac562018-06-30 00:21:29 +02003175#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3176static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3177 mbedtls_rsa_context *rsa )
3178{
3179 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3180 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3181 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3182 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3183}
3184#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3185
Gilles Peskinec5487a82018-12-03 18:08:14 +01003186psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003187 psa_algorithm_t alg,
3188 const uint8_t *input,
3189 size_t input_length,
3190 const uint8_t *salt,
3191 size_t salt_length,
3192 uint8_t *output,
3193 size_t output_size,
3194 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003195{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003196 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003197 psa_status_t status;
3198
Darryl Green5cc689a2018-07-24 15:34:10 +01003199 (void) input;
3200 (void) input_length;
3201 (void) salt;
3202 (void) output;
3203 (void) output_size;
3204
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003205 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003206
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003207 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3208 return( PSA_ERROR_INVALID_ARGUMENT );
3209
Gilles Peskinec5487a82018-12-03 18:08:14 +01003210 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003211 if( status != PSA_SUCCESS )
3212 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003213 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003214 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003215 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003216
3217#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003218 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003219 {
3220 mbedtls_rsa_context *rsa = slot->data.rsa;
3221 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003222 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003223 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003224#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003225 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003226 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003227 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3228 mbedtls_ctr_drbg_random,
3229 &global_data.ctr_drbg,
3230 MBEDTLS_RSA_PUBLIC,
3231 input_length,
3232 input,
3233 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003234 }
3235 else
3236#endif /* MBEDTLS_PKCS1_V15 */
3237#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003238 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003239 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003240 psa_rsa_oaep_set_padding_mode( alg, rsa );
3241 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3242 mbedtls_ctr_drbg_random,
3243 &global_data.ctr_drbg,
3244 MBEDTLS_RSA_PUBLIC,
3245 salt, salt_length,
3246 input_length,
3247 input,
3248 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003249 }
3250 else
3251#endif /* MBEDTLS_PKCS1_V21 */
3252 {
3253 return( PSA_ERROR_INVALID_ARGUMENT );
3254 }
3255 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003256 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003257 return( mbedtls_to_psa_error( ret ) );
3258 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003259 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003260#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003261 {
3262 return( PSA_ERROR_NOT_SUPPORTED );
3263 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003264}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003265
Gilles Peskinec5487a82018-12-03 18:08:14 +01003266psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003267 psa_algorithm_t alg,
3268 const uint8_t *input,
3269 size_t input_length,
3270 const uint8_t *salt,
3271 size_t salt_length,
3272 uint8_t *output,
3273 size_t output_size,
3274 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003275{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003276 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003277 psa_status_t status;
3278
Darryl Green5cc689a2018-07-24 15:34:10 +01003279 (void) input;
3280 (void) input_length;
3281 (void) salt;
3282 (void) output;
3283 (void) output_size;
3284
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003285 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003286
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003287 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3288 return( PSA_ERROR_INVALID_ARGUMENT );
3289
Gilles Peskinec5487a82018-12-03 18:08:14 +01003290 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003291 if( status != PSA_SUCCESS )
3292 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003293 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003294 return( PSA_ERROR_INVALID_ARGUMENT );
3295
3296#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003297 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003298 {
3299 mbedtls_rsa_context *rsa = slot->data.rsa;
3300 int ret;
3301
Gilles Peskine630a18a2018-06-29 17:49:35 +02003302 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003303 return( PSA_ERROR_INVALID_ARGUMENT );
3304
3305#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003306 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003307 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003308 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3309 mbedtls_ctr_drbg_random,
3310 &global_data.ctr_drbg,
3311 MBEDTLS_RSA_PRIVATE,
3312 output_length,
3313 input,
3314 output,
3315 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003316 }
3317 else
3318#endif /* MBEDTLS_PKCS1_V15 */
3319#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003320 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003321 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003322 psa_rsa_oaep_set_padding_mode( alg, rsa );
3323 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3324 mbedtls_ctr_drbg_random,
3325 &global_data.ctr_drbg,
3326 MBEDTLS_RSA_PRIVATE,
3327 salt, salt_length,
3328 output_length,
3329 input,
3330 output,
3331 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003332 }
3333 else
3334#endif /* MBEDTLS_PKCS1_V21 */
3335 {
3336 return( PSA_ERROR_INVALID_ARGUMENT );
3337 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003338
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003339 return( mbedtls_to_psa_error( ret ) );
3340 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003341 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003342#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003343 {
3344 return( PSA_ERROR_NOT_SUPPORTED );
3345 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003346}
Gilles Peskine20035e32018-02-03 22:44:14 +01003347
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003348
3349
mohammad1603503973b2018-03-12 15:59:30 +02003350/****************************************************************/
3351/* Symmetric cryptography */
3352/****************************************************************/
3353
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003354/* Initialize the cipher operation structure. Once this function has been
3355 * called, psa_cipher_abort can run and will do the right thing. */
3356static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3357 psa_algorithm_t alg )
3358{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003359 if( ! PSA_ALG_IS_CIPHER( alg ) )
3360 {
3361 memset( operation, 0, sizeof( *operation ) );
3362 return( PSA_ERROR_INVALID_ARGUMENT );
3363 }
3364
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003365 operation->alg = alg;
3366 operation->key_set = 0;
3367 operation->iv_set = 0;
3368 operation->iv_required = 1;
3369 operation->iv_size = 0;
3370 operation->block_size = 0;
3371 mbedtls_cipher_init( &operation->ctx.cipher );
3372 return( PSA_SUCCESS );
3373}
3374
Gilles Peskinee553c652018-06-04 16:22:46 +02003375static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003376 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003377 psa_algorithm_t alg,
3378 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003379{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003380 int ret = 0;
3381 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003382 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003383 size_t key_bits;
3384 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003385 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3386 PSA_KEY_USAGE_ENCRYPT :
3387 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003388
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003389 /* A context must be freshly initialized before it can be set up. */
3390 if( operation->alg != 0 )
3391 {
3392 return( PSA_ERROR_BAD_STATE );
3393 }
3394
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003395 status = psa_cipher_init( operation, alg );
3396 if( status != PSA_SUCCESS )
3397 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003398
Gilles Peskinec5487a82018-12-03 18:08:14 +01003399 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003400 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003401 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003402 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003403
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003404 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003405 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003406 {
3407 status = PSA_ERROR_NOT_SUPPORTED;
3408 goto exit;
3409 }
mohammad1603503973b2018-03-12 15:59:30 +02003410
mohammad1603503973b2018-03-12 15:59:30 +02003411 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003412 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003413 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003414
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003415#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003416 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003417 {
3418 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3419 unsigned char keys[24];
3420 memcpy( keys, slot->data.raw.data, 16 );
3421 memcpy( keys + 16, slot->data.raw.data, 8 );
3422 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3423 keys,
3424 192, cipher_operation );
3425 }
3426 else
3427#endif
3428 {
3429 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3430 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003431 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003432 }
Moran Peker41deec42018-04-04 15:43:05 +03003433 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003434 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003435
mohammad16038481e742018-03-18 13:57:31 +02003436#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003437 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003438 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003439 case PSA_ALG_CBC_NO_PADDING:
3440 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3441 MBEDTLS_PADDING_NONE );
3442 break;
3443 case PSA_ALG_CBC_PKCS7:
3444 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3445 MBEDTLS_PADDING_PKCS7 );
3446 break;
3447 default:
3448 /* The algorithm doesn't involve padding. */
3449 ret = 0;
3450 break;
3451 }
3452 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003453 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003454#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3455
mohammad1603503973b2018-03-12 15:59:30 +02003456 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003457 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3458 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3459 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003460 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003461 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003462 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003463#if defined(MBEDTLS_CHACHA20_C)
3464 else
3465 if( alg == PSA_ALG_CHACHA20 )
3466 operation->iv_size = 12;
3467#endif
mohammad1603503973b2018-03-12 15:59:30 +02003468
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003469exit:
3470 if( status == 0 )
3471 status = mbedtls_to_psa_error( ret );
3472 if( status != 0 )
3473 psa_cipher_abort( operation );
3474 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003475}
3476
Gilles Peskinefe119512018-07-08 21:39:34 +02003477psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003478 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003479 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003480{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003481 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003482}
3483
Gilles Peskinefe119512018-07-08 21:39:34 +02003484psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003485 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003486 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003487{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003488 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003489}
3490
Gilles Peskinefe119512018-07-08 21:39:34 +02003491psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3492 unsigned char *iv,
3493 size_t iv_size,
3494 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003495{
itayzafrir534bd7c2018-08-02 13:56:32 +03003496 psa_status_t status;
3497 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003498 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003499 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003500 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003501 }
Moran Peker41deec42018-04-04 15:43:05 +03003502 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003503 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003504 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003505 goto exit;
3506 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003507 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3508 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003509 if( ret != 0 )
3510 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003511 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003512 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003513 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003514
mohammad16038481e742018-03-18 13:57:31 +02003515 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003516 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003517
Moran Peker395db872018-05-31 14:07:14 +03003518exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003519 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003520 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003521 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003522}
3523
Gilles Peskinefe119512018-07-08 21:39:34 +02003524psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3525 const unsigned char *iv,
3526 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003527{
itayzafrir534bd7c2018-08-02 13:56:32 +03003528 psa_status_t status;
3529 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003530 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003531 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003532 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003533 }
Moran Pekera28258c2018-05-29 16:25:04 +03003534 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003535 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003536 status = PSA_ERROR_INVALID_ARGUMENT;
3537 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003538 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003539 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3540 status = mbedtls_to_psa_error( ret );
3541exit:
3542 if( status == PSA_SUCCESS )
3543 operation->iv_set = 1;
3544 else
mohammad1603503973b2018-03-12 15:59:30 +02003545 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003546 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003547}
3548
Gilles Peskinee553c652018-06-04 16:22:46 +02003549psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3550 const uint8_t *input,
3551 size_t input_length,
3552 unsigned char *output,
3553 size_t output_size,
3554 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003555{
itayzafrir534bd7c2018-08-02 13:56:32 +03003556 psa_status_t status;
3557 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003558 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003559
3560 if( operation->alg == 0 )
3561 {
3562 return( PSA_ERROR_BAD_STATE );
3563 }
3564
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003565 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003566 {
3567 /* Take the unprocessed partial block left over from previous
3568 * update calls, if any, plus the input to this call. Remove
3569 * the last partial block, if any. You get the data that will be
3570 * output in this call. */
3571 expected_output_size =
3572 ( operation->ctx.cipher.unprocessed_len + input_length )
3573 / operation->block_size * operation->block_size;
3574 }
3575 else
3576 {
3577 expected_output_size = input_length;
3578 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003579
Gilles Peskine89d789c2018-06-04 17:17:16 +02003580 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003581 {
3582 status = PSA_ERROR_BUFFER_TOO_SMALL;
3583 goto exit;
3584 }
mohammad160382759612018-03-12 18:16:40 +02003585
mohammad1603503973b2018-03-12 15:59:30 +02003586 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003587 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003588 status = mbedtls_to_psa_error( ret );
3589exit:
3590 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003591 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003592 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003593}
3594
Gilles Peskinee553c652018-06-04 16:22:46 +02003595psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3596 uint8_t *output,
3597 size_t output_size,
3598 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003599{
David Saadab4ecc272019-02-14 13:48:10 +02003600 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003601 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003602 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003603
mohammad1603503973b2018-03-12 15:59:30 +02003604 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003605 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003606 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003607 }
3608 if( operation->iv_required && ! operation->iv_set )
3609 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003610 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003611 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003612
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003613 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003614 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3615 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003616 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003617 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003618 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003619 }
3620
Janos Follath315b51c2018-07-09 16:04:51 +01003621 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3622 temp_output_buffer,
3623 output_length );
3624 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003625 {
Janos Follath315b51c2018-07-09 16:04:51 +01003626 status = mbedtls_to_psa_error( cipher_ret );
3627 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003628 }
Janos Follath315b51c2018-07-09 16:04:51 +01003629
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003630 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003631 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003632 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003633 memcpy( output, temp_output_buffer, *output_length );
3634 else
3635 {
Janos Follath315b51c2018-07-09 16:04:51 +01003636 status = PSA_ERROR_BUFFER_TOO_SMALL;
3637 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003638 }
mohammad1603503973b2018-03-12 15:59:30 +02003639
Gilles Peskine3f108122018-12-07 18:14:53 +01003640 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003641 status = psa_cipher_abort( operation );
3642
3643 return( status );
3644
3645error:
3646
3647 *output_length = 0;
3648
Gilles Peskine3f108122018-12-07 18:14:53 +01003649 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003650 (void) psa_cipher_abort( operation );
3651
3652 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003653}
3654
Gilles Peskinee553c652018-06-04 16:22:46 +02003655psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3656{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003657 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003658 {
3659 /* The object has (apparently) been initialized but it is not
3660 * in use. It's ok to call abort on such an object, and there's
3661 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003662 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003663 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003664
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003665 /* Sanity check (shouldn't happen: operation->alg should
3666 * always have been initialized to a valid value). */
3667 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3668 return( PSA_ERROR_BAD_STATE );
3669
mohammad1603503973b2018-03-12 15:59:30 +02003670 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003671
Moran Peker41deec42018-04-04 15:43:05 +03003672 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003673 operation->key_set = 0;
3674 operation->iv_set = 0;
3675 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003676 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003677 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003678
Moran Peker395db872018-05-31 14:07:14 +03003679 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003680}
3681
Gilles Peskinea0655c32018-04-30 17:06:50 +02003682
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003683
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003684
mohammad16035955c982018-04-26 00:53:03 +03003685/****************************************************************/
3686/* AEAD */
3687/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003688
Gilles Peskineedf9a652018-08-17 18:11:56 +02003689typedef struct
3690{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003691 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003692 const mbedtls_cipher_info_t *cipher_info;
3693 union
3694 {
3695#if defined(MBEDTLS_CCM_C)
3696 mbedtls_ccm_context ccm;
3697#endif /* MBEDTLS_CCM_C */
3698#if defined(MBEDTLS_GCM_C)
3699 mbedtls_gcm_context gcm;
3700#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003701#if defined(MBEDTLS_CHACHAPOLY_C)
3702 mbedtls_chachapoly_context chachapoly;
3703#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003704 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003705 psa_algorithm_t core_alg;
3706 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003707 uint8_t tag_length;
3708} aead_operation_t;
3709
Gilles Peskine30a9e412019-01-14 18:36:12 +01003710static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003711{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003712 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003713 {
3714#if defined(MBEDTLS_CCM_C)
3715 case PSA_ALG_CCM:
3716 mbedtls_ccm_free( &operation->ctx.ccm );
3717 break;
3718#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003719#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003720 case PSA_ALG_GCM:
3721 mbedtls_gcm_free( &operation->ctx.gcm );
3722 break;
3723#endif /* MBEDTLS_GCM_C */
3724 }
3725}
3726
3727static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003728 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003729 psa_key_usage_t usage,
3730 psa_algorithm_t alg )
3731{
3732 psa_status_t status;
3733 size_t key_bits;
3734 mbedtls_cipher_id_t cipher_id;
3735
Gilles Peskinec5487a82018-12-03 18:08:14 +01003736 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003737 if( status != PSA_SUCCESS )
3738 return( status );
3739
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003740 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003741
3742 operation->cipher_info =
3743 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3744 &cipher_id );
3745 if( operation->cipher_info == NULL )
3746 return( PSA_ERROR_NOT_SUPPORTED );
3747
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003748 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003749 {
3750#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003751 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3752 operation->core_alg = PSA_ALG_CCM;
3753 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003754 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3755 * The call to mbedtls_ccm_encrypt_and_tag or
3756 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003757 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3758 return( PSA_ERROR_INVALID_ARGUMENT );
3759 mbedtls_ccm_init( &operation->ctx.ccm );
3760 status = mbedtls_to_psa_error(
3761 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3762 operation->slot->data.raw.data,
3763 (unsigned int) key_bits ) );
3764 if( status != 0 )
3765 goto cleanup;
3766 break;
3767#endif /* MBEDTLS_CCM_C */
3768
3769#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003770 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3771 operation->core_alg = PSA_ALG_GCM;
3772 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003773 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3774 * The call to mbedtls_gcm_crypt_and_tag or
3775 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003776 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3777 return( PSA_ERROR_INVALID_ARGUMENT );
3778 mbedtls_gcm_init( &operation->ctx.gcm );
3779 status = mbedtls_to_psa_error(
3780 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3781 operation->slot->data.raw.data,
3782 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003783 if( status != 0 )
3784 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003785 break;
3786#endif /* MBEDTLS_GCM_C */
3787
Gilles Peskine26869f22019-05-06 15:25:00 +02003788#if defined(MBEDTLS_CHACHAPOLY_C)
3789 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3790 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3791 operation->full_tag_length = 16;
3792 /* We only support the default tag length. */
3793 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3794 return( PSA_ERROR_NOT_SUPPORTED );
3795 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3796 status = mbedtls_to_psa_error(
3797 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3798 operation->slot->data.raw.data ) );
3799 if( status != 0 )
3800 goto cleanup;
3801 break;
3802#endif /* MBEDTLS_CHACHAPOLY_C */
3803
Gilles Peskineedf9a652018-08-17 18:11:56 +02003804 default:
3805 return( PSA_ERROR_NOT_SUPPORTED );
3806 }
3807
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003808 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3809 {
3810 status = PSA_ERROR_INVALID_ARGUMENT;
3811 goto cleanup;
3812 }
3813 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003814
Gilles Peskineedf9a652018-08-17 18:11:56 +02003815 return( PSA_SUCCESS );
3816
3817cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003818 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003819 return( status );
3820}
3821
Gilles Peskinec5487a82018-12-03 18:08:14 +01003822psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003823 psa_algorithm_t alg,
3824 const uint8_t *nonce,
3825 size_t nonce_length,
3826 const uint8_t *additional_data,
3827 size_t additional_data_length,
3828 const uint8_t *plaintext,
3829 size_t plaintext_length,
3830 uint8_t *ciphertext,
3831 size_t ciphertext_size,
3832 size_t *ciphertext_length )
3833{
mohammad16035955c982018-04-26 00:53:03 +03003834 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003835 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003836 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003837
mohammad1603f08a5502018-06-03 15:05:47 +03003838 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003839
Gilles Peskinec5487a82018-12-03 18:08:14 +01003840 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003841 if( status != PSA_SUCCESS )
3842 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003843
Gilles Peskineedf9a652018-08-17 18:11:56 +02003844 /* For all currently supported modes, the tag is at the end of the
3845 * ciphertext. */
3846 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3847 {
3848 status = PSA_ERROR_BUFFER_TOO_SMALL;
3849 goto exit;
3850 }
3851 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003852
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003853#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003854 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003855 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003856 status = mbedtls_to_psa_error(
3857 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3858 MBEDTLS_GCM_ENCRYPT,
3859 plaintext_length,
3860 nonce, nonce_length,
3861 additional_data, additional_data_length,
3862 plaintext, ciphertext,
3863 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003864 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003865 else
3866#endif /* MBEDTLS_GCM_C */
3867#if defined(MBEDTLS_CCM_C)
3868 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003869 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003870 status = mbedtls_to_psa_error(
3871 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3872 plaintext_length,
3873 nonce, nonce_length,
3874 additional_data,
3875 additional_data_length,
3876 plaintext, ciphertext,
3877 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003878 }
mohammad16035c8845f2018-05-09 05:40:09 -07003879 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003880#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003881#if defined(MBEDTLS_CHACHAPOLY_C)
3882 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3883 {
3884 if( nonce_length != 12 || operation.tag_length != 16 )
3885 {
3886 status = PSA_ERROR_NOT_SUPPORTED;
3887 goto exit;
3888 }
3889 status = mbedtls_to_psa_error(
3890 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3891 plaintext_length,
3892 nonce,
3893 additional_data,
3894 additional_data_length,
3895 plaintext,
3896 ciphertext,
3897 tag ) );
3898 }
3899 else
3900#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003901 {
mohammad1603554faad2018-06-03 15:07:38 +03003902 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003903 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003904
Gilles Peskineedf9a652018-08-17 18:11:56 +02003905 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3906 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003907
Gilles Peskineedf9a652018-08-17 18:11:56 +02003908exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003909 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003910 if( status == PSA_SUCCESS )
3911 *ciphertext_length = plaintext_length + operation.tag_length;
3912 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003913}
3914
Gilles Peskineee652a32018-06-01 19:23:52 +02003915/* Locate the tag in a ciphertext buffer containing the encrypted data
3916 * followed by the tag. Return the length of the part preceding the tag in
3917 * *plaintext_length. This is the size of the plaintext in modes where
3918 * the encrypted data has the same size as the plaintext, such as
3919 * CCM and GCM. */
3920static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3921 const uint8_t *ciphertext,
3922 size_t ciphertext_length,
3923 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003924 const uint8_t **p_tag )
3925{
3926 size_t payload_length;
3927 if( tag_length > ciphertext_length )
3928 return( PSA_ERROR_INVALID_ARGUMENT );
3929 payload_length = ciphertext_length - tag_length;
3930 if( payload_length > plaintext_size )
3931 return( PSA_ERROR_BUFFER_TOO_SMALL );
3932 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003933 return( PSA_SUCCESS );
3934}
3935
Gilles Peskinec5487a82018-12-03 18:08:14 +01003936psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003937 psa_algorithm_t alg,
3938 const uint8_t *nonce,
3939 size_t nonce_length,
3940 const uint8_t *additional_data,
3941 size_t additional_data_length,
3942 const uint8_t *ciphertext,
3943 size_t ciphertext_length,
3944 uint8_t *plaintext,
3945 size_t plaintext_size,
3946 size_t *plaintext_length )
3947{
mohammad16035955c982018-04-26 00:53:03 +03003948 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003949 aead_operation_t operation;
3950 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003951
Gilles Peskineee652a32018-06-01 19:23:52 +02003952 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003953
Gilles Peskinec5487a82018-12-03 18:08:14 +01003954 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003955 if( status != PSA_SUCCESS )
3956 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003957
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003958 status = psa_aead_unpadded_locate_tag( operation.tag_length,
3959 ciphertext, ciphertext_length,
3960 plaintext_size, &tag );
3961 if( status != PSA_SUCCESS )
3962 goto exit;
3963
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003964#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003965 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003966 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003967 status = mbedtls_to_psa_error(
3968 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3969 ciphertext_length - operation.tag_length,
3970 nonce, nonce_length,
3971 additional_data,
3972 additional_data_length,
3973 tag, operation.tag_length,
3974 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003975 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003976 else
3977#endif /* MBEDTLS_GCM_C */
3978#if defined(MBEDTLS_CCM_C)
3979 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003980 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003981 status = mbedtls_to_psa_error(
3982 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3983 ciphertext_length - operation.tag_length,
3984 nonce, nonce_length,
3985 additional_data,
3986 additional_data_length,
3987 ciphertext, plaintext,
3988 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003989 }
mohammad160339574652018-06-01 04:39:53 -07003990 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003991#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003992#if defined(MBEDTLS_CHACHAPOLY_C)
3993 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3994 {
3995 if( nonce_length != 12 || operation.tag_length != 16 )
3996 {
3997 status = PSA_ERROR_NOT_SUPPORTED;
3998 goto exit;
3999 }
4000 status = mbedtls_to_psa_error(
4001 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4002 ciphertext_length - operation.tag_length,
4003 nonce,
4004 additional_data,
4005 additional_data_length,
4006 tag,
4007 ciphertext,
4008 plaintext ) );
4009 }
4010 else
4011#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004012 {
mohammad1603554faad2018-06-03 15:07:38 +03004013 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004014 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004015
Gilles Peskineedf9a652018-08-17 18:11:56 +02004016 if( status != PSA_SUCCESS && plaintext_size != 0 )
4017 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004018
Gilles Peskineedf9a652018-08-17 18:11:56 +02004019exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004020 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004021 if( status == PSA_SUCCESS )
4022 *plaintext_length = ciphertext_length - operation.tag_length;
4023 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004024}
4025
Gilles Peskinea0655c32018-04-30 17:06:50 +02004026
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004027
Gilles Peskine20035e32018-02-03 22:44:14 +01004028/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004029/* Generators */
4030/****************************************************************/
4031
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004032#define HKDF_STATE_INIT 0 /* no input yet */
4033#define HKDF_STATE_STARTED 1 /* got salt */
4034#define HKDF_STATE_KEYED 2 /* got key */
4035#define HKDF_STATE_OUTPUT 3 /* output started */
4036
Gilles Peskinecbe66502019-05-16 16:59:18 +02004037static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004038 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004039{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004040 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4041 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004042 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004043 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004044}
4045
4046
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004047psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004048{
4049 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004050 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004051 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004052 {
4053 /* The object has (apparently) been initialized but it is not
4054 * in use. It's ok to call abort on such an object, and there's
4055 * nothing to do. */
4056 }
4057 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004058#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004059 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004060 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004061 mbedtls_free( operation->ctx.hkdf.info );
4062 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004063 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004064 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004065 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004066 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004067 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004068#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004069 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004070 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004071 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4072 operation->ctx.tls12_prf.key_len );
4073 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004074 }
Hanno Becker580fba12018-11-13 20:50:45 +00004075
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004076 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004077 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004078 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4079 operation->ctx.tls12_prf.Ai_with_seed_len );
4080 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004081 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004082#else
4083 if( operation->ctx.tls12_prf.seed != NULL )
4084 {
4085 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4086 operation->ctx.tls12_prf.seed_length );
4087 mbedtls_free( operation->ctx.tls12_prf.seed );
4088 }
4089
4090 if( operation->ctx.tls12_prf.label != NULL )
4091 {
4092 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4093 operation->ctx.tls12_prf.label_length );
4094 mbedtls_free( operation->ctx.tls12_prf.label );
4095 }
4096
4097 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4098
4099 /* We leave the fields Ai and output_block to be erased safely by the
4100 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004101#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004102 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004103 else
4104#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004105 {
4106 status = PSA_ERROR_BAD_STATE;
4107 }
Janos Follath083036a2019-06-11 10:22:26 +01004108 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004109 return( status );
4110}
4111
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004112psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004113 size_t *capacity)
4114{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004115 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004116 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004117 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004118 return PSA_ERROR_BAD_STATE;
4119 }
4120
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004121 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004122 return( PSA_SUCCESS );
4123}
4124
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004125psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004126 size_t capacity )
4127{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004128 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004129 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004130 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004131 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004132 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004133 return( PSA_SUCCESS );
4134}
4135
Gilles Peskinebef7f142018-07-12 17:22:21 +02004136#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004137/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004138 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004139static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004140 psa_algorithm_t hash_alg,
4141 uint8_t *output,
4142 size_t output_length )
4143{
4144 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4145 psa_status_t status;
4146
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004147 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4148 return( PSA_ERROR_BAD_STATE );
4149 hkdf->state = HKDF_STATE_OUTPUT;
4150
Gilles Peskinebef7f142018-07-12 17:22:21 +02004151 while( output_length != 0 )
4152 {
4153 /* Copy what remains of the current block */
4154 uint8_t n = hash_length - hkdf->offset_in_block;
4155 if( n > output_length )
4156 n = (uint8_t) output_length;
4157 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4158 output += n;
4159 output_length -= n;
4160 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004161 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004162 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004163 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004164 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004165 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004166 * object was corrupted or if this function is called directly
4167 * inside the library. */
4168 if( hkdf->block_number == 0xff )
4169 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004170
4171 /* We need a new block */
4172 ++hkdf->block_number;
4173 hkdf->offset_in_block = 0;
4174 status = psa_hmac_setup_internal( &hkdf->hmac,
4175 hkdf->prk, hash_length,
4176 hash_alg );
4177 if( status != PSA_SUCCESS )
4178 return( status );
4179 if( hkdf->block_number != 1 )
4180 {
4181 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4182 hkdf->output_block,
4183 hash_length );
4184 if( status != PSA_SUCCESS )
4185 return( status );
4186 }
4187 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4188 hkdf->info,
4189 hkdf->info_length );
4190 if( status != PSA_SUCCESS )
4191 return( status );
4192 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4193 &hkdf->block_number, 1 );
4194 if( status != PSA_SUCCESS )
4195 return( status );
4196 status = psa_hmac_finish_internal( &hkdf->hmac,
4197 hkdf->output_block,
4198 sizeof( hkdf->output_block ) );
4199 if( status != PSA_SUCCESS )
4200 return( status );
4201 }
4202
4203 return( PSA_SUCCESS );
4204}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004205
Janos Follath999f6482019-06-11 12:04:10 +01004206#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004207static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4208 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004209 psa_algorithm_t alg )
4210{
4211 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4212 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4213 psa_hmac_internal_data hmac;
4214 psa_status_t status, cleanup_status;
4215
Hanno Becker3b339e22018-11-13 20:56:14 +00004216 unsigned char *Ai;
4217 size_t Ai_len;
4218
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004219 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004220 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004221 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004222 * object was corrupted or if this function is called directly
4223 * inside the library. */
4224 if( tls12_prf->block_number == 0xff )
4225 return( PSA_ERROR_BAD_STATE );
4226
4227 /* We need a new block */
4228 ++tls12_prf->block_number;
4229 tls12_prf->offset_in_block = 0;
4230
4231 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4232 *
4233 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4234 *
4235 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4236 * HMAC_hash(secret, A(2) + seed) +
4237 * HMAC_hash(secret, A(3) + seed) + ...
4238 *
4239 * A(0) = seed
4240 * A(i) = HMAC_hash( secret, A(i-1) )
4241 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004242 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004243 * `HMAC_hash(secret, A(i) + seed)` from which the output
4244 * is currently extracted as `output_block`, while
4245 * `A(i) + seed` is stored in `Ai_with_seed`.
4246 *
4247 * Generating a new block means recalculating `Ai_with_seed`
4248 * from the A(i)-part of it, and afterwards recalculating
4249 * `output_block`.
4250 *
4251 * A(0) is computed at setup time.
4252 *
4253 */
4254
4255 psa_hmac_init_internal( &hmac );
4256
4257 /* We must distinguish the calculation of A(1) from those
4258 * of A(2) and higher, because A(0)=seed has a different
4259 * length than the other A(i). */
4260 if( tls12_prf->block_number == 1 )
4261 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004262 Ai = tls12_prf->Ai_with_seed + hash_length;
4263 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004264 }
4265 else
4266 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004267 Ai = tls12_prf->Ai_with_seed;
4268 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004269 }
4270
Hanno Becker3b339e22018-11-13 20:56:14 +00004271 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4272 status = psa_hmac_setup_internal( &hmac,
4273 tls12_prf->key,
4274 tls12_prf->key_len,
4275 hash_alg );
4276 if( status != PSA_SUCCESS )
4277 goto cleanup;
4278
4279 status = psa_hash_update( &hmac.hash_ctx,
4280 Ai, Ai_len );
4281 if( status != PSA_SUCCESS )
4282 goto cleanup;
4283
4284 status = psa_hmac_finish_internal( &hmac,
4285 tls12_prf->Ai_with_seed,
4286 hash_length );
4287 if( status != PSA_SUCCESS )
4288 goto cleanup;
4289
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004290 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4291 status = psa_hmac_setup_internal( &hmac,
4292 tls12_prf->key,
4293 tls12_prf->key_len,
4294 hash_alg );
4295 if( status != PSA_SUCCESS )
4296 goto cleanup;
4297
4298 status = psa_hash_update( &hmac.hash_ctx,
4299 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004300 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004301 if( status != PSA_SUCCESS )
4302 goto cleanup;
4303
4304 status = psa_hmac_finish_internal( &hmac,
4305 tls12_prf->output_block,
4306 hash_length );
4307 if( status != PSA_SUCCESS )
4308 goto cleanup;
4309
4310cleanup:
4311
4312 cleanup_status = psa_hmac_abort_internal( &hmac );
4313 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4314 status = cleanup_status;
4315
4316 return( status );
4317}
Janos Follath7742fee2019-06-17 12:58:10 +01004318#else
4319static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4320 psa_tls12_prf_key_derivation_t *tls12_prf,
4321 psa_algorithm_t alg )
4322{
4323 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4324 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004325 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4326 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004327
Janos Follath7742fee2019-06-17 12:58:10 +01004328 /* We can't be wanting more output after block 0xff, otherwise
4329 * the capacity check in psa_key_derivation_output_bytes() would have
4330 * prevented this call. It could happen only if the operation
4331 * object was corrupted or if this function is called directly
4332 * inside the library. */
4333 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004334 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004335
4336 /* We need a new block */
4337 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004338 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004339
4340 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4341 *
4342 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4343 *
4344 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4345 * HMAC_hash(secret, A(2) + seed) +
4346 * HMAC_hash(secret, A(3) + seed) + ...
4347 *
4348 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004349 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004350 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004351 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004352 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004353 * is currently extracted as `output_block` and where i is
4354 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004355 */
4356
Janos Follathea29bfb2019-06-19 12:21:20 +01004357 /* Save the hash context before using it, to preserve the hash state with
4358 * only the inner padding in it. We need this, because inner padding depends
4359 * on the key (secret in the RFC's terminology). */
4360 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4361 if( status != PSA_SUCCESS )
4362 goto cleanup;
4363
4364 /* Calculate A(i) where i = tls12_prf->block_number. */
4365 if( tls12_prf->block_number == 1 )
4366 {
4367 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4368 * the variable seed and in this instance means it in the context of the
4369 * P_hash function, where seed = label + seed.) */
4370 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4371 tls12_prf->label, tls12_prf->label_length );
4372 if( status != PSA_SUCCESS )
4373 goto cleanup;
4374 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4375 tls12_prf->seed, tls12_prf->seed_length );
4376 if( status != PSA_SUCCESS )
4377 goto cleanup;
4378 }
4379 else
4380 {
4381 /* A(i) = HMAC_hash(secret, A(i-1)) */
4382 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4383 tls12_prf->Ai, hash_length );
4384 if( status != PSA_SUCCESS )
4385 goto cleanup;
4386 }
4387
4388 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4389 tls12_prf->Ai, hash_length );
4390 if( status != PSA_SUCCESS )
4391 goto cleanup;
4392 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4393 if( status != PSA_SUCCESS )
4394 goto cleanup;
4395
4396 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4397 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4398 tls12_prf->Ai, hash_length );
4399 if( status != PSA_SUCCESS )
4400 goto cleanup;
4401 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4402 tls12_prf->label, tls12_prf->label_length );
4403 if( status != PSA_SUCCESS )
4404 goto cleanup;
4405 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4406 tls12_prf->seed, tls12_prf->seed_length );
4407 if( status != PSA_SUCCESS )
4408 goto cleanup;
4409 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4410 tls12_prf->output_block, hash_length );
4411 if( status != PSA_SUCCESS )
4412 goto cleanup;
4413 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4414 if( status != PSA_SUCCESS )
4415 goto cleanup;
4416
Janos Follath7742fee2019-06-17 12:58:10 +01004417
4418cleanup:
4419
Janos Follathea29bfb2019-06-19 12:21:20 +01004420 cleanup_status = psa_hash_abort( &backup );
4421 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4422 status = cleanup_status;
4423
4424 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004425}
Janos Follath999f6482019-06-11 12:04:10 +01004426#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004427
Janos Follath999f6482019-06-11 12:04:10 +01004428#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004429/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004430 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004431static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004432 psa_tls12_prf_key_derivation_t *tls12_prf,
4433 psa_algorithm_t alg,
4434 uint8_t *output,
4435 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004436{
4437 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4438 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4439 psa_status_t status;
4440
4441 while( output_length != 0 )
4442 {
4443 /* Copy what remains of the current block */
4444 uint8_t n = hash_length - tls12_prf->offset_in_block;
4445
4446 /* Check if we have fully processed the current block. */
4447 if( n == 0 )
4448 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004449 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004450 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004451 if( status != PSA_SUCCESS )
4452 return( status );
4453
4454 continue;
4455 }
4456
4457 if( n > output_length )
4458 n = (uint8_t) output_length;
4459 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4460 n );
4461 output += n;
4462 output_length -= n;
4463 tls12_prf->offset_in_block += n;
4464 }
4465
4466 return( PSA_SUCCESS );
4467}
Janos Follath844eb0e2019-06-19 12:10:49 +01004468#else
4469static psa_status_t psa_key_derivation_tls12_prf_read(
4470 psa_tls12_prf_key_derivation_t *tls12_prf,
4471 psa_algorithm_t alg,
4472 uint8_t *output,
4473 size_t output_length )
4474{
4475 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4476 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4477 psa_status_t status;
4478 uint8_t offset, length;
4479
4480 while( output_length != 0 )
4481 {
4482 /* Check if we have fully processed the current block. */
4483 if( tls12_prf->left_in_block == 0 )
4484 {
4485 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4486 alg );
4487 if( status != PSA_SUCCESS )
4488 return( status );
4489
4490 continue;
4491 }
4492
4493 if( tls12_prf->left_in_block > output_length )
4494 length = (uint8_t) output_length;
4495 else
4496 length = tls12_prf->left_in_block;
4497
4498 offset = hash_length - tls12_prf->left_in_block;
4499 memcpy( output, tls12_prf->output_block + offset, length );
4500 output += length;
4501 output_length -= length;
4502 tls12_prf->left_in_block -= length;
4503 }
4504
4505 return( PSA_SUCCESS );
4506}
Janos Follath999f6482019-06-11 12:04:10 +01004507#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004508#endif /* MBEDTLS_MD_C */
4509
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004510psa_status_t psa_key_derivation_output_bytes(
4511 psa_key_derivation_operation_t *operation,
4512 uint8_t *output,
4513 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004514{
4515 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004516 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004517
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004518 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004519 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004520 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004521 return PSA_ERROR_BAD_STATE;
4522 }
4523
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004524 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004525 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004526 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004527 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004528 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004529 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004530 goto exit;
4531 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004532 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004533 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004534 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004535 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004536 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4537 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004538 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004539 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004540 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004541 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004542 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004543
Gilles Peskinebef7f142018-07-12 17:22:21 +02004544#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004545 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004546 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004547 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004548 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004549 output, output_length );
4550 }
Janos Follathadbec812019-06-14 11:05:39 +01004551 else
Janos Follathadbec812019-06-14 11:05:39 +01004552 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004553 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004554 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004555 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004556 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004557 output_length );
4558 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004559 else
4560#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004561 {
4562 return( PSA_ERROR_BAD_STATE );
4563 }
4564
4565exit:
4566 if( status != PSA_SUCCESS )
4567 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004568 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004569 * This allows us to differentiate between exhausted operations and
4570 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4571 * operations. */
4572 psa_algorithm_t alg = operation->alg;
4573 psa_key_derivation_abort( operation );
4574 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004575 memset( output, '!', output_length );
4576 }
4577 return( status );
4578}
4579
Gilles Peskine08542d82018-07-19 17:05:42 +02004580#if defined(MBEDTLS_DES_C)
4581static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4582{
4583 if( data_size >= 8 )
4584 mbedtls_des_key_set_parity( data );
4585 if( data_size >= 16 )
4586 mbedtls_des_key_set_parity( data + 8 );
4587 if( data_size >= 24 )
4588 mbedtls_des_key_set_parity( data + 16 );
4589}
4590#endif /* MBEDTLS_DES_C */
4591
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004592static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004593 psa_key_slot_t *slot,
4594 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004595 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004596{
4597 uint8_t *data = NULL;
4598 size_t bytes = PSA_BITS_TO_BYTES( bits );
4599 psa_status_t status;
4600
4601 if( ! key_type_is_raw_bytes( slot->type ) )
4602 return( PSA_ERROR_INVALID_ARGUMENT );
4603 if( bits % 8 != 0 )
4604 return( PSA_ERROR_INVALID_ARGUMENT );
4605 data = mbedtls_calloc( 1, bytes );
4606 if( data == NULL )
4607 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4608
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004609 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004610 if( status != PSA_SUCCESS )
4611 goto exit;
4612#if defined(MBEDTLS_DES_C)
4613 if( slot->type == PSA_KEY_TYPE_DES )
4614 psa_des_set_key_parity( data, bytes );
4615#endif /* MBEDTLS_DES_C */
4616 status = psa_import_key_into_slot( slot, data, bytes );
4617
4618exit:
4619 mbedtls_free( data );
4620 return( status );
4621}
4622
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004623psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004624 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004625 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004626{
4627 psa_status_t status;
4628 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004629 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004630 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004631 if( status == PSA_SUCCESS )
4632 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004633 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004634 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004635 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004636 }
4637 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004638 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004639 if( status != PSA_SUCCESS )
4640 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004641 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004642 *handle = 0;
4643 }
4644 return( status );
4645}
4646
Gilles Peskineeab56e42018-07-12 17:12:33 +02004647
4648
4649/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004650/* Key derivation */
4651/****************************************************************/
4652
Gilles Peskinea05219c2018-11-16 16:02:56 +01004653#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004654#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004655/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004656 * of the HKDF algorithm.
4657 *
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 Peskinecbe66502019-05-16 16:59:18 +02004661static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004662 const uint8_t *secret,
4663 size_t secret_length,
4664 psa_algorithm_t hash_alg,
4665 const uint8_t *salt,
4666 size_t salt_length,
4667 const uint8_t *label,
4668 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004669{
4670 psa_status_t status;
4671 status = psa_hmac_setup_internal( &hkdf->hmac,
4672 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004673 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004674 if( status != PSA_SUCCESS )
4675 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004676 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004677 if( status != PSA_SUCCESS )
4678 return( status );
4679 status = psa_hmac_finish_internal( &hkdf->hmac,
4680 hkdf->prk,
4681 sizeof( hkdf->prk ) );
4682 if( status != PSA_SUCCESS )
4683 return( status );
4684 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4685 hkdf->block_number = 0;
4686 hkdf->info_length = label_length;
4687 if( label_length != 0 )
4688 {
4689 hkdf->info = mbedtls_calloc( 1, label_length );
4690 if( hkdf->info == NULL )
4691 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4692 memcpy( hkdf->info, label, label_length );
4693 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004694 hkdf->state = HKDF_STATE_KEYED;
4695 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004696 return( PSA_SUCCESS );
4697}
Janos Follath71a4c912019-06-11 09:14:47 +01004698#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004699#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004700
Gilles Peskinea05219c2018-11-16 16:02:56 +01004701#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004702#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004703/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004704 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004705 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004706 * to potentially free embedded data structures and wipe confidential data.
4707 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004708static psa_status_t psa_key_derivation_tls12_prf_setup(
4709 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004710 const unsigned char *key,
4711 size_t key_len,
4712 psa_algorithm_t hash_alg,
4713 const uint8_t *salt,
4714 size_t salt_length,
4715 const uint8_t *label,
4716 size_t label_length )
4717{
4718 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004719 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4720 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004721
4722 tls12_prf->key = mbedtls_calloc( 1, key_len );
4723 if( tls12_prf->key == NULL )
4724 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4725 tls12_prf->key_len = key_len;
4726 memcpy( tls12_prf->key, key, key_len );
4727
Hanno Becker580fba12018-11-13 20:50:45 +00004728 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004729 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004730 if( overflow )
4731 return( PSA_ERROR_INVALID_ARGUMENT );
4732
4733 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4734 if( tls12_prf->Ai_with_seed == NULL )
4735 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4736 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4737
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004738 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4739 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004740 if( label_length != 0 )
4741 {
4742 memcpy( tls12_prf->Ai_with_seed + hash_length,
4743 label, label_length );
4744 }
4745
4746 if( salt_length != 0 )
4747 {
4748 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4749 salt, salt_length );
4750 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004751
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004752 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004753 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004754 tls12_prf->block_number = 0;
4755 tls12_prf->offset_in_block = hash_length;
4756
4757 return( PSA_SUCCESS );
4758}
Janos Follath71a4c912019-06-11 09:14:47 +01004759#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004760
Janos Follath71a4c912019-06-11 09:14:47 +01004761#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004762/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004763static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4764 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004765 const unsigned char *psk,
4766 size_t psk_len,
4767 psa_algorithm_t hash_alg,
4768 const uint8_t *salt,
4769 size_t salt_length,
4770 const uint8_t *label,
4771 size_t label_length )
4772{
4773 psa_status_t status;
4774 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4775
4776 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4777 return( PSA_ERROR_INVALID_ARGUMENT );
4778
4779 /* Quoting RFC 4279, Section 2:
4780 *
4781 * The premaster secret is formed as follows: if the PSK is N octets
4782 * long, concatenate a uint16 with the value N, N zero octets, a second
4783 * uint16 with the value N, and the PSK itself.
4784 */
4785
4786 pms[0] = ( psk_len >> 8 ) & 0xff;
4787 pms[1] = ( psk_len >> 0 ) & 0xff;
4788 memset( pms + 2, 0, psk_len );
4789 pms[2 + psk_len + 0] = pms[0];
4790 pms[2 + psk_len + 1] = pms[1];
4791 memcpy( pms + 4 + psk_len, psk, psk_len );
4792
Gilles Peskinecbe66502019-05-16 16:59:18 +02004793 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004794 pms, 4 + 2 * psk_len,
4795 hash_alg,
4796 salt, salt_length,
4797 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004798
Gilles Peskine3f108122018-12-07 18:14:53 +01004799 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004800 return( status );
4801}
Janos Follath71a4c912019-06-11 09:14:47 +01004802#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004803#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004804
Janos Follath71a4c912019-06-11 09:14:47 +01004805#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004806/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004807 * to potentially free embedded data structures and wipe confidential data.
4808 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004809static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004811 const uint8_t *secret, size_t secret_length,
4812 psa_algorithm_t alg,
4813 const uint8_t *salt, size_t salt_length,
4814 const uint8_t *label, size_t label_length,
4815 size_t capacity )
4816{
4817 psa_status_t status;
4818 size_t max_capacity;
4819
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004820 /* Set operation->alg even on failure so that abort knows what to do. */
4821 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004822
4823#if defined(MBEDTLS_MD_C)
4824 if( PSA_ALG_IS_HKDF( alg ) )
4825 {
4826 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4827 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4828 if( hash_size == 0 )
4829 return( PSA_ERROR_NOT_SUPPORTED );
4830 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004831 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004832 secret, secret_length,
4833 hash_alg,
4834 salt, salt_length,
4835 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004836 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004837 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4838 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4839 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004840 {
4841 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4842 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4843
4844 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4845 if( hash_alg != PSA_ALG_SHA_256 &&
4846 hash_alg != PSA_ALG_SHA_384 )
4847 {
4848 return( PSA_ERROR_NOT_SUPPORTED );
4849 }
4850
4851 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004852
4853 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4854 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004855 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004856 secret, secret_length,
4857 hash_alg, salt, salt_length,
4858 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004859 }
4860 else
4861 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004862 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004863 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004864 secret, secret_length,
4865 hash_alg, salt, salt_length,
4866 label, label_length );
4867 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004868 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004869 else
4870#endif
4871 {
4872 return( PSA_ERROR_NOT_SUPPORTED );
4873 }
4874
4875 if( status != PSA_SUCCESS )
4876 return( status );
4877
4878 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004879 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004880 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004881 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004882 else
4883 return( PSA_ERROR_INVALID_ARGUMENT );
4884
4885 return( PSA_SUCCESS );
4886}
Janos Follath71a4c912019-06-11 09:14:47 +01004887#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004888
Janos Follath71a4c912019-06-11 09:14:47 +01004889#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004890psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004891 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004892 psa_algorithm_t alg,
4893 const uint8_t *salt,
4894 size_t salt_length,
4895 const uint8_t *label,
4896 size_t label_length,
4897 size_t capacity )
4898{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004899 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004900 psa_status_t status;
4901
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004902 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004903 return( PSA_ERROR_BAD_STATE );
4904
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004905 /* Make sure that alg is a key derivation algorithm. This prevents
4906 * key selection algorithms, which psa_key_derivation_internal
4907 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004908 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4909 return( PSA_ERROR_INVALID_ARGUMENT );
4910
Gilles Peskinec5487a82018-12-03 18:08:14 +01004911 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004912 if( status != PSA_SUCCESS )
4913 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004914
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004915 if( slot->type != PSA_KEY_TYPE_DERIVE )
4916 return( PSA_ERROR_INVALID_ARGUMENT );
4917
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004918 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004919 slot->data.raw.data,
4920 slot->data.raw.bytes,
4921 alg,
4922 salt, salt_length,
4923 label, label_length,
4924 capacity );
4925 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004926 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004927 return( status );
4928}
Janos Follath71a4c912019-06-11 09:14:47 +01004929#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004930
Gilles Peskine969c5d62019-01-16 15:53:06 +01004931static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004932 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004933 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004934{
Janos Follath5fe19732019-06-20 15:09:30 +01004935 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4936 * initialisers for this union leave some bytes unspecified.) */
4937 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4938
Gilles Peskine969c5d62019-01-16 15:53:06 +01004939 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004940#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004941 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4942 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4943 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004944 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004945 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004946 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4947 if( hash_size == 0 )
4948 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004949 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4950 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004951 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004952 {
4953 return( PSA_ERROR_NOT_SUPPORTED );
4954 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004955 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004956 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004957 }
4958#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004959 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004960 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004961}
4962
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004964 psa_algorithm_t alg )
4965{
4966 psa_status_t status;
4967
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004968 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004969 return( PSA_ERROR_BAD_STATE );
4970
Gilles Peskine6843c292019-01-18 16:44:49 +01004971 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4972 return( PSA_ERROR_INVALID_ARGUMENT );
4973 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004974 {
4975 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004977 }
4978 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4979 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004980 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004981 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004982 else
4983 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004984
4985 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004986 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004987 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004988}
4989
4990#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004991static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004992 psa_algorithm_t hash_alg,
4993 psa_key_derivation_step_t step,
4994 const uint8_t *data,
4995 size_t data_length )
4996{
4997 psa_status_t status;
4998 switch( step )
4999 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005000 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005001 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005002 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005003 status = psa_hmac_setup_internal( &hkdf->hmac,
5004 data, data_length,
5005 hash_alg );
5006 if( status != PSA_SUCCESS )
5007 return( status );
5008 hkdf->state = HKDF_STATE_STARTED;
5009 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005010 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005011 /* If no salt was provided, use an empty salt. */
5012 if( hkdf->state == HKDF_STATE_INIT )
5013 {
5014 status = psa_hmac_setup_internal( &hkdf->hmac,
5015 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005016 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005017 if( status != PSA_SUCCESS )
5018 return( status );
5019 hkdf->state = HKDF_STATE_STARTED;
5020 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005021 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005022 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005023 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5024 data, data_length );
5025 if( status != PSA_SUCCESS )
5026 return( status );
5027 status = psa_hmac_finish_internal( &hkdf->hmac,
5028 hkdf->prk,
5029 sizeof( hkdf->prk ) );
5030 if( status != PSA_SUCCESS )
5031 return( status );
5032 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5033 hkdf->block_number = 0;
5034 hkdf->state = HKDF_STATE_KEYED;
5035 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005036 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005037 if( hkdf->state == HKDF_STATE_OUTPUT )
5038 return( PSA_ERROR_BAD_STATE );
5039 if( hkdf->info_set )
5040 return( PSA_ERROR_BAD_STATE );
5041 hkdf->info_length = data_length;
5042 if( data_length != 0 )
5043 {
5044 hkdf->info = mbedtls_calloc( 1, data_length );
5045 if( hkdf->info == NULL )
5046 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5047 memcpy( hkdf->info, data, data_length );
5048 }
5049 hkdf->info_set = 1;
5050 return( PSA_SUCCESS );
5051 default:
5052 return( PSA_ERROR_INVALID_ARGUMENT );
5053 }
5054}
Janos Follathb03233e2019-06-11 15:30:30 +01005055
5056#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5057static psa_status_t psa_tls12_prf_input( 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 (void) prf;
5064 (void) hash_alg;
5065 (void) step;
5066 (void) data;
5067 (void) data_length;
5068
5069 return( PSA_ERROR_INVALID_ARGUMENT );
5070}
Janos Follath6660f0e2019-06-17 08:44:03 +01005071
5072static psa_status_t psa_tls12_prf_psk_to_ms_input(
5073 psa_tls12_prf_key_derivation_t *prf,
5074 psa_algorithm_t hash_alg,
5075 psa_key_derivation_step_t step,
5076 const uint8_t *data,
5077 size_t data_length )
5078{
5079 (void) prf;
5080 (void) hash_alg;
5081 (void) step;
5082 (void) data;
5083 (void) data_length;
5084
5085 return( PSA_ERROR_INVALID_ARGUMENT );
5086}
Janos Follathb03233e2019-06-11 15:30:30 +01005087#else
Janos Follathf08e2652019-06-13 09:05:41 +01005088static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5089 const uint8_t *data,
5090 size_t data_length )
5091{
5092 if( prf->state != TLS12_PRF_STATE_INIT )
5093 return( PSA_ERROR_BAD_STATE );
5094
Janos Follathd6dce9f2019-07-04 09:11:38 +01005095 if( data_length != 0 )
5096 {
5097 prf->seed = mbedtls_calloc( 1, data_length );
5098 if( prf->seed == NULL )
5099 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005100
Janos Follathd6dce9f2019-07-04 09:11:38 +01005101 memcpy( prf->seed, data, data_length );
5102 prf->seed_length = data_length;
5103 }
Janos Follathf08e2652019-06-13 09:05:41 +01005104
5105 prf->state = TLS12_PRF_STATE_SEED_SET;
5106
5107 return( PSA_SUCCESS );
5108}
5109
Janos Follath81550542019-06-13 14:26:34 +01005110static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5111 psa_algorithm_t hash_alg,
5112 const uint8_t *data,
5113 size_t data_length )
5114{
5115 psa_status_t status;
5116 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5117 return( PSA_ERROR_BAD_STATE );
5118
5119 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5120 if( status != PSA_SUCCESS )
5121 return( status );
5122
5123 prf->state = TLS12_PRF_STATE_KEY_SET;
5124
5125 return( PSA_SUCCESS );
5126}
5127
Janos Follath6660f0e2019-06-17 08:44:03 +01005128static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5129 psa_tls12_prf_key_derivation_t *prf,
5130 psa_algorithm_t hash_alg,
5131 const uint8_t *data,
5132 size_t data_length )
5133{
5134 psa_status_t status;
5135 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Janos Follath40e13932019-06-26 13:22:29 +01005136 unsigned char* cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005137
5138 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5139 return( PSA_ERROR_INVALID_ARGUMENT );
5140
5141 /* Quoting RFC 4279, Section 2:
5142 *
5143 * The premaster secret is formed as follows: if the PSK is N octets
5144 * long, concatenate a uint16 with the value N, N zero octets, a second
5145 * uint16 with the value N, and the PSK itself.
5146 */
5147
Janos Follath40e13932019-06-26 13:22:29 +01005148 *cur++ = ( data_length >> 8 ) & 0xff;
5149 *cur++ = ( data_length >> 0 ) & 0xff;
5150 memset( cur, 0, data_length );
5151 cur += data_length;
5152 *cur++ = pms[0];
5153 *cur++ = pms[1];
5154 memcpy( cur, data, data_length );
5155 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005156
Janos Follath40e13932019-06-26 13:22:29 +01005157 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005158
5159 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5160 return( status );
5161}
5162
Janos Follath63028dd2019-06-13 09:15:47 +01005163static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5164 const uint8_t *data,
5165 size_t data_length )
5166{
5167 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5168 return( PSA_ERROR_BAD_STATE );
5169
Janos Follathd6dce9f2019-07-04 09:11:38 +01005170 if( data_length != 0 )
5171 {
5172 prf->label = mbedtls_calloc( 1, data_length );
5173 if( prf->label == NULL )
5174 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005175
Janos Follathd6dce9f2019-07-04 09:11:38 +01005176 memcpy( prf->label, data, data_length );
5177 prf->label_length = data_length;
5178 }
Janos Follath63028dd2019-06-13 09:15:47 +01005179
5180 prf->state = TLS12_PRF_STATE_LABEL_SET;
5181
5182 return( PSA_SUCCESS );
5183}
5184
Janos Follathb03233e2019-06-11 15:30:30 +01005185static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5186 psa_algorithm_t hash_alg,
5187 psa_key_derivation_step_t step,
5188 const uint8_t *data,
5189 size_t data_length )
5190{
Janos Follathb03233e2019-06-11 15:30:30 +01005191 switch( step )
5192 {
Janos Follathf08e2652019-06-13 09:05:41 +01005193 case PSA_KEY_DERIVATION_INPUT_SEED:
5194 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005195 case PSA_KEY_DERIVATION_INPUT_SECRET:
5196 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005197 case PSA_KEY_DERIVATION_INPUT_LABEL:
5198 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005199 default:
5200 return( PSA_ERROR_INVALID_ARGUMENT );
5201 }
5202}
Janos Follath6660f0e2019-06-17 08:44:03 +01005203
5204static psa_status_t psa_tls12_prf_psk_to_ms_input(
5205 psa_tls12_prf_key_derivation_t *prf,
5206 psa_algorithm_t hash_alg,
5207 psa_key_derivation_step_t step,
5208 const uint8_t *data,
5209 size_t data_length )
5210{
5211 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005212 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005213 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5214 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005215 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005216
5217 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5218}
Janos Follathb03233e2019-06-11 15:30:30 +01005219#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005220#endif /* MBEDTLS_MD_C */
5221
Janos Follathb80a94e2019-06-12 15:54:46 +01005222static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005223 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005224 psa_key_derivation_step_t step,
5225 const uint8_t *data,
5226 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005227{
5228 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005229 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005230
5231#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005232 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005233 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005234 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005235 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005236 step, data, data_length );
5237 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005238 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005239#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005240#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005241 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005242 {
Janos Follathb03233e2019-06-11 15:30:30 +01005243 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5244 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5245 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005246 }
5247 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5248 {
5249 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5250 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5251 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005252 }
5253 else
5254#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005255 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005256 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005257 return( PSA_ERROR_BAD_STATE );
5258 }
5259
5260 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005262 return( status );
5263}
5264
Janos Follath51f4a0f2019-06-14 11:35:55 +01005265psa_status_t psa_key_derivation_input_bytes(
5266 psa_key_derivation_operation_t *operation,
5267 psa_key_derivation_step_t step,
5268 const uint8_t *data,
5269 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005270{
Janos Follathc5621512019-06-14 11:27:57 +01005271 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5272 return( PSA_ERROR_INVALID_ARGUMENT );
5273
5274 return( psa_key_derivation_input_internal( operation, step,
5275 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005276}
5277
Janos Follath51f4a0f2019-06-14 11:35:55 +01005278psa_status_t psa_key_derivation_input_key(
5279 psa_key_derivation_operation_t *operation,
5280 psa_key_derivation_step_t step,
5281 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005282{
5283 psa_key_slot_t *slot;
5284 psa_status_t status;
5285 status = psa_get_key_from_slot( handle, &slot,
5286 PSA_KEY_USAGE_DERIVE,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005288 if( status != PSA_SUCCESS )
5289 return( status );
5290 if( slot->type != PSA_KEY_TYPE_DERIVE )
5291 return( PSA_ERROR_INVALID_ARGUMENT );
5292 /* Don't allow a key to be used as an input that is usually public.
5293 * This is debatable. It's ok from a cryptographic perspective to
5294 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005295 * the material should be dedicated to a particular input step,
5296 * otherwise this may allow the key to be used in an unintended way
5297 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005298 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005299 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005300 return( psa_key_derivation_input_internal( operation,
5301 step,
5302 slot->data.raw.data,
5303 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005304}
5305
Gilles Peskineea0fb492018-07-12 17:17:20 +02005306
5307
5308/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005309/* Key agreement */
5310/****************************************************************/
5311
Gilles Peskinea05219c2018-11-16 16:02:56 +01005312#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005313static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5314 size_t peer_key_length,
5315 const mbedtls_ecp_keypair *our_key,
5316 uint8_t *shared_secret,
5317 size_t shared_secret_size,
5318 size_t *shared_secret_length )
5319{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005320 mbedtls_ecp_keypair *their_key = NULL;
5321 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005322 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005323 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005324
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005325 status = psa_import_ec_public_key(
5326 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5327 peer_key, peer_key_length,
5328 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005329 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005330 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005331
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005332 status = mbedtls_to_psa_error(
5333 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5334 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005335 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005336 status = mbedtls_to_psa_error(
5337 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5338 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005339 goto exit;
5340
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005341 status = mbedtls_to_psa_error(
5342 mbedtls_ecdh_calc_secret( &ecdh,
5343 shared_secret_length,
5344 shared_secret, shared_secret_size,
5345 mbedtls_ctr_drbg_random,
5346 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005347
5348exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005349 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005350 mbedtls_ecp_keypair_free( their_key );
5351 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005352 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005353}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005354#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005355
Gilles Peskine01d718c2018-09-18 12:01:02 +02005356#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5357
Gilles Peskine0216fe12019-04-11 21:23:21 +02005358static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5359 psa_key_slot_t *private_key,
5360 const uint8_t *peer_key,
5361 size_t peer_key_length,
5362 uint8_t *shared_secret,
5363 size_t shared_secret_size,
5364 size_t *shared_secret_length )
5365{
5366 switch( alg )
5367 {
5368#if defined(MBEDTLS_ECDH_C)
5369 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005370 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005371 return( PSA_ERROR_INVALID_ARGUMENT );
5372 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5373 private_key->data.ecp,
5374 shared_secret, shared_secret_size,
5375 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005376#endif /* MBEDTLS_ECDH_C */
5377 default:
5378 (void) private_key;
5379 (void) peer_key;
5380 (void) peer_key_length;
5381 (void) shared_secret;
5382 (void) shared_secret_size;
5383 (void) shared_secret_length;
5384 return( PSA_ERROR_NOT_SUPPORTED );
5385 }
5386}
5387
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005388/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005389 * to potentially free embedded data structures and wipe confidential data.
5390 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005391static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005392 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005393 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005394 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005395 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005396{
5397 psa_status_t status;
5398 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5399 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005400 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005401
5402 /* Step 1: run the secret agreement algorithm to generate the shared
5403 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005404 status = psa_key_agreement_raw_internal( ka_alg,
5405 private_key,
5406 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005407 shared_secret,
5408 sizeof( shared_secret ),
5409 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005410 if( status != PSA_SUCCESS )
5411 goto exit;
5412
5413 /* Step 2: set up the key derivation to generate key material from
5414 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005415 status = psa_key_derivation_input_internal( operation, step,
5416 shared_secret,
5417 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005418
Gilles Peskine01d718c2018-09-18 12:01:02 +02005419exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005420 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005421 return( status );
5422}
5423
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005424psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005425 psa_key_derivation_step_t step,
5426 psa_key_handle_t private_key,
5427 const uint8_t *peer_key,
5428 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005429{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005430 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005431 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005432 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005433 return( PSA_ERROR_INVALID_ARGUMENT );
5434 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005435 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005436 if( status != PSA_SUCCESS )
5437 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005438 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005439 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005440 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005441 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005442 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005443 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005444}
5445
Gilles Peskinebe697d82019-05-16 18:00:41 +02005446psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5447 psa_key_handle_t private_key,
5448 const uint8_t *peer_key,
5449 size_t peer_key_length,
5450 uint8_t *output,
5451 size_t output_size,
5452 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005453{
5454 psa_key_slot_t *slot;
5455 psa_status_t status;
5456
5457 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5458 {
5459 status = PSA_ERROR_INVALID_ARGUMENT;
5460 goto exit;
5461 }
5462 status = psa_get_key_from_slot( private_key, &slot,
5463 PSA_KEY_USAGE_DERIVE, alg );
5464 if( status != PSA_SUCCESS )
5465 goto exit;
5466
5467 status = psa_key_agreement_raw_internal( alg, slot,
5468 peer_key, peer_key_length,
5469 output, output_size,
5470 output_length );
5471
5472exit:
5473 if( status != PSA_SUCCESS )
5474 {
5475 /* If an error happens and is not handled properly, the output
5476 * may be used as a key to protect sensitive data. Arrange for such
5477 * a key to be random, which is likely to result in decryption or
5478 * verification errors. This is better than filling the buffer with
5479 * some constant data such as zeros, which would result in the data
5480 * being protected with a reproducible, easily knowable key.
5481 */
5482 psa_generate_random( output, output_size );
5483 *output_length = output_size;
5484 }
5485 return( status );
5486}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005487
5488
5489/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005490/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005491/****************************************************************/
5492
5493psa_status_t psa_generate_random( uint8_t *output,
5494 size_t output_size )
5495{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005496 int ret;
5497 GUARD_MODULE_INITIALIZED;
5498
5499 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005500 return( mbedtls_to_psa_error( ret ) );
5501}
5502
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005503#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5504#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005505
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005506psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5507 size_t seed_size )
5508{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005509 if( global_data.initialized )
5510 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005511
5512 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5513 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5514 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5515 return( PSA_ERROR_INVALID_ARGUMENT );
5516
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005517 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005518}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005519#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005520
Gilles Peskinee56e8782019-04-26 17:34:02 +02005521#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5522static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5523 size_t domain_parameters_size,
5524 int *exponent )
5525{
5526 size_t i;
5527 uint32_t acc = 0;
5528
5529 if( domain_parameters_size == 0 )
5530 {
5531 *exponent = 65537;
5532 return( PSA_SUCCESS );
5533 }
5534
5535 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5536 * support values that fit in a 32-bit integer, which is larger than
5537 * int on just about every platform anyway. */
5538 if( domain_parameters_size > sizeof( acc ) )
5539 return( PSA_ERROR_NOT_SUPPORTED );
5540 for( i = 0; i < domain_parameters_size; i++ )
5541 acc = ( acc << 8 ) | domain_parameters[i];
5542 if( acc > INT_MAX )
5543 return( PSA_ERROR_NOT_SUPPORTED );
5544 *exponent = acc;
5545 return( PSA_SUCCESS );
5546}
5547#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5548
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005549static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005550 psa_key_slot_t *slot, size_t bits,
5551 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005552{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005553 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005554
Gilles Peskinee56e8782019-04-26 17:34:02 +02005555 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005556 return( PSA_ERROR_INVALID_ARGUMENT );
5557
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005558 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005559 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005560 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005561 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005562 if( status != PSA_SUCCESS )
5563 return( status );
5564 status = psa_generate_random( slot->data.raw.data,
5565 slot->data.raw.bytes );
5566 if( status != PSA_SUCCESS )
5567 {
5568 mbedtls_free( slot->data.raw.data );
5569 return( status );
5570 }
5571#if defined(MBEDTLS_DES_C)
5572 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005573 psa_des_set_key_parity( slot->data.raw.data,
5574 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005575#endif /* MBEDTLS_DES_C */
5576 }
5577 else
5578
5579#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005580 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005581 {
5582 mbedtls_rsa_context *rsa;
5583 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005584 int exponent;
5585 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005586 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5587 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005588 /* Accept only byte-aligned keys, for the same reasons as
5589 * in psa_import_rsa_key(). */
5590 if( bits % 8 != 0 )
5591 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005592 status = psa_read_rsa_exponent( domain_parameters,
5593 domain_parameters_size,
5594 &exponent );
5595 if( status != PSA_SUCCESS )
5596 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005597 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5598 if( rsa == NULL )
5599 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5600 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5601 ret = mbedtls_rsa_gen_key( rsa,
5602 mbedtls_ctr_drbg_random,
5603 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005604 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005605 exponent );
5606 if( ret != 0 )
5607 {
5608 mbedtls_rsa_free( rsa );
5609 mbedtls_free( rsa );
5610 return( mbedtls_to_psa_error( ret ) );
5611 }
5612 slot->data.rsa = rsa;
5613 }
5614 else
5615#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5616
5617#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005618 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005619 {
5620 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5621 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5622 const mbedtls_ecp_curve_info *curve_info =
5623 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5624 mbedtls_ecp_keypair *ecp;
5625 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005626 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005627 return( PSA_ERROR_NOT_SUPPORTED );
5628 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5629 return( PSA_ERROR_NOT_SUPPORTED );
5630 if( curve_info->bit_size != bits )
5631 return( PSA_ERROR_INVALID_ARGUMENT );
5632 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5633 if( ecp == NULL )
5634 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5635 mbedtls_ecp_keypair_init( ecp );
5636 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5637 mbedtls_ctr_drbg_random,
5638 &global_data.ctr_drbg );
5639 if( ret != 0 )
5640 {
5641 mbedtls_ecp_keypair_free( ecp );
5642 mbedtls_free( ecp );
5643 return( mbedtls_to_psa_error( ret ) );
5644 }
5645 slot->data.ecp = ecp;
5646 }
5647 else
5648#endif /* MBEDTLS_ECP_C */
5649
5650 return( PSA_ERROR_NOT_SUPPORTED );
5651
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005652 return( PSA_SUCCESS );
5653}
5654
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005655psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005656 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005657{
5658 psa_status_t status;
5659 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005660 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005661 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005662 if( status == PSA_SUCCESS )
5663 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005664 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005665 slot, attributes->bits,
5666 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005667 }
5668 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005669 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005670 if( status != PSA_SUCCESS )
5671 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005672 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005673 *handle = 0;
5674 }
5675 return( status );
5676}
5677
5678
Gilles Peskine05d69892018-06-19 22:00:52 +02005679
5680/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005681/* Module setup */
5682/****************************************************************/
5683
Gilles Peskine5e769522018-11-20 21:59:56 +01005684psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5685 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5686 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5687{
5688 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5689 return( PSA_ERROR_BAD_STATE );
5690 global_data.entropy_init = entropy_init;
5691 global_data.entropy_free = entropy_free;
5692 return( PSA_SUCCESS );
5693}
5694
Gilles Peskinee59236f2018-01-27 23:32:46 +01005695void mbedtls_psa_crypto_free( void )
5696{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005697 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005698 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5699 {
5700 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005701 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005702 }
5703 /* Wipe all remaining data, including configuration.
5704 * In particular, this sets all state indicator to the value
5705 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005706 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005707#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005708 /* Unregister all secure element drivers, so that we restart from
5709 * a pristine state. */
5710 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005711#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005712}
5713
5714psa_status_t psa_crypto_init( void )
5715{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005716 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005717 const unsigned char drbg_seed[] = "PSA";
5718
Gilles Peskinec6b69072018-11-20 21:42:52 +01005719 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005720 if( global_data.initialized != 0 )
5721 return( PSA_SUCCESS );
5722
Gilles Peskine5e769522018-11-20 21:59:56 +01005723 /* Set default configuration if
5724 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5725 if( global_data.entropy_init == NULL )
5726 global_data.entropy_init = mbedtls_entropy_init;
5727 if( global_data.entropy_free == NULL )
5728 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005729
Gilles Peskinec6b69072018-11-20 21:42:52 +01005730 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005731 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005732 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005733 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005734 status = mbedtls_to_psa_error(
5735 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5736 mbedtls_entropy_func,
5737 &global_data.entropy,
5738 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5739 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005740 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005741 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005742
Gilles Peskine66fb1262018-12-10 16:29:04 +01005743 status = psa_initialize_key_slots( );
5744 if( status != PSA_SUCCESS )
5745 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005746
Gilles Peskinefc762652019-07-22 19:30:34 +02005747#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5748 status = psa_crypto_load_transaction( );
5749 if( status == PSA_SUCCESS )
5750 {
5751 /*TOnogrepDO: complete or abort the transaction*/
5752 }
5753 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5754 {
5755 /* There's no transaction to complete. It's all good. */
5756 status = PSA_SUCCESS;
5757 }
5758#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5759
Gilles Peskinec6b69072018-11-20 21:42:52 +01005760 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005761 global_data.initialized = 1;
5762
Gilles Peskinee59236f2018-01-27 23:32:46 +01005763exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005764 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005765 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005766 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005767}
5768
5769#endif /* MBEDTLS_PSA_CRYPTO_C */