blob: 4c93dd0ad5824be3ca4de75f78f0ba2645fcf759 [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 Peskinec744d992019-07-30 17:26:54 +0200709 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
710 /* Ensure that the bytes-to-bit conversion doesn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100711 if( data_length > SIZE_MAX / 8 )
712 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinec744d992019-07-30 17:26:54 +0200713 /* Ensure that the key is not overly large. */
714 if( bit_size > PSA_MAX_KEY_BITS )
715 return( PSA_ERROR_NOT_SUPPORTED );
716 status = prepare_raw_data_slot( slot->type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200717 &slot->data.raw );
718 if( status != PSA_SUCCESS )
719 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200720 if( data_length != 0 )
721 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100722 }
723 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100724#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200725 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100726 {
Darryl Green940d72c2018-07-13 13:18:51 +0100727 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100728 data, data_length,
729 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100730 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000731 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
732 {
733 status = psa_import_ec_public_key(
734 PSA_KEY_TYPE_GET_CURVE( slot->type ),
735 data, data_length,
736 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000737 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100738 else
739#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000740#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
741 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100742 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000743 status = psa_import_rsa_key( slot->type,
744 data, data_length,
745 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 }
747 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000748#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100749 {
750 return( PSA_ERROR_NOT_SUPPORTED );
751 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000752 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100753}
754
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100755/** Calculate the intersection of two algorithm usage policies.
756 *
757 * Return 0 (which allows no operation) on incompatibility.
758 */
759static psa_algorithm_t psa_key_policy_algorithm_intersection(
760 psa_algorithm_t alg1,
761 psa_algorithm_t alg2 )
762{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200763 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100764 if( alg1 == alg2 )
765 return( alg1 );
766 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100767 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100768 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
769 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
770 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
771 {
772 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
773 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100774 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100775 return( alg1 );
776 }
777 /* If the policies are incompatible, allow nothing. */
778 return( 0 );
779}
780
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200781static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
782 psa_algorithm_t requested_alg )
783{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200784 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200785 if( requested_alg == policy_alg )
786 return( 1 );
787 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200788 * and requested_alg is the same hash-and-sign family with any hash,
789 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200790 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
791 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
792 {
793 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
794 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
795 }
796 /* If it isn't permitted, it's forbidden. */
797 return( 0 );
798}
799
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100800/** Test whether a policy permits an algorithm.
801 *
802 * The caller must test usage flags separately.
803 */
804static int psa_key_policy_permits( const psa_key_policy_t *policy,
805 psa_algorithm_t alg )
806{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200807 return( psa_key_algorithm_permits( policy->alg, alg ) ||
808 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100809}
810
Gilles Peskinef603c712019-01-19 13:40:11 +0100811/** Restrict a key policy based on a constraint.
812 *
813 * \param[in,out] policy The policy to restrict.
814 * \param[in] constraint The policy constraint to apply.
815 *
816 * \retval #PSA_SUCCESS
817 * \c *policy contains the intersection of the original value of
818 * \c *policy and \c *constraint.
819 * \retval #PSA_ERROR_INVALID_ARGUMENT
820 * \c *policy and \c *constraint are incompatible.
821 * \c *policy is unchanged.
822 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100823static psa_status_t psa_restrict_key_policy(
824 psa_key_policy_t *policy,
825 const psa_key_policy_t *constraint )
826{
827 psa_algorithm_t intersection_alg =
828 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200829 psa_algorithm_t intersection_alg2 =
830 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100831 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
832 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200833 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
834 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100835 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100836 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200837 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100838 return( PSA_SUCCESS );
839}
840
Darryl Green06fd18d2018-07-16 11:21:11 +0100841/** Retrieve a slot which must contain a key. The key must have allow all the
842 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
843 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100844static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100845 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100846 psa_key_usage_t usage,
847 psa_algorithm_t alg )
848{
849 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100850 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100851
852 *p_slot = NULL;
853
Gilles Peskinec5487a82018-12-03 18:08:14 +0100854 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100855 if( status != PSA_SUCCESS )
856 return( status );
857 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200858 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100859
860 /* Enforce that usage policy for the key slot contains all the flags
861 * required by the usage parameter. There is one exception: public
862 * keys can always be exported, so we treat public key objects as
863 * if they had the export flag. */
864 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
865 usage &= ~PSA_KEY_USAGE_EXPORT;
866 if( ( slot->policy.usage & usage ) != usage )
867 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100868
869 /* Enforce that the usage policy permits the requested algortihm. */
870 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100871 return( PSA_ERROR_NOT_PERMITTED );
872
873 *p_slot = slot;
874 return( PSA_SUCCESS );
875}
Darryl Green940d72c2018-07-13 13:18:51 +0100876
Gilles Peskine28f8f302019-07-24 13:30:31 +0200877/** Retrieve a slot which must contain a transparent key.
878 *
879 * A transparent key is a key for which the key material is directly
880 * available, as opposed to a key in a secure element.
881 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200882 * This is a temporary function to use instead of psa_get_key_from_slot()
883 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200884 */
885#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
886static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
887 psa_key_slot_t **p_slot,
888 psa_key_usage_t usage,
889 psa_algorithm_t alg )
890{
891 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
892 if( status != PSA_SUCCESS )
893 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200894 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200895 {
896 *p_slot = NULL;
897 return( PSA_ERROR_NOT_SUPPORTED );
898 }
899 return( PSA_SUCCESS );
900}
901#else /* MBEDTLS_PSA_CRYPTO_SE_C */
902/* With no secure element support, all keys are transparent. */
903#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
904 psa_get_key_from_slot( handle, p_slot, usage, alg )
905#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
906
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100907/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100908static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000909{
Gilles Peskine73167e12019-07-12 23:44:37 +0200910#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
911 if( psa_key_slot_is_external( slot ) )
912 {
913 /* No key material to clean. */
914 }
915 else
916#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +0000917 if( slot->type == PSA_KEY_TYPE_NONE )
918 {
919 /* No key material to clean. */
920 }
921 else if( key_type_is_raw_bytes( slot->type ) )
922 {
923 mbedtls_free( slot->data.raw.data );
924 }
925 else
926#if defined(MBEDTLS_RSA_C)
927 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
928 {
929 mbedtls_rsa_free( slot->data.rsa );
930 mbedtls_free( slot->data.rsa );
931 }
932 else
933#endif /* defined(MBEDTLS_RSA_C) */
934#if defined(MBEDTLS_ECP_C)
935 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
936 {
937 mbedtls_ecp_keypair_free( slot->data.ecp );
938 mbedtls_free( slot->data.ecp );
939 }
940 else
941#endif /* defined(MBEDTLS_ECP_C) */
942 {
943 /* Shouldn't happen: the key type is not any type that we
944 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200945 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000946 }
947
948 return( PSA_SUCCESS );
949}
950
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100951static void psa_abort_operations_using_key( psa_key_slot_t *slot )
952{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200953 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100954 (void) slot;
955}
956
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100957/** Completely wipe a slot in memory, including its policy.
958 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100959psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100960{
961 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100962 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100963 /* At this point, key material and other type-specific content has
964 * been wiped. Clear remaining metadata. We can call memset and not
965 * zeroize because the metadata is not particularly sensitive. */
966 memset( slot, 0, sizeof( *slot ) );
967 return( status );
968}
969
Gilles Peskinec5487a82018-12-03 18:08:14 +0100970psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100971{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100972 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100973 psa_status_t status = PSA_SUCCESS;
974 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200975#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
976 psa_se_drv_table_entry_t *driver;
977#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100978
Gilles Peskinec5487a82018-12-03 18:08:14 +0100979 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200980 if( status != PSA_SUCCESS )
981 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +0200982
983#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
984 driver = psa_get_se_driver_entry( slot->lifetime );
985 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +0200986 {
Gilles Peskine60450a42019-07-25 11:32:45 +0200987 /* For a key in a secure element, we need to do three things:
988 * remove the key file in internal storage, destroy the
989 * key inside the secure element, and update the driver's
990 * persistent data. Start a transaction that will encompass these
991 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +0200992 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
993 psa_crypto_transaction.key.lifetime = slot->lifetime;
994 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
995 psa_crypto_transaction.key.id = slot->persistent_storage_id;
996 status = psa_crypto_save_transaction( );
997 if( status != PSA_SUCCESS )
998 {
Gilles Peskine66be51c2019-07-25 18:02:52 +0200999 (void) psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02001000 /* TOnogrepDO: destroy what can be destroyed anyway */
1001 return( status );
1002 }
1003
Gilles Peskine354f7672019-07-12 23:46:38 +02001004 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +02001005 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001006#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1007
Darryl Greend49a4992018-06-18 17:27:26 +01001008#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1009 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1010 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001011 storage_status =
1012 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +01001013 }
1014#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001015
Gilles Peskinefc762652019-07-22 19:30:34 +02001016#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1017 if( driver != NULL )
1018 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001019 psa_status_t status2;
1020 status = psa_save_se_persistent_data( driver );
1021 status2 = psa_crypto_stop_transaction( );
1022 if( status == PSA_SUCCESS )
1023 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001024 if( status != PSA_SUCCESS )
1025 {
1026 /* TOnogrepDO: destroy what can be destroyed anyway */
1027 return( status );
1028 }
1029 }
1030#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1031
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001032 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001033 if( status != PSA_SUCCESS )
1034 return( status );
1035 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036}
1037
Gilles Peskineb870b182018-07-06 16:02:09 +02001038/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001039static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +02001040{
Gilles Peskine424f8942019-07-15 21:59:53 +02001041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1042 if( psa_get_se_driver( slot->lifetime, NULL, NULL ) )
1043 return( slot->data.se.bits );
1044#endif /* defined(MBEDTLS_PSA_CRYPTO_SE_C) */
1045
Gilles Peskineb870b182018-07-06 16:02:09 +02001046 if( key_type_is_raw_bytes( slot->type ) )
1047 return( slot->data.raw.bytes * 8 );
1048#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001049 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001050 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001051#endif /* defined(MBEDTLS_RSA_C) */
1052#if defined(MBEDTLS_ECP_C)
1053 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1054 return( slot->data.ecp->grp.pbits );
1055#endif /* defined(MBEDTLS_ECP_C) */
1056 /* Shouldn't happen except on an empty slot. */
1057 return( 0 );
1058}
1059
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001060void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1061{
Gilles Peskineb699f072019-04-26 16:06:02 +02001062 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001063 memset( attributes, 0, sizeof( *attributes ) );
1064}
1065
Gilles Peskineb699f072019-04-26 16:06:02 +02001066psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1067 psa_key_type_t type,
1068 const uint8_t *data,
1069 size_t data_length )
1070{
1071 uint8_t *copy = NULL;
1072
1073 if( data_length != 0 )
1074 {
1075 copy = mbedtls_calloc( 1, data_length );
1076 if( copy == NULL )
1077 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1078 memcpy( copy, data, data_length );
1079 }
1080 /* After this point, this function is guaranteed to succeed, so it
1081 * can start modifying `*attributes`. */
1082
1083 if( attributes->domain_parameters != NULL )
1084 {
1085 mbedtls_free( attributes->domain_parameters );
1086 attributes->domain_parameters = NULL;
1087 attributes->domain_parameters_size = 0;
1088 }
1089
1090 attributes->domain_parameters = copy;
1091 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001092 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001093 return( PSA_SUCCESS );
1094}
1095
1096psa_status_t psa_get_key_domain_parameters(
1097 const psa_key_attributes_t *attributes,
1098 uint8_t *data, size_t data_size, size_t *data_length )
1099{
1100 if( attributes->domain_parameters_size > data_size )
1101 return( PSA_ERROR_BUFFER_TOO_SMALL );
1102 *data_length = attributes->domain_parameters_size;
1103 if( attributes->domain_parameters_size != 0 )
1104 memcpy( data, attributes->domain_parameters,
1105 attributes->domain_parameters_size );
1106 return( PSA_SUCCESS );
1107}
1108
1109#if defined(MBEDTLS_RSA_C)
1110static psa_status_t psa_get_rsa_public_exponent(
1111 const mbedtls_rsa_context *rsa,
1112 psa_key_attributes_t *attributes )
1113{
1114 mbedtls_mpi mpi;
1115 int ret;
1116 uint8_t *buffer = NULL;
1117 size_t buflen;
1118 mbedtls_mpi_init( &mpi );
1119
1120 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1121 if( ret != 0 )
1122 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001123 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1124 {
1125 /* It's the default value, which is reported as an empty string,
1126 * so there's nothing to do. */
1127 goto exit;
1128 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001129
1130 buflen = mbedtls_mpi_size( &mpi );
1131 buffer = mbedtls_calloc( 1, buflen );
1132 if( buffer == NULL )
1133 {
1134 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1135 goto exit;
1136 }
1137 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1138 if( ret != 0 )
1139 goto exit;
1140 attributes->domain_parameters = buffer;
1141 attributes->domain_parameters_size = buflen;
1142
1143exit:
1144 mbedtls_mpi_free( &mpi );
1145 if( ret != 0 )
1146 mbedtls_free( buffer );
1147 return( mbedtls_to_psa_error( ret ) );
1148}
1149#endif /* MBEDTLS_RSA_C */
1150
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001151/** Retrieve the generic attributes of a key in a slot.
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001152 *
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001153 * This function does not retrieve domain parameters, which require
1154 * additional memory management.
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001155 */
1156static void psa_get_key_slot_attributes( psa_key_slot_t *slot,
1157 psa_key_attributes_t *attributes )
1158{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001159 attributes->core.id = slot->persistent_storage_id;
1160 attributes->core.lifetime = slot->lifetime;
1161 attributes->core.policy = slot->policy;
1162 attributes->core.type = slot->type;
1163 attributes->core.bits = psa_get_key_slot_bits( slot );
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001164}
1165
1166/** Retrieve all the publicly-accessible attributes of a key.
1167 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001168psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1169 psa_key_attributes_t *attributes )
1170{
1171 psa_key_slot_t *slot;
1172 psa_status_t status;
1173
1174 psa_reset_key_attributes( attributes );
1175
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001176 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001177 if( status != PSA_SUCCESS )
1178 return( status );
1179
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001180 psa_get_key_slot_attributes( slot, attributes );
Gilles Peskineb699f072019-04-26 16:06:02 +02001181
1182 switch( slot->type )
1183 {
1184#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001185 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001186 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001187#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1188 /* TOnogrepDO: reporting the public exponent for opaque keys
1189 * is not yet implemented. */
1190 if( psa_get_se_driver( slot->lifetime, NULL, NULL ) )
1191 break;
1192#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001193 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1194 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001195#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001196 default:
1197 /* Nothing else to do. */
1198 break;
1199 }
1200
1201 if( status != PSA_SUCCESS )
1202 psa_reset_key_attributes( attributes );
1203 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001204}
1205
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001206#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001207static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1208 unsigned char *buf, size_t size )
1209{
1210 int ret;
1211 unsigned char *c;
1212 size_t len = 0;
1213
1214 c = buf + size;
1215
1216 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1217
1218 return( (int) len );
1219}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001220#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001221
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001222static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1223 uint8_t *data,
1224 size_t data_size,
1225 size_t *data_length,
1226 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001227{
Gilles Peskine5d309672019-07-12 23:47:28 +02001228#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1229 const psa_drv_se_t *drv;
1230 psa_drv_se_context_t *drv_context;
1231#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1232
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001233 *data_length = 0;
1234
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001235 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001236 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001237
Gilles Peskine5d309672019-07-12 23:47:28 +02001238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1239 if( psa_get_se_driver( slot->lifetime, &drv, &drv_context ) )
1240 {
1241 psa_drv_se_export_key_t method;
1242 if( drv->key_management == NULL )
1243 return( PSA_ERROR_NOT_SUPPORTED );
1244 method = ( export_public_key ?
1245 drv->key_management->p_export_public :
1246 drv->key_management->p_export );
1247 if( method == NULL )
1248 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001249 return( method( drv_context,
1250 slot->data.se.slot_number,
1251 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001252 }
1253#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1254
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001255 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001256 {
1257 if( slot->data.raw.bytes > data_size )
1258 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001259 if( data_size != 0 )
1260 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001261 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001262 memset( data + slot->data.raw.bytes, 0,
1263 data_size - slot->data.raw.bytes );
1264 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001265 *data_length = slot->data.raw.bytes;
1266 return( PSA_SUCCESS );
1267 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001268#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001269 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001270 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001271 psa_status_t status;
1272
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001273 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001274 if( bytes > data_size )
1275 return( PSA_ERROR_BUFFER_TOO_SMALL );
1276 status = mbedtls_to_psa_error(
1277 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1278 if( status != PSA_SUCCESS )
1279 return( status );
1280 memset( data + bytes, 0, data_size - bytes );
1281 *data_length = bytes;
1282 return( PSA_SUCCESS );
1283 }
1284#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001285 else
Moran Peker17e36e12018-05-02 12:55:20 +03001286 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001287#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001288 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001289 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001290 {
Moran Pekera998bc62018-04-16 18:16:20 +03001291 mbedtls_pk_context pk;
1292 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001293 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001294 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001295#if defined(MBEDTLS_RSA_C)
1296 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001297 pk.pk_info = &mbedtls_rsa_info;
1298 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001299#else
1300 return( PSA_ERROR_NOT_SUPPORTED );
1301#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001302 }
1303 else
1304 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001305#if defined(MBEDTLS_ECP_C)
1306 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001307 pk.pk_info = &mbedtls_eckey_info;
1308 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001309#else
1310 return( PSA_ERROR_NOT_SUPPORTED );
1311#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001312 }
Moran Pekerd7326592018-05-29 16:56:39 +03001313 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001314 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001315 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001316 }
Moran Peker17e36e12018-05-02 12:55:20 +03001317 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001318 {
Moran Peker17e36e12018-05-02 12:55:20 +03001319 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001320 }
Moran Peker60364322018-04-29 11:34:58 +03001321 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001322 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001323 /* If data_size is 0 then data may be NULL and then the
1324 * call to memset would have undefined behavior. */
1325 if( data_size != 0 )
1326 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001327 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001328 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001329 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1330 * Move the data to the beginning and erase remaining data
1331 * at the original location. */
1332 if( 2 * (size_t) ret <= data_size )
1333 {
1334 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001335 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001336 }
1337 else if( (size_t) ret < data_size )
1338 {
1339 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001340 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001341 }
Moran Pekera998bc62018-04-16 18:16:20 +03001342 *data_length = ret;
1343 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001344 }
1345 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001346#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001347 {
1348 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001349 it is valid for a special-purpose implementation to omit
1350 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001351 return( PSA_ERROR_NOT_SUPPORTED );
1352 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001353 }
1354}
1355
Gilles Peskinec5487a82018-12-03 18:08:14 +01001356psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001357 uint8_t *data,
1358 size_t data_size,
1359 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001360{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001361 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001362 psa_status_t status;
1363
1364 /* Set the key to empty now, so that even when there are errors, we always
1365 * set data_length to a value between 0 and data_size. On error, setting
1366 * the key to empty is a good choice because an empty key representation is
1367 * unlikely to be accepted anywhere. */
1368 *data_length = 0;
1369
1370 /* Export requires the EXPORT flag. There is an exception for public keys,
1371 * which don't require any flag, but psa_get_key_from_slot takes
1372 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001373 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001374 if( status != PSA_SUCCESS )
1375 return( status );
1376 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001377 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001378}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001379
Gilles Peskinec5487a82018-12-03 18:08:14 +01001380psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001381 uint8_t *data,
1382 size_t data_size,
1383 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001384{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001385 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001386 psa_status_t status;
1387
1388 /* Set the key to empty now, so that even when there are errors, we always
1389 * set data_length to a value between 0 and data_size. On error, setting
1390 * the key to empty is a good choice because an empty key representation is
1391 * unlikely to be accepted anywhere. */
1392 *data_length = 0;
1393
1394 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001395 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001396 if( status != PSA_SUCCESS )
1397 return( status );
1398 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001399 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001400}
1401
Gilles Peskine4747d192019-04-17 15:05:45 +02001402static psa_status_t psa_set_key_policy_internal(
1403 psa_key_slot_t *slot,
1404 const psa_key_policy_t *policy )
1405{
1406 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001407 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001408 PSA_KEY_USAGE_ENCRYPT |
1409 PSA_KEY_USAGE_DECRYPT |
1410 PSA_KEY_USAGE_SIGN |
1411 PSA_KEY_USAGE_VERIFY |
1412 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1413 return( PSA_ERROR_INVALID_ARGUMENT );
1414
1415 slot->policy = *policy;
1416 return( PSA_SUCCESS );
1417}
1418
1419/** Prepare a key slot to receive key material.
1420 *
1421 * This function allocates a key slot and sets its metadata.
1422 *
1423 * If this function fails, call psa_fail_key_creation().
1424 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001425 * This function is intended to be used as follows:
1426 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1427 * it with the specified attributes, and assign it a handle.
1428 * -# Populate the slot with the key material.
1429 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1430 * In case of failure at any step, stop the sequence and call
1431 * psa_fail_key_creation().
1432 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001433 * \param[in] attributes Key attributes for the new key.
1434 * \param[out] handle On success, a handle for the allocated slot.
1435 * \param[out] p_slot On success, a pointer to the prepared slot.
1436 * \param[out] p_drv On any return, the driver for the key, if any.
1437 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001438 *
1439 * \retval #PSA_SUCCESS
1440 * The key slot is ready to receive key material.
1441 * \return If this function fails, the key slot is an invalid state.
1442 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001443 */
1444static psa_status_t psa_start_key_creation(
1445 const psa_key_attributes_t *attributes,
1446 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001447 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001448 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001449{
1450 psa_status_t status;
1451 psa_key_slot_t *slot;
1452
Gilles Peskine011e4282019-06-26 18:34:38 +02001453 *p_drv = NULL;
1454
Gilles Peskine267c6562019-05-27 19:01:54 +02001455 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001456 if( status != PSA_SUCCESS )
1457 return( status );
1458 slot = *p_slot;
1459
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001460 status = psa_set_key_policy_internal( slot, &attributes->core.policy );
Gilles Peskine4747d192019-04-17 15:05:45 +02001461 if( status != PSA_SUCCESS )
1462 return( status );
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001463 slot->lifetime = attributes->core.lifetime;
Gilles Peskine011e4282019-06-26 18:34:38 +02001464
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001465 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001466 {
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001467 status = psa_validate_persistent_key_parameters( attributes->core.lifetime,
1468 attributes->core.id,
Gilles Peskine011e4282019-06-26 18:34:38 +02001469 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001470 if( status != PSA_SUCCESS )
1471 return( status );
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001472 slot->persistent_storage_id = attributes->core.id;
Gilles Peskined167b942019-04-19 18:19:40 +02001473 }
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001474 slot->type = attributes->core.type;
Gilles Peskine4747d192019-04-17 15:05:45 +02001475
Gilles Peskinec744d992019-07-30 17:26:54 +02001476 /* Refuse to create overly large keys.
1477 * Note that this doesn't trigger on import if the attributes don't
1478 * explicitly specify a size (so psa_get_key_bits returns 0), so
1479 * psa_import_key() needs its own checks. */
1480 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1481 return( PSA_ERROR_NOT_SUPPORTED );
1482
Gilles Peskinecbaff462019-07-12 23:46:04 +02001483#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine60450a42019-07-25 11:32:45 +02001484 /* For a key in a secure element, we need to do three things:
1485 * create the key file in internal storage, create the
1486 * key inside the secure element, and update the driver's
1487 * persistent data. Start a transaction that will encompass these
1488 * three actions. */
1489 /* The first thing to do is to find a slot number for the new key.
1490 * We save the slot number in persistent storage as part of the
1491 * transaction data. It will be needed to recover if the power
1492 * fails during the key creation process, to clean up on the secure
1493 * element side after restarting. Obtaining a slot number from the
1494 * secure element driver updates its persistent state, but we do not yet
1495 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001496 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001497 if( *p_drv != NULL )
1498 {
1499 status = psa_find_se_slot_for_key( attributes, *p_drv,
1500 &slot->data.se.slot_number );
1501 if( status != PSA_SUCCESS )
1502 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001503 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1504 psa_crypto_transaction.key.lifetime = slot->lifetime;
1505 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1506 psa_crypto_transaction.key.id = slot->persistent_storage_id;
1507 status = psa_crypto_save_transaction( );
1508 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001509 {
1510 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001511 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001512 }
Gilles Peskine424f8942019-07-15 21:59:53 +02001513
1514 /* TOnogrepDO: validate bits. How to do this depends on the key
1515 * creation method, so setting bits might not belong here. */
1516 slot->data.se.bits = psa_get_key_bits( attributes );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001517 }
1518#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1519
Gilles Peskine4747d192019-04-17 15:05:45 +02001520 return( status );
1521}
1522
1523/** Finalize the creation of a key once its key material has been set.
1524 *
1525 * This entails writing the key to persistent storage.
1526 *
1527 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001528 * See the documentation of psa_start_key_creation() for the intended use
1529 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001530 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001531 * \param[in,out] slot Pointer to the slot with key material.
1532 * \param[in] driver The secure element driver for the key,
1533 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001534 *
1535 * \retval #PSA_SUCCESS
1536 * The key was successfully created. The handle is now valid.
1537 * \return If this function fails, the key slot is an invalid state.
1538 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001539 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001540static psa_status_t psa_finish_key_creation(
1541 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001542 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001543{
1544 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001545 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001546 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001547
1548#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1df83d42019-07-23 16:13:14 +02001549 if( slot->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001550 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1552 psa_get_key_slot_attributes( slot, &attributes );
Gilles Peskine4747d192019-04-17 15:05:45 +02001553
Gilles Peskine1df83d42019-07-23 16:13:14 +02001554#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1555 if( driver != NULL )
1556 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001557 status = psa_save_persistent_key( &attributes,
1558 (uint8_t*) &slot->data.se,
1559 sizeof( slot->data.se ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001560 }
1561 else
1562#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1563 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001564 size_t buffer_size =
1565 PSA_KEY_EXPORT_MAX_SIZE( slot->type,
1566 psa_get_key_bits( &attributes ) );
1567 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1568 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001569 if( buffer == NULL && buffer_size != 0 )
1570 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1571 status = psa_internal_export_key( slot,
1572 buffer, buffer_size, &length,
1573 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001574 if( status == PSA_SUCCESS )
1575 status = psa_save_persistent_key( &attributes, buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001576
Gilles Peskine1df83d42019-07-23 16:13:14 +02001577 if( buffer_size != 0 )
1578 mbedtls_platform_zeroize( buffer, buffer_size );
1579 mbedtls_free( buffer );
1580 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001581 }
1582#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1583
Gilles Peskinecbaff462019-07-12 23:46:04 +02001584#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1585 if( driver != NULL )
1586 {
1587 status = psa_save_se_persistent_data( driver );
1588 if( status != PSA_SUCCESS )
1589 {
1590 psa_destroy_persistent_key( slot->persistent_storage_id );
1591 return( status );
1592 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001593 status = psa_crypto_stop_transaction( );
1594 if( status != PSA_SUCCESS )
1595 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001596 }
1597#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1598
Gilles Peskine4747d192019-04-17 15:05:45 +02001599 return( status );
1600}
1601
1602/** Abort the creation of a key.
1603 *
1604 * You may call this function after calling psa_start_key_creation(),
1605 * or after psa_finish_key_creation() fails. In other circumstances, this
1606 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001607 * See the documentation of psa_start_key_creation() for the intended use
1608 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001609 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001610 * \param[in,out] slot Pointer to the slot with key material.
1611 * \param[in] driver The secure element driver for the key,
1612 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001613 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001614static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001615 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001616{
Gilles Peskine011e4282019-06-26 18:34:38 +02001617 (void) driver;
1618
Gilles Peskine4747d192019-04-17 15:05:45 +02001619 if( slot == NULL )
1620 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001621
1622#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1623 /* TOnogrepDO: If the key has already been created in the secure
1624 * element, and the failure happened later (when saving metadata
1625 * to internal storage), we need to destroy the key in the secure
1626 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001627
1628 /* Abort the ongoing transaction if any. We already did what it
1629 * takes to undo any partial creation. All that's left is to update
1630 * the transaction data itself. */
1631 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001632#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1633
Gilles Peskine4747d192019-04-17 15:05:45 +02001634 psa_wipe_key_slot( slot );
1635}
1636
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001637static psa_status_t psa_check_key_slot_attributes(
1638 const psa_key_slot_t *slot,
1639 const psa_key_attributes_t *attributes )
1640{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001641 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001642 {
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001643 if( attributes->core.type != slot->type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001644 return( PSA_ERROR_INVALID_ARGUMENT );
1645 }
1646
1647 if( attributes->domain_parameters_size != 0 )
1648 {
1649#if defined(MBEDTLS_RSA_C)
1650 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1651 {
1652 mbedtls_mpi actual, required;
1653 int ret;
1654 mbedtls_mpi_init( &actual );
1655 mbedtls_mpi_init( &required );
1656 ret = mbedtls_rsa_export( slot->data.rsa,
1657 NULL, NULL, NULL, NULL, &actual );
1658 if( ret != 0 )
1659 goto rsa_exit;
1660 ret = mbedtls_mpi_read_binary( &required,
1661 attributes->domain_parameters,
1662 attributes->domain_parameters_size );
1663 if( ret != 0 )
1664 goto rsa_exit;
1665 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1666 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1667 rsa_exit:
1668 mbedtls_mpi_free( &actual );
1669 mbedtls_mpi_free( &required );
1670 if( ret != 0)
1671 return( mbedtls_to_psa_error( ret ) );
1672 }
1673 else
1674#endif
1675 {
1676 return( PSA_ERROR_INVALID_ARGUMENT );
1677 }
1678 }
1679
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001680 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001681 {
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001682 if( attributes->core.bits != psa_get_key_slot_bits( slot ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001683 return( PSA_ERROR_INVALID_ARGUMENT );
1684 }
1685
1686 return( PSA_SUCCESS );
1687}
1688
Gilles Peskine4747d192019-04-17 15:05:45 +02001689psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001690 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001691 size_t data_length,
1692 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001693{
1694 psa_status_t status;
1695 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001696 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001697
Gilles Peskine011e4282019-06-26 18:34:38 +02001698 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001699 if( status != PSA_SUCCESS )
1700 goto exit;
1701
Gilles Peskine5d309672019-07-12 23:47:28 +02001702#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1703 if( driver != NULL )
1704 {
1705 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
1706 if( drv->key_management == NULL ||
1707 drv->key_management->p_import == NULL )
1708 {
1709 status = PSA_ERROR_NOT_SUPPORTED;
1710 goto exit;
1711 }
1712 status = drv->key_management->p_import(
1713 psa_get_se_driver_context( driver ),
1714 slot->data.se.slot_number,
1715 slot->lifetime, slot->type, slot->policy.alg, slot->policy.usage,
Gilles Peskine18017402019-07-24 20:25:59 +02001716 data, data_length,
1717 &slot->data.se.bits );
Gilles Peskine5d309672019-07-12 23:47:28 +02001718 }
1719 else
1720#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1721 {
1722 status = psa_import_key_into_slot( slot, data, data_length );
1723 if( status != PSA_SUCCESS )
1724 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001725 }
Gilles Peskine18017402019-07-24 20:25:59 +02001726 status = psa_check_key_slot_attributes( slot, attributes );
1727 if( status != PSA_SUCCESS )
1728 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001729
Gilles Peskine011e4282019-06-26 18:34:38 +02001730 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001731exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001732 if( status != PSA_SUCCESS )
1733 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001734 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001735 *handle = 0;
1736 }
1737 return( status );
1738}
1739
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001740static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001741 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001742{
1743 psa_status_t status;
1744 uint8_t *buffer = NULL;
1745 size_t buffer_size = 0;
1746 size_t length;
1747
1748 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001749 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001750 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001751 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001752 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001753 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1754 if( status != PSA_SUCCESS )
1755 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001756 target->type = source->type;
1757 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001758
1759exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001760 if( buffer_size != 0 )
1761 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001762 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001763 return( status );
1764}
1765
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001766psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1767 const psa_key_attributes_t *specified_attributes,
1768 psa_key_handle_t *target_handle )
1769{
1770 psa_status_t status;
1771 psa_key_slot_t *source_slot = NULL;
1772 psa_key_slot_t *target_slot = NULL;
1773 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001774 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001775
Gilles Peskine28f8f302019-07-24 13:30:31 +02001776 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001777 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001778 if( status != PSA_SUCCESS )
1779 goto exit;
1780
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001781 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1782 if( status != PSA_SUCCESS )
1783 goto exit;
1784
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001785 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001786 &source_slot->policy );
1787 if( status != PSA_SUCCESS )
1788 goto exit;
1789
1790 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001791 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001792 if( status != PSA_SUCCESS )
1793 goto exit;
1794
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001795#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1796 if( driver != NULL )
1797 {
1798 /* Copying to a secure element is not implemented yet. */
1799 status = PSA_ERROR_NOT_SUPPORTED;
1800 goto exit;
1801 }
1802#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1803
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001804 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001805 if( status != PSA_SUCCESS )
1806 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001807
Gilles Peskine011e4282019-06-26 18:34:38 +02001808 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001809exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001810 if( status != PSA_SUCCESS )
1811 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001812 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001813 *target_handle = 0;
1814 }
1815 return( status );
1816}
1817
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001818
1819
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001820/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001821/* Message digests */
1822/****************************************************************/
1823
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001824static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001825{
1826 switch( alg )
1827 {
1828#if defined(MBEDTLS_MD2_C)
1829 case PSA_ALG_MD2:
1830 return( &mbedtls_md2_info );
1831#endif
1832#if defined(MBEDTLS_MD4_C)
1833 case PSA_ALG_MD4:
1834 return( &mbedtls_md4_info );
1835#endif
1836#if defined(MBEDTLS_MD5_C)
1837 case PSA_ALG_MD5:
1838 return( &mbedtls_md5_info );
1839#endif
1840#if defined(MBEDTLS_RIPEMD160_C)
1841 case PSA_ALG_RIPEMD160:
1842 return( &mbedtls_ripemd160_info );
1843#endif
1844#if defined(MBEDTLS_SHA1_C)
1845 case PSA_ALG_SHA_1:
1846 return( &mbedtls_sha1_info );
1847#endif
1848#if defined(MBEDTLS_SHA256_C)
1849 case PSA_ALG_SHA_224:
1850 return( &mbedtls_sha224_info );
1851 case PSA_ALG_SHA_256:
1852 return( &mbedtls_sha256_info );
1853#endif
1854#if defined(MBEDTLS_SHA512_C)
1855 case PSA_ALG_SHA_384:
1856 return( &mbedtls_sha384_info );
1857 case PSA_ALG_SHA_512:
1858 return( &mbedtls_sha512_info );
1859#endif
1860 default:
1861 return( NULL );
1862 }
1863}
1864
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001865psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1866{
1867 switch( operation->alg )
1868 {
Gilles Peskine81736312018-06-26 15:04:31 +02001869 case 0:
1870 /* The object has (apparently) been initialized but it is not
1871 * in use. It's ok to call abort on such an object, and there's
1872 * nothing to do. */
1873 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001874#if defined(MBEDTLS_MD2_C)
1875 case PSA_ALG_MD2:
1876 mbedtls_md2_free( &operation->ctx.md2 );
1877 break;
1878#endif
1879#if defined(MBEDTLS_MD4_C)
1880 case PSA_ALG_MD4:
1881 mbedtls_md4_free( &operation->ctx.md4 );
1882 break;
1883#endif
1884#if defined(MBEDTLS_MD5_C)
1885 case PSA_ALG_MD5:
1886 mbedtls_md5_free( &operation->ctx.md5 );
1887 break;
1888#endif
1889#if defined(MBEDTLS_RIPEMD160_C)
1890 case PSA_ALG_RIPEMD160:
1891 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1892 break;
1893#endif
1894#if defined(MBEDTLS_SHA1_C)
1895 case PSA_ALG_SHA_1:
1896 mbedtls_sha1_free( &operation->ctx.sha1 );
1897 break;
1898#endif
1899#if defined(MBEDTLS_SHA256_C)
1900 case PSA_ALG_SHA_224:
1901 case PSA_ALG_SHA_256:
1902 mbedtls_sha256_free( &operation->ctx.sha256 );
1903 break;
1904#endif
1905#if defined(MBEDTLS_SHA512_C)
1906 case PSA_ALG_SHA_384:
1907 case PSA_ALG_SHA_512:
1908 mbedtls_sha512_free( &operation->ctx.sha512 );
1909 break;
1910#endif
1911 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001912 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001913 }
1914 operation->alg = 0;
1915 return( PSA_SUCCESS );
1916}
1917
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001918psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001919 psa_algorithm_t alg )
1920{
1921 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001922
1923 /* A context must be freshly initialized before it can be set up. */
1924 if( operation->alg != 0 )
1925 {
1926 return( PSA_ERROR_BAD_STATE );
1927 }
1928
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001929 switch( alg )
1930 {
1931#if defined(MBEDTLS_MD2_C)
1932 case PSA_ALG_MD2:
1933 mbedtls_md2_init( &operation->ctx.md2 );
1934 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1935 break;
1936#endif
1937#if defined(MBEDTLS_MD4_C)
1938 case PSA_ALG_MD4:
1939 mbedtls_md4_init( &operation->ctx.md4 );
1940 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1941 break;
1942#endif
1943#if defined(MBEDTLS_MD5_C)
1944 case PSA_ALG_MD5:
1945 mbedtls_md5_init( &operation->ctx.md5 );
1946 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1947 break;
1948#endif
1949#if defined(MBEDTLS_RIPEMD160_C)
1950 case PSA_ALG_RIPEMD160:
1951 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1952 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1953 break;
1954#endif
1955#if defined(MBEDTLS_SHA1_C)
1956 case PSA_ALG_SHA_1:
1957 mbedtls_sha1_init( &operation->ctx.sha1 );
1958 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1959 break;
1960#endif
1961#if defined(MBEDTLS_SHA256_C)
1962 case PSA_ALG_SHA_224:
1963 mbedtls_sha256_init( &operation->ctx.sha256 );
1964 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1965 break;
1966 case PSA_ALG_SHA_256:
1967 mbedtls_sha256_init( &operation->ctx.sha256 );
1968 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1969 break;
1970#endif
1971#if defined(MBEDTLS_SHA512_C)
1972 case PSA_ALG_SHA_384:
1973 mbedtls_sha512_init( &operation->ctx.sha512 );
1974 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1975 break;
1976 case PSA_ALG_SHA_512:
1977 mbedtls_sha512_init( &operation->ctx.sha512 );
1978 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1979 break;
1980#endif
1981 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001982 return( PSA_ALG_IS_HASH( alg ) ?
1983 PSA_ERROR_NOT_SUPPORTED :
1984 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001985 }
1986 if( ret == 0 )
1987 operation->alg = alg;
1988 else
1989 psa_hash_abort( operation );
1990 return( mbedtls_to_psa_error( ret ) );
1991}
1992
1993psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1994 const uint8_t *input,
1995 size_t input_length )
1996{
1997 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001998
1999 /* Don't require hash implementations to behave correctly on a
2000 * zero-length input, which may have an invalid pointer. */
2001 if( input_length == 0 )
2002 return( PSA_SUCCESS );
2003
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002004 switch( operation->alg )
2005 {
2006#if defined(MBEDTLS_MD2_C)
2007 case PSA_ALG_MD2:
2008 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2009 input, input_length );
2010 break;
2011#endif
2012#if defined(MBEDTLS_MD4_C)
2013 case PSA_ALG_MD4:
2014 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2015 input, input_length );
2016 break;
2017#endif
2018#if defined(MBEDTLS_MD5_C)
2019 case PSA_ALG_MD5:
2020 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2021 input, input_length );
2022 break;
2023#endif
2024#if defined(MBEDTLS_RIPEMD160_C)
2025 case PSA_ALG_RIPEMD160:
2026 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2027 input, input_length );
2028 break;
2029#endif
2030#if defined(MBEDTLS_SHA1_C)
2031 case PSA_ALG_SHA_1:
2032 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2033 input, input_length );
2034 break;
2035#endif
2036#if defined(MBEDTLS_SHA256_C)
2037 case PSA_ALG_SHA_224:
2038 case PSA_ALG_SHA_256:
2039 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2040 input, input_length );
2041 break;
2042#endif
2043#if defined(MBEDTLS_SHA512_C)
2044 case PSA_ALG_SHA_384:
2045 case PSA_ALG_SHA_512:
2046 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2047 input, input_length );
2048 break;
2049#endif
2050 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002051 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002052 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002053
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002054 if( ret != 0 )
2055 psa_hash_abort( operation );
2056 return( mbedtls_to_psa_error( ret ) );
2057}
2058
2059psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2060 uint8_t *hash,
2061 size_t hash_size,
2062 size_t *hash_length )
2063{
itayzafrir40835d42018-08-02 13:14:17 +03002064 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002065 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002066 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002067
2068 /* Fill the output buffer with something that isn't a valid hash
2069 * (barring an attack on the hash and deliberately-crafted input),
2070 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002071 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002072 /* If hash_size is 0 then hash may be NULL and then the
2073 * call to memset would have undefined behavior. */
2074 if( hash_size != 0 )
2075 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002076
2077 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002078 {
2079 status = PSA_ERROR_BUFFER_TOO_SMALL;
2080 goto exit;
2081 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002082
2083 switch( operation->alg )
2084 {
2085#if defined(MBEDTLS_MD2_C)
2086 case PSA_ALG_MD2:
2087 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2088 break;
2089#endif
2090#if defined(MBEDTLS_MD4_C)
2091 case PSA_ALG_MD4:
2092 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2093 break;
2094#endif
2095#if defined(MBEDTLS_MD5_C)
2096 case PSA_ALG_MD5:
2097 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2098 break;
2099#endif
2100#if defined(MBEDTLS_RIPEMD160_C)
2101 case PSA_ALG_RIPEMD160:
2102 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2103 break;
2104#endif
2105#if defined(MBEDTLS_SHA1_C)
2106 case PSA_ALG_SHA_1:
2107 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2108 break;
2109#endif
2110#if defined(MBEDTLS_SHA256_C)
2111 case PSA_ALG_SHA_224:
2112 case PSA_ALG_SHA_256:
2113 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2114 break;
2115#endif
2116#if defined(MBEDTLS_SHA512_C)
2117 case PSA_ALG_SHA_384:
2118 case PSA_ALG_SHA_512:
2119 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2120 break;
2121#endif
2122 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002123 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002124 }
itayzafrir40835d42018-08-02 13:14:17 +03002125 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002126
itayzafrir40835d42018-08-02 13:14:17 +03002127exit:
2128 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002129 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002130 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002131 return( psa_hash_abort( operation ) );
2132 }
2133 else
2134 {
2135 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002136 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002137 }
2138}
2139
Gilles Peskine2d277862018-06-18 15:41:12 +02002140psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2141 const uint8_t *hash,
2142 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002143{
2144 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2145 size_t actual_hash_length;
2146 psa_status_t status = psa_hash_finish( operation,
2147 actual_hash, sizeof( actual_hash ),
2148 &actual_hash_length );
2149 if( status != PSA_SUCCESS )
2150 return( status );
2151 if( actual_hash_length != hash_length )
2152 return( PSA_ERROR_INVALID_SIGNATURE );
2153 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2154 return( PSA_ERROR_INVALID_SIGNATURE );
2155 return( PSA_SUCCESS );
2156}
2157
Gilles Peskineeb35d782019-01-22 17:56:16 +01002158psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2159 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002160{
2161 if( target_operation->alg != 0 )
2162 return( PSA_ERROR_BAD_STATE );
2163
2164 switch( source_operation->alg )
2165 {
2166 case 0:
2167 return( PSA_ERROR_BAD_STATE );
2168#if defined(MBEDTLS_MD2_C)
2169 case PSA_ALG_MD2:
2170 mbedtls_md2_clone( &target_operation->ctx.md2,
2171 &source_operation->ctx.md2 );
2172 break;
2173#endif
2174#if defined(MBEDTLS_MD4_C)
2175 case PSA_ALG_MD4:
2176 mbedtls_md4_clone( &target_operation->ctx.md4,
2177 &source_operation->ctx.md4 );
2178 break;
2179#endif
2180#if defined(MBEDTLS_MD5_C)
2181 case PSA_ALG_MD5:
2182 mbedtls_md5_clone( &target_operation->ctx.md5,
2183 &source_operation->ctx.md5 );
2184 break;
2185#endif
2186#if defined(MBEDTLS_RIPEMD160_C)
2187 case PSA_ALG_RIPEMD160:
2188 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2189 &source_operation->ctx.ripemd160 );
2190 break;
2191#endif
2192#if defined(MBEDTLS_SHA1_C)
2193 case PSA_ALG_SHA_1:
2194 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2195 &source_operation->ctx.sha1 );
2196 break;
2197#endif
2198#if defined(MBEDTLS_SHA256_C)
2199 case PSA_ALG_SHA_224:
2200 case PSA_ALG_SHA_256:
2201 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2202 &source_operation->ctx.sha256 );
2203 break;
2204#endif
2205#if defined(MBEDTLS_SHA512_C)
2206 case PSA_ALG_SHA_384:
2207 case PSA_ALG_SHA_512:
2208 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2209 &source_operation->ctx.sha512 );
2210 break;
2211#endif
2212 default:
2213 return( PSA_ERROR_NOT_SUPPORTED );
2214 }
2215
2216 target_operation->alg = source_operation->alg;
2217 return( PSA_SUCCESS );
2218}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002219
2220
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002222/* MAC */
2223/****************************************************************/
2224
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002225static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002226 psa_algorithm_t alg,
2227 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002228 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002229 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002230{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002231 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002232 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002233
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002234 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002235 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002236
Gilles Peskine8c9def32018-02-08 10:02:12 +01002237 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2238 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002239 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002240 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002241 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002242 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002243 mode = MBEDTLS_MODE_STREAM;
2244 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002245 case PSA_ALG_CTR:
2246 mode = MBEDTLS_MODE_CTR;
2247 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002248 case PSA_ALG_CFB:
2249 mode = MBEDTLS_MODE_CFB;
2250 break;
2251 case PSA_ALG_OFB:
2252 mode = MBEDTLS_MODE_OFB;
2253 break;
2254 case PSA_ALG_CBC_NO_PADDING:
2255 mode = MBEDTLS_MODE_CBC;
2256 break;
2257 case PSA_ALG_CBC_PKCS7:
2258 mode = MBEDTLS_MODE_CBC;
2259 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002260 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002261 mode = MBEDTLS_MODE_CCM;
2262 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002263 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002264 mode = MBEDTLS_MODE_GCM;
2265 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002266 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2267 mode = MBEDTLS_MODE_CHACHAPOLY;
2268 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002269 default:
2270 return( NULL );
2271 }
2272 }
2273 else if( alg == PSA_ALG_CMAC )
2274 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002275 else
2276 return( NULL );
2277
2278 switch( key_type )
2279 {
2280 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002281 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002282 break;
2283 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002284 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2285 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002286 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002287 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002288 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002289 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002290 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2291 * but two-key Triple-DES is functionally three-key Triple-DES
2292 * with K1=K3, so that's how we present it to mbedtls. */
2293 if( key_bits == 128 )
2294 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002295 break;
2296 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002297 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002298 break;
2299 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002300 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002301 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002302 case PSA_KEY_TYPE_CHACHA20:
2303 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2304 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002305 default:
2306 return( NULL );
2307 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002308 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002309 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002310
Jaeden Amero23bbb752018-06-26 14:16:54 +01002311 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2312 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002313}
2314
Gilles Peskinea05219c2018-11-16 16:02:56 +01002315#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002316static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002317{
Gilles Peskine2d277862018-06-18 15:41:12 +02002318 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002319 {
2320 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002321 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002322 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002323 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002324 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002325 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002326 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002327 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002328 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002329 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002330 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002331 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002332 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002333 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002334 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002335 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002336 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002337 return( 128 );
2338 default:
2339 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002340 }
2341}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002342#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002343
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002344/* Initialize the MAC operation structure. Once this function has been
2345 * called, psa_mac_abort can run and will do the right thing. */
2346static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2347 psa_algorithm_t alg )
2348{
2349 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2350
2351 operation->alg = alg;
2352 operation->key_set = 0;
2353 operation->iv_set = 0;
2354 operation->iv_required = 0;
2355 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002356 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002357
2358#if defined(MBEDTLS_CMAC_C)
2359 if( alg == PSA_ALG_CMAC )
2360 {
2361 operation->iv_required = 0;
2362 mbedtls_cipher_init( &operation->ctx.cmac );
2363 status = PSA_SUCCESS;
2364 }
2365 else
2366#endif /* MBEDTLS_CMAC_C */
2367#if defined(MBEDTLS_MD_C)
2368 if( PSA_ALG_IS_HMAC( operation->alg ) )
2369 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002370 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2371 operation->ctx.hmac.hash_ctx.alg = 0;
2372 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002373 }
2374 else
2375#endif /* MBEDTLS_MD_C */
2376 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002377 if( ! PSA_ALG_IS_MAC( alg ) )
2378 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002379 }
2380
2381 if( status != PSA_SUCCESS )
2382 memset( operation, 0, sizeof( *operation ) );
2383 return( status );
2384}
2385
Gilles Peskine01126fa2018-07-12 17:04:55 +02002386#if defined(MBEDTLS_MD_C)
2387static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2388{
Gilles Peskine3f108122018-12-07 18:14:53 +01002389 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002390 return( psa_hash_abort( &hmac->hash_ctx ) );
2391}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002392
Janos Follath999f6482019-06-11 12:04:10 +01002393#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002394static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2395{
2396 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2397 memset( hmac, 0, sizeof( *hmac ) );
2398}
Janos Follath999f6482019-06-11 12:04:10 +01002399#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002400#endif /* MBEDTLS_MD_C */
2401
Gilles Peskine8c9def32018-02-08 10:02:12 +01002402psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2403{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002404 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002405 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002406 /* The object has (apparently) been initialized but it is not
2407 * in use. It's ok to call abort on such an object, and there's
2408 * nothing to do. */
2409 return( PSA_SUCCESS );
2410 }
2411 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002412#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002413 if( operation->alg == PSA_ALG_CMAC )
2414 {
2415 mbedtls_cipher_free( &operation->ctx.cmac );
2416 }
2417 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002419#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002420 if( PSA_ALG_IS_HMAC( operation->alg ) )
2421 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002422 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002423 }
2424 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002425#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002426 {
2427 /* Sanity check (shouldn't happen: operation->alg should
2428 * always have been initialized to a valid value). */
2429 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430 }
Moran Peker41deec42018-04-04 15:43:05 +03002431
Gilles Peskine8c9def32018-02-08 10:02:12 +01002432 operation->alg = 0;
2433 operation->key_set = 0;
2434 operation->iv_set = 0;
2435 operation->iv_required = 0;
2436 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002437 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002438
Gilles Peskine8c9def32018-02-08 10:02:12 +01002439 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002440
2441bad_state:
2442 /* If abort is called on an uninitialized object, we can't trust
2443 * anything. Wipe the object in case it contains confidential data.
2444 * This may result in a memory leak if a pointer gets overwritten,
2445 * but it's too late to do anything about this. */
2446 memset( operation, 0, sizeof( *operation ) );
2447 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002448}
2449
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002450#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002451static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002452 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002453 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002454 const mbedtls_cipher_info_t *cipher_info )
2455{
2456 int ret;
2457
2458 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002459
2460 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2461 if( ret != 0 )
2462 return( ret );
2463
2464 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2465 slot->data.raw.data,
2466 key_bits );
2467 return( ret );
2468}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002469#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002470
Gilles Peskine248051a2018-06-20 16:09:38 +02002471#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002472static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2473 const uint8_t *key,
2474 size_t key_length,
2475 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002476{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002477 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002478 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002479 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002480 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002481 psa_status_t status;
2482
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002483 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2484 * overflow below. This should never trigger if the hash algorithm
2485 * is implemented correctly. */
2486 /* The size checks against the ipad and opad buffers cannot be written
2487 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2488 * because that triggers -Wlogical-op on GCC 7.3. */
2489 if( block_size > sizeof( ipad ) )
2490 return( PSA_ERROR_NOT_SUPPORTED );
2491 if( block_size > sizeof( hmac->opad ) )
2492 return( PSA_ERROR_NOT_SUPPORTED );
2493 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002494 return( PSA_ERROR_NOT_SUPPORTED );
2495
Gilles Peskined223b522018-06-11 18:12:58 +02002496 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002497 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002498 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002499 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002500 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002501 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002502 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002503 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002504 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002505 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002506 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002507 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002508 }
Gilles Peskine96889972018-07-12 17:07:03 +02002509 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2510 * but it is permitted. It is common when HMAC is used in HKDF, for
2511 * example. Don't call `memcpy` in the 0-length because `key` could be
2512 * an invalid pointer which would make the behavior undefined. */
2513 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002514 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002515
Gilles Peskined223b522018-06-11 18:12:58 +02002516 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2517 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002518 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002519 ipad[i] ^= 0x36;
2520 memset( ipad + key_length, 0x36, block_size - key_length );
2521
2522 /* Copy the key material from ipad to opad, flipping the requisite bits,
2523 * and filling the rest of opad with the requisite constant. */
2524 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002525 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2526 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002527
Gilles Peskine01126fa2018-07-12 17:04:55 +02002528 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002529 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002530 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002531
Gilles Peskine01126fa2018-07-12 17:04:55 +02002532 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002533
2534cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002535 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002536
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002537 return( status );
2538}
Gilles Peskine248051a2018-06-20 16:09:38 +02002539#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002540
Gilles Peskine89167cb2018-07-08 20:12:23 +02002541static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002542 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002543 psa_algorithm_t alg,
2544 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002547 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002549 psa_key_usage_t usage =
2550 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002551 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002552 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002553
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002554 /* A context must be freshly initialized before it can be set up. */
2555 if( operation->alg != 0 )
2556 {
2557 return( PSA_ERROR_BAD_STATE );
2558 }
2559
Gilles Peskined911eb72018-08-14 15:18:45 +02002560 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002561 if( status != PSA_SUCCESS )
2562 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002563 if( is_sign )
2564 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002565
Gilles Peskine28f8f302019-07-24 13:30:31 +02002566 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002567 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002568 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002569 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002570
Gilles Peskine8c9def32018-02-08 10:02:12 +01002571#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002572 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002573 {
2574 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002575 mbedtls_cipher_info_from_psa( full_length_alg,
2576 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002577 int ret;
2578 if( cipher_info == NULL )
2579 {
2580 status = PSA_ERROR_NOT_SUPPORTED;
2581 goto exit;
2582 }
2583 operation->mac_size = cipher_info->block_size;
2584 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2585 status = mbedtls_to_psa_error( ret );
2586 }
2587 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002589#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002590 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002591 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002592 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002593 if( hash_alg == 0 )
2594 {
2595 status = PSA_ERROR_NOT_SUPPORTED;
2596 goto exit;
2597 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002598
2599 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2600 /* Sanity check. This shouldn't fail on a valid configuration. */
2601 if( operation->mac_size == 0 ||
2602 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2603 {
2604 status = PSA_ERROR_NOT_SUPPORTED;
2605 goto exit;
2606 }
2607
Gilles Peskine01126fa2018-07-12 17:04:55 +02002608 if( slot->type != PSA_KEY_TYPE_HMAC )
2609 {
2610 status = PSA_ERROR_INVALID_ARGUMENT;
2611 goto exit;
2612 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002613
Gilles Peskine01126fa2018-07-12 17:04:55 +02002614 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2615 slot->data.raw.data,
2616 slot->data.raw.bytes,
2617 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002618 }
2619 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002620#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002621 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002622 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002623 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002624 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002625
Gilles Peskined911eb72018-08-14 15:18:45 +02002626 if( truncated == 0 )
2627 {
2628 /* The "normal" case: untruncated algorithm. Nothing to do. */
2629 }
2630 else if( truncated < 4 )
2631 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002632 /* A very short MAC is too short for security since it can be
2633 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2634 * so we make this our minimum, even though 32 bits is still
2635 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002636 status = PSA_ERROR_NOT_SUPPORTED;
2637 }
2638 else if( truncated > operation->mac_size )
2639 {
2640 /* It's impossible to "truncate" to a larger length. */
2641 status = PSA_ERROR_INVALID_ARGUMENT;
2642 }
2643 else
2644 operation->mac_size = truncated;
2645
Gilles Peskinefbfac682018-07-08 20:51:54 +02002646exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002647 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002648 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002649 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002650 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002651 else
2652 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002653 operation->key_set = 1;
2654 }
2655 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002656}
2657
Gilles Peskine89167cb2018-07-08 20:12:23 +02002658psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002659 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002660 psa_algorithm_t alg )
2661{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002662 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002663}
2664
2665psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002666 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002667 psa_algorithm_t alg )
2668{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002669 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002670}
2671
Gilles Peskine8c9def32018-02-08 10:02:12 +01002672psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2673 const uint8_t *input,
2674 size_t input_length )
2675{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002676 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002677 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002678 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002679 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002680 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681 operation->has_input = 1;
2682
Gilles Peskine8c9def32018-02-08 10:02:12 +01002683#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002684 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002685 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002686 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2687 input, input_length );
2688 status = mbedtls_to_psa_error( ret );
2689 }
2690 else
2691#endif /* MBEDTLS_CMAC_C */
2692#if defined(MBEDTLS_MD_C)
2693 if( PSA_ALG_IS_HMAC( operation->alg ) )
2694 {
2695 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2696 input_length );
2697 }
2698 else
2699#endif /* MBEDTLS_MD_C */
2700 {
2701 /* This shouldn't happen if `operation` was initialized by
2702 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002703 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002704 }
2705
Gilles Peskinefbfac682018-07-08 20:51:54 +02002706 if( status != PSA_SUCCESS )
2707 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002708 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002709}
2710
Gilles Peskine01126fa2018-07-12 17:04:55 +02002711#if defined(MBEDTLS_MD_C)
2712static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2713 uint8_t *mac,
2714 size_t mac_size )
2715{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002716 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002717 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2718 size_t hash_size = 0;
2719 size_t block_size = psa_get_hash_block_size( hash_alg );
2720 psa_status_t status;
2721
Gilles Peskine01126fa2018-07-12 17:04:55 +02002722 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2723 if( status != PSA_SUCCESS )
2724 return( status );
2725 /* From here on, tmp needs to be wiped. */
2726
2727 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2728 if( status != PSA_SUCCESS )
2729 goto exit;
2730
2731 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2732 if( status != PSA_SUCCESS )
2733 goto exit;
2734
2735 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2736 if( status != PSA_SUCCESS )
2737 goto exit;
2738
Gilles Peskined911eb72018-08-14 15:18:45 +02002739 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2740 if( status != PSA_SUCCESS )
2741 goto exit;
2742
2743 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002744
2745exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002746 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002747 return( status );
2748}
2749#endif /* MBEDTLS_MD_C */
2750
mohammad16036df908f2018-04-02 08:34:15 -07002751static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002752 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002753 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002754{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002755 if( ! operation->key_set )
2756 return( PSA_ERROR_BAD_STATE );
2757 if( operation->iv_required && ! operation->iv_set )
2758 return( PSA_ERROR_BAD_STATE );
2759
Gilles Peskine8c9def32018-02-08 10:02:12 +01002760 if( mac_size < operation->mac_size )
2761 return( PSA_ERROR_BUFFER_TOO_SMALL );
2762
Gilles Peskine8c9def32018-02-08 10:02:12 +01002763#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002764 if( operation->alg == PSA_ALG_CMAC )
2765 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002766 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2767 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2768 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002769 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002770 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002771 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002772 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002773 else
2774#endif /* MBEDTLS_CMAC_C */
2775#if defined(MBEDTLS_MD_C)
2776 if( PSA_ALG_IS_HMAC( operation->alg ) )
2777 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002778 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002779 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002780 }
2781 else
2782#endif /* MBEDTLS_MD_C */
2783 {
2784 /* This shouldn't happen if `operation` was initialized by
2785 * a setup function. */
2786 return( PSA_ERROR_BAD_STATE );
2787 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002788}
2789
Gilles Peskineacd4be32018-07-08 19:56:25 +02002790psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2791 uint8_t *mac,
2792 size_t mac_size,
2793 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002794{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002795 psa_status_t status;
2796
Jaeden Amero252ef282019-02-15 14:05:35 +00002797 if( operation->alg == 0 )
2798 {
2799 return( PSA_ERROR_BAD_STATE );
2800 }
2801
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002802 /* Fill the output buffer with something that isn't a valid mac
2803 * (barring an attack on the mac and deliberately-crafted input),
2804 * in case the caller doesn't check the return status properly. */
2805 *mac_length = mac_size;
2806 /* If mac_size is 0 then mac may be NULL and then the
2807 * call to memset would have undefined behavior. */
2808 if( mac_size != 0 )
2809 memset( mac, '!', mac_size );
2810
Gilles Peskine89167cb2018-07-08 20:12:23 +02002811 if( ! operation->is_sign )
2812 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002813 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002814 }
mohammad16036df908f2018-04-02 08:34:15 -07002815
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002816 status = psa_mac_finish_internal( operation, mac, mac_size );
2817
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002818 if( status == PSA_SUCCESS )
2819 {
2820 status = psa_mac_abort( operation );
2821 if( status == PSA_SUCCESS )
2822 *mac_length = operation->mac_size;
2823 else
2824 memset( mac, '!', mac_size );
2825 }
2826 else
2827 psa_mac_abort( operation );
2828 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002829}
2830
Gilles Peskineacd4be32018-07-08 19:56:25 +02002831psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2832 const uint8_t *mac,
2833 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002834{
Gilles Peskine828ed142018-06-18 23:25:51 +02002835 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002836 psa_status_t status;
2837
Jaeden Amero252ef282019-02-15 14:05:35 +00002838 if( operation->alg == 0 )
2839 {
2840 return( PSA_ERROR_BAD_STATE );
2841 }
2842
Gilles Peskine89167cb2018-07-08 20:12:23 +02002843 if( operation->is_sign )
2844 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002845 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002846 }
2847 if( operation->mac_size != mac_length )
2848 {
2849 status = PSA_ERROR_INVALID_SIGNATURE;
2850 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002851 }
mohammad16036df908f2018-04-02 08:34:15 -07002852
2853 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002854 actual_mac, sizeof( actual_mac ) );
2855
2856 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2857 status = PSA_ERROR_INVALID_SIGNATURE;
2858
2859cleanup:
2860 if( status == PSA_SUCCESS )
2861 status = psa_mac_abort( operation );
2862 else
2863 psa_mac_abort( operation );
2864
Gilles Peskine3f108122018-12-07 18:14:53 +01002865 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002866
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002867 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002868}
2869
2870
Gilles Peskine20035e32018-02-03 22:44:14 +01002871
Gilles Peskine20035e32018-02-03 22:44:14 +01002872/****************************************************************/
2873/* Asymmetric cryptography */
2874/****************************************************************/
2875
Gilles Peskine2b450e32018-06-27 15:42:46 +02002876#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002877/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002878 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002879static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2880 size_t hash_length,
2881 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002882{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002883 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002884 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002885 *md_alg = mbedtls_md_get_type( md_info );
2886
2887 /* The Mbed TLS RSA module uses an unsigned int for hash length
2888 * parameters. Validate that it fits so that we don't risk an
2889 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002890#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002891 if( hash_length > UINT_MAX )
2892 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002893#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002894
2895#if defined(MBEDTLS_PKCS1_V15)
2896 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2897 * must be correct. */
2898 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2899 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002900 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002901 if( md_info == NULL )
2902 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002903 if( mbedtls_md_get_size( md_info ) != hash_length )
2904 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002905 }
2906#endif /* MBEDTLS_PKCS1_V15 */
2907
2908#if defined(MBEDTLS_PKCS1_V21)
2909 /* PSS requires a hash internally. */
2910 if( PSA_ALG_IS_RSA_PSS( alg ) )
2911 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002912 if( md_info == NULL )
2913 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002914 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002915#endif /* MBEDTLS_PKCS1_V21 */
2916
Gilles Peskine61b91d42018-06-08 16:09:36 +02002917 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002918}
2919
Gilles Peskine2b450e32018-06-27 15:42:46 +02002920static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2921 psa_algorithm_t alg,
2922 const uint8_t *hash,
2923 size_t hash_length,
2924 uint8_t *signature,
2925 size_t signature_size,
2926 size_t *signature_length )
2927{
2928 psa_status_t status;
2929 int ret;
2930 mbedtls_md_type_t md_alg;
2931
2932 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2933 if( status != PSA_SUCCESS )
2934 return( status );
2935
Gilles Peskine630a18a2018-06-29 17:49:35 +02002936 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002937 return( PSA_ERROR_BUFFER_TOO_SMALL );
2938
2939#if defined(MBEDTLS_PKCS1_V15)
2940 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2941 {
2942 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2943 MBEDTLS_MD_NONE );
2944 ret = mbedtls_rsa_pkcs1_sign( rsa,
2945 mbedtls_ctr_drbg_random,
2946 &global_data.ctr_drbg,
2947 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002948 md_alg,
2949 (unsigned int) hash_length,
2950 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002951 signature );
2952 }
2953 else
2954#endif /* MBEDTLS_PKCS1_V15 */
2955#if defined(MBEDTLS_PKCS1_V21)
2956 if( PSA_ALG_IS_RSA_PSS( alg ) )
2957 {
2958 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2959 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2960 mbedtls_ctr_drbg_random,
2961 &global_data.ctr_drbg,
2962 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002963 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002964 (unsigned int) hash_length,
2965 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002966 signature );
2967 }
2968 else
2969#endif /* MBEDTLS_PKCS1_V21 */
2970 {
2971 return( PSA_ERROR_INVALID_ARGUMENT );
2972 }
2973
2974 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002975 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002976 return( mbedtls_to_psa_error( ret ) );
2977}
2978
2979static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2980 psa_algorithm_t alg,
2981 const uint8_t *hash,
2982 size_t hash_length,
2983 const uint8_t *signature,
2984 size_t signature_length )
2985{
2986 psa_status_t status;
2987 int ret;
2988 mbedtls_md_type_t md_alg;
2989
2990 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2991 if( status != PSA_SUCCESS )
2992 return( status );
2993
Gilles Peskine630a18a2018-06-29 17:49:35 +02002994 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002995 return( PSA_ERROR_BUFFER_TOO_SMALL );
2996
2997#if defined(MBEDTLS_PKCS1_V15)
2998 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2999 {
3000 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3001 MBEDTLS_MD_NONE );
3002 ret = mbedtls_rsa_pkcs1_verify( rsa,
3003 mbedtls_ctr_drbg_random,
3004 &global_data.ctr_drbg,
3005 MBEDTLS_RSA_PUBLIC,
3006 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003007 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003008 hash,
3009 signature );
3010 }
3011 else
3012#endif /* MBEDTLS_PKCS1_V15 */
3013#if defined(MBEDTLS_PKCS1_V21)
3014 if( PSA_ALG_IS_RSA_PSS( alg ) )
3015 {
3016 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3017 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3018 mbedtls_ctr_drbg_random,
3019 &global_data.ctr_drbg,
3020 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003021 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003022 (unsigned int) hash_length,
3023 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003024 signature );
3025 }
3026 else
3027#endif /* MBEDTLS_PKCS1_V21 */
3028 {
3029 return( PSA_ERROR_INVALID_ARGUMENT );
3030 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003031
3032 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3033 * the rest of the signature is invalid". This has little use in
3034 * practice and PSA doesn't report this distinction. */
3035 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3036 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003037 return( mbedtls_to_psa_error( ret ) );
3038}
3039#endif /* MBEDTLS_RSA_C */
3040
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003041#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003042/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3043 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3044 * (even though these functions don't modify it). */
3045static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3046 psa_algorithm_t alg,
3047 const uint8_t *hash,
3048 size_t hash_length,
3049 uint8_t *signature,
3050 size_t signature_size,
3051 size_t *signature_length )
3052{
3053 int ret;
3054 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003055 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003056 mbedtls_mpi_init( &r );
3057 mbedtls_mpi_init( &s );
3058
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003059 if( signature_size < 2 * curve_bytes )
3060 {
3061 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3062 goto cleanup;
3063 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003064
Gilles Peskinea05219c2018-11-16 16:02:56 +01003065#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003066 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3067 {
3068 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3069 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3070 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3071 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3072 hash, hash_length,
3073 md_alg ) );
3074 }
3075 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003076#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003077 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003078 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003079 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3080 hash, hash_length,
3081 mbedtls_ctr_drbg_random,
3082 &global_data.ctr_drbg ) );
3083 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003084
3085 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3086 signature,
3087 curve_bytes ) );
3088 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3089 signature + curve_bytes,
3090 curve_bytes ) );
3091
3092cleanup:
3093 mbedtls_mpi_free( &r );
3094 mbedtls_mpi_free( &s );
3095 if( ret == 0 )
3096 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003097 return( mbedtls_to_psa_error( ret ) );
3098}
3099
3100static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3101 const uint8_t *hash,
3102 size_t hash_length,
3103 const uint8_t *signature,
3104 size_t signature_length )
3105{
3106 int ret;
3107 mbedtls_mpi r, s;
3108 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3109 mbedtls_mpi_init( &r );
3110 mbedtls_mpi_init( &s );
3111
3112 if( signature_length != 2 * curve_bytes )
3113 return( PSA_ERROR_INVALID_SIGNATURE );
3114
3115 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3116 signature,
3117 curve_bytes ) );
3118 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3119 signature + curve_bytes,
3120 curve_bytes ) );
3121
3122 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3123 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003124
3125cleanup:
3126 mbedtls_mpi_free( &r );
3127 mbedtls_mpi_free( &s );
3128 return( mbedtls_to_psa_error( ret ) );
3129}
3130#endif /* MBEDTLS_ECDSA_C */
3131
Gilles Peskinec5487a82018-12-03 18:08:14 +01003132psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003133 psa_algorithm_t alg,
3134 const uint8_t *hash,
3135 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003136 uint8_t *signature,
3137 size_t signature_size,
3138 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003139{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003140 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003141 psa_status_t status;
3142
3143 *signature_length = signature_size;
3144
Gilles Peskine28f8f302019-07-24 13:30:31 +02003145 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003146 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003147 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003148 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003149 {
3150 status = PSA_ERROR_INVALID_ARGUMENT;
3151 goto exit;
3152 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003153
Gilles Peskine20035e32018-02-03 22:44:14 +01003154#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003155 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003156 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003157 status = psa_rsa_sign( slot->data.rsa,
3158 alg,
3159 hash, hash_length,
3160 signature, signature_size,
3161 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003162 }
3163 else
3164#endif /* defined(MBEDTLS_RSA_C) */
3165#if defined(MBEDTLS_ECP_C)
3166 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3167 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003168#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003169 if(
3170#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3171 PSA_ALG_IS_ECDSA( alg )
3172#else
3173 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3174#endif
3175 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003176 status = psa_ecdsa_sign( slot->data.ecp,
3177 alg,
3178 hash, hash_length,
3179 signature, signature_size,
3180 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003181 else
3182#endif /* defined(MBEDTLS_ECDSA_C) */
3183 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003184 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003185 }
itayzafrir5c753392018-05-08 11:18:38 +03003186 }
3187 else
3188#endif /* defined(MBEDTLS_ECP_C) */
3189 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003190 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003191 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003192
3193exit:
3194 /* Fill the unused part of the output buffer (the whole buffer on error,
3195 * the trailing part on success) with something that isn't a valid mac
3196 * (barring an attack on the mac and deliberately-crafted input),
3197 * in case the caller doesn't check the return status properly. */
3198 if( status == PSA_SUCCESS )
3199 memset( signature + *signature_length, '!',
3200 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003201 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003202 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003203 /* If signature_size is 0 then we have nothing to do. We must not call
3204 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003205 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003206}
3207
Gilles Peskinec5487a82018-12-03 18:08:14 +01003208psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003209 psa_algorithm_t alg,
3210 const uint8_t *hash,
3211 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003212 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003213 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003214{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003215 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003216 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003217
Gilles Peskine28f8f302019-07-24 13:30:31 +02003218 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003219 if( status != PSA_SUCCESS )
3220 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003221
Gilles Peskine61b91d42018-06-08 16:09:36 +02003222#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003223 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003224 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003225 return( psa_rsa_verify( slot->data.rsa,
3226 alg,
3227 hash, hash_length,
3228 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003229 }
3230 else
3231#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003232#if defined(MBEDTLS_ECP_C)
3233 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3234 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003235#if defined(MBEDTLS_ECDSA_C)
3236 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003237 return( psa_ecdsa_verify( slot->data.ecp,
3238 hash, hash_length,
3239 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003240 else
3241#endif /* defined(MBEDTLS_ECDSA_C) */
3242 {
3243 return( PSA_ERROR_INVALID_ARGUMENT );
3244 }
itayzafrir5c753392018-05-08 11:18:38 +03003245 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003246 else
3247#endif /* defined(MBEDTLS_ECP_C) */
3248 {
3249 return( PSA_ERROR_NOT_SUPPORTED );
3250 }
3251}
3252
Gilles Peskine072ac562018-06-30 00:21:29 +02003253#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3254static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3255 mbedtls_rsa_context *rsa )
3256{
3257 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3258 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3259 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3260 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3261}
3262#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3263
Gilles Peskinec5487a82018-12-03 18:08:14 +01003264psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003265 psa_algorithm_t alg,
3266 const uint8_t *input,
3267 size_t input_length,
3268 const uint8_t *salt,
3269 size_t salt_length,
3270 uint8_t *output,
3271 size_t output_size,
3272 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003273{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003274 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003275 psa_status_t status;
3276
Darryl Green5cc689a2018-07-24 15:34:10 +01003277 (void) input;
3278 (void) input_length;
3279 (void) salt;
3280 (void) output;
3281 (void) output_size;
3282
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003283 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003284
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003285 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3286 return( PSA_ERROR_INVALID_ARGUMENT );
3287
Gilles Peskine28f8f302019-07-24 13:30:31 +02003288 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003289 if( status != PSA_SUCCESS )
3290 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003291 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003292 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003293 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003294
3295#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003296 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003297 {
3298 mbedtls_rsa_context *rsa = slot->data.rsa;
3299 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003300 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003301 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003302#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003303 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003305 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3306 mbedtls_ctr_drbg_random,
3307 &global_data.ctr_drbg,
3308 MBEDTLS_RSA_PUBLIC,
3309 input_length,
3310 input,
3311 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003312 }
3313 else
3314#endif /* MBEDTLS_PKCS1_V15 */
3315#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003316 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003317 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003318 psa_rsa_oaep_set_padding_mode( alg, rsa );
3319 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3320 mbedtls_ctr_drbg_random,
3321 &global_data.ctr_drbg,
3322 MBEDTLS_RSA_PUBLIC,
3323 salt, salt_length,
3324 input_length,
3325 input,
3326 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003327 }
3328 else
3329#endif /* MBEDTLS_PKCS1_V21 */
3330 {
3331 return( PSA_ERROR_INVALID_ARGUMENT );
3332 }
3333 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003334 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003335 return( mbedtls_to_psa_error( ret ) );
3336 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003337 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003338#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003339 {
3340 return( PSA_ERROR_NOT_SUPPORTED );
3341 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003342}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003343
Gilles Peskinec5487a82018-12-03 18:08:14 +01003344psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003345 psa_algorithm_t alg,
3346 const uint8_t *input,
3347 size_t input_length,
3348 const uint8_t *salt,
3349 size_t salt_length,
3350 uint8_t *output,
3351 size_t output_size,
3352 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003353{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003354 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003355 psa_status_t status;
3356
Darryl Green5cc689a2018-07-24 15:34:10 +01003357 (void) input;
3358 (void) input_length;
3359 (void) salt;
3360 (void) output;
3361 (void) output_size;
3362
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003363 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003364
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003365 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3366 return( PSA_ERROR_INVALID_ARGUMENT );
3367
Gilles Peskine28f8f302019-07-24 13:30:31 +02003368 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003369 if( status != PSA_SUCCESS )
3370 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003371 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003372 return( PSA_ERROR_INVALID_ARGUMENT );
3373
3374#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003375 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003376 {
3377 mbedtls_rsa_context *rsa = slot->data.rsa;
3378 int ret;
3379
Gilles Peskine630a18a2018-06-29 17:49:35 +02003380 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003381 return( PSA_ERROR_INVALID_ARGUMENT );
3382
3383#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003384 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003385 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003386 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3387 mbedtls_ctr_drbg_random,
3388 &global_data.ctr_drbg,
3389 MBEDTLS_RSA_PRIVATE,
3390 output_length,
3391 input,
3392 output,
3393 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003394 }
3395 else
3396#endif /* MBEDTLS_PKCS1_V15 */
3397#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003398 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003399 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003400 psa_rsa_oaep_set_padding_mode( alg, rsa );
3401 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3402 mbedtls_ctr_drbg_random,
3403 &global_data.ctr_drbg,
3404 MBEDTLS_RSA_PRIVATE,
3405 salt, salt_length,
3406 output_length,
3407 input,
3408 output,
3409 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003410 }
3411 else
3412#endif /* MBEDTLS_PKCS1_V21 */
3413 {
3414 return( PSA_ERROR_INVALID_ARGUMENT );
3415 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003416
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003417 return( mbedtls_to_psa_error( ret ) );
3418 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003419 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003420#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003421 {
3422 return( PSA_ERROR_NOT_SUPPORTED );
3423 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003424}
Gilles Peskine20035e32018-02-03 22:44:14 +01003425
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003426
3427
mohammad1603503973b2018-03-12 15:59:30 +02003428/****************************************************************/
3429/* Symmetric cryptography */
3430/****************************************************************/
3431
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003432/* Initialize the cipher operation structure. Once this function has been
3433 * called, psa_cipher_abort can run and will do the right thing. */
3434static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3435 psa_algorithm_t alg )
3436{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003437 if( ! PSA_ALG_IS_CIPHER( alg ) )
3438 {
3439 memset( operation, 0, sizeof( *operation ) );
3440 return( PSA_ERROR_INVALID_ARGUMENT );
3441 }
3442
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003443 operation->alg = alg;
3444 operation->key_set = 0;
3445 operation->iv_set = 0;
3446 operation->iv_required = 1;
3447 operation->iv_size = 0;
3448 operation->block_size = 0;
3449 mbedtls_cipher_init( &operation->ctx.cipher );
3450 return( PSA_SUCCESS );
3451}
3452
Gilles Peskinee553c652018-06-04 16:22:46 +02003453static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003454 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003455 psa_algorithm_t alg,
3456 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003457{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003458 int ret = 0;
3459 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003460 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003461 size_t key_bits;
3462 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003463 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3464 PSA_KEY_USAGE_ENCRYPT :
3465 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003466
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003467 /* A context must be freshly initialized before it can be set up. */
3468 if( operation->alg != 0 )
3469 {
3470 return( PSA_ERROR_BAD_STATE );
3471 }
3472
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003473 status = psa_cipher_init( operation, alg );
3474 if( status != PSA_SUCCESS )
3475 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003476
Gilles Peskine28f8f302019-07-24 13:30:31 +02003477 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003478 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003479 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003480 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003481
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003482 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003483 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003484 {
3485 status = PSA_ERROR_NOT_SUPPORTED;
3486 goto exit;
3487 }
mohammad1603503973b2018-03-12 15:59:30 +02003488
mohammad1603503973b2018-03-12 15:59:30 +02003489 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
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
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003493#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003494 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003495 {
3496 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003497 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003498 memcpy( keys, slot->data.raw.data, 16 );
3499 memcpy( keys + 16, slot->data.raw.data, 8 );
3500 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3501 keys,
3502 192, cipher_operation );
3503 }
3504 else
3505#endif
3506 {
3507 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3508 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003509 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003510 }
Moran Peker41deec42018-04-04 15:43:05 +03003511 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003512 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003513
mohammad16038481e742018-03-18 13:57:31 +02003514#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003515 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003516 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003517 case PSA_ALG_CBC_NO_PADDING:
3518 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3519 MBEDTLS_PADDING_NONE );
3520 break;
3521 case PSA_ALG_CBC_PKCS7:
3522 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3523 MBEDTLS_PADDING_PKCS7 );
3524 break;
3525 default:
3526 /* The algorithm doesn't involve padding. */
3527 ret = 0;
3528 break;
3529 }
3530 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003531 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003532#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3533
mohammad1603503973b2018-03-12 15:59:30 +02003534 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003535 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3536 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3537 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003538 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003539 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003540 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003541#if defined(MBEDTLS_CHACHA20_C)
3542 else
3543 if( alg == PSA_ALG_CHACHA20 )
3544 operation->iv_size = 12;
3545#endif
mohammad1603503973b2018-03-12 15:59:30 +02003546
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003547exit:
3548 if( status == 0 )
3549 status = mbedtls_to_psa_error( ret );
3550 if( status != 0 )
3551 psa_cipher_abort( operation );
3552 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003553}
3554
Gilles Peskinefe119512018-07-08 21:39:34 +02003555psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003556 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003557 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003558{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003559 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003560}
3561
Gilles Peskinefe119512018-07-08 21:39:34 +02003562psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003563 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003564 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003565{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003566 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003567}
3568
Gilles Peskinefe119512018-07-08 21:39:34 +02003569psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003570 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003571 size_t iv_size,
3572 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003573{
itayzafrir534bd7c2018-08-02 13:56:32 +03003574 psa_status_t status;
3575 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003576 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003577 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003578 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003579 }
Moran Peker41deec42018-04-04 15:43:05 +03003580 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003581 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003582 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003583 goto exit;
3584 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003585 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3586 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003587 if( ret != 0 )
3588 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003589 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003590 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003591 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003592
mohammad16038481e742018-03-18 13:57:31 +02003593 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003594 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003595
Moran Peker395db872018-05-31 14:07:14 +03003596exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003597 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003598 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003599 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003600}
3601
Gilles Peskinefe119512018-07-08 21:39:34 +02003602psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003603 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003604 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003605{
itayzafrir534bd7c2018-08-02 13:56:32 +03003606 psa_status_t status;
3607 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003608 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003609 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003610 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003611 }
Moran Pekera28258c2018-05-29 16:25:04 +03003612 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003613 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003614 status = PSA_ERROR_INVALID_ARGUMENT;
3615 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003616 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003617 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3618 status = mbedtls_to_psa_error( ret );
3619exit:
3620 if( status == PSA_SUCCESS )
3621 operation->iv_set = 1;
3622 else
mohammad1603503973b2018-03-12 15:59:30 +02003623 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003624 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003625}
3626
Gilles Peskinee553c652018-06-04 16:22:46 +02003627psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3628 const uint8_t *input,
3629 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003630 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003631 size_t output_size,
3632 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003633{
itayzafrir534bd7c2018-08-02 13:56:32 +03003634 psa_status_t status;
3635 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003636 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003637
3638 if( operation->alg == 0 )
3639 {
3640 return( PSA_ERROR_BAD_STATE );
3641 }
3642
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003643 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003644 {
3645 /* Take the unprocessed partial block left over from previous
3646 * update calls, if any, plus the input to this call. Remove
3647 * the last partial block, if any. You get the data that will be
3648 * output in this call. */
3649 expected_output_size =
3650 ( operation->ctx.cipher.unprocessed_len + input_length )
3651 / operation->block_size * operation->block_size;
3652 }
3653 else
3654 {
3655 expected_output_size = input_length;
3656 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003657
Gilles Peskine89d789c2018-06-04 17:17:16 +02003658 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003659 {
3660 status = PSA_ERROR_BUFFER_TOO_SMALL;
3661 goto exit;
3662 }
mohammad160382759612018-03-12 18:16:40 +02003663
mohammad1603503973b2018-03-12 15:59:30 +02003664 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003665 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003666 status = mbedtls_to_psa_error( ret );
3667exit:
3668 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003669 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003670 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003671}
3672
Gilles Peskinee553c652018-06-04 16:22:46 +02003673psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3674 uint8_t *output,
3675 size_t output_size,
3676 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003677{
David Saadab4ecc272019-02-14 13:48:10 +02003678 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003679 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003680 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003681
mohammad1603503973b2018-03-12 15:59:30 +02003682 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003683 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003684 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003685 }
3686 if( operation->iv_required && ! operation->iv_set )
3687 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003688 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003689 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003690
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003691 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003692 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3693 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003694 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003695 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003696 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003697 }
3698
Janos Follath315b51c2018-07-09 16:04:51 +01003699 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3700 temp_output_buffer,
3701 output_length );
3702 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003703 {
Janos Follath315b51c2018-07-09 16:04:51 +01003704 status = mbedtls_to_psa_error( cipher_ret );
3705 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003706 }
Janos Follath315b51c2018-07-09 16:04:51 +01003707
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003708 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003709 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003710 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003711 memcpy( output, temp_output_buffer, *output_length );
3712 else
3713 {
Janos Follath315b51c2018-07-09 16:04:51 +01003714 status = PSA_ERROR_BUFFER_TOO_SMALL;
3715 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003716 }
mohammad1603503973b2018-03-12 15:59:30 +02003717
Gilles Peskine3f108122018-12-07 18:14:53 +01003718 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003719 status = psa_cipher_abort( operation );
3720
3721 return( status );
3722
3723error:
3724
3725 *output_length = 0;
3726
Gilles Peskine3f108122018-12-07 18:14:53 +01003727 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003728 (void) psa_cipher_abort( operation );
3729
3730 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003731}
3732
Gilles Peskinee553c652018-06-04 16:22:46 +02003733psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3734{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003735 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003736 {
3737 /* The object has (apparently) been initialized but it is not
3738 * in use. It's ok to call abort on such an object, and there's
3739 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003740 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003741 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003742
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003743 /* Sanity check (shouldn't happen: operation->alg should
3744 * always have been initialized to a valid value). */
3745 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3746 return( PSA_ERROR_BAD_STATE );
3747
mohammad1603503973b2018-03-12 15:59:30 +02003748 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003749
Moran Peker41deec42018-04-04 15:43:05 +03003750 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003751 operation->key_set = 0;
3752 operation->iv_set = 0;
3753 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003754 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003755 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003756
Moran Peker395db872018-05-31 14:07:14 +03003757 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003758}
3759
Gilles Peskinea0655c32018-04-30 17:06:50 +02003760
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003761
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003762
mohammad16035955c982018-04-26 00:53:03 +03003763/****************************************************************/
3764/* AEAD */
3765/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003766
Gilles Peskineedf9a652018-08-17 18:11:56 +02003767typedef struct
3768{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003769 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003770 const mbedtls_cipher_info_t *cipher_info;
3771 union
3772 {
3773#if defined(MBEDTLS_CCM_C)
3774 mbedtls_ccm_context ccm;
3775#endif /* MBEDTLS_CCM_C */
3776#if defined(MBEDTLS_GCM_C)
3777 mbedtls_gcm_context gcm;
3778#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003779#if defined(MBEDTLS_CHACHAPOLY_C)
3780 mbedtls_chachapoly_context chachapoly;
3781#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003782 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003783 psa_algorithm_t core_alg;
3784 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003785 uint8_t tag_length;
3786} aead_operation_t;
3787
Gilles Peskine30a9e412019-01-14 18:36:12 +01003788static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003789{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003790 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003791 {
3792#if defined(MBEDTLS_CCM_C)
3793 case PSA_ALG_CCM:
3794 mbedtls_ccm_free( &operation->ctx.ccm );
3795 break;
3796#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003797#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003798 case PSA_ALG_GCM:
3799 mbedtls_gcm_free( &operation->ctx.gcm );
3800 break;
3801#endif /* MBEDTLS_GCM_C */
3802 }
3803}
3804
3805static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003806 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003807 psa_key_usage_t usage,
3808 psa_algorithm_t alg )
3809{
3810 psa_status_t status;
3811 size_t key_bits;
3812 mbedtls_cipher_id_t cipher_id;
3813
Gilles Peskine28f8f302019-07-24 13:30:31 +02003814 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003815 if( status != PSA_SUCCESS )
3816 return( status );
3817
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003818 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003819
3820 operation->cipher_info =
3821 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3822 &cipher_id );
3823 if( operation->cipher_info == NULL )
3824 return( PSA_ERROR_NOT_SUPPORTED );
3825
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003826 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003827 {
3828#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003829 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3830 operation->core_alg = PSA_ALG_CCM;
3831 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003832 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3833 * The call to mbedtls_ccm_encrypt_and_tag or
3834 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003835 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3836 return( PSA_ERROR_INVALID_ARGUMENT );
3837 mbedtls_ccm_init( &operation->ctx.ccm );
3838 status = mbedtls_to_psa_error(
3839 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3840 operation->slot->data.raw.data,
3841 (unsigned int) key_bits ) );
3842 if( status != 0 )
3843 goto cleanup;
3844 break;
3845#endif /* MBEDTLS_CCM_C */
3846
3847#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003848 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3849 operation->core_alg = PSA_ALG_GCM;
3850 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003851 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3852 * The call to mbedtls_gcm_crypt_and_tag or
3853 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003854 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3855 return( PSA_ERROR_INVALID_ARGUMENT );
3856 mbedtls_gcm_init( &operation->ctx.gcm );
3857 status = mbedtls_to_psa_error(
3858 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3859 operation->slot->data.raw.data,
3860 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003861 if( status != 0 )
3862 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003863 break;
3864#endif /* MBEDTLS_GCM_C */
3865
Gilles Peskine26869f22019-05-06 15:25:00 +02003866#if defined(MBEDTLS_CHACHAPOLY_C)
3867 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3868 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3869 operation->full_tag_length = 16;
3870 /* We only support the default tag length. */
3871 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3872 return( PSA_ERROR_NOT_SUPPORTED );
3873 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3874 status = mbedtls_to_psa_error(
3875 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3876 operation->slot->data.raw.data ) );
3877 if( status != 0 )
3878 goto cleanup;
3879 break;
3880#endif /* MBEDTLS_CHACHAPOLY_C */
3881
Gilles Peskineedf9a652018-08-17 18:11:56 +02003882 default:
3883 return( PSA_ERROR_NOT_SUPPORTED );
3884 }
3885
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003886 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3887 {
3888 status = PSA_ERROR_INVALID_ARGUMENT;
3889 goto cleanup;
3890 }
3891 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003892
Gilles Peskineedf9a652018-08-17 18:11:56 +02003893 return( PSA_SUCCESS );
3894
3895cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003896 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003897 return( status );
3898}
3899
Gilles Peskinec5487a82018-12-03 18:08:14 +01003900psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003901 psa_algorithm_t alg,
3902 const uint8_t *nonce,
3903 size_t nonce_length,
3904 const uint8_t *additional_data,
3905 size_t additional_data_length,
3906 const uint8_t *plaintext,
3907 size_t plaintext_length,
3908 uint8_t *ciphertext,
3909 size_t ciphertext_size,
3910 size_t *ciphertext_length )
3911{
mohammad16035955c982018-04-26 00:53:03 +03003912 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003913 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003914 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003915
mohammad1603f08a5502018-06-03 15:05:47 +03003916 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003917
Gilles Peskinec5487a82018-12-03 18:08:14 +01003918 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003919 if( status != PSA_SUCCESS )
3920 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003921
Gilles Peskineedf9a652018-08-17 18:11:56 +02003922 /* For all currently supported modes, the tag is at the end of the
3923 * ciphertext. */
3924 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3925 {
3926 status = PSA_ERROR_BUFFER_TOO_SMALL;
3927 goto exit;
3928 }
3929 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003930
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003931#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003932 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003933 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003934 status = mbedtls_to_psa_error(
3935 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3936 MBEDTLS_GCM_ENCRYPT,
3937 plaintext_length,
3938 nonce, nonce_length,
3939 additional_data, additional_data_length,
3940 plaintext, ciphertext,
3941 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003942 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003943 else
3944#endif /* MBEDTLS_GCM_C */
3945#if defined(MBEDTLS_CCM_C)
3946 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003947 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003948 status = mbedtls_to_psa_error(
3949 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3950 plaintext_length,
3951 nonce, nonce_length,
3952 additional_data,
3953 additional_data_length,
3954 plaintext, ciphertext,
3955 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003956 }
mohammad16035c8845f2018-05-09 05:40:09 -07003957 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003958#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003959#if defined(MBEDTLS_CHACHAPOLY_C)
3960 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3961 {
3962 if( nonce_length != 12 || operation.tag_length != 16 )
3963 {
3964 status = PSA_ERROR_NOT_SUPPORTED;
3965 goto exit;
3966 }
3967 status = mbedtls_to_psa_error(
3968 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3969 plaintext_length,
3970 nonce,
3971 additional_data,
3972 additional_data_length,
3973 plaintext,
3974 ciphertext,
3975 tag ) );
3976 }
3977 else
3978#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003979 {
mohammad1603554faad2018-06-03 15:07:38 +03003980 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003981 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003982
Gilles Peskineedf9a652018-08-17 18:11:56 +02003983 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3984 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003985
Gilles Peskineedf9a652018-08-17 18:11:56 +02003986exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003987 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003988 if( status == PSA_SUCCESS )
3989 *ciphertext_length = plaintext_length + operation.tag_length;
3990 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003991}
3992
Gilles Peskineee652a32018-06-01 19:23:52 +02003993/* Locate the tag in a ciphertext buffer containing the encrypted data
3994 * followed by the tag. Return the length of the part preceding the tag in
3995 * *plaintext_length. This is the size of the plaintext in modes where
3996 * the encrypted data has the same size as the plaintext, such as
3997 * CCM and GCM. */
3998static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3999 const uint8_t *ciphertext,
4000 size_t ciphertext_length,
4001 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004002 const uint8_t **p_tag )
4003{
4004 size_t payload_length;
4005 if( tag_length > ciphertext_length )
4006 return( PSA_ERROR_INVALID_ARGUMENT );
4007 payload_length = ciphertext_length - tag_length;
4008 if( payload_length > plaintext_size )
4009 return( PSA_ERROR_BUFFER_TOO_SMALL );
4010 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004011 return( PSA_SUCCESS );
4012}
4013
Gilles Peskinec5487a82018-12-03 18:08:14 +01004014psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004015 psa_algorithm_t alg,
4016 const uint8_t *nonce,
4017 size_t nonce_length,
4018 const uint8_t *additional_data,
4019 size_t additional_data_length,
4020 const uint8_t *ciphertext,
4021 size_t ciphertext_length,
4022 uint8_t *plaintext,
4023 size_t plaintext_size,
4024 size_t *plaintext_length )
4025{
mohammad16035955c982018-04-26 00:53:03 +03004026 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004027 aead_operation_t operation;
4028 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004029
Gilles Peskineee652a32018-06-01 19:23:52 +02004030 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004031
Gilles Peskinec5487a82018-12-03 18:08:14 +01004032 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004033 if( status != PSA_SUCCESS )
4034 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004035
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004036 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4037 ciphertext, ciphertext_length,
4038 plaintext_size, &tag );
4039 if( status != PSA_SUCCESS )
4040 goto exit;
4041
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004042#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004043 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004044 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004045 status = mbedtls_to_psa_error(
4046 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4047 ciphertext_length - operation.tag_length,
4048 nonce, nonce_length,
4049 additional_data,
4050 additional_data_length,
4051 tag, operation.tag_length,
4052 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004053 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004054 else
4055#endif /* MBEDTLS_GCM_C */
4056#if defined(MBEDTLS_CCM_C)
4057 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004058 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004059 status = mbedtls_to_psa_error(
4060 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4061 ciphertext_length - operation.tag_length,
4062 nonce, nonce_length,
4063 additional_data,
4064 additional_data_length,
4065 ciphertext, plaintext,
4066 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004067 }
mohammad160339574652018-06-01 04:39:53 -07004068 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004069#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004070#if defined(MBEDTLS_CHACHAPOLY_C)
4071 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4072 {
4073 if( nonce_length != 12 || operation.tag_length != 16 )
4074 {
4075 status = PSA_ERROR_NOT_SUPPORTED;
4076 goto exit;
4077 }
4078 status = mbedtls_to_psa_error(
4079 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4080 ciphertext_length - operation.tag_length,
4081 nonce,
4082 additional_data,
4083 additional_data_length,
4084 tag,
4085 ciphertext,
4086 plaintext ) );
4087 }
4088 else
4089#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004090 {
mohammad1603554faad2018-06-03 15:07:38 +03004091 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004092 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004093
Gilles Peskineedf9a652018-08-17 18:11:56 +02004094 if( status != PSA_SUCCESS && plaintext_size != 0 )
4095 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004096
Gilles Peskineedf9a652018-08-17 18:11:56 +02004097exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004098 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004099 if( status == PSA_SUCCESS )
4100 *plaintext_length = ciphertext_length - operation.tag_length;
4101 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004102}
4103
Gilles Peskinea0655c32018-04-30 17:06:50 +02004104
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004105
Gilles Peskine20035e32018-02-03 22:44:14 +01004106/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004107/* Generators */
4108/****************************************************************/
4109
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004110#define HKDF_STATE_INIT 0 /* no input yet */
4111#define HKDF_STATE_STARTED 1 /* got salt */
4112#define HKDF_STATE_KEYED 2 /* got key */
4113#define HKDF_STATE_OUTPUT 3 /* output started */
4114
Gilles Peskinecbe66502019-05-16 16:59:18 +02004115static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004116 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004117{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004118 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4119 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004120 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004121 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004122}
4123
4124
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004125psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004126{
4127 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004128 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004129 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004130 {
4131 /* The object has (apparently) been initialized but it is not
4132 * in use. It's ok to call abort on such an object, and there's
4133 * nothing to do. */
4134 }
4135 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004136#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004137 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004138 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004139 mbedtls_free( operation->ctx.hkdf.info );
4140 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004141 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004142 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004143 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004144 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004145 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004146#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004147 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004148 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004149 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4150 operation->ctx.tls12_prf.key_len );
4151 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004152 }
Hanno Becker580fba12018-11-13 20:50:45 +00004153
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004154 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004155 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004156 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4157 operation->ctx.tls12_prf.Ai_with_seed_len );
4158 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004159 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004160#else
4161 if( operation->ctx.tls12_prf.seed != NULL )
4162 {
4163 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4164 operation->ctx.tls12_prf.seed_length );
4165 mbedtls_free( operation->ctx.tls12_prf.seed );
4166 }
4167
4168 if( operation->ctx.tls12_prf.label != NULL )
4169 {
4170 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4171 operation->ctx.tls12_prf.label_length );
4172 mbedtls_free( operation->ctx.tls12_prf.label );
4173 }
4174
4175 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4176
4177 /* We leave the fields Ai and output_block to be erased safely by the
4178 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004179#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004180 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004181 else
4182#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004183 {
4184 status = PSA_ERROR_BAD_STATE;
4185 }
Janos Follath083036a2019-06-11 10:22:26 +01004186 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004187 return( status );
4188}
4189
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004190psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004191 size_t *capacity)
4192{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004193 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004194 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004195 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004196 return PSA_ERROR_BAD_STATE;
4197 }
4198
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004199 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004200 return( PSA_SUCCESS );
4201}
4202
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004203psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004204 size_t capacity )
4205{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004206 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004207 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004208 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004209 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004210 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004211 return( PSA_SUCCESS );
4212}
4213
Gilles Peskinebef7f142018-07-12 17:22:21 +02004214#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004215/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004216 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004217static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004218 psa_algorithm_t hash_alg,
4219 uint8_t *output,
4220 size_t output_length )
4221{
4222 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4223 psa_status_t status;
4224
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004225 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4226 return( PSA_ERROR_BAD_STATE );
4227 hkdf->state = HKDF_STATE_OUTPUT;
4228
Gilles Peskinebef7f142018-07-12 17:22:21 +02004229 while( output_length != 0 )
4230 {
4231 /* Copy what remains of the current block */
4232 uint8_t n = hash_length - hkdf->offset_in_block;
4233 if( n > output_length )
4234 n = (uint8_t) output_length;
4235 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4236 output += n;
4237 output_length -= n;
4238 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004239 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004240 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004241 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004242 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004243 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004244 * object was corrupted or if this function is called directly
4245 * inside the library. */
4246 if( hkdf->block_number == 0xff )
4247 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004248
4249 /* We need a new block */
4250 ++hkdf->block_number;
4251 hkdf->offset_in_block = 0;
4252 status = psa_hmac_setup_internal( &hkdf->hmac,
4253 hkdf->prk, hash_length,
4254 hash_alg );
4255 if( status != PSA_SUCCESS )
4256 return( status );
4257 if( hkdf->block_number != 1 )
4258 {
4259 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4260 hkdf->output_block,
4261 hash_length );
4262 if( status != PSA_SUCCESS )
4263 return( status );
4264 }
4265 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4266 hkdf->info,
4267 hkdf->info_length );
4268 if( status != PSA_SUCCESS )
4269 return( status );
4270 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4271 &hkdf->block_number, 1 );
4272 if( status != PSA_SUCCESS )
4273 return( status );
4274 status = psa_hmac_finish_internal( &hkdf->hmac,
4275 hkdf->output_block,
4276 sizeof( hkdf->output_block ) );
4277 if( status != PSA_SUCCESS )
4278 return( status );
4279 }
4280
4281 return( PSA_SUCCESS );
4282}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004283
Janos Follath999f6482019-06-11 12:04:10 +01004284#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004285static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4286 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004287 psa_algorithm_t alg )
4288{
4289 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4290 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4291 psa_hmac_internal_data hmac;
4292 psa_status_t status, cleanup_status;
4293
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004294 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004295 size_t Ai_len;
4296
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004297 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004298 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004299 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004300 * object was corrupted or if this function is called directly
4301 * inside the library. */
4302 if( tls12_prf->block_number == 0xff )
4303 return( PSA_ERROR_BAD_STATE );
4304
4305 /* We need a new block */
4306 ++tls12_prf->block_number;
4307 tls12_prf->offset_in_block = 0;
4308
4309 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4310 *
4311 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4312 *
4313 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4314 * HMAC_hash(secret, A(2) + seed) +
4315 * HMAC_hash(secret, A(3) + seed) + ...
4316 *
4317 * A(0) = seed
4318 * A(i) = HMAC_hash( secret, A(i-1) )
4319 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004320 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004321 * `HMAC_hash(secret, A(i) + seed)` from which the output
4322 * is currently extracted as `output_block`, while
4323 * `A(i) + seed` is stored in `Ai_with_seed`.
4324 *
4325 * Generating a new block means recalculating `Ai_with_seed`
4326 * from the A(i)-part of it, and afterwards recalculating
4327 * `output_block`.
4328 *
4329 * A(0) is computed at setup time.
4330 *
4331 */
4332
4333 psa_hmac_init_internal( &hmac );
4334
4335 /* We must distinguish the calculation of A(1) from those
4336 * of A(2) and higher, because A(0)=seed has a different
4337 * length than the other A(i). */
4338 if( tls12_prf->block_number == 1 )
4339 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004340 Ai = tls12_prf->Ai_with_seed + hash_length;
4341 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004342 }
4343 else
4344 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004345 Ai = tls12_prf->Ai_with_seed;
4346 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004347 }
4348
Hanno Becker3b339e22018-11-13 20:56:14 +00004349 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4350 status = psa_hmac_setup_internal( &hmac,
4351 tls12_prf->key,
4352 tls12_prf->key_len,
4353 hash_alg );
4354 if( status != PSA_SUCCESS )
4355 goto cleanup;
4356
4357 status = psa_hash_update( &hmac.hash_ctx,
4358 Ai, Ai_len );
4359 if( status != PSA_SUCCESS )
4360 goto cleanup;
4361
4362 status = psa_hmac_finish_internal( &hmac,
4363 tls12_prf->Ai_with_seed,
4364 hash_length );
4365 if( status != PSA_SUCCESS )
4366 goto cleanup;
4367
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004368 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4369 status = psa_hmac_setup_internal( &hmac,
4370 tls12_prf->key,
4371 tls12_prf->key_len,
4372 hash_alg );
4373 if( status != PSA_SUCCESS )
4374 goto cleanup;
4375
4376 status = psa_hash_update( &hmac.hash_ctx,
4377 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004378 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004379 if( status != PSA_SUCCESS )
4380 goto cleanup;
4381
4382 status = psa_hmac_finish_internal( &hmac,
4383 tls12_prf->output_block,
4384 hash_length );
4385 if( status != PSA_SUCCESS )
4386 goto cleanup;
4387
4388cleanup:
4389
4390 cleanup_status = psa_hmac_abort_internal( &hmac );
4391 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4392 status = cleanup_status;
4393
4394 return( status );
4395}
Janos Follath7742fee2019-06-17 12:58:10 +01004396#else
4397static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4398 psa_tls12_prf_key_derivation_t *tls12_prf,
4399 psa_algorithm_t alg )
4400{
4401 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4402 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004403 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4404 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004405
Janos Follath7742fee2019-06-17 12:58:10 +01004406 /* We can't be wanting more output after block 0xff, otherwise
4407 * the capacity check in psa_key_derivation_output_bytes() would have
4408 * prevented this call. It could happen only if the operation
4409 * object was corrupted or if this function is called directly
4410 * inside the library. */
4411 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004412 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004413
4414 /* We need a new block */
4415 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004416 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004417
4418 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4419 *
4420 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4421 *
4422 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4423 * HMAC_hash(secret, A(2) + seed) +
4424 * HMAC_hash(secret, A(3) + seed) + ...
4425 *
4426 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004427 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004428 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004429 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004430 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004431 * is currently extracted as `output_block` and where i is
4432 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004433 */
4434
Janos Follathea29bfb2019-06-19 12:21:20 +01004435 /* Save the hash context before using it, to preserve the hash state with
4436 * only the inner padding in it. We need this, because inner padding depends
4437 * on the key (secret in the RFC's terminology). */
4438 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4439 if( status != PSA_SUCCESS )
4440 goto cleanup;
4441
4442 /* Calculate A(i) where i = tls12_prf->block_number. */
4443 if( tls12_prf->block_number == 1 )
4444 {
4445 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4446 * the variable seed and in this instance means it in the context of the
4447 * P_hash function, where seed = label + seed.) */
4448 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4449 tls12_prf->label, tls12_prf->label_length );
4450 if( status != PSA_SUCCESS )
4451 goto cleanup;
4452 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4453 tls12_prf->seed, tls12_prf->seed_length );
4454 if( status != PSA_SUCCESS )
4455 goto cleanup;
4456 }
4457 else
4458 {
4459 /* A(i) = HMAC_hash(secret, A(i-1)) */
4460 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4461 tls12_prf->Ai, hash_length );
4462 if( status != PSA_SUCCESS )
4463 goto cleanup;
4464 }
4465
4466 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4467 tls12_prf->Ai, 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
4474 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4475 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4476 tls12_prf->Ai, hash_length );
4477 if( status != PSA_SUCCESS )
4478 goto cleanup;
4479 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4480 tls12_prf->label, tls12_prf->label_length );
4481 if( status != PSA_SUCCESS )
4482 goto cleanup;
4483 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4484 tls12_prf->seed, tls12_prf->seed_length );
4485 if( status != PSA_SUCCESS )
4486 goto cleanup;
4487 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4488 tls12_prf->output_block, hash_length );
4489 if( status != PSA_SUCCESS )
4490 goto cleanup;
4491 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4492 if( status != PSA_SUCCESS )
4493 goto cleanup;
4494
Janos Follath7742fee2019-06-17 12:58:10 +01004495
4496cleanup:
4497
Janos Follathea29bfb2019-06-19 12:21:20 +01004498 cleanup_status = psa_hash_abort( &backup );
4499 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4500 status = cleanup_status;
4501
4502 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004503}
Janos Follath999f6482019-06-11 12:04:10 +01004504#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004505
Janos Follath999f6482019-06-11 12:04:10 +01004506#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004507/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004508 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004509static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004510 psa_tls12_prf_key_derivation_t *tls12_prf,
4511 psa_algorithm_t alg,
4512 uint8_t *output,
4513 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004514{
4515 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4516 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4517 psa_status_t status;
4518
4519 while( output_length != 0 )
4520 {
4521 /* Copy what remains of the current block */
4522 uint8_t n = hash_length - tls12_prf->offset_in_block;
4523
4524 /* Check if we have fully processed the current block. */
4525 if( n == 0 )
4526 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004527 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004528 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004529 if( status != PSA_SUCCESS )
4530 return( status );
4531
4532 continue;
4533 }
4534
4535 if( n > output_length )
4536 n = (uint8_t) output_length;
4537 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4538 n );
4539 output += n;
4540 output_length -= n;
4541 tls12_prf->offset_in_block += n;
4542 }
4543
4544 return( PSA_SUCCESS );
4545}
Janos Follath844eb0e2019-06-19 12:10:49 +01004546#else
4547static psa_status_t psa_key_derivation_tls12_prf_read(
4548 psa_tls12_prf_key_derivation_t *tls12_prf,
4549 psa_algorithm_t alg,
4550 uint8_t *output,
4551 size_t output_length )
4552{
4553 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4554 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4555 psa_status_t status;
4556 uint8_t offset, length;
4557
4558 while( output_length != 0 )
4559 {
4560 /* Check if we have fully processed the current block. */
4561 if( tls12_prf->left_in_block == 0 )
4562 {
4563 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4564 alg );
4565 if( status != PSA_SUCCESS )
4566 return( status );
4567
4568 continue;
4569 }
4570
4571 if( tls12_prf->left_in_block > output_length )
4572 length = (uint8_t) output_length;
4573 else
4574 length = tls12_prf->left_in_block;
4575
4576 offset = hash_length - tls12_prf->left_in_block;
4577 memcpy( output, tls12_prf->output_block + offset, length );
4578 output += length;
4579 output_length -= length;
4580 tls12_prf->left_in_block -= length;
4581 }
4582
4583 return( PSA_SUCCESS );
4584}
Janos Follath999f6482019-06-11 12:04:10 +01004585#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004586#endif /* MBEDTLS_MD_C */
4587
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004588psa_status_t psa_key_derivation_output_bytes(
4589 psa_key_derivation_operation_t *operation,
4590 uint8_t *output,
4591 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004592{
4593 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004594 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004595
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004596 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004597 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004598 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004599 return PSA_ERROR_BAD_STATE;
4600 }
4601
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004602 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004603 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004604 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004605 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004606 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004607 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004608 goto exit;
4609 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004610 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004611 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004612 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004613 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004614 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4615 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004616 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004617 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004618 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004619 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004620 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004621
Gilles Peskinebef7f142018-07-12 17:22:21 +02004622#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004623 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004624 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004625 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004626 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004627 output, output_length );
4628 }
Janos Follathadbec812019-06-14 11:05:39 +01004629 else
Janos Follathadbec812019-06-14 11:05:39 +01004630 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004631 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004632 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004633 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004634 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004635 output_length );
4636 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004637 else
4638#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004639 {
4640 return( PSA_ERROR_BAD_STATE );
4641 }
4642
4643exit:
4644 if( status != PSA_SUCCESS )
4645 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004646 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004647 * This allows us to differentiate between exhausted operations and
4648 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4649 * operations. */
4650 psa_algorithm_t alg = operation->alg;
4651 psa_key_derivation_abort( operation );
4652 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004653 memset( output, '!', output_length );
4654 }
4655 return( status );
4656}
4657
Gilles Peskine08542d82018-07-19 17:05:42 +02004658#if defined(MBEDTLS_DES_C)
4659static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4660{
4661 if( data_size >= 8 )
4662 mbedtls_des_key_set_parity( data );
4663 if( data_size >= 16 )
4664 mbedtls_des_key_set_parity( data + 8 );
4665 if( data_size >= 24 )
4666 mbedtls_des_key_set_parity( data + 16 );
4667}
4668#endif /* MBEDTLS_DES_C */
4669
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004670static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004671 psa_key_slot_t *slot,
4672 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004673 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004674{
4675 uint8_t *data = NULL;
4676 size_t bytes = PSA_BITS_TO_BYTES( bits );
4677 psa_status_t status;
4678
4679 if( ! key_type_is_raw_bytes( slot->type ) )
4680 return( PSA_ERROR_INVALID_ARGUMENT );
4681 if( bits % 8 != 0 )
4682 return( PSA_ERROR_INVALID_ARGUMENT );
4683 data = mbedtls_calloc( 1, bytes );
4684 if( data == NULL )
4685 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4686
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004687 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004688 if( status != PSA_SUCCESS )
4689 goto exit;
4690#if defined(MBEDTLS_DES_C)
4691 if( slot->type == PSA_KEY_TYPE_DES )
4692 psa_des_set_key_parity( data, bytes );
4693#endif /* MBEDTLS_DES_C */
4694 status = psa_import_key_into_slot( slot, data, bytes );
4695
4696exit:
4697 mbedtls_free( data );
4698 return( status );
4699}
4700
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004701psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004702 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004703 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004704{
4705 psa_status_t status;
4706 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004707 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004708 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004709#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4710 if( driver != NULL )
4711 {
4712 /* Deriving a key in a secure element is not implemented yet. */
4713 status = PSA_ERROR_NOT_SUPPORTED;
4714 }
4715#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004716 if( status == PSA_SUCCESS )
4717 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004718 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004719 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004720 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004721 }
4722 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004723 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004724 if( status != PSA_SUCCESS )
4725 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004726 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004727 *handle = 0;
4728 }
4729 return( status );
4730}
4731
Gilles Peskineeab56e42018-07-12 17:12:33 +02004732
4733
4734/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004735/* Key derivation */
4736/****************************************************************/
4737
Gilles Peskinea05219c2018-11-16 16:02:56 +01004738#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004739#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004740/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004741 * of the HKDF algorithm.
4742 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004743 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004744 * to potentially free embedded data structures and wipe confidential data.
4745 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004746static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004747 const uint8_t *secret,
4748 size_t secret_length,
4749 psa_algorithm_t hash_alg,
4750 const uint8_t *salt,
4751 size_t salt_length,
4752 const uint8_t *label,
4753 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004754{
4755 psa_status_t status;
4756 status = psa_hmac_setup_internal( &hkdf->hmac,
4757 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004758 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004759 if( status != PSA_SUCCESS )
4760 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004761 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004762 if( status != PSA_SUCCESS )
4763 return( status );
4764 status = psa_hmac_finish_internal( &hkdf->hmac,
4765 hkdf->prk,
4766 sizeof( hkdf->prk ) );
4767 if( status != PSA_SUCCESS )
4768 return( status );
4769 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4770 hkdf->block_number = 0;
4771 hkdf->info_length = label_length;
4772 if( label_length != 0 )
4773 {
4774 hkdf->info = mbedtls_calloc( 1, label_length );
4775 if( hkdf->info == NULL )
4776 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4777 memcpy( hkdf->info, label, label_length );
4778 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004779 hkdf->state = HKDF_STATE_KEYED;
4780 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004781 return( PSA_SUCCESS );
4782}
Janos Follath71a4c912019-06-11 09:14:47 +01004783#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004784#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004785
Gilles Peskinea05219c2018-11-16 16:02:56 +01004786#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004787#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004788/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004789 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004790 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004791 * to potentially free embedded data structures and wipe confidential data.
4792 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004793static psa_status_t psa_key_derivation_tls12_prf_setup(
4794 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004795 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004796 size_t key_len,
4797 psa_algorithm_t hash_alg,
4798 const uint8_t *salt,
4799 size_t salt_length,
4800 const uint8_t *label,
4801 size_t label_length )
4802{
4803 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004804 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4805 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004806
4807 tls12_prf->key = mbedtls_calloc( 1, key_len );
4808 if( tls12_prf->key == NULL )
4809 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4810 tls12_prf->key_len = key_len;
4811 memcpy( tls12_prf->key, key, key_len );
4812
Hanno Becker580fba12018-11-13 20:50:45 +00004813 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004814 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004815 if( overflow )
4816 return( PSA_ERROR_INVALID_ARGUMENT );
4817
4818 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4819 if( tls12_prf->Ai_with_seed == NULL )
4820 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4821 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4822
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004823 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4824 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004825 if( label_length != 0 )
4826 {
4827 memcpy( tls12_prf->Ai_with_seed + hash_length,
4828 label, label_length );
4829 }
4830
4831 if( salt_length != 0 )
4832 {
4833 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4834 salt, salt_length );
4835 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004836
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004837 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004838 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004839 tls12_prf->block_number = 0;
4840 tls12_prf->offset_in_block = hash_length;
4841
4842 return( PSA_SUCCESS );
4843}
Janos Follath71a4c912019-06-11 09:14:47 +01004844#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004845
Janos Follath71a4c912019-06-11 09:14:47 +01004846#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004847/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004848static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4849 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004850 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004851 size_t psk_len,
4852 psa_algorithm_t hash_alg,
4853 const uint8_t *salt,
4854 size_t salt_length,
4855 const uint8_t *label,
4856 size_t label_length )
4857{
4858 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004859 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00004860
4861 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4862 return( PSA_ERROR_INVALID_ARGUMENT );
4863
4864 /* Quoting RFC 4279, Section 2:
4865 *
4866 * The premaster secret is formed as follows: if the PSK is N octets
4867 * long, concatenate a uint16 with the value N, N zero octets, a second
4868 * uint16 with the value N, and the PSK itself.
4869 */
4870
4871 pms[0] = ( psk_len >> 8 ) & 0xff;
4872 pms[1] = ( psk_len >> 0 ) & 0xff;
4873 memset( pms + 2, 0, psk_len );
4874 pms[2 + psk_len + 0] = pms[0];
4875 pms[2 + psk_len + 1] = pms[1];
4876 memcpy( pms + 4 + psk_len, psk, psk_len );
4877
Gilles Peskinecbe66502019-05-16 16:59:18 +02004878 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004879 pms, 4 + 2 * psk_len,
4880 hash_alg,
4881 salt, salt_length,
4882 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004883
Gilles Peskine3f108122018-12-07 18:14:53 +01004884 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004885 return( status );
4886}
Janos Follath71a4c912019-06-11 09:14:47 +01004887#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004888#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004889
Janos Follath71a4c912019-06-11 09:14:47 +01004890#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004891/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004892 * to potentially free embedded data structures and wipe confidential data.
4893 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004894static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004895 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004896 const uint8_t *secret, size_t secret_length,
4897 psa_algorithm_t alg,
4898 const uint8_t *salt, size_t salt_length,
4899 const uint8_t *label, size_t label_length,
4900 size_t capacity )
4901{
4902 psa_status_t status;
4903 size_t max_capacity;
4904
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004905 /* Set operation->alg even on failure so that abort knows what to do. */
4906 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004907
4908#if defined(MBEDTLS_MD_C)
4909 if( PSA_ALG_IS_HKDF( alg ) )
4910 {
4911 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4912 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4913 if( hash_size == 0 )
4914 return( PSA_ERROR_NOT_SUPPORTED );
4915 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004916 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004917 secret, secret_length,
4918 hash_alg,
4919 salt, salt_length,
4920 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004921 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004922 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4923 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4924 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004925 {
4926 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4927 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4928
4929 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4930 if( hash_alg != PSA_ALG_SHA_256 &&
4931 hash_alg != PSA_ALG_SHA_384 )
4932 {
4933 return( PSA_ERROR_NOT_SUPPORTED );
4934 }
4935
4936 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004937
4938 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4939 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004940 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004941 secret, secret_length,
4942 hash_alg, salt, salt_length,
4943 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004944 }
4945 else
4946 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004947 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004948 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004949 secret, secret_length,
4950 hash_alg, salt, salt_length,
4951 label, label_length );
4952 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004953 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004954 else
4955#endif
4956 {
4957 return( PSA_ERROR_NOT_SUPPORTED );
4958 }
4959
4960 if( status != PSA_SUCCESS )
4961 return( status );
4962
4963 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004964 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004965 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004967 else
4968 return( PSA_ERROR_INVALID_ARGUMENT );
4969
4970 return( PSA_SUCCESS );
4971}
Janos Follath71a4c912019-06-11 09:14:47 +01004972#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004973
Janos Follath71a4c912019-06-11 09:14:47 +01004974#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004975psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004976 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004977 psa_algorithm_t alg,
4978 const uint8_t *salt,
4979 size_t salt_length,
4980 const uint8_t *label,
4981 size_t label_length,
4982 size_t capacity )
4983{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004984 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004985 psa_status_t status;
4986
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004987 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004988 return( PSA_ERROR_BAD_STATE );
4989
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004990 /* Make sure that alg is a key derivation algorithm. This prevents
4991 * key selection algorithms, which psa_key_derivation_internal
4992 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004993 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4994 return( PSA_ERROR_INVALID_ARGUMENT );
4995
Gilles Peskine28f8f302019-07-24 13:30:31 +02004996 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004997 if( status != PSA_SUCCESS )
4998 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004999
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005000 if( slot->type != PSA_KEY_TYPE_DERIVE )
5001 return( PSA_ERROR_INVALID_ARGUMENT );
5002
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005003 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005004 slot->data.raw.data,
5005 slot->data.raw.bytes,
5006 alg,
5007 salt, salt_length,
5008 label, label_length,
5009 capacity );
5010 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005011 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005012 return( status );
5013}
Janos Follath71a4c912019-06-11 09:14:47 +01005014#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005015
Gilles Peskine969c5d62019-01-16 15:53:06 +01005016static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005017 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005018 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005019{
Janos Follath5fe19732019-06-20 15:09:30 +01005020 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5021 * initialisers for this union leave some bytes unspecified.) */
5022 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5023
Gilles Peskine969c5d62019-01-16 15:53:06 +01005024 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005025#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005026 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5027 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5028 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005029 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005030 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005031 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5032 if( hash_size == 0 )
5033 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005034 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5035 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005036 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005037 {
5038 return( PSA_ERROR_NOT_SUPPORTED );
5039 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005040 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005041 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005042 }
5043#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005044 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005045 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005046}
5047
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005048psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005049 psa_algorithm_t alg )
5050{
5051 psa_status_t status;
5052
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005053 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005054 return( PSA_ERROR_BAD_STATE );
5055
Gilles Peskine6843c292019-01-18 16:44:49 +01005056 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5057 return( PSA_ERROR_INVALID_ARGUMENT );
5058 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005059 {
5060 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005061 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005062 }
5063 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5064 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005065 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005066 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005067 else
5068 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005069
5070 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005071 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005072 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005073}
5074
5075#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005076static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005077 psa_algorithm_t hash_alg,
5078 psa_key_derivation_step_t step,
5079 const uint8_t *data,
5080 size_t data_length )
5081{
5082 psa_status_t status;
5083 switch( step )
5084 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005085 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005086 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005087 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005088 status = psa_hmac_setup_internal( &hkdf->hmac,
5089 data, data_length,
5090 hash_alg );
5091 if( status != PSA_SUCCESS )
5092 return( status );
5093 hkdf->state = HKDF_STATE_STARTED;
5094 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005095 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005096 /* If no salt was provided, use an empty salt. */
5097 if( hkdf->state == HKDF_STATE_INIT )
5098 {
5099 status = psa_hmac_setup_internal( &hkdf->hmac,
5100 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005101 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005102 if( status != PSA_SUCCESS )
5103 return( status );
5104 hkdf->state = HKDF_STATE_STARTED;
5105 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005106 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005107 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005108 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5109 data, data_length );
5110 if( status != PSA_SUCCESS )
5111 return( status );
5112 status = psa_hmac_finish_internal( &hkdf->hmac,
5113 hkdf->prk,
5114 sizeof( hkdf->prk ) );
5115 if( status != PSA_SUCCESS )
5116 return( status );
5117 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5118 hkdf->block_number = 0;
5119 hkdf->state = HKDF_STATE_KEYED;
5120 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005121 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005122 if( hkdf->state == HKDF_STATE_OUTPUT )
5123 return( PSA_ERROR_BAD_STATE );
5124 if( hkdf->info_set )
5125 return( PSA_ERROR_BAD_STATE );
5126 hkdf->info_length = data_length;
5127 if( data_length != 0 )
5128 {
5129 hkdf->info = mbedtls_calloc( 1, data_length );
5130 if( hkdf->info == NULL )
5131 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5132 memcpy( hkdf->info, data, data_length );
5133 }
5134 hkdf->info_set = 1;
5135 return( PSA_SUCCESS );
5136 default:
5137 return( PSA_ERROR_INVALID_ARGUMENT );
5138 }
5139}
Janos Follathb03233e2019-06-11 15:30:30 +01005140
5141#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5142static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5143 psa_algorithm_t hash_alg,
5144 psa_key_derivation_step_t step,
5145 const uint8_t *data,
5146 size_t data_length )
5147{
5148 (void) prf;
5149 (void) hash_alg;
5150 (void) step;
5151 (void) data;
5152 (void) data_length;
5153
5154 return( PSA_ERROR_INVALID_ARGUMENT );
5155}
Janos Follath6660f0e2019-06-17 08:44:03 +01005156
5157static psa_status_t psa_tls12_prf_psk_to_ms_input(
5158 psa_tls12_prf_key_derivation_t *prf,
5159 psa_algorithm_t hash_alg,
5160 psa_key_derivation_step_t step,
5161 const uint8_t *data,
5162 size_t data_length )
5163{
5164 (void) prf;
5165 (void) hash_alg;
5166 (void) step;
5167 (void) data;
5168 (void) data_length;
5169
5170 return( PSA_ERROR_INVALID_ARGUMENT );
5171}
Janos Follathb03233e2019-06-11 15:30:30 +01005172#else
Janos Follathf08e2652019-06-13 09:05:41 +01005173static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5174 const uint8_t *data,
5175 size_t data_length )
5176{
5177 if( prf->state != TLS12_PRF_STATE_INIT )
5178 return( PSA_ERROR_BAD_STATE );
5179
Janos Follathd6dce9f2019-07-04 09:11:38 +01005180 if( data_length != 0 )
5181 {
5182 prf->seed = mbedtls_calloc( 1, data_length );
5183 if( prf->seed == NULL )
5184 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005185
Janos Follathd6dce9f2019-07-04 09:11:38 +01005186 memcpy( prf->seed, data, data_length );
5187 prf->seed_length = data_length;
5188 }
Janos Follathf08e2652019-06-13 09:05:41 +01005189
5190 prf->state = TLS12_PRF_STATE_SEED_SET;
5191
5192 return( PSA_SUCCESS );
5193}
5194
Janos Follath81550542019-06-13 14:26:34 +01005195static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5196 psa_algorithm_t hash_alg,
5197 const uint8_t *data,
5198 size_t data_length )
5199{
5200 psa_status_t status;
5201 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5202 return( PSA_ERROR_BAD_STATE );
5203
5204 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5205 if( status != PSA_SUCCESS )
5206 return( status );
5207
5208 prf->state = TLS12_PRF_STATE_KEY_SET;
5209
5210 return( PSA_SUCCESS );
5211}
5212
Janos Follath6660f0e2019-06-17 08:44:03 +01005213static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5214 psa_tls12_prf_key_derivation_t *prf,
5215 psa_algorithm_t hash_alg,
5216 const uint8_t *data,
5217 size_t data_length )
5218{
5219 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005220 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5221 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005222
5223 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5224 return( PSA_ERROR_INVALID_ARGUMENT );
5225
5226 /* Quoting RFC 4279, Section 2:
5227 *
5228 * The premaster secret is formed as follows: if the PSK is N octets
5229 * long, concatenate a uint16 with the value N, N zero octets, a second
5230 * uint16 with the value N, and the PSK itself.
5231 */
5232
Janos Follath40e13932019-06-26 13:22:29 +01005233 *cur++ = ( data_length >> 8 ) & 0xff;
5234 *cur++ = ( data_length >> 0 ) & 0xff;
5235 memset( cur, 0, data_length );
5236 cur += data_length;
5237 *cur++ = pms[0];
5238 *cur++ = pms[1];
5239 memcpy( cur, data, data_length );
5240 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005241
Janos Follath40e13932019-06-26 13:22:29 +01005242 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005243
5244 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5245 return( status );
5246}
5247
Janos Follath63028dd2019-06-13 09:15:47 +01005248static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5249 const uint8_t *data,
5250 size_t data_length )
5251{
5252 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5253 return( PSA_ERROR_BAD_STATE );
5254
Janos Follathd6dce9f2019-07-04 09:11:38 +01005255 if( data_length != 0 )
5256 {
5257 prf->label = mbedtls_calloc( 1, data_length );
5258 if( prf->label == NULL )
5259 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005260
Janos Follathd6dce9f2019-07-04 09:11:38 +01005261 memcpy( prf->label, data, data_length );
5262 prf->label_length = data_length;
5263 }
Janos Follath63028dd2019-06-13 09:15:47 +01005264
5265 prf->state = TLS12_PRF_STATE_LABEL_SET;
5266
5267 return( PSA_SUCCESS );
5268}
5269
Janos Follathb03233e2019-06-11 15:30:30 +01005270static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5271 psa_algorithm_t hash_alg,
5272 psa_key_derivation_step_t step,
5273 const uint8_t *data,
5274 size_t data_length )
5275{
Janos Follathb03233e2019-06-11 15:30:30 +01005276 switch( step )
5277 {
Janos Follathf08e2652019-06-13 09:05:41 +01005278 case PSA_KEY_DERIVATION_INPUT_SEED:
5279 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005280 case PSA_KEY_DERIVATION_INPUT_SECRET:
5281 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005282 case PSA_KEY_DERIVATION_INPUT_LABEL:
5283 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005284 default:
5285 return( PSA_ERROR_INVALID_ARGUMENT );
5286 }
5287}
Janos Follath6660f0e2019-06-17 08:44:03 +01005288
5289static psa_status_t psa_tls12_prf_psk_to_ms_input(
5290 psa_tls12_prf_key_derivation_t *prf,
5291 psa_algorithm_t hash_alg,
5292 psa_key_derivation_step_t step,
5293 const uint8_t *data,
5294 size_t data_length )
5295{
5296 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005297 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005298 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5299 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005300 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005301
5302 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5303}
Janos Follathb03233e2019-06-11 15:30:30 +01005304#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005305#endif /* MBEDTLS_MD_C */
5306
Janos Follathb80a94e2019-06-12 15:54:46 +01005307static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005308 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005309 psa_key_derivation_step_t step,
5310 const uint8_t *data,
5311 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005312{
5313 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005314 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005315
5316#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005317 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005318 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005319 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005320 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005321 step, data, data_length );
5322 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005323 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005324#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005325#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005326 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005327 {
Janos Follathb03233e2019-06-11 15:30:30 +01005328 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5329 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5330 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005331 }
5332 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5333 {
5334 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5335 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5336 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005337 }
5338 else
5339#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005340 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005341 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005342 return( PSA_ERROR_BAD_STATE );
5343 }
5344
5345 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005346 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005347 return( status );
5348}
5349
Janos Follath51f4a0f2019-06-14 11:35:55 +01005350psa_status_t psa_key_derivation_input_bytes(
5351 psa_key_derivation_operation_t *operation,
5352 psa_key_derivation_step_t step,
5353 const uint8_t *data,
5354 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005355{
Janos Follathc5621512019-06-14 11:27:57 +01005356 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5357 return( PSA_ERROR_INVALID_ARGUMENT );
5358
5359 return( psa_key_derivation_input_internal( operation, step,
5360 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005361}
5362
Janos Follath51f4a0f2019-06-14 11:35:55 +01005363psa_status_t psa_key_derivation_input_key(
5364 psa_key_derivation_operation_t *operation,
5365 psa_key_derivation_step_t step,
5366 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005367{
5368 psa_key_slot_t *slot;
5369 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005370 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005371 PSA_KEY_USAGE_DERIVE,
5372 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005373 if( status != PSA_SUCCESS )
5374 return( status );
5375 if( slot->type != PSA_KEY_TYPE_DERIVE )
5376 return( PSA_ERROR_INVALID_ARGUMENT );
5377 /* Don't allow a key to be used as an input that is usually public.
5378 * This is debatable. It's ok from a cryptographic perspective to
5379 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005380 * the material should be dedicated to a particular input step,
5381 * otherwise this may allow the key to be used in an unintended way
5382 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005383 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005384 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005385 return( psa_key_derivation_input_internal( operation,
5386 step,
5387 slot->data.raw.data,
5388 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005389}
5390
Gilles Peskineea0fb492018-07-12 17:17:20 +02005391
5392
5393/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005394/* Key agreement */
5395/****************************************************************/
5396
Gilles Peskinea05219c2018-11-16 16:02:56 +01005397#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005398static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5399 size_t peer_key_length,
5400 const mbedtls_ecp_keypair *our_key,
5401 uint8_t *shared_secret,
5402 size_t shared_secret_size,
5403 size_t *shared_secret_length )
5404{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005405 mbedtls_ecp_keypair *their_key = NULL;
5406 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005407 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005408 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005409
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005410 status = psa_import_ec_public_key(
5411 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5412 peer_key, peer_key_length,
5413 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005414 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005415 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005416
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005417 status = mbedtls_to_psa_error(
5418 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5419 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005420 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005421 status = mbedtls_to_psa_error(
5422 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5423 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005424 goto exit;
5425
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005426 status = mbedtls_to_psa_error(
5427 mbedtls_ecdh_calc_secret( &ecdh,
5428 shared_secret_length,
5429 shared_secret, shared_secret_size,
5430 mbedtls_ctr_drbg_random,
5431 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005432
5433exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005434 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005435 mbedtls_ecp_keypair_free( their_key );
5436 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005437 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005438}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005439#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005440
Gilles Peskine01d718c2018-09-18 12:01:02 +02005441#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5442
Gilles Peskine0216fe12019-04-11 21:23:21 +02005443static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5444 psa_key_slot_t *private_key,
5445 const uint8_t *peer_key,
5446 size_t peer_key_length,
5447 uint8_t *shared_secret,
5448 size_t shared_secret_size,
5449 size_t *shared_secret_length )
5450{
5451 switch( alg )
5452 {
5453#if defined(MBEDTLS_ECDH_C)
5454 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005455 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005456 return( PSA_ERROR_INVALID_ARGUMENT );
5457 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5458 private_key->data.ecp,
5459 shared_secret, shared_secret_size,
5460 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005461#endif /* MBEDTLS_ECDH_C */
5462 default:
5463 (void) private_key;
5464 (void) peer_key;
5465 (void) peer_key_length;
5466 (void) shared_secret;
5467 (void) shared_secret_size;
5468 (void) shared_secret_length;
5469 return( PSA_ERROR_NOT_SUPPORTED );
5470 }
5471}
5472
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005473/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005474 * to potentially free embedded data structures and wipe confidential data.
5475 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005476static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005477 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005478 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005479 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005480 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005481{
5482 psa_status_t status;
5483 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5484 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005485 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005486
5487 /* Step 1: run the secret agreement algorithm to generate the shared
5488 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005489 status = psa_key_agreement_raw_internal( ka_alg,
5490 private_key,
5491 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005492 shared_secret,
5493 sizeof( shared_secret ),
5494 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005495 if( status != PSA_SUCCESS )
5496 goto exit;
5497
5498 /* Step 2: set up the key derivation to generate key material from
5499 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005500 status = psa_key_derivation_input_internal( operation, step,
5501 shared_secret,
5502 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005503
Gilles Peskine01d718c2018-09-18 12:01:02 +02005504exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005505 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005506 return( status );
5507}
5508
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005509psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005510 psa_key_derivation_step_t step,
5511 psa_key_handle_t private_key,
5512 const uint8_t *peer_key,
5513 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005514{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005515 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005516 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005517 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005518 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005519 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005520 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005521 if( status != PSA_SUCCESS )
5522 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005524 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005525 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005526 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005527 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005528 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005529}
5530
Gilles Peskinebe697d82019-05-16 18:00:41 +02005531psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5532 psa_key_handle_t private_key,
5533 const uint8_t *peer_key,
5534 size_t peer_key_length,
5535 uint8_t *output,
5536 size_t output_size,
5537 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005538{
5539 psa_key_slot_t *slot;
5540 psa_status_t status;
5541
5542 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5543 {
5544 status = PSA_ERROR_INVALID_ARGUMENT;
5545 goto exit;
5546 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005547 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005548 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005549 if( status != PSA_SUCCESS )
5550 goto exit;
5551
5552 status = psa_key_agreement_raw_internal( alg, slot,
5553 peer_key, peer_key_length,
5554 output, output_size,
5555 output_length );
5556
5557exit:
5558 if( status != PSA_SUCCESS )
5559 {
5560 /* If an error happens and is not handled properly, the output
5561 * may be used as a key to protect sensitive data. Arrange for such
5562 * a key to be random, which is likely to result in decryption or
5563 * verification errors. This is better than filling the buffer with
5564 * some constant data such as zeros, which would result in the data
5565 * being protected with a reproducible, easily knowable key.
5566 */
5567 psa_generate_random( output, output_size );
5568 *output_length = output_size;
5569 }
5570 return( status );
5571}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005572
5573
5574/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005575/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005576/****************************************************************/
5577
5578psa_status_t psa_generate_random( uint8_t *output,
5579 size_t output_size )
5580{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005581 int ret;
5582 GUARD_MODULE_INITIALIZED;
5583
5584 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005585 return( mbedtls_to_psa_error( ret ) );
5586}
5587
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005588#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5589#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005590
Gilles Peskine7228da22019-07-15 11:06:15 +02005591psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005592 size_t seed_size )
5593{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005594 if( global_data.initialized )
5595 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005596
5597 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5598 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5599 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5600 return( PSA_ERROR_INVALID_ARGUMENT );
5601
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005602 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005603}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005604#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005605
Gilles Peskinee56e8782019-04-26 17:34:02 +02005606#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5607static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5608 size_t domain_parameters_size,
5609 int *exponent )
5610{
5611 size_t i;
5612 uint32_t acc = 0;
5613
5614 if( domain_parameters_size == 0 )
5615 {
5616 *exponent = 65537;
5617 return( PSA_SUCCESS );
5618 }
5619
5620 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5621 * support values that fit in a 32-bit integer, which is larger than
5622 * int on just about every platform anyway. */
5623 if( domain_parameters_size > sizeof( acc ) )
5624 return( PSA_ERROR_NOT_SUPPORTED );
5625 for( i = 0; i < domain_parameters_size; i++ )
5626 acc = ( acc << 8 ) | domain_parameters[i];
5627 if( acc > INT_MAX )
5628 return( PSA_ERROR_NOT_SUPPORTED );
5629 *exponent = acc;
5630 return( PSA_SUCCESS );
5631}
5632#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5633
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005634static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005635 psa_key_slot_t *slot, size_t bits,
5636 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005637{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005638 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005639
Gilles Peskinee56e8782019-04-26 17:34:02 +02005640 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005641 return( PSA_ERROR_INVALID_ARGUMENT );
5642
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005643 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005644 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005645 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005646 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005647 if( status != PSA_SUCCESS )
5648 return( status );
5649 status = psa_generate_random( slot->data.raw.data,
5650 slot->data.raw.bytes );
5651 if( status != PSA_SUCCESS )
5652 {
5653 mbedtls_free( slot->data.raw.data );
5654 return( status );
5655 }
5656#if defined(MBEDTLS_DES_C)
5657 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005658 psa_des_set_key_parity( slot->data.raw.data,
5659 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005660#endif /* MBEDTLS_DES_C */
5661 }
5662 else
5663
5664#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005665 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005666 {
5667 mbedtls_rsa_context *rsa;
5668 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005669 int exponent;
5670 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005671 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5672 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005673 /* Accept only byte-aligned keys, for the same reasons as
5674 * in psa_import_rsa_key(). */
5675 if( bits % 8 != 0 )
5676 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005677 status = psa_read_rsa_exponent( domain_parameters,
5678 domain_parameters_size,
5679 &exponent );
5680 if( status != PSA_SUCCESS )
5681 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005682 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5683 if( rsa == NULL )
5684 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5685 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5686 ret = mbedtls_rsa_gen_key( rsa,
5687 mbedtls_ctr_drbg_random,
5688 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005689 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005690 exponent );
5691 if( ret != 0 )
5692 {
5693 mbedtls_rsa_free( rsa );
5694 mbedtls_free( rsa );
5695 return( mbedtls_to_psa_error( ret ) );
5696 }
5697 slot->data.rsa = rsa;
5698 }
5699 else
5700#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5701
5702#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005703 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005704 {
5705 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5706 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5707 const mbedtls_ecp_curve_info *curve_info =
5708 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5709 mbedtls_ecp_keypair *ecp;
5710 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005711 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005712 return( PSA_ERROR_NOT_SUPPORTED );
5713 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5714 return( PSA_ERROR_NOT_SUPPORTED );
5715 if( curve_info->bit_size != bits )
5716 return( PSA_ERROR_INVALID_ARGUMENT );
5717 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5718 if( ecp == NULL )
5719 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5720 mbedtls_ecp_keypair_init( ecp );
5721 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5722 mbedtls_ctr_drbg_random,
5723 &global_data.ctr_drbg );
5724 if( ret != 0 )
5725 {
5726 mbedtls_ecp_keypair_free( ecp );
5727 mbedtls_free( ecp );
5728 return( mbedtls_to_psa_error( ret ) );
5729 }
5730 slot->data.ecp = ecp;
5731 }
5732 else
5733#endif /* MBEDTLS_ECP_C */
5734
5735 return( PSA_ERROR_NOT_SUPPORTED );
5736
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005737 return( PSA_SUCCESS );
5738}
5739
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005740psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005741 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005742{
5743 psa_status_t status;
5744 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005745 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005746 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005747#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5748 if( driver != NULL )
5749 {
5750 /* Generating a key in a secure element is not implemented yet. */
5751 status = PSA_ERROR_NOT_SUPPORTED;
5752 }
5753#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005754 if( status == PSA_SUCCESS )
5755 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005756 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005757 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005758 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005759 }
5760 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005761 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005762 if( status != PSA_SUCCESS )
5763 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005764 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005765 *handle = 0;
5766 }
5767 return( status );
5768}
5769
5770
Gilles Peskine05d69892018-06-19 22:00:52 +02005771
5772/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005773/* Module setup */
5774/****************************************************************/
5775
Gilles Peskine5e769522018-11-20 21:59:56 +01005776psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5777 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5778 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5779{
5780 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5781 return( PSA_ERROR_BAD_STATE );
5782 global_data.entropy_init = entropy_init;
5783 global_data.entropy_free = entropy_free;
5784 return( PSA_SUCCESS );
5785}
5786
Gilles Peskinee59236f2018-01-27 23:32:46 +01005787void mbedtls_psa_crypto_free( void )
5788{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005789 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005790 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5791 {
5792 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005793 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005794 }
5795 /* Wipe all remaining data, including configuration.
5796 * In particular, this sets all state indicator to the value
5797 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005798 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005799#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005800 /* Unregister all secure element drivers, so that we restart from
5801 * a pristine state. */
5802 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005803#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005804}
5805
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005806#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5807/** Recover a transaction that was interrupted by a power failure.
5808 *
5809 * This function is called during initialization, before psa_crypto_init()
5810 * returns. If this function returns a failure status, the initialization
5811 * fails.
5812 */
5813static psa_status_t psa_crypto_recover_transaction(
5814 const psa_crypto_transaction_t *transaction )
5815{
5816 switch( transaction->unknown.type )
5817 {
5818 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5819 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
5820 /* TOnogrepDO - fall through to the failure case until this
5821 * is implemented */
5822 default:
5823 /* We found an unsupported transaction in the storage.
5824 * We don't know what state the storage is in. Give up. */
5825 return( PSA_ERROR_STORAGE_FAILURE );
5826 }
5827}
5828#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5829
Gilles Peskinee59236f2018-01-27 23:32:46 +01005830psa_status_t psa_crypto_init( void )
5831{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005832 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005833 const unsigned char drbg_seed[] = "PSA";
5834
Gilles Peskinec6b69072018-11-20 21:42:52 +01005835 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005836 if( global_data.initialized != 0 )
5837 return( PSA_SUCCESS );
5838
Gilles Peskine5e769522018-11-20 21:59:56 +01005839 /* Set default configuration if
5840 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5841 if( global_data.entropy_init == NULL )
5842 global_data.entropy_init = mbedtls_entropy_init;
5843 if( global_data.entropy_free == NULL )
5844 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005845
Gilles Peskinec6b69072018-11-20 21:42:52 +01005846 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005847 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005848 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005849 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005850 status = mbedtls_to_psa_error(
5851 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5852 mbedtls_entropy_func,
5853 &global_data.entropy,
5854 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5855 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005856 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005857 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005858
Gilles Peskine66fb1262018-12-10 16:29:04 +01005859 status = psa_initialize_key_slots( );
5860 if( status != PSA_SUCCESS )
5861 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005862
Gilles Peskine4b734222019-07-24 15:56:31 +02005863#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005864 status = psa_crypto_load_transaction( );
5865 if( status == PSA_SUCCESS )
5866 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005867 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5868 if( status != PSA_SUCCESS )
5869 goto exit;
5870 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005871 }
5872 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5873 {
5874 /* There's no transaction to complete. It's all good. */
5875 status = PSA_SUCCESS;
5876 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005877#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005878
Gilles Peskinec6b69072018-11-20 21:42:52 +01005879 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005880 global_data.initialized = 1;
5881
Gilles Peskinee59236f2018-01-27 23:32:46 +01005882exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005883 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005884 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005885 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005886}
5887
5888#endif /* MBEDTLS_PSA_CRYPTO_C */