blob: 4721f6bfe20c64a9522ac903ffbb64a331d22fed [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043#include <stdlib.h>
44#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020046#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010047#define mbedtls_calloc calloc
48#define mbedtls_free free
49#endif
50
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020052#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000053#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020054#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/blowfish.h"
56#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020057#include "mbedtls/chacha20.h"
58#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/cipher.h"
60#include "mbedtls/ccm.h"
61#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010062#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020064#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010065#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010066#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/error.h"
68#include "mbedtls/gcm.h"
69#include "mbedtls/md2.h"
70#include "mbedtls/md4.h"
71#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
73#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010076#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/sha1.h"
80#include "mbedtls/sha256.h"
81#include "mbedtls/sha512.h"
82#include "mbedtls/xtea.h"
83
Gilles Peskine996deb12018-08-01 15:45:45 +020084#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
85
Gilles Peskine9ef733f2018-02-07 21:05:37 +010086/* constant-time buffer comparison */
87static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
88{
89 size_t i;
90 unsigned char diff = 0;
91
92 for( i = 0; i < n; i++ )
93 diff |= a[i] ^ b[i];
94
95 return( diff );
96}
97
98
99
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100100/****************************************************************/
101/* Global data, support functions and library management */
102/****************************************************************/
103
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200104static int key_type_is_raw_bytes( psa_key_type_t type )
105{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200106 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200107}
108
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109/* Values for psa_global_data_t::rng_state */
110#define RNG_NOT_INITIALIZED 0
111#define RNG_INITIALIZED 1
112#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100113
Gilles Peskine2d277862018-06-18 15:41:12 +0200114typedef struct
115{
Gilles Peskine5e769522018-11-20 21:59:56 +0100116 void (* entropy_init )( mbedtls_entropy_context *ctx );
117 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118 mbedtls_entropy_context entropy;
119 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100120 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100121 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100122} psa_global_data_t;
123
124static psa_global_data_t global_data;
125
itayzafrir0adf0fc2018-09-06 16:24:41 +0300126#define GUARD_MODULE_INITIALIZED \
127 if( global_data.initialized == 0 ) \
128 return( PSA_ERROR_BAD_STATE );
129
Gilles Peskinee59236f2018-01-27 23:32:46 +0100130static psa_status_t mbedtls_to_psa_error( int ret )
131{
Gilles Peskinea5905292018-02-07 20:59:33 +0100132 /* If there's both a high-level code and low-level code, dispatch on
133 * the high-level code. */
134 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100135 {
136 case 0:
137 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100138
139 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
140 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
141 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
142 return( PSA_ERROR_NOT_SUPPORTED );
143 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
144 return( PSA_ERROR_HARDWARE_FAILURE );
145
146 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
147 return( PSA_ERROR_HARDWARE_FAILURE );
148
Gilles Peskine9a944802018-06-21 09:35:35 +0200149 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
150 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
151 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
152 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
153 case MBEDTLS_ERR_ASN1_INVALID_DATA:
154 return( PSA_ERROR_INVALID_ARGUMENT );
155 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
156 return( PSA_ERROR_INSUFFICIENT_MEMORY );
157 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
158 return( PSA_ERROR_BUFFER_TOO_SMALL );
159
Jaeden Amero93e21112019-02-20 13:57:28 +0000160#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000161 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100163 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100165 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
166 return( PSA_ERROR_NOT_SUPPORTED );
167 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
168 return( PSA_ERROR_HARDWARE_FAILURE );
169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
180 case MBEDTLS_ERR_CCM_BAD_INPUT:
181 return( PSA_ERROR_INVALID_ARGUMENT );
182 case MBEDTLS_ERR_CCM_AUTH_FAILED:
183 return( PSA_ERROR_INVALID_SIGNATURE );
184 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
Gilles Peskine26869f22019-05-06 15:25:00 +0200187 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
188 return( PSA_ERROR_INVALID_ARGUMENT );
189
190 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
191 return( PSA_ERROR_BAD_STATE );
192 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
193 return( PSA_ERROR_INVALID_SIGNATURE );
194
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
196 return( PSA_ERROR_NOT_SUPPORTED );
197 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
200 return( PSA_ERROR_INSUFFICIENT_MEMORY );
201 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
202 return( PSA_ERROR_INVALID_PADDING );
203 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
204 return( PSA_ERROR_BAD_STATE );
205 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
206 return( PSA_ERROR_INVALID_SIGNATURE );
207 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200208 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
213 return( PSA_ERROR_HARDWARE_FAILURE );
214
215 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
216 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
217 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
218 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
219 return( PSA_ERROR_NOT_SUPPORTED );
220 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
221 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
222
223 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
226 return( PSA_ERROR_HARDWARE_FAILURE );
227
Gilles Peskinee59236f2018-01-27 23:32:46 +0100228 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
229 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
230 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
233 case MBEDTLS_ERR_GCM_AUTH_FAILED:
234 return( PSA_ERROR_INVALID_SIGNATURE );
235 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200236 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100237 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
238 return( PSA_ERROR_HARDWARE_FAILURE );
239
240 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
242 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
245 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
246 return( PSA_ERROR_NOT_SUPPORTED );
247 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
248 return( PSA_ERROR_INVALID_ARGUMENT );
249 case MBEDTLS_ERR_MD_ALLOC_FAILED:
250 return( PSA_ERROR_INSUFFICIENT_MEMORY );
251 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
252 return( PSA_ERROR_STORAGE_FAILURE );
253 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
254 return( PSA_ERROR_HARDWARE_FAILURE );
255
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
257 return( PSA_ERROR_STORAGE_FAILURE );
258 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
263 return( PSA_ERROR_BUFFER_TOO_SMALL );
264 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_ALLOC_FAILED:
274 return( PSA_ERROR_INSUFFICIENT_MEMORY );
275 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
276 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100279 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100280 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
281 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
284 return( PSA_ERROR_NOT_SUPPORTED );
285 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
286 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
287 return( PSA_ERROR_NOT_PERMITTED );
288 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_PK_INVALID_ALG:
291 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
292 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
293 return( PSA_ERROR_NOT_SUPPORTED );
294 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
295 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
300 return( PSA_ERROR_HARDWARE_FAILURE );
301 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
302 return( PSA_ERROR_NOT_SUPPORTED );
303
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
305 return( PSA_ERROR_HARDWARE_FAILURE );
306
307 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_RSA_INVALID_PADDING:
310 return( PSA_ERROR_INVALID_PADDING );
311 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
312 return( PSA_ERROR_HARDWARE_FAILURE );
313 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
314 return( PSA_ERROR_INVALID_ARGUMENT );
315 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
316 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200317 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100318 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
319 return( PSA_ERROR_INVALID_SIGNATURE );
320 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
321 return( PSA_ERROR_BUFFER_TOO_SMALL );
322 case MBEDTLS_ERR_RSA_RNG_FAILED:
323 return( PSA_ERROR_INSUFFICIENT_MEMORY );
324 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
325 return( PSA_ERROR_NOT_SUPPORTED );
326 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
327 return( PSA_ERROR_HARDWARE_FAILURE );
328
329 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
330 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
331 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
332 return( PSA_ERROR_HARDWARE_FAILURE );
333
334 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
335 return( PSA_ERROR_INVALID_ARGUMENT );
336 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
337 return( PSA_ERROR_HARDWARE_FAILURE );
338
itayzafrir5c753392018-05-08 11:18:38 +0300339 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300340 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300341 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
343 return( PSA_ERROR_BUFFER_TOO_SMALL );
344 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
345 return( PSA_ERROR_NOT_SUPPORTED );
346 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
347 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
348 return( PSA_ERROR_INVALID_SIGNATURE );
349 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
350 return( PSA_ERROR_INSUFFICIENT_MEMORY );
351 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
352 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300353
Gilles Peskinee59236f2018-01-27 23:32:46 +0100354 default:
David Saadab4ecc272019-02-14 13:48:10 +0200355 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100356 }
357}
358
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200360
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/****************************************************************/
363/* Key management */
364/****************************************************************/
365
Gilles Peskine73167e12019-07-12 23:44:37 +0200366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
367static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
368{
369 return( psa_key_lifetime_is_external( slot->lifetime ) );
370}
371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
372
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100373#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200374static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
375{
376 switch( grpid )
377 {
378 case MBEDTLS_ECP_DP_SECP192R1:
379 return( PSA_ECC_CURVE_SECP192R1 );
380 case MBEDTLS_ECP_DP_SECP224R1:
381 return( PSA_ECC_CURVE_SECP224R1 );
382 case MBEDTLS_ECP_DP_SECP256R1:
383 return( PSA_ECC_CURVE_SECP256R1 );
384 case MBEDTLS_ECP_DP_SECP384R1:
385 return( PSA_ECC_CURVE_SECP384R1 );
386 case MBEDTLS_ECP_DP_SECP521R1:
387 return( PSA_ECC_CURVE_SECP521R1 );
388 case MBEDTLS_ECP_DP_BP256R1:
389 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
390 case MBEDTLS_ECP_DP_BP384R1:
391 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
392 case MBEDTLS_ECP_DP_BP512R1:
393 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
394 case MBEDTLS_ECP_DP_CURVE25519:
395 return( PSA_ECC_CURVE_CURVE25519 );
396 case MBEDTLS_ECP_DP_SECP192K1:
397 return( PSA_ECC_CURVE_SECP192K1 );
398 case MBEDTLS_ECP_DP_SECP224K1:
399 return( PSA_ECC_CURVE_SECP224K1 );
400 case MBEDTLS_ECP_DP_SECP256K1:
401 return( PSA_ECC_CURVE_SECP256K1 );
402 case MBEDTLS_ECP_DP_CURVE448:
403 return( PSA_ECC_CURVE_CURVE448 );
404 default:
405 return( 0 );
406 }
407}
408
Gilles Peskine12313cd2018-06-20 00:20:32 +0200409static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
410{
411 switch( curve )
412 {
413 case PSA_ECC_CURVE_SECP192R1:
414 return( MBEDTLS_ECP_DP_SECP192R1 );
415 case PSA_ECC_CURVE_SECP224R1:
416 return( MBEDTLS_ECP_DP_SECP224R1 );
417 case PSA_ECC_CURVE_SECP256R1:
418 return( MBEDTLS_ECP_DP_SECP256R1 );
419 case PSA_ECC_CURVE_SECP384R1:
420 return( MBEDTLS_ECP_DP_SECP384R1 );
421 case PSA_ECC_CURVE_SECP521R1:
422 return( MBEDTLS_ECP_DP_SECP521R1 );
423 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
424 return( MBEDTLS_ECP_DP_BP256R1 );
425 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
426 return( MBEDTLS_ECP_DP_BP384R1 );
427 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
428 return( MBEDTLS_ECP_DP_BP512R1 );
429 case PSA_ECC_CURVE_CURVE25519:
430 return( MBEDTLS_ECP_DP_CURVE25519 );
431 case PSA_ECC_CURVE_SECP192K1:
432 return( MBEDTLS_ECP_DP_SECP192K1 );
433 case PSA_ECC_CURVE_SECP224K1:
434 return( MBEDTLS_ECP_DP_SECP224K1 );
435 case PSA_ECC_CURVE_SECP256K1:
436 return( MBEDTLS_ECP_DP_SECP256K1 );
437 case PSA_ECC_CURVE_CURVE448:
438 return( MBEDTLS_ECP_DP_CURVE448 );
439 default:
440 return( MBEDTLS_ECP_DP_NONE );
441 }
442}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100443#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200444
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
446 size_t bits,
447 struct raw_data *raw )
448{
449 /* Check that the bit size is acceptable for the key type */
450 switch( type )
451 {
452 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200453 if( bits == 0 )
454 {
455 raw->bytes = 0;
456 raw->data = NULL;
457 return( PSA_SUCCESS );
458 }
459 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460#if defined(MBEDTLS_MD_C)
461 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200462#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200463 case PSA_KEY_TYPE_DERIVE:
464 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200465#if defined(MBEDTLS_AES_C)
466 case PSA_KEY_TYPE_AES:
467 if( bits != 128 && bits != 192 && bits != 256 )
468 return( PSA_ERROR_INVALID_ARGUMENT );
469 break;
470#endif
471#if defined(MBEDTLS_CAMELLIA_C)
472 case PSA_KEY_TYPE_CAMELLIA:
473 if( bits != 128 && bits != 192 && bits != 256 )
474 return( PSA_ERROR_INVALID_ARGUMENT );
475 break;
476#endif
477#if defined(MBEDTLS_DES_C)
478 case PSA_KEY_TYPE_DES:
479 if( bits != 64 && bits != 128 && bits != 192 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
481 break;
482#endif
483#if defined(MBEDTLS_ARC4_C)
484 case PSA_KEY_TYPE_ARC4:
485 if( bits < 8 || bits > 2048 )
486 return( PSA_ERROR_INVALID_ARGUMENT );
487 break;
488#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200489#if defined(MBEDTLS_CHACHA20_C)
490 case PSA_KEY_TYPE_CHACHA20:
491 if( bits != 256 )
492 return( PSA_ERROR_INVALID_ARGUMENT );
493 break;
494#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 default:
496 return( PSA_ERROR_NOT_SUPPORTED );
497 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200498 if( bits % 8 != 0 )
499 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200500
501 /* Allocate memory for the key */
502 raw->bytes = PSA_BITS_TO_BYTES( bits );
503 raw->data = mbedtls_calloc( 1, raw->bytes );
504 if( raw->data == NULL )
505 {
506 raw->bytes = 0;
507 return( PSA_ERROR_INSUFFICIENT_MEMORY );
508 }
509 return( PSA_SUCCESS );
510}
511
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200512#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100513/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
514 * that are not a multiple of 8) well. For example, there is only
515 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
516 * way to return the exact bit size of a key.
517 * To keep things simple, reject non-byte-aligned key sizes. */
518static psa_status_t psa_check_rsa_key_byte_aligned(
519 const mbedtls_rsa_context *rsa )
520{
521 mbedtls_mpi n;
522 psa_status_t status;
523 mbedtls_mpi_init( &n );
524 status = mbedtls_to_psa_error(
525 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
526 if( status == PSA_SUCCESS )
527 {
528 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
529 status = PSA_ERROR_NOT_SUPPORTED;
530 }
531 mbedtls_mpi_free( &n );
532 return( status );
533}
534
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000535static psa_status_t psa_import_rsa_key( psa_key_type_t type,
536 const uint8_t *data,
537 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200538 mbedtls_rsa_context **p_rsa )
539{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000540 psa_status_t status;
541 mbedtls_pk_context pk;
542 mbedtls_rsa_context *rsa;
543 size_t bits;
544
545 mbedtls_pk_init( &pk );
546
547 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200548 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000549 status = mbedtls_to_psa_error(
550 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200551 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000552 status = mbedtls_to_psa_error(
553 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
554 if( status != PSA_SUCCESS )
555 goto exit;
556
557 /* We have something that the pkparse module recognizes. If it is a
558 * valid RSA key, store it. */
559 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000561 status = PSA_ERROR_INVALID_ARGUMENT;
562 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200563 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000564
565 rsa = mbedtls_pk_rsa( pk );
566 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
567 * supports non-byte-aligned key sizes, but not well. For example,
568 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
569 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
570 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
571 {
572 status = PSA_ERROR_NOT_SUPPORTED;
573 goto exit;
574 }
575 status = psa_check_rsa_key_byte_aligned( rsa );
576
577exit:
578 /* Free the content of the pk object only on error. */
579 if( status != PSA_SUCCESS )
580 {
581 mbedtls_pk_free( &pk );
582 return( status );
583 }
584
585 /* On success, store the content of the object in the RSA context. */
586 *p_rsa = rsa;
587
588 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200589}
590#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
591
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000592#if defined(MBEDTLS_ECP_C)
593
594/* Import a public key given as the uncompressed representation defined by SEC1
595 * 2.3.3 as the content of an ECPoint. */
596static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
597 const uint8_t *data,
598 size_t data_length,
599 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200600{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000602 mbedtls_ecp_keypair *ecp = NULL;
603 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
604
605 *p_ecp = NULL;
606 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
607 if( ecp == NULL )
608 return( PSA_ERROR_INSUFFICIENT_MEMORY );
609 mbedtls_ecp_keypair_init( ecp );
610
611 /* Load the group. */
612 status = mbedtls_to_psa_error(
613 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616 /* Load the public value. */
617 status = mbedtls_to_psa_error(
618 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
619 data, data_length ) );
620 if( status != PSA_SUCCESS )
621 goto exit;
622
623 /* Check that the point is on the curve. */
624 status = mbedtls_to_psa_error(
625 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
626 if( status != PSA_SUCCESS )
627 goto exit;
628
629 *p_ecp = ecp;
630 return( PSA_SUCCESS );
631
632exit:
633 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200634 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000635 mbedtls_ecp_keypair_free( ecp );
636 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200637 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000638 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200639}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000640#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200641
Gilles Peskinef76aa772018-10-29 19:24:33 +0100642#if defined(MBEDTLS_ECP_C)
643/* Import a private key given as a byte string which is the private value
644 * in big-endian order. */
645static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
646 const uint8_t *data,
647 size_t data_length,
648 mbedtls_ecp_keypair **p_ecp )
649{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651 mbedtls_ecp_keypair *ecp = NULL;
652 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
653
Gilles Peskinec9d910b2019-05-13 14:21:57 +0200654 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
655 return( PSA_ERROR_INVALID_ARGUMENT );
656
Gilles Peskinef76aa772018-10-29 19:24:33 +0100657 *p_ecp = NULL;
658 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
659 if( ecp == NULL )
660 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000661 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100662
663 /* Load the group. */
664 status = mbedtls_to_psa_error(
665 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
666 if( status != PSA_SUCCESS )
667 goto exit;
668 /* Load the secret value. */
669 status = mbedtls_to_psa_error(
670 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
671 if( status != PSA_SUCCESS )
672 goto exit;
673 /* Validate the private key. */
674 status = mbedtls_to_psa_error(
675 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678 /* Calculate the public key from the private key. */
679 status = mbedtls_to_psa_error(
680 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
681 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
682 if( status != PSA_SUCCESS )
683 goto exit;
684
685 *p_ecp = ecp;
686 return( PSA_SUCCESS );
687
688exit:
689 if( ecp != NULL )
690 {
691 mbedtls_ecp_keypair_free( ecp );
692 mbedtls_free( ecp );
693 }
694 return( status );
695}
696#endif /* defined(MBEDTLS_ECP_C) */
697
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100698/** Import key data into a slot. `slot->type` must have been set
699 * previously. This function assumes that the slot does not contain
700 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100701psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
702 const uint8_t *data,
703 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200705 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706
Darryl Green940d72c2018-07-13 13:18:51 +0100707 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100709 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 if( data_length > SIZE_MAX / 8 )
711 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100712 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200713 PSA_BYTES_TO_BITS( data_length ),
714 &slot->data.raw );
715 if( status != PSA_SUCCESS )
716 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200717 if( data_length != 0 )
718 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100719 }
720 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100721#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200722 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100723 {
Darryl Green940d72c2018-07-13 13:18:51 +0100724 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100725 data, data_length,
726 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100727 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000728 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
729 {
730 status = psa_import_ec_public_key(
731 PSA_KEY_TYPE_GET_CURVE( slot->type ),
732 data, data_length,
733 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000734 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100735 else
736#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000737#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
738 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100739 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000740 status = psa_import_rsa_key( slot->type,
741 data, data_length,
742 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 }
744 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000745#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 {
747 return( PSA_ERROR_NOT_SUPPORTED );
748 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000749 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100750}
751
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100752/** Calculate the intersection of two algorithm usage policies.
753 *
754 * Return 0 (which allows no operation) on incompatibility.
755 */
756static psa_algorithm_t psa_key_policy_algorithm_intersection(
757 psa_algorithm_t alg1,
758 psa_algorithm_t alg2 )
759{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200760 /* Common case: both sides actually specify the same policy. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100761 if( alg1 == alg2 )
762 return( alg1 );
763 /* If the policies are from the same hash-and-sign family, check
Gilles Peskinef603c712019-01-19 13:40:11 +0100764 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100765 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
766 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
767 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
768 {
769 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
770 return( alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100771 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100772 return( alg1 );
773 }
774 /* If the policies are incompatible, allow nothing. */
775 return( 0 );
776}
777
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200778static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
779 psa_algorithm_t requested_alg )
780{
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200781 /* Common case: the policy only allows requested_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200782 if( requested_alg == policy_alg )
783 return( 1 );
784 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskinef25c9ec2019-05-22 11:45:59 +0200785 * and requested_alg is the same hash-and-sign family with any hash,
786 * then requested_alg is compliant with policy_alg. */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200787 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
788 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
789 {
790 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
791 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
792 }
793 /* If it isn't permitted, it's forbidden. */
794 return( 0 );
795}
796
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100797/** Test whether a policy permits an algorithm.
798 *
799 * The caller must test usage flags separately.
800 */
801static int psa_key_policy_permits( const psa_key_policy_t *policy,
802 psa_algorithm_t alg )
803{
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200804 return( psa_key_algorithm_permits( policy->alg, alg ) ||
805 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100806}
807
Gilles Peskinef603c712019-01-19 13:40:11 +0100808/** Restrict a key policy based on a constraint.
809 *
810 * \param[in,out] policy The policy to restrict.
811 * \param[in] constraint The policy constraint to apply.
812 *
813 * \retval #PSA_SUCCESS
814 * \c *policy contains the intersection of the original value of
815 * \c *policy and \c *constraint.
816 * \retval #PSA_ERROR_INVALID_ARGUMENT
817 * \c *policy and \c *constraint are incompatible.
818 * \c *policy is unchanged.
819 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100820static psa_status_t psa_restrict_key_policy(
821 psa_key_policy_t *policy,
822 const psa_key_policy_t *constraint )
823{
824 psa_algorithm_t intersection_alg =
825 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200826 psa_algorithm_t intersection_alg2 =
827 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100828 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
829 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200830 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
831 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100832 policy->usage &= constraint->usage;
Gilles Peskinef603c712019-01-19 13:40:11 +0100833 policy->alg = intersection_alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200834 policy->alg2 = intersection_alg2;
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100835 return( PSA_SUCCESS );
836}
837
Darryl Green06fd18d2018-07-16 11:21:11 +0100838/** Retrieve a slot which must contain a key. The key must have allow all the
839 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
840 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100841static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100842 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100843 psa_key_usage_t usage,
844 psa_algorithm_t alg )
845{
846 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100847 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100848
849 *p_slot = NULL;
850
Gilles Peskinec5487a82018-12-03 18:08:14 +0100851 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100852 if( status != PSA_SUCCESS )
853 return( status );
854 if( slot->type == PSA_KEY_TYPE_NONE )
David Saadab4ecc272019-02-14 13:48:10 +0200855 return( PSA_ERROR_DOES_NOT_EXIST );
Darryl Green06fd18d2018-07-16 11:21:11 +0100856
857 /* Enforce that usage policy for the key slot contains all the flags
858 * required by the usage parameter. There is one exception: public
859 * keys can always be exported, so we treat public key objects as
860 * if they had the export flag. */
861 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
862 usage &= ~PSA_KEY_USAGE_EXPORT;
863 if( ( slot->policy.usage & usage ) != usage )
864 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100865
866 /* Enforce that the usage policy permits the requested algortihm. */
867 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100868 return( PSA_ERROR_NOT_PERMITTED );
869
870 *p_slot = slot;
871 return( PSA_SUCCESS );
872}
Darryl Green940d72c2018-07-13 13:18:51 +0100873
Gilles Peskine28f8f302019-07-24 13:30:31 +0200874/** Retrieve a slot which must contain a transparent key.
875 *
876 * A transparent key is a key for which the key material is directly
877 * available, as opposed to a key in a secure element.
878 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200879 * This is a temporary function to use instead of psa_get_key_from_slot()
880 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200881 */
882#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
883static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
884 psa_key_slot_t **p_slot,
885 psa_key_usage_t usage,
886 psa_algorithm_t alg )
887{
888 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
889 if( status != PSA_SUCCESS )
890 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200891 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200892 {
893 *p_slot = NULL;
894 return( PSA_ERROR_NOT_SUPPORTED );
895 }
896 return( PSA_SUCCESS );
897}
898#else /* MBEDTLS_PSA_CRYPTO_SE_C */
899/* With no secure element support, all keys are transparent. */
900#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
901 psa_get_key_from_slot( handle, p_slot, usage, alg )
902#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
903
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100904/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100905static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000906{
Gilles Peskine73167e12019-07-12 23:44:37 +0200907#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
908 if( psa_key_slot_is_external( slot ) )
909 {
910 /* No key material to clean. */
911 }
912 else
913#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +0000914 if( slot->type == PSA_KEY_TYPE_NONE )
915 {
916 /* No key material to clean. */
917 }
918 else if( key_type_is_raw_bytes( slot->type ) )
919 {
920 mbedtls_free( slot->data.raw.data );
921 }
922 else
923#if defined(MBEDTLS_RSA_C)
924 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
925 {
926 mbedtls_rsa_free( slot->data.rsa );
927 mbedtls_free( slot->data.rsa );
928 }
929 else
930#endif /* defined(MBEDTLS_RSA_C) */
931#if defined(MBEDTLS_ECP_C)
932 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
933 {
934 mbedtls_ecp_keypair_free( slot->data.ecp );
935 mbedtls_free( slot->data.ecp );
936 }
937 else
938#endif /* defined(MBEDTLS_ECP_C) */
939 {
940 /* Shouldn't happen: the key type is not any type that we
941 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200942 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000943 }
944
945 return( PSA_SUCCESS );
946}
947
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100948static void psa_abort_operations_using_key( psa_key_slot_t *slot )
949{
Gilles Peskinec88644d2019-04-12 15:03:38 +0200950 /*FIXME how to implement this?*/
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100951 (void) slot;
952}
953
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100954/** Completely wipe a slot in memory, including its policy.
955 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100956psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100957{
958 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100959 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100960 /* At this point, key material and other type-specific content has
961 * been wiped. Clear remaining metadata. We can call memset and not
962 * zeroize because the metadata is not particularly sensitive. */
963 memset( slot, 0, sizeof( *slot ) );
964 return( status );
965}
966
Gilles Peskinec5487a82018-12-03 18:08:14 +0100967psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100969 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100970 psa_status_t status = PSA_SUCCESS;
971 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200972#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
973 psa_se_drv_table_entry_t *driver;
974#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975
Gilles Peskinec5487a82018-12-03 18:08:14 +0100976 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200977 if( status != PSA_SUCCESS )
978 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +0200979
980#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
981 driver = psa_get_se_driver_entry( slot->lifetime );
982 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +0200983 {
Gilles Peskine60450a42019-07-25 11:32:45 +0200984 /* For a key in a secure element, we need to do three things:
985 * remove the key file in internal storage, destroy the
986 * key inside the secure element, and update the driver's
987 * persistent data. Start a transaction that will encompass these
988 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +0200989 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
990 psa_crypto_transaction.key.lifetime = slot->lifetime;
991 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
992 psa_crypto_transaction.key.id = slot->persistent_storage_id;
993 status = psa_crypto_save_transaction( );
994 if( status != PSA_SUCCESS )
995 {
Gilles Peskine66be51c2019-07-25 18:02:52 +0200996 (void) psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +0200997 /* TOnogrepDO: destroy what can be destroyed anyway */
998 return( status );
999 }
1000
Gilles Peskine354f7672019-07-12 23:46:38 +02001001 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskinefc762652019-07-22 19:30:34 +02001002 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001003#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1004
Darryl Greend49a4992018-06-18 17:27:26 +01001005#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1006 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
1007 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01001008 storage_status =
1009 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +01001010 }
1011#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001012
Gilles Peskinefc762652019-07-22 19:30:34 +02001013#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1014 if( driver != NULL )
1015 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001016 psa_status_t status2;
1017 status = psa_save_se_persistent_data( driver );
1018 status2 = psa_crypto_stop_transaction( );
1019 if( status == PSA_SUCCESS )
1020 status = status2;
Gilles Peskinefc762652019-07-22 19:30:34 +02001021 if( status != PSA_SUCCESS )
1022 {
1023 /* TOnogrepDO: destroy what can be destroyed anyway */
1024 return( status );
1025 }
1026 }
1027#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1028
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001029 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +01001030 if( status != PSA_SUCCESS )
1031 return( status );
1032 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033}
1034
Gilles Peskineb870b182018-07-06 16:02:09 +02001035/* Return the size of the key in the given slot, in bits. */
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001036static size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +02001037{
Gilles Peskine424f8942019-07-15 21:59:53 +02001038#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1039 if( psa_get_se_driver( slot->lifetime, NULL, NULL ) )
1040 return( slot->data.se.bits );
1041#endif /* defined(MBEDTLS_PSA_CRYPTO_SE_C) */
1042
Gilles Peskineb870b182018-07-06 16:02:09 +02001043 if( key_type_is_raw_bytes( slot->type ) )
1044 return( slot->data.raw.bytes * 8 );
1045#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001046 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001047 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001048#endif /* defined(MBEDTLS_RSA_C) */
1049#if defined(MBEDTLS_ECP_C)
1050 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1051 return( slot->data.ecp->grp.pbits );
1052#endif /* defined(MBEDTLS_ECP_C) */
1053 /* Shouldn't happen except on an empty slot. */
1054 return( 0 );
1055}
1056
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001057void psa_reset_key_attributes( psa_key_attributes_t *attributes )
1058{
Gilles Peskineb699f072019-04-26 16:06:02 +02001059 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001060 memset( attributes, 0, sizeof( *attributes ) );
1061}
1062
Gilles Peskineb699f072019-04-26 16:06:02 +02001063psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1064 psa_key_type_t type,
1065 const uint8_t *data,
1066 size_t data_length )
1067{
1068 uint8_t *copy = NULL;
1069
1070 if( data_length != 0 )
1071 {
1072 copy = mbedtls_calloc( 1, data_length );
1073 if( copy == NULL )
1074 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1075 memcpy( copy, data, data_length );
1076 }
1077 /* After this point, this function is guaranteed to succeed, so it
1078 * can start modifying `*attributes`. */
1079
1080 if( attributes->domain_parameters != NULL )
1081 {
1082 mbedtls_free( attributes->domain_parameters );
1083 attributes->domain_parameters = NULL;
1084 attributes->domain_parameters_size = 0;
1085 }
1086
1087 attributes->domain_parameters = copy;
1088 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001089 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001090 return( PSA_SUCCESS );
1091}
1092
1093psa_status_t psa_get_key_domain_parameters(
1094 const psa_key_attributes_t *attributes,
1095 uint8_t *data, size_t data_size, size_t *data_length )
1096{
1097 if( attributes->domain_parameters_size > data_size )
1098 return( PSA_ERROR_BUFFER_TOO_SMALL );
1099 *data_length = attributes->domain_parameters_size;
1100 if( attributes->domain_parameters_size != 0 )
1101 memcpy( data, attributes->domain_parameters,
1102 attributes->domain_parameters_size );
1103 return( PSA_SUCCESS );
1104}
1105
1106#if defined(MBEDTLS_RSA_C)
1107static psa_status_t psa_get_rsa_public_exponent(
1108 const mbedtls_rsa_context *rsa,
1109 psa_key_attributes_t *attributes )
1110{
1111 mbedtls_mpi mpi;
1112 int ret;
1113 uint8_t *buffer = NULL;
1114 size_t buflen;
1115 mbedtls_mpi_init( &mpi );
1116
1117 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1118 if( ret != 0 )
1119 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001120 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1121 {
1122 /* It's the default value, which is reported as an empty string,
1123 * so there's nothing to do. */
1124 goto exit;
1125 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001126
1127 buflen = mbedtls_mpi_size( &mpi );
1128 buffer = mbedtls_calloc( 1, buflen );
1129 if( buffer == NULL )
1130 {
1131 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1132 goto exit;
1133 }
1134 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1135 if( ret != 0 )
1136 goto exit;
1137 attributes->domain_parameters = buffer;
1138 attributes->domain_parameters_size = buflen;
1139
1140exit:
1141 mbedtls_mpi_free( &mpi );
1142 if( ret != 0 )
1143 mbedtls_free( buffer );
1144 return( mbedtls_to_psa_error( ret ) );
1145}
1146#endif /* MBEDTLS_RSA_C */
1147
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001148/** Retrieve the generic attributes of a key in a slot.
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001149 *
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001150 * This function does not retrieve domain parameters, which require
1151 * additional memory management.
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001152 */
1153static void psa_get_key_slot_attributes( psa_key_slot_t *slot,
1154 psa_key_attributes_t *attributes )
1155{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001156 attributes->core.id = slot->persistent_storage_id;
1157 attributes->core.lifetime = slot->lifetime;
1158 attributes->core.policy = slot->policy;
1159 attributes->core.type = slot->type;
1160 attributes->core.bits = psa_get_key_slot_bits( slot );
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001161}
1162
1163/** Retrieve all the publicly-accessible attributes of a key.
1164 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001165psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1166 psa_key_attributes_t *attributes )
1167{
1168 psa_key_slot_t *slot;
1169 psa_status_t status;
1170
1171 psa_reset_key_attributes( attributes );
1172
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001173 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001174 if( status != PSA_SUCCESS )
1175 return( status );
1176
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001177 psa_get_key_slot_attributes( slot, attributes );
Gilles Peskineb699f072019-04-26 16:06:02 +02001178
1179 switch( slot->type )
1180 {
1181#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001182 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001183 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001184#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1185 /* TOnogrepDO: reporting the public exponent for opaque keys
1186 * is not yet implemented. */
1187 if( psa_get_se_driver( slot->lifetime, NULL, NULL ) )
1188 break;
1189#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001190 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1191 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001192#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001193 default:
1194 /* Nothing else to do. */
1195 break;
1196 }
1197
1198 if( status != PSA_SUCCESS )
1199 psa_reset_key_attributes( attributes );
1200 return( status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001201}
1202
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001203#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001204static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1205 unsigned char *buf, size_t size )
1206{
1207 int ret;
1208 unsigned char *c;
1209 size_t len = 0;
1210
1211 c = buf + size;
1212
1213 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1214
1215 return( (int) len );
1216}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001217#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001218
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001219static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1220 uint8_t *data,
1221 size_t data_size,
1222 size_t *data_length,
1223 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001224{
Gilles Peskine5d309672019-07-12 23:47:28 +02001225#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1226 const psa_drv_se_t *drv;
1227 psa_drv_se_context_t *drv_context;
1228#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1229
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001230 *data_length = 0;
1231
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001232 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001233 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001234
Gilles Peskine5d309672019-07-12 23:47:28 +02001235#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1236 if( psa_get_se_driver( slot->lifetime, &drv, &drv_context ) )
1237 {
1238 psa_drv_se_export_key_t method;
1239 if( drv->key_management == NULL )
1240 return( PSA_ERROR_NOT_SUPPORTED );
1241 method = ( export_public_key ?
1242 drv->key_management->p_export_public :
1243 drv->key_management->p_export );
1244 if( method == NULL )
1245 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001246 return( method( drv_context,
1247 slot->data.se.slot_number,
1248 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001249 }
1250#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1251
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001252 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001253 {
1254 if( slot->data.raw.bytes > data_size )
1255 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001256 if( data_size != 0 )
1257 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001258 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001259 memset( data + slot->data.raw.bytes, 0,
1260 data_size - slot->data.raw.bytes );
1261 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001262 *data_length = slot->data.raw.bytes;
1263 return( PSA_SUCCESS );
1264 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001265#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001266 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001267 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001268 psa_status_t status;
1269
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001270 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_slot_bits( slot ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001271 if( bytes > data_size )
1272 return( PSA_ERROR_BUFFER_TOO_SMALL );
1273 status = mbedtls_to_psa_error(
1274 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1275 if( status != PSA_SUCCESS )
1276 return( status );
1277 memset( data + bytes, 0, data_size - bytes );
1278 *data_length = bytes;
1279 return( PSA_SUCCESS );
1280 }
1281#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001282 else
Moran Peker17e36e12018-05-02 12:55:20 +03001283 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001284#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001285 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001286 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001287 {
Moran Pekera998bc62018-04-16 18:16:20 +03001288 mbedtls_pk_context pk;
1289 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001290 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001291 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001292#if defined(MBEDTLS_RSA_C)
1293 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001294 pk.pk_info = &mbedtls_rsa_info;
1295 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001296#else
1297 return( PSA_ERROR_NOT_SUPPORTED );
1298#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001299 }
1300 else
1301 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001302#if defined(MBEDTLS_ECP_C)
1303 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001304 pk.pk_info = &mbedtls_eckey_info;
1305 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001306#else
1307 return( PSA_ERROR_NOT_SUPPORTED );
1308#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001309 }
Moran Pekerd7326592018-05-29 16:56:39 +03001310 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001311 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001312 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001313 }
Moran Peker17e36e12018-05-02 12:55:20 +03001314 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001315 {
Moran Peker17e36e12018-05-02 12:55:20 +03001316 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001317 }
Moran Peker60364322018-04-29 11:34:58 +03001318 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001319 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001320 /* If data_size is 0 then data may be NULL and then the
1321 * call to memset would have undefined behavior. */
1322 if( data_size != 0 )
1323 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001324 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001325 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001326 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1327 * Move the data to the beginning and erase remaining data
1328 * at the original location. */
1329 if( 2 * (size_t) ret <= data_size )
1330 {
1331 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001332 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001333 }
1334 else if( (size_t) ret < data_size )
1335 {
1336 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001337 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001338 }
Moran Pekera998bc62018-04-16 18:16:20 +03001339 *data_length = ret;
1340 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001341 }
1342 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001343#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001344 {
1345 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001346 it is valid for a special-purpose implementation to omit
1347 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001348 return( PSA_ERROR_NOT_SUPPORTED );
1349 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001350 }
1351}
1352
Gilles Peskinec5487a82018-12-03 18:08:14 +01001353psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001354 uint8_t *data,
1355 size_t data_size,
1356 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001357{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001358 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001359 psa_status_t status;
1360
1361 /* Set the key to empty now, so that even when there are errors, we always
1362 * set data_length to a value between 0 and data_size. On error, setting
1363 * the key to empty is a good choice because an empty key representation is
1364 * unlikely to be accepted anywhere. */
1365 *data_length = 0;
1366
1367 /* Export requires the EXPORT flag. There is an exception for public keys,
1368 * which don't require any flag, but psa_get_key_from_slot takes
1369 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001370 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001371 if( status != PSA_SUCCESS )
1372 return( status );
1373 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001374 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001375}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001376
Gilles Peskinec5487a82018-12-03 18:08:14 +01001377psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001378 uint8_t *data,
1379 size_t data_size,
1380 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001381{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001382 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001383 psa_status_t status;
1384
1385 /* Set the key to empty now, so that even when there are errors, we always
1386 * set data_length to a value between 0 and data_size. On error, setting
1387 * the key to empty is a good choice because an empty key representation is
1388 * unlikely to be accepted anywhere. */
1389 *data_length = 0;
1390
1391 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001392 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001393 if( status != PSA_SUCCESS )
1394 return( status );
1395 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001396 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001397}
1398
Gilles Peskine4747d192019-04-17 15:05:45 +02001399static psa_status_t psa_set_key_policy_internal(
1400 psa_key_slot_t *slot,
1401 const psa_key_policy_t *policy )
1402{
1403 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001404 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001405 PSA_KEY_USAGE_ENCRYPT |
1406 PSA_KEY_USAGE_DECRYPT |
1407 PSA_KEY_USAGE_SIGN |
1408 PSA_KEY_USAGE_VERIFY |
1409 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1410 return( PSA_ERROR_INVALID_ARGUMENT );
1411
1412 slot->policy = *policy;
1413 return( PSA_SUCCESS );
1414}
1415
1416/** Prepare a key slot to receive key material.
1417 *
1418 * This function allocates a key slot and sets its metadata.
1419 *
1420 * If this function fails, call psa_fail_key_creation().
1421 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001422 * This function is intended to be used as follows:
1423 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1424 * it with the specified attributes, and assign it a handle.
1425 * -# Populate the slot with the key material.
1426 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1427 * In case of failure at any step, stop the sequence and call
1428 * psa_fail_key_creation().
1429 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001430 * \param[in] attributes Key attributes for the new key.
1431 * \param[out] handle On success, a handle for the allocated slot.
1432 * \param[out] p_slot On success, a pointer to the prepared slot.
1433 * \param[out] p_drv On any return, the driver for the key, if any.
1434 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001435 *
1436 * \retval #PSA_SUCCESS
1437 * The key slot is ready to receive key material.
1438 * \return If this function fails, the key slot is an invalid state.
1439 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001440 */
1441static psa_status_t psa_start_key_creation(
1442 const psa_key_attributes_t *attributes,
1443 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001444 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001445 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001446{
1447 psa_status_t status;
1448 psa_key_slot_t *slot;
1449
Gilles Peskine011e4282019-06-26 18:34:38 +02001450 *p_drv = NULL;
1451
Gilles Peskine267c6562019-05-27 19:01:54 +02001452 status = psa_internal_allocate_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001453 if( status != PSA_SUCCESS )
1454 return( status );
1455 slot = *p_slot;
1456
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001457 status = psa_set_key_policy_internal( slot, &attributes->core.policy );
Gilles Peskine4747d192019-04-17 15:05:45 +02001458 if( status != PSA_SUCCESS )
1459 return( status );
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001460 slot->lifetime = attributes->core.lifetime;
Gilles Peskine011e4282019-06-26 18:34:38 +02001461
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001462 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001463 {
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001464 status = psa_validate_persistent_key_parameters( attributes->core.lifetime,
1465 attributes->core.id,
Gilles Peskine011e4282019-06-26 18:34:38 +02001466 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001467 if( status != PSA_SUCCESS )
1468 return( status );
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001469 slot->persistent_storage_id = attributes->core.id;
Gilles Peskined167b942019-04-19 18:19:40 +02001470 }
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001471 slot->type = attributes->core.type;
Gilles Peskine4747d192019-04-17 15:05:45 +02001472
Gilles Peskinecbaff462019-07-12 23:46:04 +02001473#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine60450a42019-07-25 11:32:45 +02001474 /* For a key in a secure element, we need to do three things:
1475 * create the key file in internal storage, create the
1476 * key inside the secure element, and update the driver's
1477 * persistent data. Start a transaction that will encompass these
1478 * three actions. */
1479 /* The first thing to do is to find a slot number for the new key.
1480 * We save the slot number in persistent storage as part of the
1481 * transaction data. It will be needed to recover if the power
1482 * fails during the key creation process, to clean up on the secure
1483 * element side after restarting. Obtaining a slot number from the
1484 * secure element driver updates its persistent state, but we do not yet
1485 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001486 * we can roll back to a state where the key doesn't exist. */
Gilles Peskinecbaff462019-07-12 23:46:04 +02001487 if( *p_drv != NULL )
1488 {
1489 status = psa_find_se_slot_for_key( attributes, *p_drv,
1490 &slot->data.se.slot_number );
1491 if( status != PSA_SUCCESS )
1492 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001493 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1494 psa_crypto_transaction.key.lifetime = slot->lifetime;
1495 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1496 psa_crypto_transaction.key.id = slot->persistent_storage_id;
1497 status = psa_crypto_save_transaction( );
1498 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001499 {
1500 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001501 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001502 }
Gilles Peskine424f8942019-07-15 21:59:53 +02001503
1504 /* TOnogrepDO: validate bits. How to do this depends on the key
1505 * creation method, so setting bits might not belong here. */
1506 slot->data.se.bits = psa_get_key_bits( attributes );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001507 }
1508#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1509
Gilles Peskine4747d192019-04-17 15:05:45 +02001510 return( status );
1511}
1512
1513/** Finalize the creation of a key once its key material has been set.
1514 *
1515 * This entails writing the key to persistent storage.
1516 *
1517 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001518 * See the documentation of psa_start_key_creation() for the intended use
1519 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001520 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001521 * \param[in,out] slot Pointer to the slot with key material.
1522 * \param[in] driver The secure element driver for the key,
1523 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001524 *
1525 * \retval #PSA_SUCCESS
1526 * The key was successfully created. The handle is now valid.
1527 * \return If this function fails, the key slot is an invalid state.
1528 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001529 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001530static psa_status_t psa_finish_key_creation(
1531 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001532 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001533{
1534 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001535 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001536 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001537
1538#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1df83d42019-07-23 16:13:14 +02001539 if( slot->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001540 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001541 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1542 psa_get_key_slot_attributes( slot, &attributes );
Gilles Peskine4747d192019-04-17 15:05:45 +02001543
Gilles Peskine1df83d42019-07-23 16:13:14 +02001544#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1545 if( driver != NULL )
1546 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001547 status = psa_save_persistent_key( &attributes,
1548 (uint8_t*) &slot->data.se,
1549 sizeof( slot->data.se ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001550 }
1551 else
1552#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1553 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001554 size_t buffer_size =
1555 PSA_KEY_EXPORT_MAX_SIZE( slot->type,
1556 psa_get_key_bits( &attributes ) );
1557 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1558 size_t length = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001559 if( buffer == NULL && buffer_size != 0 )
1560 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1561 status = psa_internal_export_key( slot,
1562 buffer, buffer_size, &length,
1563 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001564 if( status == PSA_SUCCESS )
1565 status = psa_save_persistent_key( &attributes, buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001566
Gilles Peskine1df83d42019-07-23 16:13:14 +02001567 if( buffer_size != 0 )
1568 mbedtls_platform_zeroize( buffer, buffer_size );
1569 mbedtls_free( buffer );
1570 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001571 }
1572#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1573
Gilles Peskinecbaff462019-07-12 23:46:04 +02001574#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1575 if( driver != NULL )
1576 {
1577 status = psa_save_se_persistent_data( driver );
1578 if( status != PSA_SUCCESS )
1579 {
1580 psa_destroy_persistent_key( slot->persistent_storage_id );
1581 return( status );
1582 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001583 status = psa_crypto_stop_transaction( );
1584 if( status != PSA_SUCCESS )
1585 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001586 }
1587#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1588
Gilles Peskine4747d192019-04-17 15:05:45 +02001589 return( status );
1590}
1591
1592/** Abort the creation of a key.
1593 *
1594 * You may call this function after calling psa_start_key_creation(),
1595 * or after psa_finish_key_creation() fails. In other circumstances, this
1596 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001597 * See the documentation of psa_start_key_creation() for the intended use
1598 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001599 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001600 * \param[in,out] slot Pointer to the slot with key material.
1601 * \param[in] driver The secure element driver for the key,
1602 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001603 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001604static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001605 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001606{
Gilles Peskine011e4282019-06-26 18:34:38 +02001607 (void) driver;
1608
Gilles Peskine4747d192019-04-17 15:05:45 +02001609 if( slot == NULL )
1610 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001611
1612#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1613 /* TOnogrepDO: If the key has already been created in the secure
1614 * element, and the failure happened later (when saving metadata
1615 * to internal storage), we need to destroy the key in the secure
1616 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001617
1618 /* Abort the ongoing transaction if any. We already did what it
1619 * takes to undo any partial creation. All that's left is to update
1620 * the transaction data itself. */
1621 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001622#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1623
Gilles Peskine4747d192019-04-17 15:05:45 +02001624 psa_wipe_key_slot( slot );
1625}
1626
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001627static psa_status_t psa_check_key_slot_attributes(
1628 const psa_key_slot_t *slot,
1629 const psa_key_attributes_t *attributes )
1630{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001631 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001632 {
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001633 if( attributes->core.type != slot->type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001634 return( PSA_ERROR_INVALID_ARGUMENT );
1635 }
1636
1637 if( attributes->domain_parameters_size != 0 )
1638 {
1639#if defined(MBEDTLS_RSA_C)
1640 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1641 {
1642 mbedtls_mpi actual, required;
1643 int ret;
1644 mbedtls_mpi_init( &actual );
1645 mbedtls_mpi_init( &required );
1646 ret = mbedtls_rsa_export( slot->data.rsa,
1647 NULL, NULL, NULL, NULL, &actual );
1648 if( ret != 0 )
1649 goto rsa_exit;
1650 ret = mbedtls_mpi_read_binary( &required,
1651 attributes->domain_parameters,
1652 attributes->domain_parameters_size );
1653 if( ret != 0 )
1654 goto rsa_exit;
1655 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1656 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1657 rsa_exit:
1658 mbedtls_mpi_free( &actual );
1659 mbedtls_mpi_free( &required );
1660 if( ret != 0)
1661 return( mbedtls_to_psa_error( ret ) );
1662 }
1663 else
1664#endif
1665 {
1666 return( PSA_ERROR_INVALID_ARGUMENT );
1667 }
1668 }
1669
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001670 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001671 {
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001672 if( attributes->core.bits != psa_get_key_slot_bits( slot ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001673 return( PSA_ERROR_INVALID_ARGUMENT );
1674 }
1675
1676 return( PSA_SUCCESS );
1677}
1678
Gilles Peskine4747d192019-04-17 15:05:45 +02001679psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001680 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001681 size_t data_length,
1682 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001683{
1684 psa_status_t status;
1685 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001686 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001687
Gilles Peskine011e4282019-06-26 18:34:38 +02001688 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001689 if( status != PSA_SUCCESS )
1690 goto exit;
1691
Gilles Peskine5d309672019-07-12 23:47:28 +02001692#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1693 if( driver != NULL )
1694 {
1695 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
1696 if( drv->key_management == NULL ||
1697 drv->key_management->p_import == NULL )
1698 {
1699 status = PSA_ERROR_NOT_SUPPORTED;
1700 goto exit;
1701 }
1702 status = drv->key_management->p_import(
1703 psa_get_se_driver_context( driver ),
1704 slot->data.se.slot_number,
1705 slot->lifetime, slot->type, slot->policy.alg, slot->policy.usage,
Gilles Peskine18017402019-07-24 20:25:59 +02001706 data, data_length,
1707 &slot->data.se.bits );
Gilles Peskine5d309672019-07-12 23:47:28 +02001708 }
1709 else
1710#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1711 {
1712 status = psa_import_key_into_slot( slot, data, data_length );
1713 if( status != PSA_SUCCESS )
1714 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001715 }
Gilles Peskine18017402019-07-24 20:25:59 +02001716 status = psa_check_key_slot_attributes( slot, attributes );
1717 if( status != PSA_SUCCESS )
1718 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001719
Gilles Peskine011e4282019-06-26 18:34:38 +02001720 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001721exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001722 if( status != PSA_SUCCESS )
1723 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001724 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001725 *handle = 0;
1726 }
1727 return( status );
1728}
1729
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001730static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001731 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001732{
1733 psa_status_t status;
1734 uint8_t *buffer = NULL;
1735 size_t buffer_size = 0;
1736 size_t length;
1737
1738 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001739 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001740 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001741 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001742 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001743 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1744 if( status != PSA_SUCCESS )
1745 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001746 target->type = source->type;
1747 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001748
1749exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001750 if( buffer_size != 0 )
1751 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001752 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001753 return( status );
1754}
1755
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001756psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1757 const psa_key_attributes_t *specified_attributes,
1758 psa_key_handle_t *target_handle )
1759{
1760 psa_status_t status;
1761 psa_key_slot_t *source_slot = NULL;
1762 psa_key_slot_t *target_slot = NULL;
1763 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001764 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001765
Gilles Peskine28f8f302019-07-24 13:30:31 +02001766 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001767 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001768 if( status != PSA_SUCCESS )
1769 goto exit;
1770
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001771 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1772 if( status != PSA_SUCCESS )
1773 goto exit;
1774
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001775 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001776 &source_slot->policy );
1777 if( status != PSA_SUCCESS )
1778 goto exit;
1779
1780 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001781 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001782 if( status != PSA_SUCCESS )
1783 goto exit;
1784
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001785#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1786 if( driver != NULL )
1787 {
1788 /* Copying to a secure element is not implemented yet. */
1789 status = PSA_ERROR_NOT_SUPPORTED;
1790 goto exit;
1791 }
1792#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1793
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001794 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001795 if( status != PSA_SUCCESS )
1796 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001797
Gilles Peskine011e4282019-06-26 18:34:38 +02001798 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001799exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001800 if( status != PSA_SUCCESS )
1801 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001802 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001803 *target_handle = 0;
1804 }
1805 return( status );
1806}
1807
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001808
1809
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001810/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001811/* Message digests */
1812/****************************************************************/
1813
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001814static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001815{
1816 switch( alg )
1817 {
1818#if defined(MBEDTLS_MD2_C)
1819 case PSA_ALG_MD2:
1820 return( &mbedtls_md2_info );
1821#endif
1822#if defined(MBEDTLS_MD4_C)
1823 case PSA_ALG_MD4:
1824 return( &mbedtls_md4_info );
1825#endif
1826#if defined(MBEDTLS_MD5_C)
1827 case PSA_ALG_MD5:
1828 return( &mbedtls_md5_info );
1829#endif
1830#if defined(MBEDTLS_RIPEMD160_C)
1831 case PSA_ALG_RIPEMD160:
1832 return( &mbedtls_ripemd160_info );
1833#endif
1834#if defined(MBEDTLS_SHA1_C)
1835 case PSA_ALG_SHA_1:
1836 return( &mbedtls_sha1_info );
1837#endif
1838#if defined(MBEDTLS_SHA256_C)
1839 case PSA_ALG_SHA_224:
1840 return( &mbedtls_sha224_info );
1841 case PSA_ALG_SHA_256:
1842 return( &mbedtls_sha256_info );
1843#endif
1844#if defined(MBEDTLS_SHA512_C)
1845 case PSA_ALG_SHA_384:
1846 return( &mbedtls_sha384_info );
1847 case PSA_ALG_SHA_512:
1848 return( &mbedtls_sha512_info );
1849#endif
1850 default:
1851 return( NULL );
1852 }
1853}
1854
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001855psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1856{
1857 switch( operation->alg )
1858 {
Gilles Peskine81736312018-06-26 15:04:31 +02001859 case 0:
1860 /* The object has (apparently) been initialized but it is not
1861 * in use. It's ok to call abort on such an object, and there's
1862 * nothing to do. */
1863 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001864#if defined(MBEDTLS_MD2_C)
1865 case PSA_ALG_MD2:
1866 mbedtls_md2_free( &operation->ctx.md2 );
1867 break;
1868#endif
1869#if defined(MBEDTLS_MD4_C)
1870 case PSA_ALG_MD4:
1871 mbedtls_md4_free( &operation->ctx.md4 );
1872 break;
1873#endif
1874#if defined(MBEDTLS_MD5_C)
1875 case PSA_ALG_MD5:
1876 mbedtls_md5_free( &operation->ctx.md5 );
1877 break;
1878#endif
1879#if defined(MBEDTLS_RIPEMD160_C)
1880 case PSA_ALG_RIPEMD160:
1881 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1882 break;
1883#endif
1884#if defined(MBEDTLS_SHA1_C)
1885 case PSA_ALG_SHA_1:
1886 mbedtls_sha1_free( &operation->ctx.sha1 );
1887 break;
1888#endif
1889#if defined(MBEDTLS_SHA256_C)
1890 case PSA_ALG_SHA_224:
1891 case PSA_ALG_SHA_256:
1892 mbedtls_sha256_free( &operation->ctx.sha256 );
1893 break;
1894#endif
1895#if defined(MBEDTLS_SHA512_C)
1896 case PSA_ALG_SHA_384:
1897 case PSA_ALG_SHA_512:
1898 mbedtls_sha512_free( &operation->ctx.sha512 );
1899 break;
1900#endif
1901 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001902 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001903 }
1904 operation->alg = 0;
1905 return( PSA_SUCCESS );
1906}
1907
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001908psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001909 psa_algorithm_t alg )
1910{
1911 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001912
1913 /* A context must be freshly initialized before it can be set up. */
1914 if( operation->alg != 0 )
1915 {
1916 return( PSA_ERROR_BAD_STATE );
1917 }
1918
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001919 switch( alg )
1920 {
1921#if defined(MBEDTLS_MD2_C)
1922 case PSA_ALG_MD2:
1923 mbedtls_md2_init( &operation->ctx.md2 );
1924 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1925 break;
1926#endif
1927#if defined(MBEDTLS_MD4_C)
1928 case PSA_ALG_MD4:
1929 mbedtls_md4_init( &operation->ctx.md4 );
1930 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1931 break;
1932#endif
1933#if defined(MBEDTLS_MD5_C)
1934 case PSA_ALG_MD5:
1935 mbedtls_md5_init( &operation->ctx.md5 );
1936 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1937 break;
1938#endif
1939#if defined(MBEDTLS_RIPEMD160_C)
1940 case PSA_ALG_RIPEMD160:
1941 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1942 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1943 break;
1944#endif
1945#if defined(MBEDTLS_SHA1_C)
1946 case PSA_ALG_SHA_1:
1947 mbedtls_sha1_init( &operation->ctx.sha1 );
1948 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1949 break;
1950#endif
1951#if defined(MBEDTLS_SHA256_C)
1952 case PSA_ALG_SHA_224:
1953 mbedtls_sha256_init( &operation->ctx.sha256 );
1954 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1955 break;
1956 case PSA_ALG_SHA_256:
1957 mbedtls_sha256_init( &operation->ctx.sha256 );
1958 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1959 break;
1960#endif
1961#if defined(MBEDTLS_SHA512_C)
1962 case PSA_ALG_SHA_384:
1963 mbedtls_sha512_init( &operation->ctx.sha512 );
1964 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1965 break;
1966 case PSA_ALG_SHA_512:
1967 mbedtls_sha512_init( &operation->ctx.sha512 );
1968 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1969 break;
1970#endif
1971 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001972 return( PSA_ALG_IS_HASH( alg ) ?
1973 PSA_ERROR_NOT_SUPPORTED :
1974 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001975 }
1976 if( ret == 0 )
1977 operation->alg = alg;
1978 else
1979 psa_hash_abort( operation );
1980 return( mbedtls_to_psa_error( ret ) );
1981}
1982
1983psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1984 const uint8_t *input,
1985 size_t input_length )
1986{
1987 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001988
1989 /* Don't require hash implementations to behave correctly on a
1990 * zero-length input, which may have an invalid pointer. */
1991 if( input_length == 0 )
1992 return( PSA_SUCCESS );
1993
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001994 switch( operation->alg )
1995 {
1996#if defined(MBEDTLS_MD2_C)
1997 case PSA_ALG_MD2:
1998 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1999 input, input_length );
2000 break;
2001#endif
2002#if defined(MBEDTLS_MD4_C)
2003 case PSA_ALG_MD4:
2004 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2005 input, input_length );
2006 break;
2007#endif
2008#if defined(MBEDTLS_MD5_C)
2009 case PSA_ALG_MD5:
2010 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2011 input, input_length );
2012 break;
2013#endif
2014#if defined(MBEDTLS_RIPEMD160_C)
2015 case PSA_ALG_RIPEMD160:
2016 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2017 input, input_length );
2018 break;
2019#endif
2020#if defined(MBEDTLS_SHA1_C)
2021 case PSA_ALG_SHA_1:
2022 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2023 input, input_length );
2024 break;
2025#endif
2026#if defined(MBEDTLS_SHA256_C)
2027 case PSA_ALG_SHA_224:
2028 case PSA_ALG_SHA_256:
2029 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2030 input, input_length );
2031 break;
2032#endif
2033#if defined(MBEDTLS_SHA512_C)
2034 case PSA_ALG_SHA_384:
2035 case PSA_ALG_SHA_512:
2036 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2037 input, input_length );
2038 break;
2039#endif
2040 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002041 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002042 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002043
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002044 if( ret != 0 )
2045 psa_hash_abort( operation );
2046 return( mbedtls_to_psa_error( ret ) );
2047}
2048
2049psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2050 uint8_t *hash,
2051 size_t hash_size,
2052 size_t *hash_length )
2053{
itayzafrir40835d42018-08-02 13:14:17 +03002054 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002055 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002056 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002057
2058 /* Fill the output buffer with something that isn't a valid hash
2059 * (barring an attack on the hash and deliberately-crafted input),
2060 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002061 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002062 /* If hash_size is 0 then hash may be NULL and then the
2063 * call to memset would have undefined behavior. */
2064 if( hash_size != 0 )
2065 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002066
2067 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002068 {
2069 status = PSA_ERROR_BUFFER_TOO_SMALL;
2070 goto exit;
2071 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002072
2073 switch( operation->alg )
2074 {
2075#if defined(MBEDTLS_MD2_C)
2076 case PSA_ALG_MD2:
2077 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2078 break;
2079#endif
2080#if defined(MBEDTLS_MD4_C)
2081 case PSA_ALG_MD4:
2082 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2083 break;
2084#endif
2085#if defined(MBEDTLS_MD5_C)
2086 case PSA_ALG_MD5:
2087 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2088 break;
2089#endif
2090#if defined(MBEDTLS_RIPEMD160_C)
2091 case PSA_ALG_RIPEMD160:
2092 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2093 break;
2094#endif
2095#if defined(MBEDTLS_SHA1_C)
2096 case PSA_ALG_SHA_1:
2097 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2098 break;
2099#endif
2100#if defined(MBEDTLS_SHA256_C)
2101 case PSA_ALG_SHA_224:
2102 case PSA_ALG_SHA_256:
2103 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2104 break;
2105#endif
2106#if defined(MBEDTLS_SHA512_C)
2107 case PSA_ALG_SHA_384:
2108 case PSA_ALG_SHA_512:
2109 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2110 break;
2111#endif
2112 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002113 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002114 }
itayzafrir40835d42018-08-02 13:14:17 +03002115 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002116
itayzafrir40835d42018-08-02 13:14:17 +03002117exit:
2118 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002119 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002120 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002121 return( psa_hash_abort( operation ) );
2122 }
2123 else
2124 {
2125 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002126 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002127 }
2128}
2129
Gilles Peskine2d277862018-06-18 15:41:12 +02002130psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2131 const uint8_t *hash,
2132 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002133{
2134 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2135 size_t actual_hash_length;
2136 psa_status_t status = psa_hash_finish( operation,
2137 actual_hash, sizeof( actual_hash ),
2138 &actual_hash_length );
2139 if( status != PSA_SUCCESS )
2140 return( status );
2141 if( actual_hash_length != hash_length )
2142 return( PSA_ERROR_INVALID_SIGNATURE );
2143 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2144 return( PSA_ERROR_INVALID_SIGNATURE );
2145 return( PSA_SUCCESS );
2146}
2147
Gilles Peskineeb35d782019-01-22 17:56:16 +01002148psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2149 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002150{
2151 if( target_operation->alg != 0 )
2152 return( PSA_ERROR_BAD_STATE );
2153
2154 switch( source_operation->alg )
2155 {
2156 case 0:
2157 return( PSA_ERROR_BAD_STATE );
2158#if defined(MBEDTLS_MD2_C)
2159 case PSA_ALG_MD2:
2160 mbedtls_md2_clone( &target_operation->ctx.md2,
2161 &source_operation->ctx.md2 );
2162 break;
2163#endif
2164#if defined(MBEDTLS_MD4_C)
2165 case PSA_ALG_MD4:
2166 mbedtls_md4_clone( &target_operation->ctx.md4,
2167 &source_operation->ctx.md4 );
2168 break;
2169#endif
2170#if defined(MBEDTLS_MD5_C)
2171 case PSA_ALG_MD5:
2172 mbedtls_md5_clone( &target_operation->ctx.md5,
2173 &source_operation->ctx.md5 );
2174 break;
2175#endif
2176#if defined(MBEDTLS_RIPEMD160_C)
2177 case PSA_ALG_RIPEMD160:
2178 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2179 &source_operation->ctx.ripemd160 );
2180 break;
2181#endif
2182#if defined(MBEDTLS_SHA1_C)
2183 case PSA_ALG_SHA_1:
2184 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2185 &source_operation->ctx.sha1 );
2186 break;
2187#endif
2188#if defined(MBEDTLS_SHA256_C)
2189 case PSA_ALG_SHA_224:
2190 case PSA_ALG_SHA_256:
2191 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2192 &source_operation->ctx.sha256 );
2193 break;
2194#endif
2195#if defined(MBEDTLS_SHA512_C)
2196 case PSA_ALG_SHA_384:
2197 case PSA_ALG_SHA_512:
2198 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2199 &source_operation->ctx.sha512 );
2200 break;
2201#endif
2202 default:
2203 return( PSA_ERROR_NOT_SUPPORTED );
2204 }
2205
2206 target_operation->alg = source_operation->alg;
2207 return( PSA_SUCCESS );
2208}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002209
2210
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002211/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002212/* MAC */
2213/****************************************************************/
2214
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002215static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002216 psa_algorithm_t alg,
2217 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002218 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002219 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002220{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002221 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002222 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002223
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002224 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002225 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002226
Gilles Peskine8c9def32018-02-08 10:02:12 +01002227 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2228 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002229 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002230 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002231 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002232 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002233 mode = MBEDTLS_MODE_STREAM;
2234 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002235 case PSA_ALG_CTR:
2236 mode = MBEDTLS_MODE_CTR;
2237 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002238 case PSA_ALG_CFB:
2239 mode = MBEDTLS_MODE_CFB;
2240 break;
2241 case PSA_ALG_OFB:
2242 mode = MBEDTLS_MODE_OFB;
2243 break;
2244 case PSA_ALG_CBC_NO_PADDING:
2245 mode = MBEDTLS_MODE_CBC;
2246 break;
2247 case PSA_ALG_CBC_PKCS7:
2248 mode = MBEDTLS_MODE_CBC;
2249 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002250 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002251 mode = MBEDTLS_MODE_CCM;
2252 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002253 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002254 mode = MBEDTLS_MODE_GCM;
2255 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002256 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2257 mode = MBEDTLS_MODE_CHACHAPOLY;
2258 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002259 default:
2260 return( NULL );
2261 }
2262 }
2263 else if( alg == PSA_ALG_CMAC )
2264 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002265 else
2266 return( NULL );
2267
2268 switch( key_type )
2269 {
2270 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002271 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002272 break;
2273 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002274 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2275 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002276 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002277 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002278 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002279 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002280 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2281 * but two-key Triple-DES is functionally three-key Triple-DES
2282 * with K1=K3, so that's how we present it to mbedtls. */
2283 if( key_bits == 128 )
2284 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002285 break;
2286 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002287 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002288 break;
2289 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002290 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002291 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002292 case PSA_KEY_TYPE_CHACHA20:
2293 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2294 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002295 default:
2296 return( NULL );
2297 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002298 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002299 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002300
Jaeden Amero23bbb752018-06-26 14:16:54 +01002301 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2302 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002303}
2304
Gilles Peskinea05219c2018-11-16 16:02:56 +01002305#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002306static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002307{
Gilles Peskine2d277862018-06-18 15:41:12 +02002308 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002309 {
2310 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002311 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002312 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002313 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002314 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002315 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002316 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002317 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002318 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002319 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002320 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002321 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002322 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002323 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002324 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002325 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002326 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002327 return( 128 );
2328 default:
2329 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002330 }
2331}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002332#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002333
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002334/* Initialize the MAC operation structure. Once this function has been
2335 * called, psa_mac_abort can run and will do the right thing. */
2336static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2337 psa_algorithm_t alg )
2338{
2339 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2340
2341 operation->alg = alg;
2342 operation->key_set = 0;
2343 operation->iv_set = 0;
2344 operation->iv_required = 0;
2345 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002346 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002347
2348#if defined(MBEDTLS_CMAC_C)
2349 if( alg == PSA_ALG_CMAC )
2350 {
2351 operation->iv_required = 0;
2352 mbedtls_cipher_init( &operation->ctx.cmac );
2353 status = PSA_SUCCESS;
2354 }
2355 else
2356#endif /* MBEDTLS_CMAC_C */
2357#if defined(MBEDTLS_MD_C)
2358 if( PSA_ALG_IS_HMAC( operation->alg ) )
2359 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002360 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2361 operation->ctx.hmac.hash_ctx.alg = 0;
2362 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002363 }
2364 else
2365#endif /* MBEDTLS_MD_C */
2366 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002367 if( ! PSA_ALG_IS_MAC( alg ) )
2368 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002369 }
2370
2371 if( status != PSA_SUCCESS )
2372 memset( operation, 0, sizeof( *operation ) );
2373 return( status );
2374}
2375
Gilles Peskine01126fa2018-07-12 17:04:55 +02002376#if defined(MBEDTLS_MD_C)
2377static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2378{
Gilles Peskine3f108122018-12-07 18:14:53 +01002379 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002380 return( psa_hash_abort( &hmac->hash_ctx ) );
2381}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002382
Janos Follath999f6482019-06-11 12:04:10 +01002383#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002384static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2385{
2386 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2387 memset( hmac, 0, sizeof( *hmac ) );
2388}
Janos Follath999f6482019-06-11 12:04:10 +01002389#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002390#endif /* MBEDTLS_MD_C */
2391
Gilles Peskine8c9def32018-02-08 10:02:12 +01002392psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2393{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002394 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002395 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002396 /* The object has (apparently) been initialized but it is not
2397 * in use. It's ok to call abort on such an object, and there's
2398 * nothing to do. */
2399 return( PSA_SUCCESS );
2400 }
2401 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002402#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002403 if( operation->alg == PSA_ALG_CMAC )
2404 {
2405 mbedtls_cipher_free( &operation->ctx.cmac );
2406 }
2407 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002408#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002409#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002410 if( PSA_ALG_IS_HMAC( operation->alg ) )
2411 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002412 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002413 }
2414 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002415#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002416 {
2417 /* Sanity check (shouldn't happen: operation->alg should
2418 * always have been initialized to a valid value). */
2419 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002420 }
Moran Peker41deec42018-04-04 15:43:05 +03002421
Gilles Peskine8c9def32018-02-08 10:02:12 +01002422 operation->alg = 0;
2423 operation->key_set = 0;
2424 operation->iv_set = 0;
2425 operation->iv_required = 0;
2426 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002427 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002428
Gilles Peskine8c9def32018-02-08 10:02:12 +01002429 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002430
2431bad_state:
2432 /* If abort is called on an uninitialized object, we can't trust
2433 * anything. Wipe the object in case it contains confidential data.
2434 * This may result in a memory leak if a pointer gets overwritten,
2435 * but it's too late to do anything about this. */
2436 memset( operation, 0, sizeof( *operation ) );
2437 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002438}
2439
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002440#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002441static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002442 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002443 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002444 const mbedtls_cipher_info_t *cipher_info )
2445{
2446 int ret;
2447
2448 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002449
2450 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2451 if( ret != 0 )
2452 return( ret );
2453
2454 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2455 slot->data.raw.data,
2456 key_bits );
2457 return( ret );
2458}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002459#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002460
Gilles Peskine248051a2018-06-20 16:09:38 +02002461#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002462static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2463 const uint8_t *key,
2464 size_t key_length,
2465 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002466{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002467 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002468 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002469 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002470 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002471 psa_status_t status;
2472
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002473 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2474 * overflow below. This should never trigger if the hash algorithm
2475 * is implemented correctly. */
2476 /* The size checks against the ipad and opad buffers cannot be written
2477 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2478 * because that triggers -Wlogical-op on GCC 7.3. */
2479 if( block_size > sizeof( ipad ) )
2480 return( PSA_ERROR_NOT_SUPPORTED );
2481 if( block_size > sizeof( hmac->opad ) )
2482 return( PSA_ERROR_NOT_SUPPORTED );
2483 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002484 return( PSA_ERROR_NOT_SUPPORTED );
2485
Gilles Peskined223b522018-06-11 18:12:58 +02002486 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002487 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002488 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002489 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002490 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002491 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002492 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002493 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002494 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002495 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002496 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002497 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002498 }
Gilles Peskine96889972018-07-12 17:07:03 +02002499 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2500 * but it is permitted. It is common when HMAC is used in HKDF, for
2501 * example. Don't call `memcpy` in the 0-length because `key` could be
2502 * an invalid pointer which would make the behavior undefined. */
2503 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002504 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002505
Gilles Peskined223b522018-06-11 18:12:58 +02002506 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2507 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002508 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002509 ipad[i] ^= 0x36;
2510 memset( ipad + key_length, 0x36, block_size - key_length );
2511
2512 /* Copy the key material from ipad to opad, flipping the requisite bits,
2513 * and filling the rest of opad with the requisite constant. */
2514 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002515 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2516 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002517
Gilles Peskine01126fa2018-07-12 17:04:55 +02002518 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002519 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002520 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002521
Gilles Peskine01126fa2018-07-12 17:04:55 +02002522 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002523
2524cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002525 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002526
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002527 return( status );
2528}
Gilles Peskine248051a2018-06-20 16:09:38 +02002529#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002530
Gilles Peskine89167cb2018-07-08 20:12:23 +02002531static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002532 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002533 psa_algorithm_t alg,
2534 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002535{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002536 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002537 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002538 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002539 psa_key_usage_t usage =
2540 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002541 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002542 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002543
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002544 /* A context must be freshly initialized before it can be set up. */
2545 if( operation->alg != 0 )
2546 {
2547 return( PSA_ERROR_BAD_STATE );
2548 }
2549
Gilles Peskined911eb72018-08-14 15:18:45 +02002550 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002551 if( status != PSA_SUCCESS )
2552 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002553 if( is_sign )
2554 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002555
Gilles Peskine28f8f302019-07-24 13:30:31 +02002556 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002557 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002558 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002559 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002560
Gilles Peskine8c9def32018-02-08 10:02:12 +01002561#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002562 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002563 {
2564 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002565 mbedtls_cipher_info_from_psa( full_length_alg,
2566 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002567 int ret;
2568 if( cipher_info == NULL )
2569 {
2570 status = PSA_ERROR_NOT_SUPPORTED;
2571 goto exit;
2572 }
2573 operation->mac_size = cipher_info->block_size;
2574 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2575 status = mbedtls_to_psa_error( ret );
2576 }
2577 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002578#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002579#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002580 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002581 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002582 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002583 if( hash_alg == 0 )
2584 {
2585 status = PSA_ERROR_NOT_SUPPORTED;
2586 goto exit;
2587 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002588
2589 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2590 /* Sanity check. This shouldn't fail on a valid configuration. */
2591 if( operation->mac_size == 0 ||
2592 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2593 {
2594 status = PSA_ERROR_NOT_SUPPORTED;
2595 goto exit;
2596 }
2597
Gilles Peskine01126fa2018-07-12 17:04:55 +02002598 if( slot->type != PSA_KEY_TYPE_HMAC )
2599 {
2600 status = PSA_ERROR_INVALID_ARGUMENT;
2601 goto exit;
2602 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002603
Gilles Peskine01126fa2018-07-12 17:04:55 +02002604 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2605 slot->data.raw.data,
2606 slot->data.raw.bytes,
2607 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002608 }
2609 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002610#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002611 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002612 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002613 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002614 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002615
Gilles Peskined911eb72018-08-14 15:18:45 +02002616 if( truncated == 0 )
2617 {
2618 /* The "normal" case: untruncated algorithm. Nothing to do. */
2619 }
2620 else if( truncated < 4 )
2621 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002622 /* A very short MAC is too short for security since it can be
2623 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2624 * so we make this our minimum, even though 32 bits is still
2625 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002626 status = PSA_ERROR_NOT_SUPPORTED;
2627 }
2628 else if( truncated > operation->mac_size )
2629 {
2630 /* It's impossible to "truncate" to a larger length. */
2631 status = PSA_ERROR_INVALID_ARGUMENT;
2632 }
2633 else
2634 operation->mac_size = truncated;
2635
Gilles Peskinefbfac682018-07-08 20:51:54 +02002636exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002637 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002638 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002639 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002640 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002641 else
2642 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002643 operation->key_set = 1;
2644 }
2645 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002646}
2647
Gilles Peskine89167cb2018-07-08 20:12:23 +02002648psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002649 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002650 psa_algorithm_t alg )
2651{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002652 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002653}
2654
2655psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002656 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002657 psa_algorithm_t alg )
2658{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002659 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002660}
2661
Gilles Peskine8c9def32018-02-08 10:02:12 +01002662psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2663 const uint8_t *input,
2664 size_t input_length )
2665{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002666 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002667 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002668 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002669 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002670 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002671 operation->has_input = 1;
2672
Gilles Peskine8c9def32018-02-08 10:02:12 +01002673#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002674 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002675 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002676 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2677 input, input_length );
2678 status = mbedtls_to_psa_error( ret );
2679 }
2680 else
2681#endif /* MBEDTLS_CMAC_C */
2682#if defined(MBEDTLS_MD_C)
2683 if( PSA_ALG_IS_HMAC( operation->alg ) )
2684 {
2685 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2686 input_length );
2687 }
2688 else
2689#endif /* MBEDTLS_MD_C */
2690 {
2691 /* This shouldn't happen if `operation` was initialized by
2692 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002693 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002694 }
2695
Gilles Peskinefbfac682018-07-08 20:51:54 +02002696 if( status != PSA_SUCCESS )
2697 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002698 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002699}
2700
Gilles Peskine01126fa2018-07-12 17:04:55 +02002701#if defined(MBEDTLS_MD_C)
2702static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2703 uint8_t *mac,
2704 size_t mac_size )
2705{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002706 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002707 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2708 size_t hash_size = 0;
2709 size_t block_size = psa_get_hash_block_size( hash_alg );
2710 psa_status_t status;
2711
Gilles Peskine01126fa2018-07-12 17:04:55 +02002712 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2713 if( status != PSA_SUCCESS )
2714 return( status );
2715 /* From here on, tmp needs to be wiped. */
2716
2717 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2718 if( status != PSA_SUCCESS )
2719 goto exit;
2720
2721 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2722 if( status != PSA_SUCCESS )
2723 goto exit;
2724
2725 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2726 if( status != PSA_SUCCESS )
2727 goto exit;
2728
Gilles Peskined911eb72018-08-14 15:18:45 +02002729 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2730 if( status != PSA_SUCCESS )
2731 goto exit;
2732
2733 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002734
2735exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002736 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002737 return( status );
2738}
2739#endif /* MBEDTLS_MD_C */
2740
mohammad16036df908f2018-04-02 08:34:15 -07002741static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002742 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002743 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002744{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002745 if( ! operation->key_set )
2746 return( PSA_ERROR_BAD_STATE );
2747 if( operation->iv_required && ! operation->iv_set )
2748 return( PSA_ERROR_BAD_STATE );
2749
Gilles Peskine8c9def32018-02-08 10:02:12 +01002750 if( mac_size < operation->mac_size )
2751 return( PSA_ERROR_BUFFER_TOO_SMALL );
2752
Gilles Peskine8c9def32018-02-08 10:02:12 +01002753#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002754 if( operation->alg == PSA_ALG_CMAC )
2755 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002756 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2757 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2758 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002759 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002760 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002761 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002762 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002763 else
2764#endif /* MBEDTLS_CMAC_C */
2765#if defined(MBEDTLS_MD_C)
2766 if( PSA_ALG_IS_HMAC( operation->alg ) )
2767 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002768 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002769 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002770 }
2771 else
2772#endif /* MBEDTLS_MD_C */
2773 {
2774 /* This shouldn't happen if `operation` was initialized by
2775 * a setup function. */
2776 return( PSA_ERROR_BAD_STATE );
2777 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002778}
2779
Gilles Peskineacd4be32018-07-08 19:56:25 +02002780psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2781 uint8_t *mac,
2782 size_t mac_size,
2783 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002784{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002785 psa_status_t status;
2786
Jaeden Amero252ef282019-02-15 14:05:35 +00002787 if( operation->alg == 0 )
2788 {
2789 return( PSA_ERROR_BAD_STATE );
2790 }
2791
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002792 /* Fill the output buffer with something that isn't a valid mac
2793 * (barring an attack on the mac and deliberately-crafted input),
2794 * in case the caller doesn't check the return status properly. */
2795 *mac_length = mac_size;
2796 /* If mac_size is 0 then mac may be NULL and then the
2797 * call to memset would have undefined behavior. */
2798 if( mac_size != 0 )
2799 memset( mac, '!', mac_size );
2800
Gilles Peskine89167cb2018-07-08 20:12:23 +02002801 if( ! operation->is_sign )
2802 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002803 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002804 }
mohammad16036df908f2018-04-02 08:34:15 -07002805
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002806 status = psa_mac_finish_internal( operation, mac, mac_size );
2807
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002808 if( status == PSA_SUCCESS )
2809 {
2810 status = psa_mac_abort( operation );
2811 if( status == PSA_SUCCESS )
2812 *mac_length = operation->mac_size;
2813 else
2814 memset( mac, '!', mac_size );
2815 }
2816 else
2817 psa_mac_abort( operation );
2818 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002819}
2820
Gilles Peskineacd4be32018-07-08 19:56:25 +02002821psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2822 const uint8_t *mac,
2823 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002824{
Gilles Peskine828ed142018-06-18 23:25:51 +02002825 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002826 psa_status_t status;
2827
Jaeden Amero252ef282019-02-15 14:05:35 +00002828 if( operation->alg == 0 )
2829 {
2830 return( PSA_ERROR_BAD_STATE );
2831 }
2832
Gilles Peskine89167cb2018-07-08 20:12:23 +02002833 if( operation->is_sign )
2834 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002835 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002836 }
2837 if( operation->mac_size != mac_length )
2838 {
2839 status = PSA_ERROR_INVALID_SIGNATURE;
2840 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002841 }
mohammad16036df908f2018-04-02 08:34:15 -07002842
2843 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002844 actual_mac, sizeof( actual_mac ) );
2845
2846 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2847 status = PSA_ERROR_INVALID_SIGNATURE;
2848
2849cleanup:
2850 if( status == PSA_SUCCESS )
2851 status = psa_mac_abort( operation );
2852 else
2853 psa_mac_abort( operation );
2854
Gilles Peskine3f108122018-12-07 18:14:53 +01002855 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002856
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002857 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002858}
2859
2860
Gilles Peskine20035e32018-02-03 22:44:14 +01002861
Gilles Peskine20035e32018-02-03 22:44:14 +01002862/****************************************************************/
2863/* Asymmetric cryptography */
2864/****************************************************************/
2865
Gilles Peskine2b450e32018-06-27 15:42:46 +02002866#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002867/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002868 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002869static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2870 size_t hash_length,
2871 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002872{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002873 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002874 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002875 *md_alg = mbedtls_md_get_type( md_info );
2876
2877 /* The Mbed TLS RSA module uses an unsigned int for hash length
2878 * parameters. Validate that it fits so that we don't risk an
2879 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002880#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002881 if( hash_length > UINT_MAX )
2882 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002883#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002884
2885#if defined(MBEDTLS_PKCS1_V15)
2886 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2887 * must be correct. */
2888 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2889 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002890 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002891 if( md_info == NULL )
2892 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002893 if( mbedtls_md_get_size( md_info ) != hash_length )
2894 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002895 }
2896#endif /* MBEDTLS_PKCS1_V15 */
2897
2898#if defined(MBEDTLS_PKCS1_V21)
2899 /* PSS requires a hash internally. */
2900 if( PSA_ALG_IS_RSA_PSS( alg ) )
2901 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002902 if( md_info == NULL )
2903 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002904 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002905#endif /* MBEDTLS_PKCS1_V21 */
2906
Gilles Peskine61b91d42018-06-08 16:09:36 +02002907 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002908}
2909
Gilles Peskine2b450e32018-06-27 15:42:46 +02002910static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2911 psa_algorithm_t alg,
2912 const uint8_t *hash,
2913 size_t hash_length,
2914 uint8_t *signature,
2915 size_t signature_size,
2916 size_t *signature_length )
2917{
2918 psa_status_t status;
2919 int ret;
2920 mbedtls_md_type_t md_alg;
2921
2922 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2923 if( status != PSA_SUCCESS )
2924 return( status );
2925
Gilles Peskine630a18a2018-06-29 17:49:35 +02002926 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002927 return( PSA_ERROR_BUFFER_TOO_SMALL );
2928
2929#if defined(MBEDTLS_PKCS1_V15)
2930 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2931 {
2932 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2933 MBEDTLS_MD_NONE );
2934 ret = mbedtls_rsa_pkcs1_sign( rsa,
2935 mbedtls_ctr_drbg_random,
2936 &global_data.ctr_drbg,
2937 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002938 md_alg,
2939 (unsigned int) hash_length,
2940 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002941 signature );
2942 }
2943 else
2944#endif /* MBEDTLS_PKCS1_V15 */
2945#if defined(MBEDTLS_PKCS1_V21)
2946 if( PSA_ALG_IS_RSA_PSS( alg ) )
2947 {
2948 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2949 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2950 mbedtls_ctr_drbg_random,
2951 &global_data.ctr_drbg,
2952 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002953 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002954 (unsigned int) hash_length,
2955 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002956 signature );
2957 }
2958 else
2959#endif /* MBEDTLS_PKCS1_V21 */
2960 {
2961 return( PSA_ERROR_INVALID_ARGUMENT );
2962 }
2963
2964 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002965 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002966 return( mbedtls_to_psa_error( ret ) );
2967}
2968
2969static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2970 psa_algorithm_t alg,
2971 const uint8_t *hash,
2972 size_t hash_length,
2973 const uint8_t *signature,
2974 size_t signature_length )
2975{
2976 psa_status_t status;
2977 int ret;
2978 mbedtls_md_type_t md_alg;
2979
2980 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2981 if( status != PSA_SUCCESS )
2982 return( status );
2983
Gilles Peskine630a18a2018-06-29 17:49:35 +02002984 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002985 return( PSA_ERROR_BUFFER_TOO_SMALL );
2986
2987#if defined(MBEDTLS_PKCS1_V15)
2988 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2989 {
2990 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2991 MBEDTLS_MD_NONE );
2992 ret = mbedtls_rsa_pkcs1_verify( rsa,
2993 mbedtls_ctr_drbg_random,
2994 &global_data.ctr_drbg,
2995 MBEDTLS_RSA_PUBLIC,
2996 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002997 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002998 hash,
2999 signature );
3000 }
3001 else
3002#endif /* MBEDTLS_PKCS1_V15 */
3003#if defined(MBEDTLS_PKCS1_V21)
3004 if( PSA_ALG_IS_RSA_PSS( alg ) )
3005 {
3006 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3007 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3008 mbedtls_ctr_drbg_random,
3009 &global_data.ctr_drbg,
3010 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003011 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003012 (unsigned int) hash_length,
3013 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003014 signature );
3015 }
3016 else
3017#endif /* MBEDTLS_PKCS1_V21 */
3018 {
3019 return( PSA_ERROR_INVALID_ARGUMENT );
3020 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003021
3022 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3023 * the rest of the signature is invalid". This has little use in
3024 * practice and PSA doesn't report this distinction. */
3025 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3026 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003027 return( mbedtls_to_psa_error( ret ) );
3028}
3029#endif /* MBEDTLS_RSA_C */
3030
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003031#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003032/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3033 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3034 * (even though these functions don't modify it). */
3035static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3036 psa_algorithm_t alg,
3037 const uint8_t *hash,
3038 size_t hash_length,
3039 uint8_t *signature,
3040 size_t signature_size,
3041 size_t *signature_length )
3042{
3043 int ret;
3044 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003045 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003046 mbedtls_mpi_init( &r );
3047 mbedtls_mpi_init( &s );
3048
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003049 if( signature_size < 2 * curve_bytes )
3050 {
3051 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3052 goto cleanup;
3053 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003054
Gilles Peskinea05219c2018-11-16 16:02:56 +01003055#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003056 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3057 {
3058 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3059 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3060 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3061 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3062 hash, hash_length,
3063 md_alg ) );
3064 }
3065 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003066#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003067 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003068 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003069 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3070 hash, hash_length,
3071 mbedtls_ctr_drbg_random,
3072 &global_data.ctr_drbg ) );
3073 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003074
3075 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3076 signature,
3077 curve_bytes ) );
3078 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3079 signature + curve_bytes,
3080 curve_bytes ) );
3081
3082cleanup:
3083 mbedtls_mpi_free( &r );
3084 mbedtls_mpi_free( &s );
3085 if( ret == 0 )
3086 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003087 return( mbedtls_to_psa_error( ret ) );
3088}
3089
3090static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3091 const uint8_t *hash,
3092 size_t hash_length,
3093 const uint8_t *signature,
3094 size_t signature_length )
3095{
3096 int ret;
3097 mbedtls_mpi r, s;
3098 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3099 mbedtls_mpi_init( &r );
3100 mbedtls_mpi_init( &s );
3101
3102 if( signature_length != 2 * curve_bytes )
3103 return( PSA_ERROR_INVALID_SIGNATURE );
3104
3105 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3106 signature,
3107 curve_bytes ) );
3108 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3109 signature + curve_bytes,
3110 curve_bytes ) );
3111
3112 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3113 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003114
3115cleanup:
3116 mbedtls_mpi_free( &r );
3117 mbedtls_mpi_free( &s );
3118 return( mbedtls_to_psa_error( ret ) );
3119}
3120#endif /* MBEDTLS_ECDSA_C */
3121
Gilles Peskinec5487a82018-12-03 18:08:14 +01003122psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003123 psa_algorithm_t alg,
3124 const uint8_t *hash,
3125 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003126 uint8_t *signature,
3127 size_t signature_size,
3128 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003129{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003130 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003131 psa_status_t status;
3132
3133 *signature_length = signature_size;
3134
Gilles Peskine28f8f302019-07-24 13:30:31 +02003135 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003136 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003137 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003138 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003139 {
3140 status = PSA_ERROR_INVALID_ARGUMENT;
3141 goto exit;
3142 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003143
Gilles Peskine20035e32018-02-03 22:44:14 +01003144#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003145 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003146 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003147 status = psa_rsa_sign( slot->data.rsa,
3148 alg,
3149 hash, hash_length,
3150 signature, signature_size,
3151 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003152 }
3153 else
3154#endif /* defined(MBEDTLS_RSA_C) */
3155#if defined(MBEDTLS_ECP_C)
3156 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3157 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003158#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003159 if(
3160#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3161 PSA_ALG_IS_ECDSA( alg )
3162#else
3163 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3164#endif
3165 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003166 status = psa_ecdsa_sign( slot->data.ecp,
3167 alg,
3168 hash, hash_length,
3169 signature, signature_size,
3170 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003171 else
3172#endif /* defined(MBEDTLS_ECDSA_C) */
3173 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003174 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003175 }
itayzafrir5c753392018-05-08 11:18:38 +03003176 }
3177 else
3178#endif /* defined(MBEDTLS_ECP_C) */
3179 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003180 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003181 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003182
3183exit:
3184 /* Fill the unused part of the output buffer (the whole buffer on error,
3185 * the trailing part on success) with something that isn't a valid mac
3186 * (barring an attack on the mac and deliberately-crafted input),
3187 * in case the caller doesn't check the return status properly. */
3188 if( status == PSA_SUCCESS )
3189 memset( signature + *signature_length, '!',
3190 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003191 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003192 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003193 /* If signature_size is 0 then we have nothing to do. We must not call
3194 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003195 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003196}
3197
Gilles Peskinec5487a82018-12-03 18:08:14 +01003198psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003199 psa_algorithm_t alg,
3200 const uint8_t *hash,
3201 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003202 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003203 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003204{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003205 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003206 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003207
Gilles Peskine28f8f302019-07-24 13:30:31 +02003208 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003209 if( status != PSA_SUCCESS )
3210 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003211
Gilles Peskine61b91d42018-06-08 16:09:36 +02003212#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003213 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003214 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003215 return( psa_rsa_verify( slot->data.rsa,
3216 alg,
3217 hash, hash_length,
3218 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003219 }
3220 else
3221#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003222#if defined(MBEDTLS_ECP_C)
3223 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3224 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003225#if defined(MBEDTLS_ECDSA_C)
3226 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003227 return( psa_ecdsa_verify( slot->data.ecp,
3228 hash, hash_length,
3229 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003230 else
3231#endif /* defined(MBEDTLS_ECDSA_C) */
3232 {
3233 return( PSA_ERROR_INVALID_ARGUMENT );
3234 }
itayzafrir5c753392018-05-08 11:18:38 +03003235 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003236 else
3237#endif /* defined(MBEDTLS_ECP_C) */
3238 {
3239 return( PSA_ERROR_NOT_SUPPORTED );
3240 }
3241}
3242
Gilles Peskine072ac562018-06-30 00:21:29 +02003243#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3244static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3245 mbedtls_rsa_context *rsa )
3246{
3247 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3248 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3249 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3250 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3251}
3252#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3253
Gilles Peskinec5487a82018-12-03 18:08:14 +01003254psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003255 psa_algorithm_t alg,
3256 const uint8_t *input,
3257 size_t input_length,
3258 const uint8_t *salt,
3259 size_t salt_length,
3260 uint8_t *output,
3261 size_t output_size,
3262 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003263{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003264 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003265 psa_status_t status;
3266
Darryl Green5cc689a2018-07-24 15:34:10 +01003267 (void) input;
3268 (void) input_length;
3269 (void) salt;
3270 (void) output;
3271 (void) output_size;
3272
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003273 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003274
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003275 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3276 return( PSA_ERROR_INVALID_ARGUMENT );
3277
Gilles Peskine28f8f302019-07-24 13:30:31 +02003278 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003279 if( status != PSA_SUCCESS )
3280 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003281 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003282 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003283 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003284
3285#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003286 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003287 {
3288 mbedtls_rsa_context *rsa = slot->data.rsa;
3289 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003290 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003291 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003292#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003293 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003294 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003295 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3296 mbedtls_ctr_drbg_random,
3297 &global_data.ctr_drbg,
3298 MBEDTLS_RSA_PUBLIC,
3299 input_length,
3300 input,
3301 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003302 }
3303 else
3304#endif /* MBEDTLS_PKCS1_V15 */
3305#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003306 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003307 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003308 psa_rsa_oaep_set_padding_mode( alg, rsa );
3309 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3310 mbedtls_ctr_drbg_random,
3311 &global_data.ctr_drbg,
3312 MBEDTLS_RSA_PUBLIC,
3313 salt, salt_length,
3314 input_length,
3315 input,
3316 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003317 }
3318 else
3319#endif /* MBEDTLS_PKCS1_V21 */
3320 {
3321 return( PSA_ERROR_INVALID_ARGUMENT );
3322 }
3323 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003324 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003325 return( mbedtls_to_psa_error( ret ) );
3326 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003327 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003328#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003329 {
3330 return( PSA_ERROR_NOT_SUPPORTED );
3331 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003332}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003333
Gilles Peskinec5487a82018-12-03 18:08:14 +01003334psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003335 psa_algorithm_t alg,
3336 const uint8_t *input,
3337 size_t input_length,
3338 const uint8_t *salt,
3339 size_t salt_length,
3340 uint8_t *output,
3341 size_t output_size,
3342 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003343{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003344 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003345 psa_status_t status;
3346
Darryl Green5cc689a2018-07-24 15:34:10 +01003347 (void) input;
3348 (void) input_length;
3349 (void) salt;
3350 (void) output;
3351 (void) output_size;
3352
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003353 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003354
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003355 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3356 return( PSA_ERROR_INVALID_ARGUMENT );
3357
Gilles Peskine28f8f302019-07-24 13:30:31 +02003358 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003359 if( status != PSA_SUCCESS )
3360 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003361 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003362 return( PSA_ERROR_INVALID_ARGUMENT );
3363
3364#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003365 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003366 {
3367 mbedtls_rsa_context *rsa = slot->data.rsa;
3368 int ret;
3369
Gilles Peskine630a18a2018-06-29 17:49:35 +02003370 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003371 return( PSA_ERROR_INVALID_ARGUMENT );
3372
3373#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003374 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003375 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003376 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3377 mbedtls_ctr_drbg_random,
3378 &global_data.ctr_drbg,
3379 MBEDTLS_RSA_PRIVATE,
3380 output_length,
3381 input,
3382 output,
3383 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003384 }
3385 else
3386#endif /* MBEDTLS_PKCS1_V15 */
3387#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003388 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003389 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003390 psa_rsa_oaep_set_padding_mode( alg, rsa );
3391 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3392 mbedtls_ctr_drbg_random,
3393 &global_data.ctr_drbg,
3394 MBEDTLS_RSA_PRIVATE,
3395 salt, salt_length,
3396 output_length,
3397 input,
3398 output,
3399 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003400 }
3401 else
3402#endif /* MBEDTLS_PKCS1_V21 */
3403 {
3404 return( PSA_ERROR_INVALID_ARGUMENT );
3405 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003406
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003407 return( mbedtls_to_psa_error( ret ) );
3408 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003409 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003410#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003411 {
3412 return( PSA_ERROR_NOT_SUPPORTED );
3413 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003414}
Gilles Peskine20035e32018-02-03 22:44:14 +01003415
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003416
3417
mohammad1603503973b2018-03-12 15:59:30 +02003418/****************************************************************/
3419/* Symmetric cryptography */
3420/****************************************************************/
3421
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003422/* Initialize the cipher operation structure. Once this function has been
3423 * called, psa_cipher_abort can run and will do the right thing. */
3424static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3425 psa_algorithm_t alg )
3426{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003427 if( ! PSA_ALG_IS_CIPHER( alg ) )
3428 {
3429 memset( operation, 0, sizeof( *operation ) );
3430 return( PSA_ERROR_INVALID_ARGUMENT );
3431 }
3432
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003433 operation->alg = alg;
3434 operation->key_set = 0;
3435 operation->iv_set = 0;
3436 operation->iv_required = 1;
3437 operation->iv_size = 0;
3438 operation->block_size = 0;
3439 mbedtls_cipher_init( &operation->ctx.cipher );
3440 return( PSA_SUCCESS );
3441}
3442
Gilles Peskinee553c652018-06-04 16:22:46 +02003443static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003444 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003445 psa_algorithm_t alg,
3446 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003447{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003448 int ret = 0;
3449 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003450 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003451 size_t key_bits;
3452 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003453 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3454 PSA_KEY_USAGE_ENCRYPT :
3455 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003456
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003457 /* A context must be freshly initialized before it can be set up. */
3458 if( operation->alg != 0 )
3459 {
3460 return( PSA_ERROR_BAD_STATE );
3461 }
3462
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003463 status = psa_cipher_init( operation, alg );
3464 if( status != PSA_SUCCESS )
3465 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003466
Gilles Peskine28f8f302019-07-24 13:30:31 +02003467 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003468 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003469 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003470 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003471
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003472 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003473 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003474 {
3475 status = PSA_ERROR_NOT_SUPPORTED;
3476 goto exit;
3477 }
mohammad1603503973b2018-03-12 15:59:30 +02003478
mohammad1603503973b2018-03-12 15:59:30 +02003479 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003480 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003481 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003482
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003483#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003484 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003485 {
3486 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003487 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003488 memcpy( keys, slot->data.raw.data, 16 );
3489 memcpy( keys + 16, slot->data.raw.data, 8 );
3490 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3491 keys,
3492 192, cipher_operation );
3493 }
3494 else
3495#endif
3496 {
3497 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3498 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003499 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003500 }
Moran Peker41deec42018-04-04 15:43:05 +03003501 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003502 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003503
mohammad16038481e742018-03-18 13:57:31 +02003504#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003505 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003506 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003507 case PSA_ALG_CBC_NO_PADDING:
3508 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3509 MBEDTLS_PADDING_NONE );
3510 break;
3511 case PSA_ALG_CBC_PKCS7:
3512 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3513 MBEDTLS_PADDING_PKCS7 );
3514 break;
3515 default:
3516 /* The algorithm doesn't involve padding. */
3517 ret = 0;
3518 break;
3519 }
3520 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003521 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003522#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3523
mohammad1603503973b2018-03-12 15:59:30 +02003524 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003525 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3526 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3527 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003528 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003529 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003530 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003531#if defined(MBEDTLS_CHACHA20_C)
3532 else
3533 if( alg == PSA_ALG_CHACHA20 )
3534 operation->iv_size = 12;
3535#endif
mohammad1603503973b2018-03-12 15:59:30 +02003536
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003537exit:
3538 if( status == 0 )
3539 status = mbedtls_to_psa_error( ret );
3540 if( status != 0 )
3541 psa_cipher_abort( operation );
3542 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003543}
3544
Gilles Peskinefe119512018-07-08 21:39:34 +02003545psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003546 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003547 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003548{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003549 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003550}
3551
Gilles Peskinefe119512018-07-08 21:39:34 +02003552psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003553 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003554 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003555{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003556 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003557}
3558
Gilles Peskinefe119512018-07-08 21:39:34 +02003559psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003560 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003561 size_t iv_size,
3562 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003563{
itayzafrir534bd7c2018-08-02 13:56:32 +03003564 psa_status_t status;
3565 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003566 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003567 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003568 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003569 }
Moran Peker41deec42018-04-04 15:43:05 +03003570 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003571 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003572 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003573 goto exit;
3574 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003575 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3576 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003577 if( ret != 0 )
3578 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003579 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003580 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003581 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003582
mohammad16038481e742018-03-18 13:57:31 +02003583 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003584 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003585
Moran Peker395db872018-05-31 14:07:14 +03003586exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003587 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003588 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003589 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003590}
3591
Gilles Peskinefe119512018-07-08 21:39:34 +02003592psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003593 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003594 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003595{
itayzafrir534bd7c2018-08-02 13:56:32 +03003596 psa_status_t status;
3597 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003598 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003599 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003600 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003601 }
Moran Pekera28258c2018-05-29 16:25:04 +03003602 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003603 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003604 status = PSA_ERROR_INVALID_ARGUMENT;
3605 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003606 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003607 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3608 status = mbedtls_to_psa_error( ret );
3609exit:
3610 if( status == PSA_SUCCESS )
3611 operation->iv_set = 1;
3612 else
mohammad1603503973b2018-03-12 15:59:30 +02003613 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003614 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003615}
3616
Gilles Peskinee553c652018-06-04 16:22:46 +02003617psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3618 const uint8_t *input,
3619 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003620 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003621 size_t output_size,
3622 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003623{
itayzafrir534bd7c2018-08-02 13:56:32 +03003624 psa_status_t status;
3625 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003626 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003627
3628 if( operation->alg == 0 )
3629 {
3630 return( PSA_ERROR_BAD_STATE );
3631 }
3632
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003633 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003634 {
3635 /* Take the unprocessed partial block left over from previous
3636 * update calls, if any, plus the input to this call. Remove
3637 * the last partial block, if any. You get the data that will be
3638 * output in this call. */
3639 expected_output_size =
3640 ( operation->ctx.cipher.unprocessed_len + input_length )
3641 / operation->block_size * operation->block_size;
3642 }
3643 else
3644 {
3645 expected_output_size = input_length;
3646 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003647
Gilles Peskine89d789c2018-06-04 17:17:16 +02003648 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003649 {
3650 status = PSA_ERROR_BUFFER_TOO_SMALL;
3651 goto exit;
3652 }
mohammad160382759612018-03-12 18:16:40 +02003653
mohammad1603503973b2018-03-12 15:59:30 +02003654 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003655 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003656 status = mbedtls_to_psa_error( ret );
3657exit:
3658 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003659 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003660 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003661}
3662
Gilles Peskinee553c652018-06-04 16:22:46 +02003663psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3664 uint8_t *output,
3665 size_t output_size,
3666 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003667{
David Saadab4ecc272019-02-14 13:48:10 +02003668 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003669 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003670 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003671
mohammad1603503973b2018-03-12 15:59:30 +02003672 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003673 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003674 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003675 }
3676 if( operation->iv_required && ! operation->iv_set )
3677 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003678 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003679 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003680
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003681 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003682 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3683 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003684 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003685 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003686 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003687 }
3688
Janos Follath315b51c2018-07-09 16:04:51 +01003689 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3690 temp_output_buffer,
3691 output_length );
3692 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003693 {
Janos Follath315b51c2018-07-09 16:04:51 +01003694 status = mbedtls_to_psa_error( cipher_ret );
3695 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003696 }
Janos Follath315b51c2018-07-09 16:04:51 +01003697
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003698 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003699 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003700 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003701 memcpy( output, temp_output_buffer, *output_length );
3702 else
3703 {
Janos Follath315b51c2018-07-09 16:04:51 +01003704 status = PSA_ERROR_BUFFER_TOO_SMALL;
3705 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003706 }
mohammad1603503973b2018-03-12 15:59:30 +02003707
Gilles Peskine3f108122018-12-07 18:14:53 +01003708 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003709 status = psa_cipher_abort( operation );
3710
3711 return( status );
3712
3713error:
3714
3715 *output_length = 0;
3716
Gilles Peskine3f108122018-12-07 18:14:53 +01003717 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003718 (void) psa_cipher_abort( operation );
3719
3720 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003721}
3722
Gilles Peskinee553c652018-06-04 16:22:46 +02003723psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3724{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003725 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003726 {
3727 /* The object has (apparently) been initialized but it is not
3728 * in use. It's ok to call abort on such an object, and there's
3729 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003730 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003731 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003732
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003733 /* Sanity check (shouldn't happen: operation->alg should
3734 * always have been initialized to a valid value). */
3735 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3736 return( PSA_ERROR_BAD_STATE );
3737
mohammad1603503973b2018-03-12 15:59:30 +02003738 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003739
Moran Peker41deec42018-04-04 15:43:05 +03003740 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003741 operation->key_set = 0;
3742 operation->iv_set = 0;
3743 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003744 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003745 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003746
Moran Peker395db872018-05-31 14:07:14 +03003747 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003748}
3749
Gilles Peskinea0655c32018-04-30 17:06:50 +02003750
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003751
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003752
mohammad16035955c982018-04-26 00:53:03 +03003753/****************************************************************/
3754/* AEAD */
3755/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003756
Gilles Peskineedf9a652018-08-17 18:11:56 +02003757typedef struct
3758{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003759 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003760 const mbedtls_cipher_info_t *cipher_info;
3761 union
3762 {
3763#if defined(MBEDTLS_CCM_C)
3764 mbedtls_ccm_context ccm;
3765#endif /* MBEDTLS_CCM_C */
3766#if defined(MBEDTLS_GCM_C)
3767 mbedtls_gcm_context gcm;
3768#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003769#if defined(MBEDTLS_CHACHAPOLY_C)
3770 mbedtls_chachapoly_context chachapoly;
3771#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003772 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003773 psa_algorithm_t core_alg;
3774 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003775 uint8_t tag_length;
3776} aead_operation_t;
3777
Gilles Peskine30a9e412019-01-14 18:36:12 +01003778static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003779{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003780 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003781 {
3782#if defined(MBEDTLS_CCM_C)
3783 case PSA_ALG_CCM:
3784 mbedtls_ccm_free( &operation->ctx.ccm );
3785 break;
3786#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003787#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003788 case PSA_ALG_GCM:
3789 mbedtls_gcm_free( &operation->ctx.gcm );
3790 break;
3791#endif /* MBEDTLS_GCM_C */
3792 }
3793}
3794
3795static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003796 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003797 psa_key_usage_t usage,
3798 psa_algorithm_t alg )
3799{
3800 psa_status_t status;
3801 size_t key_bits;
3802 mbedtls_cipher_id_t cipher_id;
3803
Gilles Peskine28f8f302019-07-24 13:30:31 +02003804 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003805 if( status != PSA_SUCCESS )
3806 return( status );
3807
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003808 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003809
3810 operation->cipher_info =
3811 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3812 &cipher_id );
3813 if( operation->cipher_info == NULL )
3814 return( PSA_ERROR_NOT_SUPPORTED );
3815
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003816 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003817 {
3818#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003819 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3820 operation->core_alg = PSA_ALG_CCM;
3821 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003822 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3823 * The call to mbedtls_ccm_encrypt_and_tag or
3824 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003825 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3826 return( PSA_ERROR_INVALID_ARGUMENT );
3827 mbedtls_ccm_init( &operation->ctx.ccm );
3828 status = mbedtls_to_psa_error(
3829 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3830 operation->slot->data.raw.data,
3831 (unsigned int) key_bits ) );
3832 if( status != 0 )
3833 goto cleanup;
3834 break;
3835#endif /* MBEDTLS_CCM_C */
3836
3837#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003838 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3839 operation->core_alg = PSA_ALG_GCM;
3840 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003841 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3842 * The call to mbedtls_gcm_crypt_and_tag or
3843 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003844 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3845 return( PSA_ERROR_INVALID_ARGUMENT );
3846 mbedtls_gcm_init( &operation->ctx.gcm );
3847 status = mbedtls_to_psa_error(
3848 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3849 operation->slot->data.raw.data,
3850 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003851 if( status != 0 )
3852 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003853 break;
3854#endif /* MBEDTLS_GCM_C */
3855
Gilles Peskine26869f22019-05-06 15:25:00 +02003856#if defined(MBEDTLS_CHACHAPOLY_C)
3857 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3858 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3859 operation->full_tag_length = 16;
3860 /* We only support the default tag length. */
3861 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3862 return( PSA_ERROR_NOT_SUPPORTED );
3863 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3864 status = mbedtls_to_psa_error(
3865 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3866 operation->slot->data.raw.data ) );
3867 if( status != 0 )
3868 goto cleanup;
3869 break;
3870#endif /* MBEDTLS_CHACHAPOLY_C */
3871
Gilles Peskineedf9a652018-08-17 18:11:56 +02003872 default:
3873 return( PSA_ERROR_NOT_SUPPORTED );
3874 }
3875
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003876 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3877 {
3878 status = PSA_ERROR_INVALID_ARGUMENT;
3879 goto cleanup;
3880 }
3881 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003882
Gilles Peskineedf9a652018-08-17 18:11:56 +02003883 return( PSA_SUCCESS );
3884
3885cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003886 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003887 return( status );
3888}
3889
Gilles Peskinec5487a82018-12-03 18:08:14 +01003890psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003891 psa_algorithm_t alg,
3892 const uint8_t *nonce,
3893 size_t nonce_length,
3894 const uint8_t *additional_data,
3895 size_t additional_data_length,
3896 const uint8_t *plaintext,
3897 size_t plaintext_length,
3898 uint8_t *ciphertext,
3899 size_t ciphertext_size,
3900 size_t *ciphertext_length )
3901{
mohammad16035955c982018-04-26 00:53:03 +03003902 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003903 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003904 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003905
mohammad1603f08a5502018-06-03 15:05:47 +03003906 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003907
Gilles Peskinec5487a82018-12-03 18:08:14 +01003908 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003909 if( status != PSA_SUCCESS )
3910 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003911
Gilles Peskineedf9a652018-08-17 18:11:56 +02003912 /* For all currently supported modes, the tag is at the end of the
3913 * ciphertext. */
3914 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3915 {
3916 status = PSA_ERROR_BUFFER_TOO_SMALL;
3917 goto exit;
3918 }
3919 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003920
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003921#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003922 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003923 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003924 status = mbedtls_to_psa_error(
3925 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3926 MBEDTLS_GCM_ENCRYPT,
3927 plaintext_length,
3928 nonce, nonce_length,
3929 additional_data, additional_data_length,
3930 plaintext, ciphertext,
3931 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003932 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003933 else
3934#endif /* MBEDTLS_GCM_C */
3935#if defined(MBEDTLS_CCM_C)
3936 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003937 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003938 status = mbedtls_to_psa_error(
3939 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3940 plaintext_length,
3941 nonce, nonce_length,
3942 additional_data,
3943 additional_data_length,
3944 plaintext, ciphertext,
3945 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003946 }
mohammad16035c8845f2018-05-09 05:40:09 -07003947 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003948#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003949#if defined(MBEDTLS_CHACHAPOLY_C)
3950 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3951 {
3952 if( nonce_length != 12 || operation.tag_length != 16 )
3953 {
3954 status = PSA_ERROR_NOT_SUPPORTED;
3955 goto exit;
3956 }
3957 status = mbedtls_to_psa_error(
3958 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3959 plaintext_length,
3960 nonce,
3961 additional_data,
3962 additional_data_length,
3963 plaintext,
3964 ciphertext,
3965 tag ) );
3966 }
3967 else
3968#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003969 {
mohammad1603554faad2018-06-03 15:07:38 +03003970 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003971 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003972
Gilles Peskineedf9a652018-08-17 18:11:56 +02003973 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3974 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003975
Gilles Peskineedf9a652018-08-17 18:11:56 +02003976exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003977 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003978 if( status == PSA_SUCCESS )
3979 *ciphertext_length = plaintext_length + operation.tag_length;
3980 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003981}
3982
Gilles Peskineee652a32018-06-01 19:23:52 +02003983/* Locate the tag in a ciphertext buffer containing the encrypted data
3984 * followed by the tag. Return the length of the part preceding the tag in
3985 * *plaintext_length. This is the size of the plaintext in modes where
3986 * the encrypted data has the same size as the plaintext, such as
3987 * CCM and GCM. */
3988static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3989 const uint8_t *ciphertext,
3990 size_t ciphertext_length,
3991 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003992 const uint8_t **p_tag )
3993{
3994 size_t payload_length;
3995 if( tag_length > ciphertext_length )
3996 return( PSA_ERROR_INVALID_ARGUMENT );
3997 payload_length = ciphertext_length - tag_length;
3998 if( payload_length > plaintext_size )
3999 return( PSA_ERROR_BUFFER_TOO_SMALL );
4000 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004001 return( PSA_SUCCESS );
4002}
4003
Gilles Peskinec5487a82018-12-03 18:08:14 +01004004psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004005 psa_algorithm_t alg,
4006 const uint8_t *nonce,
4007 size_t nonce_length,
4008 const uint8_t *additional_data,
4009 size_t additional_data_length,
4010 const uint8_t *ciphertext,
4011 size_t ciphertext_length,
4012 uint8_t *plaintext,
4013 size_t plaintext_size,
4014 size_t *plaintext_length )
4015{
mohammad16035955c982018-04-26 00:53:03 +03004016 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004017 aead_operation_t operation;
4018 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004019
Gilles Peskineee652a32018-06-01 19:23:52 +02004020 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004021
Gilles Peskinec5487a82018-12-03 18:08:14 +01004022 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004023 if( status != PSA_SUCCESS )
4024 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004025
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004026 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4027 ciphertext, ciphertext_length,
4028 plaintext_size, &tag );
4029 if( status != PSA_SUCCESS )
4030 goto exit;
4031
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004032#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004033 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004034 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004035 status = mbedtls_to_psa_error(
4036 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4037 ciphertext_length - operation.tag_length,
4038 nonce, nonce_length,
4039 additional_data,
4040 additional_data_length,
4041 tag, operation.tag_length,
4042 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004043 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004044 else
4045#endif /* MBEDTLS_GCM_C */
4046#if defined(MBEDTLS_CCM_C)
4047 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004048 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004049 status = mbedtls_to_psa_error(
4050 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4051 ciphertext_length - operation.tag_length,
4052 nonce, nonce_length,
4053 additional_data,
4054 additional_data_length,
4055 ciphertext, plaintext,
4056 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004057 }
mohammad160339574652018-06-01 04:39:53 -07004058 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004059#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004060#if defined(MBEDTLS_CHACHAPOLY_C)
4061 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4062 {
4063 if( nonce_length != 12 || operation.tag_length != 16 )
4064 {
4065 status = PSA_ERROR_NOT_SUPPORTED;
4066 goto exit;
4067 }
4068 status = mbedtls_to_psa_error(
4069 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4070 ciphertext_length - operation.tag_length,
4071 nonce,
4072 additional_data,
4073 additional_data_length,
4074 tag,
4075 ciphertext,
4076 plaintext ) );
4077 }
4078 else
4079#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004080 {
mohammad1603554faad2018-06-03 15:07:38 +03004081 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004082 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004083
Gilles Peskineedf9a652018-08-17 18:11:56 +02004084 if( status != PSA_SUCCESS && plaintext_size != 0 )
4085 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004086
Gilles Peskineedf9a652018-08-17 18:11:56 +02004087exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004088 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004089 if( status == PSA_SUCCESS )
4090 *plaintext_length = ciphertext_length - operation.tag_length;
4091 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004092}
4093
Gilles Peskinea0655c32018-04-30 17:06:50 +02004094
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004095
Gilles Peskine20035e32018-02-03 22:44:14 +01004096/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004097/* Generators */
4098/****************************************************************/
4099
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004100#define HKDF_STATE_INIT 0 /* no input yet */
4101#define HKDF_STATE_STARTED 1 /* got salt */
4102#define HKDF_STATE_KEYED 2 /* got key */
4103#define HKDF_STATE_OUTPUT 3 /* output started */
4104
Gilles Peskinecbe66502019-05-16 16:59:18 +02004105static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004106 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004107{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004108 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4109 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004110 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004111 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004112}
4113
4114
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004115psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004116{
4117 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004118 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004119 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004120 {
4121 /* The object has (apparently) been initialized but it is not
4122 * in use. It's ok to call abort on such an object, and there's
4123 * nothing to do. */
4124 }
4125 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004126#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004127 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004128 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004129 mbedtls_free( operation->ctx.hkdf.info );
4130 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004131 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004132 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004133 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004134 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004135 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004136#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004137 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004138 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004139 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4140 operation->ctx.tls12_prf.key_len );
4141 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004142 }
Hanno Becker580fba12018-11-13 20:50:45 +00004143
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004144 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004145 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004146 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4147 operation->ctx.tls12_prf.Ai_with_seed_len );
4148 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004149 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004150#else
4151 if( operation->ctx.tls12_prf.seed != NULL )
4152 {
4153 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4154 operation->ctx.tls12_prf.seed_length );
4155 mbedtls_free( operation->ctx.tls12_prf.seed );
4156 }
4157
4158 if( operation->ctx.tls12_prf.label != NULL )
4159 {
4160 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4161 operation->ctx.tls12_prf.label_length );
4162 mbedtls_free( operation->ctx.tls12_prf.label );
4163 }
4164
4165 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4166
4167 /* We leave the fields Ai and output_block to be erased safely by the
4168 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004169#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004170 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004171 else
4172#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004173 {
4174 status = PSA_ERROR_BAD_STATE;
4175 }
Janos Follath083036a2019-06-11 10:22:26 +01004176 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004177 return( status );
4178}
4179
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004180psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004181 size_t *capacity)
4182{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004183 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004184 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004185 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004186 return PSA_ERROR_BAD_STATE;
4187 }
4188
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004189 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004190 return( PSA_SUCCESS );
4191}
4192
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004193psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004194 size_t capacity )
4195{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004196 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004197 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004198 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004199 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004200 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004201 return( PSA_SUCCESS );
4202}
4203
Gilles Peskinebef7f142018-07-12 17:22:21 +02004204#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004205/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004206 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004207static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004208 psa_algorithm_t hash_alg,
4209 uint8_t *output,
4210 size_t output_length )
4211{
4212 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4213 psa_status_t status;
4214
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004215 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4216 return( PSA_ERROR_BAD_STATE );
4217 hkdf->state = HKDF_STATE_OUTPUT;
4218
Gilles Peskinebef7f142018-07-12 17:22:21 +02004219 while( output_length != 0 )
4220 {
4221 /* Copy what remains of the current block */
4222 uint8_t n = hash_length - hkdf->offset_in_block;
4223 if( n > output_length )
4224 n = (uint8_t) output_length;
4225 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4226 output += n;
4227 output_length -= n;
4228 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004229 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004230 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004231 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004232 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004233 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004234 * object was corrupted or if this function is called directly
4235 * inside the library. */
4236 if( hkdf->block_number == 0xff )
4237 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004238
4239 /* We need a new block */
4240 ++hkdf->block_number;
4241 hkdf->offset_in_block = 0;
4242 status = psa_hmac_setup_internal( &hkdf->hmac,
4243 hkdf->prk, hash_length,
4244 hash_alg );
4245 if( status != PSA_SUCCESS )
4246 return( status );
4247 if( hkdf->block_number != 1 )
4248 {
4249 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4250 hkdf->output_block,
4251 hash_length );
4252 if( status != PSA_SUCCESS )
4253 return( status );
4254 }
4255 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4256 hkdf->info,
4257 hkdf->info_length );
4258 if( status != PSA_SUCCESS )
4259 return( status );
4260 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4261 &hkdf->block_number, 1 );
4262 if( status != PSA_SUCCESS )
4263 return( status );
4264 status = psa_hmac_finish_internal( &hkdf->hmac,
4265 hkdf->output_block,
4266 sizeof( hkdf->output_block ) );
4267 if( status != PSA_SUCCESS )
4268 return( status );
4269 }
4270
4271 return( PSA_SUCCESS );
4272}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004273
Janos Follath999f6482019-06-11 12:04:10 +01004274#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004275static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4276 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004277 psa_algorithm_t alg )
4278{
4279 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4280 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4281 psa_hmac_internal_data hmac;
4282 psa_status_t status, cleanup_status;
4283
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004284 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004285 size_t Ai_len;
4286
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004287 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004288 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004289 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004290 * object was corrupted or if this function is called directly
4291 * inside the library. */
4292 if( tls12_prf->block_number == 0xff )
4293 return( PSA_ERROR_BAD_STATE );
4294
4295 /* We need a new block */
4296 ++tls12_prf->block_number;
4297 tls12_prf->offset_in_block = 0;
4298
4299 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4300 *
4301 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4302 *
4303 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4304 * HMAC_hash(secret, A(2) + seed) +
4305 * HMAC_hash(secret, A(3) + seed) + ...
4306 *
4307 * A(0) = seed
4308 * A(i) = HMAC_hash( secret, A(i-1) )
4309 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004310 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004311 * `HMAC_hash(secret, A(i) + seed)` from which the output
4312 * is currently extracted as `output_block`, while
4313 * `A(i) + seed` is stored in `Ai_with_seed`.
4314 *
4315 * Generating a new block means recalculating `Ai_with_seed`
4316 * from the A(i)-part of it, and afterwards recalculating
4317 * `output_block`.
4318 *
4319 * A(0) is computed at setup time.
4320 *
4321 */
4322
4323 psa_hmac_init_internal( &hmac );
4324
4325 /* We must distinguish the calculation of A(1) from those
4326 * of A(2) and higher, because A(0)=seed has a different
4327 * length than the other A(i). */
4328 if( tls12_prf->block_number == 1 )
4329 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004330 Ai = tls12_prf->Ai_with_seed + hash_length;
4331 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004332 }
4333 else
4334 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004335 Ai = tls12_prf->Ai_with_seed;
4336 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004337 }
4338
Hanno Becker3b339e22018-11-13 20:56:14 +00004339 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4340 status = psa_hmac_setup_internal( &hmac,
4341 tls12_prf->key,
4342 tls12_prf->key_len,
4343 hash_alg );
4344 if( status != PSA_SUCCESS )
4345 goto cleanup;
4346
4347 status = psa_hash_update( &hmac.hash_ctx,
4348 Ai, Ai_len );
4349 if( status != PSA_SUCCESS )
4350 goto cleanup;
4351
4352 status = psa_hmac_finish_internal( &hmac,
4353 tls12_prf->Ai_with_seed,
4354 hash_length );
4355 if( status != PSA_SUCCESS )
4356 goto cleanup;
4357
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004358 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4359 status = psa_hmac_setup_internal( &hmac,
4360 tls12_prf->key,
4361 tls12_prf->key_len,
4362 hash_alg );
4363 if( status != PSA_SUCCESS )
4364 goto cleanup;
4365
4366 status = psa_hash_update( &hmac.hash_ctx,
4367 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004368 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004369 if( status != PSA_SUCCESS )
4370 goto cleanup;
4371
4372 status = psa_hmac_finish_internal( &hmac,
4373 tls12_prf->output_block,
4374 hash_length );
4375 if( status != PSA_SUCCESS )
4376 goto cleanup;
4377
4378cleanup:
4379
4380 cleanup_status = psa_hmac_abort_internal( &hmac );
4381 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4382 status = cleanup_status;
4383
4384 return( status );
4385}
Janos Follath7742fee2019-06-17 12:58:10 +01004386#else
4387static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4388 psa_tls12_prf_key_derivation_t *tls12_prf,
4389 psa_algorithm_t alg )
4390{
4391 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4392 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004393 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4394 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004395
Janos Follath7742fee2019-06-17 12:58:10 +01004396 /* We can't be wanting more output after block 0xff, otherwise
4397 * the capacity check in psa_key_derivation_output_bytes() would have
4398 * prevented this call. It could happen only if the operation
4399 * object was corrupted or if this function is called directly
4400 * inside the library. */
4401 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004402 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004403
4404 /* We need a new block */
4405 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004406 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004407
4408 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4409 *
4410 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4411 *
4412 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4413 * HMAC_hash(secret, A(2) + seed) +
4414 * HMAC_hash(secret, A(3) + seed) + ...
4415 *
4416 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004417 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004418 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004419 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004420 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004421 * is currently extracted as `output_block` and where i is
4422 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004423 */
4424
Janos Follathea29bfb2019-06-19 12:21:20 +01004425 /* Save the hash context before using it, to preserve the hash state with
4426 * only the inner padding in it. We need this, because inner padding depends
4427 * on the key (secret in the RFC's terminology). */
4428 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4429 if( status != PSA_SUCCESS )
4430 goto cleanup;
4431
4432 /* Calculate A(i) where i = tls12_prf->block_number. */
4433 if( tls12_prf->block_number == 1 )
4434 {
4435 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4436 * the variable seed and in this instance means it in the context of the
4437 * P_hash function, where seed = label + seed.) */
4438 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4439 tls12_prf->label, tls12_prf->label_length );
4440 if( status != PSA_SUCCESS )
4441 goto cleanup;
4442 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4443 tls12_prf->seed, tls12_prf->seed_length );
4444 if( status != PSA_SUCCESS )
4445 goto cleanup;
4446 }
4447 else
4448 {
4449 /* A(i) = HMAC_hash(secret, A(i-1)) */
4450 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4451 tls12_prf->Ai, hash_length );
4452 if( status != PSA_SUCCESS )
4453 goto cleanup;
4454 }
4455
4456 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4457 tls12_prf->Ai, hash_length );
4458 if( status != PSA_SUCCESS )
4459 goto cleanup;
4460 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4461 if( status != PSA_SUCCESS )
4462 goto cleanup;
4463
4464 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4465 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4466 tls12_prf->Ai, hash_length );
4467 if( status != PSA_SUCCESS )
4468 goto cleanup;
4469 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4470 tls12_prf->label, tls12_prf->label_length );
4471 if( status != PSA_SUCCESS )
4472 goto cleanup;
4473 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4474 tls12_prf->seed, tls12_prf->seed_length );
4475 if( status != PSA_SUCCESS )
4476 goto cleanup;
4477 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4478 tls12_prf->output_block, hash_length );
4479 if( status != PSA_SUCCESS )
4480 goto cleanup;
4481 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4482 if( status != PSA_SUCCESS )
4483 goto cleanup;
4484
Janos Follath7742fee2019-06-17 12:58:10 +01004485
4486cleanup:
4487
Janos Follathea29bfb2019-06-19 12:21:20 +01004488 cleanup_status = psa_hash_abort( &backup );
4489 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4490 status = cleanup_status;
4491
4492 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004493}
Janos Follath999f6482019-06-11 12:04:10 +01004494#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004495
Janos Follath999f6482019-06-11 12:04:10 +01004496#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004497/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004498 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004499static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004500 psa_tls12_prf_key_derivation_t *tls12_prf,
4501 psa_algorithm_t alg,
4502 uint8_t *output,
4503 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004504{
4505 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4506 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4507 psa_status_t status;
4508
4509 while( output_length != 0 )
4510 {
4511 /* Copy what remains of the current block */
4512 uint8_t n = hash_length - tls12_prf->offset_in_block;
4513
4514 /* Check if we have fully processed the current block. */
4515 if( n == 0 )
4516 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004517 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004518 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004519 if( status != PSA_SUCCESS )
4520 return( status );
4521
4522 continue;
4523 }
4524
4525 if( n > output_length )
4526 n = (uint8_t) output_length;
4527 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4528 n );
4529 output += n;
4530 output_length -= n;
4531 tls12_prf->offset_in_block += n;
4532 }
4533
4534 return( PSA_SUCCESS );
4535}
Janos Follath844eb0e2019-06-19 12:10:49 +01004536#else
4537static psa_status_t psa_key_derivation_tls12_prf_read(
4538 psa_tls12_prf_key_derivation_t *tls12_prf,
4539 psa_algorithm_t alg,
4540 uint8_t *output,
4541 size_t output_length )
4542{
4543 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4544 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4545 psa_status_t status;
4546 uint8_t offset, length;
4547
4548 while( output_length != 0 )
4549 {
4550 /* Check if we have fully processed the current block. */
4551 if( tls12_prf->left_in_block == 0 )
4552 {
4553 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4554 alg );
4555 if( status != PSA_SUCCESS )
4556 return( status );
4557
4558 continue;
4559 }
4560
4561 if( tls12_prf->left_in_block > output_length )
4562 length = (uint8_t) output_length;
4563 else
4564 length = tls12_prf->left_in_block;
4565
4566 offset = hash_length - tls12_prf->left_in_block;
4567 memcpy( output, tls12_prf->output_block + offset, length );
4568 output += length;
4569 output_length -= length;
4570 tls12_prf->left_in_block -= length;
4571 }
4572
4573 return( PSA_SUCCESS );
4574}
Janos Follath999f6482019-06-11 12:04:10 +01004575#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004576#endif /* MBEDTLS_MD_C */
4577
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004578psa_status_t psa_key_derivation_output_bytes(
4579 psa_key_derivation_operation_t *operation,
4580 uint8_t *output,
4581 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004582{
4583 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004584 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004585
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004586 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004587 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004588 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004589 return PSA_ERROR_BAD_STATE;
4590 }
4591
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004592 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004593 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004594 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004595 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004596 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004597 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004598 goto exit;
4599 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004600 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004601 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004602 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004603 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004604 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4605 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004606 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004607 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004608 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004609 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004610 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004611
Gilles Peskinebef7f142018-07-12 17:22:21 +02004612#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004613 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004614 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004615 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004616 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004617 output, output_length );
4618 }
Janos Follathadbec812019-06-14 11:05:39 +01004619 else
Janos Follathadbec812019-06-14 11:05:39 +01004620 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004621 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004622 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004623 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004624 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004625 output_length );
4626 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004627 else
4628#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004629 {
4630 return( PSA_ERROR_BAD_STATE );
4631 }
4632
4633exit:
4634 if( status != PSA_SUCCESS )
4635 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004636 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004637 * This allows us to differentiate between exhausted operations and
4638 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4639 * operations. */
4640 psa_algorithm_t alg = operation->alg;
4641 psa_key_derivation_abort( operation );
4642 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004643 memset( output, '!', output_length );
4644 }
4645 return( status );
4646}
4647
Gilles Peskine08542d82018-07-19 17:05:42 +02004648#if defined(MBEDTLS_DES_C)
4649static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4650{
4651 if( data_size >= 8 )
4652 mbedtls_des_key_set_parity( data );
4653 if( data_size >= 16 )
4654 mbedtls_des_key_set_parity( data + 8 );
4655 if( data_size >= 24 )
4656 mbedtls_des_key_set_parity( data + 16 );
4657}
4658#endif /* MBEDTLS_DES_C */
4659
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004660static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004661 psa_key_slot_t *slot,
4662 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004663 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004664{
4665 uint8_t *data = NULL;
4666 size_t bytes = PSA_BITS_TO_BYTES( bits );
4667 psa_status_t status;
4668
4669 if( ! key_type_is_raw_bytes( slot->type ) )
4670 return( PSA_ERROR_INVALID_ARGUMENT );
4671 if( bits % 8 != 0 )
4672 return( PSA_ERROR_INVALID_ARGUMENT );
4673 data = mbedtls_calloc( 1, bytes );
4674 if( data == NULL )
4675 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4676
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004677 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004678 if( status != PSA_SUCCESS )
4679 goto exit;
4680#if defined(MBEDTLS_DES_C)
4681 if( slot->type == PSA_KEY_TYPE_DES )
4682 psa_des_set_key_parity( data, bytes );
4683#endif /* MBEDTLS_DES_C */
4684 status = psa_import_key_into_slot( slot, data, bytes );
4685
4686exit:
4687 mbedtls_free( data );
4688 return( status );
4689}
4690
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004691psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004692 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004693 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004694{
4695 psa_status_t status;
4696 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004697 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004698 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004699#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4700 if( driver != NULL )
4701 {
4702 /* Deriving a key in a secure element is not implemented yet. */
4703 status = PSA_ERROR_NOT_SUPPORTED;
4704 }
4705#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004706 if( status == PSA_SUCCESS )
4707 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004708 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004709 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004710 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004711 }
4712 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004713 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004714 if( status != PSA_SUCCESS )
4715 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004716 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004717 *handle = 0;
4718 }
4719 return( status );
4720}
4721
Gilles Peskineeab56e42018-07-12 17:12:33 +02004722
4723
4724/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004725/* Key derivation */
4726/****************************************************************/
4727
Gilles Peskinea05219c2018-11-16 16:02:56 +01004728#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004729#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004731 * of the HKDF algorithm.
4732 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004733 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004734 * to potentially free embedded data structures and wipe confidential data.
4735 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004736static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004737 const uint8_t *secret,
4738 size_t secret_length,
4739 psa_algorithm_t hash_alg,
4740 const uint8_t *salt,
4741 size_t salt_length,
4742 const uint8_t *label,
4743 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004744{
4745 psa_status_t status;
4746 status = psa_hmac_setup_internal( &hkdf->hmac,
4747 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004748 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004749 if( status != PSA_SUCCESS )
4750 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004751 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004752 if( status != PSA_SUCCESS )
4753 return( status );
4754 status = psa_hmac_finish_internal( &hkdf->hmac,
4755 hkdf->prk,
4756 sizeof( hkdf->prk ) );
4757 if( status != PSA_SUCCESS )
4758 return( status );
4759 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4760 hkdf->block_number = 0;
4761 hkdf->info_length = label_length;
4762 if( label_length != 0 )
4763 {
4764 hkdf->info = mbedtls_calloc( 1, label_length );
4765 if( hkdf->info == NULL )
4766 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4767 memcpy( hkdf->info, label, label_length );
4768 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004769 hkdf->state = HKDF_STATE_KEYED;
4770 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004771 return( PSA_SUCCESS );
4772}
Janos Follath71a4c912019-06-11 09:14:47 +01004773#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004774#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004775
Gilles Peskinea05219c2018-11-16 16:02:56 +01004776#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004777#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004778/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004779 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004780 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004781 * to potentially free embedded data structures and wipe confidential data.
4782 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004783static psa_status_t psa_key_derivation_tls12_prf_setup(
4784 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004785 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004786 size_t key_len,
4787 psa_algorithm_t hash_alg,
4788 const uint8_t *salt,
4789 size_t salt_length,
4790 const uint8_t *label,
4791 size_t label_length )
4792{
4793 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004794 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4795 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004796
4797 tls12_prf->key = mbedtls_calloc( 1, key_len );
4798 if( tls12_prf->key == NULL )
4799 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4800 tls12_prf->key_len = key_len;
4801 memcpy( tls12_prf->key, key, key_len );
4802
Hanno Becker580fba12018-11-13 20:50:45 +00004803 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004804 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004805 if( overflow )
4806 return( PSA_ERROR_INVALID_ARGUMENT );
4807
4808 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4809 if( tls12_prf->Ai_with_seed == NULL )
4810 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4811 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4812
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004813 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4814 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004815 if( label_length != 0 )
4816 {
4817 memcpy( tls12_prf->Ai_with_seed + hash_length,
4818 label, label_length );
4819 }
4820
4821 if( salt_length != 0 )
4822 {
4823 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4824 salt, salt_length );
4825 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004826
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004827 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004828 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004829 tls12_prf->block_number = 0;
4830 tls12_prf->offset_in_block = hash_length;
4831
4832 return( PSA_SUCCESS );
4833}
Janos Follath71a4c912019-06-11 09:14:47 +01004834#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004835
Janos Follath71a4c912019-06-11 09:14:47 +01004836#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004837/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004838static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4839 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004840 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004841 size_t psk_len,
4842 psa_algorithm_t hash_alg,
4843 const uint8_t *salt,
4844 size_t salt_length,
4845 const uint8_t *label,
4846 size_t label_length )
4847{
4848 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004849 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00004850
4851 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4852 return( PSA_ERROR_INVALID_ARGUMENT );
4853
4854 /* Quoting RFC 4279, Section 2:
4855 *
4856 * The premaster secret is formed as follows: if the PSK is N octets
4857 * long, concatenate a uint16 with the value N, N zero octets, a second
4858 * uint16 with the value N, and the PSK itself.
4859 */
4860
4861 pms[0] = ( psk_len >> 8 ) & 0xff;
4862 pms[1] = ( psk_len >> 0 ) & 0xff;
4863 memset( pms + 2, 0, psk_len );
4864 pms[2 + psk_len + 0] = pms[0];
4865 pms[2 + psk_len + 1] = pms[1];
4866 memcpy( pms + 4 + psk_len, psk, psk_len );
4867
Gilles Peskinecbe66502019-05-16 16:59:18 +02004868 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004869 pms, 4 + 2 * psk_len,
4870 hash_alg,
4871 salt, salt_length,
4872 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004873
Gilles Peskine3f108122018-12-07 18:14:53 +01004874 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004875 return( status );
4876}
Janos Follath71a4c912019-06-11 09:14:47 +01004877#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004878#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004879
Janos Follath71a4c912019-06-11 09:14:47 +01004880#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004881/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004882 * to potentially free embedded data structures and wipe confidential data.
4883 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004884static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004885 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004886 const uint8_t *secret, size_t secret_length,
4887 psa_algorithm_t alg,
4888 const uint8_t *salt, size_t salt_length,
4889 const uint8_t *label, size_t label_length,
4890 size_t capacity )
4891{
4892 psa_status_t status;
4893 size_t max_capacity;
4894
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004895 /* Set operation->alg even on failure so that abort knows what to do. */
4896 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004897
4898#if defined(MBEDTLS_MD_C)
4899 if( PSA_ALG_IS_HKDF( alg ) )
4900 {
4901 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4902 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4903 if( hash_size == 0 )
4904 return( PSA_ERROR_NOT_SUPPORTED );
4905 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004907 secret, secret_length,
4908 hash_alg,
4909 salt, salt_length,
4910 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004911 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004912 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4913 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4914 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004915 {
4916 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4917 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4918
4919 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4920 if( hash_alg != PSA_ALG_SHA_256 &&
4921 hash_alg != PSA_ALG_SHA_384 )
4922 {
4923 return( PSA_ERROR_NOT_SUPPORTED );
4924 }
4925
4926 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004927
4928 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4929 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004930 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004931 secret, secret_length,
4932 hash_alg, salt, salt_length,
4933 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004934 }
4935 else
4936 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004937 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004938 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004939 secret, secret_length,
4940 hash_alg, salt, salt_length,
4941 label, label_length );
4942 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004943 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004944 else
4945#endif
4946 {
4947 return( PSA_ERROR_NOT_SUPPORTED );
4948 }
4949
4950 if( status != PSA_SUCCESS )
4951 return( status );
4952
4953 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004954 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004955 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004956 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004957 else
4958 return( PSA_ERROR_INVALID_ARGUMENT );
4959
4960 return( PSA_SUCCESS );
4961}
Janos Follath71a4c912019-06-11 09:14:47 +01004962#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004963
Janos Follath71a4c912019-06-11 09:14:47 +01004964#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004965psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004966 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004967 psa_algorithm_t alg,
4968 const uint8_t *salt,
4969 size_t salt_length,
4970 const uint8_t *label,
4971 size_t label_length,
4972 size_t capacity )
4973{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004974 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004975 psa_status_t status;
4976
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004977 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004978 return( PSA_ERROR_BAD_STATE );
4979
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004980 /* Make sure that alg is a key derivation algorithm. This prevents
4981 * key selection algorithms, which psa_key_derivation_internal
4982 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004983 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4984 return( PSA_ERROR_INVALID_ARGUMENT );
4985
Gilles Peskine28f8f302019-07-24 13:30:31 +02004986 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004987 if( status != PSA_SUCCESS )
4988 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004989
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004990 if( slot->type != PSA_KEY_TYPE_DERIVE )
4991 return( PSA_ERROR_INVALID_ARGUMENT );
4992
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004993 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004994 slot->data.raw.data,
4995 slot->data.raw.bytes,
4996 alg,
4997 salt, salt_length,
4998 label, label_length,
4999 capacity );
5000 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005001 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005002 return( status );
5003}
Janos Follath71a4c912019-06-11 09:14:47 +01005004#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005005
Gilles Peskine969c5d62019-01-16 15:53:06 +01005006static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005007 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005008 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005009{
Janos Follath5fe19732019-06-20 15:09:30 +01005010 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5011 * initialisers for this union leave some bytes unspecified.) */
5012 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5013
Gilles Peskine969c5d62019-01-16 15:53:06 +01005014 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005015#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005016 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5017 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5018 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005019 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005020 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005021 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5022 if( hash_size == 0 )
5023 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005024 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5025 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005026 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005027 {
5028 return( PSA_ERROR_NOT_SUPPORTED );
5029 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005030 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005031 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005032 }
5033#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005034 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005035 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005036}
5037
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005038psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005039 psa_algorithm_t alg )
5040{
5041 psa_status_t status;
5042
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005043 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005044 return( PSA_ERROR_BAD_STATE );
5045
Gilles Peskine6843c292019-01-18 16:44:49 +01005046 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5047 return( PSA_ERROR_INVALID_ARGUMENT );
5048 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005049 {
5050 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005051 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005052 }
5053 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5054 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005055 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005056 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005057 else
5058 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005059
5060 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005061 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005062 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005063}
5064
5065#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005066static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005067 psa_algorithm_t hash_alg,
5068 psa_key_derivation_step_t step,
5069 const uint8_t *data,
5070 size_t data_length )
5071{
5072 psa_status_t status;
5073 switch( step )
5074 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005075 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005076 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005077 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005078 status = psa_hmac_setup_internal( &hkdf->hmac,
5079 data, data_length,
5080 hash_alg );
5081 if( status != PSA_SUCCESS )
5082 return( status );
5083 hkdf->state = HKDF_STATE_STARTED;
5084 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005085 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005086 /* If no salt was provided, use an empty salt. */
5087 if( hkdf->state == HKDF_STATE_INIT )
5088 {
5089 status = psa_hmac_setup_internal( &hkdf->hmac,
5090 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005091 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005092 if( status != PSA_SUCCESS )
5093 return( status );
5094 hkdf->state = HKDF_STATE_STARTED;
5095 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005096 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005097 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005098 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5099 data, data_length );
5100 if( status != PSA_SUCCESS )
5101 return( status );
5102 status = psa_hmac_finish_internal( &hkdf->hmac,
5103 hkdf->prk,
5104 sizeof( hkdf->prk ) );
5105 if( status != PSA_SUCCESS )
5106 return( status );
5107 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5108 hkdf->block_number = 0;
5109 hkdf->state = HKDF_STATE_KEYED;
5110 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005111 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005112 if( hkdf->state == HKDF_STATE_OUTPUT )
5113 return( PSA_ERROR_BAD_STATE );
5114 if( hkdf->info_set )
5115 return( PSA_ERROR_BAD_STATE );
5116 hkdf->info_length = data_length;
5117 if( data_length != 0 )
5118 {
5119 hkdf->info = mbedtls_calloc( 1, data_length );
5120 if( hkdf->info == NULL )
5121 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5122 memcpy( hkdf->info, data, data_length );
5123 }
5124 hkdf->info_set = 1;
5125 return( PSA_SUCCESS );
5126 default:
5127 return( PSA_ERROR_INVALID_ARGUMENT );
5128 }
5129}
Janos Follathb03233e2019-06-11 15:30:30 +01005130
5131#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5132static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5133 psa_algorithm_t hash_alg,
5134 psa_key_derivation_step_t step,
5135 const uint8_t *data,
5136 size_t data_length )
5137{
5138 (void) prf;
5139 (void) hash_alg;
5140 (void) step;
5141 (void) data;
5142 (void) data_length;
5143
5144 return( PSA_ERROR_INVALID_ARGUMENT );
5145}
Janos Follath6660f0e2019-06-17 08:44:03 +01005146
5147static psa_status_t psa_tls12_prf_psk_to_ms_input(
5148 psa_tls12_prf_key_derivation_t *prf,
5149 psa_algorithm_t hash_alg,
5150 psa_key_derivation_step_t step,
5151 const uint8_t *data,
5152 size_t data_length )
5153{
5154 (void) prf;
5155 (void) hash_alg;
5156 (void) step;
5157 (void) data;
5158 (void) data_length;
5159
5160 return( PSA_ERROR_INVALID_ARGUMENT );
5161}
Janos Follathb03233e2019-06-11 15:30:30 +01005162#else
Janos Follathf08e2652019-06-13 09:05:41 +01005163static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5164 const uint8_t *data,
5165 size_t data_length )
5166{
5167 if( prf->state != TLS12_PRF_STATE_INIT )
5168 return( PSA_ERROR_BAD_STATE );
5169
Janos Follathd6dce9f2019-07-04 09:11:38 +01005170 if( data_length != 0 )
5171 {
5172 prf->seed = mbedtls_calloc( 1, data_length );
5173 if( prf->seed == NULL )
5174 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005175
Janos Follathd6dce9f2019-07-04 09:11:38 +01005176 memcpy( prf->seed, data, data_length );
5177 prf->seed_length = data_length;
5178 }
Janos Follathf08e2652019-06-13 09:05:41 +01005179
5180 prf->state = TLS12_PRF_STATE_SEED_SET;
5181
5182 return( PSA_SUCCESS );
5183}
5184
Janos Follath81550542019-06-13 14:26:34 +01005185static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5186 psa_algorithm_t hash_alg,
5187 const uint8_t *data,
5188 size_t data_length )
5189{
5190 psa_status_t status;
5191 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5192 return( PSA_ERROR_BAD_STATE );
5193
5194 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5195 if( status != PSA_SUCCESS )
5196 return( status );
5197
5198 prf->state = TLS12_PRF_STATE_KEY_SET;
5199
5200 return( PSA_SUCCESS );
5201}
5202
Janos Follath6660f0e2019-06-17 08:44:03 +01005203static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5204 psa_tls12_prf_key_derivation_t *prf,
5205 psa_algorithm_t hash_alg,
5206 const uint8_t *data,
5207 size_t data_length )
5208{
5209 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005210 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5211 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005212
5213 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5214 return( PSA_ERROR_INVALID_ARGUMENT );
5215
5216 /* Quoting RFC 4279, Section 2:
5217 *
5218 * The premaster secret is formed as follows: if the PSK is N octets
5219 * long, concatenate a uint16 with the value N, N zero octets, a second
5220 * uint16 with the value N, and the PSK itself.
5221 */
5222
Janos Follath40e13932019-06-26 13:22:29 +01005223 *cur++ = ( data_length >> 8 ) & 0xff;
5224 *cur++ = ( data_length >> 0 ) & 0xff;
5225 memset( cur, 0, data_length );
5226 cur += data_length;
5227 *cur++ = pms[0];
5228 *cur++ = pms[1];
5229 memcpy( cur, data, data_length );
5230 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005231
Janos Follath40e13932019-06-26 13:22:29 +01005232 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005233
5234 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5235 return( status );
5236}
5237
Janos Follath63028dd2019-06-13 09:15:47 +01005238static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5239 const uint8_t *data,
5240 size_t data_length )
5241{
5242 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5243 return( PSA_ERROR_BAD_STATE );
5244
Janos Follathd6dce9f2019-07-04 09:11:38 +01005245 if( data_length != 0 )
5246 {
5247 prf->label = mbedtls_calloc( 1, data_length );
5248 if( prf->label == NULL )
5249 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005250
Janos Follathd6dce9f2019-07-04 09:11:38 +01005251 memcpy( prf->label, data, data_length );
5252 prf->label_length = data_length;
5253 }
Janos Follath63028dd2019-06-13 09:15:47 +01005254
5255 prf->state = TLS12_PRF_STATE_LABEL_SET;
5256
5257 return( PSA_SUCCESS );
5258}
5259
Janos Follathb03233e2019-06-11 15:30:30 +01005260static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5261 psa_algorithm_t hash_alg,
5262 psa_key_derivation_step_t step,
5263 const uint8_t *data,
5264 size_t data_length )
5265{
Janos Follathb03233e2019-06-11 15:30:30 +01005266 switch( step )
5267 {
Janos Follathf08e2652019-06-13 09:05:41 +01005268 case PSA_KEY_DERIVATION_INPUT_SEED:
5269 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005270 case PSA_KEY_DERIVATION_INPUT_SECRET:
5271 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005272 case PSA_KEY_DERIVATION_INPUT_LABEL:
5273 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005274 default:
5275 return( PSA_ERROR_INVALID_ARGUMENT );
5276 }
5277}
Janos Follath6660f0e2019-06-17 08:44:03 +01005278
5279static psa_status_t psa_tls12_prf_psk_to_ms_input(
5280 psa_tls12_prf_key_derivation_t *prf,
5281 psa_algorithm_t hash_alg,
5282 psa_key_derivation_step_t step,
5283 const uint8_t *data,
5284 size_t data_length )
5285{
5286 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005287 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005288 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5289 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005290 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005291
5292 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5293}
Janos Follathb03233e2019-06-11 15:30:30 +01005294#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005295#endif /* MBEDTLS_MD_C */
5296
Janos Follathb80a94e2019-06-12 15:54:46 +01005297static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005298 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005299 psa_key_derivation_step_t step,
5300 const uint8_t *data,
5301 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005302{
5303 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005304 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005305
5306#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005307 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005308 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005309 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005310 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005311 step, data, data_length );
5312 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005313 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005314#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005315#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005316 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005317 {
Janos Follathb03233e2019-06-11 15:30:30 +01005318 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5319 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5320 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005321 }
5322 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5323 {
5324 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5325 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5326 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005327 }
5328 else
5329#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005330 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005331 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005332 return( PSA_ERROR_BAD_STATE );
5333 }
5334
5335 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005336 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005337 return( status );
5338}
5339
Janos Follath51f4a0f2019-06-14 11:35:55 +01005340psa_status_t psa_key_derivation_input_bytes(
5341 psa_key_derivation_operation_t *operation,
5342 psa_key_derivation_step_t step,
5343 const uint8_t *data,
5344 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005345{
Janos Follathc5621512019-06-14 11:27:57 +01005346 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5347 return( PSA_ERROR_INVALID_ARGUMENT );
5348
5349 return( psa_key_derivation_input_internal( operation, step,
5350 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005351}
5352
Janos Follath51f4a0f2019-06-14 11:35:55 +01005353psa_status_t psa_key_derivation_input_key(
5354 psa_key_derivation_operation_t *operation,
5355 psa_key_derivation_step_t step,
5356 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005357{
5358 psa_key_slot_t *slot;
5359 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005360 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005361 PSA_KEY_USAGE_DERIVE,
5362 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005363 if( status != PSA_SUCCESS )
5364 return( status );
5365 if( slot->type != PSA_KEY_TYPE_DERIVE )
5366 return( PSA_ERROR_INVALID_ARGUMENT );
5367 /* Don't allow a key to be used as an input that is usually public.
5368 * This is debatable. It's ok from a cryptographic perspective to
5369 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005370 * the material should be dedicated to a particular input step,
5371 * otherwise this may allow the key to be used in an unintended way
5372 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005373 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005374 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005375 return( psa_key_derivation_input_internal( operation,
5376 step,
5377 slot->data.raw.data,
5378 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005379}
5380
Gilles Peskineea0fb492018-07-12 17:17:20 +02005381
5382
5383/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005384/* Key agreement */
5385/****************************************************************/
5386
Gilles Peskinea05219c2018-11-16 16:02:56 +01005387#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005388static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5389 size_t peer_key_length,
5390 const mbedtls_ecp_keypair *our_key,
5391 uint8_t *shared_secret,
5392 size_t shared_secret_size,
5393 size_t *shared_secret_length )
5394{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005395 mbedtls_ecp_keypair *their_key = NULL;
5396 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005397 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005398 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005399
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005400 status = psa_import_ec_public_key(
5401 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5402 peer_key, peer_key_length,
5403 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005404 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005405 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005406
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005407 status = mbedtls_to_psa_error(
5408 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5409 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005410 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005411 status = mbedtls_to_psa_error(
5412 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5413 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005414 goto exit;
5415
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005416 status = mbedtls_to_psa_error(
5417 mbedtls_ecdh_calc_secret( &ecdh,
5418 shared_secret_length,
5419 shared_secret, shared_secret_size,
5420 mbedtls_ctr_drbg_random,
5421 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005422
5423exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005424 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005425 mbedtls_ecp_keypair_free( their_key );
5426 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005427 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005428}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005429#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005430
Gilles Peskine01d718c2018-09-18 12:01:02 +02005431#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5432
Gilles Peskine0216fe12019-04-11 21:23:21 +02005433static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5434 psa_key_slot_t *private_key,
5435 const uint8_t *peer_key,
5436 size_t peer_key_length,
5437 uint8_t *shared_secret,
5438 size_t shared_secret_size,
5439 size_t *shared_secret_length )
5440{
5441 switch( alg )
5442 {
5443#if defined(MBEDTLS_ECDH_C)
5444 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005445 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005446 return( PSA_ERROR_INVALID_ARGUMENT );
5447 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5448 private_key->data.ecp,
5449 shared_secret, shared_secret_size,
5450 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005451#endif /* MBEDTLS_ECDH_C */
5452 default:
5453 (void) private_key;
5454 (void) peer_key;
5455 (void) peer_key_length;
5456 (void) shared_secret;
5457 (void) shared_secret_size;
5458 (void) shared_secret_length;
5459 return( PSA_ERROR_NOT_SUPPORTED );
5460 }
5461}
5462
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005463/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005464 * to potentially free embedded data structures and wipe confidential data.
5465 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005466static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005467 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005468 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005469 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005470 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005471{
5472 psa_status_t status;
5473 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5474 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005475 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005476
5477 /* Step 1: run the secret agreement algorithm to generate the shared
5478 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005479 status = psa_key_agreement_raw_internal( ka_alg,
5480 private_key,
5481 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005482 shared_secret,
5483 sizeof( shared_secret ),
5484 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005485 if( status != PSA_SUCCESS )
5486 goto exit;
5487
5488 /* Step 2: set up the key derivation to generate key material from
5489 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005490 status = psa_key_derivation_input_internal( operation, step,
5491 shared_secret,
5492 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005493
Gilles Peskine01d718c2018-09-18 12:01:02 +02005494exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005495 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005496 return( status );
5497}
5498
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005499psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005500 psa_key_derivation_step_t step,
5501 psa_key_handle_t private_key,
5502 const uint8_t *peer_key,
5503 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005504{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005505 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005506 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005507 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005508 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005509 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005510 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005511 if( status != PSA_SUCCESS )
5512 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005513 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005514 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005515 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005516 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005517 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005518 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005519}
5520
Gilles Peskinebe697d82019-05-16 18:00:41 +02005521psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5522 psa_key_handle_t private_key,
5523 const uint8_t *peer_key,
5524 size_t peer_key_length,
5525 uint8_t *output,
5526 size_t output_size,
5527 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005528{
5529 psa_key_slot_t *slot;
5530 psa_status_t status;
5531
5532 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5533 {
5534 status = PSA_ERROR_INVALID_ARGUMENT;
5535 goto exit;
5536 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005537 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005538 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005539 if( status != PSA_SUCCESS )
5540 goto exit;
5541
5542 status = psa_key_agreement_raw_internal( alg, slot,
5543 peer_key, peer_key_length,
5544 output, output_size,
5545 output_length );
5546
5547exit:
5548 if( status != PSA_SUCCESS )
5549 {
5550 /* If an error happens and is not handled properly, the output
5551 * may be used as a key to protect sensitive data. Arrange for such
5552 * a key to be random, which is likely to result in decryption or
5553 * verification errors. This is better than filling the buffer with
5554 * some constant data such as zeros, which would result in the data
5555 * being protected with a reproducible, easily knowable key.
5556 */
5557 psa_generate_random( output, output_size );
5558 *output_length = output_size;
5559 }
5560 return( status );
5561}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005562
5563
5564/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005565/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005566/****************************************************************/
5567
5568psa_status_t psa_generate_random( uint8_t *output,
5569 size_t output_size )
5570{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005571 int ret;
5572 GUARD_MODULE_INITIALIZED;
5573
5574 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005575 return( mbedtls_to_psa_error( ret ) );
5576}
5577
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005578#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5579#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005580
Gilles Peskine7228da22019-07-15 11:06:15 +02005581psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005582 size_t seed_size )
5583{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005584 if( global_data.initialized )
5585 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005586
5587 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5588 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5589 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5590 return( PSA_ERROR_INVALID_ARGUMENT );
5591
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005592 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005593}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005594#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005595
Gilles Peskinee56e8782019-04-26 17:34:02 +02005596#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5597static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5598 size_t domain_parameters_size,
5599 int *exponent )
5600{
5601 size_t i;
5602 uint32_t acc = 0;
5603
5604 if( domain_parameters_size == 0 )
5605 {
5606 *exponent = 65537;
5607 return( PSA_SUCCESS );
5608 }
5609
5610 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5611 * support values that fit in a 32-bit integer, which is larger than
5612 * int on just about every platform anyway. */
5613 if( domain_parameters_size > sizeof( acc ) )
5614 return( PSA_ERROR_NOT_SUPPORTED );
5615 for( i = 0; i < domain_parameters_size; i++ )
5616 acc = ( acc << 8 ) | domain_parameters[i];
5617 if( acc > INT_MAX )
5618 return( PSA_ERROR_NOT_SUPPORTED );
5619 *exponent = acc;
5620 return( PSA_SUCCESS );
5621}
5622#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5623
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005624static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005625 psa_key_slot_t *slot, size_t bits,
5626 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005627{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005628 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005629
Gilles Peskinee56e8782019-04-26 17:34:02 +02005630 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005631 return( PSA_ERROR_INVALID_ARGUMENT );
5632
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005633 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005634 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005635 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005636 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005637 if( status != PSA_SUCCESS )
5638 return( status );
5639 status = psa_generate_random( slot->data.raw.data,
5640 slot->data.raw.bytes );
5641 if( status != PSA_SUCCESS )
5642 {
5643 mbedtls_free( slot->data.raw.data );
5644 return( status );
5645 }
5646#if defined(MBEDTLS_DES_C)
5647 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005648 psa_des_set_key_parity( slot->data.raw.data,
5649 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005650#endif /* MBEDTLS_DES_C */
5651 }
5652 else
5653
5654#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005655 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005656 {
5657 mbedtls_rsa_context *rsa;
5658 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005659 int exponent;
5660 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005661 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5662 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005663 /* Accept only byte-aligned keys, for the same reasons as
5664 * in psa_import_rsa_key(). */
5665 if( bits % 8 != 0 )
5666 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005667 status = psa_read_rsa_exponent( domain_parameters,
5668 domain_parameters_size,
5669 &exponent );
5670 if( status != PSA_SUCCESS )
5671 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005672 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5673 if( rsa == NULL )
5674 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5675 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5676 ret = mbedtls_rsa_gen_key( rsa,
5677 mbedtls_ctr_drbg_random,
5678 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005679 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005680 exponent );
5681 if( ret != 0 )
5682 {
5683 mbedtls_rsa_free( rsa );
5684 mbedtls_free( rsa );
5685 return( mbedtls_to_psa_error( ret ) );
5686 }
5687 slot->data.rsa = rsa;
5688 }
5689 else
5690#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5691
5692#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005693 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005694 {
5695 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5696 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5697 const mbedtls_ecp_curve_info *curve_info =
5698 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5699 mbedtls_ecp_keypair *ecp;
5700 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005701 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005702 return( PSA_ERROR_NOT_SUPPORTED );
5703 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5704 return( PSA_ERROR_NOT_SUPPORTED );
5705 if( curve_info->bit_size != bits )
5706 return( PSA_ERROR_INVALID_ARGUMENT );
5707 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5708 if( ecp == NULL )
5709 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5710 mbedtls_ecp_keypair_init( ecp );
5711 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5712 mbedtls_ctr_drbg_random,
5713 &global_data.ctr_drbg );
5714 if( ret != 0 )
5715 {
5716 mbedtls_ecp_keypair_free( ecp );
5717 mbedtls_free( ecp );
5718 return( mbedtls_to_psa_error( ret ) );
5719 }
5720 slot->data.ecp = ecp;
5721 }
5722 else
5723#endif /* MBEDTLS_ECP_C */
5724
5725 return( PSA_ERROR_NOT_SUPPORTED );
5726
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005727 return( PSA_SUCCESS );
5728}
5729
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005730psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005731 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005732{
5733 psa_status_t status;
5734 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005735 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005736 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005737#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5738 if( driver != NULL )
5739 {
5740 /* Generating a key in a secure element is not implemented yet. */
5741 status = PSA_ERROR_NOT_SUPPORTED;
5742 }
5743#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005744 if( status == PSA_SUCCESS )
5745 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005746 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005747 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005748 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005749 }
5750 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005751 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005752 if( status != PSA_SUCCESS )
5753 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005754 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005755 *handle = 0;
5756 }
5757 return( status );
5758}
5759
5760
Gilles Peskine05d69892018-06-19 22:00:52 +02005761
5762/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005763/* Module setup */
5764/****************************************************************/
5765
Gilles Peskine5e769522018-11-20 21:59:56 +01005766psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5767 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5768 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5769{
5770 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5771 return( PSA_ERROR_BAD_STATE );
5772 global_data.entropy_init = entropy_init;
5773 global_data.entropy_free = entropy_free;
5774 return( PSA_SUCCESS );
5775}
5776
Gilles Peskinee59236f2018-01-27 23:32:46 +01005777void mbedtls_psa_crypto_free( void )
5778{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005779 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005780 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5781 {
5782 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005783 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005784 }
5785 /* Wipe all remaining data, including configuration.
5786 * In particular, this sets all state indicator to the value
5787 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005788 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005789#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005790 /* Unregister all secure element drivers, so that we restart from
5791 * a pristine state. */
5792 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005793#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005794}
5795
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005796#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5797/** Recover a transaction that was interrupted by a power failure.
5798 *
5799 * This function is called during initialization, before psa_crypto_init()
5800 * returns. If this function returns a failure status, the initialization
5801 * fails.
5802 */
5803static psa_status_t psa_crypto_recover_transaction(
5804 const psa_crypto_transaction_t *transaction )
5805{
5806 switch( transaction->unknown.type )
5807 {
5808 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5809 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
5810 /* TOnogrepDO - fall through to the failure case until this
5811 * is implemented */
5812 default:
5813 /* We found an unsupported transaction in the storage.
5814 * We don't know what state the storage is in. Give up. */
5815 return( PSA_ERROR_STORAGE_FAILURE );
5816 }
5817}
5818#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5819
Gilles Peskinee59236f2018-01-27 23:32:46 +01005820psa_status_t psa_crypto_init( void )
5821{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005822 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005823 const unsigned char drbg_seed[] = "PSA";
5824
Gilles Peskinec6b69072018-11-20 21:42:52 +01005825 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005826 if( global_data.initialized != 0 )
5827 return( PSA_SUCCESS );
5828
Gilles Peskine5e769522018-11-20 21:59:56 +01005829 /* Set default configuration if
5830 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5831 if( global_data.entropy_init == NULL )
5832 global_data.entropy_init = mbedtls_entropy_init;
5833 if( global_data.entropy_free == NULL )
5834 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005835
Gilles Peskinec6b69072018-11-20 21:42:52 +01005836 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005837 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005838 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005839 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005840 status = mbedtls_to_psa_error(
5841 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5842 mbedtls_entropy_func,
5843 &global_data.entropy,
5844 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5845 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005846 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005847 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005848
Gilles Peskine66fb1262018-12-10 16:29:04 +01005849 status = psa_initialize_key_slots( );
5850 if( status != PSA_SUCCESS )
5851 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005852
Gilles Peskine4b734222019-07-24 15:56:31 +02005853#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005854 status = psa_crypto_load_transaction( );
5855 if( status == PSA_SUCCESS )
5856 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005857 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5858 if( status != PSA_SUCCESS )
5859 goto exit;
5860 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005861 }
5862 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5863 {
5864 /* There's no transaction to complete. It's all good. */
5865 status = PSA_SUCCESS;
5866 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005867#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005868
Gilles Peskinec6b69072018-11-20 21:42:52 +01005869 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005870 global_data.initialized = 1;
5871
Gilles Peskinee59236f2018-01-27 23:32:46 +01005872exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005873 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005874 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005875 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005876}
5877
5878#endif /* MBEDTLS_PSA_CRYPTO_C */