blob: 4bd2d13d267d83000db9454aaaef6c6eacf398dd [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 {
996 /* TOnogrepDO: destroy what can be destroyed anyway */
997 return( status );
998 }
999
Gilles Peskine354f7672019-07-12 23:46:38 +02001000 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +02001001 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001002#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1003
Darryl Greend49a4992018-06-18 17:27:26 +01001004#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1005 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1006 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001007 storage_status =
1008 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +01001009 }
1010#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001011
Gilles Peskinefc762652019-07-22 19:30:34 +02001012#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1013 if( driver != NULL )
1014 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001015 psa_status_t status2;
1016 status = psa_save_se_persistent_data( driver );
1017 status2 = psa_crypto_stop_transaction( );
1018 if( status == PSA_SUCCESS )
1019 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001020 if( status != PSA_SUCCESS )
1021 {
1022 /* TOnogrepDO: destroy what can be destroyed anyway */
1023 return( status );
1024 }
1025 }
1026#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1027
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001028 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001029 if( status != PSA_SUCCESS )
1030 return( status );
1031 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001032}
1033
Gilles Peskineb870b182018-07-06 16:02:09 +02001034/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001035static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +02001036{
1037 if( key_type_is_raw_bytes( slot->type ) )
1038 return( slot->data.raw.bytes * 8 );
1039#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001040 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001041 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001042#endif /* defined(MBEDTLS_RSA_C) */
1043#if defined(MBEDTLS_ECP_C)
1044 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1045 return( slot->data.ecp->grp.pbits );
1046#endif /* defined(MBEDTLS_ECP_C) */
1047 /* Shouldn't happen except on an empty slot. */
1048 return( 0 );
1049}
1050
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001051void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1052{
Gilles Peskineb699f072019-04-26 16:06:02 +02001053 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001054 memset( attributes, 0, sizeof( *attributes ) );
1055}
1056
Gilles Peskineb699f072019-04-26 16:06:02 +02001057psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1058 psa_key_type_t type,
1059 const uint8_t *data,
1060 size_t data_length )
1061{
1062 uint8_t *copy = NULL;
1063
1064 if( data_length != 0 )
1065 {
1066 copy = mbedtls_calloc( 1, data_length );
1067 if( copy == NULL )
1068 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1069 memcpy( copy, data, data_length );
1070 }
1071 /* After this point, this function is guaranteed to succeed, so it
1072 * can start modifying `*attributes`. */
1073
1074 if( attributes->domain_parameters != NULL )
1075 {
1076 mbedtls_free( attributes->domain_parameters );
1077 attributes->domain_parameters = NULL;
1078 attributes->domain_parameters_size = 0;
1079 }
1080
1081 attributes->domain_parameters = copy;
1082 attributes->domain_parameters_size = data_length;
1083 attributes->type = type;
1084 return( PSA_SUCCESS );
1085}
1086
1087psa_status_t psa_get_key_domain_parameters(
1088 const psa_key_attributes_t *attributes,
1089 uint8_t *data, size_t data_size, size_t *data_length )
1090{
1091 if( attributes->domain_parameters_size > data_size )
1092 return( PSA_ERROR_BUFFER_TOO_SMALL );
1093 *data_length = attributes->domain_parameters_size;
1094 if( attributes->domain_parameters_size != 0 )
1095 memcpy( data, attributes->domain_parameters,
1096 attributes->domain_parameters_size );
1097 return( PSA_SUCCESS );
1098}
1099
1100#if defined(MBEDTLS_RSA_C)
1101static psa_status_t psa_get_rsa_public_exponent(
1102 const mbedtls_rsa_context *rsa,
1103 psa_key_attributes_t *attributes )
1104{
1105 mbedtls_mpi mpi;
1106 int ret;
1107 uint8_t *buffer = NULL;
1108 size_t buflen;
1109 mbedtls_mpi_init( &mpi );
1110
1111 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1112 if( ret != 0 )
1113 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001114 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1115 {
1116 /* It's the default value, which is reported as an empty string,
1117 * so there's nothing to do. */
1118 goto exit;
1119 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001120
1121 buflen = mbedtls_mpi_size( &mpi );
1122 buffer = mbedtls_calloc( 1, buflen );
1123 if( buffer == NULL )
1124 {
1125 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1126 goto exit;
1127 }
1128 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1129 if( ret != 0 )
1130 goto exit;
1131 attributes->domain_parameters = buffer;
1132 attributes->domain_parameters_size = buflen;
1133
1134exit:
1135 mbedtls_mpi_free( &mpi );
1136 if( ret != 0 )
1137 mbedtls_free( buffer );
1138 return( mbedtls_to_psa_error( ret ) );
1139}
1140#endif /* MBEDTLS_RSA_C */
1141
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001142/** Retrieve the readily-accessible attributes of a key in a slot.
1143 *
1144 * This function does not compute attributes that are not directly
1145 * stored in the slot, such as the bit size of a transparent key.
1146 */
1147static void psa_get_key_slot_attributes( psa_key_slot_t *slot,
1148 psa_key_attributes_t *attributes )
1149{
1150 attributes->id = slot->persistent_storage_id;
1151 attributes->lifetime = slot->lifetime;
1152 attributes->policy = slot->policy;
1153 attributes->type = slot->type;
1154}
1155
1156/** Retrieve all the publicly-accessible attributes of a key.
1157 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001158psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1159 psa_key_attributes_t *attributes )
1160{
1161 psa_key_slot_t *slot;
1162 psa_status_t status;
1163
1164 psa_reset_key_attributes( attributes );
1165
Gilles Peskine28f8f302019-07-24 13:30:31 +02001166 status = psa_get_transparent_key( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001167 if( status != PSA_SUCCESS )
1168 return( status );
1169
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001170 psa_get_key_slot_attributes( slot, attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001171 attributes->bits = psa_get_key_slot_bits( slot );
Gilles Peskineb699f072019-04-26 16:06:02 +02001172
1173 switch( slot->type )
1174 {
1175#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001176 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001177 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1178 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1179 break;
1180#endif
1181 default:
1182 /* Nothing else to do. */
1183 break;
1184 }
1185
1186 if( status != PSA_SUCCESS )
1187 psa_reset_key_attributes( attributes );
1188 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001189}
1190
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001191#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001192static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1193 unsigned char *buf, size_t size )
1194{
1195 int ret;
1196 unsigned char *c;
1197 size_t len = 0;
1198
1199 c = buf + size;
1200
1201 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1202
1203 return( (int) len );
1204}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001205#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001206
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001207static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1208 uint8_t *data,
1209 size_t data_size,
1210 size_t *data_length,
1211 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001212{
Gilles Peskine5d309672019-07-12 23:47:28 +02001213#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1214 const psa_drv_se_t *drv;
1215 psa_drv_se_context_t *drv_context;
1216#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1217
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001218 *data_length = 0;
1219
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001220 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001221 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001222
Gilles Peskine5d309672019-07-12 23:47:28 +02001223#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1224 if( psa_get_se_driver( slot->lifetime, &drv, &drv_context ) )
1225 {
1226 psa_drv_se_export_key_t method;
1227 if( drv->key_management == NULL )
1228 return( PSA_ERROR_NOT_SUPPORTED );
1229 method = ( export_public_key ?
1230 drv->key_management->p_export_public :
1231 drv->key_management->p_export );
1232 if( method == NULL )
1233 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001234 return( method( drv_context,
1235 slot->data.se.slot_number,
1236 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001237 }
1238#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1239
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001240 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001241 {
1242 if( slot->data.raw.bytes > data_size )
1243 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001244 if( data_size != 0 )
1245 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001246 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001247 memset( data + slot->data.raw.bytes, 0,
1248 data_size - slot->data.raw.bytes );
1249 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001250 *data_length = slot->data.raw.bytes;
1251 return( PSA_SUCCESS );
1252 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001253#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001254 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001255 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001256 psa_status_t status;
1257
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001258 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001259 if( bytes > data_size )
1260 return( PSA_ERROR_BUFFER_TOO_SMALL );
1261 status = mbedtls_to_psa_error(
1262 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1263 if( status != PSA_SUCCESS )
1264 return( status );
1265 memset( data + bytes, 0, data_size - bytes );
1266 *data_length = bytes;
1267 return( PSA_SUCCESS );
1268 }
1269#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001270 else
Moran Peker17e36e12018-05-02 12:55:20 +03001271 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001272#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001273 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001274 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001275 {
Moran Pekera998bc62018-04-16 18:16:20 +03001276 mbedtls_pk_context pk;
1277 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001278 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001279 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001280#if defined(MBEDTLS_RSA_C)
1281 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001282 pk.pk_info = &mbedtls_rsa_info;
1283 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001284#else
1285 return( PSA_ERROR_NOT_SUPPORTED );
1286#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001287 }
1288 else
1289 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001290#if defined(MBEDTLS_ECP_C)
1291 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001292 pk.pk_info = &mbedtls_eckey_info;
1293 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001294#else
1295 return( PSA_ERROR_NOT_SUPPORTED );
1296#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001297 }
Moran Pekerd7326592018-05-29 16:56:39 +03001298 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001299 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001300 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001301 }
Moran Peker17e36e12018-05-02 12:55:20 +03001302 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001303 {
Moran Peker17e36e12018-05-02 12:55:20 +03001304 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001305 }
Moran Peker60364322018-04-29 11:34:58 +03001306 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001307 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001308 /* If data_size is 0 then data may be NULL and then the
1309 * call to memset would have undefined behavior. */
1310 if( data_size != 0 )
1311 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001312 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001313 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001314 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1315 * Move the data to the beginning and erase remaining data
1316 * at the original location. */
1317 if( 2 * (size_t) ret <= data_size )
1318 {
1319 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001320 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001321 }
1322 else if( (size_t) ret < data_size )
1323 {
1324 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001325 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001326 }
Moran Pekera998bc62018-04-16 18:16:20 +03001327 *data_length = ret;
1328 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001329 }
1330 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001331#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001332 {
1333 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001334 it is valid for a special-purpose implementation to omit
1335 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001336 return( PSA_ERROR_NOT_SUPPORTED );
1337 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001338 }
1339}
1340
Gilles Peskinec5487a82018-12-03 18:08:14 +01001341psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001342 uint8_t *data,
1343 size_t data_size,
1344 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001345{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001346 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001347 psa_status_t status;
1348
1349 /* Set the key to empty now, so that even when there are errors, we always
1350 * set data_length to a value between 0 and data_size. On error, setting
1351 * the key to empty is a good choice because an empty key representation is
1352 * unlikely to be accepted anywhere. */
1353 *data_length = 0;
1354
1355 /* Export requires the EXPORT flag. There is an exception for public keys,
1356 * which don't require any flag, but psa_get_key_from_slot takes
1357 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001358 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001359 if( status != PSA_SUCCESS )
1360 return( status );
1361 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001362 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001363}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001364
Gilles Peskinec5487a82018-12-03 18:08:14 +01001365psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001366 uint8_t *data,
1367 size_t data_size,
1368 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001369{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001370 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001371 psa_status_t status;
1372
1373 /* Set the key to empty now, so that even when there are errors, we always
1374 * set data_length to a value between 0 and data_size. On error, setting
1375 * the key to empty is a good choice because an empty key representation is
1376 * unlikely to be accepted anywhere. */
1377 *data_length = 0;
1378
1379 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001380 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001381 if( status != PSA_SUCCESS )
1382 return( status );
1383 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001384 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001385}
1386
Gilles Peskine4747d192019-04-17 15:05:45 +02001387static psa_status_t psa_set_key_policy_internal(
1388 psa_key_slot_t *slot,
1389 const psa_key_policy_t *policy )
1390{
1391 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001392 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001393 PSA_KEY_USAGE_ENCRYPT |
1394 PSA_KEY_USAGE_DECRYPT |
1395 PSA_KEY_USAGE_SIGN |
1396 PSA_KEY_USAGE_VERIFY |
1397 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1398 return( PSA_ERROR_INVALID_ARGUMENT );
1399
1400 slot->policy = *policy;
1401 return( PSA_SUCCESS );
1402}
1403
1404/** Prepare a key slot to receive key material.
1405 *
1406 * This function allocates a key slot and sets its metadata.
1407 *
1408 * If this function fails, call psa_fail_key_creation().
1409 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001410 * This function is intended to be used as follows:
1411 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1412 * it with the specified attributes, and assign it a handle.
1413 * -# Populate the slot with the key material.
1414 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1415 * In case of failure at any step, stop the sequence and call
1416 * psa_fail_key_creation().
1417 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001418 * \param[in] attributes Key attributes for the new key.
1419 * \param[out] handle On success, a handle for the allocated slot.
1420 * \param[out] p_slot On success, a pointer to the prepared slot.
1421 * \param[out] p_drv On any return, the driver for the key, if any.
1422 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001423 *
1424 * \retval #PSA_SUCCESS
1425 * The key slot is ready to receive key material.
1426 * \return If this function fails, the key slot is an invalid state.
1427 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001428 */
1429static psa_status_t psa_start_key_creation(
1430 const psa_key_attributes_t *attributes,
1431 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001432 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001433 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001434{
1435 psa_status_t status;
1436 psa_key_slot_t *slot;
1437
Gilles Peskine011e4282019-06-26 18:34:38 +02001438 *p_drv = NULL;
1439
Gilles Peskine267c6562019-05-27 19:01:54 +02001440 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001441 if( status != PSA_SUCCESS )
1442 return( status );
1443 slot = *p_slot;
1444
1445 status = psa_set_key_policy_internal( slot, &attributes->policy );
1446 if( status != PSA_SUCCESS )
1447 return( status );
1448 slot->lifetime = attributes->lifetime;
Gilles Peskine011e4282019-06-26 18:34:38 +02001449
Gilles Peskine4747d192019-04-17 15:05:45 +02001450 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001451 {
1452 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskine011e4282019-06-26 18:34:38 +02001453 attributes->id,
1454 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001455 if( status != PSA_SUCCESS )
1456 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001457 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001458 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001459 slot->type = attributes->type;
1460
Gilles Peskinecbaff462019-07-12 23:46:04 +02001461#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine60450a42019-07-25 11:32:45 +02001462 /* For a key in a secure element, we need to do three things:
1463 * create the key file in internal storage, create the
1464 * key inside the secure element, and update the driver's
1465 * persistent data. Start a transaction that will encompass these
1466 * three actions. */
1467 /* The first thing to do is to find a slot number for the new key.
1468 * We save the slot number in persistent storage as part of the
1469 * transaction data. It will be needed to recover if the power
1470 * fails during the key creation process, to clean up on the secure
1471 * element side after restarting. Obtaining a slot number from the
1472 * secure element driver updates its persistent state, but we do not yet
1473 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001474 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001475 if( *p_drv != NULL )
1476 {
1477 status = psa_find_se_slot_for_key( attributes, *p_drv,
1478 &slot->data.se.slot_number );
1479 if( status != PSA_SUCCESS )
1480 return( status );
1481 }
Gilles Peskinefc762652019-07-22 19:30: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 )
1488 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001489#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1490
Gilles Peskine4747d192019-04-17 15:05:45 +02001491 return( status );
1492}
1493
1494/** Finalize the creation of a key once its key material has been set.
1495 *
1496 * This entails writing the key to persistent storage.
1497 *
1498 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001499 * See the documentation of psa_start_key_creation() for the intended use
1500 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001501 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001502 * \param[in,out] slot Pointer to the slot with key material.
1503 * \param[in] driver The secure element driver for the key,
1504 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001505 *
1506 * \retval #PSA_SUCCESS
1507 * The key was successfully created. The handle is now valid.
1508 * \return If this function fails, the key slot is an invalid state.
1509 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001510 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001511static psa_status_t psa_finish_key_creation(
1512 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001513 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001514{
1515 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001516 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001517 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001518
1519#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1df83d42019-07-23 16:13:14 +02001520 if( slot->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001521 {
1522 uint8_t *buffer = NULL;
1523 size_t buffer_size = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001524 size_t length = 0;
Gilles Peskine4747d192019-04-17 15:05:45 +02001525
Gilles Peskine1df83d42019-07-23 16:13:14 +02001526#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1527 if( driver != NULL )
1528 {
1529 buffer = (uint8_t*) &slot->data.se.slot_number;
1530 length = sizeof( slot->data.se.slot_number );
1531 }
1532 else
1533#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1534 {
1535 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
1536 psa_get_key_slot_bits( slot ) );
1537 buffer = mbedtls_calloc( 1, buffer_size );
1538 if( buffer == NULL && buffer_size != 0 )
1539 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1540 status = psa_internal_export_key( slot,
1541 buffer, buffer_size, &length,
1542 0 );
1543 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001544
1545 if( status == PSA_SUCCESS )
1546 {
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001547 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1548 psa_get_key_slot_attributes( slot, &attributes );
1549 status = psa_save_persistent_key( &attributes, buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001550 }
1551
Gilles Peskine1df83d42019-07-23 16:13:14 +02001552#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1553 if( driver == NULL )
1554#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1555 {
1556 if( buffer_size != 0 )
1557 mbedtls_platform_zeroize( buffer, buffer_size );
1558 mbedtls_free( buffer );
1559 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001560 }
1561#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1562
Gilles Peskinecbaff462019-07-12 23:46:04 +02001563#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1564 if( driver != NULL )
1565 {
1566 status = psa_save_se_persistent_data( driver );
1567 if( status != PSA_SUCCESS )
1568 {
1569 psa_destroy_persistent_key( slot->persistent_storage_id );
1570 return( status );
1571 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001572 status = psa_crypto_stop_transaction( );
1573 if( status != PSA_SUCCESS )
1574 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001575 }
1576#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1577
Gilles Peskine4747d192019-04-17 15:05:45 +02001578 return( status );
1579}
1580
1581/** Abort the creation of a key.
1582 *
1583 * You may call this function after calling psa_start_key_creation(),
1584 * or after psa_finish_key_creation() fails. In other circumstances, this
1585 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001586 * See the documentation of psa_start_key_creation() for the intended use
1587 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001588 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001589 * \param[in,out] slot Pointer to the slot with key material.
1590 * \param[in] driver The secure element driver for the key,
1591 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001592 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001593static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001594 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001595{
Gilles Peskine011e4282019-06-26 18:34:38 +02001596 (void) driver;
1597
Gilles Peskine4747d192019-04-17 15:05:45 +02001598 if( slot == NULL )
1599 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001600
1601#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1602 /* TOnogrepDO: If the key has already been created in the secure
1603 * element, and the failure happened later (when saving metadata
1604 * to internal storage), we need to destroy the key in the secure
1605 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001606
1607 /* Abort the ongoing transaction if any. We already did what it
1608 * takes to undo any partial creation. All that's left is to update
1609 * the transaction data itself. */
1610 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001611#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1612
Gilles Peskine4747d192019-04-17 15:05:45 +02001613 psa_wipe_key_slot( slot );
1614}
1615
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001616static psa_status_t psa_check_key_slot_attributes(
1617 const psa_key_slot_t *slot,
1618 const psa_key_attributes_t *attributes )
1619{
1620 if( attributes->type != 0 )
1621 {
1622 if( attributes->type != slot->type )
1623 return( PSA_ERROR_INVALID_ARGUMENT );
1624 }
1625
1626 if( attributes->domain_parameters_size != 0 )
1627 {
1628#if defined(MBEDTLS_RSA_C)
1629 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1630 {
1631 mbedtls_mpi actual, required;
1632 int ret;
1633 mbedtls_mpi_init( &actual );
1634 mbedtls_mpi_init( &required );
1635 ret = mbedtls_rsa_export( slot->data.rsa,
1636 NULL, NULL, NULL, NULL, &actual );
1637 if( ret != 0 )
1638 goto rsa_exit;
1639 ret = mbedtls_mpi_read_binary( &required,
1640 attributes->domain_parameters,
1641 attributes->domain_parameters_size );
1642 if( ret != 0 )
1643 goto rsa_exit;
1644 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1645 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1646 rsa_exit:
1647 mbedtls_mpi_free( &actual );
1648 mbedtls_mpi_free( &required );
1649 if( ret != 0)
1650 return( mbedtls_to_psa_error( ret ) );
1651 }
1652 else
1653#endif
1654 {
1655 return( PSA_ERROR_INVALID_ARGUMENT );
1656 }
1657 }
1658
1659 if( attributes->bits != 0 )
1660 {
1661 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1662 return( PSA_ERROR_INVALID_ARGUMENT );
1663 }
1664
1665 return( PSA_SUCCESS );
1666}
1667
Gilles Peskine4747d192019-04-17 15:05:45 +02001668psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001669 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001670 size_t data_length,
1671 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001672{
1673 psa_status_t status;
1674 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001675 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001676
Gilles Peskine011e4282019-06-26 18:34:38 +02001677 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001678 if( status != PSA_SUCCESS )
1679 goto exit;
1680
Gilles Peskine5d309672019-07-12 23:47:28 +02001681#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1682 if( driver != NULL )
1683 {
1684 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
1685 if( drv->key_management == NULL ||
1686 drv->key_management->p_import == NULL )
1687 {
1688 status = PSA_ERROR_NOT_SUPPORTED;
1689 goto exit;
1690 }
1691 status = drv->key_management->p_import(
1692 psa_get_se_driver_context( driver ),
1693 slot->data.se.slot_number,
1694 slot->lifetime, slot->type, slot->policy.alg, slot->policy.usage,
1695 data, data_length );
1696 /* TOnogrepDO: psa_check_key_slot_attributes? */
1697 }
1698 else
1699#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1700 {
1701 status = psa_import_key_into_slot( slot, data, data_length );
1702 if( status != PSA_SUCCESS )
1703 goto exit;
1704 status = psa_check_key_slot_attributes( slot, attributes );
1705 if( status != PSA_SUCCESS )
1706 goto exit;
1707 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001708
Gilles Peskine011e4282019-06-26 18:34:38 +02001709 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001710exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001711 if( status != PSA_SUCCESS )
1712 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001713 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001714 *handle = 0;
1715 }
1716 return( status );
1717}
1718
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001719static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001720 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001721{
1722 psa_status_t status;
1723 uint8_t *buffer = NULL;
1724 size_t buffer_size = 0;
1725 size_t length;
1726
1727 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001728 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001729 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001730 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001731 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001732 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1733 if( status != PSA_SUCCESS )
1734 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001735 target->type = source->type;
1736 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001737
1738exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001739 if( buffer_size != 0 )
1740 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001741 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001742 return( status );
1743}
1744
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001745psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1746 const psa_key_attributes_t *specified_attributes,
1747 psa_key_handle_t *target_handle )
1748{
1749 psa_status_t status;
1750 psa_key_slot_t *source_slot = NULL;
1751 psa_key_slot_t *target_slot = NULL;
1752 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001753 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001754
Gilles Peskine28f8f302019-07-24 13:30:31 +02001755 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001756 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001757 if( status != PSA_SUCCESS )
1758 goto exit;
1759
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001760 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1761 if( status != PSA_SUCCESS )
1762 goto exit;
1763
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001764 status = psa_restrict_key_policy( &actual_attributes.policy,
1765 &source_slot->policy );
1766 if( status != PSA_SUCCESS )
1767 goto exit;
1768
1769 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001770 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001771 if( status != PSA_SUCCESS )
1772 goto exit;
1773
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001774#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1775 if( driver != NULL )
1776 {
1777 /* Copying to a secure element is not implemented yet. */
1778 status = PSA_ERROR_NOT_SUPPORTED;
1779 goto exit;
1780 }
1781#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1782
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001783 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001784 if( status != PSA_SUCCESS )
1785 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001786
Gilles Peskine011e4282019-06-26 18:34:38 +02001787 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001788exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001789 if( status != PSA_SUCCESS )
1790 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001791 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001792 *target_handle = 0;
1793 }
1794 return( status );
1795}
1796
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001797
1798
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001799/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001800/* Message digests */
1801/****************************************************************/
1802
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001803static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001804{
1805 switch( alg )
1806 {
1807#if defined(MBEDTLS_MD2_C)
1808 case PSA_ALG_MD2:
1809 return( &mbedtls_md2_info );
1810#endif
1811#if defined(MBEDTLS_MD4_C)
1812 case PSA_ALG_MD4:
1813 return( &mbedtls_md4_info );
1814#endif
1815#if defined(MBEDTLS_MD5_C)
1816 case PSA_ALG_MD5:
1817 return( &mbedtls_md5_info );
1818#endif
1819#if defined(MBEDTLS_RIPEMD160_C)
1820 case PSA_ALG_RIPEMD160:
1821 return( &mbedtls_ripemd160_info );
1822#endif
1823#if defined(MBEDTLS_SHA1_C)
1824 case PSA_ALG_SHA_1:
1825 return( &mbedtls_sha1_info );
1826#endif
1827#if defined(MBEDTLS_SHA256_C)
1828 case PSA_ALG_SHA_224:
1829 return( &mbedtls_sha224_info );
1830 case PSA_ALG_SHA_256:
1831 return( &mbedtls_sha256_info );
1832#endif
1833#if defined(MBEDTLS_SHA512_C)
1834 case PSA_ALG_SHA_384:
1835 return( &mbedtls_sha384_info );
1836 case PSA_ALG_SHA_512:
1837 return( &mbedtls_sha512_info );
1838#endif
1839 default:
1840 return( NULL );
1841 }
1842}
1843
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001844psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1845{
1846 switch( operation->alg )
1847 {
Gilles Peskine81736312018-06-26 15:04:31 +02001848 case 0:
1849 /* The object has (apparently) been initialized but it is not
1850 * in use. It's ok to call abort on such an object, and there's
1851 * nothing to do. */
1852 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001853#if defined(MBEDTLS_MD2_C)
1854 case PSA_ALG_MD2:
1855 mbedtls_md2_free( &operation->ctx.md2 );
1856 break;
1857#endif
1858#if defined(MBEDTLS_MD4_C)
1859 case PSA_ALG_MD4:
1860 mbedtls_md4_free( &operation->ctx.md4 );
1861 break;
1862#endif
1863#if defined(MBEDTLS_MD5_C)
1864 case PSA_ALG_MD5:
1865 mbedtls_md5_free( &operation->ctx.md5 );
1866 break;
1867#endif
1868#if defined(MBEDTLS_RIPEMD160_C)
1869 case PSA_ALG_RIPEMD160:
1870 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1871 break;
1872#endif
1873#if defined(MBEDTLS_SHA1_C)
1874 case PSA_ALG_SHA_1:
1875 mbedtls_sha1_free( &operation->ctx.sha1 );
1876 break;
1877#endif
1878#if defined(MBEDTLS_SHA256_C)
1879 case PSA_ALG_SHA_224:
1880 case PSA_ALG_SHA_256:
1881 mbedtls_sha256_free( &operation->ctx.sha256 );
1882 break;
1883#endif
1884#if defined(MBEDTLS_SHA512_C)
1885 case PSA_ALG_SHA_384:
1886 case PSA_ALG_SHA_512:
1887 mbedtls_sha512_free( &operation->ctx.sha512 );
1888 break;
1889#endif
1890 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001891 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001892 }
1893 operation->alg = 0;
1894 return( PSA_SUCCESS );
1895}
1896
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001897psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001898 psa_algorithm_t alg )
1899{
1900 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001901
1902 /* A context must be freshly initialized before it can be set up. */
1903 if( operation->alg != 0 )
1904 {
1905 return( PSA_ERROR_BAD_STATE );
1906 }
1907
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001908 switch( alg )
1909 {
1910#if defined(MBEDTLS_MD2_C)
1911 case PSA_ALG_MD2:
1912 mbedtls_md2_init( &operation->ctx.md2 );
1913 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1914 break;
1915#endif
1916#if defined(MBEDTLS_MD4_C)
1917 case PSA_ALG_MD4:
1918 mbedtls_md4_init( &operation->ctx.md4 );
1919 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1920 break;
1921#endif
1922#if defined(MBEDTLS_MD5_C)
1923 case PSA_ALG_MD5:
1924 mbedtls_md5_init( &operation->ctx.md5 );
1925 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1926 break;
1927#endif
1928#if defined(MBEDTLS_RIPEMD160_C)
1929 case PSA_ALG_RIPEMD160:
1930 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1931 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1932 break;
1933#endif
1934#if defined(MBEDTLS_SHA1_C)
1935 case PSA_ALG_SHA_1:
1936 mbedtls_sha1_init( &operation->ctx.sha1 );
1937 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1938 break;
1939#endif
1940#if defined(MBEDTLS_SHA256_C)
1941 case PSA_ALG_SHA_224:
1942 mbedtls_sha256_init( &operation->ctx.sha256 );
1943 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1944 break;
1945 case PSA_ALG_SHA_256:
1946 mbedtls_sha256_init( &operation->ctx.sha256 );
1947 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1948 break;
1949#endif
1950#if defined(MBEDTLS_SHA512_C)
1951 case PSA_ALG_SHA_384:
1952 mbedtls_sha512_init( &operation->ctx.sha512 );
1953 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1954 break;
1955 case PSA_ALG_SHA_512:
1956 mbedtls_sha512_init( &operation->ctx.sha512 );
1957 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1958 break;
1959#endif
1960 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001961 return( PSA_ALG_IS_HASH( alg ) ?
1962 PSA_ERROR_NOT_SUPPORTED :
1963 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001964 }
1965 if( ret == 0 )
1966 operation->alg = alg;
1967 else
1968 psa_hash_abort( operation );
1969 return( mbedtls_to_psa_error( ret ) );
1970}
1971
1972psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1973 const uint8_t *input,
1974 size_t input_length )
1975{
1976 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001977
1978 /* Don't require hash implementations to behave correctly on a
1979 * zero-length input, which may have an invalid pointer. */
1980 if( input_length == 0 )
1981 return( PSA_SUCCESS );
1982
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001983 switch( operation->alg )
1984 {
1985#if defined(MBEDTLS_MD2_C)
1986 case PSA_ALG_MD2:
1987 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1988 input, input_length );
1989 break;
1990#endif
1991#if defined(MBEDTLS_MD4_C)
1992 case PSA_ALG_MD4:
1993 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1994 input, input_length );
1995 break;
1996#endif
1997#if defined(MBEDTLS_MD5_C)
1998 case PSA_ALG_MD5:
1999 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2000 input, input_length );
2001 break;
2002#endif
2003#if defined(MBEDTLS_RIPEMD160_C)
2004 case PSA_ALG_RIPEMD160:
2005 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2006 input, input_length );
2007 break;
2008#endif
2009#if defined(MBEDTLS_SHA1_C)
2010 case PSA_ALG_SHA_1:
2011 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2012 input, input_length );
2013 break;
2014#endif
2015#if defined(MBEDTLS_SHA256_C)
2016 case PSA_ALG_SHA_224:
2017 case PSA_ALG_SHA_256:
2018 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2019 input, input_length );
2020 break;
2021#endif
2022#if defined(MBEDTLS_SHA512_C)
2023 case PSA_ALG_SHA_384:
2024 case PSA_ALG_SHA_512:
2025 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2026 input, input_length );
2027 break;
2028#endif
2029 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002030 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002031 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002032
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002033 if( ret != 0 )
2034 psa_hash_abort( operation );
2035 return( mbedtls_to_psa_error( ret ) );
2036}
2037
2038psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2039 uint8_t *hash,
2040 size_t hash_size,
2041 size_t *hash_length )
2042{
itayzafrir40835d42018-08-02 13:14:17 +03002043 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002044 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002045 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002046
2047 /* Fill the output buffer with something that isn't a valid hash
2048 * (barring an attack on the hash and deliberately-crafted input),
2049 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002050 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002051 /* If hash_size is 0 then hash may be NULL and then the
2052 * call to memset would have undefined behavior. */
2053 if( hash_size != 0 )
2054 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002055
2056 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002057 {
2058 status = PSA_ERROR_BUFFER_TOO_SMALL;
2059 goto exit;
2060 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002061
2062 switch( operation->alg )
2063 {
2064#if defined(MBEDTLS_MD2_C)
2065 case PSA_ALG_MD2:
2066 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2067 break;
2068#endif
2069#if defined(MBEDTLS_MD4_C)
2070 case PSA_ALG_MD4:
2071 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2072 break;
2073#endif
2074#if defined(MBEDTLS_MD5_C)
2075 case PSA_ALG_MD5:
2076 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2077 break;
2078#endif
2079#if defined(MBEDTLS_RIPEMD160_C)
2080 case PSA_ALG_RIPEMD160:
2081 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2082 break;
2083#endif
2084#if defined(MBEDTLS_SHA1_C)
2085 case PSA_ALG_SHA_1:
2086 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2087 break;
2088#endif
2089#if defined(MBEDTLS_SHA256_C)
2090 case PSA_ALG_SHA_224:
2091 case PSA_ALG_SHA_256:
2092 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2093 break;
2094#endif
2095#if defined(MBEDTLS_SHA512_C)
2096 case PSA_ALG_SHA_384:
2097 case PSA_ALG_SHA_512:
2098 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2099 break;
2100#endif
2101 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002102 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002103 }
itayzafrir40835d42018-08-02 13:14:17 +03002104 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002105
itayzafrir40835d42018-08-02 13:14:17 +03002106exit:
2107 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002108 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002109 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002110 return( psa_hash_abort( operation ) );
2111 }
2112 else
2113 {
2114 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002115 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002116 }
2117}
2118
Gilles Peskine2d277862018-06-18 15:41:12 +02002119psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2120 const uint8_t *hash,
2121 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002122{
2123 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2124 size_t actual_hash_length;
2125 psa_status_t status = psa_hash_finish( operation,
2126 actual_hash, sizeof( actual_hash ),
2127 &actual_hash_length );
2128 if( status != PSA_SUCCESS )
2129 return( status );
2130 if( actual_hash_length != hash_length )
2131 return( PSA_ERROR_INVALID_SIGNATURE );
2132 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2133 return( PSA_ERROR_INVALID_SIGNATURE );
2134 return( PSA_SUCCESS );
2135}
2136
Gilles Peskineeb35d782019-01-22 17:56:16 +01002137psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2138 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002139{
2140 if( target_operation->alg != 0 )
2141 return( PSA_ERROR_BAD_STATE );
2142
2143 switch( source_operation->alg )
2144 {
2145 case 0:
2146 return( PSA_ERROR_BAD_STATE );
2147#if defined(MBEDTLS_MD2_C)
2148 case PSA_ALG_MD2:
2149 mbedtls_md2_clone( &target_operation->ctx.md2,
2150 &source_operation->ctx.md2 );
2151 break;
2152#endif
2153#if defined(MBEDTLS_MD4_C)
2154 case PSA_ALG_MD4:
2155 mbedtls_md4_clone( &target_operation->ctx.md4,
2156 &source_operation->ctx.md4 );
2157 break;
2158#endif
2159#if defined(MBEDTLS_MD5_C)
2160 case PSA_ALG_MD5:
2161 mbedtls_md5_clone( &target_operation->ctx.md5,
2162 &source_operation->ctx.md5 );
2163 break;
2164#endif
2165#if defined(MBEDTLS_RIPEMD160_C)
2166 case PSA_ALG_RIPEMD160:
2167 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2168 &source_operation->ctx.ripemd160 );
2169 break;
2170#endif
2171#if defined(MBEDTLS_SHA1_C)
2172 case PSA_ALG_SHA_1:
2173 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2174 &source_operation->ctx.sha1 );
2175 break;
2176#endif
2177#if defined(MBEDTLS_SHA256_C)
2178 case PSA_ALG_SHA_224:
2179 case PSA_ALG_SHA_256:
2180 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2181 &source_operation->ctx.sha256 );
2182 break;
2183#endif
2184#if defined(MBEDTLS_SHA512_C)
2185 case PSA_ALG_SHA_384:
2186 case PSA_ALG_SHA_512:
2187 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2188 &source_operation->ctx.sha512 );
2189 break;
2190#endif
2191 default:
2192 return( PSA_ERROR_NOT_SUPPORTED );
2193 }
2194
2195 target_operation->alg = source_operation->alg;
2196 return( PSA_SUCCESS );
2197}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002198
2199
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002200/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002201/* MAC */
2202/****************************************************************/
2203
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002204static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002205 psa_algorithm_t alg,
2206 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002207 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002208 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002209{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002210 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002211 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002212
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002213 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002214 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002215
Gilles Peskine8c9def32018-02-08 10:02:12 +01002216 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2217 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002218 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002219 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002220 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002221 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002222 mode = MBEDTLS_MODE_STREAM;
2223 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002224 case PSA_ALG_CTR:
2225 mode = MBEDTLS_MODE_CTR;
2226 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002227 case PSA_ALG_CFB:
2228 mode = MBEDTLS_MODE_CFB;
2229 break;
2230 case PSA_ALG_OFB:
2231 mode = MBEDTLS_MODE_OFB;
2232 break;
2233 case PSA_ALG_CBC_NO_PADDING:
2234 mode = MBEDTLS_MODE_CBC;
2235 break;
2236 case PSA_ALG_CBC_PKCS7:
2237 mode = MBEDTLS_MODE_CBC;
2238 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002239 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002240 mode = MBEDTLS_MODE_CCM;
2241 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002242 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002243 mode = MBEDTLS_MODE_GCM;
2244 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002245 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2246 mode = MBEDTLS_MODE_CHACHAPOLY;
2247 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002248 default:
2249 return( NULL );
2250 }
2251 }
2252 else if( alg == PSA_ALG_CMAC )
2253 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002254 else
2255 return( NULL );
2256
2257 switch( key_type )
2258 {
2259 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002260 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002261 break;
2262 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002263 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2264 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002265 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002266 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002267 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002268 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002269 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2270 * but two-key Triple-DES is functionally three-key Triple-DES
2271 * with K1=K3, so that's how we present it to mbedtls. */
2272 if( key_bits == 128 )
2273 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002274 break;
2275 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002276 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002277 break;
2278 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002279 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002280 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002281 case PSA_KEY_TYPE_CHACHA20:
2282 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2283 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002284 default:
2285 return( NULL );
2286 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002287 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002288 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002289
Jaeden Amero23bbb752018-06-26 14:16:54 +01002290 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2291 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002292}
2293
Gilles Peskinea05219c2018-11-16 16:02:56 +01002294#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002295static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002296{
Gilles Peskine2d277862018-06-18 15:41:12 +02002297 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002298 {
2299 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002300 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002301 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002302 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002303 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002304 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002305 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002306 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002307 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002308 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002309 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002310 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002311 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002312 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002313 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002314 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002315 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002316 return( 128 );
2317 default:
2318 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002319 }
2320}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002321#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002322
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002323/* Initialize the MAC operation structure. Once this function has been
2324 * called, psa_mac_abort can run and will do the right thing. */
2325static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2326 psa_algorithm_t alg )
2327{
2328 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2329
2330 operation->alg = alg;
2331 operation->key_set = 0;
2332 operation->iv_set = 0;
2333 operation->iv_required = 0;
2334 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002335 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002336
2337#if defined(MBEDTLS_CMAC_C)
2338 if( alg == PSA_ALG_CMAC )
2339 {
2340 operation->iv_required = 0;
2341 mbedtls_cipher_init( &operation->ctx.cmac );
2342 status = PSA_SUCCESS;
2343 }
2344 else
2345#endif /* MBEDTLS_CMAC_C */
2346#if defined(MBEDTLS_MD_C)
2347 if( PSA_ALG_IS_HMAC( operation->alg ) )
2348 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002349 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2350 operation->ctx.hmac.hash_ctx.alg = 0;
2351 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002352 }
2353 else
2354#endif /* MBEDTLS_MD_C */
2355 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002356 if( ! PSA_ALG_IS_MAC( alg ) )
2357 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002358 }
2359
2360 if( status != PSA_SUCCESS )
2361 memset( operation, 0, sizeof( *operation ) );
2362 return( status );
2363}
2364
Gilles Peskine01126fa2018-07-12 17:04:55 +02002365#if defined(MBEDTLS_MD_C)
2366static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2367{
Gilles Peskine3f108122018-12-07 18:14:53 +01002368 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002369 return( psa_hash_abort( &hmac->hash_ctx ) );
2370}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002371
Janos Follath999f6482019-06-11 12:04:10 +01002372#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002373static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2374{
2375 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2376 memset( hmac, 0, sizeof( *hmac ) );
2377}
Janos Follath999f6482019-06-11 12:04:10 +01002378#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002379#endif /* MBEDTLS_MD_C */
2380
Gilles Peskine8c9def32018-02-08 10:02:12 +01002381psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2382{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002383 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002384 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002385 /* The object has (apparently) been initialized but it is not
2386 * in use. It's ok to call abort on such an object, and there's
2387 * nothing to do. */
2388 return( PSA_SUCCESS );
2389 }
2390 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002391#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002392 if( operation->alg == PSA_ALG_CMAC )
2393 {
2394 mbedtls_cipher_free( &operation->ctx.cmac );
2395 }
2396 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002397#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002398#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002399 if( PSA_ALG_IS_HMAC( operation->alg ) )
2400 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002401 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002402 }
2403 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002404#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002405 {
2406 /* Sanity check (shouldn't happen: operation->alg should
2407 * always have been initialized to a valid value). */
2408 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002409 }
Moran Peker41deec42018-04-04 15:43:05 +03002410
Gilles Peskine8c9def32018-02-08 10:02:12 +01002411 operation->alg = 0;
2412 operation->key_set = 0;
2413 operation->iv_set = 0;
2414 operation->iv_required = 0;
2415 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002416 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002417
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002419
2420bad_state:
2421 /* If abort is called on an uninitialized object, we can't trust
2422 * anything. Wipe the object in case it contains confidential data.
2423 * This may result in a memory leak if a pointer gets overwritten,
2424 * but it's too late to do anything about this. */
2425 memset( operation, 0, sizeof( *operation ) );
2426 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002427}
2428
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002429#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002430static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002431 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002432 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002433 const mbedtls_cipher_info_t *cipher_info )
2434{
2435 int ret;
2436
2437 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002438
2439 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2440 if( ret != 0 )
2441 return( ret );
2442
2443 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2444 slot->data.raw.data,
2445 key_bits );
2446 return( ret );
2447}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002448#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002449
Gilles Peskine248051a2018-06-20 16:09:38 +02002450#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002451static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2452 const uint8_t *key,
2453 size_t key_length,
2454 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002455{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02002456 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002457 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002458 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002459 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002460 psa_status_t status;
2461
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002462 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2463 * overflow below. This should never trigger if the hash algorithm
2464 * is implemented correctly. */
2465 /* The size checks against the ipad and opad buffers cannot be written
2466 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2467 * because that triggers -Wlogical-op on GCC 7.3. */
2468 if( block_size > sizeof( ipad ) )
2469 return( PSA_ERROR_NOT_SUPPORTED );
2470 if( block_size > sizeof( hmac->opad ) )
2471 return( PSA_ERROR_NOT_SUPPORTED );
2472 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002473 return( PSA_ERROR_NOT_SUPPORTED );
2474
Gilles Peskined223b522018-06-11 18:12:58 +02002475 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002476 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002477 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002478 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002479 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002480 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002481 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002482 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002483 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002484 ipad, sizeof( ipad ), &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 Peskine7e454bc2018-06-11 17:26:17 +02002487 }
Gilles Peskine96889972018-07-12 17:07:03 +02002488 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2489 * but it is permitted. It is common when HMAC is used in HKDF, for
2490 * example. Don't call `memcpy` in the 0-length because `key` could be
2491 * an invalid pointer which would make the behavior undefined. */
2492 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002493 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002494
Gilles Peskined223b522018-06-11 18:12:58 +02002495 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2496 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002497 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002498 ipad[i] ^= 0x36;
2499 memset( ipad + key_length, 0x36, block_size - key_length );
2500
2501 /* Copy the key material from ipad to opad, flipping the requisite bits,
2502 * and filling the rest of opad with the requisite constant. */
2503 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002504 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2505 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002506
Gilles Peskine01126fa2018-07-12 17:04:55 +02002507 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002508 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002509 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002510
Gilles Peskine01126fa2018-07-12 17:04:55 +02002511 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002512
2513cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002514 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002515
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002516 return( status );
2517}
Gilles Peskine248051a2018-06-20 16:09:38 +02002518#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002519
Gilles Peskine89167cb2018-07-08 20:12:23 +02002520static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002521 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002522 psa_algorithm_t alg,
2523 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002524{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002525 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002526 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002527 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002528 psa_key_usage_t usage =
2529 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002530 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002531 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002532
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002533 /* A context must be freshly initialized before it can be set up. */
2534 if( operation->alg != 0 )
2535 {
2536 return( PSA_ERROR_BAD_STATE );
2537 }
2538
Gilles Peskined911eb72018-08-14 15:18:45 +02002539 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002540 if( status != PSA_SUCCESS )
2541 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002542 if( is_sign )
2543 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002544
Gilles Peskine28f8f302019-07-24 13:30:31 +02002545 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002546 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002547 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002548 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002549
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002551 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002552 {
2553 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002554 mbedtls_cipher_info_from_psa( full_length_alg,
2555 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002556 int ret;
2557 if( cipher_info == NULL )
2558 {
2559 status = PSA_ERROR_NOT_SUPPORTED;
2560 goto exit;
2561 }
2562 operation->mac_size = cipher_info->block_size;
2563 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2564 status = mbedtls_to_psa_error( ret );
2565 }
2566 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002567#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002568#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002569 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002570 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002571 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002572 if( hash_alg == 0 )
2573 {
2574 status = PSA_ERROR_NOT_SUPPORTED;
2575 goto exit;
2576 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002577
2578 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2579 /* Sanity check. This shouldn't fail on a valid configuration. */
2580 if( operation->mac_size == 0 ||
2581 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2582 {
2583 status = PSA_ERROR_NOT_SUPPORTED;
2584 goto exit;
2585 }
2586
Gilles Peskine01126fa2018-07-12 17:04:55 +02002587 if( slot->type != PSA_KEY_TYPE_HMAC )
2588 {
2589 status = PSA_ERROR_INVALID_ARGUMENT;
2590 goto exit;
2591 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002592
Gilles Peskine01126fa2018-07-12 17:04:55 +02002593 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2594 slot->data.raw.data,
2595 slot->data.raw.bytes,
2596 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002597 }
2598 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002599#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002600 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002601 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002602 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002604
Gilles Peskined911eb72018-08-14 15:18:45 +02002605 if( truncated == 0 )
2606 {
2607 /* The "normal" case: untruncated algorithm. Nothing to do. */
2608 }
2609 else if( truncated < 4 )
2610 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002611 /* A very short MAC is too short for security since it can be
2612 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2613 * so we make this our minimum, even though 32 bits is still
2614 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002615 status = PSA_ERROR_NOT_SUPPORTED;
2616 }
2617 else if( truncated > operation->mac_size )
2618 {
2619 /* It's impossible to "truncate" to a larger length. */
2620 status = PSA_ERROR_INVALID_ARGUMENT;
2621 }
2622 else
2623 operation->mac_size = truncated;
2624
Gilles Peskinefbfac682018-07-08 20:51:54 +02002625exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002626 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002627 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002628 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002629 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002630 else
2631 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002632 operation->key_set = 1;
2633 }
2634 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002635}
2636
Gilles Peskine89167cb2018-07-08 20:12:23 +02002637psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002638 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002639 psa_algorithm_t alg )
2640{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002641 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002642}
2643
2644psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002645 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002646 psa_algorithm_t alg )
2647{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002648 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002649}
2650
Gilles Peskine8c9def32018-02-08 10:02:12 +01002651psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2652 const uint8_t *input,
2653 size_t input_length )
2654{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002655 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002656 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002657 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002658 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002659 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002660 operation->has_input = 1;
2661
Gilles Peskine8c9def32018-02-08 10:02:12 +01002662#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002663 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002664 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002665 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2666 input, input_length );
2667 status = mbedtls_to_psa_error( ret );
2668 }
2669 else
2670#endif /* MBEDTLS_CMAC_C */
2671#if defined(MBEDTLS_MD_C)
2672 if( PSA_ALG_IS_HMAC( operation->alg ) )
2673 {
2674 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2675 input_length );
2676 }
2677 else
2678#endif /* MBEDTLS_MD_C */
2679 {
2680 /* This shouldn't happen if `operation` was initialized by
2681 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002682 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002683 }
2684
Gilles Peskinefbfac682018-07-08 20:51:54 +02002685 if( status != PSA_SUCCESS )
2686 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002687 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002688}
2689
Gilles Peskine01126fa2018-07-12 17:04:55 +02002690#if defined(MBEDTLS_MD_C)
2691static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2692 uint8_t *mac,
2693 size_t mac_size )
2694{
2695 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2696 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2697 size_t hash_size = 0;
2698 size_t block_size = psa_get_hash_block_size( hash_alg );
2699 psa_status_t status;
2700
Gilles Peskine01126fa2018-07-12 17:04:55 +02002701 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2702 if( status != PSA_SUCCESS )
2703 return( status );
2704 /* From here on, tmp needs to be wiped. */
2705
2706 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2707 if( status != PSA_SUCCESS )
2708 goto exit;
2709
2710 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2711 if( status != PSA_SUCCESS )
2712 goto exit;
2713
2714 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2715 if( status != PSA_SUCCESS )
2716 goto exit;
2717
Gilles Peskined911eb72018-08-14 15:18:45 +02002718 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2719 if( status != PSA_SUCCESS )
2720 goto exit;
2721
2722 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002723
2724exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002725 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002726 return( status );
2727}
2728#endif /* MBEDTLS_MD_C */
2729
mohammad16036df908f2018-04-02 08:34:15 -07002730static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002731 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002732 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002733{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002734 if( ! operation->key_set )
2735 return( PSA_ERROR_BAD_STATE );
2736 if( operation->iv_required && ! operation->iv_set )
2737 return( PSA_ERROR_BAD_STATE );
2738
Gilles Peskine8c9def32018-02-08 10:02:12 +01002739 if( mac_size < operation->mac_size )
2740 return( PSA_ERROR_BUFFER_TOO_SMALL );
2741
Gilles Peskine8c9def32018-02-08 10:02:12 +01002742#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002743 if( operation->alg == PSA_ALG_CMAC )
2744 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002745 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2746 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2747 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002748 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002749 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002750 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002751 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002752 else
2753#endif /* MBEDTLS_CMAC_C */
2754#if defined(MBEDTLS_MD_C)
2755 if( PSA_ALG_IS_HMAC( operation->alg ) )
2756 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002757 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002758 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002759 }
2760 else
2761#endif /* MBEDTLS_MD_C */
2762 {
2763 /* This shouldn't happen if `operation` was initialized by
2764 * a setup function. */
2765 return( PSA_ERROR_BAD_STATE );
2766 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002767}
2768
Gilles Peskineacd4be32018-07-08 19:56:25 +02002769psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2770 uint8_t *mac,
2771 size_t mac_size,
2772 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002773{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002774 psa_status_t status;
2775
Jaeden Amero252ef282019-02-15 14:05:35 +00002776 if( operation->alg == 0 )
2777 {
2778 return( PSA_ERROR_BAD_STATE );
2779 }
2780
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002781 /* Fill the output buffer with something that isn't a valid mac
2782 * (barring an attack on the mac and deliberately-crafted input),
2783 * in case the caller doesn't check the return status properly. */
2784 *mac_length = mac_size;
2785 /* If mac_size is 0 then mac may be NULL and then the
2786 * call to memset would have undefined behavior. */
2787 if( mac_size != 0 )
2788 memset( mac, '!', mac_size );
2789
Gilles Peskine89167cb2018-07-08 20:12:23 +02002790 if( ! operation->is_sign )
2791 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002792 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002793 }
mohammad16036df908f2018-04-02 08:34:15 -07002794
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002795 status = psa_mac_finish_internal( operation, mac, mac_size );
2796
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002797 if( status == PSA_SUCCESS )
2798 {
2799 status = psa_mac_abort( operation );
2800 if( status == PSA_SUCCESS )
2801 *mac_length = operation->mac_size;
2802 else
2803 memset( mac, '!', mac_size );
2804 }
2805 else
2806 psa_mac_abort( operation );
2807 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002808}
2809
Gilles Peskineacd4be32018-07-08 19:56:25 +02002810psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2811 const uint8_t *mac,
2812 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002813{
Gilles Peskine828ed142018-06-18 23:25:51 +02002814 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002815 psa_status_t status;
2816
Jaeden Amero252ef282019-02-15 14:05:35 +00002817 if( operation->alg == 0 )
2818 {
2819 return( PSA_ERROR_BAD_STATE );
2820 }
2821
Gilles Peskine89167cb2018-07-08 20:12:23 +02002822 if( operation->is_sign )
2823 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002824 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002825 }
2826 if( operation->mac_size != mac_length )
2827 {
2828 status = PSA_ERROR_INVALID_SIGNATURE;
2829 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002830 }
mohammad16036df908f2018-04-02 08:34:15 -07002831
2832 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002833 actual_mac, sizeof( actual_mac ) );
2834
2835 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2836 status = PSA_ERROR_INVALID_SIGNATURE;
2837
2838cleanup:
2839 if( status == PSA_SUCCESS )
2840 status = psa_mac_abort( operation );
2841 else
2842 psa_mac_abort( operation );
2843
Gilles Peskine3f108122018-12-07 18:14:53 +01002844 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002845
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002846 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002847}
2848
2849
Gilles Peskine20035e32018-02-03 22:44:14 +01002850
Gilles Peskine20035e32018-02-03 22:44:14 +01002851/****************************************************************/
2852/* Asymmetric cryptography */
2853/****************************************************************/
2854
Gilles Peskine2b450e32018-06-27 15:42:46 +02002855#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002856/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002857 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002858static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2859 size_t hash_length,
2860 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002861{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002862 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002863 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002864 *md_alg = mbedtls_md_get_type( md_info );
2865
2866 /* The Mbed TLS RSA module uses an unsigned int for hash length
2867 * parameters. Validate that it fits so that we don't risk an
2868 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002869#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002870 if( hash_length > UINT_MAX )
2871 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002872#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002873
2874#if defined(MBEDTLS_PKCS1_V15)
2875 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2876 * must be correct. */
2877 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2878 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002879 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002880 if( md_info == NULL )
2881 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002882 if( mbedtls_md_get_size( md_info ) != hash_length )
2883 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002884 }
2885#endif /* MBEDTLS_PKCS1_V15 */
2886
2887#if defined(MBEDTLS_PKCS1_V21)
2888 /* PSS requires a hash internally. */
2889 if( PSA_ALG_IS_RSA_PSS( alg ) )
2890 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002891 if( md_info == NULL )
2892 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002893 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002894#endif /* MBEDTLS_PKCS1_V21 */
2895
Gilles Peskine61b91d42018-06-08 16:09:36 +02002896 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002897}
2898
Gilles Peskine2b450e32018-06-27 15:42:46 +02002899static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2900 psa_algorithm_t alg,
2901 const uint8_t *hash,
2902 size_t hash_length,
2903 uint8_t *signature,
2904 size_t signature_size,
2905 size_t *signature_length )
2906{
2907 psa_status_t status;
2908 int ret;
2909 mbedtls_md_type_t md_alg;
2910
2911 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2912 if( status != PSA_SUCCESS )
2913 return( status );
2914
Gilles Peskine630a18a2018-06-29 17:49:35 +02002915 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002916 return( PSA_ERROR_BUFFER_TOO_SMALL );
2917
2918#if defined(MBEDTLS_PKCS1_V15)
2919 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2920 {
2921 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2922 MBEDTLS_MD_NONE );
2923 ret = mbedtls_rsa_pkcs1_sign( rsa,
2924 mbedtls_ctr_drbg_random,
2925 &global_data.ctr_drbg,
2926 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002927 md_alg,
2928 (unsigned int) hash_length,
2929 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002930 signature );
2931 }
2932 else
2933#endif /* MBEDTLS_PKCS1_V15 */
2934#if defined(MBEDTLS_PKCS1_V21)
2935 if( PSA_ALG_IS_RSA_PSS( alg ) )
2936 {
2937 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2938 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2939 mbedtls_ctr_drbg_random,
2940 &global_data.ctr_drbg,
2941 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002942 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002943 (unsigned int) hash_length,
2944 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002945 signature );
2946 }
2947 else
2948#endif /* MBEDTLS_PKCS1_V21 */
2949 {
2950 return( PSA_ERROR_INVALID_ARGUMENT );
2951 }
2952
2953 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002954 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002955 return( mbedtls_to_psa_error( ret ) );
2956}
2957
2958static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2959 psa_algorithm_t alg,
2960 const uint8_t *hash,
2961 size_t hash_length,
2962 const uint8_t *signature,
2963 size_t signature_length )
2964{
2965 psa_status_t status;
2966 int ret;
2967 mbedtls_md_type_t md_alg;
2968
2969 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2970 if( status != PSA_SUCCESS )
2971 return( status );
2972
Gilles Peskine630a18a2018-06-29 17:49:35 +02002973 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002974 return( PSA_ERROR_BUFFER_TOO_SMALL );
2975
2976#if defined(MBEDTLS_PKCS1_V15)
2977 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2978 {
2979 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2980 MBEDTLS_MD_NONE );
2981 ret = mbedtls_rsa_pkcs1_verify( rsa,
2982 mbedtls_ctr_drbg_random,
2983 &global_data.ctr_drbg,
2984 MBEDTLS_RSA_PUBLIC,
2985 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002986 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002987 hash,
2988 signature );
2989 }
2990 else
2991#endif /* MBEDTLS_PKCS1_V15 */
2992#if defined(MBEDTLS_PKCS1_V21)
2993 if( PSA_ALG_IS_RSA_PSS( alg ) )
2994 {
2995 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2996 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2997 mbedtls_ctr_drbg_random,
2998 &global_data.ctr_drbg,
2999 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003000 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003001 (unsigned int) hash_length,
3002 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003003 signature );
3004 }
3005 else
3006#endif /* MBEDTLS_PKCS1_V21 */
3007 {
3008 return( PSA_ERROR_INVALID_ARGUMENT );
3009 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003010
3011 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3012 * the rest of the signature is invalid". This has little use in
3013 * practice and PSA doesn't report this distinction. */
3014 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3015 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003016 return( mbedtls_to_psa_error( ret ) );
3017}
3018#endif /* MBEDTLS_RSA_C */
3019
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003020#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003021/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3022 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3023 * (even though these functions don't modify it). */
3024static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3025 psa_algorithm_t alg,
3026 const uint8_t *hash,
3027 size_t hash_length,
3028 uint8_t *signature,
3029 size_t signature_size,
3030 size_t *signature_length )
3031{
3032 int ret;
3033 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003034 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003035 mbedtls_mpi_init( &r );
3036 mbedtls_mpi_init( &s );
3037
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003038 if( signature_size < 2 * curve_bytes )
3039 {
3040 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3041 goto cleanup;
3042 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003043
Gilles Peskinea05219c2018-11-16 16:02:56 +01003044#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003045 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3046 {
3047 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3048 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3049 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3050 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3051 hash, hash_length,
3052 md_alg ) );
3053 }
3054 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003055#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003056 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003057 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003058 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3059 hash, hash_length,
3060 mbedtls_ctr_drbg_random,
3061 &global_data.ctr_drbg ) );
3062 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003063
3064 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3065 signature,
3066 curve_bytes ) );
3067 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3068 signature + curve_bytes,
3069 curve_bytes ) );
3070
3071cleanup:
3072 mbedtls_mpi_free( &r );
3073 mbedtls_mpi_free( &s );
3074 if( ret == 0 )
3075 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003076 return( mbedtls_to_psa_error( ret ) );
3077}
3078
3079static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3080 const uint8_t *hash,
3081 size_t hash_length,
3082 const uint8_t *signature,
3083 size_t signature_length )
3084{
3085 int ret;
3086 mbedtls_mpi r, s;
3087 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3088 mbedtls_mpi_init( &r );
3089 mbedtls_mpi_init( &s );
3090
3091 if( signature_length != 2 * curve_bytes )
3092 return( PSA_ERROR_INVALID_SIGNATURE );
3093
3094 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3095 signature,
3096 curve_bytes ) );
3097 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3098 signature + curve_bytes,
3099 curve_bytes ) );
3100
3101 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3102 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003103
3104cleanup:
3105 mbedtls_mpi_free( &r );
3106 mbedtls_mpi_free( &s );
3107 return( mbedtls_to_psa_error( ret ) );
3108}
3109#endif /* MBEDTLS_ECDSA_C */
3110
Gilles Peskinec5487a82018-12-03 18:08:14 +01003111psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003112 psa_algorithm_t alg,
3113 const uint8_t *hash,
3114 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003115 uint8_t *signature,
3116 size_t signature_size,
3117 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003118{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003119 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003120 psa_status_t status;
3121
3122 *signature_length = signature_size;
3123
Gilles Peskine28f8f302019-07-24 13:30:31 +02003124 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003125 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003126 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003127 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003128 {
3129 status = PSA_ERROR_INVALID_ARGUMENT;
3130 goto exit;
3131 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003132
Gilles Peskine20035e32018-02-03 22:44:14 +01003133#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003134 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003135 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003136 status = psa_rsa_sign( slot->data.rsa,
3137 alg,
3138 hash, hash_length,
3139 signature, signature_size,
3140 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003141 }
3142 else
3143#endif /* defined(MBEDTLS_RSA_C) */
3144#if defined(MBEDTLS_ECP_C)
3145 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3146 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003147#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003148 if(
3149#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3150 PSA_ALG_IS_ECDSA( alg )
3151#else
3152 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3153#endif
3154 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003155 status = psa_ecdsa_sign( slot->data.ecp,
3156 alg,
3157 hash, hash_length,
3158 signature, signature_size,
3159 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003160 else
3161#endif /* defined(MBEDTLS_ECDSA_C) */
3162 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003163 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003164 }
itayzafrir5c753392018-05-08 11:18:38 +03003165 }
3166 else
3167#endif /* defined(MBEDTLS_ECP_C) */
3168 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003169 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003170 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003171
3172exit:
3173 /* Fill the unused part of the output buffer (the whole buffer on error,
3174 * the trailing part on success) with something that isn't a valid mac
3175 * (barring an attack on the mac and deliberately-crafted input),
3176 * in case the caller doesn't check the return status properly. */
3177 if( status == PSA_SUCCESS )
3178 memset( signature + *signature_length, '!',
3179 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003180 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003181 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003182 /* If signature_size is 0 then we have nothing to do. We must not call
3183 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003184 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003185}
3186
Gilles Peskinec5487a82018-12-03 18:08:14 +01003187psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003188 psa_algorithm_t alg,
3189 const uint8_t *hash,
3190 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003191 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003192 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003193{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003194 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003195 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003196
Gilles Peskine28f8f302019-07-24 13:30:31 +02003197 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003198 if( status != PSA_SUCCESS )
3199 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003200
Gilles Peskine61b91d42018-06-08 16:09:36 +02003201#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003202 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003203 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003204 return( psa_rsa_verify( slot->data.rsa,
3205 alg,
3206 hash, hash_length,
3207 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003208 }
3209 else
3210#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003211#if defined(MBEDTLS_ECP_C)
3212 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3213 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003214#if defined(MBEDTLS_ECDSA_C)
3215 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003216 return( psa_ecdsa_verify( slot->data.ecp,
3217 hash, hash_length,
3218 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003219 else
3220#endif /* defined(MBEDTLS_ECDSA_C) */
3221 {
3222 return( PSA_ERROR_INVALID_ARGUMENT );
3223 }
itayzafrir5c753392018-05-08 11:18:38 +03003224 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003225 else
3226#endif /* defined(MBEDTLS_ECP_C) */
3227 {
3228 return( PSA_ERROR_NOT_SUPPORTED );
3229 }
3230}
3231
Gilles Peskine072ac562018-06-30 00:21:29 +02003232#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3233static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3234 mbedtls_rsa_context *rsa )
3235{
3236 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3237 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3238 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3239 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3240}
3241#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3242
Gilles Peskinec5487a82018-12-03 18:08:14 +01003243psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003244 psa_algorithm_t alg,
3245 const uint8_t *input,
3246 size_t input_length,
3247 const uint8_t *salt,
3248 size_t salt_length,
3249 uint8_t *output,
3250 size_t output_size,
3251 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003252{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003253 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003254 psa_status_t status;
3255
Darryl Green5cc689a2018-07-24 15:34:10 +01003256 (void) input;
3257 (void) input_length;
3258 (void) salt;
3259 (void) output;
3260 (void) output_size;
3261
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003262 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003263
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003264 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3265 return( PSA_ERROR_INVALID_ARGUMENT );
3266
Gilles Peskine28f8f302019-07-24 13:30:31 +02003267 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003268 if( status != PSA_SUCCESS )
3269 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003270 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003271 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003272 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003273
3274#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003275 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003276 {
3277 mbedtls_rsa_context *rsa = slot->data.rsa;
3278 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003279 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003280 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003281#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003282 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003283 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003284 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3285 mbedtls_ctr_drbg_random,
3286 &global_data.ctr_drbg,
3287 MBEDTLS_RSA_PUBLIC,
3288 input_length,
3289 input,
3290 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003291 }
3292 else
3293#endif /* MBEDTLS_PKCS1_V15 */
3294#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003295 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003296 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003297 psa_rsa_oaep_set_padding_mode( alg, rsa );
3298 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3299 mbedtls_ctr_drbg_random,
3300 &global_data.ctr_drbg,
3301 MBEDTLS_RSA_PUBLIC,
3302 salt, salt_length,
3303 input_length,
3304 input,
3305 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003306 }
3307 else
3308#endif /* MBEDTLS_PKCS1_V21 */
3309 {
3310 return( PSA_ERROR_INVALID_ARGUMENT );
3311 }
3312 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003313 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003314 return( mbedtls_to_psa_error( ret ) );
3315 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003316 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003317#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003318 {
3319 return( PSA_ERROR_NOT_SUPPORTED );
3320 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003321}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003322
Gilles Peskinec5487a82018-12-03 18:08:14 +01003323psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003324 psa_algorithm_t alg,
3325 const uint8_t *input,
3326 size_t input_length,
3327 const uint8_t *salt,
3328 size_t salt_length,
3329 uint8_t *output,
3330 size_t output_size,
3331 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003332{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003333 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003334 psa_status_t status;
3335
Darryl Green5cc689a2018-07-24 15:34:10 +01003336 (void) input;
3337 (void) input_length;
3338 (void) salt;
3339 (void) output;
3340 (void) output_size;
3341
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003342 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003343
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003344 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3345 return( PSA_ERROR_INVALID_ARGUMENT );
3346
Gilles Peskine28f8f302019-07-24 13:30:31 +02003347 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003348 if( status != PSA_SUCCESS )
3349 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003350 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003351 return( PSA_ERROR_INVALID_ARGUMENT );
3352
3353#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003354 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003355 {
3356 mbedtls_rsa_context *rsa = slot->data.rsa;
3357 int ret;
3358
Gilles Peskine630a18a2018-06-29 17:49:35 +02003359 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003360 return( PSA_ERROR_INVALID_ARGUMENT );
3361
3362#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003363 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003364 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003365 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3366 mbedtls_ctr_drbg_random,
3367 &global_data.ctr_drbg,
3368 MBEDTLS_RSA_PRIVATE,
3369 output_length,
3370 input,
3371 output,
3372 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003373 }
3374 else
3375#endif /* MBEDTLS_PKCS1_V15 */
3376#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003377 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003378 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003379 psa_rsa_oaep_set_padding_mode( alg, rsa );
3380 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3381 mbedtls_ctr_drbg_random,
3382 &global_data.ctr_drbg,
3383 MBEDTLS_RSA_PRIVATE,
3384 salt, salt_length,
3385 output_length,
3386 input,
3387 output,
3388 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003389 }
3390 else
3391#endif /* MBEDTLS_PKCS1_V21 */
3392 {
3393 return( PSA_ERROR_INVALID_ARGUMENT );
3394 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003395
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003396 return( mbedtls_to_psa_error( ret ) );
3397 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003398 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003399#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003400 {
3401 return( PSA_ERROR_NOT_SUPPORTED );
3402 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003403}
Gilles Peskine20035e32018-02-03 22:44:14 +01003404
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003405
3406
mohammad1603503973b2018-03-12 15:59:30 +02003407/****************************************************************/
3408/* Symmetric cryptography */
3409/****************************************************************/
3410
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003411/* Initialize the cipher operation structure. Once this function has been
3412 * called, psa_cipher_abort can run and will do the right thing. */
3413static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3414 psa_algorithm_t alg )
3415{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003416 if( ! PSA_ALG_IS_CIPHER( alg ) )
3417 {
3418 memset( operation, 0, sizeof( *operation ) );
3419 return( PSA_ERROR_INVALID_ARGUMENT );
3420 }
3421
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003422 operation->alg = alg;
3423 operation->key_set = 0;
3424 operation->iv_set = 0;
3425 operation->iv_required = 1;
3426 operation->iv_size = 0;
3427 operation->block_size = 0;
3428 mbedtls_cipher_init( &operation->ctx.cipher );
3429 return( PSA_SUCCESS );
3430}
3431
Gilles Peskinee553c652018-06-04 16:22:46 +02003432static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003433 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003434 psa_algorithm_t alg,
3435 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003436{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003437 int ret = 0;
3438 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003439 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003440 size_t key_bits;
3441 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003442 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3443 PSA_KEY_USAGE_ENCRYPT :
3444 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003445
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003446 /* A context must be freshly initialized before it can be set up. */
3447 if( operation->alg != 0 )
3448 {
3449 return( PSA_ERROR_BAD_STATE );
3450 }
3451
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003452 status = psa_cipher_init( operation, alg );
3453 if( status != PSA_SUCCESS )
3454 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003455
Gilles Peskine28f8f302019-07-24 13:30:31 +02003456 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003457 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003458 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003459 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003460
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003461 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003462 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003463 {
3464 status = PSA_ERROR_NOT_SUPPORTED;
3465 goto exit;
3466 }
mohammad1603503973b2018-03-12 15:59:30 +02003467
mohammad1603503973b2018-03-12 15:59:30 +02003468 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003469 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003470 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003471
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003472#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003473 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003474 {
3475 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
3476 unsigned char keys[24];
3477 memcpy( keys, slot->data.raw.data, 16 );
3478 memcpy( keys + 16, slot->data.raw.data, 8 );
3479 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3480 keys,
3481 192, cipher_operation );
3482 }
3483 else
3484#endif
3485 {
3486 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3487 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003488 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003489 }
Moran Peker41deec42018-04-04 15:43:05 +03003490 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003491 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003492
mohammad16038481e742018-03-18 13:57:31 +02003493#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003494 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003495 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003496 case PSA_ALG_CBC_NO_PADDING:
3497 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3498 MBEDTLS_PADDING_NONE );
3499 break;
3500 case PSA_ALG_CBC_PKCS7:
3501 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3502 MBEDTLS_PADDING_PKCS7 );
3503 break;
3504 default:
3505 /* The algorithm doesn't involve padding. */
3506 ret = 0;
3507 break;
3508 }
3509 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003510 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003511#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3512
mohammad1603503973b2018-03-12 15:59:30 +02003513 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003514 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3515 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3516 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003517 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003518 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003519 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003520#if defined(MBEDTLS_CHACHA20_C)
3521 else
3522 if( alg == PSA_ALG_CHACHA20 )
3523 operation->iv_size = 12;
3524#endif
mohammad1603503973b2018-03-12 15:59:30 +02003525
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003526exit:
3527 if( status == 0 )
3528 status = mbedtls_to_psa_error( ret );
3529 if( status != 0 )
3530 psa_cipher_abort( operation );
3531 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003532}
3533
Gilles Peskinefe119512018-07-08 21:39:34 +02003534psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003535 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003536 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003537{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003538 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003539}
3540
Gilles Peskinefe119512018-07-08 21:39:34 +02003541psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003542 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003543 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003544{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003545 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003546}
3547
Gilles Peskinefe119512018-07-08 21:39:34 +02003548psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3549 unsigned char *iv,
3550 size_t iv_size,
3551 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003552{
itayzafrir534bd7c2018-08-02 13:56:32 +03003553 psa_status_t status;
3554 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003555 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003556 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003557 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003558 }
Moran Peker41deec42018-04-04 15:43:05 +03003559 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003560 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003561 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003562 goto exit;
3563 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003564 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3565 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003566 if( ret != 0 )
3567 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003568 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003569 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003570 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003571
mohammad16038481e742018-03-18 13:57:31 +02003572 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003573 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003574
Moran Peker395db872018-05-31 14:07:14 +03003575exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003576 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003577 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003578 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003579}
3580
Gilles Peskinefe119512018-07-08 21:39:34 +02003581psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3582 const unsigned char *iv,
3583 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003584{
itayzafrir534bd7c2018-08-02 13:56:32 +03003585 psa_status_t status;
3586 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003587 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003588 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003589 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003590 }
Moran Pekera28258c2018-05-29 16:25:04 +03003591 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003592 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003593 status = PSA_ERROR_INVALID_ARGUMENT;
3594 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003595 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003596 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3597 status = mbedtls_to_psa_error( ret );
3598exit:
3599 if( status == PSA_SUCCESS )
3600 operation->iv_set = 1;
3601 else
mohammad1603503973b2018-03-12 15:59:30 +02003602 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003603 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003604}
3605
Gilles Peskinee553c652018-06-04 16:22:46 +02003606psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3607 const uint8_t *input,
3608 size_t input_length,
3609 unsigned char *output,
3610 size_t output_size,
3611 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003612{
itayzafrir534bd7c2018-08-02 13:56:32 +03003613 psa_status_t status;
3614 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003615 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003616
3617 if( operation->alg == 0 )
3618 {
3619 return( PSA_ERROR_BAD_STATE );
3620 }
3621
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003622 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003623 {
3624 /* Take the unprocessed partial block left over from previous
3625 * update calls, if any, plus the input to this call. Remove
3626 * the last partial block, if any. You get the data that will be
3627 * output in this call. */
3628 expected_output_size =
3629 ( operation->ctx.cipher.unprocessed_len + input_length )
3630 / operation->block_size * operation->block_size;
3631 }
3632 else
3633 {
3634 expected_output_size = input_length;
3635 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003636
Gilles Peskine89d789c2018-06-04 17:17:16 +02003637 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003638 {
3639 status = PSA_ERROR_BUFFER_TOO_SMALL;
3640 goto exit;
3641 }
mohammad160382759612018-03-12 18:16:40 +02003642
mohammad1603503973b2018-03-12 15:59:30 +02003643 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003644 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003645 status = mbedtls_to_psa_error( ret );
3646exit:
3647 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003648 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003649 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003650}
3651
Gilles Peskinee553c652018-06-04 16:22:46 +02003652psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3653 uint8_t *output,
3654 size_t output_size,
3655 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003656{
David Saadab4ecc272019-02-14 13:48:10 +02003657 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003658 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003659 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003660
mohammad1603503973b2018-03-12 15:59:30 +02003661 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003662 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003663 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003664 }
3665 if( operation->iv_required && ! operation->iv_set )
3666 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003667 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003668 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003669
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003670 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003671 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3672 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003673 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003674 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003675 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003676 }
3677
Janos Follath315b51c2018-07-09 16:04:51 +01003678 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3679 temp_output_buffer,
3680 output_length );
3681 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003682 {
Janos Follath315b51c2018-07-09 16:04:51 +01003683 status = mbedtls_to_psa_error( cipher_ret );
3684 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003685 }
Janos Follath315b51c2018-07-09 16:04:51 +01003686
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003687 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003688 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003689 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003690 memcpy( output, temp_output_buffer, *output_length );
3691 else
3692 {
Janos Follath315b51c2018-07-09 16:04:51 +01003693 status = PSA_ERROR_BUFFER_TOO_SMALL;
3694 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003695 }
mohammad1603503973b2018-03-12 15:59:30 +02003696
Gilles Peskine3f108122018-12-07 18:14:53 +01003697 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003698 status = psa_cipher_abort( operation );
3699
3700 return( status );
3701
3702error:
3703
3704 *output_length = 0;
3705
Gilles Peskine3f108122018-12-07 18:14:53 +01003706 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003707 (void) psa_cipher_abort( operation );
3708
3709 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003710}
3711
Gilles Peskinee553c652018-06-04 16:22:46 +02003712psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3713{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003714 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003715 {
3716 /* The object has (apparently) been initialized but it is not
3717 * in use. It's ok to call abort on such an object, and there's
3718 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003719 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003720 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003721
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003722 /* Sanity check (shouldn't happen: operation->alg should
3723 * always have been initialized to a valid value). */
3724 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3725 return( PSA_ERROR_BAD_STATE );
3726
mohammad1603503973b2018-03-12 15:59:30 +02003727 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003728
Moran Peker41deec42018-04-04 15:43:05 +03003729 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003730 operation->key_set = 0;
3731 operation->iv_set = 0;
3732 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003733 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003734 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003735
Moran Peker395db872018-05-31 14:07:14 +03003736 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003737}
3738
Gilles Peskinea0655c32018-04-30 17:06:50 +02003739
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003740
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003741
mohammad16035955c982018-04-26 00:53:03 +03003742/****************************************************************/
3743/* AEAD */
3744/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003745
Gilles Peskineedf9a652018-08-17 18:11:56 +02003746typedef struct
3747{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003748 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003749 const mbedtls_cipher_info_t *cipher_info;
3750 union
3751 {
3752#if defined(MBEDTLS_CCM_C)
3753 mbedtls_ccm_context ccm;
3754#endif /* MBEDTLS_CCM_C */
3755#if defined(MBEDTLS_GCM_C)
3756 mbedtls_gcm_context gcm;
3757#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003758#if defined(MBEDTLS_CHACHAPOLY_C)
3759 mbedtls_chachapoly_context chachapoly;
3760#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003761 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003762 psa_algorithm_t core_alg;
3763 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003764 uint8_t tag_length;
3765} aead_operation_t;
3766
Gilles Peskine30a9e412019-01-14 18:36:12 +01003767static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003768{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003769 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003770 {
3771#if defined(MBEDTLS_CCM_C)
3772 case PSA_ALG_CCM:
3773 mbedtls_ccm_free( &operation->ctx.ccm );
3774 break;
3775#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003776#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003777 case PSA_ALG_GCM:
3778 mbedtls_gcm_free( &operation->ctx.gcm );
3779 break;
3780#endif /* MBEDTLS_GCM_C */
3781 }
3782}
3783
3784static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003785 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003786 psa_key_usage_t usage,
3787 psa_algorithm_t alg )
3788{
3789 psa_status_t status;
3790 size_t key_bits;
3791 mbedtls_cipher_id_t cipher_id;
3792
Gilles Peskine28f8f302019-07-24 13:30:31 +02003793 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003794 if( status != PSA_SUCCESS )
3795 return( status );
3796
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003797 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003798
3799 operation->cipher_info =
3800 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3801 &cipher_id );
3802 if( operation->cipher_info == NULL )
3803 return( PSA_ERROR_NOT_SUPPORTED );
3804
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003805 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003806 {
3807#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003808 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3809 operation->core_alg = PSA_ALG_CCM;
3810 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003811 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3812 * The call to mbedtls_ccm_encrypt_and_tag or
3813 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003814 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3815 return( PSA_ERROR_INVALID_ARGUMENT );
3816 mbedtls_ccm_init( &operation->ctx.ccm );
3817 status = mbedtls_to_psa_error(
3818 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3819 operation->slot->data.raw.data,
3820 (unsigned int) key_bits ) );
3821 if( status != 0 )
3822 goto cleanup;
3823 break;
3824#endif /* MBEDTLS_CCM_C */
3825
3826#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003827 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3828 operation->core_alg = PSA_ALG_GCM;
3829 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003830 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3831 * The call to mbedtls_gcm_crypt_and_tag or
3832 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003833 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3834 return( PSA_ERROR_INVALID_ARGUMENT );
3835 mbedtls_gcm_init( &operation->ctx.gcm );
3836 status = mbedtls_to_psa_error(
3837 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3838 operation->slot->data.raw.data,
3839 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003840 if( status != 0 )
3841 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003842 break;
3843#endif /* MBEDTLS_GCM_C */
3844
Gilles Peskine26869f22019-05-06 15:25:00 +02003845#if defined(MBEDTLS_CHACHAPOLY_C)
3846 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3847 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3848 operation->full_tag_length = 16;
3849 /* We only support the default tag length. */
3850 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3851 return( PSA_ERROR_NOT_SUPPORTED );
3852 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3853 status = mbedtls_to_psa_error(
3854 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3855 operation->slot->data.raw.data ) );
3856 if( status != 0 )
3857 goto cleanup;
3858 break;
3859#endif /* MBEDTLS_CHACHAPOLY_C */
3860
Gilles Peskineedf9a652018-08-17 18:11:56 +02003861 default:
3862 return( PSA_ERROR_NOT_SUPPORTED );
3863 }
3864
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003865 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3866 {
3867 status = PSA_ERROR_INVALID_ARGUMENT;
3868 goto cleanup;
3869 }
3870 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003871
Gilles Peskineedf9a652018-08-17 18:11:56 +02003872 return( PSA_SUCCESS );
3873
3874cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003875 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003876 return( status );
3877}
3878
Gilles Peskinec5487a82018-12-03 18:08:14 +01003879psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003880 psa_algorithm_t alg,
3881 const uint8_t *nonce,
3882 size_t nonce_length,
3883 const uint8_t *additional_data,
3884 size_t additional_data_length,
3885 const uint8_t *plaintext,
3886 size_t plaintext_length,
3887 uint8_t *ciphertext,
3888 size_t ciphertext_size,
3889 size_t *ciphertext_length )
3890{
mohammad16035955c982018-04-26 00:53:03 +03003891 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003892 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003893 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003894
mohammad1603f08a5502018-06-03 15:05:47 +03003895 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003896
Gilles Peskinec5487a82018-12-03 18:08:14 +01003897 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003898 if( status != PSA_SUCCESS )
3899 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003900
Gilles Peskineedf9a652018-08-17 18:11:56 +02003901 /* For all currently supported modes, the tag is at the end of the
3902 * ciphertext. */
3903 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3904 {
3905 status = PSA_ERROR_BUFFER_TOO_SMALL;
3906 goto exit;
3907 }
3908 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003909
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003910#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003911 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003912 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003913 status = mbedtls_to_psa_error(
3914 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3915 MBEDTLS_GCM_ENCRYPT,
3916 plaintext_length,
3917 nonce, nonce_length,
3918 additional_data, additional_data_length,
3919 plaintext, ciphertext,
3920 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003921 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003922 else
3923#endif /* MBEDTLS_GCM_C */
3924#if defined(MBEDTLS_CCM_C)
3925 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003926 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003927 status = mbedtls_to_psa_error(
3928 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3929 plaintext_length,
3930 nonce, nonce_length,
3931 additional_data,
3932 additional_data_length,
3933 plaintext, ciphertext,
3934 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003935 }
mohammad16035c8845f2018-05-09 05:40:09 -07003936 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003937#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003938#if defined(MBEDTLS_CHACHAPOLY_C)
3939 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3940 {
3941 if( nonce_length != 12 || operation.tag_length != 16 )
3942 {
3943 status = PSA_ERROR_NOT_SUPPORTED;
3944 goto exit;
3945 }
3946 status = mbedtls_to_psa_error(
3947 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3948 plaintext_length,
3949 nonce,
3950 additional_data,
3951 additional_data_length,
3952 plaintext,
3953 ciphertext,
3954 tag ) );
3955 }
3956 else
3957#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003958 {
mohammad1603554faad2018-06-03 15:07:38 +03003959 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003960 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003961
Gilles Peskineedf9a652018-08-17 18:11:56 +02003962 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3963 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003964
Gilles Peskineedf9a652018-08-17 18:11:56 +02003965exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003966 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003967 if( status == PSA_SUCCESS )
3968 *ciphertext_length = plaintext_length + operation.tag_length;
3969 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003970}
3971
Gilles Peskineee652a32018-06-01 19:23:52 +02003972/* Locate the tag in a ciphertext buffer containing the encrypted data
3973 * followed by the tag. Return the length of the part preceding the tag in
3974 * *plaintext_length. This is the size of the plaintext in modes where
3975 * the encrypted data has the same size as the plaintext, such as
3976 * CCM and GCM. */
3977static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3978 const uint8_t *ciphertext,
3979 size_t ciphertext_length,
3980 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003981 const uint8_t **p_tag )
3982{
3983 size_t payload_length;
3984 if( tag_length > ciphertext_length )
3985 return( PSA_ERROR_INVALID_ARGUMENT );
3986 payload_length = ciphertext_length - tag_length;
3987 if( payload_length > plaintext_size )
3988 return( PSA_ERROR_BUFFER_TOO_SMALL );
3989 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003990 return( PSA_SUCCESS );
3991}
3992
Gilles Peskinec5487a82018-12-03 18:08:14 +01003993psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003994 psa_algorithm_t alg,
3995 const uint8_t *nonce,
3996 size_t nonce_length,
3997 const uint8_t *additional_data,
3998 size_t additional_data_length,
3999 const uint8_t *ciphertext,
4000 size_t ciphertext_length,
4001 uint8_t *plaintext,
4002 size_t plaintext_size,
4003 size_t *plaintext_length )
4004{
mohammad16035955c982018-04-26 00:53:03 +03004005 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004006 aead_operation_t operation;
4007 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004008
Gilles Peskineee652a32018-06-01 19:23:52 +02004009 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004010
Gilles Peskinec5487a82018-12-03 18:08:14 +01004011 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004012 if( status != PSA_SUCCESS )
4013 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004014
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004015 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4016 ciphertext, ciphertext_length,
4017 plaintext_size, &tag );
4018 if( status != PSA_SUCCESS )
4019 goto exit;
4020
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004021#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004022 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004023 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004024 status = mbedtls_to_psa_error(
4025 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4026 ciphertext_length - operation.tag_length,
4027 nonce, nonce_length,
4028 additional_data,
4029 additional_data_length,
4030 tag, operation.tag_length,
4031 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004032 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004033 else
4034#endif /* MBEDTLS_GCM_C */
4035#if defined(MBEDTLS_CCM_C)
4036 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004037 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004038 status = mbedtls_to_psa_error(
4039 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4040 ciphertext_length - operation.tag_length,
4041 nonce, nonce_length,
4042 additional_data,
4043 additional_data_length,
4044 ciphertext, plaintext,
4045 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004046 }
mohammad160339574652018-06-01 04:39:53 -07004047 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004048#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004049#if defined(MBEDTLS_CHACHAPOLY_C)
4050 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4051 {
4052 if( nonce_length != 12 || operation.tag_length != 16 )
4053 {
4054 status = PSA_ERROR_NOT_SUPPORTED;
4055 goto exit;
4056 }
4057 status = mbedtls_to_psa_error(
4058 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4059 ciphertext_length - operation.tag_length,
4060 nonce,
4061 additional_data,
4062 additional_data_length,
4063 tag,
4064 ciphertext,
4065 plaintext ) );
4066 }
4067 else
4068#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004069 {
mohammad1603554faad2018-06-03 15:07:38 +03004070 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004071 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004072
Gilles Peskineedf9a652018-08-17 18:11:56 +02004073 if( status != PSA_SUCCESS && plaintext_size != 0 )
4074 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004075
Gilles Peskineedf9a652018-08-17 18:11:56 +02004076exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004077 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004078 if( status == PSA_SUCCESS )
4079 *plaintext_length = ciphertext_length - operation.tag_length;
4080 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004081}
4082
Gilles Peskinea0655c32018-04-30 17:06:50 +02004083
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004084
Gilles Peskine20035e32018-02-03 22:44:14 +01004085/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004086/* Generators */
4087/****************************************************************/
4088
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004089#define HKDF_STATE_INIT 0 /* no input yet */
4090#define HKDF_STATE_STARTED 1 /* got salt */
4091#define HKDF_STATE_KEYED 2 /* got key */
4092#define HKDF_STATE_OUTPUT 3 /* output started */
4093
Gilles Peskinecbe66502019-05-16 16:59:18 +02004094static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004095 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004096{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004097 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4098 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004099 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004100 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004101}
4102
4103
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004104psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004105{
4106 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004107 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004108 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004109 {
4110 /* The object has (apparently) been initialized but it is not
4111 * in use. It's ok to call abort on such an object, and there's
4112 * nothing to do. */
4113 }
4114 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004115#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004116 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004117 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004118 mbedtls_free( operation->ctx.hkdf.info );
4119 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004120 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004121 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004122 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004123 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004124 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004125#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004126 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004127 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004128 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4129 operation->ctx.tls12_prf.key_len );
4130 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004131 }
Hanno Becker580fba12018-11-13 20:50:45 +00004132
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004133 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004134 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004135 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4136 operation->ctx.tls12_prf.Ai_with_seed_len );
4137 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004138 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004139#else
4140 if( operation->ctx.tls12_prf.seed != NULL )
4141 {
4142 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4143 operation->ctx.tls12_prf.seed_length );
4144 mbedtls_free( operation->ctx.tls12_prf.seed );
4145 }
4146
4147 if( operation->ctx.tls12_prf.label != NULL )
4148 {
4149 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4150 operation->ctx.tls12_prf.label_length );
4151 mbedtls_free( operation->ctx.tls12_prf.label );
4152 }
4153
4154 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4155
4156 /* We leave the fields Ai and output_block to be erased safely by the
4157 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004158#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004159 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004160 else
4161#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004162 {
4163 status = PSA_ERROR_BAD_STATE;
4164 }
Janos Follath083036a2019-06-11 10:22:26 +01004165 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004166 return( status );
4167}
4168
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004169psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004170 size_t *capacity)
4171{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004172 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004173 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004174 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004175 return PSA_ERROR_BAD_STATE;
4176 }
4177
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004178 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004179 return( PSA_SUCCESS );
4180}
4181
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004182psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004183 size_t capacity )
4184{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004185 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004186 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004187 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004188 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004189 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004190 return( PSA_SUCCESS );
4191}
4192
Gilles Peskinebef7f142018-07-12 17:22:21 +02004193#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004194/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004195 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004196static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004197 psa_algorithm_t hash_alg,
4198 uint8_t *output,
4199 size_t output_length )
4200{
4201 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4202 psa_status_t status;
4203
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004204 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4205 return( PSA_ERROR_BAD_STATE );
4206 hkdf->state = HKDF_STATE_OUTPUT;
4207
Gilles Peskinebef7f142018-07-12 17:22:21 +02004208 while( output_length != 0 )
4209 {
4210 /* Copy what remains of the current block */
4211 uint8_t n = hash_length - hkdf->offset_in_block;
4212 if( n > output_length )
4213 n = (uint8_t) output_length;
4214 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4215 output += n;
4216 output_length -= n;
4217 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004218 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004219 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004220 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004221 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004222 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004223 * object was corrupted or if this function is called directly
4224 * inside the library. */
4225 if( hkdf->block_number == 0xff )
4226 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004227
4228 /* We need a new block */
4229 ++hkdf->block_number;
4230 hkdf->offset_in_block = 0;
4231 status = psa_hmac_setup_internal( &hkdf->hmac,
4232 hkdf->prk, hash_length,
4233 hash_alg );
4234 if( status != PSA_SUCCESS )
4235 return( status );
4236 if( hkdf->block_number != 1 )
4237 {
4238 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4239 hkdf->output_block,
4240 hash_length );
4241 if( status != PSA_SUCCESS )
4242 return( status );
4243 }
4244 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4245 hkdf->info,
4246 hkdf->info_length );
4247 if( status != PSA_SUCCESS )
4248 return( status );
4249 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4250 &hkdf->block_number, 1 );
4251 if( status != PSA_SUCCESS )
4252 return( status );
4253 status = psa_hmac_finish_internal( &hkdf->hmac,
4254 hkdf->output_block,
4255 sizeof( hkdf->output_block ) );
4256 if( status != PSA_SUCCESS )
4257 return( status );
4258 }
4259
4260 return( PSA_SUCCESS );
4261}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004262
Janos Follath999f6482019-06-11 12:04:10 +01004263#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004264static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4265 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004266 psa_algorithm_t alg )
4267{
4268 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4269 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4270 psa_hmac_internal_data hmac;
4271 psa_status_t status, cleanup_status;
4272
Hanno Becker3b339e22018-11-13 20:56:14 +00004273 unsigned char *Ai;
4274 size_t Ai_len;
4275
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004276 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004277 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004278 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004279 * object was corrupted or if this function is called directly
4280 * inside the library. */
4281 if( tls12_prf->block_number == 0xff )
4282 return( PSA_ERROR_BAD_STATE );
4283
4284 /* We need a new block */
4285 ++tls12_prf->block_number;
4286 tls12_prf->offset_in_block = 0;
4287
4288 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4289 *
4290 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4291 *
4292 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4293 * HMAC_hash(secret, A(2) + seed) +
4294 * HMAC_hash(secret, A(3) + seed) + ...
4295 *
4296 * A(0) = seed
4297 * A(i) = HMAC_hash( secret, A(i-1) )
4298 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004299 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004300 * `HMAC_hash(secret, A(i) + seed)` from which the output
4301 * is currently extracted as `output_block`, while
4302 * `A(i) + seed` is stored in `Ai_with_seed`.
4303 *
4304 * Generating a new block means recalculating `Ai_with_seed`
4305 * from the A(i)-part of it, and afterwards recalculating
4306 * `output_block`.
4307 *
4308 * A(0) is computed at setup time.
4309 *
4310 */
4311
4312 psa_hmac_init_internal( &hmac );
4313
4314 /* We must distinguish the calculation of A(1) from those
4315 * of A(2) and higher, because A(0)=seed has a different
4316 * length than the other A(i). */
4317 if( tls12_prf->block_number == 1 )
4318 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004319 Ai = tls12_prf->Ai_with_seed + hash_length;
4320 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004321 }
4322 else
4323 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004324 Ai = tls12_prf->Ai_with_seed;
4325 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004326 }
4327
Hanno Becker3b339e22018-11-13 20:56:14 +00004328 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4329 status = psa_hmac_setup_internal( &hmac,
4330 tls12_prf->key,
4331 tls12_prf->key_len,
4332 hash_alg );
4333 if( status != PSA_SUCCESS )
4334 goto cleanup;
4335
4336 status = psa_hash_update( &hmac.hash_ctx,
4337 Ai, Ai_len );
4338 if( status != PSA_SUCCESS )
4339 goto cleanup;
4340
4341 status = psa_hmac_finish_internal( &hmac,
4342 tls12_prf->Ai_with_seed,
4343 hash_length );
4344 if( status != PSA_SUCCESS )
4345 goto cleanup;
4346
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004347 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4348 status = psa_hmac_setup_internal( &hmac,
4349 tls12_prf->key,
4350 tls12_prf->key_len,
4351 hash_alg );
4352 if( status != PSA_SUCCESS )
4353 goto cleanup;
4354
4355 status = psa_hash_update( &hmac.hash_ctx,
4356 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004357 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004358 if( status != PSA_SUCCESS )
4359 goto cleanup;
4360
4361 status = psa_hmac_finish_internal( &hmac,
4362 tls12_prf->output_block,
4363 hash_length );
4364 if( status != PSA_SUCCESS )
4365 goto cleanup;
4366
4367cleanup:
4368
4369 cleanup_status = psa_hmac_abort_internal( &hmac );
4370 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4371 status = cleanup_status;
4372
4373 return( status );
4374}
Janos Follath7742fee2019-06-17 12:58:10 +01004375#else
4376static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4377 psa_tls12_prf_key_derivation_t *tls12_prf,
4378 psa_algorithm_t alg )
4379{
4380 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4381 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004382 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4383 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004384
Janos Follath7742fee2019-06-17 12:58:10 +01004385 /* We can't be wanting more output after block 0xff, otherwise
4386 * the capacity check in psa_key_derivation_output_bytes() would have
4387 * prevented this call. It could happen only if the operation
4388 * object was corrupted or if this function is called directly
4389 * inside the library. */
4390 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004391 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004392
4393 /* We need a new block */
4394 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004395 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004396
4397 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4398 *
4399 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4400 *
4401 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4402 * HMAC_hash(secret, A(2) + seed) +
4403 * HMAC_hash(secret, A(3) + seed) + ...
4404 *
4405 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004406 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004407 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004408 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004409 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004410 * is currently extracted as `output_block` and where i is
4411 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004412 */
4413
Janos Follathea29bfb2019-06-19 12:21:20 +01004414 /* Save the hash context before using it, to preserve the hash state with
4415 * only the inner padding in it. We need this, because inner padding depends
4416 * on the key (secret in the RFC's terminology). */
4417 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4418 if( status != PSA_SUCCESS )
4419 goto cleanup;
4420
4421 /* Calculate A(i) where i = tls12_prf->block_number. */
4422 if( tls12_prf->block_number == 1 )
4423 {
4424 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4425 * the variable seed and in this instance means it in the context of the
4426 * P_hash function, where seed = label + seed.) */
4427 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4428 tls12_prf->label, tls12_prf->label_length );
4429 if( status != PSA_SUCCESS )
4430 goto cleanup;
4431 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4432 tls12_prf->seed, tls12_prf->seed_length );
4433 if( status != PSA_SUCCESS )
4434 goto cleanup;
4435 }
4436 else
4437 {
4438 /* A(i) = HMAC_hash(secret, A(i-1)) */
4439 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4440 tls12_prf->Ai, hash_length );
4441 if( status != PSA_SUCCESS )
4442 goto cleanup;
4443 }
4444
4445 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4446 tls12_prf->Ai, hash_length );
4447 if( status != PSA_SUCCESS )
4448 goto cleanup;
4449 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4450 if( status != PSA_SUCCESS )
4451 goto cleanup;
4452
4453 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4454 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4455 tls12_prf->Ai, hash_length );
4456 if( status != PSA_SUCCESS )
4457 goto cleanup;
4458 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4459 tls12_prf->label, tls12_prf->label_length );
4460 if( status != PSA_SUCCESS )
4461 goto cleanup;
4462 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4463 tls12_prf->seed, tls12_prf->seed_length );
4464 if( status != PSA_SUCCESS )
4465 goto cleanup;
4466 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4467 tls12_prf->output_block, hash_length );
4468 if( status != PSA_SUCCESS )
4469 goto cleanup;
4470 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4471 if( status != PSA_SUCCESS )
4472 goto cleanup;
4473
Janos Follath7742fee2019-06-17 12:58:10 +01004474
4475cleanup:
4476
Janos Follathea29bfb2019-06-19 12:21:20 +01004477 cleanup_status = psa_hash_abort( &backup );
4478 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4479 status = cleanup_status;
4480
4481 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004482}
Janos Follath999f6482019-06-11 12:04:10 +01004483#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004484
Janos Follath999f6482019-06-11 12:04:10 +01004485#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004487 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004488static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004489 psa_tls12_prf_key_derivation_t *tls12_prf,
4490 psa_algorithm_t alg,
4491 uint8_t *output,
4492 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004493{
4494 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4495 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4496 psa_status_t status;
4497
4498 while( output_length != 0 )
4499 {
4500 /* Copy what remains of the current block */
4501 uint8_t n = hash_length - tls12_prf->offset_in_block;
4502
4503 /* Check if we have fully processed the current block. */
4504 if( n == 0 )
4505 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004506 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004507 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004508 if( status != PSA_SUCCESS )
4509 return( status );
4510
4511 continue;
4512 }
4513
4514 if( n > output_length )
4515 n = (uint8_t) output_length;
4516 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4517 n );
4518 output += n;
4519 output_length -= n;
4520 tls12_prf->offset_in_block += n;
4521 }
4522
4523 return( PSA_SUCCESS );
4524}
Janos Follath844eb0e2019-06-19 12:10:49 +01004525#else
4526static psa_status_t psa_key_derivation_tls12_prf_read(
4527 psa_tls12_prf_key_derivation_t *tls12_prf,
4528 psa_algorithm_t alg,
4529 uint8_t *output,
4530 size_t output_length )
4531{
4532 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4533 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4534 psa_status_t status;
4535 uint8_t offset, length;
4536
4537 while( output_length != 0 )
4538 {
4539 /* Check if we have fully processed the current block. */
4540 if( tls12_prf->left_in_block == 0 )
4541 {
4542 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4543 alg );
4544 if( status != PSA_SUCCESS )
4545 return( status );
4546
4547 continue;
4548 }
4549
4550 if( tls12_prf->left_in_block > output_length )
4551 length = (uint8_t) output_length;
4552 else
4553 length = tls12_prf->left_in_block;
4554
4555 offset = hash_length - tls12_prf->left_in_block;
4556 memcpy( output, tls12_prf->output_block + offset, length );
4557 output += length;
4558 output_length -= length;
4559 tls12_prf->left_in_block -= length;
4560 }
4561
4562 return( PSA_SUCCESS );
4563}
Janos Follath999f6482019-06-11 12:04:10 +01004564#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004565#endif /* MBEDTLS_MD_C */
4566
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004567psa_status_t psa_key_derivation_output_bytes(
4568 psa_key_derivation_operation_t *operation,
4569 uint8_t *output,
4570 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004571{
4572 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004573 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004574
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004575 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004576 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004577 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004578 return PSA_ERROR_BAD_STATE;
4579 }
4580
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004581 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004582 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004583 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004584 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004585 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004586 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004587 goto exit;
4588 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004589 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004590 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004591 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004592 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004593 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4594 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004595 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004596 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004597 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004598 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004599 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004600
Gilles Peskinebef7f142018-07-12 17:22:21 +02004601#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004602 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004603 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004604 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004605 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004606 output, output_length );
4607 }
Janos Follathadbec812019-06-14 11:05:39 +01004608 else
Janos Follathadbec812019-06-14 11:05:39 +01004609 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004610 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004611 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004612 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004613 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004614 output_length );
4615 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004616 else
4617#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004618 {
4619 return( PSA_ERROR_BAD_STATE );
4620 }
4621
4622exit:
4623 if( status != PSA_SUCCESS )
4624 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004625 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004626 * This allows us to differentiate between exhausted operations and
4627 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4628 * operations. */
4629 psa_algorithm_t alg = operation->alg;
4630 psa_key_derivation_abort( operation );
4631 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004632 memset( output, '!', output_length );
4633 }
4634 return( status );
4635}
4636
Gilles Peskine08542d82018-07-19 17:05:42 +02004637#if defined(MBEDTLS_DES_C)
4638static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4639{
4640 if( data_size >= 8 )
4641 mbedtls_des_key_set_parity( data );
4642 if( data_size >= 16 )
4643 mbedtls_des_key_set_parity( data + 8 );
4644 if( data_size >= 24 )
4645 mbedtls_des_key_set_parity( data + 16 );
4646}
4647#endif /* MBEDTLS_DES_C */
4648
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004649static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004650 psa_key_slot_t *slot,
4651 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004652 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004653{
4654 uint8_t *data = NULL;
4655 size_t bytes = PSA_BITS_TO_BYTES( bits );
4656 psa_status_t status;
4657
4658 if( ! key_type_is_raw_bytes( slot->type ) )
4659 return( PSA_ERROR_INVALID_ARGUMENT );
4660 if( bits % 8 != 0 )
4661 return( PSA_ERROR_INVALID_ARGUMENT );
4662 data = mbedtls_calloc( 1, bytes );
4663 if( data == NULL )
4664 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4665
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004666 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004667 if( status != PSA_SUCCESS )
4668 goto exit;
4669#if defined(MBEDTLS_DES_C)
4670 if( slot->type == PSA_KEY_TYPE_DES )
4671 psa_des_set_key_parity( data, bytes );
4672#endif /* MBEDTLS_DES_C */
4673 status = psa_import_key_into_slot( slot, data, bytes );
4674
4675exit:
4676 mbedtls_free( data );
4677 return( status );
4678}
4679
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004680psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004681 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004682 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004683{
4684 psa_status_t status;
4685 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004686 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004687 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004688#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4689 if( driver != NULL )
4690 {
4691 /* Deriving a key in a secure element is not implemented yet. */
4692 status = PSA_ERROR_NOT_SUPPORTED;
4693 }
4694#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004695 if( status == PSA_SUCCESS )
4696 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004697 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004698 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004699 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004700 }
4701 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004702 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004703 if( status != PSA_SUCCESS )
4704 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004705 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004706 *handle = 0;
4707 }
4708 return( status );
4709}
4710
Gilles Peskineeab56e42018-07-12 17:12:33 +02004711
4712
4713/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004714/* Key derivation */
4715/****************************************************************/
4716
Gilles Peskinea05219c2018-11-16 16:02:56 +01004717#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004718#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004719/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004720 * of the HKDF algorithm.
4721 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004722 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004723 * to potentially free embedded data structures and wipe confidential data.
4724 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004725static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004726 const uint8_t *secret,
4727 size_t secret_length,
4728 psa_algorithm_t hash_alg,
4729 const uint8_t *salt,
4730 size_t salt_length,
4731 const uint8_t *label,
4732 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004733{
4734 psa_status_t status;
4735 status = psa_hmac_setup_internal( &hkdf->hmac,
4736 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004737 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004738 if( status != PSA_SUCCESS )
4739 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004740 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004741 if( status != PSA_SUCCESS )
4742 return( status );
4743 status = psa_hmac_finish_internal( &hkdf->hmac,
4744 hkdf->prk,
4745 sizeof( hkdf->prk ) );
4746 if( status != PSA_SUCCESS )
4747 return( status );
4748 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4749 hkdf->block_number = 0;
4750 hkdf->info_length = label_length;
4751 if( label_length != 0 )
4752 {
4753 hkdf->info = mbedtls_calloc( 1, label_length );
4754 if( hkdf->info == NULL )
4755 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4756 memcpy( hkdf->info, label, label_length );
4757 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004758 hkdf->state = HKDF_STATE_KEYED;
4759 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004760 return( PSA_SUCCESS );
4761}
Janos Follath71a4c912019-06-11 09:14:47 +01004762#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004763#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004764
Gilles Peskinea05219c2018-11-16 16:02:56 +01004765#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004766#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004767/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004768 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004769 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004770 * to potentially free embedded data structures and wipe confidential data.
4771 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004772static psa_status_t psa_key_derivation_tls12_prf_setup(
4773 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004774 const unsigned char *key,
4775 size_t key_len,
4776 psa_algorithm_t hash_alg,
4777 const uint8_t *salt,
4778 size_t salt_length,
4779 const uint8_t *label,
4780 size_t label_length )
4781{
4782 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004783 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4784 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004785
4786 tls12_prf->key = mbedtls_calloc( 1, key_len );
4787 if( tls12_prf->key == NULL )
4788 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4789 tls12_prf->key_len = key_len;
4790 memcpy( tls12_prf->key, key, key_len );
4791
Hanno Becker580fba12018-11-13 20:50:45 +00004792 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004793 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004794 if( overflow )
4795 return( PSA_ERROR_INVALID_ARGUMENT );
4796
4797 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4798 if( tls12_prf->Ai_with_seed == NULL )
4799 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4800 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4801
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004802 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4803 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004804 if( label_length != 0 )
4805 {
4806 memcpy( tls12_prf->Ai_with_seed + hash_length,
4807 label, label_length );
4808 }
4809
4810 if( salt_length != 0 )
4811 {
4812 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4813 salt, salt_length );
4814 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004815
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004816 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004817 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004818 tls12_prf->block_number = 0;
4819 tls12_prf->offset_in_block = hash_length;
4820
4821 return( PSA_SUCCESS );
4822}
Janos Follath71a4c912019-06-11 09:14:47 +01004823#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004824
Janos Follath71a4c912019-06-11 09:14:47 +01004825#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004826/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004827static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4828 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004829 const unsigned char *psk,
4830 size_t psk_len,
4831 psa_algorithm_t hash_alg,
4832 const uint8_t *salt,
4833 size_t salt_length,
4834 const uint8_t *label,
4835 size_t label_length )
4836{
4837 psa_status_t status;
4838 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4839
4840 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4841 return( PSA_ERROR_INVALID_ARGUMENT );
4842
4843 /* Quoting RFC 4279, Section 2:
4844 *
4845 * The premaster secret is formed as follows: if the PSK is N octets
4846 * long, concatenate a uint16 with the value N, N zero octets, a second
4847 * uint16 with the value N, and the PSK itself.
4848 */
4849
4850 pms[0] = ( psk_len >> 8 ) & 0xff;
4851 pms[1] = ( psk_len >> 0 ) & 0xff;
4852 memset( pms + 2, 0, psk_len );
4853 pms[2 + psk_len + 0] = pms[0];
4854 pms[2 + psk_len + 1] = pms[1];
4855 memcpy( pms + 4 + psk_len, psk, psk_len );
4856
Gilles Peskinecbe66502019-05-16 16:59:18 +02004857 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004858 pms, 4 + 2 * psk_len,
4859 hash_alg,
4860 salt, salt_length,
4861 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004862
Gilles Peskine3f108122018-12-07 18:14:53 +01004863 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004864 return( status );
4865}
Janos Follath71a4c912019-06-11 09:14:47 +01004866#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004867#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004868
Janos Follath71a4c912019-06-11 09:14:47 +01004869#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004870/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004871 * to potentially free embedded data structures and wipe confidential data.
4872 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004873static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004874 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004875 const uint8_t *secret, size_t secret_length,
4876 psa_algorithm_t alg,
4877 const uint8_t *salt, size_t salt_length,
4878 const uint8_t *label, size_t label_length,
4879 size_t capacity )
4880{
4881 psa_status_t status;
4882 size_t max_capacity;
4883
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004884 /* Set operation->alg even on failure so that abort knows what to do. */
4885 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004886
4887#if defined(MBEDTLS_MD_C)
4888 if( PSA_ALG_IS_HKDF( alg ) )
4889 {
4890 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4891 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4892 if( hash_size == 0 )
4893 return( PSA_ERROR_NOT_SUPPORTED );
4894 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004895 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004896 secret, secret_length,
4897 hash_alg,
4898 salt, salt_length,
4899 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004900 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004901 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4902 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4903 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004904 {
4905 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4906 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4907
4908 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4909 if( hash_alg != PSA_ALG_SHA_256 &&
4910 hash_alg != PSA_ALG_SHA_384 )
4911 {
4912 return( PSA_ERROR_NOT_SUPPORTED );
4913 }
4914
4915 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004916
4917 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4918 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004920 secret, secret_length,
4921 hash_alg, salt, salt_length,
4922 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004923 }
4924 else
4925 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004926 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004927 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004928 secret, secret_length,
4929 hash_alg, salt, salt_length,
4930 label, label_length );
4931 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004932 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004933 else
4934#endif
4935 {
4936 return( PSA_ERROR_NOT_SUPPORTED );
4937 }
4938
4939 if( status != PSA_SUCCESS )
4940 return( status );
4941
4942 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004943 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004944 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004945 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004946 else
4947 return( PSA_ERROR_INVALID_ARGUMENT );
4948
4949 return( PSA_SUCCESS );
4950}
Janos Follath71a4c912019-06-11 09:14:47 +01004951#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004952
Janos Follath71a4c912019-06-11 09:14:47 +01004953#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004954psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004955 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004956 psa_algorithm_t alg,
4957 const uint8_t *salt,
4958 size_t salt_length,
4959 const uint8_t *label,
4960 size_t label_length,
4961 size_t capacity )
4962{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004963 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004964 psa_status_t status;
4965
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004967 return( PSA_ERROR_BAD_STATE );
4968
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004969 /* Make sure that alg is a key derivation algorithm. This prevents
4970 * key selection algorithms, which psa_key_derivation_internal
4971 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004972 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4973 return( PSA_ERROR_INVALID_ARGUMENT );
4974
Gilles Peskine28f8f302019-07-24 13:30:31 +02004975 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004976 if( status != PSA_SUCCESS )
4977 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004978
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004979 if( slot->type != PSA_KEY_TYPE_DERIVE )
4980 return( PSA_ERROR_INVALID_ARGUMENT );
4981
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004982 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004983 slot->data.raw.data,
4984 slot->data.raw.bytes,
4985 alg,
4986 salt, salt_length,
4987 label, label_length,
4988 capacity );
4989 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004990 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004991 return( status );
4992}
Janos Follath71a4c912019-06-11 09:14:47 +01004993#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004994
Gilles Peskine969c5d62019-01-16 15:53:06 +01004995static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004996 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004997 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004998{
Janos Follath5fe19732019-06-20 15:09:30 +01004999 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5000 * initialisers for this union leave some bytes unspecified.) */
5001 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5002
Gilles Peskine969c5d62019-01-16 15:53:06 +01005003 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005004#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005005 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5006 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5007 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005008 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005009 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005010 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5011 if( hash_size == 0 )
5012 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005013 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5014 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005015 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005016 {
5017 return( PSA_ERROR_NOT_SUPPORTED );
5018 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005019 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005020 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005021 }
5022#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005023 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005024 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005025}
5026
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005027psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005028 psa_algorithm_t alg )
5029{
5030 psa_status_t status;
5031
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005033 return( PSA_ERROR_BAD_STATE );
5034
Gilles Peskine6843c292019-01-18 16:44:49 +01005035 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5036 return( PSA_ERROR_INVALID_ARGUMENT );
5037 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005038 {
5039 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005040 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005041 }
5042 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5043 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005044 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005045 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005046 else
5047 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005048
5049 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005050 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005051 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005052}
5053
5054#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005055static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005056 psa_algorithm_t hash_alg,
5057 psa_key_derivation_step_t step,
5058 const uint8_t *data,
5059 size_t data_length )
5060{
5061 psa_status_t status;
5062 switch( step )
5063 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005064 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005065 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005066 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005067 status = psa_hmac_setup_internal( &hkdf->hmac,
5068 data, data_length,
5069 hash_alg );
5070 if( status != PSA_SUCCESS )
5071 return( status );
5072 hkdf->state = HKDF_STATE_STARTED;
5073 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005074 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005075 /* If no salt was provided, use an empty salt. */
5076 if( hkdf->state == HKDF_STATE_INIT )
5077 {
5078 status = psa_hmac_setup_internal( &hkdf->hmac,
5079 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005080 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005081 if( status != PSA_SUCCESS )
5082 return( status );
5083 hkdf->state = HKDF_STATE_STARTED;
5084 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005085 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005086 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005087 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5088 data, data_length );
5089 if( status != PSA_SUCCESS )
5090 return( status );
5091 status = psa_hmac_finish_internal( &hkdf->hmac,
5092 hkdf->prk,
5093 sizeof( hkdf->prk ) );
5094 if( status != PSA_SUCCESS )
5095 return( status );
5096 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5097 hkdf->block_number = 0;
5098 hkdf->state = HKDF_STATE_KEYED;
5099 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005100 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005101 if( hkdf->state == HKDF_STATE_OUTPUT )
5102 return( PSA_ERROR_BAD_STATE );
5103 if( hkdf->info_set )
5104 return( PSA_ERROR_BAD_STATE );
5105 hkdf->info_length = data_length;
5106 if( data_length != 0 )
5107 {
5108 hkdf->info = mbedtls_calloc( 1, data_length );
5109 if( hkdf->info == NULL )
5110 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5111 memcpy( hkdf->info, data, data_length );
5112 }
5113 hkdf->info_set = 1;
5114 return( PSA_SUCCESS );
5115 default:
5116 return( PSA_ERROR_INVALID_ARGUMENT );
5117 }
5118}
Janos Follathb03233e2019-06-11 15:30:30 +01005119
5120#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5121static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5122 psa_algorithm_t hash_alg,
5123 psa_key_derivation_step_t step,
5124 const uint8_t *data,
5125 size_t data_length )
5126{
5127 (void) prf;
5128 (void) hash_alg;
5129 (void) step;
5130 (void) data;
5131 (void) data_length;
5132
5133 return( PSA_ERROR_INVALID_ARGUMENT );
5134}
Janos Follath6660f0e2019-06-17 08:44:03 +01005135
5136static psa_status_t psa_tls12_prf_psk_to_ms_input(
5137 psa_tls12_prf_key_derivation_t *prf,
5138 psa_algorithm_t hash_alg,
5139 psa_key_derivation_step_t step,
5140 const uint8_t *data,
5141 size_t data_length )
5142{
5143 (void) prf;
5144 (void) hash_alg;
5145 (void) step;
5146 (void) data;
5147 (void) data_length;
5148
5149 return( PSA_ERROR_INVALID_ARGUMENT );
5150}
Janos Follathb03233e2019-06-11 15:30:30 +01005151#else
Janos Follathf08e2652019-06-13 09:05:41 +01005152static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5153 const uint8_t *data,
5154 size_t data_length )
5155{
5156 if( prf->state != TLS12_PRF_STATE_INIT )
5157 return( PSA_ERROR_BAD_STATE );
5158
Janos Follathd6dce9f2019-07-04 09:11:38 +01005159 if( data_length != 0 )
5160 {
5161 prf->seed = mbedtls_calloc( 1, data_length );
5162 if( prf->seed == NULL )
5163 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005164
Janos Follathd6dce9f2019-07-04 09:11:38 +01005165 memcpy( prf->seed, data, data_length );
5166 prf->seed_length = data_length;
5167 }
Janos Follathf08e2652019-06-13 09:05:41 +01005168
5169 prf->state = TLS12_PRF_STATE_SEED_SET;
5170
5171 return( PSA_SUCCESS );
5172}
5173
Janos Follath81550542019-06-13 14:26:34 +01005174static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5175 psa_algorithm_t hash_alg,
5176 const uint8_t *data,
5177 size_t data_length )
5178{
5179 psa_status_t status;
5180 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5181 return( PSA_ERROR_BAD_STATE );
5182
5183 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5184 if( status != PSA_SUCCESS )
5185 return( status );
5186
5187 prf->state = TLS12_PRF_STATE_KEY_SET;
5188
5189 return( PSA_SUCCESS );
5190}
5191
Janos Follath6660f0e2019-06-17 08:44:03 +01005192static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5193 psa_tls12_prf_key_derivation_t *prf,
5194 psa_algorithm_t hash_alg,
5195 const uint8_t *data,
5196 size_t data_length )
5197{
5198 psa_status_t status;
5199 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Janos Follath40e13932019-06-26 13:22:29 +01005200 unsigned char* cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005201
5202 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5203 return( PSA_ERROR_INVALID_ARGUMENT );
5204
5205 /* Quoting RFC 4279, Section 2:
5206 *
5207 * The premaster secret is formed as follows: if the PSK is N octets
5208 * long, concatenate a uint16 with the value N, N zero octets, a second
5209 * uint16 with the value N, and the PSK itself.
5210 */
5211
Janos Follath40e13932019-06-26 13:22:29 +01005212 *cur++ = ( data_length >> 8 ) & 0xff;
5213 *cur++ = ( data_length >> 0 ) & 0xff;
5214 memset( cur, 0, data_length );
5215 cur += data_length;
5216 *cur++ = pms[0];
5217 *cur++ = pms[1];
5218 memcpy( cur, data, data_length );
5219 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005220
Janos Follath40e13932019-06-26 13:22:29 +01005221 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005222
5223 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5224 return( status );
5225}
5226
Janos Follath63028dd2019-06-13 09:15:47 +01005227static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5228 const uint8_t *data,
5229 size_t data_length )
5230{
5231 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5232 return( PSA_ERROR_BAD_STATE );
5233
Janos Follathd6dce9f2019-07-04 09:11:38 +01005234 if( data_length != 0 )
5235 {
5236 prf->label = mbedtls_calloc( 1, data_length );
5237 if( prf->label == NULL )
5238 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005239
Janos Follathd6dce9f2019-07-04 09:11:38 +01005240 memcpy( prf->label, data, data_length );
5241 prf->label_length = data_length;
5242 }
Janos Follath63028dd2019-06-13 09:15:47 +01005243
5244 prf->state = TLS12_PRF_STATE_LABEL_SET;
5245
5246 return( PSA_SUCCESS );
5247}
5248
Janos Follathb03233e2019-06-11 15:30:30 +01005249static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5250 psa_algorithm_t hash_alg,
5251 psa_key_derivation_step_t step,
5252 const uint8_t *data,
5253 size_t data_length )
5254{
Janos Follathb03233e2019-06-11 15:30:30 +01005255 switch( step )
5256 {
Janos Follathf08e2652019-06-13 09:05:41 +01005257 case PSA_KEY_DERIVATION_INPUT_SEED:
5258 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005259 case PSA_KEY_DERIVATION_INPUT_SECRET:
5260 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005261 case PSA_KEY_DERIVATION_INPUT_LABEL:
5262 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005263 default:
5264 return( PSA_ERROR_INVALID_ARGUMENT );
5265 }
5266}
Janos Follath6660f0e2019-06-17 08:44:03 +01005267
5268static psa_status_t psa_tls12_prf_psk_to_ms_input(
5269 psa_tls12_prf_key_derivation_t *prf,
5270 psa_algorithm_t hash_alg,
5271 psa_key_derivation_step_t step,
5272 const uint8_t *data,
5273 size_t data_length )
5274{
5275 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005276 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005277 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5278 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005279 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005280
5281 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5282}
Janos Follathb03233e2019-06-11 15:30:30 +01005283#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005284#endif /* MBEDTLS_MD_C */
5285
Janos Follathb80a94e2019-06-12 15:54:46 +01005286static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005288 psa_key_derivation_step_t step,
5289 const uint8_t *data,
5290 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005291{
5292 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005293 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005294
5295#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005296 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005297 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005298 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005299 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005300 step, data, data_length );
5301 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005302 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005303#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005304#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005305 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005306 {
Janos Follathb03233e2019-06-11 15:30:30 +01005307 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5308 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5309 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005310 }
5311 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5312 {
5313 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5314 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5315 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005316 }
5317 else
5318#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005319 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005320 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005321 return( PSA_ERROR_BAD_STATE );
5322 }
5323
5324 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005325 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005326 return( status );
5327}
5328
Janos Follath51f4a0f2019-06-14 11:35:55 +01005329psa_status_t psa_key_derivation_input_bytes(
5330 psa_key_derivation_operation_t *operation,
5331 psa_key_derivation_step_t step,
5332 const uint8_t *data,
5333 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005334{
Janos Follathc5621512019-06-14 11:27:57 +01005335 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5336 return( PSA_ERROR_INVALID_ARGUMENT );
5337
5338 return( psa_key_derivation_input_internal( operation, step,
5339 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005340}
5341
Janos Follath51f4a0f2019-06-14 11:35:55 +01005342psa_status_t psa_key_derivation_input_key(
5343 psa_key_derivation_operation_t *operation,
5344 psa_key_derivation_step_t step,
5345 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005346{
5347 psa_key_slot_t *slot;
5348 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005349 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005350 PSA_KEY_USAGE_DERIVE,
5351 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005352 if( status != PSA_SUCCESS )
5353 return( status );
5354 if( slot->type != PSA_KEY_TYPE_DERIVE )
5355 return( PSA_ERROR_INVALID_ARGUMENT );
5356 /* Don't allow a key to be used as an input that is usually public.
5357 * This is debatable. It's ok from a cryptographic perspective to
5358 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005359 * the material should be dedicated to a particular input step,
5360 * otherwise this may allow the key to be used in an unintended way
5361 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005362 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005363 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005364 return( psa_key_derivation_input_internal( operation,
5365 step,
5366 slot->data.raw.data,
5367 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005368}
5369
Gilles Peskineea0fb492018-07-12 17:17:20 +02005370
5371
5372/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005373/* Key agreement */
5374/****************************************************************/
5375
Gilles Peskinea05219c2018-11-16 16:02:56 +01005376#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005377static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5378 size_t peer_key_length,
5379 const mbedtls_ecp_keypair *our_key,
5380 uint8_t *shared_secret,
5381 size_t shared_secret_size,
5382 size_t *shared_secret_length )
5383{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005384 mbedtls_ecp_keypair *their_key = NULL;
5385 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005386 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005387 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005388
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005389 status = psa_import_ec_public_key(
5390 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5391 peer_key, peer_key_length,
5392 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005393 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005394 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005395
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005396 status = mbedtls_to_psa_error(
5397 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5398 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005399 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005400 status = mbedtls_to_psa_error(
5401 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5402 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005403 goto exit;
5404
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005405 status = mbedtls_to_psa_error(
5406 mbedtls_ecdh_calc_secret( &ecdh,
5407 shared_secret_length,
5408 shared_secret, shared_secret_size,
5409 mbedtls_ctr_drbg_random,
5410 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005411
5412exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005413 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005414 mbedtls_ecp_keypair_free( their_key );
5415 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005416 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005417}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005418#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005419
Gilles Peskine01d718c2018-09-18 12:01:02 +02005420#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5421
Gilles Peskine0216fe12019-04-11 21:23:21 +02005422static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5423 psa_key_slot_t *private_key,
5424 const uint8_t *peer_key,
5425 size_t peer_key_length,
5426 uint8_t *shared_secret,
5427 size_t shared_secret_size,
5428 size_t *shared_secret_length )
5429{
5430 switch( alg )
5431 {
5432#if defined(MBEDTLS_ECDH_C)
5433 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005434 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005435 return( PSA_ERROR_INVALID_ARGUMENT );
5436 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5437 private_key->data.ecp,
5438 shared_secret, shared_secret_size,
5439 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005440#endif /* MBEDTLS_ECDH_C */
5441 default:
5442 (void) private_key;
5443 (void) peer_key;
5444 (void) peer_key_length;
5445 (void) shared_secret;
5446 (void) shared_secret_size;
5447 (void) shared_secret_length;
5448 return( PSA_ERROR_NOT_SUPPORTED );
5449 }
5450}
5451
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005452/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005453 * to potentially free embedded data structures and wipe confidential data.
5454 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005455static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005456 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005457 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005458 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005459 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005460{
5461 psa_status_t status;
5462 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5463 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005464 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005465
5466 /* Step 1: run the secret agreement algorithm to generate the shared
5467 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005468 status = psa_key_agreement_raw_internal( ka_alg,
5469 private_key,
5470 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005471 shared_secret,
5472 sizeof( shared_secret ),
5473 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005474 if( status != PSA_SUCCESS )
5475 goto exit;
5476
5477 /* Step 2: set up the key derivation to generate key material from
5478 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005479 status = psa_key_derivation_input_internal( operation, step,
5480 shared_secret,
5481 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005482
Gilles Peskine01d718c2018-09-18 12:01:02 +02005483exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005484 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005485 return( status );
5486}
5487
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005489 psa_key_derivation_step_t step,
5490 psa_key_handle_t private_key,
5491 const uint8_t *peer_key,
5492 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005493{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005494 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005495 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005496 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005497 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005498 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005499 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005500 if( status != PSA_SUCCESS )
5501 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005502 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005503 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005504 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005505 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005506 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005507 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005508}
5509
Gilles Peskinebe697d82019-05-16 18:00:41 +02005510psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5511 psa_key_handle_t private_key,
5512 const uint8_t *peer_key,
5513 size_t peer_key_length,
5514 uint8_t *output,
5515 size_t output_size,
5516 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005517{
5518 psa_key_slot_t *slot;
5519 psa_status_t status;
5520
5521 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5522 {
5523 status = PSA_ERROR_INVALID_ARGUMENT;
5524 goto exit;
5525 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005526 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005527 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005528 if( status != PSA_SUCCESS )
5529 goto exit;
5530
5531 status = psa_key_agreement_raw_internal( alg, slot,
5532 peer_key, peer_key_length,
5533 output, output_size,
5534 output_length );
5535
5536exit:
5537 if( status != PSA_SUCCESS )
5538 {
5539 /* If an error happens and is not handled properly, the output
5540 * may be used as a key to protect sensitive data. Arrange for such
5541 * a key to be random, which is likely to result in decryption or
5542 * verification errors. This is better than filling the buffer with
5543 * some constant data such as zeros, which would result in the data
5544 * being protected with a reproducible, easily knowable key.
5545 */
5546 psa_generate_random( output, output_size );
5547 *output_length = output_size;
5548 }
5549 return( status );
5550}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005551
5552
5553/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005554/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005555/****************************************************************/
5556
5557psa_status_t psa_generate_random( uint8_t *output,
5558 size_t output_size )
5559{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005560 int ret;
5561 GUARD_MODULE_INITIALIZED;
5562
5563 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005564 return( mbedtls_to_psa_error( ret ) );
5565}
5566
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005567#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5568#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005569
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005570psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
5571 size_t seed_size )
5572{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005573 if( global_data.initialized )
5574 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005575
5576 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5577 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5578 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5579 return( PSA_ERROR_INVALID_ARGUMENT );
5580
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005581 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005582}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005583#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005584
Gilles Peskinee56e8782019-04-26 17:34:02 +02005585#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5586static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5587 size_t domain_parameters_size,
5588 int *exponent )
5589{
5590 size_t i;
5591 uint32_t acc = 0;
5592
5593 if( domain_parameters_size == 0 )
5594 {
5595 *exponent = 65537;
5596 return( PSA_SUCCESS );
5597 }
5598
5599 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5600 * support values that fit in a 32-bit integer, which is larger than
5601 * int on just about every platform anyway. */
5602 if( domain_parameters_size > sizeof( acc ) )
5603 return( PSA_ERROR_NOT_SUPPORTED );
5604 for( i = 0; i < domain_parameters_size; i++ )
5605 acc = ( acc << 8 ) | domain_parameters[i];
5606 if( acc > INT_MAX )
5607 return( PSA_ERROR_NOT_SUPPORTED );
5608 *exponent = acc;
5609 return( PSA_SUCCESS );
5610}
5611#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5612
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005613static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005614 psa_key_slot_t *slot, size_t bits,
5615 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005616{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005617 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005618
Gilles Peskinee56e8782019-04-26 17:34:02 +02005619 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005620 return( PSA_ERROR_INVALID_ARGUMENT );
5621
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005622 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005623 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005624 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005625 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005626 if( status != PSA_SUCCESS )
5627 return( status );
5628 status = psa_generate_random( slot->data.raw.data,
5629 slot->data.raw.bytes );
5630 if( status != PSA_SUCCESS )
5631 {
5632 mbedtls_free( slot->data.raw.data );
5633 return( status );
5634 }
5635#if defined(MBEDTLS_DES_C)
5636 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005637 psa_des_set_key_parity( slot->data.raw.data,
5638 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005639#endif /* MBEDTLS_DES_C */
5640 }
5641 else
5642
5643#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005644 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005645 {
5646 mbedtls_rsa_context *rsa;
5647 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005648 int exponent;
5649 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005650 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5651 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005652 /* Accept only byte-aligned keys, for the same reasons as
5653 * in psa_import_rsa_key(). */
5654 if( bits % 8 != 0 )
5655 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005656 status = psa_read_rsa_exponent( domain_parameters,
5657 domain_parameters_size,
5658 &exponent );
5659 if( status != PSA_SUCCESS )
5660 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005661 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5662 if( rsa == NULL )
5663 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5664 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5665 ret = mbedtls_rsa_gen_key( rsa,
5666 mbedtls_ctr_drbg_random,
5667 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005668 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005669 exponent );
5670 if( ret != 0 )
5671 {
5672 mbedtls_rsa_free( rsa );
5673 mbedtls_free( rsa );
5674 return( mbedtls_to_psa_error( ret ) );
5675 }
5676 slot->data.rsa = rsa;
5677 }
5678 else
5679#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5680
5681#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005682 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005683 {
5684 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5685 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5686 const mbedtls_ecp_curve_info *curve_info =
5687 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5688 mbedtls_ecp_keypair *ecp;
5689 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005690 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005691 return( PSA_ERROR_NOT_SUPPORTED );
5692 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5693 return( PSA_ERROR_NOT_SUPPORTED );
5694 if( curve_info->bit_size != bits )
5695 return( PSA_ERROR_INVALID_ARGUMENT );
5696 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5697 if( ecp == NULL )
5698 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5699 mbedtls_ecp_keypair_init( ecp );
5700 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5701 mbedtls_ctr_drbg_random,
5702 &global_data.ctr_drbg );
5703 if( ret != 0 )
5704 {
5705 mbedtls_ecp_keypair_free( ecp );
5706 mbedtls_free( ecp );
5707 return( mbedtls_to_psa_error( ret ) );
5708 }
5709 slot->data.ecp = ecp;
5710 }
5711 else
5712#endif /* MBEDTLS_ECP_C */
5713
5714 return( PSA_ERROR_NOT_SUPPORTED );
5715
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005716 return( PSA_SUCCESS );
5717}
5718
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005719psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005720 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005721{
5722 psa_status_t status;
5723 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005724 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005725 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005726#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5727 if( driver != NULL )
5728 {
5729 /* Generating a key in a secure element is not implemented yet. */
5730 status = PSA_ERROR_NOT_SUPPORTED;
5731 }
5732#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005733 if( status == PSA_SUCCESS )
5734 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005735 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005736 slot, attributes->bits,
5737 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005738 }
5739 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005740 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005741 if( status != PSA_SUCCESS )
5742 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005743 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005744 *handle = 0;
5745 }
5746 return( status );
5747}
5748
5749
Gilles Peskine05d69892018-06-19 22:00:52 +02005750
5751/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005752/* Module setup */
5753/****************************************************************/
5754
Gilles Peskine5e769522018-11-20 21:59:56 +01005755psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5756 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5757 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5758{
5759 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5760 return( PSA_ERROR_BAD_STATE );
5761 global_data.entropy_init = entropy_init;
5762 global_data.entropy_free = entropy_free;
5763 return( PSA_SUCCESS );
5764}
5765
Gilles Peskinee59236f2018-01-27 23:32:46 +01005766void mbedtls_psa_crypto_free( void )
5767{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005768 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005769 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5770 {
5771 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005772 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005773 }
5774 /* Wipe all remaining data, including configuration.
5775 * In particular, this sets all state indicator to the value
5776 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005777 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005778#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005779 /* Unregister all secure element drivers, so that we restart from
5780 * a pristine state. */
5781 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005782#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005783}
5784
5785psa_status_t psa_crypto_init( void )
5786{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005787 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005788 const unsigned char drbg_seed[] = "PSA";
5789
Gilles Peskinec6b69072018-11-20 21:42:52 +01005790 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005791 if( global_data.initialized != 0 )
5792 return( PSA_SUCCESS );
5793
Gilles Peskine5e769522018-11-20 21:59:56 +01005794 /* Set default configuration if
5795 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5796 if( global_data.entropy_init == NULL )
5797 global_data.entropy_init = mbedtls_entropy_init;
5798 if( global_data.entropy_free == NULL )
5799 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005800
Gilles Peskinec6b69072018-11-20 21:42:52 +01005801 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005802 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005803 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005804 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005805 status = mbedtls_to_psa_error(
5806 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5807 mbedtls_entropy_func,
5808 &global_data.entropy,
5809 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5810 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005811 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005812 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005813
Gilles Peskine66fb1262018-12-10 16:29:04 +01005814 status = psa_initialize_key_slots( );
5815 if( status != PSA_SUCCESS )
5816 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005817
Gilles Peskine4b734222019-07-24 15:56:31 +02005818#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005819 status = psa_crypto_load_transaction( );
5820 if( status == PSA_SUCCESS )
5821 {
5822 /*TOnogrepDO: complete or abort the transaction*/
5823 }
5824 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5825 {
5826 /* There's no transaction to complete. It's all good. */
5827 status = PSA_SUCCESS;
5828 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005829#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005830
Gilles Peskinec6b69072018-11-20 21:42:52 +01005831 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005832 global_data.initialized = 1;
5833
Gilles Peskinee59236f2018-01-27 23:32:46 +01005834exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005835 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005836 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005837 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005838}
5839
5840#endif /* MBEDTLS_PSA_CRYPTO_C */