blob: b2fc26e1be60d7cee0e2c4c0b2675a8ef121b5ae [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043#include <stdlib.h>
44#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020046#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010047#define mbedtls_calloc calloc
48#define mbedtls_free free
49#endif
50
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020052#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000053#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020054#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/blowfish.h"
56#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020057#include "mbedtls/chacha20.h"
58#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/cipher.h"
60#include "mbedtls/ccm.h"
61#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020064#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010065#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010066#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/error.h"
68#include "mbedtls/gcm.h"
69#include "mbedtls/md2.h"
70#include "mbedtls/md4.h"
71#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
73#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010076#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/sha1.h"
80#include "mbedtls/sha256.h"
81#include "mbedtls/sha512.h"
82#include "mbedtls/xtea.h"
83
Gilles Peskine996deb12018-08-01 15:45:45 +020084#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
85
Gilles Peskine9ef733f2018-02-07 21:05:37 +010086/* constant-time buffer comparison */
87static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
88{
89 size_t i;
90 unsigned char diff = 0;
91
92 for( i = 0; i < n; i++ )
93 diff |= a[i] ^ b[i];
94
95 return( diff );
96}
97
98
99
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100100/****************************************************************/
101/* Global data, support functions and library management */
102/****************************************************************/
103
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104static int key_type_is_raw_bytes( psa_key_type_t type )
105{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200106 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200107}
108
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109/* Values for psa_global_data_t::rng_state */
110#define RNG_NOT_INITIALIZED 0
111#define RNG_INITIALIZED 1
112#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100113
Gilles Peskine2d277862018-06-18 15:41:12 +0200114typedef struct
115{
Gilles Peskine5e769522018-11-20 21:59:56 +0100116 void (* entropy_init )( mbedtls_entropy_context *ctx );
117 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118 mbedtls_entropy_context entropy;
119 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100120 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100121 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100122} psa_global_data_t;
123
124static psa_global_data_t global_data;
125
itayzafrir0adf0fc2018-09-06 16:24:41 +0300126#define GUARD_MODULE_INITIALIZED \
127 if( global_data.initialized == 0 ) \
128 return( PSA_ERROR_BAD_STATE );
129
Gilles Peskinee59236f2018-01-27 23:32:46 +0100130static psa_status_t mbedtls_to_psa_error( int ret )
131{
Gilles Peskinea5905292018-02-07 20:59:33 +0100132 /* If there's both a high-level code and low-level code, dispatch on
133 * the high-level code. */
134 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100135 {
136 case 0:
137 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100138
139 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
140 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
141 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
142 return( PSA_ERROR_NOT_SUPPORTED );
143 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
144 return( PSA_ERROR_HARDWARE_FAILURE );
145
146 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
147 return( PSA_ERROR_HARDWARE_FAILURE );
148
Gilles Peskine9a944802018-06-21 09:35:35 +0200149 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
150 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
151 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
152 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
153 case MBEDTLS_ERR_ASN1_INVALID_DATA:
154 return( PSA_ERROR_INVALID_ARGUMENT );
155 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
156 return( PSA_ERROR_INSUFFICIENT_MEMORY );
157 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
158 return( PSA_ERROR_BUFFER_TOO_SMALL );
159
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000161 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100165 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
166 return( PSA_ERROR_NOT_SUPPORTED );
167 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
168 return( PSA_ERROR_HARDWARE_FAILURE );
169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
180 case MBEDTLS_ERR_CCM_BAD_INPUT:
181 return( PSA_ERROR_INVALID_ARGUMENT );
182 case MBEDTLS_ERR_CCM_AUTH_FAILED:
183 return( PSA_ERROR_INVALID_SIGNATURE );
184 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
Gilles Peskine26869f22019-05-06 15:25:00 +0200187 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
188 return( PSA_ERROR_INVALID_ARGUMENT );
189
190 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
191 return( PSA_ERROR_BAD_STATE );
192 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
193 return( PSA_ERROR_INVALID_SIGNATURE );
194
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
196 return( PSA_ERROR_NOT_SUPPORTED );
197 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
200 return( PSA_ERROR_INSUFFICIENT_MEMORY );
201 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
202 return( PSA_ERROR_INVALID_PADDING );
203 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
204 return( PSA_ERROR_BAD_STATE );
205 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
206 return( PSA_ERROR_INVALID_SIGNATURE );
207 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200208 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
213 return( PSA_ERROR_HARDWARE_FAILURE );
214
215 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
216 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
217 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
218 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
219 return( PSA_ERROR_NOT_SUPPORTED );
220 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
221 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
222
223 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
Gilles Peskinee59236f2018-01-27 23:32:46 +0100228 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
229 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
230 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
233 case MBEDTLS_ERR_GCM_AUTH_FAILED:
234 return( PSA_ERROR_INVALID_SIGNATURE );
235 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200236 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100237 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
238 return( PSA_ERROR_HARDWARE_FAILURE );
239
240 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
245 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
246 return( PSA_ERROR_NOT_SUPPORTED );
247 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
248 return( PSA_ERROR_INVALID_ARGUMENT );
249 case MBEDTLS_ERR_MD_ALLOC_FAILED:
250 return( PSA_ERROR_INSUFFICIENT_MEMORY );
251 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
252 return( PSA_ERROR_STORAGE_FAILURE );
253 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
254 return( PSA_ERROR_HARDWARE_FAILURE );
255
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
257 return( PSA_ERROR_STORAGE_FAILURE );
258 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
263 return( PSA_ERROR_BUFFER_TOO_SMALL );
264 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_ALLOC_FAILED:
274 return( PSA_ERROR_INSUFFICIENT_MEMORY );
275 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
276 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100279 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100280 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
281 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
284 return( PSA_ERROR_NOT_SUPPORTED );
285 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
286 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
287 return( PSA_ERROR_NOT_PERMITTED );
288 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_PK_INVALID_ALG:
291 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
292 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
293 return( PSA_ERROR_NOT_SUPPORTED );
294 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
295 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
300 return( PSA_ERROR_HARDWARE_FAILURE );
301 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
302 return( PSA_ERROR_NOT_SUPPORTED );
303
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
305 return( PSA_ERROR_HARDWARE_FAILURE );
306
307 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_RSA_INVALID_PADDING:
310 return( PSA_ERROR_INVALID_PADDING );
311 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
312 return( PSA_ERROR_HARDWARE_FAILURE );
313 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
314 return( PSA_ERROR_INVALID_ARGUMENT );
315 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
316 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200317 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100318 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
319 return( PSA_ERROR_INVALID_SIGNATURE );
320 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
321 return( PSA_ERROR_BUFFER_TOO_SMALL );
322 case MBEDTLS_ERR_RSA_RNG_FAILED:
323 return( PSA_ERROR_INSUFFICIENT_MEMORY );
324 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
325 return( PSA_ERROR_NOT_SUPPORTED );
326 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
327 return( PSA_ERROR_HARDWARE_FAILURE );
328
329 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
330 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
332 return( PSA_ERROR_HARDWARE_FAILURE );
333
334 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
335 return( PSA_ERROR_INVALID_ARGUMENT );
336 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
337 return( PSA_ERROR_HARDWARE_FAILURE );
338
itayzafrir5c753392018-05-08 11:18:38 +0300339 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300341 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
343 return( PSA_ERROR_BUFFER_TOO_SMALL );
344 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
345 return( PSA_ERROR_NOT_SUPPORTED );
346 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
347 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
348 return( PSA_ERROR_INVALID_SIGNATURE );
349 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
350 return( PSA_ERROR_INSUFFICIENT_MEMORY );
351 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
352 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
369 return( psa_key_lifetime_is_external( slot->lifetime ) );
370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200374static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
375{
376 switch( grpid )
377 {
378 case MBEDTLS_ECP_DP_SECP192R1:
379 return( PSA_ECC_CURVE_SECP192R1 );
380 case MBEDTLS_ECP_DP_SECP224R1:
381 return( PSA_ECC_CURVE_SECP224R1 );
382 case MBEDTLS_ECP_DP_SECP256R1:
383 return( PSA_ECC_CURVE_SECP256R1 );
384 case MBEDTLS_ECP_DP_SECP384R1:
385 return( PSA_ECC_CURVE_SECP384R1 );
386 case MBEDTLS_ECP_DP_SECP521R1:
387 return( PSA_ECC_CURVE_SECP521R1 );
388 case MBEDTLS_ECP_DP_BP256R1:
389 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
390 case MBEDTLS_ECP_DP_BP384R1:
391 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
392 case MBEDTLS_ECP_DP_BP512R1:
393 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
394 case MBEDTLS_ECP_DP_CURVE25519:
395 return( PSA_ECC_CURVE_CURVE25519 );
396 case MBEDTLS_ECP_DP_SECP192K1:
397 return( PSA_ECC_CURVE_SECP192K1 );
398 case MBEDTLS_ECP_DP_SECP224K1:
399 return( PSA_ECC_CURVE_SECP224K1 );
400 case MBEDTLS_ECP_DP_SECP256K1:
401 return( PSA_ECC_CURVE_SECP256K1 );
402 case MBEDTLS_ECP_DP_CURVE448:
403 return( PSA_ECC_CURVE_CURVE448 );
404 default:
405 return( 0 );
406 }
407}
408
Gilles Peskine12313cd2018-06-20 00:20:32 +0200409static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
410{
411 switch( curve )
412 {
413 case PSA_ECC_CURVE_SECP192R1:
414 return( MBEDTLS_ECP_DP_SECP192R1 );
415 case PSA_ECC_CURVE_SECP224R1:
416 return( MBEDTLS_ECP_DP_SECP224R1 );
417 case PSA_ECC_CURVE_SECP256R1:
418 return( MBEDTLS_ECP_DP_SECP256R1 );
419 case PSA_ECC_CURVE_SECP384R1:
420 return( MBEDTLS_ECP_DP_SECP384R1 );
421 case PSA_ECC_CURVE_SECP521R1:
422 return( MBEDTLS_ECP_DP_SECP521R1 );
423 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
424 return( MBEDTLS_ECP_DP_BP256R1 );
425 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
426 return( MBEDTLS_ECP_DP_BP384R1 );
427 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
428 return( MBEDTLS_ECP_DP_BP512R1 );
429 case PSA_ECC_CURVE_CURVE25519:
430 return( MBEDTLS_ECP_DP_CURVE25519 );
431 case PSA_ECC_CURVE_SECP192K1:
432 return( MBEDTLS_ECP_DP_SECP192K1 );
433 case PSA_ECC_CURVE_SECP224K1:
434 return( MBEDTLS_ECP_DP_SECP224K1 );
435 case PSA_ECC_CURVE_SECP256K1:
436 return( MBEDTLS_ECP_DP_SECP256K1 );
437 case PSA_ECC_CURVE_CURVE448:
438 return( MBEDTLS_ECP_DP_CURVE448 );
439 default:
440 return( MBEDTLS_ECP_DP_NONE );
441 }
442}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100443#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200444
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
446 size_t bits,
447 struct raw_data *raw )
448{
449 /* Check that the bit size is acceptable for the key type */
450 switch( type )
451 {
452 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200453 if( bits == 0 )
454 {
455 raw->bytes = 0;
456 raw->data = NULL;
457 return( PSA_SUCCESS );
458 }
459 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460#if defined(MBEDTLS_MD_C)
461 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200462#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200463 case PSA_KEY_TYPE_DERIVE:
464 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200465#if defined(MBEDTLS_AES_C)
466 case PSA_KEY_TYPE_AES:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_CAMELLIA_C)
472 case PSA_KEY_TYPE_CAMELLIA:
473 if( bits != 128 && bits != 192 && bits != 256 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_DES_C)
478 case PSA_KEY_TYPE_DES:
479 if( bits != 64 && bits != 128 && bits != 192 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
483#if defined(MBEDTLS_ARC4_C)
484 case PSA_KEY_TYPE_ARC4:
485 if( bits < 8 || bits > 2048 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200489#if defined(MBEDTLS_CHACHA20_C)
490 case PSA_KEY_TYPE_CHACHA20:
491 if( bits != 256 )
492 return( PSA_ERROR_INVALID_ARGUMENT );
493 break;
494#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 default:
496 return( PSA_ERROR_NOT_SUPPORTED );
497 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200498 if( bits % 8 != 0 )
499 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200500
501 /* Allocate memory for the key */
502 raw->bytes = PSA_BITS_TO_BYTES( bits );
503 raw->data = mbedtls_calloc( 1, raw->bytes );
504 if( raw->data == NULL )
505 {
506 raw->bytes = 0;
507 return( PSA_ERROR_INSUFFICIENT_MEMORY );
508 }
509 return( PSA_SUCCESS );
510}
511
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200512#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100513/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
514 * that are not a multiple of 8) well. For example, there is only
515 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
516 * way to return the exact bit size of a key.
517 * To keep things simple, reject non-byte-aligned key sizes. */
518static psa_status_t psa_check_rsa_key_byte_aligned(
519 const mbedtls_rsa_context *rsa )
520{
521 mbedtls_mpi n;
522 psa_status_t status;
523 mbedtls_mpi_init( &n );
524 status = mbedtls_to_psa_error(
525 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
526 if( status == PSA_SUCCESS )
527 {
528 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
529 status = PSA_ERROR_NOT_SUPPORTED;
530 }
531 mbedtls_mpi_free( &n );
532 return( status );
533}
534
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000535static psa_status_t psa_import_rsa_key( psa_key_type_t type,
536 const uint8_t *data,
537 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200538 mbedtls_rsa_context **p_rsa )
539{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000540 psa_status_t status;
541 mbedtls_pk_context pk;
542 mbedtls_rsa_context *rsa;
543 size_t bits;
544
545 mbedtls_pk_init( &pk );
546
547 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200548 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000549 status = mbedtls_to_psa_error(
550 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200551 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000552 status = mbedtls_to_psa_error(
553 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
554 if( status != PSA_SUCCESS )
555 goto exit;
556
557 /* We have something that the pkparse module recognizes. If it is a
558 * valid RSA key, store it. */
559 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000561 status = PSA_ERROR_INVALID_ARGUMENT;
562 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200563 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000564
565 rsa = mbedtls_pk_rsa( pk );
566 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
567 * supports non-byte-aligned key sizes, but not well. For example,
568 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
569 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
570 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
571 {
572 status = PSA_ERROR_NOT_SUPPORTED;
573 goto exit;
574 }
575 status = psa_check_rsa_key_byte_aligned( rsa );
576
577exit:
578 /* Free the content of the pk object only on error. */
579 if( status != PSA_SUCCESS )
580 {
581 mbedtls_pk_free( &pk );
582 return( status );
583 }
584
585 /* On success, store the content of the object in the RSA context. */
586 *p_rsa = rsa;
587
588 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200589}
590#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
591
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000592#if defined(MBEDTLS_ECP_C)
593
594/* Import a public key given as the uncompressed representation defined by SEC1
595 * 2.3.3 as the content of an ECPoint. */
596static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
597 const uint8_t *data,
598 size_t data_length,
599 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200600{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000602 mbedtls_ecp_keypair *ecp = NULL;
603 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
604
605 *p_ecp = NULL;
606 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
607 if( ecp == NULL )
608 return( PSA_ERROR_INSUFFICIENT_MEMORY );
609 mbedtls_ecp_keypair_init( ecp );
610
611 /* Load the group. */
612 status = mbedtls_to_psa_error(
613 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616 /* Load the public value. */
617 status = mbedtls_to_psa_error(
618 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
619 data, data_length ) );
620 if( status != PSA_SUCCESS )
621 goto exit;
622
623 /* Check that the point is on the curve. */
624 status = mbedtls_to_psa_error(
625 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
626 if( status != PSA_SUCCESS )
627 goto exit;
628
629 *p_ecp = ecp;
630 return( PSA_SUCCESS );
631
632exit:
633 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200634 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000635 mbedtls_ecp_keypair_free( ecp );
636 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200637 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000638 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200639}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000640#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200641
Gilles Peskinef76aa772018-10-29 19:24:33 +0100642#if defined(MBEDTLS_ECP_C)
643/* Import a private key given as a byte string which is the private value
644 * in big-endian order. */
645static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
646 const uint8_t *data,
647 size_t data_length,
648 mbedtls_ecp_keypair **p_ecp )
649{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651 mbedtls_ecp_keypair *ecp = NULL;
652 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
653
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200654 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
655 return( PSA_ERROR_INVALID_ARGUMENT );
656
Gilles Peskinef76aa772018-10-29 19:24:33 +0100657 *p_ecp = NULL;
658 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
659 if( ecp == NULL )
660 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000661 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100662
663 /* Load the group. */
664 status = mbedtls_to_psa_error(
665 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
666 if( status != PSA_SUCCESS )
667 goto exit;
668 /* Load the secret value. */
669 status = mbedtls_to_psa_error(
670 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
671 if( status != PSA_SUCCESS )
672 goto exit;
673 /* Validate the private key. */
674 status = mbedtls_to_psa_error(
675 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678 /* Calculate the public key from the private key. */
679 status = mbedtls_to_psa_error(
680 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
681 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
682 if( status != PSA_SUCCESS )
683 goto exit;
684
685 *p_ecp = ecp;
686 return( PSA_SUCCESS );
687
688exit:
689 if( ecp != NULL )
690 {
691 mbedtls_ecp_keypair_free( ecp );
692 mbedtls_free( ecp );
693 }
694 return( status );
695}
696#endif /* defined(MBEDTLS_ECP_C) */
697
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100698/** Import key data into a slot. `slot->type` must have been set
699 * previously. This function assumes that the slot does not contain
700 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100701psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
702 const uint8_t *data,
703 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200705 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706
Darryl Green940d72c2018-07-13 13:18:51 +0100707 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100709 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 if( data_length > SIZE_MAX / 8 )
711 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100712 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200713 PSA_BYTES_TO_BITS( data_length ),
714 &slot->data.raw );
715 if( status != PSA_SUCCESS )
716 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200717 if( data_length != 0 )
718 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100719 }
720 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100721#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200722 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100723 {
Darryl Green940d72c2018-07-13 13:18:51 +0100724 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100725 data, data_length,
726 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100727 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000728 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
729 {
730 status = psa_import_ec_public_key(
731 PSA_KEY_TYPE_GET_CURVE( slot->type ),
732 data, data_length,
733 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000734 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100735 else
736#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000737#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
738 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100739 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000740 status = psa_import_rsa_key( slot->type,
741 data, data_length,
742 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 }
744 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000745#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 {
747 return( PSA_ERROR_NOT_SUPPORTED );
748 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000749 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100750}
751
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100752/** Calculate the intersection of two algorithm usage policies.
753 *
754 * Return 0 (which allows no operation) on incompatibility.
755 */
756static psa_algorithm_t psa_key_policy_algorithm_intersection(
757 psa_algorithm_t alg1,
758 psa_algorithm_t alg2 )
759{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200760 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100761 if( alg1 == alg2 )
762 return( alg1 );
763 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100764 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100765 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
766 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
767 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
768 {
769 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
770 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100771 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100772 return( alg1 );
773 }
774 /* If the policies are incompatible, allow nothing. */
775 return( 0 );
776}
777
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200778static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
779 psa_algorithm_t requested_alg )
780{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200781 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200782 if( requested_alg == policy_alg )
783 return( 1 );
784 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200785 * and requested_alg is the same hash-and-sign family with any hash,
786 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200787 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
788 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
789 {
790 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
791 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
792 }
793 /* If it isn't permitted, it's forbidden. */
794 return( 0 );
795}
796
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100797/** Test whether a policy permits an algorithm.
798 *
799 * The caller must test usage flags separately.
800 */
801static int psa_key_policy_permits( const psa_key_policy_t *policy,
802 psa_algorithm_t alg )
803{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200804 return( psa_key_algorithm_permits( policy->alg, alg ) ||
805 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100806}
807
Gilles Peskinef603c712019-01-19 13:40:11 +0100808/** Restrict a key policy based on a constraint.
809 *
810 * \param[in,out] policy The policy to restrict.
811 * \param[in] constraint The policy constraint to apply.
812 *
813 * \retval #PSA_SUCCESS
814 * \c *policy contains the intersection of the original value of
815 * \c *policy and \c *constraint.
816 * \retval #PSA_ERROR_INVALID_ARGUMENT
817 * \c *policy and \c *constraint are incompatible.
818 * \c *policy is unchanged.
819 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100820static psa_status_t psa_restrict_key_policy(
821 psa_key_policy_t *policy,
822 const psa_key_policy_t *constraint )
823{
824 psa_algorithm_t intersection_alg =
825 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200826 psa_algorithm_t intersection_alg2 =
827 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100828 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
829 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200830 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
831 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100832 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100833 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200834 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100835 return( PSA_SUCCESS );
836}
837
Darryl Green06fd18d2018-07-16 11:21:11 +0100838/** Retrieve a slot which must contain a key. The key must have allow all the
839 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
840 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100841static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100842 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100843 psa_key_usage_t usage,
844 psa_algorithm_t alg )
845{
846 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100847 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100848
849 *p_slot = NULL;
850
Gilles Peskinec5487a82018-12-03 18:08:14 +0100851 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100852 if( status != PSA_SUCCESS )
853 return( status );
854 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200855 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100856
857 /* Enforce that usage policy for the key slot contains all the flags
858 * required by the usage parameter. There is one exception: public
859 * keys can always be exported, so we treat public key objects as
860 * if they had the export flag. */
861 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
862 usage &= ~PSA_KEY_USAGE_EXPORT;
863 if( ( slot->policy.usage & usage ) != usage )
864 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100865
866 /* Enforce that the usage policy permits the requested algortihm. */
867 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100868 return( PSA_ERROR_NOT_PERMITTED );
869
870 *p_slot = slot;
871 return( PSA_SUCCESS );
872}
Darryl Green940d72c2018-07-13 13:18:51 +0100873
Gilles Peskine28f8f302019-07-24 13:30:31 +0200874/** Retrieve a slot which must contain a transparent key.
875 *
876 * A transparent key is a key for which the key material is directly
877 * available, as opposed to a key in a secure element.
878 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200879 * This is a temporary function to use instead of psa_get_key_from_slot()
880 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200881 */
882#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
883static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
884 psa_key_slot_t **p_slot,
885 psa_key_usage_t usage,
886 psa_algorithm_t alg )
887{
888 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
889 if( status != PSA_SUCCESS )
890 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200891 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200892 {
893 *p_slot = NULL;
894 return( PSA_ERROR_NOT_SUPPORTED );
895 }
896 return( PSA_SUCCESS );
897}
898#else /* MBEDTLS_PSA_CRYPTO_SE_C */
899/* With no secure element support, all keys are transparent. */
900#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
901 psa_get_key_from_slot( handle, p_slot, usage, alg )
902#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
903
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100904/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100905static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000906{
Gilles Peskine73167e12019-07-12 23:44:37 +0200907#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
908 if( psa_key_slot_is_external( slot ) )
909 {
910 /* No key material to clean. */
911 }
912 else
913#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +0000914 if( slot->type == PSA_KEY_TYPE_NONE )
915 {
916 /* No key material to clean. */
917 }
918 else if( key_type_is_raw_bytes( slot->type ) )
919 {
920 mbedtls_free( slot->data.raw.data );
921 }
922 else
923#if defined(MBEDTLS_RSA_C)
924 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
925 {
926 mbedtls_rsa_free( slot->data.rsa );
927 mbedtls_free( slot->data.rsa );
928 }
929 else
930#endif /* defined(MBEDTLS_RSA_C) */
931#if defined(MBEDTLS_ECP_C)
932 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
933 {
934 mbedtls_ecp_keypair_free( slot->data.ecp );
935 mbedtls_free( slot->data.ecp );
936 }
937 else
938#endif /* defined(MBEDTLS_ECP_C) */
939 {
940 /* Shouldn't happen: the key type is not any type that we
941 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200942 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000943 }
944
945 return( PSA_SUCCESS );
946}
947
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100948static void psa_abort_operations_using_key( psa_key_slot_t *slot )
949{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200950 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100951 (void) slot;
952}
953
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100954/** Completely wipe a slot in memory, including its policy.
955 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100956psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100957{
958 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100959 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100960 /* At this point, key material and other type-specific content has
961 * been wiped. Clear remaining metadata. We can call memset and not
962 * zeroize because the metadata is not particularly sensitive. */
963 memset( slot, 0, sizeof( *slot ) );
964 return( status );
965}
966
Gilles Peskinec5487a82018-12-03 18:08:14 +0100967psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100969 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100970 psa_status_t status = PSA_SUCCESS;
971 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200972#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
973 psa_se_drv_table_entry_t *driver;
974#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975
Gilles Peskinec5487a82018-12-03 18:08:14 +0100976 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200977 if( status != PSA_SUCCESS )
978 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +0200979
980#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
981 driver = psa_get_se_driver_entry( slot->lifetime );
982 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +0200983 {
Gilles Peskine60450a42019-07-25 11:32:45 +0200984 /* For a key in a secure element, we need to do three things:
985 * remove the key file in internal storage, destroy the
986 * key inside the secure element, and update the driver's
987 * persistent data. Start a transaction that will encompass these
988 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +0200989 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
990 psa_crypto_transaction.key.lifetime = slot->lifetime;
991 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
992 psa_crypto_transaction.key.id = slot->persistent_storage_id;
993 status = psa_crypto_save_transaction( );
994 if( status != PSA_SUCCESS )
995 {
Gilles Peskine66be51c2019-07-25 18:02:52 +0200996 (void) psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +0200997 /* TOnogrepDO: destroy what can be destroyed anyway */
998 return( status );
999 }
1000
Gilles Peskine354f7672019-07-12 23:46:38 +02001001 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +02001002 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001003#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1004
Darryl Greend49a4992018-06-18 17:27:26 +01001005#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1006 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1007 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001008 storage_status =
1009 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +01001010 }
1011#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001012
Gilles Peskinefc762652019-07-22 19:30:34 +02001013#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1014 if( driver != NULL )
1015 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001016 psa_status_t status2;
1017 status = psa_save_se_persistent_data( driver );
1018 status2 = psa_crypto_stop_transaction( );
1019 if( status == PSA_SUCCESS )
1020 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001021 if( status != PSA_SUCCESS )
1022 {
1023 /* TOnogrepDO: destroy what can be destroyed anyway */
1024 return( status );
1025 }
1026 }
1027#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1028
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001029 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001030 if( status != PSA_SUCCESS )
1031 return( status );
1032 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033}
1034
Gilles Peskineb870b182018-07-06 16:02:09 +02001035/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001036static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +02001037{
1038 if( key_type_is_raw_bytes( slot->type ) )
1039 return( slot->data.raw.bytes * 8 );
1040#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001041 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001042 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001043#endif /* defined(MBEDTLS_RSA_C) */
1044#if defined(MBEDTLS_ECP_C)
1045 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1046 return( slot->data.ecp->grp.pbits );
1047#endif /* defined(MBEDTLS_ECP_C) */
1048 /* Shouldn't happen except on an empty slot. */
1049 return( 0 );
1050}
1051
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001052void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1053{
Gilles Peskineb699f072019-04-26 16:06:02 +02001054 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001055 memset( attributes, 0, sizeof( *attributes ) );
1056}
1057
Gilles Peskineb699f072019-04-26 16:06:02 +02001058psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1059 psa_key_type_t type,
1060 const uint8_t *data,
1061 size_t data_length )
1062{
1063 uint8_t *copy = NULL;
1064
1065 if( data_length != 0 )
1066 {
1067 copy = mbedtls_calloc( 1, data_length );
1068 if( copy == NULL )
1069 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1070 memcpy( copy, data, data_length );
1071 }
1072 /* After this point, this function is guaranteed to succeed, so it
1073 * can start modifying `*attributes`. */
1074
1075 if( attributes->domain_parameters != NULL )
1076 {
1077 mbedtls_free( attributes->domain_parameters );
1078 attributes->domain_parameters = NULL;
1079 attributes->domain_parameters_size = 0;
1080 }
1081
1082 attributes->domain_parameters = copy;
1083 attributes->domain_parameters_size = data_length;
1084 attributes->type = type;
1085 return( PSA_SUCCESS );
1086}
1087
1088psa_status_t psa_get_key_domain_parameters(
1089 const psa_key_attributes_t *attributes,
1090 uint8_t *data, size_t data_size, size_t *data_length )
1091{
1092 if( attributes->domain_parameters_size > data_size )
1093 return( PSA_ERROR_BUFFER_TOO_SMALL );
1094 *data_length = attributes->domain_parameters_size;
1095 if( attributes->domain_parameters_size != 0 )
1096 memcpy( data, attributes->domain_parameters,
1097 attributes->domain_parameters_size );
1098 return( PSA_SUCCESS );
1099}
1100
1101#if defined(MBEDTLS_RSA_C)
1102static psa_status_t psa_get_rsa_public_exponent(
1103 const mbedtls_rsa_context *rsa,
1104 psa_key_attributes_t *attributes )
1105{
1106 mbedtls_mpi mpi;
1107 int ret;
1108 uint8_t *buffer = NULL;
1109 size_t buflen;
1110 mbedtls_mpi_init( &mpi );
1111
1112 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1113 if( ret != 0 )
1114 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001115 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1116 {
1117 /* It's the default value, which is reported as an empty string,
1118 * so there's nothing to do. */
1119 goto exit;
1120 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001121
1122 buflen = mbedtls_mpi_size( &mpi );
1123 buffer = mbedtls_calloc( 1, buflen );
1124 if( buffer == NULL )
1125 {
1126 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1127 goto exit;
1128 }
1129 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1130 if( ret != 0 )
1131 goto exit;
1132 attributes->domain_parameters = buffer;
1133 attributes->domain_parameters_size = buflen;
1134
1135exit:
1136 mbedtls_mpi_free( &mpi );
1137 if( ret != 0 )
1138 mbedtls_free( buffer );
1139 return( mbedtls_to_psa_error( ret ) );
1140}
1141#endif /* MBEDTLS_RSA_C */
1142
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001143/** Retrieve the readily-accessible attributes of a key in a slot.
1144 *
1145 * This function does not compute attributes that are not directly
1146 * stored in the slot, such as the bit size of a transparent key.
1147 */
1148static void psa_get_key_slot_attributes( psa_key_slot_t *slot,
1149 psa_key_attributes_t *attributes )
1150{
1151 attributes->id = slot->persistent_storage_id;
1152 attributes->lifetime = slot->lifetime;
1153 attributes->policy = slot->policy;
1154 attributes->type = slot->type;
1155}
1156
1157/** Retrieve all the publicly-accessible attributes of a key.
1158 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001159psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1160 psa_key_attributes_t *attributes )
1161{
1162 psa_key_slot_t *slot;
1163 psa_status_t status;
1164
1165 psa_reset_key_attributes( attributes );
1166
Gilles Peskine28f8f302019-07-24 13:30:31 +02001167 status = psa_get_transparent_key( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001168 if( status != PSA_SUCCESS )
1169 return( status );
1170
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001171 psa_get_key_slot_attributes( slot, attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001172 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001173
1174 switch( slot->type )
1175 {
1176#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001177 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001178 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1179 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1180 break;
1181#endif
1182 default:
1183 /* Nothing else to do. */
1184 break;
1185 }
1186
1187 if( status != PSA_SUCCESS )
1188 psa_reset_key_attributes( attributes );
1189 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001190}
1191
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001192#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001193static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1194 unsigned char *buf, size_t size )
1195{
1196 int ret;
1197 unsigned char *c;
1198 size_t len = 0;
1199
1200 c = buf + size;
1201
1202 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1203
1204 return( (int) len );
1205}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001206#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001207
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001208static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1209 uint8_t *data,
1210 size_t data_size,
1211 size_t *data_length,
1212 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001213{
Gilles Peskine5d309672019-07-12 23:47:28 +02001214#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1215 const psa_drv_se_t *drv;
1216 psa_drv_se_context_t *drv_context;
1217#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1218
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001219 *data_length = 0;
1220
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001221 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001222 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001223
Gilles Peskine5d309672019-07-12 23:47:28 +02001224#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1225 if( psa_get_se_driver( slot->lifetime, &drv, &drv_context ) )
1226 {
1227 psa_drv_se_export_key_t method;
1228 if( drv->key_management == NULL )
1229 return( PSA_ERROR_NOT_SUPPORTED );
1230 method = ( export_public_key ?
1231 drv->key_management->p_export_public :
1232 drv->key_management->p_export );
1233 if( method == NULL )
1234 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001235 return( method( drv_context,
1236 slot->data.se.slot_number,
1237 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001238 }
1239#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1240
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001241 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001242 {
1243 if( slot->data.raw.bytes > data_size )
1244 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001245 if( data_size != 0 )
1246 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001247 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001248 memset( data + slot->data.raw.bytes, 0,
1249 data_size - slot->data.raw.bytes );
1250 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001251 *data_length = slot->data.raw.bytes;
1252 return( PSA_SUCCESS );
1253 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001254#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001255 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001256 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001257 psa_status_t status;
1258
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001259 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001260 if( bytes > data_size )
1261 return( PSA_ERROR_BUFFER_TOO_SMALL );
1262 status = mbedtls_to_psa_error(
1263 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1264 if( status != PSA_SUCCESS )
1265 return( status );
1266 memset( data + bytes, 0, data_size - bytes );
1267 *data_length = bytes;
1268 return( PSA_SUCCESS );
1269 }
1270#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001271 else
Moran Peker17e36e12018-05-02 12:55:20 +03001272 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001273#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001274 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001275 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001276 {
Moran Pekera998bc62018-04-16 18:16:20 +03001277 mbedtls_pk_context pk;
1278 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001279 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001280 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001281#if defined(MBEDTLS_RSA_C)
1282 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001283 pk.pk_info = &mbedtls_rsa_info;
1284 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001285#else
1286 return( PSA_ERROR_NOT_SUPPORTED );
1287#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001288 }
1289 else
1290 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001291#if defined(MBEDTLS_ECP_C)
1292 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001293 pk.pk_info = &mbedtls_eckey_info;
1294 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001295#else
1296 return( PSA_ERROR_NOT_SUPPORTED );
1297#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001298 }
Moran Pekerd7326592018-05-29 16:56:39 +03001299 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001300 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001301 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001302 }
Moran Peker17e36e12018-05-02 12:55:20 +03001303 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001304 {
Moran Peker17e36e12018-05-02 12:55:20 +03001305 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001306 }
Moran Peker60364322018-04-29 11:34:58 +03001307 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001308 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001309 /* If data_size is 0 then data may be NULL and then the
1310 * call to memset would have undefined behavior. */
1311 if( data_size != 0 )
1312 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001313 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001314 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001315 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1316 * Move the data to the beginning and erase remaining data
1317 * at the original location. */
1318 if( 2 * (size_t) ret <= data_size )
1319 {
1320 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001321 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001322 }
1323 else if( (size_t) ret < data_size )
1324 {
1325 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001326 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001327 }
Moran Pekera998bc62018-04-16 18:16:20 +03001328 *data_length = ret;
1329 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001330 }
1331 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001332#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001333 {
1334 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001335 it is valid for a special-purpose implementation to omit
1336 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001337 return( PSA_ERROR_NOT_SUPPORTED );
1338 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001339 }
1340}
1341
Gilles Peskinec5487a82018-12-03 18:08:14 +01001342psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001343 uint8_t *data,
1344 size_t data_size,
1345 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001346{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001347 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001348 psa_status_t status;
1349
1350 /* Set the key to empty now, so that even when there are errors, we always
1351 * set data_length to a value between 0 and data_size. On error, setting
1352 * the key to empty is a good choice because an empty key representation is
1353 * unlikely to be accepted anywhere. */
1354 *data_length = 0;
1355
1356 /* Export requires the EXPORT flag. There is an exception for public keys,
1357 * which don't require any flag, but psa_get_key_from_slot takes
1358 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001359 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001360 if( status != PSA_SUCCESS )
1361 return( status );
1362 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001363 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001364}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001365
Gilles Peskinec5487a82018-12-03 18:08:14 +01001366psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001367 uint8_t *data,
1368 size_t data_size,
1369 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001370{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001371 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001372 psa_status_t status;
1373
1374 /* Set the key to empty now, so that even when there are errors, we always
1375 * set data_length to a value between 0 and data_size. On error, setting
1376 * the key to empty is a good choice because an empty key representation is
1377 * unlikely to be accepted anywhere. */
1378 *data_length = 0;
1379
1380 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001381 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001382 if( status != PSA_SUCCESS )
1383 return( status );
1384 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001385 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001386}
1387
Gilles Peskine4747d192019-04-17 15:05:45 +02001388static psa_status_t psa_set_key_policy_internal(
1389 psa_key_slot_t *slot,
1390 const psa_key_policy_t *policy )
1391{
1392 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001393 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001394 PSA_KEY_USAGE_ENCRYPT |
1395 PSA_KEY_USAGE_DECRYPT |
1396 PSA_KEY_USAGE_SIGN |
1397 PSA_KEY_USAGE_VERIFY |
1398 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1399 return( PSA_ERROR_INVALID_ARGUMENT );
1400
1401 slot->policy = *policy;
1402 return( PSA_SUCCESS );
1403}
1404
1405/** Prepare a key slot to receive key material.
1406 *
1407 * This function allocates a key slot and sets its metadata.
1408 *
1409 * If this function fails, call psa_fail_key_creation().
1410 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001411 * This function is intended to be used as follows:
1412 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1413 * it with the specified attributes, and assign it a handle.
1414 * -# Populate the slot with the key material.
1415 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1416 * In case of failure at any step, stop the sequence and call
1417 * psa_fail_key_creation().
1418 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001419 * \param[in] attributes Key attributes for the new key.
1420 * \param[out] handle On success, a handle for the allocated slot.
1421 * \param[out] p_slot On success, a pointer to the prepared slot.
1422 * \param[out] p_drv On any return, the driver for the key, if any.
1423 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001424 *
1425 * \retval #PSA_SUCCESS
1426 * The key slot is ready to receive key material.
1427 * \return If this function fails, the key slot is an invalid state.
1428 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001429 */
1430static psa_status_t psa_start_key_creation(
1431 const psa_key_attributes_t *attributes,
1432 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001433 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001434 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001435{
1436 psa_status_t status;
1437 psa_key_slot_t *slot;
1438
Gilles Peskine011e4282019-06-26 18:34:38 +02001439 *p_drv = NULL;
1440
Gilles Peskine267c6562019-05-27 19:01:54 +02001441 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001442 if( status != PSA_SUCCESS )
1443 return( status );
1444 slot = *p_slot;
1445
1446 status = psa_set_key_policy_internal( slot, &attributes->policy );
1447 if( status != PSA_SUCCESS )
1448 return( status );
1449 slot->lifetime = attributes->lifetime;
Gilles Peskine011e4282019-06-26 18:34:38 +02001450
Gilles Peskine4747d192019-04-17 15:05:45 +02001451 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001452 {
1453 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskine011e4282019-06-26 18:34:38 +02001454 attributes->id,
1455 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001456 if( status != PSA_SUCCESS )
1457 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001458 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001459 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001460 slot->type = attributes->type;
1461
Gilles Peskinecbaff462019-07-12 23:46:04 +02001462#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine60450a42019-07-25 11:32:45 +02001463 /* For a key in a secure element, we need to do three things:
1464 * create the key file in internal storage, create the
1465 * key inside the secure element, and update the driver's
1466 * persistent data. Start a transaction that will encompass these
1467 * three actions. */
1468 /* The first thing to do is to find a slot number for the new key.
1469 * We save the slot number in persistent storage as part of the
1470 * transaction data. It will be needed to recover if the power
1471 * fails during the key creation process, to clean up on the secure
1472 * element side after restarting. Obtaining a slot number from the
1473 * secure element driver updates its persistent state, but we do not yet
1474 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001475 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001476 if( *p_drv != NULL )
1477 {
1478 status = psa_find_se_slot_for_key( attributes, *p_drv,
1479 &slot->data.se.slot_number );
1480 if( status != PSA_SUCCESS )
1481 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001482 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1483 psa_crypto_transaction.key.lifetime = slot->lifetime;
1484 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1485 psa_crypto_transaction.key.id = slot->persistent_storage_id;
1486 status = psa_crypto_save_transaction( );
1487 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001488 {
1489 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001490 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001491 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001492 }
1493#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1494
Gilles Peskine4747d192019-04-17 15:05:45 +02001495 return( status );
1496}
1497
1498/** Finalize the creation of a key once its key material has been set.
1499 *
1500 * This entails writing the key to persistent storage.
1501 *
1502 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001503 * See the documentation of psa_start_key_creation() for the intended use
1504 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001505 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001506 * \param[in,out] slot Pointer to the slot with key material.
1507 * \param[in] driver The secure element driver for the key,
1508 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001509 *
1510 * \retval #PSA_SUCCESS
1511 * The key was successfully created. The handle is now valid.
1512 * \return If this function fails, the key slot is an invalid state.
1513 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001514 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001515static psa_status_t psa_finish_key_creation(
1516 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001517 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001518{
1519 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001520 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001521 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001522
1523#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1df83d42019-07-23 16:13:14 +02001524 if( slot->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001525 {
1526 uint8_t *buffer = NULL;
1527 size_t buffer_size = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001528 size_t length = 0;
Gilles Peskine4747d192019-04-17 15:05:45 +02001529
Gilles Peskine1df83d42019-07-23 16:13:14 +02001530#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1531 if( driver != NULL )
1532 {
1533 buffer = (uint8_t*) &slot->data.se.slot_number;
1534 length = sizeof( slot->data.se.slot_number );
1535 }
1536 else
1537#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1538 {
1539 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
1540 psa_get_key_slot_bits( slot ) );
1541 buffer = mbedtls_calloc( 1, buffer_size );
1542 if( buffer == NULL && buffer_size != 0 )
1543 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1544 status = psa_internal_export_key( slot,
1545 buffer, buffer_size, &length,
1546 0 );
1547 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001548
1549 if( status == PSA_SUCCESS )
1550 {
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1552 psa_get_key_slot_attributes( slot, &attributes );
1553 status = psa_save_persistent_key( &attributes, buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001554 }
1555
Gilles Peskine1df83d42019-07-23 16:13:14 +02001556#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1557 if( driver == NULL )
1558#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1559 {
1560 if( buffer_size != 0 )
1561 mbedtls_platform_zeroize( buffer, buffer_size );
1562 mbedtls_free( buffer );
1563 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001564 }
1565#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1566
Gilles Peskinecbaff462019-07-12 23:46:04 +02001567#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1568 if( driver != NULL )
1569 {
1570 status = psa_save_se_persistent_data( driver );
1571 if( status != PSA_SUCCESS )
1572 {
1573 psa_destroy_persistent_key( slot->persistent_storage_id );
1574 return( status );
1575 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001576 status = psa_crypto_stop_transaction( );
1577 if( status != PSA_SUCCESS )
1578 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001579 }
1580#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1581
Gilles Peskine4747d192019-04-17 15:05:45 +02001582 return( status );
1583}
1584
1585/** Abort the creation of a key.
1586 *
1587 * You may call this function after calling psa_start_key_creation(),
1588 * or after psa_finish_key_creation() fails. In other circumstances, this
1589 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001590 * See the documentation of psa_start_key_creation() for the intended use
1591 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001592 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001593 * \param[in,out] slot Pointer to the slot with key material.
1594 * \param[in] driver The secure element driver for the key,
1595 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001596 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001597static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001598 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001599{
Gilles Peskine011e4282019-06-26 18:34:38 +02001600 (void) driver;
1601
Gilles Peskine4747d192019-04-17 15:05:45 +02001602 if( slot == NULL )
1603 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001604
1605#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1606 /* TOnogrepDO: If the key has already been created in the secure
1607 * element, and the failure happened later (when saving metadata
1608 * to internal storage), we need to destroy the key in the secure
1609 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001610
1611 /* Abort the ongoing transaction if any. We already did what it
1612 * takes to undo any partial creation. All that's left is to update
1613 * the transaction data itself. */
1614 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001615#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1616
Gilles Peskine4747d192019-04-17 15:05:45 +02001617 psa_wipe_key_slot( slot );
1618}
1619
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001620static psa_status_t psa_check_key_slot_attributes(
1621 const psa_key_slot_t *slot,
1622 const psa_key_attributes_t *attributes )
1623{
1624 if( attributes->type != 0 )
1625 {
1626 if( attributes->type != slot->type )
1627 return( PSA_ERROR_INVALID_ARGUMENT );
1628 }
1629
1630 if( attributes->domain_parameters_size != 0 )
1631 {
1632#if defined(MBEDTLS_RSA_C)
1633 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1634 {
1635 mbedtls_mpi actual, required;
1636 int ret;
1637 mbedtls_mpi_init( &actual );
1638 mbedtls_mpi_init( &required );
1639 ret = mbedtls_rsa_export( slot->data.rsa,
1640 NULL, NULL, NULL, NULL, &actual );
1641 if( ret != 0 )
1642 goto rsa_exit;
1643 ret = mbedtls_mpi_read_binary( &required,
1644 attributes->domain_parameters,
1645 attributes->domain_parameters_size );
1646 if( ret != 0 )
1647 goto rsa_exit;
1648 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1649 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1650 rsa_exit:
1651 mbedtls_mpi_free( &actual );
1652 mbedtls_mpi_free( &required );
1653 if( ret != 0)
1654 return( mbedtls_to_psa_error( ret ) );
1655 }
1656 else
1657#endif
1658 {
1659 return( PSA_ERROR_INVALID_ARGUMENT );
1660 }
1661 }
1662
1663 if( attributes->bits != 0 )
1664 {
1665 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1666 return( PSA_ERROR_INVALID_ARGUMENT );
1667 }
1668
1669 return( PSA_SUCCESS );
1670}
1671
Gilles Peskine4747d192019-04-17 15:05:45 +02001672psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001673 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001674 size_t data_length,
1675 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001676{
1677 psa_status_t status;
1678 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001679 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001680
Gilles Peskine011e4282019-06-26 18:34:38 +02001681 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001682 if( status != PSA_SUCCESS )
1683 goto exit;
1684
Gilles Peskine5d309672019-07-12 23:47:28 +02001685#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1686 if( driver != NULL )
1687 {
1688 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
1689 if( drv->key_management == NULL ||
1690 drv->key_management->p_import == NULL )
1691 {
1692 status = PSA_ERROR_NOT_SUPPORTED;
1693 goto exit;
1694 }
1695 status = drv->key_management->p_import(
1696 psa_get_se_driver_context( driver ),
1697 slot->data.se.slot_number,
1698 slot->lifetime, slot->type, slot->policy.alg, slot->policy.usage,
1699 data, data_length );
1700 /* TOnogrepDO: psa_check_key_slot_attributes? */
1701 }
1702 else
1703#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1704 {
1705 status = psa_import_key_into_slot( slot, data, data_length );
1706 if( status != PSA_SUCCESS )
1707 goto exit;
1708 status = psa_check_key_slot_attributes( slot, attributes );
1709 if( status != PSA_SUCCESS )
1710 goto exit;
1711 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001712
Gilles Peskine011e4282019-06-26 18:34:38 +02001713 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001714exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001715 if( status != PSA_SUCCESS )
1716 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001717 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001718 *handle = 0;
1719 }
1720 return( status );
1721}
1722
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001723static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001724 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001725{
1726 psa_status_t status;
1727 uint8_t *buffer = NULL;
1728 size_t buffer_size = 0;
1729 size_t length;
1730
1731 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001732 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001733 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001734 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001735 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001736 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1737 if( status != PSA_SUCCESS )
1738 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001739 target->type = source->type;
1740 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001741
1742exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001743 if( buffer_size != 0 )
1744 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001745 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001746 return( status );
1747}
1748
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001749psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1750 const psa_key_attributes_t *specified_attributes,
1751 psa_key_handle_t *target_handle )
1752{
1753 psa_status_t status;
1754 psa_key_slot_t *source_slot = NULL;
1755 psa_key_slot_t *target_slot = NULL;
1756 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001757 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001758
Gilles Peskine28f8f302019-07-24 13:30:31 +02001759 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001760 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001761 if( status != PSA_SUCCESS )
1762 goto exit;
1763
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001764 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1765 if( status != PSA_SUCCESS )
1766 goto exit;
1767
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001768 status = psa_restrict_key_policy( &actual_attributes.policy,
1769 &source_slot->policy );
1770 if( status != PSA_SUCCESS )
1771 goto exit;
1772
1773 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001774 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001775 if( status != PSA_SUCCESS )
1776 goto exit;
1777
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001778#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1779 if( driver != NULL )
1780 {
1781 /* Copying to a secure element is not implemented yet. */
1782 status = PSA_ERROR_NOT_SUPPORTED;
1783 goto exit;
1784 }
1785#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1786
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001787 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001788 if( status != PSA_SUCCESS )
1789 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001790
Gilles Peskine011e4282019-06-26 18:34:38 +02001791 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001792exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001793 if( status != PSA_SUCCESS )
1794 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001795 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001796 *target_handle = 0;
1797 }
1798 return( status );
1799}
1800
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001801
1802
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001803/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001804/* Message digests */
1805/****************************************************************/
1806
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001807static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001808{
1809 switch( alg )
1810 {
1811#if defined(MBEDTLS_MD2_C)
1812 case PSA_ALG_MD2:
1813 return( &mbedtls_md2_info );
1814#endif
1815#if defined(MBEDTLS_MD4_C)
1816 case PSA_ALG_MD4:
1817 return( &mbedtls_md4_info );
1818#endif
1819#if defined(MBEDTLS_MD5_C)
1820 case PSA_ALG_MD5:
1821 return( &mbedtls_md5_info );
1822#endif
1823#if defined(MBEDTLS_RIPEMD160_C)
1824 case PSA_ALG_RIPEMD160:
1825 return( &mbedtls_ripemd160_info );
1826#endif
1827#if defined(MBEDTLS_SHA1_C)
1828 case PSA_ALG_SHA_1:
1829 return( &mbedtls_sha1_info );
1830#endif
1831#if defined(MBEDTLS_SHA256_C)
1832 case PSA_ALG_SHA_224:
1833 return( &mbedtls_sha224_info );
1834 case PSA_ALG_SHA_256:
1835 return( &mbedtls_sha256_info );
1836#endif
1837#if defined(MBEDTLS_SHA512_C)
1838 case PSA_ALG_SHA_384:
1839 return( &mbedtls_sha384_info );
1840 case PSA_ALG_SHA_512:
1841 return( &mbedtls_sha512_info );
1842#endif
1843 default:
1844 return( NULL );
1845 }
1846}
1847
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001848psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1849{
1850 switch( operation->alg )
1851 {
Gilles Peskine81736312018-06-26 15:04:31 +02001852 case 0:
1853 /* The object has (apparently) been initialized but it is not
1854 * in use. It's ok to call abort on such an object, and there's
1855 * nothing to do. */
1856 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001857#if defined(MBEDTLS_MD2_C)
1858 case PSA_ALG_MD2:
1859 mbedtls_md2_free( &operation->ctx.md2 );
1860 break;
1861#endif
1862#if defined(MBEDTLS_MD4_C)
1863 case PSA_ALG_MD4:
1864 mbedtls_md4_free( &operation->ctx.md4 );
1865 break;
1866#endif
1867#if defined(MBEDTLS_MD5_C)
1868 case PSA_ALG_MD5:
1869 mbedtls_md5_free( &operation->ctx.md5 );
1870 break;
1871#endif
1872#if defined(MBEDTLS_RIPEMD160_C)
1873 case PSA_ALG_RIPEMD160:
1874 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1875 break;
1876#endif
1877#if defined(MBEDTLS_SHA1_C)
1878 case PSA_ALG_SHA_1:
1879 mbedtls_sha1_free( &operation->ctx.sha1 );
1880 break;
1881#endif
1882#if defined(MBEDTLS_SHA256_C)
1883 case PSA_ALG_SHA_224:
1884 case PSA_ALG_SHA_256:
1885 mbedtls_sha256_free( &operation->ctx.sha256 );
1886 break;
1887#endif
1888#if defined(MBEDTLS_SHA512_C)
1889 case PSA_ALG_SHA_384:
1890 case PSA_ALG_SHA_512:
1891 mbedtls_sha512_free( &operation->ctx.sha512 );
1892 break;
1893#endif
1894 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001895 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001896 }
1897 operation->alg = 0;
1898 return( PSA_SUCCESS );
1899}
1900
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001901psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001902 psa_algorithm_t alg )
1903{
1904 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001905
1906 /* A context must be freshly initialized before it can be set up. */
1907 if( operation->alg != 0 )
1908 {
1909 return( PSA_ERROR_BAD_STATE );
1910 }
1911
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001912 switch( alg )
1913 {
1914#if defined(MBEDTLS_MD2_C)
1915 case PSA_ALG_MD2:
1916 mbedtls_md2_init( &operation->ctx.md2 );
1917 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1918 break;
1919#endif
1920#if defined(MBEDTLS_MD4_C)
1921 case PSA_ALG_MD4:
1922 mbedtls_md4_init( &operation->ctx.md4 );
1923 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1924 break;
1925#endif
1926#if defined(MBEDTLS_MD5_C)
1927 case PSA_ALG_MD5:
1928 mbedtls_md5_init( &operation->ctx.md5 );
1929 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1930 break;
1931#endif
1932#if defined(MBEDTLS_RIPEMD160_C)
1933 case PSA_ALG_RIPEMD160:
1934 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1935 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1936 break;
1937#endif
1938#if defined(MBEDTLS_SHA1_C)
1939 case PSA_ALG_SHA_1:
1940 mbedtls_sha1_init( &operation->ctx.sha1 );
1941 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1942 break;
1943#endif
1944#if defined(MBEDTLS_SHA256_C)
1945 case PSA_ALG_SHA_224:
1946 mbedtls_sha256_init( &operation->ctx.sha256 );
1947 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1948 break;
1949 case PSA_ALG_SHA_256:
1950 mbedtls_sha256_init( &operation->ctx.sha256 );
1951 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1952 break;
1953#endif
1954#if defined(MBEDTLS_SHA512_C)
1955 case PSA_ALG_SHA_384:
1956 mbedtls_sha512_init( &operation->ctx.sha512 );
1957 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1958 break;
1959 case PSA_ALG_SHA_512:
1960 mbedtls_sha512_init( &operation->ctx.sha512 );
1961 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1962 break;
1963#endif
1964 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001965 return( PSA_ALG_IS_HASH( alg ) ?
1966 PSA_ERROR_NOT_SUPPORTED :
1967 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001968 }
1969 if( ret == 0 )
1970 operation->alg = alg;
1971 else
1972 psa_hash_abort( operation );
1973 return( mbedtls_to_psa_error( ret ) );
1974}
1975
1976psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1977 const uint8_t *input,
1978 size_t input_length )
1979{
1980 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001981
1982 /* Don't require hash implementations to behave correctly on a
1983 * zero-length input, which may have an invalid pointer. */
1984 if( input_length == 0 )
1985 return( PSA_SUCCESS );
1986
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001987 switch( operation->alg )
1988 {
1989#if defined(MBEDTLS_MD2_C)
1990 case PSA_ALG_MD2:
1991 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1992 input, input_length );
1993 break;
1994#endif
1995#if defined(MBEDTLS_MD4_C)
1996 case PSA_ALG_MD4:
1997 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1998 input, input_length );
1999 break;
2000#endif
2001#if defined(MBEDTLS_MD5_C)
2002 case PSA_ALG_MD5:
2003 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2004 input, input_length );
2005 break;
2006#endif
2007#if defined(MBEDTLS_RIPEMD160_C)
2008 case PSA_ALG_RIPEMD160:
2009 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2010 input, input_length );
2011 break;
2012#endif
2013#if defined(MBEDTLS_SHA1_C)
2014 case PSA_ALG_SHA_1:
2015 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2016 input, input_length );
2017 break;
2018#endif
2019#if defined(MBEDTLS_SHA256_C)
2020 case PSA_ALG_SHA_224:
2021 case PSA_ALG_SHA_256:
2022 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2023 input, input_length );
2024 break;
2025#endif
2026#if defined(MBEDTLS_SHA512_C)
2027 case PSA_ALG_SHA_384:
2028 case PSA_ALG_SHA_512:
2029 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2030 input, input_length );
2031 break;
2032#endif
2033 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002034 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002035 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002036
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002037 if( ret != 0 )
2038 psa_hash_abort( operation );
2039 return( mbedtls_to_psa_error( ret ) );
2040}
2041
2042psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2043 uint8_t *hash,
2044 size_t hash_size,
2045 size_t *hash_length )
2046{
itayzafrir40835d42018-08-02 13:14:17 +03002047 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002048 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002049 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002050
2051 /* Fill the output buffer with something that isn't a valid hash
2052 * (barring an attack on the hash and deliberately-crafted input),
2053 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002054 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002055 /* If hash_size is 0 then hash may be NULL and then the
2056 * call to memset would have undefined behavior. */
2057 if( hash_size != 0 )
2058 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002059
2060 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002061 {
2062 status = PSA_ERROR_BUFFER_TOO_SMALL;
2063 goto exit;
2064 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002065
2066 switch( operation->alg )
2067 {
2068#if defined(MBEDTLS_MD2_C)
2069 case PSA_ALG_MD2:
2070 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2071 break;
2072#endif
2073#if defined(MBEDTLS_MD4_C)
2074 case PSA_ALG_MD4:
2075 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2076 break;
2077#endif
2078#if defined(MBEDTLS_MD5_C)
2079 case PSA_ALG_MD5:
2080 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2081 break;
2082#endif
2083#if defined(MBEDTLS_RIPEMD160_C)
2084 case PSA_ALG_RIPEMD160:
2085 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2086 break;
2087#endif
2088#if defined(MBEDTLS_SHA1_C)
2089 case PSA_ALG_SHA_1:
2090 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2091 break;
2092#endif
2093#if defined(MBEDTLS_SHA256_C)
2094 case PSA_ALG_SHA_224:
2095 case PSA_ALG_SHA_256:
2096 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2097 break;
2098#endif
2099#if defined(MBEDTLS_SHA512_C)
2100 case PSA_ALG_SHA_384:
2101 case PSA_ALG_SHA_512:
2102 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2103 break;
2104#endif
2105 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002106 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002107 }
itayzafrir40835d42018-08-02 13:14:17 +03002108 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002109
itayzafrir40835d42018-08-02 13:14:17 +03002110exit:
2111 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002112 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002113 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002114 return( psa_hash_abort( operation ) );
2115 }
2116 else
2117 {
2118 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002119 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002120 }
2121}
2122
Gilles Peskine2d277862018-06-18 15:41:12 +02002123psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2124 const uint8_t *hash,
2125 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002126{
2127 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2128 size_t actual_hash_length;
2129 psa_status_t status = psa_hash_finish( operation,
2130 actual_hash, sizeof( actual_hash ),
2131 &actual_hash_length );
2132 if( status != PSA_SUCCESS )
2133 return( status );
2134 if( actual_hash_length != hash_length )
2135 return( PSA_ERROR_INVALID_SIGNATURE );
2136 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2137 return( PSA_ERROR_INVALID_SIGNATURE );
2138 return( PSA_SUCCESS );
2139}
2140
Gilles Peskineeb35d782019-01-22 17:56:16 +01002141psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2142 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002143{
2144 if( target_operation->alg != 0 )
2145 return( PSA_ERROR_BAD_STATE );
2146
2147 switch( source_operation->alg )
2148 {
2149 case 0:
2150 return( PSA_ERROR_BAD_STATE );
2151#if defined(MBEDTLS_MD2_C)
2152 case PSA_ALG_MD2:
2153 mbedtls_md2_clone( &target_operation->ctx.md2,
2154 &source_operation->ctx.md2 );
2155 break;
2156#endif
2157#if defined(MBEDTLS_MD4_C)
2158 case PSA_ALG_MD4:
2159 mbedtls_md4_clone( &target_operation->ctx.md4,
2160 &source_operation->ctx.md4 );
2161 break;
2162#endif
2163#if defined(MBEDTLS_MD5_C)
2164 case PSA_ALG_MD5:
2165 mbedtls_md5_clone( &target_operation->ctx.md5,
2166 &source_operation->ctx.md5 );
2167 break;
2168#endif
2169#if defined(MBEDTLS_RIPEMD160_C)
2170 case PSA_ALG_RIPEMD160:
2171 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2172 &source_operation->ctx.ripemd160 );
2173 break;
2174#endif
2175#if defined(MBEDTLS_SHA1_C)
2176 case PSA_ALG_SHA_1:
2177 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2178 &source_operation->ctx.sha1 );
2179 break;
2180#endif
2181#if defined(MBEDTLS_SHA256_C)
2182 case PSA_ALG_SHA_224:
2183 case PSA_ALG_SHA_256:
2184 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2185 &source_operation->ctx.sha256 );
2186 break;
2187#endif
2188#if defined(MBEDTLS_SHA512_C)
2189 case PSA_ALG_SHA_384:
2190 case PSA_ALG_SHA_512:
2191 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2192 &source_operation->ctx.sha512 );
2193 break;
2194#endif
2195 default:
2196 return( PSA_ERROR_NOT_SUPPORTED );
2197 }
2198
2199 target_operation->alg = source_operation->alg;
2200 return( PSA_SUCCESS );
2201}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002202
2203
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002204/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002205/* MAC */
2206/****************************************************************/
2207
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002208static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002209 psa_algorithm_t alg,
2210 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002211 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002212 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002213{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002214 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002215 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002216
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002217 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002218 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002219
Gilles Peskine8c9def32018-02-08 10:02:12 +01002220 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2221 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002222 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002223 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002224 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002225 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002226 mode = MBEDTLS_MODE_STREAM;
2227 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002228 case PSA_ALG_CTR:
2229 mode = MBEDTLS_MODE_CTR;
2230 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002231 case PSA_ALG_CFB:
2232 mode = MBEDTLS_MODE_CFB;
2233 break;
2234 case PSA_ALG_OFB:
2235 mode = MBEDTLS_MODE_OFB;
2236 break;
2237 case PSA_ALG_CBC_NO_PADDING:
2238 mode = MBEDTLS_MODE_CBC;
2239 break;
2240 case PSA_ALG_CBC_PKCS7:
2241 mode = MBEDTLS_MODE_CBC;
2242 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002243 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002244 mode = MBEDTLS_MODE_CCM;
2245 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002246 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002247 mode = MBEDTLS_MODE_GCM;
2248 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002249 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2250 mode = MBEDTLS_MODE_CHACHAPOLY;
2251 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002252 default:
2253 return( NULL );
2254 }
2255 }
2256 else if( alg == PSA_ALG_CMAC )
2257 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002258 else
2259 return( NULL );
2260
2261 switch( key_type )
2262 {
2263 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002264 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002265 break;
2266 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002267 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2268 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002269 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002270 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002271 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002272 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002273 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2274 * but two-key Triple-DES is functionally three-key Triple-DES
2275 * with K1=K3, so that's how we present it to mbedtls. */
2276 if( key_bits == 128 )
2277 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002278 break;
2279 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002280 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002281 break;
2282 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002283 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002284 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002285 case PSA_KEY_TYPE_CHACHA20:
2286 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2287 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002288 default:
2289 return( NULL );
2290 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002291 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002292 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002293
Jaeden Amero23bbb752018-06-26 14:16:54 +01002294 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2295 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002296}
2297
Gilles Peskinea05219c2018-11-16 16:02:56 +01002298#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002299static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002300{
Gilles Peskine2d277862018-06-18 15:41:12 +02002301 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002302 {
2303 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002304 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002305 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002306 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002307 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002308 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002309 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002310 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002311 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002312 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002313 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002314 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002315 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002316 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002317 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002318 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002319 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002320 return( 128 );
2321 default:
2322 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002323 }
2324}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002325#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002326
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002327/* Initialize the MAC operation structure. Once this function has been
2328 * called, psa_mac_abort can run and will do the right thing. */
2329static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2330 psa_algorithm_t alg )
2331{
2332 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2333
2334 operation->alg = alg;
2335 operation->key_set = 0;
2336 operation->iv_set = 0;
2337 operation->iv_required = 0;
2338 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002339 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002340
2341#if defined(MBEDTLS_CMAC_C)
2342 if( alg == PSA_ALG_CMAC )
2343 {
2344 operation->iv_required = 0;
2345 mbedtls_cipher_init( &operation->ctx.cmac );
2346 status = PSA_SUCCESS;
2347 }
2348 else
2349#endif /* MBEDTLS_CMAC_C */
2350#if defined(MBEDTLS_MD_C)
2351 if( PSA_ALG_IS_HMAC( operation->alg ) )
2352 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002353 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2354 operation->ctx.hmac.hash_ctx.alg = 0;
2355 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002356 }
2357 else
2358#endif /* MBEDTLS_MD_C */
2359 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002360 if( ! PSA_ALG_IS_MAC( alg ) )
2361 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002362 }
2363
2364 if( status != PSA_SUCCESS )
2365 memset( operation, 0, sizeof( *operation ) );
2366 return( status );
2367}
2368
Gilles Peskine01126fa2018-07-12 17:04:55 +02002369#if defined(MBEDTLS_MD_C)
2370static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2371{
Gilles Peskine3f108122018-12-07 18:14:53 +01002372 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002373 return( psa_hash_abort( &hmac->hash_ctx ) );
2374}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002375
Janos Follath999f6482019-06-11 12:04:10 +01002376#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002377static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2378{
2379 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2380 memset( hmac, 0, sizeof( *hmac ) );
2381}
Janos Follath999f6482019-06-11 12:04:10 +01002382#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002383#endif /* MBEDTLS_MD_C */
2384
Gilles Peskine8c9def32018-02-08 10:02:12 +01002385psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2386{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002387 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002388 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002389 /* The object has (apparently) been initialized but it is not
2390 * in use. It's ok to call abort on such an object, and there's
2391 * nothing to do. */
2392 return( PSA_SUCCESS );
2393 }
2394 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002395#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002396 if( operation->alg == PSA_ALG_CMAC )
2397 {
2398 mbedtls_cipher_free( &operation->ctx.cmac );
2399 }
2400 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002401#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002402#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002403 if( PSA_ALG_IS_HMAC( operation->alg ) )
2404 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002405 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002406 }
2407 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002408#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002409 {
2410 /* Sanity check (shouldn't happen: operation->alg should
2411 * always have been initialized to a valid value). */
2412 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002413 }
Moran Peker41deec42018-04-04 15:43:05 +03002414
Gilles Peskine8c9def32018-02-08 10:02:12 +01002415 operation->alg = 0;
2416 operation->key_set = 0;
2417 operation->iv_set = 0;
2418 operation->iv_required = 0;
2419 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002420 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002421
Gilles Peskine8c9def32018-02-08 10:02:12 +01002422 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002423
2424bad_state:
2425 /* If abort is called on an uninitialized object, we can't trust
2426 * anything. Wipe the object in case it contains confidential data.
2427 * This may result in a memory leak if a pointer gets overwritten,
2428 * but it's too late to do anything about this. */
2429 memset( operation, 0, sizeof( *operation ) );
2430 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002431}
2432
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002433#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002434static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002435 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002436 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002437 const mbedtls_cipher_info_t *cipher_info )
2438{
2439 int ret;
2440
2441 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002442
2443 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2444 if( ret != 0 )
2445 return( ret );
2446
2447 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2448 slot->data.raw.data,
2449 key_bits );
2450 return( ret );
2451}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002452#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002453
Gilles Peskine248051a2018-06-20 16:09:38 +02002454#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002455static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2456 const uint8_t *key,
2457 size_t key_length,
2458 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002459{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002460 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002461 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002462 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002463 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002464 psa_status_t status;
2465
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002466 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2467 * overflow below. This should never trigger if the hash algorithm
2468 * is implemented correctly. */
2469 /* The size checks against the ipad and opad buffers cannot be written
2470 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2471 * because that triggers -Wlogical-op on GCC 7.3. */
2472 if( block_size > sizeof( ipad ) )
2473 return( PSA_ERROR_NOT_SUPPORTED );
2474 if( block_size > sizeof( hmac->opad ) )
2475 return( PSA_ERROR_NOT_SUPPORTED );
2476 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002477 return( PSA_ERROR_NOT_SUPPORTED );
2478
Gilles Peskined223b522018-06-11 18:12:58 +02002479 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002480 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002481 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002482 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002483 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002484 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002485 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002486 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002487 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002488 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002489 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002490 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002491 }
Gilles Peskine96889972018-07-12 17:07:03 +02002492 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2493 * but it is permitted. It is common when HMAC is used in HKDF, for
2494 * example. Don't call `memcpy` in the 0-length because `key` could be
2495 * an invalid pointer which would make the behavior undefined. */
2496 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002497 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002498
Gilles Peskined223b522018-06-11 18:12:58 +02002499 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2500 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002501 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002502 ipad[i] ^= 0x36;
2503 memset( ipad + key_length, 0x36, block_size - key_length );
2504
2505 /* Copy the key material from ipad to opad, flipping the requisite bits,
2506 * and filling the rest of opad with the requisite constant. */
2507 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002508 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2509 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002510
Gilles Peskine01126fa2018-07-12 17:04:55 +02002511 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002512 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002513 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002514
Gilles Peskine01126fa2018-07-12 17:04:55 +02002515 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002516
2517cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002518 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002519
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002520 return( status );
2521}
Gilles Peskine248051a2018-06-20 16:09:38 +02002522#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002523
Gilles Peskine89167cb2018-07-08 20:12:23 +02002524static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002525 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002526 psa_algorithm_t alg,
2527 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002528{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002529 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002530 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002532 psa_key_usage_t usage =
2533 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002534 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002535 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002536
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002537 /* A context must be freshly initialized before it can be set up. */
2538 if( operation->alg != 0 )
2539 {
2540 return( PSA_ERROR_BAD_STATE );
2541 }
2542
Gilles Peskined911eb72018-08-14 15:18:45 +02002543 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002544 if( status != PSA_SUCCESS )
2545 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002546 if( is_sign )
2547 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548
Gilles Peskine28f8f302019-07-24 13:30:31 +02002549 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002550 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002551 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002552 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002553
Gilles Peskine8c9def32018-02-08 10:02:12 +01002554#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002555 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002556 {
2557 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002558 mbedtls_cipher_info_from_psa( full_length_alg,
2559 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002560 int ret;
2561 if( cipher_info == NULL )
2562 {
2563 status = PSA_ERROR_NOT_SUPPORTED;
2564 goto exit;
2565 }
2566 operation->mac_size = cipher_info->block_size;
2567 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2568 status = mbedtls_to_psa_error( ret );
2569 }
2570 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002571#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002573 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002574 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002575 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002576 if( hash_alg == 0 )
2577 {
2578 status = PSA_ERROR_NOT_SUPPORTED;
2579 goto exit;
2580 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002581
2582 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2583 /* Sanity check. This shouldn't fail on a valid configuration. */
2584 if( operation->mac_size == 0 ||
2585 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2586 {
2587 status = PSA_ERROR_NOT_SUPPORTED;
2588 goto exit;
2589 }
2590
Gilles Peskine01126fa2018-07-12 17:04:55 +02002591 if( slot->type != PSA_KEY_TYPE_HMAC )
2592 {
2593 status = PSA_ERROR_INVALID_ARGUMENT;
2594 goto exit;
2595 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002596
Gilles Peskine01126fa2018-07-12 17:04:55 +02002597 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2598 slot->data.raw.data,
2599 slot->data.raw.bytes,
2600 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002601 }
2602 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002604 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002605 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002606 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002607 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002608
Gilles Peskined911eb72018-08-14 15:18:45 +02002609 if( truncated == 0 )
2610 {
2611 /* The "normal" case: untruncated algorithm. Nothing to do. */
2612 }
2613 else if( truncated < 4 )
2614 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002615 /* A very short MAC is too short for security since it can be
2616 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2617 * so we make this our minimum, even though 32 bits is still
2618 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002619 status = PSA_ERROR_NOT_SUPPORTED;
2620 }
2621 else if( truncated > operation->mac_size )
2622 {
2623 /* It's impossible to "truncate" to a larger length. */
2624 status = PSA_ERROR_INVALID_ARGUMENT;
2625 }
2626 else
2627 operation->mac_size = truncated;
2628
Gilles Peskinefbfac682018-07-08 20:51:54 +02002629exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002630 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002631 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002632 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002633 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002634 else
2635 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002636 operation->key_set = 1;
2637 }
2638 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002639}
2640
Gilles Peskine89167cb2018-07-08 20:12:23 +02002641psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002642 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002643 psa_algorithm_t alg )
2644{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002645 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002646}
2647
2648psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002649 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002650 psa_algorithm_t alg )
2651{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002652 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002653}
2654
Gilles Peskine8c9def32018-02-08 10:02:12 +01002655psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2656 const uint8_t *input,
2657 size_t input_length )
2658{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002659 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002660 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002661 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002662 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002663 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664 operation->has_input = 1;
2665
Gilles Peskine8c9def32018-02-08 10:02:12 +01002666#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002667 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002668 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002669 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2670 input, input_length );
2671 status = mbedtls_to_psa_error( ret );
2672 }
2673 else
2674#endif /* MBEDTLS_CMAC_C */
2675#if defined(MBEDTLS_MD_C)
2676 if( PSA_ALG_IS_HMAC( operation->alg ) )
2677 {
2678 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2679 input_length );
2680 }
2681 else
2682#endif /* MBEDTLS_MD_C */
2683 {
2684 /* This shouldn't happen if `operation` was initialized by
2685 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002686 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002687 }
2688
Gilles Peskinefbfac682018-07-08 20:51:54 +02002689 if( status != PSA_SUCCESS )
2690 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002691 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002692}
2693
Gilles Peskine01126fa2018-07-12 17:04:55 +02002694#if defined(MBEDTLS_MD_C)
2695static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2696 uint8_t *mac,
2697 size_t mac_size )
2698{
2699 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2700 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2701 size_t hash_size = 0;
2702 size_t block_size = psa_get_hash_block_size( hash_alg );
2703 psa_status_t status;
2704
Gilles Peskine01126fa2018-07-12 17:04:55 +02002705 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2706 if( status != PSA_SUCCESS )
2707 return( status );
2708 /* From here on, tmp needs to be wiped. */
2709
2710 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2711 if( status != PSA_SUCCESS )
2712 goto exit;
2713
2714 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2715 if( status != PSA_SUCCESS )
2716 goto exit;
2717
2718 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2719 if( status != PSA_SUCCESS )
2720 goto exit;
2721
Gilles Peskined911eb72018-08-14 15:18:45 +02002722 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2723 if( status != PSA_SUCCESS )
2724 goto exit;
2725
2726 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002727
2728exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002729 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002730 return( status );
2731}
2732#endif /* MBEDTLS_MD_C */
2733
mohammad16036df908f2018-04-02 08:34:15 -07002734static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002735 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002736 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002737{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002738 if( ! operation->key_set )
2739 return( PSA_ERROR_BAD_STATE );
2740 if( operation->iv_required && ! operation->iv_set )
2741 return( PSA_ERROR_BAD_STATE );
2742
Gilles Peskine8c9def32018-02-08 10:02:12 +01002743 if( mac_size < operation->mac_size )
2744 return( PSA_ERROR_BUFFER_TOO_SMALL );
2745
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002747 if( operation->alg == PSA_ALG_CMAC )
2748 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002749 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2750 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2751 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002752 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002753 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002754 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002755 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002756 else
2757#endif /* MBEDTLS_CMAC_C */
2758#if defined(MBEDTLS_MD_C)
2759 if( PSA_ALG_IS_HMAC( operation->alg ) )
2760 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002761 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002762 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002763 }
2764 else
2765#endif /* MBEDTLS_MD_C */
2766 {
2767 /* This shouldn't happen if `operation` was initialized by
2768 * a setup function. */
2769 return( PSA_ERROR_BAD_STATE );
2770 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002771}
2772
Gilles Peskineacd4be32018-07-08 19:56:25 +02002773psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2774 uint8_t *mac,
2775 size_t mac_size,
2776 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002777{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002778 psa_status_t status;
2779
Jaeden Amero252ef282019-02-15 14:05:35 +00002780 if( operation->alg == 0 )
2781 {
2782 return( PSA_ERROR_BAD_STATE );
2783 }
2784
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002785 /* Fill the output buffer with something that isn't a valid mac
2786 * (barring an attack on the mac and deliberately-crafted input),
2787 * in case the caller doesn't check the return status properly. */
2788 *mac_length = mac_size;
2789 /* If mac_size is 0 then mac may be NULL and then the
2790 * call to memset would have undefined behavior. */
2791 if( mac_size != 0 )
2792 memset( mac, '!', mac_size );
2793
Gilles Peskine89167cb2018-07-08 20:12:23 +02002794 if( ! operation->is_sign )
2795 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002796 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002797 }
mohammad16036df908f2018-04-02 08:34:15 -07002798
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002799 status = psa_mac_finish_internal( operation, mac, mac_size );
2800
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002801 if( status == PSA_SUCCESS )
2802 {
2803 status = psa_mac_abort( operation );
2804 if( status == PSA_SUCCESS )
2805 *mac_length = operation->mac_size;
2806 else
2807 memset( mac, '!', mac_size );
2808 }
2809 else
2810 psa_mac_abort( operation );
2811 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002812}
2813
Gilles Peskineacd4be32018-07-08 19:56:25 +02002814psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2815 const uint8_t *mac,
2816 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002817{
Gilles Peskine828ed142018-06-18 23:25:51 +02002818 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002819 psa_status_t status;
2820
Jaeden Amero252ef282019-02-15 14:05:35 +00002821 if( operation->alg == 0 )
2822 {
2823 return( PSA_ERROR_BAD_STATE );
2824 }
2825
Gilles Peskine89167cb2018-07-08 20:12:23 +02002826 if( operation->is_sign )
2827 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002828 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002829 }
2830 if( operation->mac_size != mac_length )
2831 {
2832 status = PSA_ERROR_INVALID_SIGNATURE;
2833 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002834 }
mohammad16036df908f2018-04-02 08:34:15 -07002835
2836 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002837 actual_mac, sizeof( actual_mac ) );
2838
2839 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2840 status = PSA_ERROR_INVALID_SIGNATURE;
2841
2842cleanup:
2843 if( status == PSA_SUCCESS )
2844 status = psa_mac_abort( operation );
2845 else
2846 psa_mac_abort( operation );
2847
Gilles Peskine3f108122018-12-07 18:14:53 +01002848 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002849
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002850 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002851}
2852
2853
Gilles Peskine20035e32018-02-03 22:44:14 +01002854
Gilles Peskine20035e32018-02-03 22:44:14 +01002855/****************************************************************/
2856/* Asymmetric cryptography */
2857/****************************************************************/
2858
Gilles Peskine2b450e32018-06-27 15:42:46 +02002859#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002860/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002861 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002862static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2863 size_t hash_length,
2864 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002865{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002866 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002867 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002868 *md_alg = mbedtls_md_get_type( md_info );
2869
2870 /* The Mbed TLS RSA module uses an unsigned int for hash length
2871 * parameters. Validate that it fits so that we don't risk an
2872 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002873#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002874 if( hash_length > UINT_MAX )
2875 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002876#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002877
2878#if defined(MBEDTLS_PKCS1_V15)
2879 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2880 * must be correct. */
2881 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2882 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002883 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002884 if( md_info == NULL )
2885 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002886 if( mbedtls_md_get_size( md_info ) != hash_length )
2887 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002888 }
2889#endif /* MBEDTLS_PKCS1_V15 */
2890
2891#if defined(MBEDTLS_PKCS1_V21)
2892 /* PSS requires a hash internally. */
2893 if( PSA_ALG_IS_RSA_PSS( alg ) )
2894 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002895 if( md_info == NULL )
2896 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002897 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002898#endif /* MBEDTLS_PKCS1_V21 */
2899
Gilles Peskine61b91d42018-06-08 16:09:36 +02002900 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002901}
2902
Gilles Peskine2b450e32018-06-27 15:42:46 +02002903static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2904 psa_algorithm_t alg,
2905 const uint8_t *hash,
2906 size_t hash_length,
2907 uint8_t *signature,
2908 size_t signature_size,
2909 size_t *signature_length )
2910{
2911 psa_status_t status;
2912 int ret;
2913 mbedtls_md_type_t md_alg;
2914
2915 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2916 if( status != PSA_SUCCESS )
2917 return( status );
2918
Gilles Peskine630a18a2018-06-29 17:49:35 +02002919 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002920 return( PSA_ERROR_BUFFER_TOO_SMALL );
2921
2922#if defined(MBEDTLS_PKCS1_V15)
2923 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2924 {
2925 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2926 MBEDTLS_MD_NONE );
2927 ret = mbedtls_rsa_pkcs1_sign( rsa,
2928 mbedtls_ctr_drbg_random,
2929 &global_data.ctr_drbg,
2930 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002931 md_alg,
2932 (unsigned int) hash_length,
2933 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002934 signature );
2935 }
2936 else
2937#endif /* MBEDTLS_PKCS1_V15 */
2938#if defined(MBEDTLS_PKCS1_V21)
2939 if( PSA_ALG_IS_RSA_PSS( alg ) )
2940 {
2941 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2942 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2943 mbedtls_ctr_drbg_random,
2944 &global_data.ctr_drbg,
2945 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002946 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002947 (unsigned int) hash_length,
2948 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002949 signature );
2950 }
2951 else
2952#endif /* MBEDTLS_PKCS1_V21 */
2953 {
2954 return( PSA_ERROR_INVALID_ARGUMENT );
2955 }
2956
2957 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002958 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002959 return( mbedtls_to_psa_error( ret ) );
2960}
2961
2962static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2963 psa_algorithm_t alg,
2964 const uint8_t *hash,
2965 size_t hash_length,
2966 const uint8_t *signature,
2967 size_t signature_length )
2968{
2969 psa_status_t status;
2970 int ret;
2971 mbedtls_md_type_t md_alg;
2972
2973 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2974 if( status != PSA_SUCCESS )
2975 return( status );
2976
Gilles Peskine630a18a2018-06-29 17:49:35 +02002977 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002978 return( PSA_ERROR_BUFFER_TOO_SMALL );
2979
2980#if defined(MBEDTLS_PKCS1_V15)
2981 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2982 {
2983 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2984 MBEDTLS_MD_NONE );
2985 ret = mbedtls_rsa_pkcs1_verify( rsa,
2986 mbedtls_ctr_drbg_random,
2987 &global_data.ctr_drbg,
2988 MBEDTLS_RSA_PUBLIC,
2989 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002990 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002991 hash,
2992 signature );
2993 }
2994 else
2995#endif /* MBEDTLS_PKCS1_V15 */
2996#if defined(MBEDTLS_PKCS1_V21)
2997 if( PSA_ALG_IS_RSA_PSS( alg ) )
2998 {
2999 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3000 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3001 mbedtls_ctr_drbg_random,
3002 &global_data.ctr_drbg,
3003 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003004 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003005 (unsigned int) hash_length,
3006 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003007 signature );
3008 }
3009 else
3010#endif /* MBEDTLS_PKCS1_V21 */
3011 {
3012 return( PSA_ERROR_INVALID_ARGUMENT );
3013 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003014
3015 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3016 * the rest of the signature is invalid". This has little use in
3017 * practice and PSA doesn't report this distinction. */
3018 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3019 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003020 return( mbedtls_to_psa_error( ret ) );
3021}
3022#endif /* MBEDTLS_RSA_C */
3023
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003024#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003025/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3026 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3027 * (even though these functions don't modify it). */
3028static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3029 psa_algorithm_t alg,
3030 const uint8_t *hash,
3031 size_t hash_length,
3032 uint8_t *signature,
3033 size_t signature_size,
3034 size_t *signature_length )
3035{
3036 int ret;
3037 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003038 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003039 mbedtls_mpi_init( &r );
3040 mbedtls_mpi_init( &s );
3041
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003042 if( signature_size < 2 * curve_bytes )
3043 {
3044 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3045 goto cleanup;
3046 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003047
Gilles Peskinea05219c2018-11-16 16:02:56 +01003048#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003049 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3050 {
3051 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3052 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3053 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3054 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3055 hash, hash_length,
3056 md_alg ) );
3057 }
3058 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003059#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003060 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003061 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003062 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3063 hash, hash_length,
3064 mbedtls_ctr_drbg_random,
3065 &global_data.ctr_drbg ) );
3066 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003067
3068 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3069 signature,
3070 curve_bytes ) );
3071 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3072 signature + curve_bytes,
3073 curve_bytes ) );
3074
3075cleanup:
3076 mbedtls_mpi_free( &r );
3077 mbedtls_mpi_free( &s );
3078 if( ret == 0 )
3079 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003080 return( mbedtls_to_psa_error( ret ) );
3081}
3082
3083static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3084 const uint8_t *hash,
3085 size_t hash_length,
3086 const uint8_t *signature,
3087 size_t signature_length )
3088{
3089 int ret;
3090 mbedtls_mpi r, s;
3091 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3092 mbedtls_mpi_init( &r );
3093 mbedtls_mpi_init( &s );
3094
3095 if( signature_length != 2 * curve_bytes )
3096 return( PSA_ERROR_INVALID_SIGNATURE );
3097
3098 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3099 signature,
3100 curve_bytes ) );
3101 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3102 signature + curve_bytes,
3103 curve_bytes ) );
3104
3105 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3106 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003107
3108cleanup:
3109 mbedtls_mpi_free( &r );
3110 mbedtls_mpi_free( &s );
3111 return( mbedtls_to_psa_error( ret ) );
3112}
3113#endif /* MBEDTLS_ECDSA_C */
3114
Gilles Peskinec5487a82018-12-03 18:08:14 +01003115psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003116 psa_algorithm_t alg,
3117 const uint8_t *hash,
3118 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003119 uint8_t *signature,
3120 size_t signature_size,
3121 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003122{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003123 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003124 psa_status_t status;
3125
3126 *signature_length = signature_size;
3127
Gilles Peskine28f8f302019-07-24 13:30:31 +02003128 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003129 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003130 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003131 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003132 {
3133 status = PSA_ERROR_INVALID_ARGUMENT;
3134 goto exit;
3135 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003136
Gilles Peskine20035e32018-02-03 22:44:14 +01003137#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003138 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003139 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003140 status = psa_rsa_sign( slot->data.rsa,
3141 alg,
3142 hash, hash_length,
3143 signature, signature_size,
3144 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003145 }
3146 else
3147#endif /* defined(MBEDTLS_RSA_C) */
3148#if defined(MBEDTLS_ECP_C)
3149 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3150 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003151#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003152 if(
3153#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3154 PSA_ALG_IS_ECDSA( alg )
3155#else
3156 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3157#endif
3158 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003159 status = psa_ecdsa_sign( slot->data.ecp,
3160 alg,
3161 hash, hash_length,
3162 signature, signature_size,
3163 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003164 else
3165#endif /* defined(MBEDTLS_ECDSA_C) */
3166 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003167 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003168 }
itayzafrir5c753392018-05-08 11:18:38 +03003169 }
3170 else
3171#endif /* defined(MBEDTLS_ECP_C) */
3172 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003173 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003174 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003175
3176exit:
3177 /* Fill the unused part of the output buffer (the whole buffer on error,
3178 * the trailing part on success) with something that isn't a valid mac
3179 * (barring an attack on the mac and deliberately-crafted input),
3180 * in case the caller doesn't check the return status properly. */
3181 if( status == PSA_SUCCESS )
3182 memset( signature + *signature_length, '!',
3183 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003184 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003185 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003186 /* If signature_size is 0 then we have nothing to do. We must not call
3187 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003188 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003189}
3190
Gilles Peskinec5487a82018-12-03 18:08:14 +01003191psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003192 psa_algorithm_t alg,
3193 const uint8_t *hash,
3194 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003195 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003196 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003197{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003198 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003199 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003200
Gilles Peskine28f8f302019-07-24 13:30:31 +02003201 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003202 if( status != PSA_SUCCESS )
3203 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003204
Gilles Peskine61b91d42018-06-08 16:09:36 +02003205#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003206 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003207 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003208 return( psa_rsa_verify( slot->data.rsa,
3209 alg,
3210 hash, hash_length,
3211 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003212 }
3213 else
3214#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003215#if defined(MBEDTLS_ECP_C)
3216 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3217 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003218#if defined(MBEDTLS_ECDSA_C)
3219 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003220 return( psa_ecdsa_verify( slot->data.ecp,
3221 hash, hash_length,
3222 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003223 else
3224#endif /* defined(MBEDTLS_ECDSA_C) */
3225 {
3226 return( PSA_ERROR_INVALID_ARGUMENT );
3227 }
itayzafrir5c753392018-05-08 11:18:38 +03003228 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003229 else
3230#endif /* defined(MBEDTLS_ECP_C) */
3231 {
3232 return( PSA_ERROR_NOT_SUPPORTED );
3233 }
3234}
3235
Gilles Peskine072ac562018-06-30 00:21:29 +02003236#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3237static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3238 mbedtls_rsa_context *rsa )
3239{
3240 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3241 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3242 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3243 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3244}
3245#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3246
Gilles Peskinec5487a82018-12-03 18:08:14 +01003247psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003248 psa_algorithm_t alg,
3249 const uint8_t *input,
3250 size_t input_length,
3251 const uint8_t *salt,
3252 size_t salt_length,
3253 uint8_t *output,
3254 size_t output_size,
3255 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003256{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003257 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003258 psa_status_t status;
3259
Darryl Green5cc689a2018-07-24 15:34:10 +01003260 (void) input;
3261 (void) input_length;
3262 (void) salt;
3263 (void) output;
3264 (void) output_size;
3265
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003266 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003267
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003268 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3269 return( PSA_ERROR_INVALID_ARGUMENT );
3270
Gilles Peskine28f8f302019-07-24 13:30:31 +02003271 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003272 if( status != PSA_SUCCESS )
3273 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003274 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003275 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003276 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003277
3278#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003279 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003280 {
3281 mbedtls_rsa_context *rsa = slot->data.rsa;
3282 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003283 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003284 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003286 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003287 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003288 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3289 mbedtls_ctr_drbg_random,
3290 &global_data.ctr_drbg,
3291 MBEDTLS_RSA_PUBLIC,
3292 input_length,
3293 input,
3294 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003295 }
3296 else
3297#endif /* MBEDTLS_PKCS1_V15 */
3298#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003299 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003300 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003301 psa_rsa_oaep_set_padding_mode( alg, rsa );
3302 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3303 mbedtls_ctr_drbg_random,
3304 &global_data.ctr_drbg,
3305 MBEDTLS_RSA_PUBLIC,
3306 salt, salt_length,
3307 input_length,
3308 input,
3309 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003310 }
3311 else
3312#endif /* MBEDTLS_PKCS1_V21 */
3313 {
3314 return( PSA_ERROR_INVALID_ARGUMENT );
3315 }
3316 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003317 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003318 return( mbedtls_to_psa_error( ret ) );
3319 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003320 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003321#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003322 {
3323 return( PSA_ERROR_NOT_SUPPORTED );
3324 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003325}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003326
Gilles Peskinec5487a82018-12-03 18:08:14 +01003327psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003328 psa_algorithm_t alg,
3329 const uint8_t *input,
3330 size_t input_length,
3331 const uint8_t *salt,
3332 size_t salt_length,
3333 uint8_t *output,
3334 size_t output_size,
3335 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003336{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003337 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003338 psa_status_t status;
3339
Darryl Green5cc689a2018-07-24 15:34:10 +01003340 (void) input;
3341 (void) input_length;
3342 (void) salt;
3343 (void) output;
3344 (void) output_size;
3345
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003346 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003347
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003348 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3349 return( PSA_ERROR_INVALID_ARGUMENT );
3350
Gilles Peskine28f8f302019-07-24 13:30:31 +02003351 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003352 if( status != PSA_SUCCESS )
3353 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003354 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003355 return( PSA_ERROR_INVALID_ARGUMENT );
3356
3357#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003358 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003359 {
3360 mbedtls_rsa_context *rsa = slot->data.rsa;
3361 int ret;
3362
Gilles Peskine630a18a2018-06-29 17:49:35 +02003363 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003364 return( PSA_ERROR_INVALID_ARGUMENT );
3365
3366#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003367 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003368 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003369 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3370 mbedtls_ctr_drbg_random,
3371 &global_data.ctr_drbg,
3372 MBEDTLS_RSA_PRIVATE,
3373 output_length,
3374 input,
3375 output,
3376 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003377 }
3378 else
3379#endif /* MBEDTLS_PKCS1_V15 */
3380#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003381 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003382 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003383 psa_rsa_oaep_set_padding_mode( alg, rsa );
3384 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3385 mbedtls_ctr_drbg_random,
3386 &global_data.ctr_drbg,
3387 MBEDTLS_RSA_PRIVATE,
3388 salt, salt_length,
3389 output_length,
3390 input,
3391 output,
3392 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003393 }
3394 else
3395#endif /* MBEDTLS_PKCS1_V21 */
3396 {
3397 return( PSA_ERROR_INVALID_ARGUMENT );
3398 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003399
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003400 return( mbedtls_to_psa_error( ret ) );
3401 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003402 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003403#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003404 {
3405 return( PSA_ERROR_NOT_SUPPORTED );
3406 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003407}
Gilles Peskine20035e32018-02-03 22:44:14 +01003408
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003409
3410
mohammad1603503973b2018-03-12 15:59:30 +02003411/****************************************************************/
3412/* Symmetric cryptography */
3413/****************************************************************/
3414
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003415/* Initialize the cipher operation structure. Once this function has been
3416 * called, psa_cipher_abort can run and will do the right thing. */
3417static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3418 psa_algorithm_t alg )
3419{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003420 if( ! PSA_ALG_IS_CIPHER( alg ) )
3421 {
3422 memset( operation, 0, sizeof( *operation ) );
3423 return( PSA_ERROR_INVALID_ARGUMENT );
3424 }
3425
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003426 operation->alg = alg;
3427 operation->key_set = 0;
3428 operation->iv_set = 0;
3429 operation->iv_required = 1;
3430 operation->iv_size = 0;
3431 operation->block_size = 0;
3432 mbedtls_cipher_init( &operation->ctx.cipher );
3433 return( PSA_SUCCESS );
3434}
3435
Gilles Peskinee553c652018-06-04 16:22:46 +02003436static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003437 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003438 psa_algorithm_t alg,
3439 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003440{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003441 int ret = 0;
3442 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003443 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003444 size_t key_bits;
3445 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003446 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3447 PSA_KEY_USAGE_ENCRYPT :
3448 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003449
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003450 /* A context must be freshly initialized before it can be set up. */
3451 if( operation->alg != 0 )
3452 {
3453 return( PSA_ERROR_BAD_STATE );
3454 }
3455
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003456 status = psa_cipher_init( operation, alg );
3457 if( status != PSA_SUCCESS )
3458 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003459
Gilles Peskine28f8f302019-07-24 13:30:31 +02003460 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003461 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003462 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003463 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003464
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003465 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003466 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003467 {
3468 status = PSA_ERROR_NOT_SUPPORTED;
3469 goto exit;
3470 }
mohammad1603503973b2018-03-12 15:59:30 +02003471
mohammad1603503973b2018-03-12 15:59:30 +02003472 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003473 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003474 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003475
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003476#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003477 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003478 {
3479 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3480 unsigned char keys[24];
3481 memcpy( keys, slot->data.raw.data, 16 );
3482 memcpy( keys + 16, slot->data.raw.data, 8 );
3483 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3484 keys,
3485 192, cipher_operation );
3486 }
3487 else
3488#endif
3489 {
3490 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3491 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003492 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003493 }
Moran Peker41deec42018-04-04 15:43:05 +03003494 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003495 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003496
mohammad16038481e742018-03-18 13:57:31 +02003497#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003498 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003499 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003500 case PSA_ALG_CBC_NO_PADDING:
3501 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3502 MBEDTLS_PADDING_NONE );
3503 break;
3504 case PSA_ALG_CBC_PKCS7:
3505 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3506 MBEDTLS_PADDING_PKCS7 );
3507 break;
3508 default:
3509 /* The algorithm doesn't involve padding. */
3510 ret = 0;
3511 break;
3512 }
3513 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003514 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003515#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3516
mohammad1603503973b2018-03-12 15:59:30 +02003517 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003518 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3519 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3520 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003521 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003522 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003523 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003524#if defined(MBEDTLS_CHACHA20_C)
3525 else
3526 if( alg == PSA_ALG_CHACHA20 )
3527 operation->iv_size = 12;
3528#endif
mohammad1603503973b2018-03-12 15:59:30 +02003529
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003530exit:
3531 if( status == 0 )
3532 status = mbedtls_to_psa_error( ret );
3533 if( status != 0 )
3534 psa_cipher_abort( operation );
3535 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003536}
3537
Gilles Peskinefe119512018-07-08 21:39:34 +02003538psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003539 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003540 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003541{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003542 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003543}
3544
Gilles Peskinefe119512018-07-08 21:39:34 +02003545psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003546 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003547 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003548{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003549 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003550}
3551
Gilles Peskinefe119512018-07-08 21:39:34 +02003552psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3553 unsigned char *iv,
3554 size_t iv_size,
3555 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003556{
itayzafrir534bd7c2018-08-02 13:56:32 +03003557 psa_status_t status;
3558 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003559 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003560 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003561 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003562 }
Moran Peker41deec42018-04-04 15:43:05 +03003563 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003564 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003565 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003566 goto exit;
3567 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003568 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3569 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003570 if( ret != 0 )
3571 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003572 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003573 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003574 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003575
mohammad16038481e742018-03-18 13:57:31 +02003576 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003577 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003578
Moran Peker395db872018-05-31 14:07:14 +03003579exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003580 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003581 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003582 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003583}
3584
Gilles Peskinefe119512018-07-08 21:39:34 +02003585psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3586 const unsigned char *iv,
3587 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003588{
itayzafrir534bd7c2018-08-02 13:56:32 +03003589 psa_status_t status;
3590 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003591 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003592 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003593 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003594 }
Moran Pekera28258c2018-05-29 16:25:04 +03003595 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003596 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003597 status = PSA_ERROR_INVALID_ARGUMENT;
3598 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003599 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003600 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3601 status = mbedtls_to_psa_error( ret );
3602exit:
3603 if( status == PSA_SUCCESS )
3604 operation->iv_set = 1;
3605 else
mohammad1603503973b2018-03-12 15:59:30 +02003606 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003607 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003608}
3609
Gilles Peskinee553c652018-06-04 16:22:46 +02003610psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3611 const uint8_t *input,
3612 size_t input_length,
3613 unsigned char *output,
3614 size_t output_size,
3615 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003616{
itayzafrir534bd7c2018-08-02 13:56:32 +03003617 psa_status_t status;
3618 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003619 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003620
3621 if( operation->alg == 0 )
3622 {
3623 return( PSA_ERROR_BAD_STATE );
3624 }
3625
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003626 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003627 {
3628 /* Take the unprocessed partial block left over from previous
3629 * update calls, if any, plus the input to this call. Remove
3630 * the last partial block, if any. You get the data that will be
3631 * output in this call. */
3632 expected_output_size =
3633 ( operation->ctx.cipher.unprocessed_len + input_length )
3634 / operation->block_size * operation->block_size;
3635 }
3636 else
3637 {
3638 expected_output_size = input_length;
3639 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003640
Gilles Peskine89d789c2018-06-04 17:17:16 +02003641 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003642 {
3643 status = PSA_ERROR_BUFFER_TOO_SMALL;
3644 goto exit;
3645 }
mohammad160382759612018-03-12 18:16:40 +02003646
mohammad1603503973b2018-03-12 15:59:30 +02003647 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003648 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003649 status = mbedtls_to_psa_error( ret );
3650exit:
3651 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003652 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003653 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003654}
3655
Gilles Peskinee553c652018-06-04 16:22:46 +02003656psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3657 uint8_t *output,
3658 size_t output_size,
3659 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003660{
David Saadab4ecc272019-02-14 13:48:10 +02003661 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003662 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003663 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003664
mohammad1603503973b2018-03-12 15:59:30 +02003665 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003666 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003667 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003668 }
3669 if( operation->iv_required && ! operation->iv_set )
3670 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003671 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003672 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003673
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003674 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003675 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3676 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003677 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003678 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003679 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003680 }
3681
Janos Follath315b51c2018-07-09 16:04:51 +01003682 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3683 temp_output_buffer,
3684 output_length );
3685 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003686 {
Janos Follath315b51c2018-07-09 16:04:51 +01003687 status = mbedtls_to_psa_error( cipher_ret );
3688 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003689 }
Janos Follath315b51c2018-07-09 16:04:51 +01003690
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003691 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003692 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003693 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003694 memcpy( output, temp_output_buffer, *output_length );
3695 else
3696 {
Janos Follath315b51c2018-07-09 16:04:51 +01003697 status = PSA_ERROR_BUFFER_TOO_SMALL;
3698 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003699 }
mohammad1603503973b2018-03-12 15:59:30 +02003700
Gilles Peskine3f108122018-12-07 18:14:53 +01003701 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003702 status = psa_cipher_abort( operation );
3703
3704 return( status );
3705
3706error:
3707
3708 *output_length = 0;
3709
Gilles Peskine3f108122018-12-07 18:14:53 +01003710 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003711 (void) psa_cipher_abort( operation );
3712
3713 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003714}
3715
Gilles Peskinee553c652018-06-04 16:22:46 +02003716psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3717{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003718 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003719 {
3720 /* The object has (apparently) been initialized but it is not
3721 * in use. It's ok to call abort on such an object, and there's
3722 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003723 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003724 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003725
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003726 /* Sanity check (shouldn't happen: operation->alg should
3727 * always have been initialized to a valid value). */
3728 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3729 return( PSA_ERROR_BAD_STATE );
3730
mohammad1603503973b2018-03-12 15:59:30 +02003731 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003732
Moran Peker41deec42018-04-04 15:43:05 +03003733 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003734 operation->key_set = 0;
3735 operation->iv_set = 0;
3736 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003737 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003738 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003739
Moran Peker395db872018-05-31 14:07:14 +03003740 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003741}
3742
Gilles Peskinea0655c32018-04-30 17:06:50 +02003743
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003744
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003745
mohammad16035955c982018-04-26 00:53:03 +03003746/****************************************************************/
3747/* AEAD */
3748/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003749
Gilles Peskineedf9a652018-08-17 18:11:56 +02003750typedef struct
3751{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003752 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003753 const mbedtls_cipher_info_t *cipher_info;
3754 union
3755 {
3756#if defined(MBEDTLS_CCM_C)
3757 mbedtls_ccm_context ccm;
3758#endif /* MBEDTLS_CCM_C */
3759#if defined(MBEDTLS_GCM_C)
3760 mbedtls_gcm_context gcm;
3761#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003762#if defined(MBEDTLS_CHACHAPOLY_C)
3763 mbedtls_chachapoly_context chachapoly;
3764#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003765 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003766 psa_algorithm_t core_alg;
3767 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003768 uint8_t tag_length;
3769} aead_operation_t;
3770
Gilles Peskine30a9e412019-01-14 18:36:12 +01003771static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003772{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003773 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003774 {
3775#if defined(MBEDTLS_CCM_C)
3776 case PSA_ALG_CCM:
3777 mbedtls_ccm_free( &operation->ctx.ccm );
3778 break;
3779#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003780#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003781 case PSA_ALG_GCM:
3782 mbedtls_gcm_free( &operation->ctx.gcm );
3783 break;
3784#endif /* MBEDTLS_GCM_C */
3785 }
3786}
3787
3788static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003789 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003790 psa_key_usage_t usage,
3791 psa_algorithm_t alg )
3792{
3793 psa_status_t status;
3794 size_t key_bits;
3795 mbedtls_cipher_id_t cipher_id;
3796
Gilles Peskine28f8f302019-07-24 13:30:31 +02003797 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003798 if( status != PSA_SUCCESS )
3799 return( status );
3800
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003801 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003802
3803 operation->cipher_info =
3804 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3805 &cipher_id );
3806 if( operation->cipher_info == NULL )
3807 return( PSA_ERROR_NOT_SUPPORTED );
3808
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003809 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003810 {
3811#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003812 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3813 operation->core_alg = PSA_ALG_CCM;
3814 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003815 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3816 * The call to mbedtls_ccm_encrypt_and_tag or
3817 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003818 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3819 return( PSA_ERROR_INVALID_ARGUMENT );
3820 mbedtls_ccm_init( &operation->ctx.ccm );
3821 status = mbedtls_to_psa_error(
3822 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3823 operation->slot->data.raw.data,
3824 (unsigned int) key_bits ) );
3825 if( status != 0 )
3826 goto cleanup;
3827 break;
3828#endif /* MBEDTLS_CCM_C */
3829
3830#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003831 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3832 operation->core_alg = PSA_ALG_GCM;
3833 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003834 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3835 * The call to mbedtls_gcm_crypt_and_tag or
3836 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003837 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3838 return( PSA_ERROR_INVALID_ARGUMENT );
3839 mbedtls_gcm_init( &operation->ctx.gcm );
3840 status = mbedtls_to_psa_error(
3841 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3842 operation->slot->data.raw.data,
3843 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003844 if( status != 0 )
3845 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003846 break;
3847#endif /* MBEDTLS_GCM_C */
3848
Gilles Peskine26869f22019-05-06 15:25:00 +02003849#if defined(MBEDTLS_CHACHAPOLY_C)
3850 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3851 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3852 operation->full_tag_length = 16;
3853 /* We only support the default tag length. */
3854 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3855 return( PSA_ERROR_NOT_SUPPORTED );
3856 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3857 status = mbedtls_to_psa_error(
3858 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3859 operation->slot->data.raw.data ) );
3860 if( status != 0 )
3861 goto cleanup;
3862 break;
3863#endif /* MBEDTLS_CHACHAPOLY_C */
3864
Gilles Peskineedf9a652018-08-17 18:11:56 +02003865 default:
3866 return( PSA_ERROR_NOT_SUPPORTED );
3867 }
3868
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003869 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3870 {
3871 status = PSA_ERROR_INVALID_ARGUMENT;
3872 goto cleanup;
3873 }
3874 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003875
Gilles Peskineedf9a652018-08-17 18:11:56 +02003876 return( PSA_SUCCESS );
3877
3878cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003879 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003880 return( status );
3881}
3882
Gilles Peskinec5487a82018-12-03 18:08:14 +01003883psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003884 psa_algorithm_t alg,
3885 const uint8_t *nonce,
3886 size_t nonce_length,
3887 const uint8_t *additional_data,
3888 size_t additional_data_length,
3889 const uint8_t *plaintext,
3890 size_t plaintext_length,
3891 uint8_t *ciphertext,
3892 size_t ciphertext_size,
3893 size_t *ciphertext_length )
3894{
mohammad16035955c982018-04-26 00:53:03 +03003895 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003896 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003897 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003898
mohammad1603f08a5502018-06-03 15:05:47 +03003899 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003900
Gilles Peskinec5487a82018-12-03 18:08:14 +01003901 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003902 if( status != PSA_SUCCESS )
3903 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003904
Gilles Peskineedf9a652018-08-17 18:11:56 +02003905 /* For all currently supported modes, the tag is at the end of the
3906 * ciphertext. */
3907 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3908 {
3909 status = PSA_ERROR_BUFFER_TOO_SMALL;
3910 goto exit;
3911 }
3912 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003913
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003914#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003915 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003916 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003917 status = mbedtls_to_psa_error(
3918 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3919 MBEDTLS_GCM_ENCRYPT,
3920 plaintext_length,
3921 nonce, nonce_length,
3922 additional_data, additional_data_length,
3923 plaintext, ciphertext,
3924 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003925 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003926 else
3927#endif /* MBEDTLS_GCM_C */
3928#if defined(MBEDTLS_CCM_C)
3929 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003930 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003931 status = mbedtls_to_psa_error(
3932 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3933 plaintext_length,
3934 nonce, nonce_length,
3935 additional_data,
3936 additional_data_length,
3937 plaintext, ciphertext,
3938 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003939 }
mohammad16035c8845f2018-05-09 05:40:09 -07003940 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003941#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003942#if defined(MBEDTLS_CHACHAPOLY_C)
3943 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3944 {
3945 if( nonce_length != 12 || operation.tag_length != 16 )
3946 {
3947 status = PSA_ERROR_NOT_SUPPORTED;
3948 goto exit;
3949 }
3950 status = mbedtls_to_psa_error(
3951 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3952 plaintext_length,
3953 nonce,
3954 additional_data,
3955 additional_data_length,
3956 plaintext,
3957 ciphertext,
3958 tag ) );
3959 }
3960 else
3961#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003962 {
mohammad1603554faad2018-06-03 15:07:38 +03003963 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003964 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003965
Gilles Peskineedf9a652018-08-17 18:11:56 +02003966 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3967 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003968
Gilles Peskineedf9a652018-08-17 18:11:56 +02003969exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003970 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003971 if( status == PSA_SUCCESS )
3972 *ciphertext_length = plaintext_length + operation.tag_length;
3973 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003974}
3975
Gilles Peskineee652a32018-06-01 19:23:52 +02003976/* Locate the tag in a ciphertext buffer containing the encrypted data
3977 * followed by the tag. Return the length of the part preceding the tag in
3978 * *plaintext_length. This is the size of the plaintext in modes where
3979 * the encrypted data has the same size as the plaintext, such as
3980 * CCM and GCM. */
3981static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3982 const uint8_t *ciphertext,
3983 size_t ciphertext_length,
3984 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003985 const uint8_t **p_tag )
3986{
3987 size_t payload_length;
3988 if( tag_length > ciphertext_length )
3989 return( PSA_ERROR_INVALID_ARGUMENT );
3990 payload_length = ciphertext_length - tag_length;
3991 if( payload_length > plaintext_size )
3992 return( PSA_ERROR_BUFFER_TOO_SMALL );
3993 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003994 return( PSA_SUCCESS );
3995}
3996
Gilles Peskinec5487a82018-12-03 18:08:14 +01003997psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003998 psa_algorithm_t alg,
3999 const uint8_t *nonce,
4000 size_t nonce_length,
4001 const uint8_t *additional_data,
4002 size_t additional_data_length,
4003 const uint8_t *ciphertext,
4004 size_t ciphertext_length,
4005 uint8_t *plaintext,
4006 size_t plaintext_size,
4007 size_t *plaintext_length )
4008{
mohammad16035955c982018-04-26 00:53:03 +03004009 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004010 aead_operation_t operation;
4011 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004012
Gilles Peskineee652a32018-06-01 19:23:52 +02004013 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004014
Gilles Peskinec5487a82018-12-03 18:08:14 +01004015 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004016 if( status != PSA_SUCCESS )
4017 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004018
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004019 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4020 ciphertext, ciphertext_length,
4021 plaintext_size, &tag );
4022 if( status != PSA_SUCCESS )
4023 goto exit;
4024
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004025#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004026 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004027 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004028 status = mbedtls_to_psa_error(
4029 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4030 ciphertext_length - operation.tag_length,
4031 nonce, nonce_length,
4032 additional_data,
4033 additional_data_length,
4034 tag, operation.tag_length,
4035 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004036 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004037 else
4038#endif /* MBEDTLS_GCM_C */
4039#if defined(MBEDTLS_CCM_C)
4040 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004041 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004042 status = mbedtls_to_psa_error(
4043 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4044 ciphertext_length - operation.tag_length,
4045 nonce, nonce_length,
4046 additional_data,
4047 additional_data_length,
4048 ciphertext, plaintext,
4049 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004050 }
mohammad160339574652018-06-01 04:39:53 -07004051 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004052#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004053#if defined(MBEDTLS_CHACHAPOLY_C)
4054 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4055 {
4056 if( nonce_length != 12 || operation.tag_length != 16 )
4057 {
4058 status = PSA_ERROR_NOT_SUPPORTED;
4059 goto exit;
4060 }
4061 status = mbedtls_to_psa_error(
4062 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4063 ciphertext_length - operation.tag_length,
4064 nonce,
4065 additional_data,
4066 additional_data_length,
4067 tag,
4068 ciphertext,
4069 plaintext ) );
4070 }
4071 else
4072#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004073 {
mohammad1603554faad2018-06-03 15:07:38 +03004074 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004075 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004076
Gilles Peskineedf9a652018-08-17 18:11:56 +02004077 if( status != PSA_SUCCESS && plaintext_size != 0 )
4078 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004079
Gilles Peskineedf9a652018-08-17 18:11:56 +02004080exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004081 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004082 if( status == PSA_SUCCESS )
4083 *plaintext_length = ciphertext_length - operation.tag_length;
4084 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004085}
4086
Gilles Peskinea0655c32018-04-30 17:06:50 +02004087
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004088
Gilles Peskine20035e32018-02-03 22:44:14 +01004089/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004090/* Generators */
4091/****************************************************************/
4092
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004093#define HKDF_STATE_INIT 0 /* no input yet */
4094#define HKDF_STATE_STARTED 1 /* got salt */
4095#define HKDF_STATE_KEYED 2 /* got key */
4096#define HKDF_STATE_OUTPUT 3 /* output started */
4097
Gilles Peskinecbe66502019-05-16 16:59:18 +02004098static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004099 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004100{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004101 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4102 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004103 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004104 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004105}
4106
4107
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004108psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004109{
4110 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004111 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004112 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004113 {
4114 /* The object has (apparently) been initialized but it is not
4115 * in use. It's ok to call abort on such an object, and there's
4116 * nothing to do. */
4117 }
4118 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004119#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004120 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004121 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004122 mbedtls_free( operation->ctx.hkdf.info );
4123 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004124 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004125 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004126 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004127 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004128 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004129#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004130 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004131 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004132 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4133 operation->ctx.tls12_prf.key_len );
4134 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004135 }
Hanno Becker580fba12018-11-13 20:50:45 +00004136
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004137 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004138 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004139 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4140 operation->ctx.tls12_prf.Ai_with_seed_len );
4141 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004142 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004143#else
4144 if( operation->ctx.tls12_prf.seed != NULL )
4145 {
4146 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4147 operation->ctx.tls12_prf.seed_length );
4148 mbedtls_free( operation->ctx.tls12_prf.seed );
4149 }
4150
4151 if( operation->ctx.tls12_prf.label != NULL )
4152 {
4153 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4154 operation->ctx.tls12_prf.label_length );
4155 mbedtls_free( operation->ctx.tls12_prf.label );
4156 }
4157
4158 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4159
4160 /* We leave the fields Ai and output_block to be erased safely by the
4161 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004162#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004163 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004164 else
4165#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004166 {
4167 status = PSA_ERROR_BAD_STATE;
4168 }
Janos Follath083036a2019-06-11 10:22:26 +01004169 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004170 return( status );
4171}
4172
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004173psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004174 size_t *capacity)
4175{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004176 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004177 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004178 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004179 return PSA_ERROR_BAD_STATE;
4180 }
4181
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004182 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004183 return( PSA_SUCCESS );
4184}
4185
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004186psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004187 size_t capacity )
4188{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004189 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004190 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004191 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004192 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004193 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004194 return( PSA_SUCCESS );
4195}
4196
Gilles Peskinebef7f142018-07-12 17:22:21 +02004197#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004198/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004199 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004200static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004201 psa_algorithm_t hash_alg,
4202 uint8_t *output,
4203 size_t output_length )
4204{
4205 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4206 psa_status_t status;
4207
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004208 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4209 return( PSA_ERROR_BAD_STATE );
4210 hkdf->state = HKDF_STATE_OUTPUT;
4211
Gilles Peskinebef7f142018-07-12 17:22:21 +02004212 while( output_length != 0 )
4213 {
4214 /* Copy what remains of the current block */
4215 uint8_t n = hash_length - hkdf->offset_in_block;
4216 if( n > output_length )
4217 n = (uint8_t) output_length;
4218 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4219 output += n;
4220 output_length -= n;
4221 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004222 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004223 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004224 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004225 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004226 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004227 * object was corrupted or if this function is called directly
4228 * inside the library. */
4229 if( hkdf->block_number == 0xff )
4230 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004231
4232 /* We need a new block */
4233 ++hkdf->block_number;
4234 hkdf->offset_in_block = 0;
4235 status = psa_hmac_setup_internal( &hkdf->hmac,
4236 hkdf->prk, hash_length,
4237 hash_alg );
4238 if( status != PSA_SUCCESS )
4239 return( status );
4240 if( hkdf->block_number != 1 )
4241 {
4242 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4243 hkdf->output_block,
4244 hash_length );
4245 if( status != PSA_SUCCESS )
4246 return( status );
4247 }
4248 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4249 hkdf->info,
4250 hkdf->info_length );
4251 if( status != PSA_SUCCESS )
4252 return( status );
4253 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4254 &hkdf->block_number, 1 );
4255 if( status != PSA_SUCCESS )
4256 return( status );
4257 status = psa_hmac_finish_internal( &hkdf->hmac,
4258 hkdf->output_block,
4259 sizeof( hkdf->output_block ) );
4260 if( status != PSA_SUCCESS )
4261 return( status );
4262 }
4263
4264 return( PSA_SUCCESS );
4265}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004266
Janos Follath999f6482019-06-11 12:04:10 +01004267#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004268static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4269 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004270 psa_algorithm_t alg )
4271{
4272 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4273 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4274 psa_hmac_internal_data hmac;
4275 psa_status_t status, cleanup_status;
4276
Hanno Becker3b339e22018-11-13 20:56:14 +00004277 unsigned char *Ai;
4278 size_t Ai_len;
4279
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004280 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004281 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004282 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004283 * object was corrupted or if this function is called directly
4284 * inside the library. */
4285 if( tls12_prf->block_number == 0xff )
4286 return( PSA_ERROR_BAD_STATE );
4287
4288 /* We need a new block */
4289 ++tls12_prf->block_number;
4290 tls12_prf->offset_in_block = 0;
4291
4292 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4293 *
4294 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4295 *
4296 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4297 * HMAC_hash(secret, A(2) + seed) +
4298 * HMAC_hash(secret, A(3) + seed) + ...
4299 *
4300 * A(0) = seed
4301 * A(i) = HMAC_hash( secret, A(i-1) )
4302 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004303 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004304 * `HMAC_hash(secret, A(i) + seed)` from which the output
4305 * is currently extracted as `output_block`, while
4306 * `A(i) + seed` is stored in `Ai_with_seed`.
4307 *
4308 * Generating a new block means recalculating `Ai_with_seed`
4309 * from the A(i)-part of it, and afterwards recalculating
4310 * `output_block`.
4311 *
4312 * A(0) is computed at setup time.
4313 *
4314 */
4315
4316 psa_hmac_init_internal( &hmac );
4317
4318 /* We must distinguish the calculation of A(1) from those
4319 * of A(2) and higher, because A(0)=seed has a different
4320 * length than the other A(i). */
4321 if( tls12_prf->block_number == 1 )
4322 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004323 Ai = tls12_prf->Ai_with_seed + hash_length;
4324 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004325 }
4326 else
4327 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004328 Ai = tls12_prf->Ai_with_seed;
4329 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004330 }
4331
Hanno Becker3b339e22018-11-13 20:56:14 +00004332 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4333 status = psa_hmac_setup_internal( &hmac,
4334 tls12_prf->key,
4335 tls12_prf->key_len,
4336 hash_alg );
4337 if( status != PSA_SUCCESS )
4338 goto cleanup;
4339
4340 status = psa_hash_update( &hmac.hash_ctx,
4341 Ai, Ai_len );
4342 if( status != PSA_SUCCESS )
4343 goto cleanup;
4344
4345 status = psa_hmac_finish_internal( &hmac,
4346 tls12_prf->Ai_with_seed,
4347 hash_length );
4348 if( status != PSA_SUCCESS )
4349 goto cleanup;
4350
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004351 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4352 status = psa_hmac_setup_internal( &hmac,
4353 tls12_prf->key,
4354 tls12_prf->key_len,
4355 hash_alg );
4356 if( status != PSA_SUCCESS )
4357 goto cleanup;
4358
4359 status = psa_hash_update( &hmac.hash_ctx,
4360 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004361 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004362 if( status != PSA_SUCCESS )
4363 goto cleanup;
4364
4365 status = psa_hmac_finish_internal( &hmac,
4366 tls12_prf->output_block,
4367 hash_length );
4368 if( status != PSA_SUCCESS )
4369 goto cleanup;
4370
4371cleanup:
4372
4373 cleanup_status = psa_hmac_abort_internal( &hmac );
4374 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4375 status = cleanup_status;
4376
4377 return( status );
4378}
Janos Follath7742fee2019-06-17 12:58:10 +01004379#else
4380static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4381 psa_tls12_prf_key_derivation_t *tls12_prf,
4382 psa_algorithm_t alg )
4383{
4384 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4385 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004386 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4387 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004388
Janos Follath7742fee2019-06-17 12:58:10 +01004389 /* We can't be wanting more output after block 0xff, otherwise
4390 * the capacity check in psa_key_derivation_output_bytes() would have
4391 * prevented this call. It could happen only if the operation
4392 * object was corrupted or if this function is called directly
4393 * inside the library. */
4394 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004395 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004396
4397 /* We need a new block */
4398 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004399 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004400
4401 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4402 *
4403 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4404 *
4405 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4406 * HMAC_hash(secret, A(2) + seed) +
4407 * HMAC_hash(secret, A(3) + seed) + ...
4408 *
4409 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004410 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004411 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004412 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004413 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004414 * is currently extracted as `output_block` and where i is
4415 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004416 */
4417
Janos Follathea29bfb2019-06-19 12:21:20 +01004418 /* Save the hash context before using it, to preserve the hash state with
4419 * only the inner padding in it. We need this, because inner padding depends
4420 * on the key (secret in the RFC's terminology). */
4421 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4422 if( status != PSA_SUCCESS )
4423 goto cleanup;
4424
4425 /* Calculate A(i) where i = tls12_prf->block_number. */
4426 if( tls12_prf->block_number == 1 )
4427 {
4428 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4429 * the variable seed and in this instance means it in the context of the
4430 * P_hash function, where seed = label + seed.) */
4431 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4432 tls12_prf->label, tls12_prf->label_length );
4433 if( status != PSA_SUCCESS )
4434 goto cleanup;
4435 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4436 tls12_prf->seed, tls12_prf->seed_length );
4437 if( status != PSA_SUCCESS )
4438 goto cleanup;
4439 }
4440 else
4441 {
4442 /* A(i) = HMAC_hash(secret, A(i-1)) */
4443 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4444 tls12_prf->Ai, hash_length );
4445 if( status != PSA_SUCCESS )
4446 goto cleanup;
4447 }
4448
4449 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4450 tls12_prf->Ai, hash_length );
4451 if( status != PSA_SUCCESS )
4452 goto cleanup;
4453 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4454 if( status != PSA_SUCCESS )
4455 goto cleanup;
4456
4457 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4458 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4459 tls12_prf->Ai, hash_length );
4460 if( status != PSA_SUCCESS )
4461 goto cleanup;
4462 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4463 tls12_prf->label, tls12_prf->label_length );
4464 if( status != PSA_SUCCESS )
4465 goto cleanup;
4466 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4467 tls12_prf->seed, tls12_prf->seed_length );
4468 if( status != PSA_SUCCESS )
4469 goto cleanup;
4470 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4471 tls12_prf->output_block, hash_length );
4472 if( status != PSA_SUCCESS )
4473 goto cleanup;
4474 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4475 if( status != PSA_SUCCESS )
4476 goto cleanup;
4477
Janos Follath7742fee2019-06-17 12:58:10 +01004478
4479cleanup:
4480
Janos Follathea29bfb2019-06-19 12:21:20 +01004481 cleanup_status = psa_hash_abort( &backup );
4482 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4483 status = cleanup_status;
4484
4485 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004486}
Janos Follath999f6482019-06-11 12:04:10 +01004487#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004488
Janos Follath999f6482019-06-11 12:04:10 +01004489#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004491 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004492static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004493 psa_tls12_prf_key_derivation_t *tls12_prf,
4494 psa_algorithm_t alg,
4495 uint8_t *output,
4496 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004497{
4498 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4499 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4500 psa_status_t status;
4501
4502 while( output_length != 0 )
4503 {
4504 /* Copy what remains of the current block */
4505 uint8_t n = hash_length - tls12_prf->offset_in_block;
4506
4507 /* Check if we have fully processed the current block. */
4508 if( n == 0 )
4509 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004510 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004511 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004512 if( status != PSA_SUCCESS )
4513 return( status );
4514
4515 continue;
4516 }
4517
4518 if( n > output_length )
4519 n = (uint8_t) output_length;
4520 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4521 n );
4522 output += n;
4523 output_length -= n;
4524 tls12_prf->offset_in_block += n;
4525 }
4526
4527 return( PSA_SUCCESS );
4528}
Janos Follath844eb0e2019-06-19 12:10:49 +01004529#else
4530static psa_status_t psa_key_derivation_tls12_prf_read(
4531 psa_tls12_prf_key_derivation_t *tls12_prf,
4532 psa_algorithm_t alg,
4533 uint8_t *output,
4534 size_t output_length )
4535{
4536 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4537 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4538 psa_status_t status;
4539 uint8_t offset, length;
4540
4541 while( output_length != 0 )
4542 {
4543 /* Check if we have fully processed the current block. */
4544 if( tls12_prf->left_in_block == 0 )
4545 {
4546 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4547 alg );
4548 if( status != PSA_SUCCESS )
4549 return( status );
4550
4551 continue;
4552 }
4553
4554 if( tls12_prf->left_in_block > output_length )
4555 length = (uint8_t) output_length;
4556 else
4557 length = tls12_prf->left_in_block;
4558
4559 offset = hash_length - tls12_prf->left_in_block;
4560 memcpy( output, tls12_prf->output_block + offset, length );
4561 output += length;
4562 output_length -= length;
4563 tls12_prf->left_in_block -= length;
4564 }
4565
4566 return( PSA_SUCCESS );
4567}
Janos Follath999f6482019-06-11 12:04:10 +01004568#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004569#endif /* MBEDTLS_MD_C */
4570
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004571psa_status_t psa_key_derivation_output_bytes(
4572 psa_key_derivation_operation_t *operation,
4573 uint8_t *output,
4574 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004575{
4576 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004577 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004578
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004579 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004580 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004581 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004582 return PSA_ERROR_BAD_STATE;
4583 }
4584
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004585 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004586 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004587 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004588 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004589 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004590 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004591 goto exit;
4592 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004593 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004594 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004595 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004596 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004597 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4598 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004599 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004600 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004601 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004602 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004603 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004604
Gilles Peskinebef7f142018-07-12 17:22:21 +02004605#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004606 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004607 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004608 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004609 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004610 output, output_length );
4611 }
Janos Follathadbec812019-06-14 11:05:39 +01004612 else
Janos Follathadbec812019-06-14 11:05:39 +01004613 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004614 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004615 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004616 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004617 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004618 output_length );
4619 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004620 else
4621#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004622 {
4623 return( PSA_ERROR_BAD_STATE );
4624 }
4625
4626exit:
4627 if( status != PSA_SUCCESS )
4628 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004629 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004630 * This allows us to differentiate between exhausted operations and
4631 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4632 * operations. */
4633 psa_algorithm_t alg = operation->alg;
4634 psa_key_derivation_abort( operation );
4635 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004636 memset( output, '!', output_length );
4637 }
4638 return( status );
4639}
4640
Gilles Peskine08542d82018-07-19 17:05:42 +02004641#if defined(MBEDTLS_DES_C)
4642static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4643{
4644 if( data_size >= 8 )
4645 mbedtls_des_key_set_parity( data );
4646 if( data_size >= 16 )
4647 mbedtls_des_key_set_parity( data + 8 );
4648 if( data_size >= 24 )
4649 mbedtls_des_key_set_parity( data + 16 );
4650}
4651#endif /* MBEDTLS_DES_C */
4652
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004653static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004654 psa_key_slot_t *slot,
4655 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004656 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004657{
4658 uint8_t *data = NULL;
4659 size_t bytes = PSA_BITS_TO_BYTES( bits );
4660 psa_status_t status;
4661
4662 if( ! key_type_is_raw_bytes( slot->type ) )
4663 return( PSA_ERROR_INVALID_ARGUMENT );
4664 if( bits % 8 != 0 )
4665 return( PSA_ERROR_INVALID_ARGUMENT );
4666 data = mbedtls_calloc( 1, bytes );
4667 if( data == NULL )
4668 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4669
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004670 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004671 if( status != PSA_SUCCESS )
4672 goto exit;
4673#if defined(MBEDTLS_DES_C)
4674 if( slot->type == PSA_KEY_TYPE_DES )
4675 psa_des_set_key_parity( data, bytes );
4676#endif /* MBEDTLS_DES_C */
4677 status = psa_import_key_into_slot( slot, data, bytes );
4678
4679exit:
4680 mbedtls_free( data );
4681 return( status );
4682}
4683
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004684psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004685 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004686 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004687{
4688 psa_status_t status;
4689 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004690 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004691 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004692#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4693 if( driver != NULL )
4694 {
4695 /* Deriving a key in a secure element is not implemented yet. */
4696 status = PSA_ERROR_NOT_SUPPORTED;
4697 }
4698#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004699 if( status == PSA_SUCCESS )
4700 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004701 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004702 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004703 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004704 }
4705 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004706 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004707 if( status != PSA_SUCCESS )
4708 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004709 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004710 *handle = 0;
4711 }
4712 return( status );
4713}
4714
Gilles Peskineeab56e42018-07-12 17:12:33 +02004715
4716
4717/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004718/* Key derivation */
4719/****************************************************************/
4720
Gilles Peskinea05219c2018-11-16 16:02:56 +01004721#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004722#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004723/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004724 * of the HKDF algorithm.
4725 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004726 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004727 * to potentially free embedded data structures and wipe confidential data.
4728 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004729static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004730 const uint8_t *secret,
4731 size_t secret_length,
4732 psa_algorithm_t hash_alg,
4733 const uint8_t *salt,
4734 size_t salt_length,
4735 const uint8_t *label,
4736 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004737{
4738 psa_status_t status;
4739 status = psa_hmac_setup_internal( &hkdf->hmac,
4740 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004741 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004742 if( status != PSA_SUCCESS )
4743 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004744 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004745 if( status != PSA_SUCCESS )
4746 return( status );
4747 status = psa_hmac_finish_internal( &hkdf->hmac,
4748 hkdf->prk,
4749 sizeof( hkdf->prk ) );
4750 if( status != PSA_SUCCESS )
4751 return( status );
4752 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4753 hkdf->block_number = 0;
4754 hkdf->info_length = label_length;
4755 if( label_length != 0 )
4756 {
4757 hkdf->info = mbedtls_calloc( 1, label_length );
4758 if( hkdf->info == NULL )
4759 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4760 memcpy( hkdf->info, label, label_length );
4761 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004762 hkdf->state = HKDF_STATE_KEYED;
4763 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004764 return( PSA_SUCCESS );
4765}
Janos Follath71a4c912019-06-11 09:14:47 +01004766#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004767#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004768
Gilles Peskinea05219c2018-11-16 16:02:56 +01004769#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004770#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004771/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004772 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004773 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004774 * to potentially free embedded data structures and wipe confidential data.
4775 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004776static psa_status_t psa_key_derivation_tls12_prf_setup(
4777 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004778 const unsigned char *key,
4779 size_t key_len,
4780 psa_algorithm_t hash_alg,
4781 const uint8_t *salt,
4782 size_t salt_length,
4783 const uint8_t *label,
4784 size_t label_length )
4785{
4786 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004787 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4788 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004789
4790 tls12_prf->key = mbedtls_calloc( 1, key_len );
4791 if( tls12_prf->key == NULL )
4792 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4793 tls12_prf->key_len = key_len;
4794 memcpy( tls12_prf->key, key, key_len );
4795
Hanno Becker580fba12018-11-13 20:50:45 +00004796 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004797 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004798 if( overflow )
4799 return( PSA_ERROR_INVALID_ARGUMENT );
4800
4801 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4802 if( tls12_prf->Ai_with_seed == NULL )
4803 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4804 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4805
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004806 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4807 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004808 if( label_length != 0 )
4809 {
4810 memcpy( tls12_prf->Ai_with_seed + hash_length,
4811 label, label_length );
4812 }
4813
4814 if( salt_length != 0 )
4815 {
4816 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4817 salt, salt_length );
4818 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004819
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004820 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004821 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004822 tls12_prf->block_number = 0;
4823 tls12_prf->offset_in_block = hash_length;
4824
4825 return( PSA_SUCCESS );
4826}
Janos Follath71a4c912019-06-11 09:14:47 +01004827#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004828
Janos Follath71a4c912019-06-11 09:14:47 +01004829#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004831static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4832 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004833 const unsigned char *psk,
4834 size_t psk_len,
4835 psa_algorithm_t hash_alg,
4836 const uint8_t *salt,
4837 size_t salt_length,
4838 const uint8_t *label,
4839 size_t label_length )
4840{
4841 psa_status_t status;
4842 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4843
4844 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4845 return( PSA_ERROR_INVALID_ARGUMENT );
4846
4847 /* Quoting RFC 4279, Section 2:
4848 *
4849 * The premaster secret is formed as follows: if the PSK is N octets
4850 * long, concatenate a uint16 with the value N, N zero octets, a second
4851 * uint16 with the value N, and the PSK itself.
4852 */
4853
4854 pms[0] = ( psk_len >> 8 ) & 0xff;
4855 pms[1] = ( psk_len >> 0 ) & 0xff;
4856 memset( pms + 2, 0, psk_len );
4857 pms[2 + psk_len + 0] = pms[0];
4858 pms[2 + psk_len + 1] = pms[1];
4859 memcpy( pms + 4 + psk_len, psk, psk_len );
4860
Gilles Peskinecbe66502019-05-16 16:59:18 +02004861 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004862 pms, 4 + 2 * psk_len,
4863 hash_alg,
4864 salt, salt_length,
4865 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004866
Gilles Peskine3f108122018-12-07 18:14:53 +01004867 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004868 return( status );
4869}
Janos Follath71a4c912019-06-11 09:14:47 +01004870#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004871#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004872
Janos Follath71a4c912019-06-11 09:14:47 +01004873#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004874/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004875 * to potentially free embedded data structures and wipe confidential data.
4876 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004877static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004878 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004879 const uint8_t *secret, size_t secret_length,
4880 psa_algorithm_t alg,
4881 const uint8_t *salt, size_t salt_length,
4882 const uint8_t *label, size_t label_length,
4883 size_t capacity )
4884{
4885 psa_status_t status;
4886 size_t max_capacity;
4887
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004888 /* Set operation->alg even on failure so that abort knows what to do. */
4889 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004890
4891#if defined(MBEDTLS_MD_C)
4892 if( PSA_ALG_IS_HKDF( alg ) )
4893 {
4894 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4895 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4896 if( hash_size == 0 )
4897 return( PSA_ERROR_NOT_SUPPORTED );
4898 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004899 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004900 secret, secret_length,
4901 hash_alg,
4902 salt, salt_length,
4903 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004904 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004905 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4906 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4907 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004908 {
4909 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4910 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4911
4912 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4913 if( hash_alg != PSA_ALG_SHA_256 &&
4914 hash_alg != PSA_ALG_SHA_384 )
4915 {
4916 return( PSA_ERROR_NOT_SUPPORTED );
4917 }
4918
4919 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004920
4921 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4922 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004923 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004924 secret, secret_length,
4925 hash_alg, salt, salt_length,
4926 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004927 }
4928 else
4929 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004930 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004932 secret, secret_length,
4933 hash_alg, salt, salt_length,
4934 label, label_length );
4935 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004936 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004937 else
4938#endif
4939 {
4940 return( PSA_ERROR_NOT_SUPPORTED );
4941 }
4942
4943 if( status != PSA_SUCCESS )
4944 return( status );
4945
4946 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004947 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004948 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004950 else
4951 return( PSA_ERROR_INVALID_ARGUMENT );
4952
4953 return( PSA_SUCCESS );
4954}
Janos Follath71a4c912019-06-11 09:14:47 +01004955#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004956
Janos Follath71a4c912019-06-11 09:14:47 +01004957#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004958psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004959 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004960 psa_algorithm_t alg,
4961 const uint8_t *salt,
4962 size_t salt_length,
4963 const uint8_t *label,
4964 size_t label_length,
4965 size_t capacity )
4966{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004967 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004968 psa_status_t status;
4969
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004971 return( PSA_ERROR_BAD_STATE );
4972
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004973 /* Make sure that alg is a key derivation algorithm. This prevents
4974 * key selection algorithms, which psa_key_derivation_internal
4975 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004976 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4977 return( PSA_ERROR_INVALID_ARGUMENT );
4978
Gilles Peskine28f8f302019-07-24 13:30:31 +02004979 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004980 if( status != PSA_SUCCESS )
4981 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004982
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004983 if( slot->type != PSA_KEY_TYPE_DERIVE )
4984 return( PSA_ERROR_INVALID_ARGUMENT );
4985
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004986 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004987 slot->data.raw.data,
4988 slot->data.raw.bytes,
4989 alg,
4990 salt, salt_length,
4991 label, label_length,
4992 capacity );
4993 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004994 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004995 return( status );
4996}
Janos Follath71a4c912019-06-11 09:14:47 +01004997#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004998
Gilles Peskine969c5d62019-01-16 15:53:06 +01004999static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005000 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005001 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005002{
Janos Follath5fe19732019-06-20 15:09:30 +01005003 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5004 * initialisers for this union leave some bytes unspecified.) */
5005 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5006
Gilles Peskine969c5d62019-01-16 15:53:06 +01005007 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005008#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005009 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5010 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5011 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005012 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005013 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005014 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5015 if( hash_size == 0 )
5016 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005017 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5018 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005019 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005020 {
5021 return( PSA_ERROR_NOT_SUPPORTED );
5022 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005023 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005024 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005025 }
5026#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005027 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005028 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005029}
5030
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005031psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005032 psa_algorithm_t alg )
5033{
5034 psa_status_t status;
5035
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005036 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005037 return( PSA_ERROR_BAD_STATE );
5038
Gilles Peskine6843c292019-01-18 16:44:49 +01005039 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5040 return( PSA_ERROR_INVALID_ARGUMENT );
5041 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005042 {
5043 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005044 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005045 }
5046 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5047 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005048 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005049 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005050 else
5051 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005052
5053 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005054 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005055 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005056}
5057
5058#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005059static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005060 psa_algorithm_t hash_alg,
5061 psa_key_derivation_step_t step,
5062 const uint8_t *data,
5063 size_t data_length )
5064{
5065 psa_status_t status;
5066 switch( step )
5067 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005068 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005069 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005070 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005071 status = psa_hmac_setup_internal( &hkdf->hmac,
5072 data, data_length,
5073 hash_alg );
5074 if( status != PSA_SUCCESS )
5075 return( status );
5076 hkdf->state = HKDF_STATE_STARTED;
5077 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005078 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005079 /* If no salt was provided, use an empty salt. */
5080 if( hkdf->state == HKDF_STATE_INIT )
5081 {
5082 status = psa_hmac_setup_internal( &hkdf->hmac,
5083 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005084 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005085 if( status != PSA_SUCCESS )
5086 return( status );
5087 hkdf->state = HKDF_STATE_STARTED;
5088 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005089 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005090 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005091 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5092 data, data_length );
5093 if( status != PSA_SUCCESS )
5094 return( status );
5095 status = psa_hmac_finish_internal( &hkdf->hmac,
5096 hkdf->prk,
5097 sizeof( hkdf->prk ) );
5098 if( status != PSA_SUCCESS )
5099 return( status );
5100 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5101 hkdf->block_number = 0;
5102 hkdf->state = HKDF_STATE_KEYED;
5103 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005104 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005105 if( hkdf->state == HKDF_STATE_OUTPUT )
5106 return( PSA_ERROR_BAD_STATE );
5107 if( hkdf->info_set )
5108 return( PSA_ERROR_BAD_STATE );
5109 hkdf->info_length = data_length;
5110 if( data_length != 0 )
5111 {
5112 hkdf->info = mbedtls_calloc( 1, data_length );
5113 if( hkdf->info == NULL )
5114 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5115 memcpy( hkdf->info, data, data_length );
5116 }
5117 hkdf->info_set = 1;
5118 return( PSA_SUCCESS );
5119 default:
5120 return( PSA_ERROR_INVALID_ARGUMENT );
5121 }
5122}
Janos Follathb03233e2019-06-11 15:30:30 +01005123
5124#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5125static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5126 psa_algorithm_t hash_alg,
5127 psa_key_derivation_step_t step,
5128 const uint8_t *data,
5129 size_t data_length )
5130{
5131 (void) prf;
5132 (void) hash_alg;
5133 (void) step;
5134 (void) data;
5135 (void) data_length;
5136
5137 return( PSA_ERROR_INVALID_ARGUMENT );
5138}
Janos Follath6660f0e2019-06-17 08:44:03 +01005139
5140static psa_status_t psa_tls12_prf_psk_to_ms_input(
5141 psa_tls12_prf_key_derivation_t *prf,
5142 psa_algorithm_t hash_alg,
5143 psa_key_derivation_step_t step,
5144 const uint8_t *data,
5145 size_t data_length )
5146{
5147 (void) prf;
5148 (void) hash_alg;
5149 (void) step;
5150 (void) data;
5151 (void) data_length;
5152
5153 return( PSA_ERROR_INVALID_ARGUMENT );
5154}
Janos Follathb03233e2019-06-11 15:30:30 +01005155#else
Janos Follathf08e2652019-06-13 09:05:41 +01005156static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5157 const uint8_t *data,
5158 size_t data_length )
5159{
5160 if( prf->state != TLS12_PRF_STATE_INIT )
5161 return( PSA_ERROR_BAD_STATE );
5162
Janos Follathd6dce9f2019-07-04 09:11:38 +01005163 if( data_length != 0 )
5164 {
5165 prf->seed = mbedtls_calloc( 1, data_length );
5166 if( prf->seed == NULL )
5167 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005168
Janos Follathd6dce9f2019-07-04 09:11:38 +01005169 memcpy( prf->seed, data, data_length );
5170 prf->seed_length = data_length;
5171 }
Janos Follathf08e2652019-06-13 09:05:41 +01005172
5173 prf->state = TLS12_PRF_STATE_SEED_SET;
5174
5175 return( PSA_SUCCESS );
5176}
5177
Janos Follath81550542019-06-13 14:26:34 +01005178static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5179 psa_algorithm_t hash_alg,
5180 const uint8_t *data,
5181 size_t data_length )
5182{
5183 psa_status_t status;
5184 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5185 return( PSA_ERROR_BAD_STATE );
5186
5187 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5188 if( status != PSA_SUCCESS )
5189 return( status );
5190
5191 prf->state = TLS12_PRF_STATE_KEY_SET;
5192
5193 return( PSA_SUCCESS );
5194}
5195
Janos Follath6660f0e2019-06-17 08:44:03 +01005196static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5197 psa_tls12_prf_key_derivation_t *prf,
5198 psa_algorithm_t hash_alg,
5199 const uint8_t *data,
5200 size_t data_length )
5201{
5202 psa_status_t status;
5203 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Janos Follath40e13932019-06-26 13:22:29 +01005204 unsigned char* cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005205
5206 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5207 return( PSA_ERROR_INVALID_ARGUMENT );
5208
5209 /* Quoting RFC 4279, Section 2:
5210 *
5211 * The premaster secret is formed as follows: if the PSK is N octets
5212 * long, concatenate a uint16 with the value N, N zero octets, a second
5213 * uint16 with the value N, and the PSK itself.
5214 */
5215
Janos Follath40e13932019-06-26 13:22:29 +01005216 *cur++ = ( data_length >> 8 ) & 0xff;
5217 *cur++ = ( data_length >> 0 ) & 0xff;
5218 memset( cur, 0, data_length );
5219 cur += data_length;
5220 *cur++ = pms[0];
5221 *cur++ = pms[1];
5222 memcpy( cur, data, data_length );
5223 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005224
Janos Follath40e13932019-06-26 13:22:29 +01005225 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005226
5227 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5228 return( status );
5229}
5230
Janos Follath63028dd2019-06-13 09:15:47 +01005231static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5232 const uint8_t *data,
5233 size_t data_length )
5234{
5235 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5236 return( PSA_ERROR_BAD_STATE );
5237
Janos Follathd6dce9f2019-07-04 09:11:38 +01005238 if( data_length != 0 )
5239 {
5240 prf->label = mbedtls_calloc( 1, data_length );
5241 if( prf->label == NULL )
5242 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005243
Janos Follathd6dce9f2019-07-04 09:11:38 +01005244 memcpy( prf->label, data, data_length );
5245 prf->label_length = data_length;
5246 }
Janos Follath63028dd2019-06-13 09:15:47 +01005247
5248 prf->state = TLS12_PRF_STATE_LABEL_SET;
5249
5250 return( PSA_SUCCESS );
5251}
5252
Janos Follathb03233e2019-06-11 15:30:30 +01005253static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5254 psa_algorithm_t hash_alg,
5255 psa_key_derivation_step_t step,
5256 const uint8_t *data,
5257 size_t data_length )
5258{
Janos Follathb03233e2019-06-11 15:30:30 +01005259 switch( step )
5260 {
Janos Follathf08e2652019-06-13 09:05:41 +01005261 case PSA_KEY_DERIVATION_INPUT_SEED:
5262 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005263 case PSA_KEY_DERIVATION_INPUT_SECRET:
5264 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005265 case PSA_KEY_DERIVATION_INPUT_LABEL:
5266 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005267 default:
5268 return( PSA_ERROR_INVALID_ARGUMENT );
5269 }
5270}
Janos Follath6660f0e2019-06-17 08:44:03 +01005271
5272static psa_status_t psa_tls12_prf_psk_to_ms_input(
5273 psa_tls12_prf_key_derivation_t *prf,
5274 psa_algorithm_t hash_alg,
5275 psa_key_derivation_step_t step,
5276 const uint8_t *data,
5277 size_t data_length )
5278{
5279 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005280 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005281 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5282 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005283 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005284
5285 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5286}
Janos Follathb03233e2019-06-11 15:30:30 +01005287#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005288#endif /* MBEDTLS_MD_C */
5289
Janos Follathb80a94e2019-06-12 15:54:46 +01005290static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005291 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005292 psa_key_derivation_step_t step,
5293 const uint8_t *data,
5294 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005295{
5296 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005297 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005298
5299#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005300 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005301 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005302 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005303 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005304 step, data, data_length );
5305 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005306 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005307#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005308#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005309 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005310 {
Janos Follathb03233e2019-06-11 15:30:30 +01005311 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5312 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5313 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005314 }
5315 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5316 {
5317 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5318 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5319 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005320 }
5321 else
5322#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005323 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005324 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005325 return( PSA_ERROR_BAD_STATE );
5326 }
5327
5328 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005329 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005330 return( status );
5331}
5332
Janos Follath51f4a0f2019-06-14 11:35:55 +01005333psa_status_t psa_key_derivation_input_bytes(
5334 psa_key_derivation_operation_t *operation,
5335 psa_key_derivation_step_t step,
5336 const uint8_t *data,
5337 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005338{
Janos Follathc5621512019-06-14 11:27:57 +01005339 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5340 return( PSA_ERROR_INVALID_ARGUMENT );
5341
5342 return( psa_key_derivation_input_internal( operation, step,
5343 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005344}
5345
Janos Follath51f4a0f2019-06-14 11:35:55 +01005346psa_status_t psa_key_derivation_input_key(
5347 psa_key_derivation_operation_t *operation,
5348 psa_key_derivation_step_t step,
5349 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005350{
5351 psa_key_slot_t *slot;
5352 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005353 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005354 PSA_KEY_USAGE_DERIVE,
5355 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005356 if( status != PSA_SUCCESS )
5357 return( status );
5358 if( slot->type != PSA_KEY_TYPE_DERIVE )
5359 return( PSA_ERROR_INVALID_ARGUMENT );
5360 /* Don't allow a key to be used as an input that is usually public.
5361 * This is debatable. It's ok from a cryptographic perspective to
5362 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005363 * the material should be dedicated to a particular input step,
5364 * otherwise this may allow the key to be used in an unintended way
5365 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005366 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005367 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005368 return( psa_key_derivation_input_internal( operation,
5369 step,
5370 slot->data.raw.data,
5371 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005372}
5373
Gilles Peskineea0fb492018-07-12 17:17:20 +02005374
5375
5376/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005377/* Key agreement */
5378/****************************************************************/
5379
Gilles Peskinea05219c2018-11-16 16:02:56 +01005380#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005381static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5382 size_t peer_key_length,
5383 const mbedtls_ecp_keypair *our_key,
5384 uint8_t *shared_secret,
5385 size_t shared_secret_size,
5386 size_t *shared_secret_length )
5387{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005388 mbedtls_ecp_keypair *their_key = NULL;
5389 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005390 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005391 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005392
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005393 status = psa_import_ec_public_key(
5394 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5395 peer_key, peer_key_length,
5396 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005397 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005398 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005399
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005400 status = mbedtls_to_psa_error(
5401 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5402 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005403 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005404 status = mbedtls_to_psa_error(
5405 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5406 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005407 goto exit;
5408
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005409 status = mbedtls_to_psa_error(
5410 mbedtls_ecdh_calc_secret( &ecdh,
5411 shared_secret_length,
5412 shared_secret, shared_secret_size,
5413 mbedtls_ctr_drbg_random,
5414 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005415
5416exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005417 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005418 mbedtls_ecp_keypair_free( their_key );
5419 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005420 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005421}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005422#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005423
Gilles Peskine01d718c2018-09-18 12:01:02 +02005424#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5425
Gilles Peskine0216fe12019-04-11 21:23:21 +02005426static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5427 psa_key_slot_t *private_key,
5428 const uint8_t *peer_key,
5429 size_t peer_key_length,
5430 uint8_t *shared_secret,
5431 size_t shared_secret_size,
5432 size_t *shared_secret_length )
5433{
5434 switch( alg )
5435 {
5436#if defined(MBEDTLS_ECDH_C)
5437 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005438 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005439 return( PSA_ERROR_INVALID_ARGUMENT );
5440 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5441 private_key->data.ecp,
5442 shared_secret, shared_secret_size,
5443 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005444#endif /* MBEDTLS_ECDH_C */
5445 default:
5446 (void) private_key;
5447 (void) peer_key;
5448 (void) peer_key_length;
5449 (void) shared_secret;
5450 (void) shared_secret_size;
5451 (void) shared_secret_length;
5452 return( PSA_ERROR_NOT_SUPPORTED );
5453 }
5454}
5455
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005456/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005457 * to potentially free embedded data structures and wipe confidential data.
5458 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005459static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005460 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005461 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005462 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005463 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005464{
5465 psa_status_t status;
5466 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5467 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005468 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005469
5470 /* Step 1: run the secret agreement algorithm to generate the shared
5471 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005472 status = psa_key_agreement_raw_internal( ka_alg,
5473 private_key,
5474 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005475 shared_secret,
5476 sizeof( shared_secret ),
5477 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005478 if( status != PSA_SUCCESS )
5479 goto exit;
5480
5481 /* Step 2: set up the key derivation to generate key material from
5482 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005483 status = psa_key_derivation_input_internal( operation, step,
5484 shared_secret,
5485 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005486
Gilles Peskine01d718c2018-09-18 12:01:02 +02005487exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005488 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005489 return( status );
5490}
5491
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005492psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005493 psa_key_derivation_step_t step,
5494 psa_key_handle_t private_key,
5495 const uint8_t *peer_key,
5496 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005497{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005498 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005499 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005500 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005501 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005502 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005503 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005504 if( status != PSA_SUCCESS )
5505 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005506 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005507 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005508 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005509 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005510 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005511 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005512}
5513
Gilles Peskinebe697d82019-05-16 18:00:41 +02005514psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5515 psa_key_handle_t private_key,
5516 const uint8_t *peer_key,
5517 size_t peer_key_length,
5518 uint8_t *output,
5519 size_t output_size,
5520 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005521{
5522 psa_key_slot_t *slot;
5523 psa_status_t status;
5524
5525 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5526 {
5527 status = PSA_ERROR_INVALID_ARGUMENT;
5528 goto exit;
5529 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005530 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005531 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005532 if( status != PSA_SUCCESS )
5533 goto exit;
5534
5535 status = psa_key_agreement_raw_internal( alg, slot,
5536 peer_key, peer_key_length,
5537 output, output_size,
5538 output_length );
5539
5540exit:
5541 if( status != PSA_SUCCESS )
5542 {
5543 /* If an error happens and is not handled properly, the output
5544 * may be used as a key to protect sensitive data. Arrange for such
5545 * a key to be random, which is likely to result in decryption or
5546 * verification errors. This is better than filling the buffer with
5547 * some constant data such as zeros, which would result in the data
5548 * being protected with a reproducible, easily knowable key.
5549 */
5550 psa_generate_random( output, output_size );
5551 *output_length = output_size;
5552 }
5553 return( status );
5554}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005555
5556
5557/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005558/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005559/****************************************************************/
5560
5561psa_status_t psa_generate_random( uint8_t *output,
5562 size_t output_size )
5563{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005564 int ret;
5565 GUARD_MODULE_INITIALIZED;
5566
5567 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005568 return( mbedtls_to_psa_error( ret ) );
5569}
5570
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005571#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5572#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005573
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005574psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5575 size_t seed_size )
5576{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005577 if( global_data.initialized )
5578 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005579
5580 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5581 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5582 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5583 return( PSA_ERROR_INVALID_ARGUMENT );
5584
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005585 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005586}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005587#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005588
Gilles Peskinee56e8782019-04-26 17:34:02 +02005589#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5590static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5591 size_t domain_parameters_size,
5592 int *exponent )
5593{
5594 size_t i;
5595 uint32_t acc = 0;
5596
5597 if( domain_parameters_size == 0 )
5598 {
5599 *exponent = 65537;
5600 return( PSA_SUCCESS );
5601 }
5602
5603 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5604 * support values that fit in a 32-bit integer, which is larger than
5605 * int on just about every platform anyway. */
5606 if( domain_parameters_size > sizeof( acc ) )
5607 return( PSA_ERROR_NOT_SUPPORTED );
5608 for( i = 0; i < domain_parameters_size; i++ )
5609 acc = ( acc << 8 ) | domain_parameters[i];
5610 if( acc > INT_MAX )
5611 return( PSA_ERROR_NOT_SUPPORTED );
5612 *exponent = acc;
5613 return( PSA_SUCCESS );
5614}
5615#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5616
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005617static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005618 psa_key_slot_t *slot, size_t bits,
5619 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005620{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005621 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005622
Gilles Peskinee56e8782019-04-26 17:34:02 +02005623 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005624 return( PSA_ERROR_INVALID_ARGUMENT );
5625
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005626 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005627 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005628 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005629 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005630 if( status != PSA_SUCCESS )
5631 return( status );
5632 status = psa_generate_random( slot->data.raw.data,
5633 slot->data.raw.bytes );
5634 if( status != PSA_SUCCESS )
5635 {
5636 mbedtls_free( slot->data.raw.data );
5637 return( status );
5638 }
5639#if defined(MBEDTLS_DES_C)
5640 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005641 psa_des_set_key_parity( slot->data.raw.data,
5642 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005643#endif /* MBEDTLS_DES_C */
5644 }
5645 else
5646
5647#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005648 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005649 {
5650 mbedtls_rsa_context *rsa;
5651 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005652 int exponent;
5653 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005654 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5655 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005656 /* Accept only byte-aligned keys, for the same reasons as
5657 * in psa_import_rsa_key(). */
5658 if( bits % 8 != 0 )
5659 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005660 status = psa_read_rsa_exponent( domain_parameters,
5661 domain_parameters_size,
5662 &exponent );
5663 if( status != PSA_SUCCESS )
5664 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005665 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5666 if( rsa == NULL )
5667 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5668 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5669 ret = mbedtls_rsa_gen_key( rsa,
5670 mbedtls_ctr_drbg_random,
5671 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005672 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005673 exponent );
5674 if( ret != 0 )
5675 {
5676 mbedtls_rsa_free( rsa );
5677 mbedtls_free( rsa );
5678 return( mbedtls_to_psa_error( ret ) );
5679 }
5680 slot->data.rsa = rsa;
5681 }
5682 else
5683#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5684
5685#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005686 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005687 {
5688 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5689 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5690 const mbedtls_ecp_curve_info *curve_info =
5691 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5692 mbedtls_ecp_keypair *ecp;
5693 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005694 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005695 return( PSA_ERROR_NOT_SUPPORTED );
5696 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5697 return( PSA_ERROR_NOT_SUPPORTED );
5698 if( curve_info->bit_size != bits )
5699 return( PSA_ERROR_INVALID_ARGUMENT );
5700 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5701 if( ecp == NULL )
5702 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5703 mbedtls_ecp_keypair_init( ecp );
5704 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5705 mbedtls_ctr_drbg_random,
5706 &global_data.ctr_drbg );
5707 if( ret != 0 )
5708 {
5709 mbedtls_ecp_keypair_free( ecp );
5710 mbedtls_free( ecp );
5711 return( mbedtls_to_psa_error( ret ) );
5712 }
5713 slot->data.ecp = ecp;
5714 }
5715 else
5716#endif /* MBEDTLS_ECP_C */
5717
5718 return( PSA_ERROR_NOT_SUPPORTED );
5719
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005720 return( PSA_SUCCESS );
5721}
5722
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005723psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005724 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005725{
5726 psa_status_t status;
5727 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005728 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005729 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005730#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5731 if( driver != NULL )
5732 {
5733 /* Generating a key in a secure element is not implemented yet. */
5734 status = PSA_ERROR_NOT_SUPPORTED;
5735 }
5736#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005737 if( status == PSA_SUCCESS )
5738 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005739 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005740 slot, attributes->bits,
5741 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005742 }
5743 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005744 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005745 if( status != PSA_SUCCESS )
5746 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005747 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005748 *handle = 0;
5749 }
5750 return( status );
5751}
5752
5753
Gilles Peskine05d69892018-06-19 22:00:52 +02005754
5755/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005756/* Module setup */
5757/****************************************************************/
5758
Gilles Peskine5e769522018-11-20 21:59:56 +01005759psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5760 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5761 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5762{
5763 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5764 return( PSA_ERROR_BAD_STATE );
5765 global_data.entropy_init = entropy_init;
5766 global_data.entropy_free = entropy_free;
5767 return( PSA_SUCCESS );
5768}
5769
Gilles Peskinee59236f2018-01-27 23:32:46 +01005770void mbedtls_psa_crypto_free( void )
5771{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005772 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005773 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5774 {
5775 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005776 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005777 }
5778 /* Wipe all remaining data, including configuration.
5779 * In particular, this sets all state indicator to the value
5780 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005781 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005782#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005783 /* Unregister all secure element drivers, so that we restart from
5784 * a pristine state. */
5785 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005786#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005787}
5788
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005789#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5790/** Recover a transaction that was interrupted by a power failure.
5791 *
5792 * This function is called during initialization, before psa_crypto_init()
5793 * returns. If this function returns a failure status, the initialization
5794 * fails.
5795 */
5796static psa_status_t psa_crypto_recover_transaction(
5797 const psa_crypto_transaction_t *transaction )
5798{
5799 switch( transaction->unknown.type )
5800 {
5801 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5802 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
5803 /* TOnogrepDO - fall through to the failure case until this
5804 * is implemented */
5805 default:
5806 /* We found an unsupported transaction in the storage.
5807 * We don't know what state the storage is in. Give up. */
5808 return( PSA_ERROR_STORAGE_FAILURE );
5809 }
5810}
5811#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5812
Gilles Peskinee59236f2018-01-27 23:32:46 +01005813psa_status_t psa_crypto_init( void )
5814{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005815 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005816 const unsigned char drbg_seed[] = "PSA";
5817
Gilles Peskinec6b69072018-11-20 21:42:52 +01005818 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005819 if( global_data.initialized != 0 )
5820 return( PSA_SUCCESS );
5821
Gilles Peskine5e769522018-11-20 21:59:56 +01005822 /* Set default configuration if
5823 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5824 if( global_data.entropy_init == NULL )
5825 global_data.entropy_init = mbedtls_entropy_init;
5826 if( global_data.entropy_free == NULL )
5827 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005828
Gilles Peskinec6b69072018-11-20 21:42:52 +01005829 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005830 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005831 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005832 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005833 status = mbedtls_to_psa_error(
5834 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5835 mbedtls_entropy_func,
5836 &global_data.entropy,
5837 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5838 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005839 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005840 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005841
Gilles Peskine66fb1262018-12-10 16:29:04 +01005842 status = psa_initialize_key_slots( );
5843 if( status != PSA_SUCCESS )
5844 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005845
Gilles Peskine4b734222019-07-24 15:56:31 +02005846#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005847 status = psa_crypto_load_transaction( );
5848 if( status == PSA_SUCCESS )
5849 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005850 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5851 if( status != PSA_SUCCESS )
5852 goto exit;
5853 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005854 }
5855 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5856 {
5857 /* There's no transaction to complete. It's all good. */
5858 status = PSA_SUCCESS;
5859 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005860#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005861
Gilles Peskinec6b69072018-11-20 21:42:52 +01005862 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005863 global_data.initialized = 1;
5864
Gilles Peskinee59236f2018-01-27 23:32:46 +01005865exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005866 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005867 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005868 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005869}
5870
5871#endif /* MBEDTLS_PSA_CRYPTO_C */