blob: b3a6f8a9a9df67d8db0a80900779609b1ea46b6b [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;
1089 attributes->type = type;
1090 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{
1156 attributes->id = slot->persistent_storage_id;
1157 attributes->lifetime = slot->lifetime;
1158 attributes->policy = slot->policy;
1159 attributes->type = slot->type;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001160 attributes->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
1457 status = psa_set_key_policy_internal( slot, &attributes->policy );
1458 if( status != PSA_SUCCESS )
1459 return( status );
1460 slot->lifetime = attributes->lifetime;
Gilles Peskine011e4282019-06-26 18:34:38 +02001461
Gilles Peskine4747d192019-04-17 15:05:45 +02001462 if( attributes->lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskined167b942019-04-19 18:19:40 +02001463 {
1464 status = psa_validate_persistent_key_parameters( attributes->lifetime,
Gilles Peskine011e4282019-06-26 18:34:38 +02001465 attributes->id,
1466 p_drv, 1 );
Gilles Peskined167b942019-04-19 18:19:40 +02001467 if( status != PSA_SUCCESS )
1468 return( status );
Gilles Peskine4747d192019-04-17 15:05:45 +02001469 slot->persistent_storage_id = attributes->id;
Gilles Peskined167b942019-04-19 18:19:40 +02001470 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001471 slot->type = attributes->type;
1472
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 {
1541 uint8_t *buffer = NULL;
1542 size_t buffer_size = 0;
Gilles Peskine1df83d42019-07-23 16:13:14 +02001543 size_t length = 0;
Gilles Peskine4747d192019-04-17 15:05:45 +02001544
Gilles Peskine1df83d42019-07-23 16:13:14 +02001545#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1546 if( driver != NULL )
1547 {
1548 buffer = (uint8_t*) &slot->data.se.slot_number;
1549 length = sizeof( slot->data.se.slot_number );
1550 }
1551 else
1552#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1553 {
1554 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type,
1555 psa_get_key_slot_bits( slot ) );
1556 buffer = mbedtls_calloc( 1, buffer_size );
1557 if( buffer == NULL && buffer_size != 0 )
1558 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1559 status = psa_internal_export_key( slot,
1560 buffer, buffer_size, &length,
1561 0 );
1562 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001563
1564 if( status == PSA_SUCCESS )
1565 {
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001566 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1567 psa_get_key_slot_attributes( slot, &attributes );
1568 status = psa_save_persistent_key( &attributes, buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001569 }
1570
Gilles Peskine1df83d42019-07-23 16:13:14 +02001571#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1572 if( driver == NULL )
1573#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1574 {
1575 if( buffer_size != 0 )
1576 mbedtls_platform_zeroize( buffer, buffer_size );
1577 mbedtls_free( buffer );
1578 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001579 }
1580#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1581
Gilles Peskinecbaff462019-07-12 23:46:04 +02001582#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1583 if( driver != NULL )
1584 {
1585 status = psa_save_se_persistent_data( driver );
1586 if( status != PSA_SUCCESS )
1587 {
1588 psa_destroy_persistent_key( slot->persistent_storage_id );
1589 return( status );
1590 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001591 status = psa_crypto_stop_transaction( );
1592 if( status != PSA_SUCCESS )
1593 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001594 }
1595#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1596
Gilles Peskine4747d192019-04-17 15:05:45 +02001597 return( status );
1598}
1599
1600/** Abort the creation of a key.
1601 *
1602 * You may call this function after calling psa_start_key_creation(),
1603 * or after psa_finish_key_creation() fails. In other circumstances, this
1604 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001605 * See the documentation of psa_start_key_creation() for the intended use
1606 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001607 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001608 * \param[in,out] slot Pointer to the slot with key material.
1609 * \param[in] driver The secure element driver for the key,
1610 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001611 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001612static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001613 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001614{
Gilles Peskine011e4282019-06-26 18:34:38 +02001615 (void) driver;
1616
Gilles Peskine4747d192019-04-17 15:05:45 +02001617 if( slot == NULL )
1618 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001619
1620#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1621 /* TOnogrepDO: If the key has already been created in the secure
1622 * element, and the failure happened later (when saving metadata
1623 * to internal storage), we need to destroy the key in the secure
1624 * element. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001625
1626 /* Abort the ongoing transaction if any. We already did what it
1627 * takes to undo any partial creation. All that's left is to update
1628 * the transaction data itself. */
1629 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001630#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1631
Gilles Peskine4747d192019-04-17 15:05:45 +02001632 psa_wipe_key_slot( slot );
1633}
1634
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001635static psa_status_t psa_check_key_slot_attributes(
1636 const psa_key_slot_t *slot,
1637 const psa_key_attributes_t *attributes )
1638{
1639 if( attributes->type != 0 )
1640 {
1641 if( attributes->type != slot->type )
1642 return( PSA_ERROR_INVALID_ARGUMENT );
1643 }
1644
1645 if( attributes->domain_parameters_size != 0 )
1646 {
1647#if defined(MBEDTLS_RSA_C)
1648 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
1649 {
1650 mbedtls_mpi actual, required;
1651 int ret;
1652 mbedtls_mpi_init( &actual );
1653 mbedtls_mpi_init( &required );
1654 ret = mbedtls_rsa_export( slot->data.rsa,
1655 NULL, NULL, NULL, NULL, &actual );
1656 if( ret != 0 )
1657 goto rsa_exit;
1658 ret = mbedtls_mpi_read_binary( &required,
1659 attributes->domain_parameters,
1660 attributes->domain_parameters_size );
1661 if( ret != 0 )
1662 goto rsa_exit;
1663 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1664 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1665 rsa_exit:
1666 mbedtls_mpi_free( &actual );
1667 mbedtls_mpi_free( &required );
1668 if( ret != 0)
1669 return( mbedtls_to_psa_error( ret ) );
1670 }
1671 else
1672#endif
1673 {
1674 return( PSA_ERROR_INVALID_ARGUMENT );
1675 }
1676 }
1677
1678 if( attributes->bits != 0 )
1679 {
1680 if( attributes->bits != psa_get_key_slot_bits( slot ) )
1681 return( PSA_ERROR_INVALID_ARGUMENT );
1682 }
1683
1684 return( PSA_SUCCESS );
1685}
1686
Gilles Peskine4747d192019-04-17 15:05:45 +02001687psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001688 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001689 size_t data_length,
1690 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001691{
1692 psa_status_t status;
1693 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001694 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001695
Gilles Peskine011e4282019-06-26 18:34:38 +02001696 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001697 if( status != PSA_SUCCESS )
1698 goto exit;
1699
Gilles Peskine5d309672019-07-12 23:47:28 +02001700#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1701 if( driver != NULL )
1702 {
1703 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
1704 if( drv->key_management == NULL ||
1705 drv->key_management->p_import == NULL )
1706 {
1707 status = PSA_ERROR_NOT_SUPPORTED;
1708 goto exit;
1709 }
1710 status = drv->key_management->p_import(
1711 psa_get_se_driver_context( driver ),
1712 slot->data.se.slot_number,
1713 slot->lifetime, slot->type, slot->policy.alg, slot->policy.usage,
1714 data, data_length );
1715 /* TOnogrepDO: psa_check_key_slot_attributes? */
1716 }
1717 else
1718#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1719 {
1720 status = psa_import_key_into_slot( slot, data, data_length );
1721 if( status != PSA_SUCCESS )
1722 goto exit;
1723 status = psa_check_key_slot_attributes( slot, attributes );
1724 if( status != PSA_SUCCESS )
1725 goto exit;
1726 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001727
Gilles Peskine011e4282019-06-26 18:34:38 +02001728 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001729exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001730 if( status != PSA_SUCCESS )
1731 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001732 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001733 *handle = 0;
1734 }
1735 return( status );
1736}
1737
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001738static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001739 psa_key_slot_t *target )
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001740{
1741 psa_status_t status;
1742 uint8_t *buffer = NULL;
1743 size_t buffer_size = 0;
1744 size_t length;
1745
1746 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001747 psa_get_key_slot_bits( source ) );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001748 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001749 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001750 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001751 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1752 if( status != PSA_SUCCESS )
1753 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001754 target->type = source->type;
1755 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001756
1757exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001758 if( buffer_size != 0 )
1759 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001760 mbedtls_free( buffer );
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001761 return( status );
1762}
1763
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001764psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1765 const psa_key_attributes_t *specified_attributes,
1766 psa_key_handle_t *target_handle )
1767{
1768 psa_status_t status;
1769 psa_key_slot_t *source_slot = NULL;
1770 psa_key_slot_t *target_slot = NULL;
1771 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001772 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001773
Gilles Peskine28f8f302019-07-24 13:30:31 +02001774 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001775 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001776 if( status != PSA_SUCCESS )
1777 goto exit;
1778
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001779 status = psa_check_key_slot_attributes( source_slot, specified_attributes );
1780 if( status != PSA_SUCCESS )
1781 goto exit;
1782
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001783 status = psa_restrict_key_policy( &actual_attributes.policy,
1784 &source_slot->policy );
1785 if( status != PSA_SUCCESS )
1786 goto exit;
1787
1788 status = psa_start_key_creation( &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001789 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001790 if( status != PSA_SUCCESS )
1791 goto exit;
1792
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001793#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1794 if( driver != NULL )
1795 {
1796 /* Copying to a secure element is not implemented yet. */
1797 status = PSA_ERROR_NOT_SUPPORTED;
1798 goto exit;
1799 }
1800#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1801
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001802 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001803 if( status != PSA_SUCCESS )
1804 goto exit;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001805
Gilles Peskine011e4282019-06-26 18:34:38 +02001806 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001807exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001808 if( status != PSA_SUCCESS )
1809 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001810 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001811 *target_handle = 0;
1812 }
1813 return( status );
1814}
1815
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001816
1817
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001818/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001819/* Message digests */
1820/****************************************************************/
1821
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001822static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001823{
1824 switch( alg )
1825 {
1826#if defined(MBEDTLS_MD2_C)
1827 case PSA_ALG_MD2:
1828 return( &mbedtls_md2_info );
1829#endif
1830#if defined(MBEDTLS_MD4_C)
1831 case PSA_ALG_MD4:
1832 return( &mbedtls_md4_info );
1833#endif
1834#if defined(MBEDTLS_MD5_C)
1835 case PSA_ALG_MD5:
1836 return( &mbedtls_md5_info );
1837#endif
1838#if defined(MBEDTLS_RIPEMD160_C)
1839 case PSA_ALG_RIPEMD160:
1840 return( &mbedtls_ripemd160_info );
1841#endif
1842#if defined(MBEDTLS_SHA1_C)
1843 case PSA_ALG_SHA_1:
1844 return( &mbedtls_sha1_info );
1845#endif
1846#if defined(MBEDTLS_SHA256_C)
1847 case PSA_ALG_SHA_224:
1848 return( &mbedtls_sha224_info );
1849 case PSA_ALG_SHA_256:
1850 return( &mbedtls_sha256_info );
1851#endif
1852#if defined(MBEDTLS_SHA512_C)
1853 case PSA_ALG_SHA_384:
1854 return( &mbedtls_sha384_info );
1855 case PSA_ALG_SHA_512:
1856 return( &mbedtls_sha512_info );
1857#endif
1858 default:
1859 return( NULL );
1860 }
1861}
1862
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001863psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1864{
1865 switch( operation->alg )
1866 {
Gilles Peskine81736312018-06-26 15:04:31 +02001867 case 0:
1868 /* The object has (apparently) been initialized but it is not
1869 * in use. It's ok to call abort on such an object, and there's
1870 * nothing to do. */
1871 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001872#if defined(MBEDTLS_MD2_C)
1873 case PSA_ALG_MD2:
1874 mbedtls_md2_free( &operation->ctx.md2 );
1875 break;
1876#endif
1877#if defined(MBEDTLS_MD4_C)
1878 case PSA_ALG_MD4:
1879 mbedtls_md4_free( &operation->ctx.md4 );
1880 break;
1881#endif
1882#if defined(MBEDTLS_MD5_C)
1883 case PSA_ALG_MD5:
1884 mbedtls_md5_free( &operation->ctx.md5 );
1885 break;
1886#endif
1887#if defined(MBEDTLS_RIPEMD160_C)
1888 case PSA_ALG_RIPEMD160:
1889 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1890 break;
1891#endif
1892#if defined(MBEDTLS_SHA1_C)
1893 case PSA_ALG_SHA_1:
1894 mbedtls_sha1_free( &operation->ctx.sha1 );
1895 break;
1896#endif
1897#if defined(MBEDTLS_SHA256_C)
1898 case PSA_ALG_SHA_224:
1899 case PSA_ALG_SHA_256:
1900 mbedtls_sha256_free( &operation->ctx.sha256 );
1901 break;
1902#endif
1903#if defined(MBEDTLS_SHA512_C)
1904 case PSA_ALG_SHA_384:
1905 case PSA_ALG_SHA_512:
1906 mbedtls_sha512_free( &operation->ctx.sha512 );
1907 break;
1908#endif
1909 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001910 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001911 }
1912 operation->alg = 0;
1913 return( PSA_SUCCESS );
1914}
1915
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001916psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001917 psa_algorithm_t alg )
1918{
1919 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001920
1921 /* A context must be freshly initialized before it can be set up. */
1922 if( operation->alg != 0 )
1923 {
1924 return( PSA_ERROR_BAD_STATE );
1925 }
1926
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001927 switch( alg )
1928 {
1929#if defined(MBEDTLS_MD2_C)
1930 case PSA_ALG_MD2:
1931 mbedtls_md2_init( &operation->ctx.md2 );
1932 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1933 break;
1934#endif
1935#if defined(MBEDTLS_MD4_C)
1936 case PSA_ALG_MD4:
1937 mbedtls_md4_init( &operation->ctx.md4 );
1938 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1939 break;
1940#endif
1941#if defined(MBEDTLS_MD5_C)
1942 case PSA_ALG_MD5:
1943 mbedtls_md5_init( &operation->ctx.md5 );
1944 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1945 break;
1946#endif
1947#if defined(MBEDTLS_RIPEMD160_C)
1948 case PSA_ALG_RIPEMD160:
1949 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1950 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1951 break;
1952#endif
1953#if defined(MBEDTLS_SHA1_C)
1954 case PSA_ALG_SHA_1:
1955 mbedtls_sha1_init( &operation->ctx.sha1 );
1956 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1957 break;
1958#endif
1959#if defined(MBEDTLS_SHA256_C)
1960 case PSA_ALG_SHA_224:
1961 mbedtls_sha256_init( &operation->ctx.sha256 );
1962 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1963 break;
1964 case PSA_ALG_SHA_256:
1965 mbedtls_sha256_init( &operation->ctx.sha256 );
1966 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1967 break;
1968#endif
1969#if defined(MBEDTLS_SHA512_C)
1970 case PSA_ALG_SHA_384:
1971 mbedtls_sha512_init( &operation->ctx.sha512 );
1972 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1973 break;
1974 case PSA_ALG_SHA_512:
1975 mbedtls_sha512_init( &operation->ctx.sha512 );
1976 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1977 break;
1978#endif
1979 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001980 return( PSA_ALG_IS_HASH( alg ) ?
1981 PSA_ERROR_NOT_SUPPORTED :
1982 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001983 }
1984 if( ret == 0 )
1985 operation->alg = alg;
1986 else
1987 psa_hash_abort( operation );
1988 return( mbedtls_to_psa_error( ret ) );
1989}
1990
1991psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1992 const uint8_t *input,
1993 size_t input_length )
1994{
1995 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001996
1997 /* Don't require hash implementations to behave correctly on a
1998 * zero-length input, which may have an invalid pointer. */
1999 if( input_length == 0 )
2000 return( PSA_SUCCESS );
2001
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002002 switch( operation->alg )
2003 {
2004#if defined(MBEDTLS_MD2_C)
2005 case PSA_ALG_MD2:
2006 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2007 input, input_length );
2008 break;
2009#endif
2010#if defined(MBEDTLS_MD4_C)
2011 case PSA_ALG_MD4:
2012 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2013 input, input_length );
2014 break;
2015#endif
2016#if defined(MBEDTLS_MD5_C)
2017 case PSA_ALG_MD5:
2018 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2019 input, input_length );
2020 break;
2021#endif
2022#if defined(MBEDTLS_RIPEMD160_C)
2023 case PSA_ALG_RIPEMD160:
2024 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2025 input, input_length );
2026 break;
2027#endif
2028#if defined(MBEDTLS_SHA1_C)
2029 case PSA_ALG_SHA_1:
2030 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2031 input, input_length );
2032 break;
2033#endif
2034#if defined(MBEDTLS_SHA256_C)
2035 case PSA_ALG_SHA_224:
2036 case PSA_ALG_SHA_256:
2037 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2038 input, input_length );
2039 break;
2040#endif
2041#if defined(MBEDTLS_SHA512_C)
2042 case PSA_ALG_SHA_384:
2043 case PSA_ALG_SHA_512:
2044 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2045 input, input_length );
2046 break;
2047#endif
2048 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002049 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002050 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002051
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002052 if( ret != 0 )
2053 psa_hash_abort( operation );
2054 return( mbedtls_to_psa_error( ret ) );
2055}
2056
2057psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2058 uint8_t *hash,
2059 size_t hash_size,
2060 size_t *hash_length )
2061{
itayzafrir40835d42018-08-02 13:14:17 +03002062 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002063 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002064 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002065
2066 /* Fill the output buffer with something that isn't a valid hash
2067 * (barring an attack on the hash and deliberately-crafted input),
2068 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002069 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002070 /* If hash_size is 0 then hash may be NULL and then the
2071 * call to memset would have undefined behavior. */
2072 if( hash_size != 0 )
2073 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002074
2075 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002076 {
2077 status = PSA_ERROR_BUFFER_TOO_SMALL;
2078 goto exit;
2079 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002080
2081 switch( operation->alg )
2082 {
2083#if defined(MBEDTLS_MD2_C)
2084 case PSA_ALG_MD2:
2085 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2086 break;
2087#endif
2088#if defined(MBEDTLS_MD4_C)
2089 case PSA_ALG_MD4:
2090 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2091 break;
2092#endif
2093#if defined(MBEDTLS_MD5_C)
2094 case PSA_ALG_MD5:
2095 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2096 break;
2097#endif
2098#if defined(MBEDTLS_RIPEMD160_C)
2099 case PSA_ALG_RIPEMD160:
2100 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2101 break;
2102#endif
2103#if defined(MBEDTLS_SHA1_C)
2104 case PSA_ALG_SHA_1:
2105 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2106 break;
2107#endif
2108#if defined(MBEDTLS_SHA256_C)
2109 case PSA_ALG_SHA_224:
2110 case PSA_ALG_SHA_256:
2111 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2112 break;
2113#endif
2114#if defined(MBEDTLS_SHA512_C)
2115 case PSA_ALG_SHA_384:
2116 case PSA_ALG_SHA_512:
2117 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2118 break;
2119#endif
2120 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002121 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002122 }
itayzafrir40835d42018-08-02 13:14:17 +03002123 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002124
itayzafrir40835d42018-08-02 13:14:17 +03002125exit:
2126 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002127 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002128 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002129 return( psa_hash_abort( operation ) );
2130 }
2131 else
2132 {
2133 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002134 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002135 }
2136}
2137
Gilles Peskine2d277862018-06-18 15:41:12 +02002138psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2139 const uint8_t *hash,
2140 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002141{
2142 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2143 size_t actual_hash_length;
2144 psa_status_t status = psa_hash_finish( operation,
2145 actual_hash, sizeof( actual_hash ),
2146 &actual_hash_length );
2147 if( status != PSA_SUCCESS )
2148 return( status );
2149 if( actual_hash_length != hash_length )
2150 return( PSA_ERROR_INVALID_SIGNATURE );
2151 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2152 return( PSA_ERROR_INVALID_SIGNATURE );
2153 return( PSA_SUCCESS );
2154}
2155
Gilles Peskineeb35d782019-01-22 17:56:16 +01002156psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2157 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002158{
2159 if( target_operation->alg != 0 )
2160 return( PSA_ERROR_BAD_STATE );
2161
2162 switch( source_operation->alg )
2163 {
2164 case 0:
2165 return( PSA_ERROR_BAD_STATE );
2166#if defined(MBEDTLS_MD2_C)
2167 case PSA_ALG_MD2:
2168 mbedtls_md2_clone( &target_operation->ctx.md2,
2169 &source_operation->ctx.md2 );
2170 break;
2171#endif
2172#if defined(MBEDTLS_MD4_C)
2173 case PSA_ALG_MD4:
2174 mbedtls_md4_clone( &target_operation->ctx.md4,
2175 &source_operation->ctx.md4 );
2176 break;
2177#endif
2178#if defined(MBEDTLS_MD5_C)
2179 case PSA_ALG_MD5:
2180 mbedtls_md5_clone( &target_operation->ctx.md5,
2181 &source_operation->ctx.md5 );
2182 break;
2183#endif
2184#if defined(MBEDTLS_RIPEMD160_C)
2185 case PSA_ALG_RIPEMD160:
2186 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2187 &source_operation->ctx.ripemd160 );
2188 break;
2189#endif
2190#if defined(MBEDTLS_SHA1_C)
2191 case PSA_ALG_SHA_1:
2192 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2193 &source_operation->ctx.sha1 );
2194 break;
2195#endif
2196#if defined(MBEDTLS_SHA256_C)
2197 case PSA_ALG_SHA_224:
2198 case PSA_ALG_SHA_256:
2199 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2200 &source_operation->ctx.sha256 );
2201 break;
2202#endif
2203#if defined(MBEDTLS_SHA512_C)
2204 case PSA_ALG_SHA_384:
2205 case PSA_ALG_SHA_512:
2206 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2207 &source_operation->ctx.sha512 );
2208 break;
2209#endif
2210 default:
2211 return( PSA_ERROR_NOT_SUPPORTED );
2212 }
2213
2214 target_operation->alg = source_operation->alg;
2215 return( PSA_SUCCESS );
2216}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002217
2218
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002219/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002220/* MAC */
2221/****************************************************************/
2222
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002223static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002224 psa_algorithm_t alg,
2225 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002226 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002227 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002228{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002229 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002230 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002231
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002232 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002233 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002234
Gilles Peskine8c9def32018-02-08 10:02:12 +01002235 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2236 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002237 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002238 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002239 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002240 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002241 mode = MBEDTLS_MODE_STREAM;
2242 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002243 case PSA_ALG_CTR:
2244 mode = MBEDTLS_MODE_CTR;
2245 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002246 case PSA_ALG_CFB:
2247 mode = MBEDTLS_MODE_CFB;
2248 break;
2249 case PSA_ALG_OFB:
2250 mode = MBEDTLS_MODE_OFB;
2251 break;
2252 case PSA_ALG_CBC_NO_PADDING:
2253 mode = MBEDTLS_MODE_CBC;
2254 break;
2255 case PSA_ALG_CBC_PKCS7:
2256 mode = MBEDTLS_MODE_CBC;
2257 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002258 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002259 mode = MBEDTLS_MODE_CCM;
2260 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002261 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002262 mode = MBEDTLS_MODE_GCM;
2263 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002264 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2265 mode = MBEDTLS_MODE_CHACHAPOLY;
2266 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002267 default:
2268 return( NULL );
2269 }
2270 }
2271 else if( alg == PSA_ALG_CMAC )
2272 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002273 else
2274 return( NULL );
2275
2276 switch( key_type )
2277 {
2278 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002279 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002280 break;
2281 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002282 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2283 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002284 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002285 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002286 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002287 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002288 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2289 * but two-key Triple-DES is functionally three-key Triple-DES
2290 * with K1=K3, so that's how we present it to mbedtls. */
2291 if( key_bits == 128 )
2292 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002293 break;
2294 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002295 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002296 break;
2297 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002298 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002299 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002300 case PSA_KEY_TYPE_CHACHA20:
2301 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2302 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002303 default:
2304 return( NULL );
2305 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002306 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002307 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002308
Jaeden Amero23bbb752018-06-26 14:16:54 +01002309 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2310 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002311}
2312
Gilles Peskinea05219c2018-11-16 16:02:56 +01002313#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002314static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002315{
Gilles Peskine2d277862018-06-18 15:41:12 +02002316 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002317 {
2318 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002319 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002320 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002321 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002322 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002323 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002324 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002325 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002326 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002327 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002328 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002329 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002330 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002331 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002332 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002333 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002334 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002335 return( 128 );
2336 default:
2337 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002338 }
2339}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002340#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002341
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002342/* Initialize the MAC operation structure. Once this function has been
2343 * called, psa_mac_abort can run and will do the right thing. */
2344static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2345 psa_algorithm_t alg )
2346{
2347 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2348
2349 operation->alg = alg;
2350 operation->key_set = 0;
2351 operation->iv_set = 0;
2352 operation->iv_required = 0;
2353 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002354 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002355
2356#if defined(MBEDTLS_CMAC_C)
2357 if( alg == PSA_ALG_CMAC )
2358 {
2359 operation->iv_required = 0;
2360 mbedtls_cipher_init( &operation->ctx.cmac );
2361 status = PSA_SUCCESS;
2362 }
2363 else
2364#endif /* MBEDTLS_CMAC_C */
2365#if defined(MBEDTLS_MD_C)
2366 if( PSA_ALG_IS_HMAC( operation->alg ) )
2367 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002368 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2369 operation->ctx.hmac.hash_ctx.alg = 0;
2370 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002371 }
2372 else
2373#endif /* MBEDTLS_MD_C */
2374 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002375 if( ! PSA_ALG_IS_MAC( alg ) )
2376 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002377 }
2378
2379 if( status != PSA_SUCCESS )
2380 memset( operation, 0, sizeof( *operation ) );
2381 return( status );
2382}
2383
Gilles Peskine01126fa2018-07-12 17:04:55 +02002384#if defined(MBEDTLS_MD_C)
2385static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2386{
Gilles Peskine3f108122018-12-07 18:14:53 +01002387 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002388 return( psa_hash_abort( &hmac->hash_ctx ) );
2389}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002390
Janos Follath999f6482019-06-11 12:04:10 +01002391#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Hanno Beckerc8a41d72018-10-09 17:33:01 +01002392static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
2393{
2394 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
2395 memset( hmac, 0, sizeof( *hmac ) );
2396}
Janos Follath999f6482019-06-11 12:04:10 +01002397#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002398#endif /* MBEDTLS_MD_C */
2399
Gilles Peskine8c9def32018-02-08 10:02:12 +01002400psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2401{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002402 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002403 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002404 /* The object has (apparently) been initialized but it is not
2405 * in use. It's ok to call abort on such an object, and there's
2406 * nothing to do. */
2407 return( PSA_SUCCESS );
2408 }
2409 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002410#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002411 if( operation->alg == PSA_ALG_CMAC )
2412 {
2413 mbedtls_cipher_free( &operation->ctx.cmac );
2414 }
2415 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002416#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002417#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002418 if( PSA_ALG_IS_HMAC( operation->alg ) )
2419 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002420 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002421 }
2422 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002423#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002424 {
2425 /* Sanity check (shouldn't happen: operation->alg should
2426 * always have been initialized to a valid value). */
2427 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002428 }
Moran Peker41deec42018-04-04 15:43:05 +03002429
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430 operation->alg = 0;
2431 operation->key_set = 0;
2432 operation->iv_set = 0;
2433 operation->iv_required = 0;
2434 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002435 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002436
Gilles Peskine8c9def32018-02-08 10:02:12 +01002437 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002438
2439bad_state:
2440 /* If abort is called on an uninitialized object, we can't trust
2441 * anything. Wipe the object in case it contains confidential data.
2442 * This may result in a memory leak if a pointer gets overwritten,
2443 * but it's too late to do anything about this. */
2444 memset( operation, 0, sizeof( *operation ) );
2445 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002446}
2447
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002448#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002449static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002450 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002451 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002452 const mbedtls_cipher_info_t *cipher_info )
2453{
2454 int ret;
2455
2456 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002457
2458 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2459 if( ret != 0 )
2460 return( ret );
2461
2462 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2463 slot->data.raw.data,
2464 key_bits );
2465 return( ret );
2466}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002467#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002468
Gilles Peskine248051a2018-06-20 16:09:38 +02002469#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002470static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2471 const uint8_t *key,
2472 size_t key_length,
2473 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002474{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002475 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002476 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002477 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002478 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002479 psa_status_t status;
2480
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002481 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2482 * overflow below. This should never trigger if the hash algorithm
2483 * is implemented correctly. */
2484 /* The size checks against the ipad and opad buffers cannot be written
2485 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2486 * because that triggers -Wlogical-op on GCC 7.3. */
2487 if( block_size > sizeof( ipad ) )
2488 return( PSA_ERROR_NOT_SUPPORTED );
2489 if( block_size > sizeof( hmac->opad ) )
2490 return( PSA_ERROR_NOT_SUPPORTED );
2491 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002492 return( PSA_ERROR_NOT_SUPPORTED );
2493
Gilles Peskined223b522018-06-11 18:12:58 +02002494 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002495 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002496 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002497 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002498 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002499 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002500 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002501 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002502 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002503 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002504 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002505 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002506 }
Gilles Peskine96889972018-07-12 17:07:03 +02002507 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2508 * but it is permitted. It is common when HMAC is used in HKDF, for
2509 * example. Don't call `memcpy` in the 0-length because `key` could be
2510 * an invalid pointer which would make the behavior undefined. */
2511 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002512 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002513
Gilles Peskined223b522018-06-11 18:12:58 +02002514 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2515 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002516 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002517 ipad[i] ^= 0x36;
2518 memset( ipad + key_length, 0x36, block_size - key_length );
2519
2520 /* Copy the key material from ipad to opad, flipping the requisite bits,
2521 * and filling the rest of opad with the requisite constant. */
2522 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002523 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2524 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002525
Gilles Peskine01126fa2018-07-12 17:04:55 +02002526 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002527 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002528 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002529
Gilles Peskine01126fa2018-07-12 17:04:55 +02002530 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002531
2532cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01002533 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002534
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002535 return( status );
2536}
Gilles Peskine248051a2018-06-20 16:09:38 +02002537#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002538
Gilles Peskine89167cb2018-07-08 20:12:23 +02002539static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002540 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002541 psa_algorithm_t alg,
2542 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002543{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002544 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002545 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002547 psa_key_usage_t usage =
2548 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002549 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002550 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002551
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002552 /* A context must be freshly initialized before it can be set up. */
2553 if( operation->alg != 0 )
2554 {
2555 return( PSA_ERROR_BAD_STATE );
2556 }
2557
Gilles Peskined911eb72018-08-14 15:18:45 +02002558 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002559 if( status != PSA_SUCCESS )
2560 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002561 if( is_sign )
2562 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002563
Gilles Peskine28f8f302019-07-24 13:30:31 +02002564 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002565 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002566 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002567 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002568
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002570 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002571 {
2572 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002573 mbedtls_cipher_info_from_psa( full_length_alg,
2574 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002575 int ret;
2576 if( cipher_info == NULL )
2577 {
2578 status = PSA_ERROR_NOT_SUPPORTED;
2579 goto exit;
2580 }
2581 operation->mac_size = cipher_info->block_size;
2582 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2583 status = mbedtls_to_psa_error( ret );
2584 }
2585 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002586#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002587#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002588 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002589 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002590 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002591 if( hash_alg == 0 )
2592 {
2593 status = PSA_ERROR_NOT_SUPPORTED;
2594 goto exit;
2595 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002596
2597 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2598 /* Sanity check. This shouldn't fail on a valid configuration. */
2599 if( operation->mac_size == 0 ||
2600 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2601 {
2602 status = PSA_ERROR_NOT_SUPPORTED;
2603 goto exit;
2604 }
2605
Gilles Peskine01126fa2018-07-12 17:04:55 +02002606 if( slot->type != PSA_KEY_TYPE_HMAC )
2607 {
2608 status = PSA_ERROR_INVALID_ARGUMENT;
2609 goto exit;
2610 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002611
Gilles Peskine01126fa2018-07-12 17:04:55 +02002612 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2613 slot->data.raw.data,
2614 slot->data.raw.bytes,
2615 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002616 }
2617 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002618#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002619 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002620 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002621 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002622 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002623
Gilles Peskined911eb72018-08-14 15:18:45 +02002624 if( truncated == 0 )
2625 {
2626 /* The "normal" case: untruncated algorithm. Nothing to do. */
2627 }
2628 else if( truncated < 4 )
2629 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002630 /* A very short MAC is too short for security since it can be
2631 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2632 * so we make this our minimum, even though 32 bits is still
2633 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002634 status = PSA_ERROR_NOT_SUPPORTED;
2635 }
2636 else if( truncated > operation->mac_size )
2637 {
2638 /* It's impossible to "truncate" to a larger length. */
2639 status = PSA_ERROR_INVALID_ARGUMENT;
2640 }
2641 else
2642 operation->mac_size = truncated;
2643
Gilles Peskinefbfac682018-07-08 20:51:54 +02002644exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002645 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002646 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002647 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002648 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002649 else
2650 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002651 operation->key_set = 1;
2652 }
2653 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002654}
2655
Gilles Peskine89167cb2018-07-08 20:12:23 +02002656psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002657 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002658 psa_algorithm_t alg )
2659{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002660 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002661}
2662
2663psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002664 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002665 psa_algorithm_t alg )
2666{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002667 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002668}
2669
Gilles Peskine8c9def32018-02-08 10:02:12 +01002670psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2671 const uint8_t *input,
2672 size_t input_length )
2673{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002674 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002675 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002676 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002677 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002678 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002679 operation->has_input = 1;
2680
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002682 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002683 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002684 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2685 input, input_length );
2686 status = mbedtls_to_psa_error( ret );
2687 }
2688 else
2689#endif /* MBEDTLS_CMAC_C */
2690#if defined(MBEDTLS_MD_C)
2691 if( PSA_ALG_IS_HMAC( operation->alg ) )
2692 {
2693 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2694 input_length );
2695 }
2696 else
2697#endif /* MBEDTLS_MD_C */
2698 {
2699 /* This shouldn't happen if `operation` was initialized by
2700 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002701 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002702 }
2703
Gilles Peskinefbfac682018-07-08 20:51:54 +02002704 if( status != PSA_SUCCESS )
2705 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002706 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002707}
2708
Gilles Peskine01126fa2018-07-12 17:04:55 +02002709#if defined(MBEDTLS_MD_C)
2710static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2711 uint8_t *mac,
2712 size_t mac_size )
2713{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002714 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002715 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2716 size_t hash_size = 0;
2717 size_t block_size = psa_get_hash_block_size( hash_alg );
2718 psa_status_t status;
2719
Gilles Peskine01126fa2018-07-12 17:04:55 +02002720 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2721 if( status != PSA_SUCCESS )
2722 return( status );
2723 /* From here on, tmp needs to be wiped. */
2724
2725 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2726 if( status != PSA_SUCCESS )
2727 goto exit;
2728
2729 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2730 if( status != PSA_SUCCESS )
2731 goto exit;
2732
2733 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2734 if( status != PSA_SUCCESS )
2735 goto exit;
2736
Gilles Peskined911eb72018-08-14 15:18:45 +02002737 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2738 if( status != PSA_SUCCESS )
2739 goto exit;
2740
2741 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002742
2743exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002744 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002745 return( status );
2746}
2747#endif /* MBEDTLS_MD_C */
2748
mohammad16036df908f2018-04-02 08:34:15 -07002749static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002750 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002751 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002753 if( ! operation->key_set )
2754 return( PSA_ERROR_BAD_STATE );
2755 if( operation->iv_required && ! operation->iv_set )
2756 return( PSA_ERROR_BAD_STATE );
2757
Gilles Peskine8c9def32018-02-08 10:02:12 +01002758 if( mac_size < operation->mac_size )
2759 return( PSA_ERROR_BUFFER_TOO_SMALL );
2760
Gilles Peskine8c9def32018-02-08 10:02:12 +01002761#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002762 if( operation->alg == PSA_ALG_CMAC )
2763 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002764 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2765 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2766 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002767 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002768 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002769 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002770 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002771 else
2772#endif /* MBEDTLS_CMAC_C */
2773#if defined(MBEDTLS_MD_C)
2774 if( PSA_ALG_IS_HMAC( operation->alg ) )
2775 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002776 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002777 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002778 }
2779 else
2780#endif /* MBEDTLS_MD_C */
2781 {
2782 /* This shouldn't happen if `operation` was initialized by
2783 * a setup function. */
2784 return( PSA_ERROR_BAD_STATE );
2785 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002786}
2787
Gilles Peskineacd4be32018-07-08 19:56:25 +02002788psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2789 uint8_t *mac,
2790 size_t mac_size,
2791 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002792{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002793 psa_status_t status;
2794
Jaeden Amero252ef282019-02-15 14:05:35 +00002795 if( operation->alg == 0 )
2796 {
2797 return( PSA_ERROR_BAD_STATE );
2798 }
2799
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002800 /* Fill the output buffer with something that isn't a valid mac
2801 * (barring an attack on the mac and deliberately-crafted input),
2802 * in case the caller doesn't check the return status properly. */
2803 *mac_length = mac_size;
2804 /* If mac_size is 0 then mac may be NULL and then the
2805 * call to memset would have undefined behavior. */
2806 if( mac_size != 0 )
2807 memset( mac, '!', mac_size );
2808
Gilles Peskine89167cb2018-07-08 20:12:23 +02002809 if( ! operation->is_sign )
2810 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002811 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002812 }
mohammad16036df908f2018-04-02 08:34:15 -07002813
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002814 status = psa_mac_finish_internal( operation, mac, mac_size );
2815
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002816 if( status == PSA_SUCCESS )
2817 {
2818 status = psa_mac_abort( operation );
2819 if( status == PSA_SUCCESS )
2820 *mac_length = operation->mac_size;
2821 else
2822 memset( mac, '!', mac_size );
2823 }
2824 else
2825 psa_mac_abort( operation );
2826 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002827}
2828
Gilles Peskineacd4be32018-07-08 19:56:25 +02002829psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2830 const uint8_t *mac,
2831 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002832{
Gilles Peskine828ed142018-06-18 23:25:51 +02002833 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002834 psa_status_t status;
2835
Jaeden Amero252ef282019-02-15 14:05:35 +00002836 if( operation->alg == 0 )
2837 {
2838 return( PSA_ERROR_BAD_STATE );
2839 }
2840
Gilles Peskine89167cb2018-07-08 20:12:23 +02002841 if( operation->is_sign )
2842 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002843 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002844 }
2845 if( operation->mac_size != mac_length )
2846 {
2847 status = PSA_ERROR_INVALID_SIGNATURE;
2848 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002849 }
mohammad16036df908f2018-04-02 08:34:15 -07002850
2851 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002852 actual_mac, sizeof( actual_mac ) );
2853
2854 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2855 status = PSA_ERROR_INVALID_SIGNATURE;
2856
2857cleanup:
2858 if( status == PSA_SUCCESS )
2859 status = psa_mac_abort( operation );
2860 else
2861 psa_mac_abort( operation );
2862
Gilles Peskine3f108122018-12-07 18:14:53 +01002863 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002864
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002865 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002866}
2867
2868
Gilles Peskine20035e32018-02-03 22:44:14 +01002869
Gilles Peskine20035e32018-02-03 22:44:14 +01002870/****************************************************************/
2871/* Asymmetric cryptography */
2872/****************************************************************/
2873
Gilles Peskine2b450e32018-06-27 15:42:46 +02002874#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002875/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002876 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002877static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2878 size_t hash_length,
2879 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002880{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002881 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002882 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002883 *md_alg = mbedtls_md_get_type( md_info );
2884
2885 /* The Mbed TLS RSA module uses an unsigned int for hash length
2886 * parameters. Validate that it fits so that we don't risk an
2887 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002888#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002889 if( hash_length > UINT_MAX )
2890 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002891#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002892
2893#if defined(MBEDTLS_PKCS1_V15)
2894 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2895 * must be correct. */
2896 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2897 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002898 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002899 if( md_info == NULL )
2900 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002901 if( mbedtls_md_get_size( md_info ) != hash_length )
2902 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002903 }
2904#endif /* MBEDTLS_PKCS1_V15 */
2905
2906#if defined(MBEDTLS_PKCS1_V21)
2907 /* PSS requires a hash internally. */
2908 if( PSA_ALG_IS_RSA_PSS( alg ) )
2909 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002910 if( md_info == NULL )
2911 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002912 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002913#endif /* MBEDTLS_PKCS1_V21 */
2914
Gilles Peskine61b91d42018-06-08 16:09:36 +02002915 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002916}
2917
Gilles Peskine2b450e32018-06-27 15:42:46 +02002918static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2919 psa_algorithm_t alg,
2920 const uint8_t *hash,
2921 size_t hash_length,
2922 uint8_t *signature,
2923 size_t signature_size,
2924 size_t *signature_length )
2925{
2926 psa_status_t status;
2927 int ret;
2928 mbedtls_md_type_t md_alg;
2929
2930 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2931 if( status != PSA_SUCCESS )
2932 return( status );
2933
Gilles Peskine630a18a2018-06-29 17:49:35 +02002934 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002935 return( PSA_ERROR_BUFFER_TOO_SMALL );
2936
2937#if defined(MBEDTLS_PKCS1_V15)
2938 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2939 {
2940 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2941 MBEDTLS_MD_NONE );
2942 ret = mbedtls_rsa_pkcs1_sign( rsa,
2943 mbedtls_ctr_drbg_random,
2944 &global_data.ctr_drbg,
2945 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002946 md_alg,
2947 (unsigned int) hash_length,
2948 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002949 signature );
2950 }
2951 else
2952#endif /* MBEDTLS_PKCS1_V15 */
2953#if defined(MBEDTLS_PKCS1_V21)
2954 if( PSA_ALG_IS_RSA_PSS( alg ) )
2955 {
2956 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2957 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2958 mbedtls_ctr_drbg_random,
2959 &global_data.ctr_drbg,
2960 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002961 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002962 (unsigned int) hash_length,
2963 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002964 signature );
2965 }
2966 else
2967#endif /* MBEDTLS_PKCS1_V21 */
2968 {
2969 return( PSA_ERROR_INVALID_ARGUMENT );
2970 }
2971
2972 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002973 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002974 return( mbedtls_to_psa_error( ret ) );
2975}
2976
2977static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2978 psa_algorithm_t alg,
2979 const uint8_t *hash,
2980 size_t hash_length,
2981 const uint8_t *signature,
2982 size_t signature_length )
2983{
2984 psa_status_t status;
2985 int ret;
2986 mbedtls_md_type_t md_alg;
2987
2988 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2989 if( status != PSA_SUCCESS )
2990 return( status );
2991
Gilles Peskine630a18a2018-06-29 17:49:35 +02002992 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002993 return( PSA_ERROR_BUFFER_TOO_SMALL );
2994
2995#if defined(MBEDTLS_PKCS1_V15)
2996 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2997 {
2998 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2999 MBEDTLS_MD_NONE );
3000 ret = mbedtls_rsa_pkcs1_verify( rsa,
3001 mbedtls_ctr_drbg_random,
3002 &global_data.ctr_drbg,
3003 MBEDTLS_RSA_PUBLIC,
3004 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003005 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003006 hash,
3007 signature );
3008 }
3009 else
3010#endif /* MBEDTLS_PKCS1_V15 */
3011#if defined(MBEDTLS_PKCS1_V21)
3012 if( PSA_ALG_IS_RSA_PSS( alg ) )
3013 {
3014 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3015 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3016 mbedtls_ctr_drbg_random,
3017 &global_data.ctr_drbg,
3018 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003019 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003020 (unsigned int) hash_length,
3021 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003022 signature );
3023 }
3024 else
3025#endif /* MBEDTLS_PKCS1_V21 */
3026 {
3027 return( PSA_ERROR_INVALID_ARGUMENT );
3028 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003029
3030 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3031 * the rest of the signature is invalid". This has little use in
3032 * practice and PSA doesn't report this distinction. */
3033 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3034 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003035 return( mbedtls_to_psa_error( ret ) );
3036}
3037#endif /* MBEDTLS_RSA_C */
3038
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003039#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003040/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3041 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3042 * (even though these functions don't modify it). */
3043static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3044 psa_algorithm_t alg,
3045 const uint8_t *hash,
3046 size_t hash_length,
3047 uint8_t *signature,
3048 size_t signature_size,
3049 size_t *signature_length )
3050{
3051 int ret;
3052 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003053 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003054 mbedtls_mpi_init( &r );
3055 mbedtls_mpi_init( &s );
3056
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003057 if( signature_size < 2 * curve_bytes )
3058 {
3059 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3060 goto cleanup;
3061 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003062
Gilles Peskinea05219c2018-11-16 16:02:56 +01003063#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003064 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3065 {
3066 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3067 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3068 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3069 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
3070 hash, hash_length,
3071 md_alg ) );
3072 }
3073 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003074#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003075 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003076 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003077 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3078 hash, hash_length,
3079 mbedtls_ctr_drbg_random,
3080 &global_data.ctr_drbg ) );
3081 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003082
3083 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3084 signature,
3085 curve_bytes ) );
3086 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3087 signature + curve_bytes,
3088 curve_bytes ) );
3089
3090cleanup:
3091 mbedtls_mpi_free( &r );
3092 mbedtls_mpi_free( &s );
3093 if( ret == 0 )
3094 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003095 return( mbedtls_to_psa_error( ret ) );
3096}
3097
3098static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3099 const uint8_t *hash,
3100 size_t hash_length,
3101 const uint8_t *signature,
3102 size_t signature_length )
3103{
3104 int ret;
3105 mbedtls_mpi r, s;
3106 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3107 mbedtls_mpi_init( &r );
3108 mbedtls_mpi_init( &s );
3109
3110 if( signature_length != 2 * curve_bytes )
3111 return( PSA_ERROR_INVALID_SIGNATURE );
3112
3113 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3114 signature,
3115 curve_bytes ) );
3116 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3117 signature + curve_bytes,
3118 curve_bytes ) );
3119
3120 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3121 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003122
3123cleanup:
3124 mbedtls_mpi_free( &r );
3125 mbedtls_mpi_free( &s );
3126 return( mbedtls_to_psa_error( ret ) );
3127}
3128#endif /* MBEDTLS_ECDSA_C */
3129
Gilles Peskinec5487a82018-12-03 18:08:14 +01003130psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003131 psa_algorithm_t alg,
3132 const uint8_t *hash,
3133 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003134 uint8_t *signature,
3135 size_t signature_size,
3136 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003137{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003138 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003139 psa_status_t status;
3140
3141 *signature_length = signature_size;
3142
Gilles Peskine28f8f302019-07-24 13:30:31 +02003143 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003144 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003145 goto exit;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003146 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003147 {
3148 status = PSA_ERROR_INVALID_ARGUMENT;
3149 goto exit;
3150 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003151
Gilles Peskine20035e32018-02-03 22:44:14 +01003152#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003153 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003154 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003155 status = psa_rsa_sign( slot->data.rsa,
3156 alg,
3157 hash, hash_length,
3158 signature, signature_size,
3159 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003160 }
3161 else
3162#endif /* defined(MBEDTLS_RSA_C) */
3163#if defined(MBEDTLS_ECP_C)
3164 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3165 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003166#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003167 if(
3168#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3169 PSA_ALG_IS_ECDSA( alg )
3170#else
3171 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3172#endif
3173 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003174 status = psa_ecdsa_sign( slot->data.ecp,
3175 alg,
3176 hash, hash_length,
3177 signature, signature_size,
3178 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003179 else
3180#endif /* defined(MBEDTLS_ECDSA_C) */
3181 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003182 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003183 }
itayzafrir5c753392018-05-08 11:18:38 +03003184 }
3185 else
3186#endif /* defined(MBEDTLS_ECP_C) */
3187 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003188 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003189 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003190
3191exit:
3192 /* Fill the unused part of the output buffer (the whole buffer on error,
3193 * the trailing part on success) with something that isn't a valid mac
3194 * (barring an attack on the mac and deliberately-crafted input),
3195 * in case the caller doesn't check the return status properly. */
3196 if( status == PSA_SUCCESS )
3197 memset( signature + *signature_length, '!',
3198 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003199 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003200 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003201 /* If signature_size is 0 then we have nothing to do. We must not call
3202 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003203 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003204}
3205
Gilles Peskinec5487a82018-12-03 18:08:14 +01003206psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03003207 psa_algorithm_t alg,
3208 const uint8_t *hash,
3209 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003210 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003211 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003212{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003213 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003214 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003215
Gilles Peskine28f8f302019-07-24 13:30:31 +02003216 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003217 if( status != PSA_SUCCESS )
3218 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003219
Gilles Peskine61b91d42018-06-08 16:09:36 +02003220#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003221 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003222 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003223 return( psa_rsa_verify( slot->data.rsa,
3224 alg,
3225 hash, hash_length,
3226 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003227 }
3228 else
3229#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003230#if defined(MBEDTLS_ECP_C)
3231 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
3232 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003233#if defined(MBEDTLS_ECDSA_C)
3234 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003235 return( psa_ecdsa_verify( slot->data.ecp,
3236 hash, hash_length,
3237 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003238 else
3239#endif /* defined(MBEDTLS_ECDSA_C) */
3240 {
3241 return( PSA_ERROR_INVALID_ARGUMENT );
3242 }
itayzafrir5c753392018-05-08 11:18:38 +03003243 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003244 else
3245#endif /* defined(MBEDTLS_ECP_C) */
3246 {
3247 return( PSA_ERROR_NOT_SUPPORTED );
3248 }
3249}
3250
Gilles Peskine072ac562018-06-30 00:21:29 +02003251#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3252static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3253 mbedtls_rsa_context *rsa )
3254{
3255 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3256 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3257 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3258 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3259}
3260#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3261
Gilles Peskinec5487a82018-12-03 18:08:14 +01003262psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003263 psa_algorithm_t alg,
3264 const uint8_t *input,
3265 size_t input_length,
3266 const uint8_t *salt,
3267 size_t salt_length,
3268 uint8_t *output,
3269 size_t output_size,
3270 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003271{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003272 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003273 psa_status_t status;
3274
Darryl Green5cc689a2018-07-24 15:34:10 +01003275 (void) input;
3276 (void) input_length;
3277 (void) salt;
3278 (void) output;
3279 (void) output_size;
3280
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003281 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003282
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003283 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3284 return( PSA_ERROR_INVALID_ARGUMENT );
3285
Gilles Peskine28f8f302019-07-24 13:30:31 +02003286 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003287 if( status != PSA_SUCCESS )
3288 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02003289 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003290 PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003291 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003292
3293#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02003294 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003295 {
3296 mbedtls_rsa_context *rsa = slot->data.rsa;
3297 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003298 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02003299 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003300#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003301 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003302 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003303 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3304 mbedtls_ctr_drbg_random,
3305 &global_data.ctr_drbg,
3306 MBEDTLS_RSA_PUBLIC,
3307 input_length,
3308 input,
3309 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003310 }
3311 else
3312#endif /* MBEDTLS_PKCS1_V15 */
3313#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003314 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003315 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003316 psa_rsa_oaep_set_padding_mode( alg, rsa );
3317 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3318 mbedtls_ctr_drbg_random,
3319 &global_data.ctr_drbg,
3320 MBEDTLS_RSA_PUBLIC,
3321 salt, salt_length,
3322 input_length,
3323 input,
3324 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003325 }
3326 else
3327#endif /* MBEDTLS_PKCS1_V21 */
3328 {
3329 return( PSA_ERROR_INVALID_ARGUMENT );
3330 }
3331 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003332 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003333 return( mbedtls_to_psa_error( ret ) );
3334 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003335 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003336#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003337 {
3338 return( PSA_ERROR_NOT_SUPPORTED );
3339 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003340}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003341
Gilles Peskinec5487a82018-12-03 18:08:14 +01003342psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003343 psa_algorithm_t alg,
3344 const uint8_t *input,
3345 size_t input_length,
3346 const uint8_t *salt,
3347 size_t salt_length,
3348 uint8_t *output,
3349 size_t output_size,
3350 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003351{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003352 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003353 psa_status_t status;
3354
Darryl Green5cc689a2018-07-24 15:34:10 +01003355 (void) input;
3356 (void) input_length;
3357 (void) salt;
3358 (void) output;
3359 (void) output_size;
3360
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003361 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003362
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003363 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3364 return( PSA_ERROR_INVALID_ARGUMENT );
3365
Gilles Peskine28f8f302019-07-24 13:30:31 +02003366 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003367 if( status != PSA_SUCCESS )
3368 return( status );
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003369 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003370 return( PSA_ERROR_INVALID_ARGUMENT );
3371
3372#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003373 if( slot->type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003374 {
3375 mbedtls_rsa_context *rsa = slot->data.rsa;
3376 int ret;
3377
Gilles Peskine630a18a2018-06-29 17:49:35 +02003378 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003379 return( PSA_ERROR_INVALID_ARGUMENT );
3380
3381#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003382 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003383 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003384 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3385 mbedtls_ctr_drbg_random,
3386 &global_data.ctr_drbg,
3387 MBEDTLS_RSA_PRIVATE,
3388 output_length,
3389 input,
3390 output,
3391 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003392 }
3393 else
3394#endif /* MBEDTLS_PKCS1_V15 */
3395#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003396 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003397 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003398 psa_rsa_oaep_set_padding_mode( alg, rsa );
3399 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3400 mbedtls_ctr_drbg_random,
3401 &global_data.ctr_drbg,
3402 MBEDTLS_RSA_PRIVATE,
3403 salt, salt_length,
3404 output_length,
3405 input,
3406 output,
3407 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003408 }
3409 else
3410#endif /* MBEDTLS_PKCS1_V21 */
3411 {
3412 return( PSA_ERROR_INVALID_ARGUMENT );
3413 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003414
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003415 return( mbedtls_to_psa_error( ret ) );
3416 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003417 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003418#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003419 {
3420 return( PSA_ERROR_NOT_SUPPORTED );
3421 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003422}
Gilles Peskine20035e32018-02-03 22:44:14 +01003423
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003424
3425
mohammad1603503973b2018-03-12 15:59:30 +02003426/****************************************************************/
3427/* Symmetric cryptography */
3428/****************************************************************/
3429
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003430/* Initialize the cipher operation structure. Once this function has been
3431 * called, psa_cipher_abort can run and will do the right thing. */
3432static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3433 psa_algorithm_t alg )
3434{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003435 if( ! PSA_ALG_IS_CIPHER( alg ) )
3436 {
3437 memset( operation, 0, sizeof( *operation ) );
3438 return( PSA_ERROR_INVALID_ARGUMENT );
3439 }
3440
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003441 operation->alg = alg;
3442 operation->key_set = 0;
3443 operation->iv_set = 0;
3444 operation->iv_required = 1;
3445 operation->iv_size = 0;
3446 operation->block_size = 0;
3447 mbedtls_cipher_init( &operation->ctx.cipher );
3448 return( PSA_SUCCESS );
3449}
3450
Gilles Peskinee553c652018-06-04 16:22:46 +02003451static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003452 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003453 psa_algorithm_t alg,
3454 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003455{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003456 int ret = 0;
3457 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003458 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003459 size_t key_bits;
3460 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003461 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3462 PSA_KEY_USAGE_ENCRYPT :
3463 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003464
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003465 /* A context must be freshly initialized before it can be set up. */
3466 if( operation->alg != 0 )
3467 {
3468 return( PSA_ERROR_BAD_STATE );
3469 }
3470
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003471 status = psa_cipher_init( operation, alg );
3472 if( status != PSA_SUCCESS )
3473 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003474
Gilles Peskine28f8f302019-07-24 13:30:31 +02003475 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003476 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003477 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003478 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003479
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003480 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003481 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003482 {
3483 status = PSA_ERROR_NOT_SUPPORTED;
3484 goto exit;
3485 }
mohammad1603503973b2018-03-12 15:59:30 +02003486
mohammad1603503973b2018-03-12 15:59:30 +02003487 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003488 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003489 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003490
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003491#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003492 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003493 {
3494 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003495 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003496 memcpy( keys, slot->data.raw.data, 16 );
3497 memcpy( keys + 16, slot->data.raw.data, 8 );
3498 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3499 keys,
3500 192, cipher_operation );
3501 }
3502 else
3503#endif
3504 {
3505 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3506 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003507 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003508 }
Moran Peker41deec42018-04-04 15:43:05 +03003509 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003510 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003511
mohammad16038481e742018-03-18 13:57:31 +02003512#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003513 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003514 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003515 case PSA_ALG_CBC_NO_PADDING:
3516 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3517 MBEDTLS_PADDING_NONE );
3518 break;
3519 case PSA_ALG_CBC_PKCS7:
3520 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3521 MBEDTLS_PADDING_PKCS7 );
3522 break;
3523 default:
3524 /* The algorithm doesn't involve padding. */
3525 ret = 0;
3526 break;
3527 }
3528 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003529 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003530#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3531
mohammad1603503973b2018-03-12 15:59:30 +02003532 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003533 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
3534 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3535 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003536 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003537 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003538 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003539#if defined(MBEDTLS_CHACHA20_C)
3540 else
3541 if( alg == PSA_ALG_CHACHA20 )
3542 operation->iv_size = 12;
3543#endif
mohammad1603503973b2018-03-12 15:59:30 +02003544
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003545exit:
3546 if( status == 0 )
3547 status = mbedtls_to_psa_error( ret );
3548 if( status != 0 )
3549 psa_cipher_abort( operation );
3550 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003551}
3552
Gilles Peskinefe119512018-07-08 21:39:34 +02003553psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003554 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003555 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003556{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003557 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003558}
3559
Gilles Peskinefe119512018-07-08 21:39:34 +02003560psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003561 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003562 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003563{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003564 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003565}
3566
Gilles Peskinefe119512018-07-08 21:39:34 +02003567psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003568 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003569 size_t iv_size,
3570 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003571{
itayzafrir534bd7c2018-08-02 13:56:32 +03003572 psa_status_t status;
3573 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003574 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003575 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003576 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003577 }
Moran Peker41deec42018-04-04 15:43:05 +03003578 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003579 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003580 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003581 goto exit;
3582 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003583 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3584 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003585 if( ret != 0 )
3586 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003587 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003588 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003589 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003590
mohammad16038481e742018-03-18 13:57:31 +02003591 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003592 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003593
Moran Peker395db872018-05-31 14:07:14 +03003594exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003595 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003596 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003597 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003598}
3599
Gilles Peskinefe119512018-07-08 21:39:34 +02003600psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003601 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003602 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003603{
itayzafrir534bd7c2018-08-02 13:56:32 +03003604 psa_status_t status;
3605 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003606 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003607 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003608 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003609 }
Moran Pekera28258c2018-05-29 16:25:04 +03003610 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003611 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003612 status = PSA_ERROR_INVALID_ARGUMENT;
3613 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003614 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003615 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3616 status = mbedtls_to_psa_error( ret );
3617exit:
3618 if( status == PSA_SUCCESS )
3619 operation->iv_set = 1;
3620 else
mohammad1603503973b2018-03-12 15:59:30 +02003621 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003622 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003623}
3624
Gilles Peskinee553c652018-06-04 16:22:46 +02003625psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3626 const uint8_t *input,
3627 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003628 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003629 size_t output_size,
3630 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003631{
itayzafrir534bd7c2018-08-02 13:56:32 +03003632 psa_status_t status;
3633 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003634 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003635
3636 if( operation->alg == 0 )
3637 {
3638 return( PSA_ERROR_BAD_STATE );
3639 }
3640
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003641 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003642 {
3643 /* Take the unprocessed partial block left over from previous
3644 * update calls, if any, plus the input to this call. Remove
3645 * the last partial block, if any. You get the data that will be
3646 * output in this call. */
3647 expected_output_size =
3648 ( operation->ctx.cipher.unprocessed_len + input_length )
3649 / operation->block_size * operation->block_size;
3650 }
3651 else
3652 {
3653 expected_output_size = input_length;
3654 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003655
Gilles Peskine89d789c2018-06-04 17:17:16 +02003656 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003657 {
3658 status = PSA_ERROR_BUFFER_TOO_SMALL;
3659 goto exit;
3660 }
mohammad160382759612018-03-12 18:16:40 +02003661
mohammad1603503973b2018-03-12 15:59:30 +02003662 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003663 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003664 status = mbedtls_to_psa_error( ret );
3665exit:
3666 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003667 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003668 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003669}
3670
Gilles Peskinee553c652018-06-04 16:22:46 +02003671psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3672 uint8_t *output,
3673 size_t output_size,
3674 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003675{
David Saadab4ecc272019-02-14 13:48:10 +02003676 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003677 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003678 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003679
mohammad1603503973b2018-03-12 15:59:30 +02003680 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003681 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003682 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003683 }
3684 if( operation->iv_required && ! operation->iv_set )
3685 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003686 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003687 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003688
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003689 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003690 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3691 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003692 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003693 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003694 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003695 }
3696
Janos Follath315b51c2018-07-09 16:04:51 +01003697 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3698 temp_output_buffer,
3699 output_length );
3700 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003701 {
Janos Follath315b51c2018-07-09 16:04:51 +01003702 status = mbedtls_to_psa_error( cipher_ret );
3703 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003704 }
Janos Follath315b51c2018-07-09 16:04:51 +01003705
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003706 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003707 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003708 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003709 memcpy( output, temp_output_buffer, *output_length );
3710 else
3711 {
Janos Follath315b51c2018-07-09 16:04:51 +01003712 status = PSA_ERROR_BUFFER_TOO_SMALL;
3713 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003714 }
mohammad1603503973b2018-03-12 15:59:30 +02003715
Gilles Peskine3f108122018-12-07 18:14:53 +01003716 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003717 status = psa_cipher_abort( operation );
3718
3719 return( status );
3720
3721error:
3722
3723 *output_length = 0;
3724
Gilles Peskine3f108122018-12-07 18:14:53 +01003725 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003726 (void) psa_cipher_abort( operation );
3727
3728 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003729}
3730
Gilles Peskinee553c652018-06-04 16:22:46 +02003731psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3732{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003733 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003734 {
3735 /* The object has (apparently) been initialized but it is not
3736 * in use. It's ok to call abort on such an object, and there's
3737 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003738 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003739 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003740
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003741 /* Sanity check (shouldn't happen: operation->alg should
3742 * always have been initialized to a valid value). */
3743 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3744 return( PSA_ERROR_BAD_STATE );
3745
mohammad1603503973b2018-03-12 15:59:30 +02003746 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003747
Moran Peker41deec42018-04-04 15:43:05 +03003748 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003749 operation->key_set = 0;
3750 operation->iv_set = 0;
3751 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003752 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003753 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003754
Moran Peker395db872018-05-31 14:07:14 +03003755 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003756}
3757
Gilles Peskinea0655c32018-04-30 17:06:50 +02003758
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003759
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003760
mohammad16035955c982018-04-26 00:53:03 +03003761/****************************************************************/
3762/* AEAD */
3763/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003764
Gilles Peskineedf9a652018-08-17 18:11:56 +02003765typedef struct
3766{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003767 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003768 const mbedtls_cipher_info_t *cipher_info;
3769 union
3770 {
3771#if defined(MBEDTLS_CCM_C)
3772 mbedtls_ccm_context ccm;
3773#endif /* MBEDTLS_CCM_C */
3774#if defined(MBEDTLS_GCM_C)
3775 mbedtls_gcm_context gcm;
3776#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003777#if defined(MBEDTLS_CHACHAPOLY_C)
3778 mbedtls_chachapoly_context chachapoly;
3779#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003780 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003781 psa_algorithm_t core_alg;
3782 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003783 uint8_t tag_length;
3784} aead_operation_t;
3785
Gilles Peskine30a9e412019-01-14 18:36:12 +01003786static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003787{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003788 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003789 {
3790#if defined(MBEDTLS_CCM_C)
3791 case PSA_ALG_CCM:
3792 mbedtls_ccm_free( &operation->ctx.ccm );
3793 break;
3794#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003795#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003796 case PSA_ALG_GCM:
3797 mbedtls_gcm_free( &operation->ctx.gcm );
3798 break;
3799#endif /* MBEDTLS_GCM_C */
3800 }
3801}
3802
3803static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003804 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003805 psa_key_usage_t usage,
3806 psa_algorithm_t alg )
3807{
3808 psa_status_t status;
3809 size_t key_bits;
3810 mbedtls_cipher_id_t cipher_id;
3811
Gilles Peskine28f8f302019-07-24 13:30:31 +02003812 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003813 if( status != PSA_SUCCESS )
3814 return( status );
3815
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003816 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003817
3818 operation->cipher_info =
3819 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3820 &cipher_id );
3821 if( operation->cipher_info == NULL )
3822 return( PSA_ERROR_NOT_SUPPORTED );
3823
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003824 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003825 {
3826#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003827 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3828 operation->core_alg = PSA_ALG_CCM;
3829 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003830 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3831 * The call to mbedtls_ccm_encrypt_and_tag or
3832 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003833 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3834 return( PSA_ERROR_INVALID_ARGUMENT );
3835 mbedtls_ccm_init( &operation->ctx.ccm );
3836 status = mbedtls_to_psa_error(
3837 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3838 operation->slot->data.raw.data,
3839 (unsigned int) key_bits ) );
3840 if( status != 0 )
3841 goto cleanup;
3842 break;
3843#endif /* MBEDTLS_CCM_C */
3844
3845#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003846 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3847 operation->core_alg = PSA_ALG_GCM;
3848 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003849 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3850 * The call to mbedtls_gcm_crypt_and_tag or
3851 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003852 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3853 return( PSA_ERROR_INVALID_ARGUMENT );
3854 mbedtls_gcm_init( &operation->ctx.gcm );
3855 status = mbedtls_to_psa_error(
3856 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3857 operation->slot->data.raw.data,
3858 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003859 if( status != 0 )
3860 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003861 break;
3862#endif /* MBEDTLS_GCM_C */
3863
Gilles Peskine26869f22019-05-06 15:25:00 +02003864#if defined(MBEDTLS_CHACHAPOLY_C)
3865 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3866 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
3867 operation->full_tag_length = 16;
3868 /* We only support the default tag length. */
3869 if( alg != PSA_ALG_CHACHA20_POLY1305 )
3870 return( PSA_ERROR_NOT_SUPPORTED );
3871 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
3872 status = mbedtls_to_psa_error(
3873 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
3874 operation->slot->data.raw.data ) );
3875 if( status != 0 )
3876 goto cleanup;
3877 break;
3878#endif /* MBEDTLS_CHACHAPOLY_C */
3879
Gilles Peskineedf9a652018-08-17 18:11:56 +02003880 default:
3881 return( PSA_ERROR_NOT_SUPPORTED );
3882 }
3883
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003884 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3885 {
3886 status = PSA_ERROR_INVALID_ARGUMENT;
3887 goto cleanup;
3888 }
3889 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003890
Gilles Peskineedf9a652018-08-17 18:11:56 +02003891 return( PSA_SUCCESS );
3892
3893cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003894 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003895 return( status );
3896}
3897
Gilles Peskinec5487a82018-12-03 18:08:14 +01003898psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003899 psa_algorithm_t alg,
3900 const uint8_t *nonce,
3901 size_t nonce_length,
3902 const uint8_t *additional_data,
3903 size_t additional_data_length,
3904 const uint8_t *plaintext,
3905 size_t plaintext_length,
3906 uint8_t *ciphertext,
3907 size_t ciphertext_size,
3908 size_t *ciphertext_length )
3909{
mohammad16035955c982018-04-26 00:53:03 +03003910 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003911 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003912 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003913
mohammad1603f08a5502018-06-03 15:05:47 +03003914 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003915
Gilles Peskinec5487a82018-12-03 18:08:14 +01003916 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003917 if( status != PSA_SUCCESS )
3918 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003919
Gilles Peskineedf9a652018-08-17 18:11:56 +02003920 /* For all currently supported modes, the tag is at the end of the
3921 * ciphertext. */
3922 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3923 {
3924 status = PSA_ERROR_BUFFER_TOO_SMALL;
3925 goto exit;
3926 }
3927 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003928
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003929#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003930 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003931 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003932 status = mbedtls_to_psa_error(
3933 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3934 MBEDTLS_GCM_ENCRYPT,
3935 plaintext_length,
3936 nonce, nonce_length,
3937 additional_data, additional_data_length,
3938 plaintext, ciphertext,
3939 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003940 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003941 else
3942#endif /* MBEDTLS_GCM_C */
3943#if defined(MBEDTLS_CCM_C)
3944 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003945 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003946 status = mbedtls_to_psa_error(
3947 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3948 plaintext_length,
3949 nonce, nonce_length,
3950 additional_data,
3951 additional_data_length,
3952 plaintext, ciphertext,
3953 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003954 }
mohammad16035c8845f2018-05-09 05:40:09 -07003955 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003956#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003957#if defined(MBEDTLS_CHACHAPOLY_C)
3958 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
3959 {
3960 if( nonce_length != 12 || operation.tag_length != 16 )
3961 {
3962 status = PSA_ERROR_NOT_SUPPORTED;
3963 goto exit;
3964 }
3965 status = mbedtls_to_psa_error(
3966 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
3967 plaintext_length,
3968 nonce,
3969 additional_data,
3970 additional_data_length,
3971 plaintext,
3972 ciphertext,
3973 tag ) );
3974 }
3975 else
3976#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003977 {
mohammad1603554faad2018-06-03 15:07:38 +03003978 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003979 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003980
Gilles Peskineedf9a652018-08-17 18:11:56 +02003981 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3982 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003983
Gilles Peskineedf9a652018-08-17 18:11:56 +02003984exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003985 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003986 if( status == PSA_SUCCESS )
3987 *ciphertext_length = plaintext_length + operation.tag_length;
3988 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003989}
3990
Gilles Peskineee652a32018-06-01 19:23:52 +02003991/* Locate the tag in a ciphertext buffer containing the encrypted data
3992 * followed by the tag. Return the length of the part preceding the tag in
3993 * *plaintext_length. This is the size of the plaintext in modes where
3994 * the encrypted data has the same size as the plaintext, such as
3995 * CCM and GCM. */
3996static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3997 const uint8_t *ciphertext,
3998 size_t ciphertext_length,
3999 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004000 const uint8_t **p_tag )
4001{
4002 size_t payload_length;
4003 if( tag_length > ciphertext_length )
4004 return( PSA_ERROR_INVALID_ARGUMENT );
4005 payload_length = ciphertext_length - tag_length;
4006 if( payload_length > plaintext_size )
4007 return( PSA_ERROR_BUFFER_TOO_SMALL );
4008 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004009 return( PSA_SUCCESS );
4010}
4011
Gilles Peskinec5487a82018-12-03 18:08:14 +01004012psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004013 psa_algorithm_t alg,
4014 const uint8_t *nonce,
4015 size_t nonce_length,
4016 const uint8_t *additional_data,
4017 size_t additional_data_length,
4018 const uint8_t *ciphertext,
4019 size_t ciphertext_length,
4020 uint8_t *plaintext,
4021 size_t plaintext_size,
4022 size_t *plaintext_length )
4023{
mohammad16035955c982018-04-26 00:53:03 +03004024 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004025 aead_operation_t operation;
4026 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004027
Gilles Peskineee652a32018-06-01 19:23:52 +02004028 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004029
Gilles Peskinec5487a82018-12-03 18:08:14 +01004030 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004031 if( status != PSA_SUCCESS )
4032 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004033
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004034 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4035 ciphertext, ciphertext_length,
4036 plaintext_size, &tag );
4037 if( status != PSA_SUCCESS )
4038 goto exit;
4039
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004040#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004041 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004042 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004043 status = mbedtls_to_psa_error(
4044 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4045 ciphertext_length - operation.tag_length,
4046 nonce, nonce_length,
4047 additional_data,
4048 additional_data_length,
4049 tag, operation.tag_length,
4050 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004051 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004052 else
4053#endif /* MBEDTLS_GCM_C */
4054#if defined(MBEDTLS_CCM_C)
4055 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004056 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004057 status = mbedtls_to_psa_error(
4058 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4059 ciphertext_length - operation.tag_length,
4060 nonce, nonce_length,
4061 additional_data,
4062 additional_data_length,
4063 ciphertext, plaintext,
4064 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004065 }
mohammad160339574652018-06-01 04:39:53 -07004066 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004067#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004068#if defined(MBEDTLS_CHACHAPOLY_C)
4069 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4070 {
4071 if( nonce_length != 12 || operation.tag_length != 16 )
4072 {
4073 status = PSA_ERROR_NOT_SUPPORTED;
4074 goto exit;
4075 }
4076 status = mbedtls_to_psa_error(
4077 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4078 ciphertext_length - operation.tag_length,
4079 nonce,
4080 additional_data,
4081 additional_data_length,
4082 tag,
4083 ciphertext,
4084 plaintext ) );
4085 }
4086 else
4087#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004088 {
mohammad1603554faad2018-06-03 15:07:38 +03004089 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004090 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004091
Gilles Peskineedf9a652018-08-17 18:11:56 +02004092 if( status != PSA_SUCCESS && plaintext_size != 0 )
4093 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004094
Gilles Peskineedf9a652018-08-17 18:11:56 +02004095exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004096 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004097 if( status == PSA_SUCCESS )
4098 *plaintext_length = ciphertext_length - operation.tag_length;
4099 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004100}
4101
Gilles Peskinea0655c32018-04-30 17:06:50 +02004102
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004103
Gilles Peskine20035e32018-02-03 22:44:14 +01004104/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004105/* Generators */
4106/****************************************************************/
4107
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004108#define HKDF_STATE_INIT 0 /* no input yet */
4109#define HKDF_STATE_STARTED 1 /* got salt */
4110#define HKDF_STATE_KEYED 2 /* got key */
4111#define HKDF_STATE_OUTPUT 3 /* output started */
4112
Gilles Peskinecbe66502019-05-16 16:59:18 +02004113static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004114 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004115{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004116 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4117 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004118 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004119 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004120}
4121
4122
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004123psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004124{
4125 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004126 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004127 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004128 {
4129 /* The object has (apparently) been initialized but it is not
4130 * in use. It's ok to call abort on such an object, and there's
4131 * nothing to do. */
4132 }
4133 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004134#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004135 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004136 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004137 mbedtls_free( operation->ctx.hkdf.info );
4138 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004139 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004140 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004141 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004142 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004143 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004144#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004145 if( operation->ctx.tls12_prf.key != NULL )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004146 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004147 mbedtls_platform_zeroize( operation->ctx.tls12_prf.key,
4148 operation->ctx.tls12_prf.key_len );
4149 mbedtls_free( operation->ctx.tls12_prf.key );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004150 }
Hanno Becker580fba12018-11-13 20:50:45 +00004151
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004152 if( operation->ctx.tls12_prf.Ai_with_seed != NULL )
Hanno Becker580fba12018-11-13 20:50:45 +00004153 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004154 mbedtls_platform_zeroize( operation->ctx.tls12_prf.Ai_with_seed,
4155 operation->ctx.tls12_prf.Ai_with_seed_len );
4156 mbedtls_free( operation->ctx.tls12_prf.Ai_with_seed );
Hanno Becker580fba12018-11-13 20:50:45 +00004157 }
Janos Follath6a1d2622019-06-11 10:37:28 +01004158#else
4159 if( operation->ctx.tls12_prf.seed != NULL )
4160 {
4161 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4162 operation->ctx.tls12_prf.seed_length );
4163 mbedtls_free( operation->ctx.tls12_prf.seed );
4164 }
4165
4166 if( operation->ctx.tls12_prf.label != NULL )
4167 {
4168 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4169 operation->ctx.tls12_prf.label_length );
4170 mbedtls_free( operation->ctx.tls12_prf.label );
4171 }
4172
4173 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4174
4175 /* We leave the fields Ai and output_block to be erased safely by the
4176 * mbedtls_platform_zeroize() in the end of this function. */
Janos Follath999f6482019-06-11 12:04:10 +01004177#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004178 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004179 else
4180#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004181 {
4182 status = PSA_ERROR_BAD_STATE;
4183 }
Janos Follath083036a2019-06-11 10:22:26 +01004184 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004185 return( status );
4186}
4187
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004188psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004189 size_t *capacity)
4190{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004191 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004192 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004193 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004194 return PSA_ERROR_BAD_STATE;
4195 }
4196
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004197 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004198 return( PSA_SUCCESS );
4199}
4200
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004201psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004202 size_t capacity )
4203{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004204 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004205 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004206 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004207 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004208 operation->capacity = capacity;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004209 return( PSA_SUCCESS );
4210}
4211
Gilles Peskinebef7f142018-07-12 17:22:21 +02004212#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004213/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004214 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004215static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004216 psa_algorithm_t hash_alg,
4217 uint8_t *output,
4218 size_t output_length )
4219{
4220 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4221 psa_status_t status;
4222
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004223 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4224 return( PSA_ERROR_BAD_STATE );
4225 hkdf->state = HKDF_STATE_OUTPUT;
4226
Gilles Peskinebef7f142018-07-12 17:22:21 +02004227 while( output_length != 0 )
4228 {
4229 /* Copy what remains of the current block */
4230 uint8_t n = hash_length - hkdf->offset_in_block;
4231 if( n > output_length )
4232 n = (uint8_t) output_length;
4233 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4234 output += n;
4235 output_length -= n;
4236 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004237 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004238 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004239 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004240 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004241 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004242 * object was corrupted or if this function is called directly
4243 * inside the library. */
4244 if( hkdf->block_number == 0xff )
4245 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004246
4247 /* We need a new block */
4248 ++hkdf->block_number;
4249 hkdf->offset_in_block = 0;
4250 status = psa_hmac_setup_internal( &hkdf->hmac,
4251 hkdf->prk, hash_length,
4252 hash_alg );
4253 if( status != PSA_SUCCESS )
4254 return( status );
4255 if( hkdf->block_number != 1 )
4256 {
4257 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4258 hkdf->output_block,
4259 hash_length );
4260 if( status != PSA_SUCCESS )
4261 return( status );
4262 }
4263 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4264 hkdf->info,
4265 hkdf->info_length );
4266 if( status != PSA_SUCCESS )
4267 return( status );
4268 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4269 &hkdf->block_number, 1 );
4270 if( status != PSA_SUCCESS )
4271 return( status );
4272 status = psa_hmac_finish_internal( &hkdf->hmac,
4273 hkdf->output_block,
4274 sizeof( hkdf->output_block ) );
4275 if( status != PSA_SUCCESS )
4276 return( status );
4277 }
4278
4279 return( PSA_SUCCESS );
4280}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004281
Janos Follath999f6482019-06-11 12:04:10 +01004282#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004283static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4284 psa_tls12_prf_key_derivation_t *tls12_prf,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004285 psa_algorithm_t alg )
4286{
4287 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4288 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4289 psa_hmac_internal_data hmac;
4290 psa_status_t status, cleanup_status;
4291
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004292 uint8_t *Ai;
Hanno Becker3b339e22018-11-13 20:56:14 +00004293 size_t Ai_len;
4294
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004295 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004296 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004297 * prevented this call. It could happen only if the operation
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004298 * object was corrupted or if this function is called directly
4299 * inside the library. */
4300 if( tls12_prf->block_number == 0xff )
4301 return( PSA_ERROR_BAD_STATE );
4302
4303 /* We need a new block */
4304 ++tls12_prf->block_number;
4305 tls12_prf->offset_in_block = 0;
4306
4307 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4308 *
4309 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4310 *
4311 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4312 * HMAC_hash(secret, A(2) + seed) +
4313 * HMAC_hash(secret, A(3) + seed) + ...
4314 *
4315 * A(0) = seed
4316 * A(i) = HMAC_hash( secret, A(i-1) )
4317 *
Gilles Peskinecbe66502019-05-16 16:59:18 +02004318 * The `psa_tls12_prf_key_derivation` structures saves the block
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004319 * `HMAC_hash(secret, A(i) + seed)` from which the output
4320 * is currently extracted as `output_block`, while
4321 * `A(i) + seed` is stored in `Ai_with_seed`.
4322 *
4323 * Generating a new block means recalculating `Ai_with_seed`
4324 * from the A(i)-part of it, and afterwards recalculating
4325 * `output_block`.
4326 *
4327 * A(0) is computed at setup time.
4328 *
4329 */
4330
4331 psa_hmac_init_internal( &hmac );
4332
4333 /* We must distinguish the calculation of A(1) from those
4334 * of A(2) and higher, because A(0)=seed has a different
4335 * length than the other A(i). */
4336 if( tls12_prf->block_number == 1 )
4337 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004338 Ai = tls12_prf->Ai_with_seed + hash_length;
4339 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004340 }
4341 else
4342 {
Hanno Becker3b339e22018-11-13 20:56:14 +00004343 Ai = tls12_prf->Ai_with_seed;
4344 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004345 }
4346
Hanno Becker3b339e22018-11-13 20:56:14 +00004347 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
4348 status = psa_hmac_setup_internal( &hmac,
4349 tls12_prf->key,
4350 tls12_prf->key_len,
4351 hash_alg );
4352 if( status != PSA_SUCCESS )
4353 goto cleanup;
4354
4355 status = psa_hash_update( &hmac.hash_ctx,
4356 Ai, Ai_len );
4357 if( status != PSA_SUCCESS )
4358 goto cleanup;
4359
4360 status = psa_hmac_finish_internal( &hmac,
4361 tls12_prf->Ai_with_seed,
4362 hash_length );
4363 if( status != PSA_SUCCESS )
4364 goto cleanup;
4365
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004366 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
4367 status = psa_hmac_setup_internal( &hmac,
4368 tls12_prf->key,
4369 tls12_prf->key_len,
4370 hash_alg );
4371 if( status != PSA_SUCCESS )
4372 goto cleanup;
4373
4374 status = psa_hash_update( &hmac.hash_ctx,
4375 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00004376 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004377 if( status != PSA_SUCCESS )
4378 goto cleanup;
4379
4380 status = psa_hmac_finish_internal( &hmac,
4381 tls12_prf->output_block,
4382 hash_length );
4383 if( status != PSA_SUCCESS )
4384 goto cleanup;
4385
4386cleanup:
4387
4388 cleanup_status = psa_hmac_abort_internal( &hmac );
4389 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4390 status = cleanup_status;
4391
4392 return( status );
4393}
Janos Follath7742fee2019-06-17 12:58:10 +01004394#else
4395static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4396 psa_tls12_prf_key_derivation_t *tls12_prf,
4397 psa_algorithm_t alg )
4398{
4399 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4400 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004401 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4402 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004403
Janos Follath7742fee2019-06-17 12:58:10 +01004404 /* We can't be wanting more output after block 0xff, otherwise
4405 * the capacity check in psa_key_derivation_output_bytes() would have
4406 * prevented this call. It could happen only if the operation
4407 * object was corrupted or if this function is called directly
4408 * inside the library. */
4409 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004410 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004411
4412 /* We need a new block */
4413 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004414 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004415
4416 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4417 *
4418 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4419 *
4420 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4421 * HMAC_hash(secret, A(2) + seed) +
4422 * HMAC_hash(secret, A(3) + seed) + ...
4423 *
4424 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004425 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004426 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004427 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004428 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004429 * is currently extracted as `output_block` and where i is
4430 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004431 */
4432
Janos Follathea29bfb2019-06-19 12:21:20 +01004433 /* Save the hash context before using it, to preserve the hash state with
4434 * only the inner padding in it. We need this, because inner padding depends
4435 * on the key (secret in the RFC's terminology). */
4436 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4437 if( status != PSA_SUCCESS )
4438 goto cleanup;
4439
4440 /* Calculate A(i) where i = tls12_prf->block_number. */
4441 if( tls12_prf->block_number == 1 )
4442 {
4443 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4444 * the variable seed and in this instance means it in the context of the
4445 * P_hash function, where seed = label + seed.) */
4446 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4447 tls12_prf->label, tls12_prf->label_length );
4448 if( status != PSA_SUCCESS )
4449 goto cleanup;
4450 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4451 tls12_prf->seed, tls12_prf->seed_length );
4452 if( status != PSA_SUCCESS )
4453 goto cleanup;
4454 }
4455 else
4456 {
4457 /* A(i) = HMAC_hash(secret, A(i-1)) */
4458 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4459 tls12_prf->Ai, hash_length );
4460 if( status != PSA_SUCCESS )
4461 goto cleanup;
4462 }
4463
4464 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4465 tls12_prf->Ai, hash_length );
4466 if( status != PSA_SUCCESS )
4467 goto cleanup;
4468 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4469 if( status != PSA_SUCCESS )
4470 goto cleanup;
4471
4472 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4473 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4474 tls12_prf->Ai, hash_length );
4475 if( status != PSA_SUCCESS )
4476 goto cleanup;
4477 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4478 tls12_prf->label, tls12_prf->label_length );
4479 if( status != PSA_SUCCESS )
4480 goto cleanup;
4481 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4482 tls12_prf->seed, tls12_prf->seed_length );
4483 if( status != PSA_SUCCESS )
4484 goto cleanup;
4485 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4486 tls12_prf->output_block, hash_length );
4487 if( status != PSA_SUCCESS )
4488 goto cleanup;
4489 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4490 if( status != PSA_SUCCESS )
4491 goto cleanup;
4492
Janos Follath7742fee2019-06-17 12:58:10 +01004493
4494cleanup:
4495
Janos Follathea29bfb2019-06-19 12:21:20 +01004496 cleanup_status = psa_hash_abort( &backup );
4497 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4498 status = cleanup_status;
4499
4500 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004501}
Janos Follath999f6482019-06-11 12:04:10 +01004502#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004503
Janos Follath999f6482019-06-11 12:04:10 +01004504#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004505/* Read some bytes from an TLS-1.2-PRF-based operation.
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004506 * See Section 5 of RFC 5246. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004507static psa_status_t psa_key_derivation_tls12_prf_read(
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004508 psa_tls12_prf_key_derivation_t *tls12_prf,
4509 psa_algorithm_t alg,
4510 uint8_t *output,
4511 size_t output_length )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004512{
4513 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4514 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4515 psa_status_t status;
4516
4517 while( output_length != 0 )
4518 {
4519 /* Copy what remains of the current block */
4520 uint8_t n = hash_length - tls12_prf->offset_in_block;
4521
4522 /* Check if we have fully processed the current block. */
4523 if( n == 0 )
4524 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004525 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004526 alg );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004527 if( status != PSA_SUCCESS )
4528 return( status );
4529
4530 continue;
4531 }
4532
4533 if( n > output_length )
4534 n = (uint8_t) output_length;
4535 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
4536 n );
4537 output += n;
4538 output_length -= n;
4539 tls12_prf->offset_in_block += n;
4540 }
4541
4542 return( PSA_SUCCESS );
4543}
Janos Follath844eb0e2019-06-19 12:10:49 +01004544#else
4545static psa_status_t psa_key_derivation_tls12_prf_read(
4546 psa_tls12_prf_key_derivation_t *tls12_prf,
4547 psa_algorithm_t alg,
4548 uint8_t *output,
4549 size_t output_length )
4550{
4551 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4552 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4553 psa_status_t status;
4554 uint8_t offset, length;
4555
4556 while( output_length != 0 )
4557 {
4558 /* Check if we have fully processed the current block. */
4559 if( tls12_prf->left_in_block == 0 )
4560 {
4561 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4562 alg );
4563 if( status != PSA_SUCCESS )
4564 return( status );
4565
4566 continue;
4567 }
4568
4569 if( tls12_prf->left_in_block > output_length )
4570 length = (uint8_t) output_length;
4571 else
4572 length = tls12_prf->left_in_block;
4573
4574 offset = hash_length - tls12_prf->left_in_block;
4575 memcpy( output, tls12_prf->output_block + offset, length );
4576 output += length;
4577 output_length -= length;
4578 tls12_prf->left_in_block -= length;
4579 }
4580
4581 return( PSA_SUCCESS );
4582}
Janos Follath999f6482019-06-11 12:04:10 +01004583#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004584#endif /* MBEDTLS_MD_C */
4585
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004586psa_status_t psa_key_derivation_output_bytes(
4587 psa_key_derivation_operation_t *operation,
4588 uint8_t *output,
4589 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004590{
4591 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004592 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004593
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004594 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004595 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004596 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004597 return PSA_ERROR_BAD_STATE;
4598 }
4599
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004600 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004601 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004602 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004603 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004604 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004605 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004606 goto exit;
4607 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004608 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004609 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004610 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004611 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004612 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4613 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004614 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004615 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004616 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004617 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004618 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004619
Gilles Peskinebef7f142018-07-12 17:22:21 +02004620#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004621 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004622 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004623 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004624 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004625 output, output_length );
4626 }
Janos Follathadbec812019-06-14 11:05:39 +01004627 else
Janos Follathadbec812019-06-14 11:05:39 +01004628 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004629 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004630 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004631 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004632 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004633 output_length );
4634 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004635 else
4636#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004637 {
4638 return( PSA_ERROR_BAD_STATE );
4639 }
4640
4641exit:
4642 if( status != PSA_SUCCESS )
4643 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004644 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004645 * This allows us to differentiate between exhausted operations and
4646 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4647 * operations. */
4648 psa_algorithm_t alg = operation->alg;
4649 psa_key_derivation_abort( operation );
4650 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004651 memset( output, '!', output_length );
4652 }
4653 return( status );
4654}
4655
Gilles Peskine08542d82018-07-19 17:05:42 +02004656#if defined(MBEDTLS_DES_C)
4657static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4658{
4659 if( data_size >= 8 )
4660 mbedtls_des_key_set_parity( data );
4661 if( data_size >= 16 )
4662 mbedtls_des_key_set_parity( data + 8 );
4663 if( data_size >= 24 )
4664 mbedtls_des_key_set_parity( data + 16 );
4665}
4666#endif /* MBEDTLS_DES_C */
4667
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004668static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004669 psa_key_slot_t *slot,
4670 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004671 psa_key_derivation_operation_t *operation )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004672{
4673 uint8_t *data = NULL;
4674 size_t bytes = PSA_BITS_TO_BYTES( bits );
4675 psa_status_t status;
4676
4677 if( ! key_type_is_raw_bytes( slot->type ) )
4678 return( PSA_ERROR_INVALID_ARGUMENT );
4679 if( bits % 8 != 0 )
4680 return( PSA_ERROR_INVALID_ARGUMENT );
4681 data = mbedtls_calloc( 1, bytes );
4682 if( data == NULL )
4683 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4684
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004685 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004686 if( status != PSA_SUCCESS )
4687 goto exit;
4688#if defined(MBEDTLS_DES_C)
4689 if( slot->type == PSA_KEY_TYPE_DES )
4690 psa_des_set_key_parity( data, bytes );
4691#endif /* MBEDTLS_DES_C */
4692 status = psa_import_key_into_slot( slot, data, bytes );
4693
4694exit:
4695 mbedtls_free( data );
4696 return( status );
4697}
4698
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004699psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004700 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004701 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004702{
4703 psa_status_t status;
4704 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004705 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02004706 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004707#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4708 if( driver != NULL )
4709 {
4710 /* Deriving a key in a secure element is not implemented yet. */
4711 status = PSA_ERROR_NOT_SUPPORTED;
4712 }
4713#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004714 if( status == PSA_SUCCESS )
4715 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004716 status = psa_generate_derived_key_internal( slot,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004717 attributes->bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004718 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004719 }
4720 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004721 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004722 if( status != PSA_SUCCESS )
4723 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004724 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004725 *handle = 0;
4726 }
4727 return( status );
4728}
4729
Gilles Peskineeab56e42018-07-12 17:12:33 +02004730
4731
4732/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004733/* Key derivation */
4734/****************************************************************/
4735
Gilles Peskinea05219c2018-11-16 16:02:56 +01004736#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004737#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738/* Set up an HKDF-based operation. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004739 * of the HKDF algorithm.
4740 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004741 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004742 * to potentially free embedded data structures and wipe confidential data.
4743 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004744static psa_status_t psa_key_derivation_hkdf_setup( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004745 const uint8_t *secret,
4746 size_t secret_length,
4747 psa_algorithm_t hash_alg,
4748 const uint8_t *salt,
4749 size_t salt_length,
4750 const uint8_t *label,
4751 size_t label_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004752{
4753 psa_status_t status;
4754 status = psa_hmac_setup_internal( &hkdf->hmac,
4755 salt, salt_length,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004756 hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004757 if( status != PSA_SUCCESS )
4758 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004759 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004760 if( status != PSA_SUCCESS )
4761 return( status );
4762 status = psa_hmac_finish_internal( &hkdf->hmac,
4763 hkdf->prk,
4764 sizeof( hkdf->prk ) );
4765 if( status != PSA_SUCCESS )
4766 return( status );
4767 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4768 hkdf->block_number = 0;
4769 hkdf->info_length = label_length;
4770 if( label_length != 0 )
4771 {
4772 hkdf->info = mbedtls_calloc( 1, label_length );
4773 if( hkdf->info == NULL )
4774 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4775 memcpy( hkdf->info, label, label_length );
4776 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004777 hkdf->state = HKDF_STATE_KEYED;
4778 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004779 return( PSA_SUCCESS );
4780}
Janos Follath71a4c912019-06-11 09:14:47 +01004781#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004782#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004783
Gilles Peskinea05219c2018-11-16 16:02:56 +01004784#if defined(MBEDTLS_MD_C)
Janos Follath71a4c912019-06-11 09:14:47 +01004785#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004786/* Set up a TLS-1.2-prf-based operation (see RFC 5246, Section 5).
Gilles Peskine346797d2018-11-16 16:05:06 +01004787 *
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004788 * Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004789 * to potentially free embedded data structures and wipe confidential data.
4790 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004791static psa_status_t psa_key_derivation_tls12_prf_setup(
4792 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004793 const uint8_t *key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004794 size_t key_len,
4795 psa_algorithm_t hash_alg,
4796 const uint8_t *salt,
4797 size_t salt_length,
4798 const uint8_t *label,
4799 size_t label_length )
4800{
4801 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004802 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4803 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004804
4805 tls12_prf->key = mbedtls_calloc( 1, key_len );
4806 if( tls12_prf->key == NULL )
4807 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4808 tls12_prf->key_len = key_len;
4809 memcpy( tls12_prf->key, key, key_len );
4810
Hanno Becker580fba12018-11-13 20:50:45 +00004811 overflow = ( salt_length + label_length < salt_length ) ||
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004812 ( salt_length + label_length + hash_length < hash_length );
Hanno Becker580fba12018-11-13 20:50:45 +00004813 if( overflow )
4814 return( PSA_ERROR_INVALID_ARGUMENT );
4815
4816 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4817 if( tls12_prf->Ai_with_seed == NULL )
4818 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4819 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4820
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004821 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4822 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004823 if( label_length != 0 )
4824 {
4825 memcpy( tls12_prf->Ai_with_seed + hash_length,
4826 label, label_length );
4827 }
4828
4829 if( salt_length != 0 )
4830 {
4831 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4832 salt, salt_length );
4833 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004834
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004835 /* The first block gets generated when
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004836 * psa_key_derivation_output_bytes() is called. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004837 tls12_prf->block_number = 0;
4838 tls12_prf->offset_in_block = hash_length;
4839
4840 return( PSA_SUCCESS );
4841}
Janos Follath71a4c912019-06-11 09:14:47 +01004842#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Hanno Becker1aaedc02018-11-16 11:35:34 +00004843
Janos Follath71a4c912019-06-11 09:14:47 +01004844#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845/* Set up a TLS-1.2-PSK-to-MS-based operation. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004846static psa_status_t psa_key_derivation_tls12_psk_to_ms_setup(
4847 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004848 const uint8_t *psk,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004849 size_t psk_len,
4850 psa_algorithm_t hash_alg,
4851 const uint8_t *salt,
4852 size_t salt_length,
4853 const uint8_t *label,
4854 size_t label_length )
4855{
4856 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004857 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
Hanno Becker1aaedc02018-11-16 11:35:34 +00004858
4859 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4860 return( PSA_ERROR_INVALID_ARGUMENT );
4861
4862 /* Quoting RFC 4279, Section 2:
4863 *
4864 * The premaster secret is formed as follows: if the PSK is N octets
4865 * long, concatenate a uint16 with the value N, N zero octets, a second
4866 * uint16 with the value N, and the PSK itself.
4867 */
4868
4869 pms[0] = ( psk_len >> 8 ) & 0xff;
4870 pms[1] = ( psk_len >> 0 ) & 0xff;
4871 memset( pms + 2, 0, psk_len );
4872 pms[2 + psk_len + 0] = pms[0];
4873 pms[2 + psk_len + 1] = pms[1];
4874 memcpy( pms + 4 + psk_len, psk, psk_len );
4875
Gilles Peskinecbe66502019-05-16 16:59:18 +02004876 status = psa_key_derivation_tls12_prf_setup( tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004877 pms, 4 + 2 * psk_len,
4878 hash_alg,
4879 salt, salt_length,
4880 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004881
Gilles Peskine3f108122018-12-07 18:14:53 +01004882 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004883 return( status );
4884}
Janos Follath71a4c912019-06-11 09:14:47 +01004885#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinea05219c2018-11-16 16:02:56 +01004886#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004887
Janos Follath71a4c912019-06-11 09:14:47 +01004888#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004889/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01004890 * to potentially free embedded data structures and wipe confidential data.
4891 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004892static psa_status_t psa_key_derivation_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004893 psa_key_derivation_operation_t *operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004894 const uint8_t *secret, size_t secret_length,
4895 psa_algorithm_t alg,
4896 const uint8_t *salt, size_t salt_length,
4897 const uint8_t *label, size_t label_length,
4898 size_t capacity )
4899{
4900 psa_status_t status;
4901 size_t max_capacity;
4902
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004903 /* Set operation->alg even on failure so that abort knows what to do. */
4904 operation->alg = alg;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004905
4906#if defined(MBEDTLS_MD_C)
4907 if( PSA_ALG_IS_HKDF( alg ) )
4908 {
4909 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4910 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4911 if( hash_size == 0 )
4912 return( PSA_ERROR_NOT_SUPPORTED );
4913 max_capacity = 255 * hash_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004914 status = psa_key_derivation_hkdf_setup( &operation->ctx.hkdf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004915 secret, secret_length,
4916 hash_alg,
4917 salt, salt_length,
4918 label, label_length );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004919 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004920 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4921 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4922 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004923 {
4924 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4925 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4926
4927 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4928 if( hash_alg != PSA_ALG_SHA_256 &&
4929 hash_alg != PSA_ALG_SHA_384 )
4930 {
4931 return( PSA_ERROR_NOT_SUPPORTED );
4932 }
4933
4934 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004935
4936 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4937 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004938 status = psa_key_derivation_tls12_prf_setup( &operation->ctx.tls12_prf,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004939 secret, secret_length,
4940 hash_alg, salt, salt_length,
4941 label, label_length );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004942 }
4943 else
4944 {
Gilles Peskinecbe66502019-05-16 16:59:18 +02004945 status = psa_key_derivation_tls12_psk_to_ms_setup(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004946 &operation->ctx.tls12_prf,
Hanno Becker1aaedc02018-11-16 11:35:34 +00004947 secret, secret_length,
4948 hash_alg, salt, salt_length,
4949 label, label_length );
4950 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004951 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004952 else
4953#endif
4954 {
4955 return( PSA_ERROR_NOT_SUPPORTED );
4956 }
4957
4958 if( status != PSA_SUCCESS )
4959 return( status );
4960
4961 if( capacity <= max_capacity )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004962 operation->capacity = capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004963 else if( capacity == PSA_KEY_DERIVATION_UNLIMITED_CAPACITY )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004964 operation->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004965 else
4966 return( PSA_ERROR_INVALID_ARGUMENT );
4967
4968 return( PSA_SUCCESS );
4969}
Janos Follath71a4c912019-06-11 09:14:47 +01004970#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004971
Janos Follath71a4c912019-06-11 09:14:47 +01004972#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004973psa_status_t psa_key_derivation( psa_key_derivation_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004974 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004975 psa_algorithm_t alg,
4976 const uint8_t *salt,
4977 size_t salt_length,
4978 const uint8_t *label,
4979 size_t label_length,
4980 size_t capacity )
4981{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004982 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004983 psa_status_t status;
4984
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004985 if( operation->alg != 0 )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004986 return( PSA_ERROR_BAD_STATE );
4987
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004988 /* Make sure that alg is a key derivation algorithm. This prevents
4989 * key selection algorithms, which psa_key_derivation_internal
4990 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004991 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4992 return( PSA_ERROR_INVALID_ARGUMENT );
4993
Gilles Peskine28f8f302019-07-24 13:30:31 +02004994 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004995 if( status != PSA_SUCCESS )
4996 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004997
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004998 if( slot->type != PSA_KEY_TYPE_DERIVE )
4999 return( PSA_ERROR_INVALID_ARGUMENT );
5000
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005001 status = psa_key_derivation_internal( operation,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02005002 slot->data.raw.data,
5003 slot->data.raw.bytes,
5004 alg,
5005 salt, salt_length,
5006 label, label_length,
5007 capacity );
5008 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005009 psa_key_derivation_abort( operation );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005010 return( status );
5011}
Janos Follath71a4c912019-06-11 09:14:47 +01005012#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02005013
Gilles Peskine969c5d62019-01-16 15:53:06 +01005014static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005015 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005016 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005017{
Janos Follath5fe19732019-06-20 15:09:30 +01005018 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5019 * initialisers for this union leave some bytes unspecified.) */
5020 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5021
Gilles Peskine969c5d62019-01-16 15:53:06 +01005022 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005023#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005024 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5025 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5026 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005027 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005028 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005029 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5030 if( hash_size == 0 )
5031 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005032 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5033 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005034 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005035 {
5036 return( PSA_ERROR_NOT_SUPPORTED );
5037 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005038 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005039 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005040 }
5041#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005042 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005043 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005044}
5045
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005046psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005047 psa_algorithm_t alg )
5048{
5049 psa_status_t status;
5050
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005051 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005052 return( PSA_ERROR_BAD_STATE );
5053
Gilles Peskine6843c292019-01-18 16:44:49 +01005054 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5055 return( PSA_ERROR_INVALID_ARGUMENT );
5056 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005057 {
5058 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005059 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005060 }
5061 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5062 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005064 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005065 else
5066 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005067
5068 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005069 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005070 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005071}
5072
5073#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005074static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005075 psa_algorithm_t hash_alg,
5076 psa_key_derivation_step_t step,
5077 const uint8_t *data,
5078 size_t data_length )
5079{
5080 psa_status_t status;
5081 switch( step )
5082 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005083 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005084 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005085 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005086 status = psa_hmac_setup_internal( &hkdf->hmac,
5087 data, data_length,
5088 hash_alg );
5089 if( status != PSA_SUCCESS )
5090 return( status );
5091 hkdf->state = HKDF_STATE_STARTED;
5092 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005093 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005094 /* If no salt was provided, use an empty salt. */
5095 if( hkdf->state == HKDF_STATE_INIT )
5096 {
5097 status = psa_hmac_setup_internal( &hkdf->hmac,
5098 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005099 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005100 if( status != PSA_SUCCESS )
5101 return( status );
5102 hkdf->state = HKDF_STATE_STARTED;
5103 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005104 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005105 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005106 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5107 data, data_length );
5108 if( status != PSA_SUCCESS )
5109 return( status );
5110 status = psa_hmac_finish_internal( &hkdf->hmac,
5111 hkdf->prk,
5112 sizeof( hkdf->prk ) );
5113 if( status != PSA_SUCCESS )
5114 return( status );
5115 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5116 hkdf->block_number = 0;
5117 hkdf->state = HKDF_STATE_KEYED;
5118 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005119 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005120 if( hkdf->state == HKDF_STATE_OUTPUT )
5121 return( PSA_ERROR_BAD_STATE );
5122 if( hkdf->info_set )
5123 return( PSA_ERROR_BAD_STATE );
5124 hkdf->info_length = data_length;
5125 if( data_length != 0 )
5126 {
5127 hkdf->info = mbedtls_calloc( 1, data_length );
5128 if( hkdf->info == NULL )
5129 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5130 memcpy( hkdf->info, data, data_length );
5131 }
5132 hkdf->info_set = 1;
5133 return( PSA_SUCCESS );
5134 default:
5135 return( PSA_ERROR_INVALID_ARGUMENT );
5136 }
5137}
Janos Follathb03233e2019-06-11 15:30:30 +01005138
5139#if defined(PSA_PRE_1_0_KEY_DERIVATION)
5140static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5141 psa_algorithm_t hash_alg,
5142 psa_key_derivation_step_t step,
5143 const uint8_t *data,
5144 size_t data_length )
5145{
5146 (void) prf;
5147 (void) hash_alg;
5148 (void) step;
5149 (void) data;
5150 (void) data_length;
5151
5152 return( PSA_ERROR_INVALID_ARGUMENT );
5153}
Janos Follath6660f0e2019-06-17 08:44:03 +01005154
5155static psa_status_t psa_tls12_prf_psk_to_ms_input(
5156 psa_tls12_prf_key_derivation_t *prf,
5157 psa_algorithm_t hash_alg,
5158 psa_key_derivation_step_t step,
5159 const uint8_t *data,
5160 size_t data_length )
5161{
5162 (void) prf;
5163 (void) hash_alg;
5164 (void) step;
5165 (void) data;
5166 (void) data_length;
5167
5168 return( PSA_ERROR_INVALID_ARGUMENT );
5169}
Janos Follathb03233e2019-06-11 15:30:30 +01005170#else
Janos Follathf08e2652019-06-13 09:05:41 +01005171static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5172 const uint8_t *data,
5173 size_t data_length )
5174{
5175 if( prf->state != TLS12_PRF_STATE_INIT )
5176 return( PSA_ERROR_BAD_STATE );
5177
Janos Follathd6dce9f2019-07-04 09:11:38 +01005178 if( data_length != 0 )
5179 {
5180 prf->seed = mbedtls_calloc( 1, data_length );
5181 if( prf->seed == NULL )
5182 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005183
Janos Follathd6dce9f2019-07-04 09:11:38 +01005184 memcpy( prf->seed, data, data_length );
5185 prf->seed_length = data_length;
5186 }
Janos Follathf08e2652019-06-13 09:05:41 +01005187
5188 prf->state = TLS12_PRF_STATE_SEED_SET;
5189
5190 return( PSA_SUCCESS );
5191}
5192
Janos Follath81550542019-06-13 14:26:34 +01005193static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5194 psa_algorithm_t hash_alg,
5195 const uint8_t *data,
5196 size_t data_length )
5197{
5198 psa_status_t status;
5199 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5200 return( PSA_ERROR_BAD_STATE );
5201
5202 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5203 if( status != PSA_SUCCESS )
5204 return( status );
5205
5206 prf->state = TLS12_PRF_STATE_KEY_SET;
5207
5208 return( PSA_SUCCESS );
5209}
5210
Janos Follath6660f0e2019-06-17 08:44:03 +01005211static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5212 psa_tls12_prf_key_derivation_t *prf,
5213 psa_algorithm_t hash_alg,
5214 const uint8_t *data,
5215 size_t data_length )
5216{
5217 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005218 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5219 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005220
5221 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5222 return( PSA_ERROR_INVALID_ARGUMENT );
5223
5224 /* Quoting RFC 4279, Section 2:
5225 *
5226 * The premaster secret is formed as follows: if the PSK is N octets
5227 * long, concatenate a uint16 with the value N, N zero octets, a second
5228 * uint16 with the value N, and the PSK itself.
5229 */
5230
Janos Follath40e13932019-06-26 13:22:29 +01005231 *cur++ = ( data_length >> 8 ) & 0xff;
5232 *cur++ = ( data_length >> 0 ) & 0xff;
5233 memset( cur, 0, data_length );
5234 cur += data_length;
5235 *cur++ = pms[0];
5236 *cur++ = pms[1];
5237 memcpy( cur, data, data_length );
5238 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005239
Janos Follath40e13932019-06-26 13:22:29 +01005240 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005241
5242 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5243 return( status );
5244}
5245
Janos Follath63028dd2019-06-13 09:15:47 +01005246static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5247 const uint8_t *data,
5248 size_t data_length )
5249{
5250 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5251 return( PSA_ERROR_BAD_STATE );
5252
Janos Follathd6dce9f2019-07-04 09:11:38 +01005253 if( data_length != 0 )
5254 {
5255 prf->label = mbedtls_calloc( 1, data_length );
5256 if( prf->label == NULL )
5257 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005258
Janos Follathd6dce9f2019-07-04 09:11:38 +01005259 memcpy( prf->label, data, data_length );
5260 prf->label_length = data_length;
5261 }
Janos Follath63028dd2019-06-13 09:15:47 +01005262
5263 prf->state = TLS12_PRF_STATE_LABEL_SET;
5264
5265 return( PSA_SUCCESS );
5266}
5267
Janos Follathb03233e2019-06-11 15:30:30 +01005268static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5269 psa_algorithm_t hash_alg,
5270 psa_key_derivation_step_t step,
5271 const uint8_t *data,
5272 size_t data_length )
5273{
Janos Follathb03233e2019-06-11 15:30:30 +01005274 switch( step )
5275 {
Janos Follathf08e2652019-06-13 09:05:41 +01005276 case PSA_KEY_DERIVATION_INPUT_SEED:
5277 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005278 case PSA_KEY_DERIVATION_INPUT_SECRET:
5279 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005280 case PSA_KEY_DERIVATION_INPUT_LABEL:
5281 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005282 default:
5283 return( PSA_ERROR_INVALID_ARGUMENT );
5284 }
5285}
Janos Follath6660f0e2019-06-17 08:44:03 +01005286
5287static psa_status_t psa_tls12_prf_psk_to_ms_input(
5288 psa_tls12_prf_key_derivation_t *prf,
5289 psa_algorithm_t hash_alg,
5290 psa_key_derivation_step_t step,
5291 const uint8_t *data,
5292 size_t data_length )
5293{
5294 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005295 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005296 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5297 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005298 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005299
5300 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5301}
Janos Follathb03233e2019-06-11 15:30:30 +01005302#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005303#endif /* MBEDTLS_MD_C */
5304
Janos Follathb80a94e2019-06-12 15:54:46 +01005305static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005306 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005307 psa_key_derivation_step_t step,
5308 const uint8_t *data,
5309 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005310{
5311 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005312 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005313
5314#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005315 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005316 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005317 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005318 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005319 step, data, data_length );
5320 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005321 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005322#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005323#if defined(MBEDTLS_MD_C)
Janos Follath6660f0e2019-06-17 08:44:03 +01005324 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005325 {
Janos Follathb03233e2019-06-11 15:30:30 +01005326 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5327 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5328 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005329 }
5330 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5331 {
5332 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5333 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5334 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005335 }
5336 else
5337#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005338 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005339 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005340 return( PSA_ERROR_BAD_STATE );
5341 }
5342
5343 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005344 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005345 return( status );
5346}
5347
Janos Follath51f4a0f2019-06-14 11:35:55 +01005348psa_status_t psa_key_derivation_input_bytes(
5349 psa_key_derivation_operation_t *operation,
5350 psa_key_derivation_step_t step,
5351 const uint8_t *data,
5352 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005353{
Janos Follathc5621512019-06-14 11:27:57 +01005354 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5355 return( PSA_ERROR_INVALID_ARGUMENT );
5356
5357 return( psa_key_derivation_input_internal( operation, step,
5358 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005359}
5360
Janos Follath51f4a0f2019-06-14 11:35:55 +01005361psa_status_t psa_key_derivation_input_key(
5362 psa_key_derivation_operation_t *operation,
5363 psa_key_derivation_step_t step,
5364 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005365{
5366 psa_key_slot_t *slot;
5367 psa_status_t status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02005368 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005369 PSA_KEY_USAGE_DERIVE,
5370 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005371 if( status != PSA_SUCCESS )
5372 return( status );
5373 if( slot->type != PSA_KEY_TYPE_DERIVE )
5374 return( PSA_ERROR_INVALID_ARGUMENT );
5375 /* Don't allow a key to be used as an input that is usually public.
5376 * This is debatable. It's ok from a cryptographic perspective to
5377 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005378 * the material should be dedicated to a particular input step,
5379 * otherwise this may allow the key to be used in an unintended way
5380 * and leak values derived from the key. So be conservative. */
Gilles Peskine03410b52019-05-16 16:05:19 +02005381 if( step != PSA_KEY_DERIVATION_INPUT_SECRET )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005382 return( PSA_ERROR_INVALID_ARGUMENT );
Janos Follathb80a94e2019-06-12 15:54:46 +01005383 return( psa_key_derivation_input_internal( operation,
5384 step,
5385 slot->data.raw.data,
5386 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005387}
5388
Gilles Peskineea0fb492018-07-12 17:17:20 +02005389
5390
5391/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005392/* Key agreement */
5393/****************************************************************/
5394
Gilles Peskinea05219c2018-11-16 16:02:56 +01005395#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005396static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5397 size_t peer_key_length,
5398 const mbedtls_ecp_keypair *our_key,
5399 uint8_t *shared_secret,
5400 size_t shared_secret_size,
5401 size_t *shared_secret_length )
5402{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005403 mbedtls_ecp_keypair *their_key = NULL;
5404 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005405 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005406 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005407
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005408 status = psa_import_ec_public_key(
5409 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5410 peer_key, peer_key_length,
5411 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005412 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005413 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01005414
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005415 status = mbedtls_to_psa_error(
5416 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5417 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005418 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005419 status = mbedtls_to_psa_error(
5420 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5421 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005422 goto exit;
5423
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005424 status = mbedtls_to_psa_error(
5425 mbedtls_ecdh_calc_secret( &ecdh,
5426 shared_secret_length,
5427 shared_secret, shared_secret_size,
5428 mbedtls_ctr_drbg_random,
5429 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005430
5431exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005432 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00005433 mbedtls_ecp_keypair_free( their_key );
5434 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00005435 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005436}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005437#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005438
Gilles Peskine01d718c2018-09-18 12:01:02 +02005439#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5440
Gilles Peskine0216fe12019-04-11 21:23:21 +02005441static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5442 psa_key_slot_t *private_key,
5443 const uint8_t *peer_key,
5444 size_t peer_key_length,
5445 uint8_t *shared_secret,
5446 size_t shared_secret_size,
5447 size_t *shared_secret_length )
5448{
5449 switch( alg )
5450 {
5451#if defined(MBEDTLS_ECDH_C)
5452 case PSA_ALG_ECDH:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005453 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->type ) )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005454 return( PSA_ERROR_INVALID_ARGUMENT );
5455 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5456 private_key->data.ecp,
5457 shared_secret, shared_secret_size,
5458 shared_secret_length ) );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005459#endif /* MBEDTLS_ECDH_C */
5460 default:
5461 (void) private_key;
5462 (void) peer_key;
5463 (void) peer_key_length;
5464 (void) shared_secret;
5465 (void) shared_secret_size;
5466 (void) shared_secret_length;
5467 return( PSA_ERROR_NOT_SUPPORTED );
5468 }
5469}
5470
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005471/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine346797d2018-11-16 16:05:06 +01005472 * to potentially free embedded data structures and wipe confidential data.
5473 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005474static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005475 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01005476 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005477 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005478 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005479{
5480 psa_status_t status;
5481 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5482 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005483 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005484
5485 /* Step 1: run the secret agreement algorithm to generate the shared
5486 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005487 status = psa_key_agreement_raw_internal( ka_alg,
5488 private_key,
5489 peer_key, peer_key_length,
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005490 shared_secret,
5491 sizeof( shared_secret ),
5492 &shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005493 if( status != PSA_SUCCESS )
5494 goto exit;
5495
5496 /* Step 2: set up the key derivation to generate key material from
5497 * the shared secret. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005498 status = psa_key_derivation_input_internal( operation, step,
5499 shared_secret,
5500 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005501
Gilles Peskine01d718c2018-09-18 12:01:02 +02005502exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005503 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005504 return( status );
5505}
5506
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005507psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005508 psa_key_derivation_step_t step,
5509 psa_key_handle_t private_key,
5510 const uint8_t *peer_key,
5511 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005512{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005513 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005514 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005515 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005516 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005517 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005518 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005519 if( status != PSA_SUCCESS )
5520 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005521 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005522 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005523 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005524 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005526 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005527}
5528
Gilles Peskinebe697d82019-05-16 18:00:41 +02005529psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5530 psa_key_handle_t private_key,
5531 const uint8_t *peer_key,
5532 size_t peer_key_length,
5533 uint8_t *output,
5534 size_t output_size,
5535 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005536{
5537 psa_key_slot_t *slot;
5538 psa_status_t status;
5539
5540 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5541 {
5542 status = PSA_ERROR_INVALID_ARGUMENT;
5543 goto exit;
5544 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005545 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005546 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005547 if( status != PSA_SUCCESS )
5548 goto exit;
5549
5550 status = psa_key_agreement_raw_internal( alg, slot,
5551 peer_key, peer_key_length,
5552 output, output_size,
5553 output_length );
5554
5555exit:
5556 if( status != PSA_SUCCESS )
5557 {
5558 /* If an error happens and is not handled properly, the output
5559 * may be used as a key to protect sensitive data. Arrange for such
5560 * a key to be random, which is likely to result in decryption or
5561 * verification errors. This is better than filling the buffer with
5562 * some constant data such as zeros, which would result in the data
5563 * being protected with a reproducible, easily knowable key.
5564 */
5565 psa_generate_random( output, output_size );
5566 *output_length = output_size;
5567 }
5568 return( status );
5569}
Gilles Peskine01d718c2018-09-18 12:01:02 +02005570
5571
5572/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005573/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02005574/****************************************************************/
5575
5576psa_status_t psa_generate_random( uint8_t *output,
5577 size_t output_size )
5578{
itayzafrir0adf0fc2018-09-06 16:24:41 +03005579 int ret;
5580 GUARD_MODULE_INITIALIZED;
5581
5582 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02005583 return( mbedtls_to_psa_error( ret ) );
5584}
5585
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005586#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5587#include "mbedtls/entropy_poll.h"
avolinski13beb102018-11-20 16:51:49 +02005588
Gilles Peskine7228da22019-07-15 11:06:15 +02005589psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005590 size_t seed_size )
5591{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005592 if( global_data.initialized )
5593 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005594
5595 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5596 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5597 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5598 return( PSA_ERROR_INVALID_ARGUMENT );
5599
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005600 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005601}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005602#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005603
Gilles Peskinee56e8782019-04-26 17:34:02 +02005604#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5605static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5606 size_t domain_parameters_size,
5607 int *exponent )
5608{
5609 size_t i;
5610 uint32_t acc = 0;
5611
5612 if( domain_parameters_size == 0 )
5613 {
5614 *exponent = 65537;
5615 return( PSA_SUCCESS );
5616 }
5617
5618 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5619 * support values that fit in a 32-bit integer, which is larger than
5620 * int on just about every platform anyway. */
5621 if( domain_parameters_size > sizeof( acc ) )
5622 return( PSA_ERROR_NOT_SUPPORTED );
5623 for( i = 0; i < domain_parameters_size; i++ )
5624 acc = ( acc << 8 ) | domain_parameters[i];
5625 if( acc > INT_MAX )
5626 return( PSA_ERROR_NOT_SUPPORTED );
5627 *exponent = acc;
5628 return( PSA_SUCCESS );
5629}
5630#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5631
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005632static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005633 psa_key_slot_t *slot, size_t bits,
5634 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02005635{
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005636 psa_key_type_t type = slot->type;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005637
Gilles Peskinee56e8782019-04-26 17:34:02 +02005638 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005639 return( PSA_ERROR_INVALID_ARGUMENT );
5640
Gilles Peskine48c0ea12018-06-21 14:15:31 +02005641 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005642 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005643 psa_status_t status;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005644 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005645 if( status != PSA_SUCCESS )
5646 return( status );
5647 status = psa_generate_random( slot->data.raw.data,
5648 slot->data.raw.bytes );
5649 if( status != PSA_SUCCESS )
5650 {
5651 mbedtls_free( slot->data.raw.data );
5652 return( status );
5653 }
5654#if defined(MBEDTLS_DES_C)
5655 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005656 psa_des_set_key_parity( slot->data.raw.data,
5657 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005658#endif /* MBEDTLS_DES_C */
5659 }
5660 else
5661
5662#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005663 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005664 {
5665 mbedtls_rsa_context *rsa;
5666 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005667 int exponent;
5668 psa_status_t status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005669 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5670 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01005671 /* Accept only byte-aligned keys, for the same reasons as
5672 * in psa_import_rsa_key(). */
5673 if( bits % 8 != 0 )
5674 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005675 status = psa_read_rsa_exponent( domain_parameters,
5676 domain_parameters_size,
5677 &exponent );
5678 if( status != PSA_SUCCESS )
5679 return( status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005680 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5681 if( rsa == NULL )
5682 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5683 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5684 ret = mbedtls_rsa_gen_key( rsa,
5685 mbedtls_ctr_drbg_random,
5686 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01005687 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02005688 exponent );
5689 if( ret != 0 )
5690 {
5691 mbedtls_rsa_free( rsa );
5692 mbedtls_free( rsa );
5693 return( mbedtls_to_psa_error( ret ) );
5694 }
5695 slot->data.rsa = rsa;
5696 }
5697 else
5698#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5699
5700#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005701 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005702 {
5703 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5704 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5705 const mbedtls_ecp_curve_info *curve_info =
5706 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5707 mbedtls_ecp_keypair *ecp;
5708 int ret;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005709 if( domain_parameters_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005710 return( PSA_ERROR_NOT_SUPPORTED );
5711 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5712 return( PSA_ERROR_NOT_SUPPORTED );
5713 if( curve_info->bit_size != bits )
5714 return( PSA_ERROR_INVALID_ARGUMENT );
5715 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5716 if( ecp == NULL )
5717 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5718 mbedtls_ecp_keypair_init( ecp );
5719 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5720 mbedtls_ctr_drbg_random,
5721 &global_data.ctr_drbg );
5722 if( ret != 0 )
5723 {
5724 mbedtls_ecp_keypair_free( ecp );
5725 mbedtls_free( ecp );
5726 return( mbedtls_to_psa_error( ret ) );
5727 }
5728 slot->data.ecp = ecp;
5729 }
5730 else
5731#endif /* MBEDTLS_ECP_C */
5732
5733 return( PSA_ERROR_NOT_SUPPORTED );
5734
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005735 return( PSA_SUCCESS );
5736}
5737
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005738psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005739 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005740{
5741 psa_status_t status;
5742 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005743 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine011e4282019-06-26 18:34:38 +02005744 status = psa_start_key_creation( attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005745#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5746 if( driver != NULL )
5747 {
5748 /* Generating a key in a secure element is not implemented yet. */
5749 status = PSA_ERROR_NOT_SUPPORTED;
5750 }
5751#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005752 if( status == PSA_SUCCESS )
5753 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005754 status = psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005755 slot, attributes->bits,
5756 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005757 }
5758 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005759 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005760 if( status != PSA_SUCCESS )
5761 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005762 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005763 *handle = 0;
5764 }
5765 return( status );
5766}
5767
5768
Gilles Peskine05d69892018-06-19 22:00:52 +02005769
5770/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01005771/* Module setup */
5772/****************************************************************/
5773
Gilles Peskine5e769522018-11-20 21:59:56 +01005774psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5775 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5776 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5777{
5778 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5779 return( PSA_ERROR_BAD_STATE );
5780 global_data.entropy_init = entropy_init;
5781 global_data.entropy_free = entropy_free;
5782 return( PSA_SUCCESS );
5783}
5784
Gilles Peskinee59236f2018-01-27 23:32:46 +01005785void mbedtls_psa_crypto_free( void )
5786{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005787 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005788 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5789 {
5790 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005791 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005792 }
5793 /* Wipe all remaining data, including configuration.
5794 * In particular, this sets all state indicator to the value
5795 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005796 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005797#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005798 /* Unregister all secure element drivers, so that we restart from
5799 * a pristine state. */
5800 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005801#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005802}
5803
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005804#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5805/** Recover a transaction that was interrupted by a power failure.
5806 *
5807 * This function is called during initialization, before psa_crypto_init()
5808 * returns. If this function returns a failure status, the initialization
5809 * fails.
5810 */
5811static psa_status_t psa_crypto_recover_transaction(
5812 const psa_crypto_transaction_t *transaction )
5813{
5814 switch( transaction->unknown.type )
5815 {
5816 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5817 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
5818 /* TOnogrepDO - fall through to the failure case until this
5819 * is implemented */
5820 default:
5821 /* We found an unsupported transaction in the storage.
5822 * We don't know what state the storage is in. Give up. */
5823 return( PSA_ERROR_STORAGE_FAILURE );
5824 }
5825}
5826#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5827
Gilles Peskinee59236f2018-01-27 23:32:46 +01005828psa_status_t psa_crypto_init( void )
5829{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005830 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005831 const unsigned char drbg_seed[] = "PSA";
5832
Gilles Peskinec6b69072018-11-20 21:42:52 +01005833 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005834 if( global_data.initialized != 0 )
5835 return( PSA_SUCCESS );
5836
Gilles Peskine5e769522018-11-20 21:59:56 +01005837 /* Set default configuration if
5838 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5839 if( global_data.entropy_init == NULL )
5840 global_data.entropy_init = mbedtls_entropy_init;
5841 if( global_data.entropy_free == NULL )
5842 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005843
Gilles Peskinec6b69072018-11-20 21:42:52 +01005844 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005845 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005846 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005847 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005848 status = mbedtls_to_psa_error(
5849 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5850 mbedtls_entropy_func,
5851 &global_data.entropy,
5852 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5853 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005854 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005855 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005856
Gilles Peskine66fb1262018-12-10 16:29:04 +01005857 status = psa_initialize_key_slots( );
5858 if( status != PSA_SUCCESS )
5859 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005860
Gilles Peskine4b734222019-07-24 15:56:31 +02005861#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005862 status = psa_crypto_load_transaction( );
5863 if( status == PSA_SUCCESS )
5864 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005865 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5866 if( status != PSA_SUCCESS )
5867 goto exit;
5868 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005869 }
5870 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5871 {
5872 /* There's no transaction to complete. It's all good. */
5873 status = PSA_SUCCESS;
5874 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005875#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005876
Gilles Peskinec6b69072018-11-20 21:42:52 +01005877 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01005878 global_data.initialized = 1;
5879
Gilles Peskinee59236f2018-01-27 23:32:46 +01005880exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005881 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005882 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005883 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005884}
5885
5886#endif /* MBEDTLS_PSA_CRYPTO_C */